function bookingSearchOnLoad(companionBooking)
{
	changeTripTypeRadioButton(document.bookingSearchBean.tripType); 
	changeDestinationCityList(getFormElement(document.bookingSearchBean, 'segmentDetails[0].originAirportCode'), getFormElement(document.bookingSearchBean, 'segmentDetails[0].destinationAirportCode'), navigator.userAgent.toLowerCase().indexOf('msie') == -1); 
	if (!companionBooking) {
		changeNumberOfInfants();
	}
	displayPointOfPurchase() 
}


function displaySearchCalendarText()
{
	setElementCalendarText(getFormElement(document.SBform,'sector_1_d_new'),
						       getFormElement(document.SBform,'sector_1_m_new'),'onwardText');
	setElementCalendarText(getFormElement(document.SBform,'sector_2_d_new'),
						       getFormElement(document.SBform,'sector_2_m_new'),'returnText');
}


function bookingStep1MultiCityOnLoad(companionBooking)
{
	initializeMultiSearchOnLoad();  
	if (!companionBooking) {
		changeNumberOfInfants();
	}
	displayPointOfPurchase() 
}

function initializeMultiSearchOnLoad() 
{
	changeDestinationCityList(getFormElement(document.bookingSearchBean, 'segmentDetails[0].originAirportCode'), getFormElement(document.bookingSearchBean, 'segmentDetails[0].destinationAirportCode'), navigator.userAgent.toLowerCase().indexOf('msie') == -1);
	changeDestinationCityList(getFormElement(document.bookingSearchBean, 'segmentDetails[1].originAirportCode'), getFormElement(document.bookingSearchBean, 'segmentDetails[1].destinationAirportCode'), navigator.userAgent.toLowerCase().indexOf('msie') == -1);
	changeDestinationCityList(getFormElement(document.bookingSearchBean, 'segmentDetails[2].originAirportCode'), getFormElement(document.bookingSearchBean, 'segmentDetails[2].destinationAirportCode'), navigator.userAgent.toLowerCase().indexOf('msie') == -1);
	changeDestinationCityList(getFormElement(document.bookingSearchBean, 'segmentDetails[3].originAirportCode'), getFormElement(document.bookingSearchBean, 'segmentDetails[3].destinationAirportCode'), navigator.userAgent.toLowerCase().indexOf('msie') == -1);
	changeDestinationCityList(getFormElement(document.bookingSearchBean, 'segmentDetails[4].originAirportCode'), getFormElement(document.bookingSearchBean, 'segmentDetails[4].destinationAirportCode'), navigator.userAgent.toLowerCase().indexOf('msie') == -1);
}

function changeTripTypeRadioButton(tripType) 
{	
	if(tripType[0].checked)
	{
		show('returnDateDivId');
	}
	else
	{
		hide('returnDateDivId');
	}
}

function changeTripTypeRadioButtonOpenJaw(tripType) 
{	
	if(tripType[0].checked || tripType[2].checked)
	{
		show('returnDateDivId');
	}
	else
	{
		hide('returnDateDivId');
	}
}

function changeTripTypeRadioButtonForDivs(tripType) 
{	
	if(tripType[0].checked)
	{
		show('returnDateDivIdLabel');
		show('returnDateDivId');
	}
	else
	{
		hide('returnDateDivIdLabel');
		hide('returnDateDivId');
	}
}
function changeTripTypeRadioButtonForDivsAndOpenJaw(tripType) 
{	
	if(tripType[0].checked || tripType[2].checked)
	{
		show('returnDateDivIdLabel');
		show('returnDateDivId');
	}
	else
	{
		hide('returnDateDivIdLabel');
		hide('returnDateDivId');
	}
}

function displayPointOfPurchase() 
{
	var showpurchaselocation=false
	for (i = 1; i < 6; i++)
	{
		if (document.getElementById("origin" + i) != null)
		{
			var origin = document.getElementById("origin" + i).options[document.getElementById("origin" + i).selectedIndex].value;
			var destination = document.getElementById("destination" + i).options[document.getElementById("destination" + i).selectedIndex].value;
			if (isUSAirportCode(origin))
			{
				showpurchaselocation=true;
				break;
			}
			if (isUSAirportCode(destination))
			{
				showpurchaselocation=true;
				break;
			}
		}
	}
	if (showpurchaselocation)
	{
		show("purchaselocation");
	}
	else
	{
		hide("purchaselocation");
		document.getElementById("pop").selectedIndex = 0;
	}
}		

function isUSAirportCode(airportCode)
{
	for(var i = 0; i < usAirportCodes.length; i++)
	{
		var usAirportCode = usAirportCodes[i];
		if(airportCode == usAirportCode)
		{
			return true;
		}
	}
	return false;
}

function changeDestinationCityList(fromList, toList, changeSelectedIndex) 
{
	fromListValue = fromList.options[fromList.selectedIndex].value;
	toListValue = toList.options[toList.selectedIndex].value;
	toList.length = eval(fromListValue + "_DESTS.length + 1;");
	index = 0;
	for (i = 0; i < toList.length - 1; i++) 
	{
		toList.options[i + 1].value = eval(fromListValue + "_DESTS[" + i + "]");
		for (j = 1; j < fromList.length; j++) 
		{
			if (fromList.options[j].value == toList.options[i + 1].value) 
			{
				toList.options[i + 1].text = fromList.options[j].text;
				break;
			}
		}
		if (toListValue == toList.options[i + 1].value) 
		{
			index = i + 1;
		}
	}
	if (changeSelectedIndex) 
	{
		toList.selectedIndex = index;
	}
}

function changeNumberOfInfants() 
{
	var infantsSelect = document.getElementById("numinfants");
	var numGuests = document.getElementById("numguests").selectedIndex + 1;
	if(infantsSelect.options.length > numGuests){
		for(i = infantsSelect.options.length; i > numGuests; i--)
		{
			infantsSelect.remove(i)
		}
	}
	else if(infantsSelect.options.length <= numGuests)
	{
		for(i = infantsSelect.options.length; i <= numGuests && i < 5; i++)
		{
			var optionInfant = document.createElement("option");
			optionInfant.text=i+ " Infants";
			optionInfant.value=i;
			try
			{
				infantsSelect.add(optionInfant, null);
			}
			catch(ex)
			{
				infantsSelect.add(optionInfant);
			}
		}
	}
}
function getFormElement(form, elementName) 
{
	return form.elements[elementName];
}

function bookingSearchValidation()
{
	
    var onwardDateIsValid = validateDate('sector_1_d_new', 'sector_1_m_new','onwardText');
	var returnDateIsValid = true;
	var hasOpenJaw = document.SBform.travel_type[2] != undefined;
	var isReturnTrip = document.SBform.travel_type[0].checked;
	if(hasOpenJaw)
	{
		isReturnTrip = isReturnTrip || document.SBform.travel_type[2].checked;
	}
	var isvalid = true;
	if( isReturnTrip )
	{
		returnDateIsValid = validateDate('sector_2_d_new', 'sector_2_m_new','returnText');
	}
	
	if(onwardDateIsValid && returnDateIsValid)
	{
		
	}
	else
	{
		bookingsearch_doValidationFormatting(true, true, onwardDateIsValid, returnDateIsValid);
		isvalid = false;
	}
	if(isvalid)
	{
		westJetValidation();
	}
}

function bookingsearch_doValidationFormatting(originAirportIsValid, destinationAirportIsValid, onwardDateIsValid, returnDateIsValid)
{
	/*
	var whereAreYouGoingLabel = getElementById('where.are.you.going.label');
	var departingDateLabel = getElementById('departing.date.label');
	var returnDateLabel = getElementById('return.date.label');
    whereAreYouGoingLabel.style.color = '000066';
    departingDateLabel.style.color = '000066';	
    returnDateLabel.style.color = '000066';	
    */
	hide('booking.error.required.departure.date');
	hide('booking.error.required.return.date');
	if(!originAirportIsValid)
	{
		show('booking.error.departure.city.required');
	}
	if(!destinationAirportIsValid)
	{
		show('booking.error.destination.city.required');
	}
    if(!originAirportIsValid || !destinationAirportIsValid)
    {
    	whereAreYouGoingLabel.style.color = 'red';
    }
    if(!onwardDateIsValid)
    {
    	show('booking.error.required.departure.date');
    	//departingDateLabel.style.color = 'red';
    }
	if(!returnDateIsValid)
	{
		show('booking.error.required.return.date');
		//returnDateLabel.style.color = 'red';
	}
	show('search_validation_spacer');
	show('search_error_messages');
}

function validateDate(dateInputName, monthInputName, divName)
{
    var dateInput = getFormElement(document.SBform, dateInputName);
    var monthInput = getFormElement(document.SBform, monthInputName);
    var div = document.getElementById(divName);
    var dateIsValid = div.innerHTML != "&nbsp;";
    /*
    var dateInputValue = dateInput.value;
    var monthInputValue = monthInput.value;
   	var dateIsValid = dateInputValue != dd && dateInputValue != "" && monthInputValue != mm && monthInputValue != "";
    return dateIsValid;
    */
    return dateIsValid;
}

function populateComboFromField(combobox, inputField, isOrigin)
{
	var selectedAirportCode = getAirportCodeByAirportDisplay(inputField.value);
	if(selectedAirportCode != null)
	{
		if(isOrigin)
		{
			combobox.value = selectedAirportCode;
		}
		else 
		{
			combobox.value = selectedAirportCode;
		}	
	}
	else 
	{
		combobox.value = "XXX";
	}
	
}

function populateCombosInStepOne(airportList)
{
	airports = airportList;	
	populateComboFromField(document.SBform.sector1_o, document.SBform.origin, true);
	populateComboFromField(document.SBform.sector1_d, document.SBform.destination, false);
	if(document.SBform.travel_type[2].checked) 
	{
		populateComboFromField(document.SBform.sector2_o, document.SBform.origin2, true);
		populateComboFromField(document.SBform.sector2_d, document.SBform.destination2, false);
	}	
	
}

function populateCombosForFrenchTA(airportList)
{
	airports = airportList;	
	populateComboFromField(document.SBform.sector1_o, document.SBform.origin, true);
	populateComboFromField(document.SBform.sector1_d, document.SBform.destination, false);
	
}


function changeTripTypeRadioButtonForWidget(tripType) 
{	
	if(tripType[0].checked)
	{
		show('returnDateDivId');
		hide('openJawId');
	}
	else if(tripType[1].checked)
	{
		hide('returnDateDivId');
		hide('openJawId');
	}
	else 
	{
		show('returnDateDivId');
		show('openJawId');
	}
}
function getFormElement(form, elementName) 
{
	return form.elements[elementName];
}
function validateWidget(bookingForm)
{
	var origin1 = getFormElement(bookingForm, 'segmentDetails[0].originAirportCode').value;
	var destination1 = getFormElement(bookingForm, 'segmentDetails[0].destinationAirportCode').value;	
	if(origin1.length==0 || origin1 == "XXX" || origin1 == "null")
	{
		alert(enterOrigin);
		return false;
	}
	if(destination1.length==0 || destination1 == "XXX" || destination1 == "null")
	{
		alert(enterDestination);
		return false;
	}
	if(bookingForm.tripType[2].checked)
	{
		var origin2 = getFormElement(bookingForm, 'segmentDetails[1].originAirportCode').value;
		var destination2 = getFormElement(bookingForm, 'segmentDetails[1].destinationAirportCode').value;	
		if(origin2.length == 0 || origin2 == "XXX" || origin2 == "null")
		{
			alert(enterOrigin);
			return false;
		}
		if(destination2.length==0 || destination2 == "XXX" || destination2 == "null")
		{
			alert(enterDestination);
			return false;
		}		
	}
	var onwardDate;
	var returnDate;
	if(document.getElementById('onwardText').innerHTML.length < 10)
	{
		alert(enterOnwardDate);
		return false;
	}
	else 
	{
		onwardDate = parseMonthDate(getFormElement(bookingForm, 'segmentDetails[0].onwardMonth').value,
			getFormElement(bookingForm, 'segmentDetails[0].onwardDate').value, new Date());
	}
	if(!bookingForm.tripType[1].checked)
	{
		if(document.getElementById('returnText').innerHTML.length < 10)
		{
			alert(enterReturnDate);
			return false;
		}
		else 
		{
			returnDate = parseMonthDate(bookingForm.returnMonth.value, bookingForm.returnDate.value, new Date());
		}	
	}
	if(!validateDates(onwardDate, returnDate))
	{
		return false;
	}
	if(bookingForm.tripType[2].checked &&
		onwardDate.getTime() == returnDate.getTime() &&
		origin1 != destination2)
	{
		alert(sameDayOpenJawMessage);
		return false;
	}
	if(onwardDate.getTime() == today.getTime())
	{
		alert(currentDayMessage);
	}
	return true;
	
}

function withinMaxBookingDate(date)
{
	if(maxDate.getTime() < date.getTime())
	{
		var maxDateString = dayNameArray[maxDate.getDay()] + ', '+ maxDate.getDate() + ' ' + monthNameArray[maxDate.getMonth()] + ' ' + maxDate.getFullYear();
		alert(maxDateExceededMessage.replace('{0}', maxDateString));
		return false;
	}
	return true;
}

function validateDates(onwardDate, returnDate)
{	
	if(today.getTime() > onwardDate.getTime())
	{
		alert(pastDateMessage);
		return false;
	}
	if(!withinMaxBookingDate(onwardDate))
	{
		return false;
	}
	if(returnDate != null)
	{
		if(returnDate.getTime() < onwardDate.getTime())
		{
			alert(returnDateEarlierMessage);
			return false;
		}
		if(!withinMaxBookingDate(returnDate))
		{
			return false;
		}	
	}
	
	return true;
}
