var xmlHttp;
function GetXmlHttpObject() {
	try	{
		// Firefox, Opera 8.0+, Safari
		xmlHttp = new XMLHttpRequest();
	}
	catch(e) {
		// Internet Explorer
		try	{
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e) {
			try {
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(e) {
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}
}

document.onkeydown = keydown;  

compareSiteWindowArr = new Array();
compareSiteUrlArr = new Array();
compareSiteIdArr = new Array();
compareSiteCheckArr = new Array();

function keydown(event) {  
	var code;  
	var e;  
	if (document.all) {  
		if (!event) {  
			var e = window.event;  
			code = e.keyCode;  
		}  
	}  
	else if (event.which) {  
		code = event.which;  
		e = event;  
	}  
	
	if (code == 9) {//Write some logic  		
		setAirportAuto();
	}  
} 


function setAirportAuto(objAirport) {
	if(objAirport == 'departure') {
		if(document.getElementById('destinationDiv').style.display == '' && document.getElementById('destinationSel')) {
			var objIata = document.getElementById('destinationSel').value;
			var objName = document.getElementById(objIata).value;
			submitFlightSearchForm(objIata, 'destination');
		}	
	}
	else if(objAirport == 'destination') {
		if(document.getElementById('departureDiv').style.display == '' && document.getElementById('departureSel')) {
			var objIata = document.getElementById('departureSel').value;
			var objName = document.getElementById(objIata).value;
			submitFlightSearchForm(objIata, 'departure');
		}	
	}
	else {
		if(document.getElementById('destinationDiv').style.display == '' && document.getElementById('destinationSel')) {
			var objIata = document.getElementById('destinationSel').value;
			var objName = document.getElementById(objIata).value;
			submitFlightSearchForm(objIata, 'destination');
		}	
		if(document.getElementById('departureDiv').style.display == '' && document.getElementById('departureSel')) {
			var objIata = document.getElementById('departureSel').value;
			var objName = document.getElementById(objIata).value;
			submitFlightSearchForm(objIata, 'departure');
		}	
	}
	
}

// Function call when 'keyup' done in text fields 'From' and 'To'
// e - unicode (event) - it returns which key has been pressed
// objText - search keyword entered by user
// objAirport - type (Departure / Destination)
function autoDisplayAirport(e, objText, objAirport) {
	var unicode = e.keyCode? e.keyCode : e.charCode;
	
	setAirportAuto(objAirport);
	
	// If 'Enter' is pressed, set airport name in textbox, set airport iata in hidden variable, set popup div to ''
	if(unicode == 13) {
		selSpan = document.getElementById(objAirport + "SelSpanId").value;

		if(document.getElementById('hidSpanId' + selSpan)) {

			var objIataHid = document.getElementById('hidSpanId' + selSpan).value;
			document.getElementById(objAirport + 'Hid').value = objIataHid;

			document.getElementById(objAirport + 'Airport').value = document.getElementById(objIataHid).value;
			document.getElementById(objAirport + 'Div').style.display = "none";
			document.getElementById(objAirport + 'Div').innerHTML = '';
		}
		
		// Function call to get vendors for selected route in Compare Section of Home page
		sendCompareVendorReq();
	}
	else if(unicode == 38 || unicode == 40) {
		
		var minVal = 1;
		var maxVal = document.getElementById(objAirport + 'CntSpanId').value;
		maxVal--;

		// If 'UP' is pressed, decrease selSpanIdDeaprture or selSpanIdDestination hidden variable and according to that highlight li
		if(unicode == 38) {

			selSpan = document.getElementById(objAirport + "SelSpanId").value;
			selSpan--;
			document.getElementById(objAirport + "SelSpanId").value = selSpan;

			if(selSpan < minVal) {
				selSpan = minVal;
				document.getElementById(objAirport + "SelSpanId").value = selSpan;
			}

			if(selSpan > maxVal) {
				selSpan = maxVal;
				document.getElementById(objAirport + "SelSpanId").value = selSpan;
			}

			var flDisp = true;
			var cnt = 1;
			while(flDisp) {

				spanSrc = document.getElementById('liveSearch' + cnt);
				if(spanSrc) {

					if(cnt == selSpan) {
						spanSrc.className='li_active';
						document.getElementById(objAirport + 'Sel').value = document.getElementById('hidSpanId' + cnt).value;
					}
					else {
						spanSrc.className='';
					}
				}
				else {
					flDisp = false;
				}

				cnt++;
			}
		}
		// If 'Down' is pressed, increase selSpanIdDeaprture or selSpanIdDestination hidden variable and according to that highlight li
		else if(unicode == 40) {

			selSpan = document.getElementById(objAirport + "SelSpanId").value;
			selSpan++;
			document.getElementById(objAirport + "SelSpanId").value = selSpan;

			if(selSpan < minVal) {
				selSpan = minVal;
				document.getElementById(objAirport + "SelSpanId").value = selSpan;
			}

			if(selSpan > maxVal) {
				selSpan = maxVal;
				document.getElementById(objAirport + "SelSpanId").value = selSpan;
			}

			var flDisp = true;
			var cnt = 1;
			while(flDisp) {

				spanSrc = document.getElementById('liveSearch' + cnt);
				if(spanSrc) {

					if(cnt == selSpan) {
						spanSrc.className='li_active';
						document.getElementById(objAirport + 'Sel').value = document.getElementById('hidSpanId' + cnt).value;
					}
					else {
						spanSrc.className='';
					}
				}
				else {
					flDisp = false;
				}

				cnt++;
			}
		}
	}
	else {
		// If entered text length is equal or more than 2
		if(objText.length > 2) {
			GetXmlHttpObject();

			var url = usPath + "flightsearchajax.php?doAction=GetSearchResult";
			url = url + "&txtSrc=" + encodeURIComponent(objText) + '&txtAirport=' + objAirport + '&r=' + Math.random().toString().replace(".", "");

			xmlHttp.onreadystatechange = function () {
				if(xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
					var strResponseTextVal = xmlHttp.responseText;
					// Response would be with separator '~@'
					// 0 - li display
					// 1 - type of airport (Departure or destination)
					// 2 - total count of airports displaying
					var arrResponseText = strResponseTextVal.split("~@");
					
					var divSelect = arrResponseText[1] + 'Div';				
					
					if(document.getElementById(divSelect)) {
						document.getElementById(divSelect).style.display = "";
						document.getElementById(divSelect).innerHTML = arrResponseText[0];

						document.getElementById(objAirport + "CntSpanId").value = arrResponseText[2];

						jQuery('.i-scrollbar-typ1-js .scroll-pane').jScrollPane({scrollbarWidth:11, showArrows:false});
						jQuery('.i-scrollbar-typ1-js .scroll-pane').wrap(jQuery("<div></div>")
							.attr({'className':'scroll-pane-overflow'})
						);

						jQuery(".search_popup ul li").hover(
							function() {jQuery(this).addClass('li-selected');},
							function() {jQuery(this).removeClass('li-selected');}
						);
					}
				}
			}

			xmlHttp.open("GET",url,true);
			xmlHttp.send(null);
		}
		else {
			// If entered text is less than 2, unset popup 
			var divSelect = objAirport + 'Div';
			document.getElementById(divSelect).style.display = "none";
			document.getElementById(divSelect).innerHTML = '';
		}
	}
}

// Function call when select any option from popup by clicking using mouse, it will pass IATA and type of airport
// This function will set text field and hidden variables and then unset popup
function submitFlightSearchForm(objIataHid, objAirport) {

	document.getElementById(objAirport + 'Airport').value = document.getElementById(objIataHid).value;
	document.getElementById(objAirport + 'Div').style.display = "none";
	document.getElementById(objAirport + 'Div').innerHTML = '';
	document.getElementById(objAirport + 'Hid').value = objIataHid;
	
	// Function call to get vendors for selected route in Compare Section of Home page
	sendCompareVendorReq();
}

// Function call to change the color of airport options in popup when mouseover done
function changeFlightSearchColor(selSpan, objAirport) {
	var flDisp = true;
	var cnt = 1;
	while(flDisp) {

		spanSrc = document.getElementById('liveSearch' + cnt);
		if(spanSrc) {

			if(cnt == selSpan) {
				spanSrc.className='li_active';
				document.getElementById(objAirport + 'SelSpanId').value = cnt;
				document.getElementById(objAirport + 'Sel').value = document.getElementById('hidSpanId' + cnt).value;
			}
			else {
				spanSrc.className='';
			}
		}
		else {
			flDisp = false;
		}

		cnt++;
	}
}

function changeEmailCSS(objEmail, objType) {
	if(objType == 'Focus') {
		objEmail.style.color = '#000000';
		objEmail.value = '';
	}
	else {
		if(!notEmpty(objEmail, null, false, true)) {
			objEmail.value = 'Email address to receive deals (optional)';
			objEmail.style.color = '#999999';
		}
	}
}


function setOneWay(objVal) {
	if(objVal == true) {
		jQuery("#flightsDestinationDateLabel").datepicker('disable');
		jQuery("#flightsDestinationDateLabel").addClass('cal-disable');
	}
	else {
		jQuery("#flightsDestinationDateLabel").datepicker('enable');	
		jQuery("#flightsDestinationDateLabel").removeClass('cal-disable');
	}
	// Function call to get vendors for selected route in Compare Section of Home page
	sendCompareVendorReq();
}


function setCompare() {
	// Function call to get vendors for selected route in Compare Section of Home page
	sendCompareVendorReq();
}

function submitFlightSearchButton(flagPopup, flagCompare) {
	var departure = document.getElementById('departureAirport');
	var destination = document.getElementById('destinationAirport');
	
	// Departure
	if(!notEmpty(departure, null, false, true)) {
		alert("Please enter depart airport");
		departure.focus();
		return false;
	}

	// Destination
	if(!notEmpty(destination, null, false, true)) {
		alert("Please enter return airport");
		destination.focus();
		return false;
	}

	// Date validation	
	var departArr = document.getElementById('flightsDepartureDateLabel').value;

	if(!notEmpty(document.getElementById('flightsDepartureDateLabel'), null, false, true)) {
		alert("Please enter Depart date");
		document.getElementById('flightsDepartureDateLabel').focus();
		return false;
	}

	// Added by Karthick
	
	// date length should be 10 characters (no more no less)
   	if (departArr.length !== 10) {
		if(document.getElementById('hidUSSite') && document.getElementById('hidUSSite').value == 'Y') {
			alert("Please enter Depart date in correct format. [MM/DD/YYYY]");
		}
		else {
			alert("Please enter Depart date in correct format. [DD/MM/YYYY]");
		}			
		document.getElementById('flightsDepartureDateLabel').focus();
		return false;
    }
	

	// third and sixth character should be '/'
	if (departArr.substring(2, 3) !== '/' || departArr.substring(5, 6) !== '/') {
	    if(document.getElementById('hidUSSite') && document.getElementById('hidUSSite').value == 'Y') {
			alert("Please enter Depart date in correct format. [MM/DD/YYYY]");
		}
		else {
			alert("Please enter Depart date in correct format. [DD/MM/YYYY]");
		}
		document.getElementById('flightsDepartureDateLabel').focus();
		return false;
    }

	if(document.getElementById('hidUSSite') && document.getElementById('hidUSSite').value == 'Y') {
		var departMon = departArr.substring(0, 2) - 1;
		var departDay = departArr.substring(3, 5) - 0;
		var departYear = departArr.substring(6) - 0;
	}
	else {
		var departDay = departArr.substring(0, 2) - 0;
		var departMon = departArr.substring(3, 5) - 1;
		var departYear = departArr.substring(6) - 0;
	}
		
	var depTime = new Date(departYear, departMon, departDay).getTime(); 
	
	if (departYear < 1000 || departYear > 3000) {
		if(document.getElementById('hidUSSite') && document.getElementById('hidUSSite').value == 'Y') {
			alert("Please enter Depart date in correct format. [MM/DD/YYYY]");
		}
		else {
			alert("Please enter Depart date in correct format. [DD/MM/YYYY]");
		}
		document.getElementById('flightsDepartureDateLabel').focus();
		return false;
    }
    	
	objDate = new Date();
	objDate.setTime(depTime);
    	
    if ((objDate.getFullYear() !== departYear) || (objDate.getMonth() !== departMon) ||  (objDate.getDate() !== departDay)) {
		if(document.getElementById('hidUSSite') && document.getElementById('hidUSSite').value == 'Y') {
			alert("Please enter Depart date in correct format. [MM/DD/YYYY]");
		}
		else {
			alert("Please enter Depart date in correct format. [DD/MM/YYYY]");
		}
		document.getElementById('flightsDepartureDateLabel').focus();
		return false;
    }

	var travelType = 2;
	if(document.getElementById('flightsOneWay')) {
		var travelType = document.getElementById('flightsOneWay').checked;

		if(travelType == false) {
			travelType = 2;
		}
		else {
			travelType = 1;
		}
	}
	
	if(travelType == 2) {
		var returnArr = document.getElementById('flightsDestinationDateLabel').value;
		
		if(!notEmpty(document.getElementById('flightsDestinationDateLabel'), null, false, true)) {
			alert("Please enter Return date");
			document.getElementById('flightsDestinationDateLabel').focus();
			return false;
		}

		// date length should be 10 characters (no more no less)
		if (returnArr.length !== 10) {
			if(document.getElementById('hidUSSite') && document.getElementById('hidUSSite').value == 'Y') {
				alert("Please enter Return date in correct format. [MM/DD/YYYY]");
			}
			else {
				alert("Please enter Return date in correct format. [DD/MM/YYYY]");
			}			
			document.getElementById('flightsDestinationDateLabel').focus();
			return false;
		}
		// third and sixth character should be '/'
		if (returnArr.substring(2, 3) !== '/' || returnArr.substring(5, 6) !== '/') {
			if(document.getElementById('hidUSSite') && document.getElementById('hidUSSite').value == 'Y') {
				alert("Please enter Return date in correct format. [MM/DD/YYYY]");
			}
			else {
				alert("Please enter Return date in correct format. [DD/MM/YYYY]");
			}
			document.getElementById('flightsDestinationDateLabel').focus();
			return false;
		}		
		
		if(document.getElementById('hidUSSite') && document.getElementById('hidUSSite').value == 'Y') {
			var returnMon = returnArr.substring(0, 2) - 1;
			var returnDay = returnArr.substring(3, 5) - 0;
			var returnYear = returnArr.substring(6) - 0;
		}
		else {
			var returnDay = returnArr.substring(0, 2) - 0;
			var returnMon = returnArr.substring(3, 5) - 1;
			var returnYear = returnArr.substring(6) - 0;
		}

		var retTime = new Date(returnYear, returnMon, returnDay).getTime(); 		

		if (returnYear < 1000 || returnYear > 3000) {
			if(document.getElementById('hidUSSite') && document.getElementById('hidUSSite').value == 'Y') {
				alert("Please enter Return date in correct format. [MM/DD/YYYY]");
			}
			else {
				alert("Please enter Return date in correct format. [DD/MM/YYYY]");
			}
			document.getElementById('flightsDestinationDateLabel').focus();
			return false;
		}

		objDate1 = new Date();
		objDate1.setTime(retTime);

		if ((objDate1.getFullYear() !== returnYear) || (objDate1.getMonth() !== returnMon) ||  (objDate1.getDate() !== returnDay)) {
			if(document.getElementById('hidUSSite') && document.getElementById('hidUSSite').value == 'Y') {
				alert("Please enter Return date in correct format. [MM/DD/YYYY]");
			}
			else {
				alert("Please enter Return date in correct format. [DD/MM/YYYY]");
			}
			document.getElementById('flightsDestinationDateLabel').focus();
			return false;
		}		
		
		if(depTime > retTime) {
			alert("Departure Date should be less than Return Date");
			return false;
		}	
	}
	
	// Email Address
	if(document.getElementById('emailAddressInput')) {
		var emailAddress = document.getElementById('emailAddressInput');
		
		if(emailAddress.value != 'Email address to receive deals (optional)' && emailAddress.value != '') {
			reg = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@(([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
			if(!reg.test(emailAddress.value)) {
				alert("Please enter valid Email address to receive deals");
				return false;
			}
		}		
		
		var email = document.getElementById('emailAddressInput').value;
		if(email == 'Email address to receive deals (optional)') {
			email = "";
		}
	}
	else {
		var emailAddress = "";
		var email = "";
	}	
	
	var directfl = 'N';
	if(document.getElementById('chkDirect')) {
		var directfl = document.getElementById('chkDirect').checked;
				
		if(directfl == false) {
			directfl = 'N';
		}
		else {
			directfl = 'Y';
		}	
	}
	
	if(document.getElementById('flightsPassenger')) {
		document.getElementById('passengers').value = document.getElementById('flightsPassenger').value;
	}
	
	if(document.getElementById('flightsChildren')) {
		document.getElementById('children').value = document.getElementById('flightsChildren').value;
	}
	
	if(document.getElementById('flightsInfants')) {
		document.getElementById('infants').value = document.getElementById('flightsInfants').value;	
	}
	
	if(document.getElementById('flightsCabin')) {
		document.getElementById('cabin').value = document.getElementById('flightsCabin').value;
	}

	document.getElementById('departure').value = document.getElementById('departureHid').value;
	document.getElementById('destination').value = document.getElementById('destinationHid').value;
	document.getElementById('trip').value = travelType;
	document.getElementById('emailAddress').value = email;
	document.getElementById('directfl').value = directfl;
	document.getElementById('chkOneWay').value = travelType;	

	var target = Math.random().toString().replace(".", "");
	
	var url = usPath + "flights-engine2/?action=search&client=directflights" 
			+ "&passengers=" + document.getElementById('passengers').value
			+ "&children=" + document.getElementById('children').value
			+ "&infants=" + document.getElementById('infants').value
			+ "&departureDate=" + document.getElementById('departureDate').value
			+ "&returnDate=" + document.getElementById('returnDate').value
			+ "&departure=" + document.getElementById('departure').value
			+ "&destination=" + document.getElementById('destination').value
			+ "&cabin=" + document.getElementById('cabin').value
			+ "&trip=" + document.getElementById('trip').value
			+ "&emailAddress=" + document.getElementById('emailAddress').value
			+ "&departureTime=0"
			+ "&returnTime=0"
			+ "&clientType=" + document.getElementById('clientType').value
			+ "&popup=true"
			+ "&directfl=" + document.getElementById('directfl').value
			+ "&code=" + document.getElementById('code').value
			+ "&bannercode=" + document.getElementById('bannercode').value
			+ "&cheapflightcode=" + document.getElementById('cheapflightcode').value
			+ "&affiliate=" + document.getElementById('affiliate').value
			+ "&r=" + target;

	if(document.getElementById('affId') && document.getElementById('affId').value != '') {
		var url = url + "&affId=" + document.getElementById('affId').value; 
	}
	
	if(document.getElementById('otherIframe')) {
		openPopup(url);
	}
	else {				
		if(flagPopup) {
			openMainSearchPopup(url);			
		}	
		else if(!flagCompare) {
			openCompareVendorIE('search');
		}
		else {
			// Commented following function to submit the page for search result 
			submitSearch(true);
			//ocation.href = url + "#search-grid-point";
		}				
	}
}

// This function is used to open search result popup
function openMainSearchPopup(url) {
	var screenHeight = screen.height;
	var screenWidth = screen.width;
	
	//var top = screenHeight / 5;	
	var top = screenHeight;	
	var left = screenWidth / 6;			
	var width = (left) * 4.5;	
	
	var height = (top * 3);		
	MM_openBrWindow(url + "#search-grid-point",'','menubar=yes,toolbar=yes,location=yes,scrollbars=yes,width=' + width + ',height=' + height + ',left=' + screenWidth / 8 + ',top=' + top + ',resizable=yes');

	for(cnt = 0; cnt <= compareSiteWindowArr.length; cnt++) {
		if(compareSiteWindowArr[cnt]) {
			if(!compareSiteWindowArr[cnt].closed) {
							
				var checkId = compareSiteCheckArr[cnt];
				var newWindow = compareSiteWindowArr[cnt];				
				
				newWindow.location = compareSiteUrlArr[cnt];

				// Insert clicks into table for tracking purpose	
				var vendorId = compareSiteIdArr[cnt];
				insertSearchHits(vendorId);
			}
		}
	}			
	compareSiteCheckArr = new Array();
	compareSiteWindowArr = new Array();
	compareSiteUrlArr = new Array();
	compareSiteIdArr = new Array();
}

// This function is used to open vendor popup if browser is IE or opera.. else will open all popups together with search button
function openCompareVendorIE(objChkId) {		
	
	// Check browser
	var flagOpenPopup = checkBrowser();
		
	// If browser is IE or OPERA, open popup
	if(flagOpenPopup) {		
		openCompareVendorPopup(flagOpenPopup, objChkId);
	}
	else {
		// If request from search buttoon
		if(objChkId == 'search') {
			openCompareVendorPopup(flagOpenPopup, objChkId);
		}
		else {
			return false;
		}
	}
}


// This function is used to check for browser.. It will return true for opera and IE and false for other brosers
function checkBrowser() {
	var flagOpenPopup = false;
	if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){ //test for MSIE 
		flagOpenPopup = true;
	}
	else if (/Opera[\/\s](\d+\.\d+)/.test(navigator.userAgent)) { //test for Opera 
		flagOpenPopup = true;
	}
	else {
		flagOpenPopup = false;
	}	
	return flagOpenPopup;
}
	

// Function to fetch all the vendors for selected route..
function sendCompareVendorReq() {
	
	// Validation for departure, destination, departure date, return date
	var flagCompare = false;
	if(document.getElementById('departureHid').value != '' && document.getElementById('destinationHid').value != '') {
		if(document.getElementById('flightsDepartureDateLabel').value != '') {
			
			var travelType = 2;
			if(document.getElementById('flightsOneWay')) {
				var travelType = document.getElementById('flightsOneWay').checked;

				if(travelType == false) {
					travelType = '2';
				}
				else {
					travelType = '1';
				}
			}		
				
			if(travelType == 2) {

				if(document.getElementById('flightsDestinationDateLabel').value != '') {
					flagCompare = true;
				}
			}
			else {
				flagCompare = true;
			}
		}	
	}
	
	if(!flagCompare) {
		return false;
	}

	// As this is implmentated using system variable, compare section div will be there is this section is on
	if(document.getElementById('divCompareVendor')) {

		// If departure and destination route selected..
		if(document.getElementById('departureHid').value != '' && document.getElementById('destinationHid').value != '' && document.getElementById('action').value != 'search') {

			GetXmlHttpObject();

			var travelType = document.getElementById('flightsOneWay').checked;

			if(travelType == false) {
				travelType = '2';
			}
			else {
				travelType = '1';
			}

			var url = usPath + "flightsearchajax.php?doAction=getCompareVendor";
			url = url + "&departure=" + document.getElementById('departureHid').value + '&destination=' + document.getElementById('destinationHid').value + "&passengers=" + document.getElementById('flightsPassenger').value + "&children=" + document.getElementById('flightsChildren').value + "&infants=" + document.getElementById('flightsInfants').value + "&cabin=" + document.getElementById('flightsCabin').value + "&trip=" + travelType + "&departureDate=" + document.getElementById('departureDate').value + "&returnDate=" + document.getElementById('returnDate').value;

			xmlHttp.onreadystatechange = function () {
				if(xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
					var strResponseTextVal = xmlHttp.responseText;

					if(!IsEmptyString(strResponseTextVal)) {
						
						var arrRes = strResponseTextVal.split('~');
						document.getElementById('divCompareVendor').innerHTML = '';
						document.getElementById('divCompareVendor').innerHTML = arrRes[0];
						document.getElementById('divCompareVendor').style.display = 'block';
						
						// Set all compare vendors to checked for all browsers other than IE and Opera
						if(!checkBrowser()) {
							checkDefaultCompareVendors(arrRes[1]);
						}
					}				
					else {
						document.getElementById('divCompareVendor').innerHTML = '';
						document.getElementById('divCompareVendor').style.display = 'none';
					}
				}
			}

			xmlHttp.open("GET",url,true);
			xmlHttp.send(null);
		}
	}
}

// This function is used to check all compare site vendors..
function checkDefaultCompareVendors(randomVendors) {
	var totalComVen = document.getElementById('chkCompareHid').value;
	var arrRandVend = randomVendors.split(",");

	for(intCmpVen = 0; intCmpVen <= totalComVen; intCmpVen++) {

		if(document.getElementById('chkCompare' + intCmpVen)) {	

			for (cntRand = 0; cntRand <= arrRandVend.length; cntRand++)	{

				if(document.getElementById('chkCompareHidVId' + intCmpVen).value == arrRandVend[cntRand]) {
					document.getElementById('chkCompare' + intCmpVen).checked = true;		
				}	
			}		
		}
	}
}

function openCompareVendorPopup(flagOpenPopup, objChkId) {
	
	if(document.getElementById('chkCompareHid')) {

		var totalComVen = document.getElementById('chkCompareHid').value;
				
		var flagChecked = false;
		var strLeft = 0;
		var screenHeight = screen.height;
		var screenWidth = screen.width;
		var winWidth = screenWidth / 3;
		var cntWindow = 0;
		var cntCompareCheck = 0;	
		
		if(flagOpenPopup) {
			
			// If function call from opera or IE when user click on search button we need to open search result popup only
			if(objChkId != 'search') {				
				
				if(document.getElementById('chkCompare' + objChkId).checked) {						
					
					var strUrl = document.getElementById("chkCompareHid" + objChkId).value;
					var vendorId = document.getElementById("chkCompareHidVId" + objChkId).value;					
					
					var newWindow = window.open(usPath + 'compare.php?compareVendor=' + vendorId, 'compareWin' + Math.random().toString().replace(".", ""), 'scrollbars=no,width=455,height=300,left=0,top=0,resizable=yes,screenX=0,screenY=0');
					compareSiteWindowArr.push(newWindow);
					compareSiteUrlArr.push(strUrl);
					compareSiteIdArr.push(vendorId);
					compareSiteCheckArr.push('chkCompare' + objChkId);
					newWindow.blur();
				}
			}
			else {
				
				/*if(!validateCompareBox()) {
					return false;
				}
				*/
				
				var intSiteChecked = 0;
				for(intCmpVen = 0; intCmpVen <= totalComVen; intCmpVen++) {
					if(document.getElementById('chkCompare' + intCmpVen)) {	
						if(document.getElementById('chkCompare' + intCmpVen).checked) {
							intSiteChecked++;
						}
					}
				}
				
				var cntCmp = 0;
				for(cnt = 0; cnt <= compareSiteWindowArr.length; cnt++) {
					if(compareSiteWindowArr[cnt]) {
						if(!compareSiteWindowArr[cnt].closed) {
							
							if(intSiteChecked == 2) {
								if(cntCmp == 0) {
									strLeft = 0;
									winWidth = screenWidth / 2;
								}
								else {
									strLeft = screenWidth / 2;
									winWidth = screenWidth / 2;
								}								
							}
							else {
								if(cntCmp > 2) {
									var popLeft = cntCmp % 3;
									strLeft = winWidth * popLeft;	
								}
								else {
									strLeft = winWidth * cntCmp;	
								}	
							}
							
							var checkId = compareSiteCheckArr[cnt];
							var newWindow = compareSiteWindowArr[cnt];
							
							newWindow.resizeTo(winWidth,screenHeight);
							newWindow.moveTo(strLeft,0);
							newWindow.location = compareSiteUrlArr[cnt];

							// Insert clicks into table for tracking purpose	
							var vendorId = compareSiteIdArr[cnt];
							document.getElementById(checkId).checked = false;
							insertSearchHits(vendorId);
							newWindow.focus();	
							cntCmp++;
						}
					}
				}			
				compareSiteCheckArr = new Array();
				compareSiteWindowArr = new Array();
				compareSiteUrlArr = new Array();
				compareSiteIdArr = new Array();
				
				submitFlightSearchButton(true, true);
			}
		}
		else {		
			
			/*if(!validateCompareBox()) {
				return false;
			}
			*/


			var intSiteChecked = 0;
			for(intCmpVen = 0; intCmpVen <= totalComVen; intCmpVen++) {
				if(document.getElementById('chkCompare' + intCmpVen)) {	
					if(document.getElementById('chkCompare' + intCmpVen).checked) {
						intSiteChecked++;
					}
				}
			}			
			
			//	return false;
			// Other vendor sites
			var cntWindow = 0;
			for(intCmpVen = 0; intCmpVen <= totalComVen; intCmpVen++) {
				if(document.getElementById('chkCompare' + intCmpVen)) {	
					if(document.getElementById('chkCompare' + intCmpVen).checked) {
						flagChecked = true;
						
						// If having only 2 vendors selected
						if(intSiteChecked == 2) {
							if(cntWindow == 0) {
								strLeft = 0;
								winWidth = screenWidth / 2;
							}
							else {
								strLeft = screenWidth / 2;
								winWidth = screenWidth / 2;
							}
						}
						else {
							if(cntWindow > 2) {
								var popLeft = cntWindow % 3;
								strLeft = winWidth * popLeft;	
							}
							else {
								strLeft = winWidth * cntWindow;	
							}	
						}
				
						/*if(cntWindow % 2 == 0) {
							strLeft = 0;
						}
						else {
							strLeft = winWidth * 2;			
						}
						*/
												
						var strUrl = document.getElementById("chkCompareHid" + intCmpVen).value;
						var vendorId = document.getElementById("chkCompareHidVId" + intCmpVen).value;
																
						
						var newWindow = window.open(usPath + 'compare.php?compareVendor=' + vendorId, 'compareWin' + Math.random().toString().replace(".", ""), 'scrollbars=no,width=' + winWidth + ',height=' + screenHeight + ',left=' + strLeft + ',top=0,resizable=yes');
						document.getElementById('chkCompare' + intCmpVen).checked = false;	
						compareSiteWindowArr.push(newWindow);
						compareSiteUrlArr.push(strUrl);
						compareSiteIdArr.push(vendorId);
						compareSiteCheckArr.push('chkCompare' + intCmpVen);										
						cntWindow++;
					}				
				}
			}		
		
			if(!flagChecked) {
				//document.getElementById('spnCompare').style.display = 'block';
				//return false;
			}	
			submitFlightSearchButton(true, true);
		}		
	}
	else {
		submitFlightSearchButton(false, true);
	}
}


function validateCompareBox() {
	var totalComVen = document.getElementById('chkCompareHid').value;
	var flagChecked = false;
	var cntCompareCheck = 0;	
	
	// If browser is other than IE and OPERA, ignore mandatory checks
	var testTotal = totalComVen;
	if(testTotal >= 2) {
		testTotal = 2;
	}
	else {
		testTotal = 1;
	}

	for(var intCmpVen = 0; intCmpVen <= totalComVen; intCmpVen++) {
		if(document.getElementById('chkCompare' + intCmpVen)) {	
			if(document.getElementById('chkCompare' + intCmpVen).checked) {
				cntCompareCheck++;
			}
		}
	}

	if(cntCompareCheck < testTotal) {
		document.getElementById('spnCompare').style.display = 'block';
		document.getElementById('fntCompare').innerHTML = testTotal;
		return false;
	}
	else {
		document.getElementById('spnCompare').style.display = 'none';
		return true;
	}
}


function insertSearchHits(vendorId) {
	GetXmlHttpObject();

	var travelType = document.getElementById('flightsOneWay').checked;

	if(travelType == false) {
		travelType = '2';
	}
	else {
		travelType = '1';
	}

	var url = usPath + "flightsearchajax.php?doAction=InsertSearchHits";
	url = url + "&vendorId=" + vendorId + "&departure=" + document.getElementById('departureHid').value + '&destination=' + document.getElementById('destinationHid').value + "&departureName=" + encodeURIComponent(document.getElementById('departureAirport').value) + "&destinationName=" + encodeURIComponent(document.getElementById('destinationAirport').value)  + "&departureDate=" + document.getElementById('departureDate').value
	+ "&returnDate=" + document.getElementById('returnDate').value + "&adult=" + document.getElementById('flightsPassenger').value + '&children=' + document.getElementById('flightsChildren').value + "&infant=" + document.getElementById('flightsInfants').value + '&cabin=' + document.getElementById('flightsCabin').value + '&travelType=' + travelType + "&code=" + document.getElementById('code').value + "&bannercode=" + document.getElementById('bannercode').value + "&cheapflightcode=" + document.getElementById('cheapflightcode').value + "&affiliate=" + document.getElementById('affiliate').value + '&r=' + Math.random().toString().replace(".", "");
	
	xmlHttp.onreadystatechange = function () {
		if(xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
			var strResponseTextVal = xmlHttp.responseText;					
		}
	}
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);	
}

function changeDeparture(objDepVal, regionName, recordType, countryCode) {
	GetXmlHttpObject();
	
	var url = usPath + "flightsearchajax.php?doAction=getlandingPageAirlines";
	url = url + "&departureIata=" + objDepVal + '&regionName=' + encodeURIComponent(regionName) + '&countryCode=' + encodeURIComponent(countryCode) + '&recordType=' + encodeURIComponent(recordType) + '&r=' + Math.random().toString().replace(".", "");
	
	xmlHttp.onreadystatechange = function () {
		if(xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
			var strResponseTextVal = xmlHttp.responseText;
			
			document.getElementById('divAirlines').innerHTML = strResponseTextVal;
			
		}
	}

	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);						
}


