var todayDifference = 12 - (2);
var todayYear = 2005;
var todayDay = 18;

function validateSearchRequest( originPage )
{

	var flightType = document.getElementById("flightType").value;
	setUpDates( originPage );
	// Dates checked in JSP

	if( flightType = 'multi'  || checkAirports() )
	{
		document.SearchForm.submit();
		showWaiting(0);

	}else{

		return true;
	}
}

function checkAirports()
{

	//  Doesn't check multi
	if (!document.SearchForm)
		return false;
	var from = document.getElementById("FlightLeg1").value;
	var to = document.getElementById("FlightLeg2").value;
	if( from == "" || to == "" || from == to)
	{
		alert( 'You must choose a valid origin destination pair' );
		return false;
	}
	else{
		return true;
		}
}
function setUpDates( originPage )
{


	//originPage should be either home or modify indicating where the method was called from
	var flightType = document.getElementById("flightType").value;

	if (!document.SearchForm)
		return false;

	//  Set the year input according to the month input
	if( (originPage == 'home' && flightType != 'multi') || originPage == 'modify'  && flightType != 'multi')
	{

		//  Even after a multi search the oneway/return input is shown on the modify search
		setYear( document.SearchForm.DepartDay, document.SearchForm.DepartMonth, document.SearchForm.DepartYear, todayDifference, todayDay, todayYear );
	}
	if( (originPage == 'home' && flightType == 'return') || originPage == 'modify' && flightType == 'return' )
	{
		//  Even after a multi search the oneway/return input is shown on the modify search
		setYear( document.SearchForm.ReturnDay, document.SearchForm.ReturnMonth, document.SearchForm.ReturnYear, todayDifference, todayDay, todayYear );
	}
	if( originPage == 'home' && flightType == 'multi')
	{
	alert('1' + document.SearchForm.multiDepartDay1);
	alert('2 ' + document.SearchForm.multiDepartMonth1 );
		setYear( document.SearchForm.multiDepartDay1, document.SearchForm.multiDepartMonth1, document.SearchForm.multiDepartYear1, todayDifference, todayDay, todayYear );
		setYear( document.SearchForm.multiDepartDay2, document.SearchForm.multiDepartMonth2, document.SearchForm.multiDepartYear2, todayDifference, todayDay, todayYear );
		setYear( document.SearchForm.multiDepartDay3, document.SearchForm.multiDepartMonth3, document.SearchForm.multiDepartYear3, todayDifference, todayDay, todayYear );
		setYear( document.SearchForm.multiDepartDay4, document.SearchForm.multiDepartMonth4, document.SearchForm.multiDepartYear4, todayDifference, todayDay, todayYear );
	}
}
function setYear( day, month, year, todayDifference, todayDay, todayYear )
{

	//  Set the year input according to the month input
	var theMonth = month.value;
	var difference = 12 - theMonth;

	if( todayDifference > difference ){
		//  This Year
		 year.value = todayYear;
	}else if( todayDifference = difference){
		//  This month so check whether the date is before or after the current date, if it is before then the year is next year
		//  If the dates are the same then we'll assume it is this year - May need to check time etc
		var day = day.value;
		if( day < todayDay ){
			//  Next Year
			 year.value = todayYear + 1;
		}else{
			//  This Year
			year.value = todayYear;
		}
	}else
	{
		//  Next Year
		year.value = todayYear + 1;
	}
}


// Optional event sink for the OJCal
function DepartCalEvent() {
	onDateChange(SearchForm);
}

//Prevent selection of Dearture Dates greater than Return Dates and vice versa
function onDateChange(form)
{

	updateHiddenYear();

	departMonthIndex = document.getElementById("DepartMonth");
	returnMonthIndex = document.getElementById("ReturnMonth");
	departDayIndex = document.getElementById("DepartDay");
	returnDayIndex = document.getElementById("ReturnDay");

	if (returnMonthIndex.selectedIndex < departMonthIndex.selectedIndex)
	{
		returnMonthIndex.selectedIndex  = departMonthIndex.selectedIndex;
		returnDayIndex.selectedIndex = departDayIndex.selectedIndex;

	}
	else if (departMonthIndex.selectedIndex > returnMonthIndex.selectedIndex)
	{
		departMonthIndex.selectedIndex = returnMonthIndex.selectedIndex;
		departDayIndex.selectedIndex = returnDayIndex.selectedIndex;
	}
	else if (departDayIndex.selectedIndex > returnDayIndex.selectedIndex &&
		returnMonthIndex.selectedIndex == departMonthIndex.selectedIndex)
	{
		returnDayIndex.selectedIndex = departDayIndex.selectedIndex;
	}
	else if (returnDayIndex.selectedIndex < departDayIndex.selectedIndex &&
		returnMonthIndex.selectedIndex == departMonthIndex.selectedIndex)
	{
		departDayIndex.selectedIndex = returnDayIndex.selectedIndex;
	}
}

// Update hidden year after changing month
function updateHiddenYear()
{

	var now = new Date();

	if ((now.getMonth()+1) > document.getElementById("DepartMonth").value) {
		document.getElementById("DepartYear").value = now.getYear() + 1;
	}
	else {
		document.getElementById("DepartYear").value = now.getYear();
	}

	if ((now.getMonth()+1) > document.getElementById("ReturnMonth").value) {
		document.getElementById("ReturnYear").value = now.getYear() + 1;
	}
	else {
		document.getElementById("ReturnYear").value = now.getYear();
	}
}






var today = new Date();
var calWin;

var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];

var days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];

// input: Base name of form fields
// indate: date currently set in form
// range: Number of months in future to allow
// left, top: offsets of calendar window within parent
function OJCal(input, indate, range, left, top)
{
	if((indate == null) || (indate == ""))
		indate = today;
	else
	{
		i = indate.indexOf('-');
		day = indate.substring(0, i);
		rest = indate.substring(i + 1);
		j = rest.indexOf('-');
		month = rest.substring(0, j);
		year = rest.substring(j + 1);
		indate = new Date(year, month, day);
		indate.setMonth(indate.getMonth() - 1);
	}
	prevDate = new Date(indate);
	// Don't allow scroll back past current month
	if((today.getYear() < indate.getYear()) || ((today.getYear() == indate.getYear()) && (today.getMonth() < indate.getMonth())))
		prevDate.setMonth(indate.getMonth() - 1);

	nextDate = new Date(indate);
	// Don't allow scroll forward more than 'range' months
	testDate = new Date(today);
        if(range > 0)
            testDate.setMonth(testDate.getMonth() + range);
	if((indate.getYear() < testDate.getYear()) || ((indate.getYear() == testDate.getYear()) && (indate.getMonth() < testDate.getMonth())))
		nextDate.setMonth(nextDate.getMonth() + 1);
		
	nextMonthDate = new Date(indate);
	nextMonthDate.setMonth(nextMonthDate.getMonth() + 1);

	calWin = window.open("OJCal", "OJCal", "height=175 width=240 toolbar=0 menubar=0 status=0 fullscreen=no left=" + left + " top=" + top);
	calDoc = calWin.document;
	calDoc.open();
	
				calDoc.writeln('<html dir="ltr">');
	calDoc.writeln("<head><title>xRez Calendar</title>");
	calDoc.writeln("<LINK href='/includes/openjaw_cal.css' rel='stylesheet' type='text/css'/>");
	calDoc.writeln("</head><body>");
	calDoc.writeln("<table width='100%' cellspacing='0'>");
	calDoc.writeln("<tr class='calHeader' >");
	calDoc.write("<td><a href=\"javascript:window.opener.OJCal('" + input + "', '" + date2Str(prevDate) + "', " + range + ");\" class='calHeader' ><<</a></td>");
	calDoc.writeln("<td colspan='5'>" + months[indate.getMonth()] + " " + indate.getYear() + "</td>");
	calDoc.write("<td><a href=\"javascript:window.opener.OJCal('" + input + "', '" + date2Str(nextDate) + "', " + range + ");\" class='calHeader' >>></a></td>");
	calDoc.writeln("</tr><tr class='dayName'>");

	for(var i = 0; i < days.length; i++)
		calDoc.write("<td class='dayNameTD'>" + days[i] +"</td>");
	calDoc.writeln("</tr>");
	
	day = new Date(indate);
	day.setDate(1);
	
	if((day.getMonth() == today.getMonth()) && (day.getYear() == today.getYear()))
		todayDay = today.getDate();
	else
		todayDay = 0;

	day.setDate(1-(7+day.getDay())%7);

	pastToday = false;

	while(day.getMonth() != nextMonthDate.getMonth())
	{
		calDoc.writeln("<tr >");
		
		for(i = 0; i < 7; i++)
		{
			calDoc.write("<td class='dateTD'>");

				var dayOfMonth = day.getDate();
				if(dayOfMonth < 10)
					dayOfMonth = "0" + dayOfMonth;
				var month = day.getMonth() + 1;
				
				if( indate.getMonth() == (month - 1))
				{
					
					if((day.getDate() >= todayDay) || pastToday)
					{
						pastToday = true;
						//  Different for a multi search
						if( input.match('multi') == 'multi' )
						{
							// Get the index
							index = input.substring(5,6);
							calDoc.writeln("<a href=\"javascript:window.opener.document.getElementById('multiDepartDay" + index +"').value = '" + dayOfMonth + "';window.opener.document.getElementById('multiDepartMonth" + index + "').value = '" + month + "';window.opener.document.getElementById('departYear" + index + "').value ='" + day.getYear() + "';window.close();\">");
						}
						if( input.match('multi') != 'multi' )
						{
							calDoc.writeln("<a href=\"javascript:window.opener.document.getElementById('" + 
								input + "Day').value = '" + dayOfMonth + "';window.opener.document.getElementById('" +
								input + "Month').value = '" + month + "';window.opener.document.getElementById('" +
								input + "Year').value ='" + day.getYear() + "';" + 
								"if (window.opener." + input + "CalEvent) window.opener." + input + "CalEvent();" +
								"window.close();\">");
						}
						calDoc.write("<font class='");
						if(day.getDate() == todayDay)
							calDoc.write("today");
						calDoc.write("'>" + day.getDate());
						calDoc.write("</font>");
						calDoc.writeln("</a>");
						
					}
					else
						calDoc.write(day.getDate());
				}else{
					calDoc.writeln('<img border="0" height="1" width="1" src="/gohopImages/c.gif">');
				}
				
			calDoc.writeln("</td>");
			day.setDate(day.getDate() + 1);
		}
		calDoc.writeln("</tr>");
	}
	calDoc.writeln("</table>");
	calDoc.writeln("</body></html>");
	calDoc.close();
}

function date2Str(indate)
{
	return(indate.getDate() + "-" + (indate.getMonth() + 1) + "-" + indate.getYear());
}