// INITIALISATION VALUES *******************
monthNames = new Array("January","February","March","April","May","June","July","August","September","October","November","December")
shortMonthNames = new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec")
daysInMonth = new Array( 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
now = new Date();
dayInMS = 24*60*60*1000;
weekInMS = 7*24*60*60*1000;
yearInMS = 365*24*60*60*1000;
//Set endDate var to be a week later than the startDate parameter
startDate = new Date(Date.parse(now) + dayInMS);
endDate = new Date(Date.parse(startDate) + weekInMS);
finalDate = new Date(Date.parse(startDate) + yearInMS);

var Today = new Date();
// END INITIALISATION VALUES *******************

// FUNCTIONS *******************
/*
	FUNCTION: DateOK -
	function takes year, month and day as numbers and validates whether date is valid or not. Copes with leap years.
	NOTE: the range of months is 0=January to 11=December!
	REQUIRES an array (in this example called daysInMonth) to hold the number of days expected in each month
	Code modified slightly from that found here: http://www.merlyn.demon.co.uk/js-date4.htm#TDVal
*/
function DateOK(Y, M, D) {
    var L = daysInMonth[M-1];
    return D > 0 &&
        !!L &&
        (D <= L || D == 29 && Y % 4 == 0 && (Y % 100 != 0 || Y % 400 == 0));
}

//FUNCTION: to2digits - ensures that single digit numbers are prefixed with a 0
function to2digits(no) {
	thestrNo = ""+no;
	return ((thestrNo.length==1) ? "0" : "")+thestrNo;
}

//FUNCTION: generateMonthOptions - generates form select options for a rolling 12 months starting with the month passed in 
// in the DateObject var.
function generateMonthOptions(DateObject,bShortMonth) {
	for (monthNo=DateObject.getMonth(); monthNo<12; monthNo++) {
		document.write('<OPTION value="' + (DateObject.getFullYear()) + to2digits(monthNo+1) + '"');
		if (monthNo == DateObject.getMonth()) 
			document.write(' SELECTED');
		if (typeof bShortMonth != "undefined" && bShortMonth == 'short') {
			document.write('>' + shortMonthNames[monthNo] + '&nbsp;' + to2digits((now.getFullYear()) - 2000) + '</OPTION>'); }
		else {
			document.write('>' + monthNames[monthNo] + '&nbsp;' + to2digits((now.getFullYear()) - 2000) + '</OPTION>');
		}
	}
	for (monthNo=0; monthNo<DateObject.getMonth(); monthNo++) {
		document.write('<OPTION value="' + (DateObject.getFullYear()+1) + to2digits(monthNo+1) + '"');
		if (monthNo == DateObject.getMonth())
			document.write(' SELECTED');
		if (typeof bShortMonth != "undefined" && bShortMonth == 'short') {
			document.write('>' + shortMonthNames[monthNo] + '&nbsp;' + to2digits((now.getFullYear()+1) - 2000) + '</OPTION>');
			}
		else {
			document.write('>' + monthNames[monthNo] + '&nbsp;' + to2digits((now.getFullYear()+1) - 2000) + '</OPTION>');
		}

	}
}

/* FUNCTION: generateDaysOptions
	This function creates 31 dropdown options for days of the month on travel search tools.
*/
function generateDaysOptions() {
	for (i=0;i<31;i++) {
		iVal = i+1;
		document.write( '<option value="' + iVal + '">' + to2digits(iVal) + '</option>');
	}
}

/*	FUNCTION: timeDifference
	Takes 2 params laterdate & earlier date and can give difference down to the second if required.
	Parameter syntax: 	
			laterdate = new Date(2000,0,1);     // 1st January 2000
			earlierdate = new Date(1998,2,13);  // 13th March 1998 
			diffReqd = d | h | m | s - if you only pass in a date (i.e. yyyy,mm,dd) then you will get 0 returned for the time 	
			comparisons. You have been warned!
*/
function timeDifference(laterdate,earlierdate,diffRqd) {
    var difference = laterdate.getTime() - earlierdate.getTime();
	var daysDifference = Math.floor(difference/1000/60/60/24);
    difference -= daysDifference*1000*60*60*24
    var hoursDifference = Math.floor(difference/1000/60/60);
    difference -= hoursDifference*1000*60*60
    var minutesDifference = Math.floor(difference/1000/60);
    difference -= minutesDifference*1000*60
    var secondsDifference = Math.floor(difference/1000);
	if(diffRqd == 'd' )	return daysDifference;
	if(diffRqd == 'h' )	return hoursDifference;
	if(diffRqd == 'm' )	return minutesDifference;
	if(diffRqd == 's' )	return secondsDifference;
}


/* FUNCTION: initValues
	Takes the name of the form as a parameter
*/
function initValues(formName) {
	try {
		document.forms[formName].iDepDay.selectedIndex = startDate.getDate()-1;
		document.forms[formName].iRetDay.selectedIndex = endDate.getDate()-1;
		var monIdx = endDate.getMonth() - startDate.getMonth();
		if (monIdx < 0 && endDate.getFullYear() > startDate.getFullYear()) monIdx = monIdx + 12;
		document.forms[formName].sRetMth.selectedIndex = monIdx;
	} catch (e) {  }
}


function callbackCalendar()	{
	if (startDate<=calendar.date && calendar.date<=finalDate)	{
		var dayIdx = calendar.date.getDate()-1;
		var startMonNum = startDate.getMonth();
		var monNum = calendar.date.getMonth();
		var monIdx = monNum - startMonNum;
		if (monIdx < 0 && calendar.date.getFullYear() > startDate.getFullYear()) monIdx = monIdx + 12;
		var dayFld = eval(calendar.custom.dayFld);
		var monFld = eval(calendar.custom.monFld);
		dayFld.selectedIndex = dayIdx;
		monFld.selectedIndex = monIdx;
	}	else	{
		alert('Please select a date between ' + displayDate(startDate) + ' and ' + displayDate(finalDate));
	}
}

function callbackTPCalendar()	{
	if (startDate<=calendar.date && calendar.date<=finalDate)	{
		var dayIdx = calendar.date.getDate()-1;
		var startMonNum = startDate.getMonth();
		var monNum = calendar.date.getMonth();
		var yearNum = new String(calendar.date.getFullYear());
		var monIdx = monNum - startMonNum;
		if (monIdx < 0 && calendar.date.getFullYear() > startDate.getFullYear()) monIdx = monIdx + 12;
		var dayFld = eval(calendar.custom.dayFld);
		var monFld = eval(calendar.custom.monFld);
		var yrFld = eval(calendar.custom.yrFld);
		dayFld.value = dayIdx+1;
		monFld.value = monNum+1;
		yrFld.value = yearNum.substring(2,4);
		
		//Update the holding fields
		updateFlightDates();
	}	
	else	{
		alert('Please select a date between ' + displayDate(startDate) + ' and ' + displayDate(finalDate));
	}
}

function updateFlightDates() {
//Convert to dates usable in our TP form and populate
	// Departure dates.
	var o_dday = document.forms['tp_flighttool']['iDepDay'];
	var o_dmon = document.forms['tp_flighttool']['sDepMth'];
	var o_dyr = document.forms['tp_flighttool']['sDepYr'];
	
	if (o_dday.value != '') {
		var s_dday = o_dday.value;
		var s_dmon = o_dmon.value;
		var s_dyr = o_dyr.value;
		
		// Make sure that the day values have preceeding 0's where single digits.
		if (s_dday.length == 1) {
		    s_dday = '0' + s_dday;
		}
		if (s_dmon.length == 1) {
		    s_dmon = '0' + s_dmon;
		}
		
		// Build the new param values (dd/mm/yy).
		var s_ddate = s_dday + '/' + s_dmon + '/' + s_dyr;
		// Update the hidden fields.
		document.forms['tp_flighttool']['ddate'].value = s_ddate;
		
	}
	
	// Return dates.
	var o_rday = document.forms['tp_flighttool']['iRetDay'];
	var o_rmon = document.forms['tp_flighttool']['sRetMth'];
	var o_ryr = document.forms['tp_flighttool']['sRetYr'];
	if (o_rday.value != '') {
		var s_rday = o_rday.value;
		var s_rmon = o_rmon.value;
		var s_ryr = o_ryr.value;
		// Make sure that the day values have preceeding 0's where single digits.
		if (s_rday.length == 1) {
		    s_rday = '0' + s_rday;
		}
		if (s_rmon.length == 1) {
		    s_rmon = '0' + s_rmon;
		}
		
		// Build the new param values (mm/dd/yy).
		var s_rdate = s_rday + '/' + s_rmon + '/' + s_ryr;
		
		// Debug - REMOVE!
		//alert('Departure date is: ' + s_ddate + ' ('+s_dmon+')');
		//alert('Return    date is: ' + s_rdate + ' ('+s_rmon+')');
		
		document.forms['tp_flighttool']['rdate'].value = s_rdate;
	}
}	
		
		
function displayDate(thisDate)	{
	var str = padZeros(thisDate.getDate()) + '/' + padZeros(thisDate.getMonth()+1) + '/' + thisDate.getFullYear();
	return str;
}

function padZeros(num)	{
	if (num<10)	{
		return '0' + num;
	}	else	{
		return num;
	}
}