var IE=false;
var N=navigator.appName;
var V=navigator.appVersion.split(';')
V=parseInt(V[1].substring(6,7));
if(N=='Microsoft Internet Explorer'){IE=true;}

/* Form airport lookup functionality start here */
var arpCodeNames=new Array();
var load= new Image();
var found = 1;
var click_full = 0;  // This variable is for checking that mouse is clicked on some search or outside of the div for selecing default first 

load.src="/images/common/loading.gif";




function changeReturnDate(objDate) {
	
	var objReturnDate = "";
	if(objDate == "ddate") {
		objReturnDate = "rdate";
	}else if(objDate == "Bdate") {
		objReturnDate = "Brdate"; 
	}else if(objDate == "cidate") {
		objReturnDate = "codate"; 
	}
	var departDateStr = document.getElementById(objDate).value;

	var departArr = departDateStr.split("/");
	var departDate = new Date(departArr[2],departArr[1]-1,departArr[0]);
	var arrivalDate = "";

	arrivalDate = new Date(departDate.setDate(departDate.getDate() + 7));
	var mon = arrivalDate.getMonth();
	var day = arrivalDate.getDate();
	var dayStr = "";
	var monStr = "";
	

	if(parseInt(day) <= 9) {
		dayStr = "0"+day;
	}else{
		dayStr = day;
	}
	if((parseInt(mon)+1) <= 9) {
		monStr = "0"+(parseInt(mon)+1);
	}else{
		monStr = (parseInt(mon)+1);
	}
	
    var arrivalDateStr = dayStr+"/"+monStr+"/"+arrivalDate.getFullYear();
	if(isNaN(parseInt(day)) || isNaN(parseInt(mon))) {
		document.getElementById(objReturnDate).value = "dd/mm/yyyy";	
	}else{
		document.getElementById(objReturnDate).value = arrivalDateStr;
	}

}


function getXmlHttpRequestObject() {
	if (window.XMLHttpRequest) {
		return new XMLHttpRequest();
	} else if(window.ActiveXObject) {
		return new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		alert("Your Browser Stucks!\nIt's about time to upgrade don't you think?");
	}
}
var searchKey = getXmlHttpRequestObject();

function apLookup(){
	this.aArr=new Array();
}
apLookup.prototype.assignArray=function(apL){
	this.aArr=apL;
};

/* Code for ajax Call */
apLookup.prototype.ajaxRequest=function(key,input,div,country,match,show,len){
	if (searchKey.readyState == 4 || searchKey.readyState == 0) {
		searchKey.open("GET", '/international/common/js/apArray.mjs?key=' + key+"&country_code="+country+"&match_type="+match, true);
		searchKey.onreadystatechange = function(){
			if (searchKey.readyState == 4) {
				arpCodeNames=new Array();
				eval(searchKey.responseText)
				this.flyshowLookup=new apSearch(arpCodeNames,input,div,country,match,show,len)
				this.flyshowLookup.onTextChange(false)
			}
		}
		searchKey.send(null);
	}		
}

apLookup.prototype.getMatches=function(str,apL,show,type,len,country){
	if(!len){len=5}
	var ctr=0;
	var lookup='';
	for(var i in this.aArr){
		if((this.aArr[i].CI) && (this.aArr[i].CO)) {
			lookup=this.aArr[i].CI + ', ' + this.aArr[i].CO + ' (' + this.aArr[i].APC + ') - '+this.aArr[i].AP;
		}
		if(lookup.toLowerCase().indexOf(str.toLowerCase()) != -1){
			var displayName=this.aArr[i].CI + ', ' + this.aArr[i].CO + ' (' + this.aArr[i].APC + ') - '+this.aArr[i].AP;
			var val = displayName;
			var sInp = str;
			var st = val.toLowerCase().indexOf(sInp.toLowerCase() );
			var output = val.substring(0,st) + "<b>" + val.substring(st, st+sInp.length) + "</b>" + val.substring(st+sInp.length);
			apL.push(output);
			ctr++;
		}
		if(ctr == (show)) 
			break;		
	 }

	// to shifl all airport on top 
	// Fill Default Message if no Result found 
	var allairportTag=false;
	 for(var j in apL){
		if((apL[j].toString().toLowerCase().indexOf('all airport') != -1) && !allairportTag){
			allairportTag=true;
			var tmpAllap=apL[j]
			delete apL[j];
			apL.unshift(tmpAllap);
		}
	}
	//if(apL.length > len){document.getElementById('').style.display='none';}
	//else{document.getElementById('ADT').style.display='';}
		
	/*if(apL.length > 0){
		document.getElementById('ADT').style.display='none';
		document.getElementById('CHD').style.display='none';
		document.getElementById('INF').style.display='none';
		
	}else{
		document.getElementById('ADT').style.display='';
		document.getElementById('INF').style.display='';
		document.getElementById('CHD').style.display='';
		
	}*/
	// set Default Message if not result return by query 
	if((searchKey.readyState == 4 && searchKey.responseText==99) || (arpCodeNames.length>0 && apL.length==0 && searchKey.readyState == 4)){
		found = 0;
		//apL.push("No matching city/airport found. Try entering first <br />3 letters and select from the list.")
		if(country == 'US'){
			apL.push("No matching US city/airport found. Try entering first <br />3 letters and select from the list.")
		}else if(country == 'IN'){
			apL.push("No match found. Try entering first 2 letters.")
		}else if(country == 'GB~IN'){
			apL.push("No matching Great Britain or Indian city/airport found. Try entering first <br />3 letters and select from the list.")
		}else if(country.indexOf('GB')!=-1){
			apL.push("No matching Great Britain city/airport found. Try entering first <br />3 letters and select from the list.")
		}else{
			apL.push("No match found. Try entering first 2 letters.")
		}
	
	} else {
		found = 1;
	}

	/*if(apL.length > 0){
		document.getElementById('ADT').style.display='none';
		document.getElementById('CHD').style.display='none';
		document.getElementById('INF').style.display='none';
		
	}else{
		document.getElementById('ADT').style.display='';
		document.getElementById('INF').style.display='';
		document.getElementById('CHD').style.display='';
		
	}*/
		
};

function apSearch(aArr,oText,oDiv,country_code,match_type,show,len){
	this.oText=oText;
	this.oDiv=oDiv;
	this.oCountery=country_code
	this.oMatch=match_type
	this.show=show;
	this.olen=len;
	this.cur=-1;
	this.db=new apLookup();
	this.db.assignArray(aArr);
	//this.oDiv.style.width=this.oText.offsetWidth+120;;
	//this.oDiv.style.top=getTop(this.oText) +'px';
	//this.oDiv.style.left=getLeft(oText) +'px';
	oText.onkeyup=this.keyUp;
	oText.onkeydown=this.keyDown;
	oText.apSearch=this;
	oText.onblur=this.hideairport;
}

apSearch.prototype.hideairport=function(){
	this.apSearch.oDiv.style.display = "none";
	document.getElementById("showlookupframe").style.display = "none";
	if(this.apSearch.oText.value.length >=3){
		if(click_full == 0) {
			this.apSearch.hideairportions();
		}
	}
};

apSearch.prototype.selectText=function(iStart,iEnd){
	if(this.oText.createTextRange) /* For IE */	{
		var oRange=this.oText.createTextRange();
		oRange.moveStart("character",iStart);
		oRange.moveEnd("character",iEnd-this.oText.value.length);
		oRange.select();
	}else if(this.oText.setSelectionRange) /* For Mozilla */{
		this.oText.setSelectionRange(iStart,iEnd);
	}
	this.oText.focus();
};

apSearch.prototype.textComplete=function(sFirstMatch){
	if(this.oText.createTextRange || this.oText.setSelectionRange){
		var iStart=this.oText.value.length;
		this.selectText(iStart,sFirstMatch.length);
	}
};

apSearch.prototype.keyDown=function(oEvent){
        oEvent=window.event || oEvent;
        iKeyCode=oEvent.keyCode;
        switch(iKeyCode){
                case 38: //up arrow
                        this.apSearch.moveUp();
                        break;
                case 40: //down arrow
                        this.apSearch.moveDown();
                        break;
		case 9: //tab key
                        this.apSearch.moveForward();
			break;
                case 13: //return key
                        this.apSearch.hideairportions();
                        return false;
                        break;
		case 27: //escape key
                        this.apSearch.hideairportions();
                        return false;
                        break;
		case 17: 
				        return false;
                        break;
                }
};

apSearch.prototype.moveDown=function(){
	if((this.oDiv.childNodes.length == 1) && (found == 0)) {
		this.oText.value = "";
		this.oDiv.style.display="none";
		document.getElementById("showlookupframe").style.display = "none";

	}else if(this.oDiv.childNodes.length>0 && this.cur<(this.oDiv.childNodes.length-1)){
		++this.cur;
		for(var i=0;i<this.oDiv.childNodes.length;i++){
			if(i==this.cur){
				this.oDiv.childNodes[i].className="over";
				this.oText.value=this.oDiv.childNodes[i].innerHTML.stripHTML();
			}else{
				this.oDiv.childNodes[i].className="";
			}
		}
	}
};

apSearch.prototype.moveForward=function(){
	if(this.oText.value != "") {
		if((this.oDiv.childNodes.length <=0) && (click_full == 0)) {
			this.oText.value = "";
			this.oText.className="";
			document.getElementById("showlookupframe").style.display = "none";
		}
		if(this.oDiv.childNodes.length>0 && this.cur<(this.oDiv.childNodes.length-1)){
			for(var i=0;i<this.oDiv.childNodes.length;i++){
				if(i==this.cur){
					this.oDiv.childNodes[i].className="over";
					this.oText.value=this.oDiv.childNodes[i].innerHTML.stripHTML();
					click_full = 1;
					document.getElementById("showlookupframe").style.display = "none";
					this.oDiv.style.display = "none";
					return false;
				}else{
					this.oDiv.childNodes[i].className="";
					document.getElementById("showlookupframe").style.display = "none";
					this.oDiv.style.display = "none";
					return false;

				}
			}
		}
	}
};

apSearch.prototype.moveUp=function(){
	if(this.oDiv.childNodes.length>0 && this.cur>0){
		--this.cur;
		for(var i=0;i<this.oDiv.childNodes.length;i++){
			if(i==this.cur){
				this.oDiv.childNodes[i].className="over";
				this.oText.value=this.oDiv.childNodes[i].innerHTML.stripHTML();
			}else{
				this.oDiv.childNodes[i].className="";
			}
		}
	}
};
apSearch.prototype.hideairportions = function (){

	/*document.getElementById('ADT').style.display='';
	document.getElementById('INF').style.display='';
	document.getElementById('CHD').style.display='';*/
	this.oisC=false;
	if((this.oDiv.childNodes.length == 0) && (found == 0)) {
		this.oText.value = "";
		this.oDiv.style.display="none";
		document.getElementById("showlookupframe").style.display = "none";
		click_full = 0;
		this.oText.className = "";
	}
	if(this.cur<(this.oDiv.childNodes.length==1))
	{
		++this.cur;
	}
	if(this.oDiv.childNodes.length>0){
	         for(var i=0;i<this.oDiv.childNodes.length;i++){
				
                        if(i==this.cur){
                                this.oDiv.childNodes[i].className="over";
								if(found == 1) {
									this.oText.value=this.oDiv.childNodes[i].innerHTML.stripHTML();
									click_full = 1;
									this.oDiv.style.display = "none";
									document.getElementById("showlookupframe").style.display = "none";
								} else {
									this.oText.value="";
									this.oDiv.style.display = "none";
									document.getElementById("showlookupframe").style.display = "none";
								}
                        }else{
                              	
				this.oDiv.childNodes[i].className="";
				this.oDiv.style.display = "none";
                        }
						
       		}
	}	
	this.oDiv.innerHTML="";
	this.oDiv.style.display="none";
	document.getElementById("showlookupframe").style.display = "none";
};

apSearch.prototype.keyUp=function(oEvent){
	var sKey=escape(this.apSearch.oText.value);
	this.ajax=new apLookup();
	oEvent=oEvent || window.event;
	var iKeyCode=oEvent.keyCode;
	if((iKeyCode >= 47 && iKeyCode <= 57)) {
		this.apSearch.oText.value = "";
	   this.apSearch.onTextChange(false); 
	}else if((sKey.indexOf('%')==0)) {
	   this.apSearch.oText.value = "";
	   this.apSearch.onTextChange(false); 
	}else if(iKeyCode==8 || iKeyCode==46){
		this.apSearch.onTextChange(false); 
	}else if (iKeyCode < 32 || (iKeyCode >= 33 && iKeyCode <= 46) || (iKeyCode >= 112 && iKeyCode <= 123) || ((iKeyCode >= 96 && iKeyCode <= 105))){
    	}else{
		if(sKey.length == 3){
			this.apSearch.oText.className = "loading";
			document.getElementById("showlookupframe").style.display = "none";
			arpCodeNames.length = 0;
			this.ajax.ajaxRequest(sKey,this.apSearch.oText,this.apSearch.oDiv,this.apSearch.oCountery,this.apSearch.oMatch,this.apSearch.show,this.apSearch.olen)
		}
		this.apSearch.onTextChange(true); /* with apSearch */
	}
};
apSearch.prototype.positionairport=function(){
	var oNode=this.oText;
	var x=0,y=oNode.offsetHeight;
	while(oNode.offsetParent && oNode.offsetParent.tagName.toUpperCase() != 'BODY'){
		x+=oNode.offsetLeft;
		y+=oNode.offsetTop;
		oNode=oNode.offsetParent;
	}
	if(V==6 || V==7){
	x+=oNode.offsetLeft+1;
	y+=oNode.offsetTop+1;
	}else{
	x+=oNode.offsetLeft;
	y+=oNode.offsetTop;
	}
	this.oDiv.style.top=y+"px";
	this.oDiv.style.left=x+"px";
	
	document.getElementById("showlookupframe").style.top = y+"px";
	document.getElementById("showlookupframe").style.left = this.oDiv.style.left;

}

apSearch.prototype.onTextChange=function(bTextComplete){
	var txt=this.oText.value;
	var oThis=this;
	this.cur=-1;
	click_full = 0;
	this.oDiv.style.display = "none";

	if(txt.length<3){
		
	}
	if(txt.length >2 ){
		while(this.oDiv.hasChildNodes())
		this.oDiv.removeChild(this.oDiv.firstChild);
		var aStr=new Array();
		this.db.getMatches(txt,aStr,this.show,this.oText,this.olen,this.oCountery)
		
		if(!aStr.length) {this.hideairportions ;return}
		if(bTextComplete) this.textComplete(aStr[0]);
		this.positionairport();
		var height = 0;
		var width = 210;
		for(i in aStr){
			var oNew=document.createElement('div');
			//oNew.style.height = "15px";
			//oNew.style.width = "330px";
			oNew.style.padding = "2px 2px 2px 2px";
			this.oDiv.appendChild(oNew);
		
			oNew.onmouseover=
			oNew.onmouseout=
			oNew.onmousedown=function(oEvent){
				oEvent=window.event || oEvent;
				oSrcDiv=oEvent.target || oEvent.srcElement;
				if(oEvent.type=="mousedown"){
					if(found == 1) {
						oThis.oText.value=this.innerHTML.stripHTML();
						click_full = 1;
						oThis.oDiv.style.display="none";
						document.getElementById("showlookupframe").style.display = "none";
					}
				}
				else if(oEvent.type=="mouseover"){
					this.className="over";
				}
				else if(oEvent.type=="mouseout"){
					this.className="";
				
				}else{
					this.oText.focus();
				}
			};
			
			var stri = ""; 
			var stri = ""+aStr[i];
			if(stri.indexOf('undefined') != -1){
				this.oDiv.removeChild(oNew);
			}else if(stri.substring(0,8)=="function"){
				this.oDiv.removeChild(oNew);
			}else if(stri.indexOf("function reverse") != -1){
				this.oDiv.removeChild(oNew);
			}else {
			  oNew.innerHTML=aStr[i];
			  height = height + 15;

			}
				if(i == 0) {
				 oNew.className="over";
			}
			
		}
		this.oText.className='';
		this.oDiv.style.display = "block";
		document.getElementById("showlookupframe").style.height = height+20;
		document.getElementById("showlookupframe").style.width = width;
		if(height > 15) {
			document.getElementById("showlookupframe").style.display = "block";
		}
	}else{
		this.oDiv.innerHTML="";
		this.oDiv.style.display="none";
		document.getElementById("showlookupframe").style.top = 0+"px";
		document.getElementById("showlookupframe").style.left = 0+"px";
		document.getElementById("showlookupframe").style.display = "none";
	}
};

String.prototype.stripHTML = function()
{
	var matchTag = /<(?:.|\s)*?>/g;
        return this.replace(matchTag, "");
};
// get x and y postion for calender x
function getLeft(obj)
{
	if ('string' == typeof obj)
	obj = document.getElementById(obj);
	var x = 0;
	while (obj != null)
	{
	x += obj.offsetLeft;
	obj = obj.offsetParent;
	}
	return x-580;
}
function getTop(obj){
	if ('string' == typeof obj)
	obj = document.getElementById(obj);
	var y = 0;
	while (obj != null)
	{
	y += obj.offsetTop;
	obj = obj.offsetParent;
	}
	return y-485;
}

/* Form airport lookup functionality end here */
