// Global variables


var tab4Args = "";

//Global variables for search Id & Itineary Id
var mapItSearchId;
var mapItItinId;
var saleLeftIndx=0;
var saleLeftTimeOut;

var currentFlight=""; //current input id for flights' airport completer 


// ChangeFindFlightMode()
// --------------------------------------------------------------------------------
// Change the find a flight box between RT, OW, and MC
//
function ChangeFindFlightMode(changeTo, pageModuleImpId)
{
	
	if (changeTo == 'RT') {
		LoadAjaxContent('findAFlight', '/modules/findAFlight/findAFlightRT.jsp', 'pageModuleImpId=' + pageModuleImpId);
	}
	else if (changeTo == 'OW') {
		LoadAjaxContent('findAFlight', '/modules/findAFlight/findAFlightOW.jsp', 'pageModuleImpId=' + pageModuleImpId);
	}
	else {
		LoadAjaxContent('findAFlight', '/modules/findAFlight/findAFlightMC.jsp', 'pageModuleImpId=' + pageModuleImpId);
	}
}

// AddToTrip()
// --------------------------------------------------------------------------------
// Callback from map when airport is clicked
//
function AddToTrip(iatacode) {
	document.getElementById("selectedArpFromMap").value = iatacode;
	LoadAjaxCallFunction('/common/cityNameFromAirportCodeSS.jsp', "iatacode=" + iatacode, popAirportsinFF);
}

// popAirportsinFF()
// --------------------------------------------------------------------------------
// Place the airports into the find a flight box from the map
//
function popAirportsinFF(returnedString) {

	returnedString = trim(returnedString);

	// Populate the Round trip and OW
	if (document.getElementById("tripType").value != "MC") {

		if (document.getElementById("deptPlace1").value == "") { 
			document.getElementById("deptPlace1hidden").value = document.getElementById("selectedArpFromMap").value;
			document.getElementById("deptPlace1").value = "" + returnedString + "";
		}
		else { 
			document.getElementById("arrivalPlace1hidden").value = document.getElementById("selectedArpFromMap").value;
			document.getElementById("arrivalPlace1").value = "" + returnedString + "";		 
		}
	}

	// Populate the MC
	else {

		if (document.getElementById("deptPlace1").value == "") {

			document.getElementById("deptPlace1hidden").value = document.getElementById("selectedArpFromMap").value;
			document.getElementById("deptPlace1").value = "" + returnedString + "";
		} else { 
			if (document.getElementById("arrivalPlace1").value == "") {

				document.getElementById("arrivalPlace1hidden").value = document.getElementById("selectedArpFromMap").value;
				document.getElementById("arrivalPlace1").value = "" + returnedString + "";
				
				document.getElementById("deptPlace2hidden").value = document.getElementById("selectedArpFromMap").value;
				document.getElementById("deptPlace2").value = "" + returnedString + "";
				 
			} 
			else if (document.getElementById("deptPlace2").value == "") {
				
				document.getElementById("deptPlace2hidden").value = document.getElementById("selectedArpFromMap").value;
				document.getElementById("deptPlace2").value = "" + returnedString + "";			
				 
			} 
			else if (document.getElementById("arrivalPlace2").value == "") {

				document.getElementById("arrivalPlace2hidden").value = document.getElementById("selectedArpFromMap").value;
				document.getElementById("arrivalPlace2").value = "" + returnedString + "";		
				
				document.getElementById("deptPlace3hidden").value = document.getElementById("selectedArpFromMap").value;
				document.getElementById("deptPlace3").value = "" + returnedString + "";	
				 
			} 
			else if (document.getElementById("deptPlace3").value == "") {

				document.getElementById("deptPlace3hidden").value = document.getElementById("selectedArpFromMap").value;
				document.getElementById("deptPlace3").value = "" + returnedString + "";			
				 
			} 
			else if (document.getElementById("arrivalPlace3").value == "") {

				document.getElementById("arrivalPlace3hidden").value = document.getElementById("selectedArpFromMap").value;
				document.getElementById("arrivalPlace3").value = "" + returnedString + "";	
			 
			}  
		}
	}

	drawSearchOnMap();

}
// clearfrom1()
// --------------------------------------------------------------------------------
// Clear OD 1 in the find a flight box (any mode)
//
function clearfrom1(){

	document.getElementById("deptPlace1hidden").value = '';
	document.getElementById("deptPlace1").value = '';	
	document.getElementById("arrivalPlace1hidden").value = '';
	document.getElementById("arrivalPlace1").value = '';
	document.getElementById("trip-mto-1").innerHTML = "";
	document.getElementById("trip-mwhen-1").innerHTML = "";
	document.getElementById("FromDate1").value="dd-mon-yyyy";
	document.getElementById("FromTime1").value="00.00-00.00";

	clearfrom2();
}


// clearfrom2()
// --------------------------------------------------------------------------------
// Clear OD 2 in the find a flight box (any mode)
//
function clearfrom2(){

	document.getElementById("deptPlace2hidden").value = '';
	document.getElementById("deptPlace2").value = '';
	document.getElementById("arrivalPlace2hidden").value = '';
	document.getElementById("arrivalPlace2").value = '';
	document.getElementById("trip-mto-2").innerHTML = "";
	document.getElementById("trip-mwhen-2").innerHTML = "";
	document.getElementById("FromDate2").value="dd-mon-yyyy";
	document.getElementById("FromTime2").value="00.00-00.00";	

	clearfrom3();
}


// clearfrom3()
// --------------------------------------------------------------------------------
// Clear OD 3 in the find a flight box (any mode)
//
function clearfrom3(){

	document.getElementById("deptPlace3hidden").value = '';
	document.getElementById("deptPlace3").value = '';
	document.getElementById("arrivalPlace3hidden").value = '';
	document.getElementById("arrivalPlace3").value = '';
	document.getElementById("trip-mto-3").innerHTML = "";
	document.getElementById("trip-mwhen-3").innerHTML = "";
	document.getElementById("FromDate3").value="dd-mon-yyyy";
	document.getElementById("FromTime3").value="00.00-00.00";
}


// toggleMultiCityTab()
// --------------------------------------------------------------------------------
// Toggles the tabs in the find a flight box for multi-city
//
function toggleMultiCityTab(divSelected) {

	for (var i=1;i<4;i++) {

		if( i == divSelected) {
			document.getElementById("trip-int-"+i).style.display = "block";
			document.getElementById("trip-mh-"+i).className = "selected";
		}
		else {
			document.getElementById("trip-int-"+i).style.display = "none";
			document.getElementById("trip-mh-"+i).className = "";

			if(document.getElementById("deptPlace" + i +"hidden").value !== "" && document.getElementById("arrivalPlace" + i +"hidden").value != "") {
				document.getElementById("trip-mto-"+i).innerHTML = document.getElementById("deptPlace" + i +"hidden").value + "-" + document.getElementById("arrivalPlace" + i +"hidden").value;  
				document.getElementById("trip-mwhen-"+i).innerHTML = document.getElementById("FromDate" + i).value + " " + document.getElementById("FromTime" + i).options[document.getElementById("FromTime" + i).selectedIndex].text;
			}
		}
	}
}


// displayCreditCardMessage()
// --------------------------------------------------------------------------------
// Display the credit card list of countries on the screen
//
function displayCreditCardMessage(displayBuffer) {
	alert(displayBuffer);
}


// fareSearch()
// --------------------------------------------------------------------------------
// Conduct a fare search and get the searchId
//
function fareSearch(pmi) { 
	// Search trackers
	try{
		//Site Catalyst Tracking
		fareSearchSC();	
	}
	catch(error){ }


	// Validate the fare search form
	if(validateFields(pmi)) {
	
		// Display the popunder
		if(Number(siteId)<=10){
			callPopUnder();
		}
		
		//Reset the search Id. This is done to identify the back button from results page.
		LoadAjaxCallFunction('/home/resetSearchId.jsp', "s=" + sessionId, resetSearchIdCallback);

		//Tracker
		LoadAjaxCallFunction('/common/bookingflowtracker.jsp', 's='+ sessionId +'&flowstep=SearchClicked&cb=' + Math.random());
	}
}


function resetSearchIdCallback() {
	
	// Construct the fare search
	constructFareSearch(0);

}


// constructFareSearch()
// --------------------------------------------------------------------------------
// Construct the fare search query string from the find a flight form
//
function constructFareSearch(searchType) {

	var tripid = '';
	var Origtarray;
	var Desttarray;
	var DateArray;
	var CabinClass;
	var CarrPreferance;
	var noAdults;
	var noSenior;
	var noChild;
	var noInfants;
	var noStudents;	
	var Non_stops = '';
	var TimeArray;
	var searchText;
		
	
	// Extract the find a flight variables
	var from1 = document.getElementById("deptPlace1hidden").value;
	var To1 	= document.getElementById("arrivalPlace1hidden").value;
	
	var from2 = document.getElementById("deptPlace2hidden").value;
	var To2 	= document.getElementById("arrivalPlace2hidden").value;
	
	var from3 = document.getElementById("deptPlace3hidden").value;
	var To3 	= document.getElementById("arrivalPlace3hidden").value;
	
	var fromDate1 = document.getElementById("FromDate1").value;
	var fromDate2 = document.getElementById("FromDate2").value;
	var fromDate3 = document.getElementById("FromDate3").value;
	 

	// Build the arrays for RT
	if (document.getElementById("tripType").value == "RT") {

		var pref1 =  getpreferedtime(document.getElementById("FromTime1").value);
		var hrsBandF1 =  gethrsBandA(document.getElementById("FromTime1").value);
		var pref2 =  getpreferedtime(document.getElementById("FromTime2").value);
		var hrsBandF2 =  gethrsBandA(document.getElementById("FromTime2").value);
		
		if( ((from1 != null) && (from1.length > 0))  && ((To1 != null) && (To1.length > 0)) && (fromDate1 != 'dd-mon-yyy')  && (fromDate2 != 'dd-mon-yyy')){
		
			Origtarray = [from1,To1];
			Desttarray = [To1, from1];
			DateArray=[formatDateForBusSvcs(fromDate1, "N"),formatDateForBusSvcs(fromDate2, "N")];
			TimeArray = [Math.ceil(pref1),Math.ceil(hrsBandF1),Math.ceil(pref2),Math.ceil(hrsBandF2)];
		}
	}


	// Build the arrays for OW and MC
	else {
		
		// MC - 3 ODs
		if((from1.length > 0) && (To1.length > 0) && (from2.length > 0) && (To2.length > 0) && (from3.length > 0) && (To3.length > 0) && ((fromDate1 !='dd-mon-yyy') && (fromDate2 !='dd-mon-yyy') && (fromDate3 !='dd-mon-yyy'))){	

			  	Origtarray = [from1,from2,from3];
				Desttarray = [To1,To2,To3];
				DateArray=[formatDateForBusSvcs(fromDate1, "N"), formatDateForBusSvcs(fromDate2, "N"), formatDateForBusSvcs(fromDate3, "N")];

				var pref1 =  getpreferedtime(document.getElementById("FromTime1").value);
				var hrsBandF1 =  gethrsBandA(document.getElementById("FromTime1").value);
				var pref2 =  getpreferedtime(document.getElementById("FromTime2").value);
				var hrsBandF2 =  gethrsBandA(document.getElementById("FromTime2").value);
				var pref3 =  getpreferedtime(document.getElementById("FromTime3").value);
				var hrsBandF3 =  gethrsBandA(document.getElementById("FromTime3").value);

				TimeArray = [Math.ceil(pref1),Math.ceil(hrsBandF1),Math.ceil(pref2),Math.ceil(hrsBandF2),Math.ceil(pref3),Math.ceil(hrsBandF3)];

		}
		
		// MC - 2 OD
		else if((from1.length > 0) && (To1.length > 0) && (from2.length > 0) && (To2.length > 0) && ((fromDate1 !='dd-mon-yyy') && (fromDate2 !='dd-mon-yyy'))){	

			  	Origtarray = [from1,from2];
				Desttarray = [To1,To2];
				DateArray=[formatDateForBusSvcs(fromDate1, "N"), formatDateForBusSvcs(fromDate2, "N")];

				var pref1 = getpreferedtime(document.getElementById("FromTime1").value);
				var hrsBandF1 = gethrsBandA(document.getElementById("FromTime1").value);
				var pref2 = getpreferedtime(document.getElementById("FromTime2").value);
				var hrsBandF2 = gethrsBandA(document.getElementById("FromTime2").value);

				TimeArray = [Math.ceil(pref1),Math.ceil(hrsBandF1),Math.ceil(pref2),Math.ceil(hrsBandF2)];

		}
		
		// MC / OW - 1 OD
		else if((from1.length > 0) &&(To1.length > 0) && (fromDate1 !='dd-mon-yyy')){	

			  	Origtarray = [from1];
				Desttarray = [To1];
				DateArray=[formatDateForBusSvcs(fromDate1, "N")];

				var pref1 = getpreferedtime(document.getElementById("FromTime1").value);
				var hrsBandF1 = gethrsBandA(document.getElementById("FromTime1").value);
				TimeArray = [Math.ceil(pref1),Math.ceil(hrsBandF1)];
		}
	}


	// Get the other preferences
	noAdults = document.getElementById('pAdults').value;
	noSenior = document.getElementById('pSeniors').value;
	noChild = document.getElementById('pChildren').value;
	noInfants = document.getElementById('pInfant').value;
	noStudent= document.getElementById('pStudent').value;	
	CabinClass = document.getElementById('pClass').value;
	CarrPreferance = document.getElementById('pCarrierhidden').value;
	
	// Get nonstop preference	
	if(document.getElementById("pNonStop").checked) { Non_stops = 'Y'; }	
	
	// Prepare the variables
	var vayamavisit = getCookie("vayamaVisit");
	var clickthrough = "N";
	
	if (vayamavisit == null || vayamavisit == "") vayamavisit = "0";

	var couponCode = document.getElementById("pCouponcode").value;
	
	
	var arguments = "s=" + sessionId + "&vayamaVisit=" + vayamavisit + "&clickThrough=" + clickthrough + "&originArray=" + Origtarray + "&destArray=" + Desttarray + "&dateArray=" + DateArray + "&cabinClass=" + CabinClass + "&carrPreferance=" + CarrPreferance + "&noAdults=" + noAdults + "&noSenior=" + noSenior + "&noChild=" + noChild + "&noInfants=" + noInfants + "&noStudents=" + noStudent + "&nonStops=" + Non_stops + "&timeArray=" +  TimeArray + "&couponCode=" + couponCode + "&tripType=" + document.getElementById("tripType").value + "&random=" + Math.random();

	// Send the parameters to the searching page to complete the search
	var url2Open = "";

	switch( Number(siteId) ) {

		case 11:
		case 12:
		case 13:
		case 14:
			url2Open = "/cheapTickets/home/CTSearching.jsp?" + arguments;
			break;

		case 16:
		case 10:
			url2Open = "/budgetAir/home/searching.jsp?" + arguments;
			break;

		default:
			url2Open = "/home/searching.jsp?" + arguments;
	}
	
	window.open(url2Open, "_self");
}


// clearFindAFlight()
// --------------------------------------------------------------------------------
// Clear the find a flight box and results
//
function clearFindAFlight() {

	// RT and OW have 1 value
	var clearToCount = 1;
	
	
	// Multi city has 3 sets
	if (document.getElementById("tripType").value == "MC")
		clearToCount = 3;

	for(var i=1; i <= clearToCount; i++) {
		
		document.getElementById("deptPlace"+i+"hidden").value ='';
		document.getElementById("arrivalPlace"+i+"hidden").value ='';
		document.getElementById("deptPlace"+i).value ='';
		document.getElementById("arrivalPlace"+i).value ='';
		
		document.getElementById("FromDate"+i).value = 'dd-mon-yyyy';

		// Only multi-city has the expanded date and city summaries for tabs
		if (document.getElementById("tripType").value == "MC") {
			document.getElementById("trip-mto-"+i).innerHTML = '';
			document.getElementById("trip-mwhen-"+i).innerHTML = '';
		}

	}


	// RT has one additional date for the return
	if (document.getElementById("tripType").value == "RT")
		document.getElementById("FromDate2").value = 'dd-mon-yyyy';


	setdefaultselect("pAdults","1");
	setdefaultselect("pChildren","0");
	setdefaultselect("pInfant","0");
	setdefaultselect("pStudent","0");	
	setdefaultselect("pClass","Y");

	document.getElementById("pCarrier").value="";
	document.getElementById("pNonStop").checked=false;
	
	listOff();

	if( typeof( flashMap ) != 'undefined' ) {
		if(flashMap && mapIsReady) {
			flashMap.SendCommand("<ResetMap />");
			displayWizardPosition("Step 1", "Your previous search has been cleared. You can restart your flight search by either entering your information into the <b>find a flight</b> box on the left or by clicking on your departure and arrival cities in the vayama map.","TopLeft");
		}
	}

}

function hideInfant(){
	document.getElementById("tooltipCover").style.display="none";
	hideInfo();
	
}
// showInfantInfo()
// --------------------------------------------------------------------------------
// Display information about booking infants
//
function showInfantInfo(pmi) {

	// Load infantInfoSS html into the messageHolder
	LoadAjaxInDivAndCallFunction('messageHolder', '/modules/findAFlight/infantInfo.jsp', 'pageModuleImpId=' + pmi, showInfantInfoCallback);

}


// showInfantInfoCallback()
// --------------------------------------------------------------------------------
// Display information about booking infants
//
function showInfantInfoCallback() {
	 
	displayInfo(document.getElementById('infant'), 'messageHolder', 'right', 53);
	setUniversalPopupCover("tooltip");
}


// showInfantInfo()
// --------------------------------------------------------------------------------
// Display information about booking infants
//
function showStudentInfo(pmi) {

	// Load infantInfoSS html into the messageHolder
	LoadAjaxInDivAndCallFunction('messageHolder', '/modules/findAFlight/studentInfo.jsp', 'pageModuleImpId=' + pmi, showStudentInfoCallback);

}


// showInfantInfoCallback()
// --------------------------------------------------------------------------------
// Display information about booking infants
//
function showStudentInfoCallback() {
	
	displayInfo(document.getElementById('student'), 'messageHolder', 'right', 4);
}


// validateFields()
// --------------------------------------------------------------------------------
// Validates the user entry in the find a flight box
//
// retrieve error messages from CMS
function validateFields(pmi) {

	// Make sure dep city 1 is present
	if(document.getElementById("deptPlace1hidden").value == '') {
		LoadAjaxCallFunction('/modules/findAFlight/flightBoxErrorMessages.jsp', 'pageModuleImpId='+pmi+'&messageHolder=departureCityErr&leftPosition=0&id=deptPlace1', flightBoxMessageCallback);
		return false;
	}

	// Make sure arv city 1 is present
	if(document.getElementById("arrivalPlace1hidden").value == '') {
		LoadAjaxCallFunction('/modules/findAFlight/flightBoxErrorMessages.jsp', 'pageModuleImpId='+pmi+'&messageHolder=ArrivalCityErr&leftPosition=0&id=arrivalPlace1', flightBoxMessageCallback);
		return false;
	}

	// Insure that the dep city 1 != arv city 1
	if(document.getElementById("deptPlace1hidden").value == document.getElementById("arrivalPlace1hidden").value){
		LoadAjaxCallFunction('/modules/findAFlight/flightBoxErrorMessages.jsp', 'pageModuleImpId='+pmi+'&messageHolder=CityNotSameErr&leftPosition=0&id=arrivalPlace1', flightBoxMessageCallback);
		return false;
	}


	// Make sure dep date 1 is present
	if( (document.getElementById("FromDate1").value == 'dd-mon-yyyy') || (document.getElementById("FromDate1").value == '') ) {
		LoadAjaxCallFunction('/modules/findAFlight/flightBoxErrorMessages.jsp', 'pageModuleImpId='+pmi+'&messageHolder=departureDateErr&leftPosition=100&id=FromDate1', flightBoxMessageCallback);
		return false;
	}


	// If RT make sure arrive date 1 is present
	if (document.getElementById("tripType").value == "RT") {
		 																  	
		if( (document.getElementById("FromDate2").value == 'dd-mon-yyyy') || (document.getElementById("FromDate2").value == '') ) {
			LoadAjaxCallFunction('/modules/findAFlight/flightBoxErrorMessages.jsp', 'pageModuleImpId='+pmi+'&messageHolder=returnDateErr&leftPosition=100&id=FromDate2', flightBoxMessageCallback);
			return false;
		}

		// Make sure that OD1 and OD2 are in date order
		if(getJSDateFromDisplayDate(document.getElementById("FromDate2").value) < getJSDateFromDisplayDate(document.getElementById("FromDate1").value)) {
			LoadAjaxCallFunction('/modules/findAFlight/flightBoxErrorMessages.jsp', 'pageModuleImpId='+pmi+'&messageHolder=departureDateAfterErr&leftPosition=100&id=FromDate2', flightBoxMessageCallback);
			return false;
		}		

	}


	// If MC continue validating
	if (document.getElementById("tripType").value == "MC") {

		if(document.getElementById("arrivalPlace2hidden").value != '') {

			// Check to make sure that dates are present if arrival cities are present
			if( (document.getElementById("FromDate2").value == 'dd-mon-yyyy') || (document.getElementById("FromDate2").value == '') ) {
				LoadAjaxCallFunction('/modules/findAFlight/flightBoxErrorMessages.jsp', 'pageModuleImpId='+pmi+'&messageHolder=dateErr&leftPosition=100&id=FromDate2', flightBoxMessageCallback);
				return false;
			}
			
			// Make sure that OD1 and OD2 are in date order
			if(getJSDateFromDisplayDate(document.getElementById("FromDate2").value) < getJSDateFromDisplayDate(document.getElementById("FromDate1").value)) {
				LoadAjaxCallFunction('/modules/findAFlight/flightBoxErrorMessages.jsp', 'pageModuleImpId='+pmi+'&messageHolder=departureDateAfterErr&leftPosition=100&id=FromDate2', flightBoxMessageCallback);
				return false;
			}
			
			// Insure that the dep city 2 != arv city 2
			if(document.getElementById("deptPlace2hidden").value != '' && (document.getElementById("deptPlace2hidden").value == document.getElementById("arrivalPlace2hidden").value)){
				LoadAjaxCallFunction('/modules/findAFlight/flightBoxErrorMessages.jsp', 'pageModuleImpId='+pmi+'&messageHolder=CityNotSameErr&leftPosition=0&id=arrivalPlace2', flightBoxMessageCallback);
				return false;
			}			
			
		}

		if(document.getElementById("arrivalPlace3hidden").value != '') {
			
			// Make sure that OD3 has a date if a city is present
			if( (document.getElementById("FromDate3").value == 'dd-mon-yyyy') || (document.getElementById("FromDate3").value == '') ) {
				LoadAjaxCallFunction('/modules/findAFlight/flightBoxErrorMessages.jsp', 'pageModuleImpId='+pmi+'&messageHolder=dateErr&leftPosition=100&id=FromDate3', flightBoxMessageCallback);
				return false;
			}
			
			// Make sure OD2 and OD3 are in date order
			if(getJSDateFromDisplayDate(document.getElementById("FromDate3").value) <= getJSDateFromDisplayDate(document.getElementById("FromDate2").value)) {
				LoadAjaxCallFunction('/modules/findAFlight/flightBoxErrorMessages.jsp', 'pageModuleImpId='+pmi+'&messageHolder=departureDateAfterErr&leftPosition=100&id=FromDate3', flightBoxMessageCallback);
				return false;
			}
			
			// Insure that the dep city 3 != arv city 3
			if(document.getElementById("deptPlace3hidden").value != '' && (document.getElementById("deptPlace3hidden").value == document.getElementById("arrivalPlace3hidden").value)){
				LoadAjaxCallFunction('/modules/findAFlight/flightBoxErrorMessages.jsp', 'pageModuleImpId='+pmi+'&messageHolder=CityNotSameErr&leftPosition=0&id=arrivalPlace3', flightBoxMessageCallback);
				return false;
			}				
		}
	}


	// Test the purchase date to make sure it is not too soon
	var testDate = getJSDateFromDisplayDate(document.getElementById("FromDate1").value);
	var todayDate = new Date();

	if(todayDate.getTime() + (86400000 * 2) > testDate.getTime()) {
		errorSC("findflights","searchedlessthanfourdays");
		LoadAjaxCallFunction('/modules/findAFlight/flightBoxErrorMessages.jsp', 'pageModuleImpId='+pmi+'&messageHolder=departureWithin4DaysErr&leftPosition=100&id=FromDate1', flightBoxMessageCallback);
		return false;		
	}


	// Make sure we have at least one traveler
	var noAdults = 1 * document.getElementById('pAdults').value;
	var noChild = 1 * document.getElementById('pChildren').value;
	var noInfants = 1 * document.getElementById('pInfant').value;
	var noStudents = 1 * document.getElementById('pStudent').value;
	
	if(noAdults + noChild + noStudents == 0) {
		LoadAjaxCallFunction('/modules/findAFlight/flightBoxErrorMessages.jsp', 'pageModuleImpId='+pmi+'&messageHolder=travelerErr&leftPosition=0&id=pAdults', flightBoxMessageCallback);
		return false;
	}	

	// Make sure we dont have over 6 people on a reservation
	if(noAdults + noChild > 6) {
		LoadAjaxCallFunction('/modules/findAFlight/flightBoxErrorMessages.jsp', 'pageModuleImpId='+pmi+'&messageHolder=passengersExceedErr&leftPosition=90&id=pAdults', flightBoxMessageCallback);
		return false;
	}	


	// Make sure that the number of children does not exceed the number of adults
	if(noAdults == 0 && noChild > 0) {
		LoadAjaxCallFunction('/modules/findAFlight/flightBoxErrorMessages.jsp', 'pageModuleImpId='+pmi+'&messageHolder=adultTravelerRequiredErr&leftPosition=20&id=pChildren', flightBoxMessageCallback);
		return false;
	}

	// Make sure that the number of infants does not exceed the number of adults
	if(noAdults == 0 && noInfants > 0) {
		LoadAjaxCallFunction('/modules/findAFlight/flightBoxErrorMessages.jsp', 'pageModuleImpId='+pmi+'&messageHolder=infantWithoutAdultErr&leftPosition=20&id=pInfant', flightBoxMessageCallback);
		return false;
	}

	// Make sure that the number of infants does not exceed the number of adults
	if(noAdults < noInfants ) {
		LoadAjaxCallFunction('/modules/findAFlight/flightBoxErrorMessages.jsp', 'pageModuleImpId='+pmi+'&messageHolder=onlyOneInfantPerAdultErr&leftPosition=20&id=pInfant', flightBoxMessageCallback);
		return false;
	}

	// Make sure we are not mixing students and other travelers
	if(noStudents > 0 && (noAdults > 0 || noInfants > 0 || noChild > 0) ) {
		LoadAjaxCallFunction('/modules/findAFlight/flightBoxErrorMessages.jsp', 'pageModuleImpId='+pmi+'&messageHolder=studentCannotMixedErr&leftPosition=20&id=pStudent', flightBoxMessageCallback);
		return false;
	}

	return true;
}


// flightBoxMessageCallback()
// --------------------------------------------------------------------------------
// Display flight box error message
//
function flightBoxMessageCallback(returnString) {
	var resultString = trim(returnString);
	var resultsArray = resultString.split("|");
	document.getElementById("messageHolder").innerHTML = resultsArray[2];
	displayInfo(document.getElementById(''+resultsArray[0]+''), 'messageHolder', 'right', parseInt(resultsArray[1]));
}

   

// setdefaultselect()
// --------------------------------------------------------------------------------
// Generic function to set values of selects
//
function setdefaultselect(varobjname , defaultval) {
	var selobj = document.getElementById(varobjname);

	for(var i=0;i<selobj.length;i++) {
		if(selobj[i].value == defaultval) {
			selobj[i].selected = true;
			break;
		}
	}
}

 
function getpreferedtime(timeval)
{
	var timeArray = timeval.split('-');
	var Btime = Number(timeArray[0].substr(0,2));
	var Etime = Number(timeArray[1].substr(0,2));

	PrefTime = Btime + ((Etime-Btime)/2); 

	return PrefTime;
}



function gethrsBandA(timeval)
{

	var timeArray = timeval.split('-');
	var Btime = Number(timeArray[0].substr(0,2));
	var Etime = Number(timeArray[1].substr(0,2));

	HrsBeforeAndAfter = (Etime-Btime)/2;

	return HrsBeforeAndAfter;
}


// callPopUnder()
// --------------------------------------------------------------------------------
// Display the evil popunder
//
function callPopUnder(){
	
	var isFF4 = false;
  	if (/Firefox[\/\s](\d+\.\d+)/.test(navigator.userAgent)){ //test for Firefox/x.x or Firefox x.x (ignoring remaining digits);
	 	var ffversion=new Number(RegExp.$1) // capture x.x portion and store as a number
		if (ffversion>=4)
	  		isFF4 = true;
	}

	if(!isFF4){
		// Determine if we have already shown the popunder
		var alreadyPopUn = getCookie('vpu');
		
		if(alreadyPopUn != "1" && typeof(exitPopupWin)=="undefined") { 
			
			var from1 = document.getElementById("deptPlace1hidden").value;
			var To1 	= document.getElementById("arrivalPlace1hidden").value;
			
			var from2 = document.getElementById("deptPlace2hidden").value;
			var To2 	= document.getElementById("arrivalPlace2hidden").value;
			
			var from3 = document.getElementById("deptPlace3hidden").value;
			var To3 	= document.getElementById("arrivalPlace3hidden").value;
			
			var fromDate1 = document.getElementById("FromDate1").value;
			var fromDate2 = document.getElementById("FromDate2").value;
			var fromDate3 = document.getElementById("FromDate3").value;
			
			var noAdults = 1 * document.getElementById('pAdults').value;
			var noChild = document.getElementById('pChildren').value;
			var noStudents = 1 * document.getElementById('pStudent').value;
			var CabinClass = document.getElementById('pClass').value;
	
			noAdults = noAdults + noStudents;
				
			// to fix one way airfrare showing 11/30/1999 or some crazy date 
			if(document.getElementById('oneWayTripR')){
				fromDate2 = fromDate1;
			}
			
			var arguments = "siteId="+siteId+"&from1=" + from1 + "&to1=" + To1 + "&from2=" + from2 + "&to2=" + To2 + "&from3=" + from3 + "&to3=" + To3 + "&fromDate1=" + fromDate1 + "&fromDate2=" + fromDate2 + "&fromDate3=" + fromDate3 + "&cabinClass=" + CabinClass + "&noAdults=" + noAdults + "&noChild=" + noChild + "&tripType=" + document.getElementById("tripType").value;
		
			var exitPopupWin = window.open('/home/popupPageHolder.jsp' + '?' + arguments, 'vayamaexit', 'scrollbars=yes, resizable=yes, toolbar=no, menubar=no, status=no, location=no, left=10, top=10, height=580px, width=1020px');
			
			if(exitPopupWin) { 
				exitPopupWin.blur();
				window.focus();
				setCookie('vpu', "1", getExpDate(0, 0, 10));
			}
		}			
	}
}


// getCookie()
// --------------------------------------------------------------------------------
// Read a cookie
//
function getCookie(name) {
    var start = document.cookie.indexOf(name + "=");
    var len = start + name.length + 1;
    if ((!start) && (name != document.cookie.substring(0, name.length))) {
        return null;
    }
    if (start == -1) {
        return null;
    }
    var end = document.cookie.indexOf(";", len);
    if (end == -1) {
        end = document.cookie.length;
    }
    return unescape(document.cookie.substring(len, end));
}

		
// setCookie()
// --------------------------------------------------------------------------------
// set a cookie
//
function setCookie(name, value, expires) {
 	document.cookie = name + "=" + escape (value) + ";path=/" + 
        ((expires) ? "; expires=" + expires : "");
}


// getExpDate()
// --------------------------------------------------------------------------------
// generate expiry date
//
function getExpDate(days, hours, minutes) {
    var expDate = new Date();
    if (typeof days == "number" && typeof hours == "number" && typeof hours == "number") {
        expDate.setDate(expDate.getDate() + parseInt(days));
        expDate.setHours(expDate.getHours() + parseInt(hours));
        expDate.setMinutes(expDate.getMinutes() + parseInt(minutes));
        return expDate.toGMTString();
    }
}

