/* Form airport lookup functionality start here */
var load= new Image();
load.src="/images/xhotelv2/loading.gif";

var searchKey;
function apLookup(){
	this.aArr=new Array();
}

apLookup.prototype.assignArray=function(apL){
        this.aArr=apL;
};

apLookup.prototype.getMatches=function(str,apL,show,type,len){
        if(!len){len=5}
        var ctr=0;
        var lookup='';
	var lookup1='';
        var lookup2='';
        var lookup3='';
        var lookup4='';
        for(var i in this.aArr){
        	lookup=this.aArr[i].alias;
		lookup1 = this.aArr[i].alias1; 
                if(lookup.toLowerCase().indexOf(str.toLowerCase()) == 0 || lookup1.toLowerCase().indexOf(str.toLowerCase()) == 0){
                	var displayName=this.aArr[i].name;
                        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;
        }
        // set Default Message if not result return by query
      	if(cityArr.length>0 && apL.length==0 ){
         	apL.push("<font color='black'>City is not available. Try entering first 3 letters and select from the list.</font>")
      	}
};

function apSearch(aArr,oText,oDiv,show,len){
	var country_code;
	var match_type;
        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";       
};

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.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 (){
	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";
                                this.oText.value=this.oDiv.childNodes[i].innerHTML.stripHTML();
                        }else{
                                this.oDiv.childNodes[i].className="";
                        }
       }
	 }
	this.oDiv.innerHTML="";
        this.oDiv.style.visibility="hidden";
};

apSearch.prototype.keyUp=function(oEvent){
        var sKey=escape(this.apSearch.oText.value);        
        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==2){
                        //this.apSearch.oText.className = 'loading';
			this.showLookup=new apSearch(cityArr,'','','','')
               	}
                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="65px";
        this.oDiv.style.left="8px";
}

apSearch.prototype.onTextChange=function(bTextComplete){
        var txt=this.oText.value;
        var oThis=this;
        this.cur=-1;
        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";
        }
};

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 */
