// This function checks for a valid date on the form
function CheckDate() {
	
	SelDay = document.forms.MainForm.Day.options[document.forms.MainForm.Day.selectedIndex].value;
	l = (document.forms.MainForm.month_1.options[document.forms.MainForm.month_1.selectedIndex].value).length;
	if (l==7)
	{
		SelMonth = (document.forms.MainForm.month_1.options[document.forms.MainForm.month_1.selectedIndex].value).slice(0,2);
		SelYear = (document.forms.MainForm.month_1.options[document.forms.MainForm.month_1.selectedIndex].value).slice(3,7);
	}
	else // if l==6
	{
		SelMonth = (document.forms.MainForm.month_1.options[document.forms.MainForm.month_1.selectedIndex].value).slice(0,1);
		SelYear = (document.forms.MainForm.month_1.options[document.forms.MainForm.month_1.selectedIndex].value).slice(2,6);
	}
	
	LeapYear = SelYear % 4;

	if (LeapYear == 0) {
		DateArray = new Array(0,31,29,31,30,31,30,31,31,30,31,30,31);
	} else {
		DateArray = new Array(0,31,28,31,30,31,30,31,31,30,31,30,31);
	}

	if (SelDay > DateArray[SelMonth]) {
		alert("You have selected an invalid departure date. Please check and try again");
		return false;
	} else {
		// DEP DATE IS OK - CHECK RETURN DATE
		SelDay = document.MainForm.RetDay.options[document.MainForm.RetDay.selectedIndex].value;
		SelMonth = Math.abs((document.MainForm.month_2.options[document.MainForm.month_2.selectedIndex].value).slice(0,2));
		SelYear = (document.MainForm.month_2.options[document.MainForm.month_2.selectedIndex].value).slice(2,6);

		LeapYear = SelYear % 4;

		if (LeapYear == 0) {
			DateArray = new Array(0,31,29,31,30,31,30,31,31,30,31,30,31);
		} else {
			DateArray = new Array(0,31,28,31,30,31,30,31,31,30,31,30,31);
		}

		if (SelDay > DateArray[SelMonth])
		{
			alert("You have selected an invalid return date. Please check and try again");
			return false;
		}
		else
		{
			// DATES CHECK OUT OK
			return true;
		}
	}
}

// Added for job 13625 (though not used)
function dateAlterCalendar()
{
	if (document.MainForm.DepartDate.value.length > 0)
	{
		var departDate = new Date(getDateFromFormat(document.MainForm.DepartDate.value, "dd/MM/yyyy"));
		
		if (departDate != 0)
		{
			var returnDate = new Date();
	
			returnDate.setFullYear(departDate.getFullYear());
			returnDate.setMonth(departDate.getMonth());
			returnDate.setDate(departDate.getDate() + 14);
		}
	
		document.MainForm.ReturnDate.value = formatDate(returnDate, "dd/MM/yyyy");
	}
}

function dateAlter()
{
	// GET TODAYS DATE
	dateToday = new Date();
	// GET CURRENT MONTH
	thisMonth = dateToday.getMonth() + 1;
	// GET CURRENT DAY
	thisDate   = dateToday.getDate();
	thisYear   = dateToday.getFullYear();

	// GET CHOSEN DEPARTURE DAY
	departDay = document.MainForm.Day.selectedIndex
	// GET CHOSEN DEPARTURE MONTH&YEAR
	departMonthYear = document.MainForm.month_1.value
	// GET CURRENT RETURN DAY
	returnDay = document.MainForm.RetDay.selectedIndex
	// GET CURRENT RETURN MONTH&YEAR
	returnMonthYear = document.MainForm.month_2.value
	

	// BUILD AN ARRAY OF THE MONTH&YEAR VALUES
	var MonthArray = new Array();
	
	for (i=thisMonth;i<=12;i++) {
			MonthArray[i] = i.toString()+'-'+thisYear.toString();
	}
	
	for (i=1;i<thisMonth;i++) {
			MonthArray[i] = i.toString()+'-'+(parseInt(thisYear,10)+1).toString();
	}
	
	// NOW FIND OUT HOW MANY DAYS ARE IN EACH MONTH DEPENDING ON WHETHER THE CURRENT YEAR IS A LEAP YEAR OR NOT
	if (thisYear % 4 == 0){
		var DateArray = new Array(31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	}
	else {
		var DateArray = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	}
	
	// GET CHOSEN DEPARTURE MONTH NUMBER & MAX DAYS IN THAT MONTH
	if (departMonthYear.length==7)
		monthNumber = departMonthYear.substr(0,2);
	else
		monthNumber = departMonthYear.substr(0,1);

	maxDays = DateArray[monthNumber-1];
	if (departDay + 14 >= maxDays){
		document.MainForm.RetDay.selectedIndex = ((departDay-maxDays) + 14);
		if (departMonthYear.substr(0,2) == '12'){
			//thisYear = parseInt(thisYear,10)+1
			//departMonthYear = (parseInt(thisYear,10)+1).toString() + '01';
			document.MainForm.month_2.value = MonthArray[1];
		} else {
			document.MainForm.month_2.value = MonthArray[parseInt(departMonthYear.substr(0,2),10)+1];
		}
	}
	else if (departDay + 14 < maxDays){
		document.MainForm.RetDay.selectedIndex = departDay + 14;
		document.MainForm.month_2.value = MonthArray[parseInt(departMonthYear.substr(0,2),10)];
	}
}

// Added for job 13625 (though not used)
function dateChangeCalendar(form)
{
	if (form.DepartDate.value.length == 0 &&
		form.ReturnDate.value.length == 0)
	{
		var departDate = new Date();
		
		departDate.setDate(departDate.getDate() + 13);
	
		var returnDate = new Date();
	
		returnDate.setFullYear(departDate.getFullYear());
		returnDate.setMonth(departDate.getMonth());
		returnDate.setDate(departDate.getDate() + 14);
	
		form.DepartDate.value = formatDate(departDate, "dd/MM/yyyy");
		form.ReturnDate.value = formatDate(returnDate, "dd/MM/yyyy");
	}
}

function dateChange(){
	dateToday = new Date();
	thisYear   = dateToday.getYear();
	thisMonth = dateToday.getMonth() + 1;
	thisDate   = dateToday.getDate();
	tomorrowDate = dateToday.getDate()+14;

	var MonthArray = new Array();


	//By Kamran
	//Firefox getYears() returns year-1900
	if (navigator.userAgent.indexOf("Firefox")!=-1)
		thisYear=1900+thisYear;

	for (i=thisMonth;i<=12;i++) {
				MonthArray[i] = i.toString()+'-'+thisYear.toString();
		}
		
		for (i=1;i<thisMonth;i++) {
				MonthArray[i] = i.toString()+'-'+(thisYear+1).toString();
	}
	
	if (thisYear % 4 == 0){
		var DateArray = new Array(31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	}
	else {
		var DateArray = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	}

	maxDays    = DateArray[thisMonth-1];
	if (thisDate + 14 > maxDays){
		document.MainForm.Day.selectedIndex = ((thisDate-maxDays) + 13);
		if (thisMonth == 12){
		thisMonth = 0;
		}
		document.MainForm.month_1.value = MonthArray[thisMonth+1];
		}
	else if (thisDate + 14 < maxDays){
		document.MainForm.Day.selectedIndex = thisDate + 13;
		document.MainForm.month_1.value = MonthArray[thisMonth];
		}
	else if (thisDate + 14 == maxDays){
		document.MainForm.Day.selectedIndex = thisDate + 13;
		document.MainForm.month_1.value = MonthArray[thisMonth];
		}

	if ((tomorrowDate > maxDays) || (tomorrowDate + 14 > maxDays)) {
		document.MainForm.RetDay.selectedIndex = ((tomorrowDate-maxDays) + 13);
		if (thisMonth == 12){
		thisMonth = 00;
		}
		document.MainForm.month_2.value = MonthArray[thisMonth+1];
		}
	else if (tomorrowDate < maxDays){
		document.MainForm.RetDay.selectedIndex = tomorrowDate + 13;
		document.MainForm.month_2.value = MonthArray[thisMonth];
	}
}

	
	
//==============================================================================
// THIS SECTION GETS CURRENT DATE VALUES AND BUILDS ARRAY OF DATES FOR 12 MONTHS
//==============================================================================

dateToday = new Date();
thisYear   = dateToday.getFullYear();
nextYear	 = dateToday.getFullYear() + 1;
thisMonth = dateToday.getMonth() + 1;
thisDate   = dateToday.getDate();
//tomorrowDate = dateToday.getDate()+14;

var MonthArray = new Array();

function writemonths() {

	for (i=thisMonth;i<=12;i++) {
				document.write ('<option value="' +i.toString() + '-' + thisYear.toString() + '">'  + MonthName[i.toString()] + ' ' + (thisYear.toString()).slice(2) +  '</option>');
	}

	for (i=1;i<thisMonth;i++) {
				document.write ('<option value="' +i.toString() + '-' + (nextYear).toString() + '">'  + MonthName[i.toString()] + ' ' + (nextYear.toString()).slice(2) +  '</option>');
	}



}

var MonthName = new Array('','Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
