// This function checks for a valid date on the form
function CheckDate() {

	SelDay = document.forms.QueryForm.DepDay.options[document.forms.QueryForm.DepDay.selectedIndex].value;
	SelMonth = Math.abs((document.forms.QueryForm.DepMonthYear.options[document.forms.QueryForm.DepMonthYear.selectedIndex].value).slice(0,2));
	SelYear = (document.forms.QueryForm.DepMonthYear.options[document.forms.QueryForm.DepMonthYear.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.QueryForm.RetDay.options[document.QueryForm.RetDay.selectedIndex].value;
		SelMonth = Math.abs((document.QueryForm.RetMonthYear.options[document.QueryForm.RetMonthYear.selectedIndex].value).slice(0,2));
		SelYear = (document.QueryForm.RetMonthYear.options[document.QueryForm.RetMonthYear.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;
		}
	}
}

function dateAlter(qryform) 
{
	// 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.QueryForm.DepDay.selectedIndex
	// GET CHOSEN DEPARTURE MONTH&YEAR
	departMonthYear = document.QueryForm.DepMonthYear.value
	// GET CURRENT RETURN DAY
	returnDay = document.QueryForm.RetDay.selectedIndex

	// GET CURRENT RETURN MONTH&YEAR
	returnMonthYear = document.QueryForm.RetMonthYear.value
	

	// BUILD AN ARRAY OF THE MONTH&YEAR VALUES
	var MonthArray = new Array();
	
	for (i=thisMonth;i<=12;i++) {
		if (i < 10) {
			MonthArray[i] = thisYear.toString()+'0'+i.toString();
			
		} else {
			MonthArray[i] = thisYear.toString()+i.toString();
		}
	}
	
	for (i=1;i<thisMonth;i++) {
		if (i < 10) {
			MonthArray[i] = (parseInt(thisYear,10)+1).toString()+'0'+i.toString();
		} else {
			MonthArray[i] = (parseInt(thisYear,10)+1).toString()+i.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
	monthNumber = departMonthYear.substr(4,2);
	maxDays = DateArray[monthNumber-1];

	
	if (departDay + 14 >= maxDays){
		document.QueryForm.RetDay.selectedIndex = ((departDay-maxDays) + 14);
		if (departMonthYear.substr(4,2) == '12'){
			//thisYear = parseInt(thisYear,10)+1
			//departMonthYear = (parseInt(thisYear,10)+1).toString() + '01';
			document.QueryForm.RetMonthYear.value = MonthArray[1];
		} else {
			document.QueryForm.RetMonthYear.value = MonthArray[parseInt(departMonthYear.substr(4,2),10)+1];
		}
	}
	else if (departDay + 14 < maxDays){
		document.QueryForm.RetDay.selectedIndex = departDay + 14;
		document.QueryForm.RetMonthYear.value = MonthArray[parseInt(departMonthYear.substr(4,2),10)];
	}

}
	
function dateChange(qryform){
	
	dateToday = new Date();
	thisYear   = dateToday.getYear();
	thisMonth = dateToday.getMonth() + 1;
	thisDate   = dateToday.getDate();
	tomorrowDate = dateToday.getDate()+14;

	var MonthArray = new Array();
		
	for (i=thisMonth;i<=12;i++) {
			if (i < 10) {
				MonthArray[i] = thisYear.toString()+'0'+i.toString();
			} else {
				MonthArray[i] = thisYear.toString()+i.toString();
			}
		}
		
		for (i=1;i<thisMonth;i++) {
			if (i < 10) {
				MonthArray[i] = (thisYear+1).toString()+'0'+i.toString();
			} else {
				MonthArray[i] = (thisYear+1).toString()+i.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.QueryForm.DepDay.selectedIndex = ((thisDate-maxDays) + 13);
		if (thisMonth == 12){
		thisMonth = 00;
		}
		document.QueryForm.DepMonthYear.value = MonthArray[thisMonth+1];
		}
	else if (thisDate + 14 < maxDays){
		document.QueryForm.DepDay.selectedIndex = thisDate + 13;
		document.QueryForm.DepMonthYear.value = MonthArray[thisMonth];
		}
	else if (thisDate + 14 == maxDays){
		document.QueryForm.DepDay.selectedIndex = thisDate + 13;
		document.QueryForm.DepMonthYear.value = MonthArray[thisMonth];
		}

	if ((tomorrowDate > maxDays) || (tomorrowDate + 13 > maxDays)) {
		document.QueryForm.RetDay.selectedIndex = ((tomorrowDate-maxDays) + 12);
		if (thisMonth == 12){
		thisMonth = 00;
		}
		document.QueryForm.RetMonthYear.value = MonthArray[thisMonth+1];
		}
	else if (tomorrowDate < maxDays){
		document.QueryForm.RetDay.selectedIndex = tomorrowDate + 12;
		document.QueryForm.RetMonthYear.value = MonthArray[thisMonth];
	}
	}
	
	
//==============================================================================
// THIS SECTION GETS CURRENT DATE VALUES AND BUILDS ARRAY OF DATES FOR 12 MONTHS
//==============================================================================

dateToday = new Date();
thisYear   = dateToday.getFullYear();
thisMonth = dateToday.getMonth() + 1;
thisDate   = dateToday.getDate();
//tomorrowDate = dateToday.getDate()+14;

var MonthArray = new Array();

function writemonths() {

	for (i=thisMonth;i<=12;i++) {
			if (i < 10) {
				document.write ('<option value="' +thisYear.toString() + '0' + i.toString() + '">' + MonthName[i.toString()] + ' ' + (thisYear.toString()).slice(2) + '</option>');
			} else {
				document.write ('<option value="' +thisYear.toString() + i.toString() + '">'  + MonthName[i.toString()] + ' ' + (thisYear.toString()).slice(2) +  '</option>');
			}
	}

	for (i=1;i<thisMonth;i++) {
		if (i < 10) {
			document.write ('<option value="' + (thisYear+1).toString() + '0' + i.toString() + '">'  + MonthName[i.toString()] + ' ' + ((thisYear+1).toString()).slice(2) +  '</option>');
		} else {
			document.write ('<option value="' + (thisYear+1).toString() + i.toString() + '">'  + MonthName[i.toString()] + ' ' + ((thisYear+1).toString()).slice(2) +  '</option>');
		}
	}



}

var MonthName = new Array('','Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');