var arpCodeNames=new Array();
var load= new Image();
load.src="/images/intl/img/loading.gif";

function getXmlHttpRequestObject() {
	if (window.XMLHttpRequest) {
		return new XMLHttpRequest();
	} else if(window.ActiveXObject) {
		return new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		alert("Your Browser Sucks!\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();
							//alert(searchKey.responseText)
							eval(searchKey.responseText)
							this.showLookup=new apSearch(arpCodeNames,input,div,country,match,show,len)
							this.showLookup.onTextChange(false)
						}
				}
				searchKey.send(null);
		}		
	}

apLookup.prototype.getMatches=function(str,apL,show,type,len){
		if(!len){len=5}
		var ctr=0;
	var lookup='';
	for(var i in this.aArr){
			lookup=this.aArr[i].CI+" "+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;		
	 }
						if(apL.length > len){document.getElementById('bclass').style.display='none';}
						else{document.getElementById('bclass').style.display='';}
						
						if(apL.length > 0){document.getElementById('numINF').style.display='none';}
						else{document.getElementById('numINF').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)){
							apL.push("<font color='black'>No match found. Try entering first 3 letters <br /> and select from the list</font>")
						}
					


	
};
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.visibility="hidden";
	document.getElementById('bclass').style.display='';
	document.getElementById('numINF').style.display='';
};
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 13: //return key
			this.apSearch.hideairportions();
			return false;
			//window.focus();
			break;
		}

};
apSearch.prototype.moveDown=function(){
	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.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('bclass').style.display='';
		document.getElementById('numINF').style.display='';
		
		this.oDiv.innerHTML="";
		
		this.oDiv.style.visibility="hidden";
};
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==8 || iKeyCode==46){
		this.apSearch.onTextChange(false); 
	}
	else if (iKeyCode < 32 || (iKeyCode >= 33 && iKeyCode <= 46) || (iKeyCode >= 112 && iKeyCode <= 123)){
    }
	else{
			if(sKey.length==3){
			this.apSearch.oText.className = 'loading';
			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 */
			this.apSearch.oDiv.style.display='block';
	}


};
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;
	}
	x+=oNode.offsetLeft;
	y+=oNode.offsetTop;
	this.oDiv.style.top=y+"px";
	this.oDiv.style.left=x+"px";
}

apSearch.prototype.onTextChange=function(bTextComplete){
	var txt=this.oText.value;
	var oThis=this;
	this.cur=-1;
	if(txt.length<3){
		document.getElementById('bclass').style.display='';
		document.getElementById('numINF').style.display='';
		
	}
	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)
		
		if(!aStr.length) {this.hideairport ;return}
		if(bTextComplete) this.textComplete(aStr[0]);
		this.positionairport();
		for(i in aStr){
			var oNew=document.createElement('div');
			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"){
					oThis.oText.value=this.innerHTML.stripHTML();
				}
				else if(oEvent.type=="mouseover"){
					this.className="over";
				}
				else if(oEvent.type=="mouseout"){
					this.className="";
				}
				else{
					this.oText.focus();
				}
			};
			
				oNew.innerHTML=aStr[i]; 
				//Remvoe Junk Data for Prototype.js used on flex searc page 
				if(oNew.innerHTML.substring(0,8)=="function"){
					this.oDiv.removeChild(oNew);
				};
		}
	this.oText.className='';
	this.oDiv.style.visibility="visible";
	}
	else{
		this.oDiv.innerHTML="";
		this.oDiv.style.visibility="hidden";
		document.getElementById('bclass').style.display='';
		document.getElementById('numINF').style.display='';
	}
};
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;
}


