window.onkeydown = KillSpaceBar;
var dA
var aA
var obj = new Array()
obj[0] = "departureAirportText"
obj[1] = "arrivalAirportText"
var http_request = false;
var url= "/opodo/flights/resolveLocation?query=";

var resultsArray = new Array()
resultsArray[0] = "departureAirport0Text"
resultsArray[1] = "arrivalAirport0Text"
resultsArray[2] = "departureAirport1Text"
resultsArray[3] = "arrivalAirport1Text"
resultsArray[4] = "departureAirport2Text"
resultsArray[5] = "arrivalAirport2Text"	
resultsArray[6] = "departureAirport3Text"
resultsArray[7] = "arrivalAirport3Text"
resultsArray[8] = "departureAirport4Text"
resultsArray[9] = "arrivalAirport4Text"
resultsArray[10] = "departureAirport5Text"
resultsArray[11] = "arrivalAirport5Text"	
resultsArray[12] = "departureAirport6Text"
resultsArray[13] = "arrivalAirport6Text"
resultsArray[14] = "departureAirport7Text"
resultsArray[15] = "arrivalAirport7Text"
resultsArray[16] = "departureAirport8Text"
resultsArray[17] = "arrivalAirport8Text"	
resultsArray[18] = "departureAirport9Text"
resultsArray[19] = "arrivalAirport9Text"

  /*
  find the position of an object in the page to position the box 
  */
function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

function getRequest(){
  	var request = false;
    try {
  		request = new XMLHttpRequest();
	} catch (trymicrosoft) {
  		try {
    		request = new ActiveXObject("Msxml2.XMLHTTP");
  		} catch (othermicrosoft) {
    		try {
      			request = new ActiveXObject("Microsoft.XMLHTTP");
    		} catch (failed) {
      			request = false;
    		}
  		}
	}
    if (request.overrideMimeType) {
      request.overrideMimeType('text/xml');
    }
    if (!request) {
      alert('Cannot create XMLHTTP instance');
      return false;
    }
    return request;
}

function hideDisplay(){
	if (display) display.style.display='none';
}
var display;
function search(query,locationType,index,e){
	if(document.getElementById(locationType+'Airport')){
	var code = document.getElementById(locationType+'Airport');
	}else{
	var code = document.getElementById(locationType+'Airport'+index);
	}
	if (!e){var e = window.event}
	if (e.keyCode){code = e.keyCode;}
	else if (e.which){code = e.which;}
	if(code != 37 || code != 39){
		if(code.value != ""){
//			code.value = "";
		}
	}
	if (display) display.style.display='none';
	if(!index) index = "";
	display = document.getElementById(locationType+'Airport'+index+'Box');
	display.style.display='none';
	if (4>query.value.length) {
		return;
    }
	http_request = getRequest();
    http_request.onreadystatechange = getOnreadystatechangeFunction(query,locationType,index);
    http_request.open('GET', url + query.value, true);
    http_request.send(null);
}
function getTableHeight(oframe,otable){
	document.getElementById(oframe).style.height = document.getElementById(otable).parentNode.offsetHeight+3+"px";
	document.getElementById("departureAirportBox").style.height = document.getElementById(otable).offsetHeight+10+"px";
	document.getElementById("arrivalAirportBox").style.height = document.getElementById(otable).offsetHeight+10+"px";
}
var hasresults = false;

function checkOld(query,locationType){
	if(document.getElementById(locationType+'Airport')){
		var code = document.getElementById(locationType+'Airport');
	}else{
		var code = document.getElementById(locationType+'Airport'+index);
	}
if(query.value.length > 2 && query.value.length < 4 ){
			code.value = "";
	}
}


function searchCode(query,locationType,index){
    if(!index) index = "";
    var code = document.getElementById(locationType+'Airport'+index);
    if(code.value != ""){code.value = "";}
    if (display) display.style.display='none';
    display = document.getElementById(locationType+'Airport'+index+'Box');
    display.style.display='none';
    http_request = getRequest();
    http_request.onreadystatechange = getOnreadystatechangeFunction(query,locationType,index);
    http_request.open('GET', url + query.value, true);
    http_request.send(null);
}

var isAmbiguous = false;

function look4iata(str,locationType,index){
var nr = index
    isAmbiguous = false;
	if (!index) index="";
	hasresults=true;
	if (3!=str.length) {return;}
	http_request = getRequest();
    http_request.open('GET', url + str, false);
    http_request.send(null);
    var results = http_request.responseText;
    eval("locations = "+results);
    //["222","Long Island & Rum Cay","Rum Cay","Bahamas","RCY"]
    var code = '';
    var text = '';
    var isIataCode = false;
    var isCity = false;
    for(var i=0;i<locations.length;i++) {
		var location = locations[i];
		var locationCode = location[4];
		var locationCity = location[1];
        var locationText = location[2]+', '+location[1]+', '+location[3]+' ['+location[4]+']';
		if (locationCity.toUpperCase() == str.toUpperCase()){
            code = locationCode;
            text = locationText;
            isCity = true;
            if (locations.length==1){
                break;
            }
        }
        if(locationCode == str.toUpperCase()){
            code = locationCode;
            text = locationText;
            isIataCode = true;
            if (locations.length==1){
                break;
            }
		}				
	}
    if (isCity && isIataCode)
        isAmbiguous = true;
    if (!isCity && !isIataCode)
        isAmbiguous = true;
    if (isAmbiguous)
        return;
if(document.getElementById(locationType+'Airport'+nr)){
		    document.getElementById(locationType+'Airport'+nr).value = code;
			document.getElementById(locationType+'Airport'+nr+'Text').value = text;

}else{
		    document.getElementById(locationType+'Airport').value = code;
			document.getElementById(locationType+'AirportText').value = text;
	}
}

function closePredictive(locationType,index)
{
	var id = locationType+'Airport'+index;
	document.getElementById(id+'Box').innerHTML='';
	document.getElementById(id+'Box').style.display='none';
	offOn();
}

function update(locationType,index,locationCode,locationText){
	var id = locationType+'Airport'+index;
	document.getElementById(id).value = locationCode;
	document.getElementById(id+'Text').value = locationText;
	document.getElementById(id+'Box').innerHTML='';
	document.getElementById(id+'Box').style.display='none';
	offOn()
	if(locationType == "arrival"){aA = locationCode}
	else{dA = locationCode}
}
function updateInput(e,locationType,index,locationCode,locationText,x){
	var id = locationType+'Airport'+index;
	document.getElementById(id).value = locationCode;
	document.getElementById(id+'Text').value = locationText;
	if(locationType == "arrival"){aA = locationCode}
	else{dA = locationCode}

	return false
}


function setFocus(e, locationType,index){
	if (!e){var e = window.event}
	if (e.keyCode){code = e.keyCode;}
	else if (e.which){code = e.which;}
	if(code == 13 ){
		if(locationType == "departure"){
			var arr = document.getElementById("arrivalAirport"+ index +"Text");
			arr.focus();
		}else{
		var number = new Number(index);
		number.toString();
			number = number+1
			if(document.getElementById("departureAirport"+ number +"Text")){
			var arr = document.getElementById("departureAirport"+ number +"Text");
			arr.focus();
			}
		}
	}
	
	return false;
}

function enterupdate(e,locationType,index,locationCode,locationText,x){
if (!e){var e = window.event}
if (e.keyCode){code = e.keyCode;}
else if (e.which){code = e.which;}

//window.status = "code is " + code;

if(code == 9 )
	return false;

if(window.event && window.event.keyCode == 13 || code == 13 || window.event && window.event.keyCode == 9 || code == 9){
	var id = locationType+'Airport'+index;
	document.getElementById(id).value = locationCode;
	document.getElementById(id+'Text').value = locationText;
	document.getElementById(id+'Box').innerHTML='';
	document.getElementById(id+'Box').style.display='none';
	offOn()
	if(locationType == "arrival"){aA = locationCode}
	else{dA = locationCode}
	return false;
	}
}
function noenter() 
{
  return !(window.event && window.event.keyCode == 13); 
}

function changeClass(who,what){
	who.className = what					
}

function findMe(me){
for(i=0;i<=resultsArray.length-1;i++){
	if(me.id == resultsArray[i]){
		if(i != resultsArray.length-1){
				document.getElementById(resultsArray[i+1]).focus()
			}else{
				document.getElementById(resultsArray[0]).focus()			
			}
		}
	}
}

function getCode(e)
{
	var code;
	e = e || window.event;
	if (e.keyCode) 
	{
		code = e.keyCode;
	} 
	else if (e.which) 
	{
		code = e.which;
	}
	
	return code;
}


function KillSpaceBar(e){
var code;
e = e || window.event;
if (e.keyCode) {
	code = e.keyCode;
	} else if (e.which) {
	code = e.which;
}

//alert("code is: " + code);

//|| code == 9) 

if (code == 38 || code == 40 )
{
	e.cancelBubble = true;
	e.returnValue = false;
	if (e.stopPropagation) {
		e.stopPropagation();
		e.preventDefault();
		}
		return false;
	}
}


var rev = false
function keyCodeAndArray(evt,me)
{
KillSpaceBar(evt)
	evt = evt ? evt : event;
    var keyCode = evt.keyCode;
	
	if(keyCode == 40 || keyCode == 9){
		if(rev == true){
			resultsArray.reverse()
			rev = false
		}
		findMe(me)
	}
	if(keyCode == 38){
		if(rev == false){
			resultsArray.reverse()
			rev = true
		}
		findMe(me)
	}
}
function selectit(evt,w){
	evt = evt ? evt : event;
    var keyCode = evt.keyCode;
	if(keyCode == 40 || keyCode == 9){
		if(checkArray(w.id,resultsArray)){
			checkNextArray[w.id,resultsArray]//.focus();
			}else{
			if(document.getElementById(resultsArray[0])){
				document.getElementById(resultsArray[0]).focus();
			}
		}
	}
}  

function checkArray(thisitem,thisArray){
var i=0
while (i < thisArray.length) {
	if (thisArray[i] == thisitem){
		return true
	} else {
		i++;
		}
	}
	return false
}

function checkNextArray(thisitem,thisArray){
var i=0
while (i < thisArray.length) {
	if (thisArray[i] == thisitem){
		if(thisArray[i+1]){
			return document.getElementById(thisArray[i+1]).focus()
		}
	} else {
		i++;
		}
	}
	return false
}


function removeArray(array) {
var i = 0;
while (i < array.length) {
		array.splice(i, 1);
	}
}
function fiPos( oLinks , oLinkIam) {
var left = 0
var top = 0
var oLink = document.getElementById(oLinks)
  if( oLink.offsetParent ) {
    for( var posX = 0, posY = 0; oLink.offsetParent; oLink = oLink.offsetParent ) {
      posX += oLink.offsetLeft;
	  posY += oLink.offsetTop + 20;
  }
	left = left-=0; posX = posX -=0; iSleft = left += posX;	 
	top = top-=0; 	posY = posY -=0; iStop = top += posY;	 
		try{document.getElementById(oLinkIam).style.left = iSleft + "px";}catch(err){}
		try{document.getElementById(oLinkIam).style.top = iStop +"px";}catch(err){}
	 }
}
function onOff(who)
{
for(i=0;i<obj.length;i++)
	{
		if(document.getElementById(obj[i]) != who)
		{
			dis = obj[i]
			//try{document.getElementById(dis).disabled = true}catch(err){}
		}
	}
}
function offOn()
{
for(i=0;i<obj.length;i++)
	{
		dis = obj[i]
		//try{document.getElementById(dis).disabled = false}catch(err){}
	}
}
function onewayTrip2(){
    var fsp = document.flightsearchpod;
    fsp.returnDay.selectedIndex = "0";
    fsp.returnMonth.selectedIndex = "0";
    fsp.returnDay.disabled = true;
    fsp.returnMonth.disabled = true;
    fsp.returnTime.disabled = true;
    tripType = "O";
    document.getElementById('returncalendarlink').style.display='none';
}

function returnTrip2(){
    var fsp = document.flightsearchpod;
    fsp.returnDay.disabled = false;
    fsp.returnMonth.disabled = false;
    fsp.returnTime.disabled = false;
	calcDay();
    tripType = "R";
    document.getElementById('returncalendarlink').style.display='';

}
var Maximum = 10;
function setOptions2(chosen) {
    frm = document.flightsearchpod;
    var selbox = frm.numberOfChildren;
    selbox.options.length = 0;
    var selbox2 = frm.numberOfInfants;
    selbox2.options.length = 0;
    var box1 = frm.numberOfAdults.options[frm.numberOfAdults.selectedIndex].value;
    for(q=0;q<=box1;q++){
         selbox2.options[selbox2.options.length] = new Option(q,q);
    }
    for(i=0 ; i < Maximum ; i++){
        if (chosen == i) {
            selbox.options[selbox.options.length] = new Option(0,0);
            for(z=1;z <(Maximum-i) ; z++){
              selbox.options[selbox.options.length] = new Option(z,z);
            }
        }
    }
}
function verifyTab(e, obj)
{
	var code = getCode(e);
	if (code == 9)
	{
		/**
		 * If the predictive text is not open, I leave the tab go
		 * to the next field, if it is open, I kill the tab so we loop
		 * through the predictive text options
		*/
		if (obj.value.length > 3)
		{
			killEvent(e);
			return false;
		}
	}
}
function killEvent(e)
{
	e.cancelBubble = true;
	e.returnValue = false;
	if (e.stopPropagation) 
	{
		e.stopPropagation();
		e.preventDefault();
	}
}

function changeValue(checkbox){
	if(checkbox.value=="false"){
		checkbox.value="true"
	}else if(checkbox.value=="true"){
		checkbox.value="false"
	}
}

function enableSubmit(who,event){
if(document.getElementById("departureAirportText").value.length <3 ){
	document.getElementById("departureAirportText").focus()
	return false
}
if(document.getElementById("arrivalAirportText").value.length <3 ){
	document.getElementById("arrivalAirportText").focus()
	return false
}
	var ok = true;
	if(	document.getElementById("departureAirport").value == "" ){
		var dep = document.getElementById("departureAirportText");
		if(dep.value.length > 3 ){
			search(dep,'departure','',event);
			if(hasresults){
				ok = false;
			}
		}
		if(dep.value.length ==3 ){
			look4iata(dep.value,'departure');
            if (isAmbiguous){
                ok = false;
                searchCode(dep,'departure');
            } else { 
                ok=true;
            }
		}
		
	}
	if(	ok && document.getElementById("arrivalAirport").value == "" ){
		var arr = document.getElementById("arrivalAirportText");
		if(arr.value.length > 3){
			search(arr,'arrival','',event);
			if(hasresults){
				ok = false;
			}
		}
		if(arr.value.length ==3){
			look4iata(arr.value,'arrival');
            if (isAmbiguous){
                ok = false;
                searchCode(arr,'arrival');
            } else { 
                ok=true;
            }
		}
	}
	if(ok){
		if(document.getElementById('holdingScreen')){
		showHoldingScreen(who);
		animateIt();	
		}
	if(document.getElementById('adSide')){
		document.getElementById('adSide').style.display='none';
	}
		
		who.submit();
	}
}


function enableSubmitMulti(who,howMany){
	if(document.getElementById('adSide')){
		document.getElementById('adSide').style.display='none';
	}

	var ok = true;
	for(i=0; i<= howMany; i++){
	if(document.getElementById("departureAirport"+i).value == "" ){
		var dep = document.getElementById("departureAirport"+ i +"Text");
		if(dep.value.length > 3 ){
			search(dep,'departure',i);
			if(hasresults){
				ok = false;
			}
		}
		if(dep.value.length ==3 ){
			look4iata(dep.value,'departure',i);
        	    if (isAmbiguous){
        	        ok = false;
    	            searchCode(dep,'departure',i);
	            } else { 
                ok=true;
            	}
			}
		if(dep.value.length < 3 ){
		if(i < 2){
				ok = false;
				dep.focus()
				return false;
			}
		}
	}
}
	for(z=0; z<= howMany; z++){
	if(	ok && document.getElementById("arrivalAirport"+z).value == "" ){
		var arr = document.getElementById("arrivalAirport"+ z +"Text");
		if(arr.value.length > 3){
			search(arr,'arrival',z);
			if(hasresults){
				ok = false;
			}
		}
		if(arr.value.length ==3){
			look4iata(arr.value,'arrival',z);
            if (isAmbiguous){
                ok = false;
                searchCode(arr,'arrival',z);
            } else { 
                ok=true;
    	        }
			}
		if(arr.value.length < 3 ){
			if(z <2){
			ok = false;
			arr.focus()
			return false;
			}
		}
	}
}
	if(ok){
	if(document.getElementById("arrivalAirport"+2).value == ""){
		howMany = howMany-1
	}
		if(document.getElementById('holdingScreen')){
		showHoldingScreen(who,howMany);
		animateIt();	
		}
		who.submit();
	}
}