var depreq;
var destreq;
var hasSearches;
var 	nosearch;
//iaian start
var randomnumber=Math.floor(Math.random()*5);
function header() {
		if (randomnumber == randomnumber){
		//document.getElementById("banner").setAttribute("class", "banner1");
			document.getElementById("banner").className = "banner1";}
 		if (randomnumber == "1") {
 			document.getElementById("banner").className = "banner2";}
		if (randomnumber == "2") {
			document.getElementById("banner").className = "banner3";}
		if (randomnumber == "3") {
			document.getElementById("banner").className = "banner4";}
		if (randomnumber == "4") {
			document.getElementById("banner").className = "banner5";}
} 
//iaian end
function checkSubmit() {
	if (document.form1._txtOutboundDepCity.value == "") {
		alert("A Valid Departure City is Required, Start Typing your Departure City For Results");
		document.form1._txtOutboundDepCity.focus();
		return false;
	}
	if (document.form1._txtOutboundDestCity.value == "") {
		alert("A Valid Destination City is Required, Start Typing your Destination City For Results");
		document.form1._txtOutboundDestCity.focus();
		return false;
	}
	
	if (document.form1.txtOutboundDepCity.value == "-1") {
		var depVal = document.form1.DepReq.autocompleter.findValue();
		if (depVal) {
			document.form1.txtOutboundDepCity.value = depVal;
		} else {
			alert("A Valid Departure City is Required, Start Typing your Departure City For Results");
			document.form1._txtOutboundDepCity.focus();
			return false;
		}
	}
	
	if (document.form1.txtOutboundDestCity.value == "-1") {
		var destVal = document.form1.DestReq.autocompleter.findValue();
		if (destVal) {
			document.form1.txtOutboundDestCity.value = destVal;
		} else {
			alert("A Valid Destination City is Required, Start Typing your Destination City For Results");
			document.form1._txtOutboundDestCity.focus();
			return false;
		}
	}

	if ( Date.parse(document.form1.cmbDateFrom.value) > Date.parse(document.form1.cmbDateTo.value) ) {
	  alert("Invalid Date Range.\nDeparture Date cannot be After Return Date")
		return false;
	}
	
	searchStr = document.form1._txtOutboundDepCity.value + "|" + document.form1.txtOutboundDepCity.value + "|" + document.form1._txtOutboundDestCity.value + "|" + document.form1.txtOutboundDestCity.value + "|" + document.form1.cmbDateFrom.value + "|" + document.form1.cmbDateTo.value + "|" + document.form1.adultno.value + "|" + document.form1.childno.value + "|" + document.form1.infantno.value + "^^";
	
	if (hasSearches) {
		currentCookie = readCookie("exitotravel");
		if (currentCookie.split('^^').length < 6) {
			eraseCookie("exitotravel");
			//alert("CREATE SEARCH COOKIE = " + searchStr + currentCookie);
			createCookie("exitotravel", searchStr + currentCookie, null);
		} else {
			var newCookie = "";
			temp = currentCookie.split('^^');
			for ( var i=0, len=temp.length-2; i<len; ++i ){
			//for ( var i=temp.length-2; i>=0; --i ){
				newCookie = newCookie + temp[i] + "^^";
			}
			eraseCookie("exitotravel");
			//alert("CREATE SEARCH COOKIE = " + searchStr + newCookie);
			createCookie("exitotravel", searchStr + newCookie, null);
		}
	} else {
		//alert("CREATE SEARCH COOKIE = " + searchStr);
		createCookie("exitotravel", searchStr, null);
		hasSearches = true;
	}
	
	//var pageTracker = _gat._getTracker("UA-3677794-1");
	//pageTracker._initData();
	//pageTracker._trackPageview('/submitted_search');
	//STOP FROM SUMBIT FOR TESTING
	//return false;
	
}

function findValueDepReq(li) {
	if( li == null ) return false;
	// if coming from an AJAX call, let's use the CityId as the value
	if( !!li.extra ) var sValue = li.extra[0];
	// otherwise, let's just display the value in the text box
	else var sValue = li.selectValue;
	// alert("The value you selected was: " + sValue);
	$("#DepCode").val(sValue);
	//alert("DepCode = " + $("#DepCode").val());
}

function addDays(myDate,days) {
  return new Date(myDate.getTime() + days*24*60*60*1000);
}

function niceDate(date){
	var d  = date.getDate();
	var day = (d < 10) ? '0' + d : d;
	var m = date.getMonth() + 1;
	var month = (m < 10) ? '0' + m : m;
	var yy = date.getYear();
	var year = (yy < 1000) ? yy + 1900 : yy;
	return month + "/" + day + "/" + year;
}

function findValueDestReq(li) {
	if( li == null ) return alert("No match!");
	// if coming from an AJAX call, let's use the CityId as the value
	if( !!li.extra ) var sValue = li.extra[0];
	// otherwise, let's just display the value in the text box
	else var sValue = li.selectValue;
	// alert("The value you selected was: " + sValue);
	$("#DestCode").val(sValue);
	//alert("DestCode = " + $("#DestCode").val());
}

function formatItem(row) {
	return row[0];
}

var win=null;

function NewWindow(mypage,myname,w,h,scroll,pos){
	if(pos=="random"){LeftPosition=(screen.width)?Math.floor(Math.random()*(screen.width-w)):100;TopPosition=(screen.height)?Math.floor(Math.random()*((screen.height-h)-75)):100;}
	if(pos=="center"){LeftPosition=(screen.width)?(screen.width-w)/2:100;TopPosition=(screen.height)?(screen.height-h)/2:100;}
	else if((pos!="center" && pos!="random") || pos==null){LeftPosition=0;TopPosition=20}
	settings='width='+w+',height='+h+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=no';
	win=window.open(mypage,myname,settings);
}

$(document).ready(function() {

	var depreq = $("#DepReq").autocompleteArray(
		depcities,
		{
			delay:10,
			minChars:1,
			matchSubset:1,
			matchContains: 1,
			onItemSelect:findValueDepReq,
			onFindValue:findValueDepReq,
			autoFill:false,
			maxItemsToShow:10,
			formatItem:formatItem
		}
	);
	var destreq = $("#DestReq").autocompleteArray(
		destcities,
		{
			delay:10,
			minChars:1,
			matchSubset:1,
			matchContains: 1,
			onItemSelect:findValueDestReq,
			onFindValue:findValueDestReq,
			autoFill:false,
			maxItemsToShow:10,
			formatItem:formatItem
		}
	);

});


function checkCookies() {
	savedSearches = readCookie("exitotravel");
	//alert(savedSearches);
	if (savedSearches == null) {

		//alert("HERE");
		hasSearches = false;
		displaywhyexito(nosearch);
	} else {
		hasSearches = true;      
		displaySearches(savedSearches);
	}
}
function displaywhyexito(nosearch) {
	document.getElementById('exitoInfo').innerHTML = "<ul>Why is eXito Different? <li>We have special negotiated fares that are 20-30% lower.</li> <li>We do not charge booking fees for promotional fares.*</li><li>We offer multi-city itineraries for much less!</li> <li>You can reach a person on the phone should you need help.</li></ul>";
} 

function displaySearches(searches) {
	theSearches = searches.split('^^');
	var line = "";
	for ( var i=0, len=theSearches.length-1; i<len; ++i ){
		tripdata = theSearches[i].split("|");
	line = line + "<li><a href = '#' onclick = \"doSavedSearch('" + tripdata[0] + "', '" + tripdata[1] + "','" + tripdata[2] + "','" + tripdata[3] + "','" + tripdata[4] + "','" + tripdata[5] + "','" + tripdata[6] + "','" + tripdata[7] + "','" + tripdata[8] + "')\">" + tripdata[1] + " - " + tripdata[3] + " " + tripdata[4] + " - " + tripdata[5] + "</a></li>";
	}
	document.getElementById('exitoInfo').innerHTML = "<ul><b>Previous Searches</b><br/>" + line ;
}

function doSavedSearch(depStr, depCode, destStr, destCode, fromDate, toDate, adults, children, infants) {
	document.form1._txtOutboundDepCity.value = depStr;
	document.form1.txtOutboundDepCity.value = depCode;
	document.form1._txtOutboundDestCity.value = destStr;
	document.form1.txtOutboundDestCity.value = destCode;
	document.form1.cmbDateFrom.value = fromDate;
	document.form1.cmbDateTo.value = toDate;
	document.form1.adultno.value = adults;
	document.form1.childno.value = children;
	document.form1.infantno.value = infants;
	document.form1.submit();
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

