var arpCodeNames=new Array();
var load= new Image();load.src="/yatra_blue-theme/images/common/loading.gif";var _found = 1;var  _click_full = 0;var global_city_found = 0;
var V='';var N=navigator.appName;var ie=false;if(N=="Microsoft Internet Explorer"){ie=true;};V=navigator.appVersion.split(';');V=parseInt(V[1].substring(6,7));
var tab=false;
var defaultval="Enter city/airport"
function ID(id){return document.getElementById(id)};
function ST(id){return ID(id).style.display;};
function changeClass(id,id2){ID(id.id).style.color='#000000';id.value='';if(id2)ID(id2).value=''}
function hidethis(id,id2){ID(id).style.display='none';ID(id2).style.display='none';}
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 hpLookup(){this.hArr=new Array();};hpLookup.prototype.assignArray=function(hpL){this.hArr=hpL;};
hpLookup.prototype.ajaxRequest=function(key,input,sinput,div,country,match,display,width,isAjax,minChar,Product){
	if (searchKey.readyState == 4 || searchKey.readyState == 0) {
		searchKey.open("GET", '/CommonIB/include/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.showlookup=new hpSearch(arpCodeNames,input,sinput,div,country,match,display,width,isAjax,minChar,Product)
				this.showlookup.onTextChange(false);
			}
		}
		searchKey.send(null);
	}
}

hpLookup.prototype.getMatches=function(str,hpL,otext,stext,country,match_type,display,width,isAjax,minChar,Product){

	if(!display){display=50};var ctr=0;var lookup='';var aliaslookup='';
/* =======================================================================================
		Hotel Specific Code For Hotel City Lookup
/=========================================================================================*/
	if(!isAjax){
		var searchString=str.substring(0,1).toLowerCase();
		if(searchString==""){searchString='a'}
		switch(searchString){case 'a':this.hArr=aArr;break; 	case 'b':this.hArr=bArr;;break; 	case 'c':this.hArr=cArr;break; 	case 'd':this.hArr=dArr;break; 	case 'e':this.hArr=eArr;break; 	case 'f':this.hArr=fArr;break; 	case 'g':this.hArr=gArr;break; 	case 'h':this.hArr=hArr;break; 	case 'i':this.hArr=iArr;break; 	case 'j':this.hArr=jArr;break; 	case 'k':this.hArr=kArr;break; 	case 'l':this.hArr=lArr;break; 	case 'm':this.hArr=mArr;break; 	case 'n':this.hArr=nArr;break; 	case 'o':this.hArr=oArr;break; 	case 'p':this.hArr=pArr;break; 	case 'q':this.hArr=qArr;break; 	case 'r':this.hArr=rArr;break; 	case 's':this.hArr=sArr;break; 	case 't':this.hArr=tArr;break; 	case 'u':this.hArr=uArr;break; 	case 'v':this.hArr=vArr;break; 	case 'w':this.hArr=wArr;break; 	case 'x':this.hArr=xArr;break; 	case 'y':this.hArr=yArr;break; 	case 'z':this.hArr=zArr;break;}
		for(var i=0; i<this.hArr.cityList.length; i++){
				var isMatch=true;
				if(!this.hArr.cityList[i].city){
					isMatch=false;
				}
				if(this.hArr.cityList[i]){
					lookup=this.hArr.cityList[i].city;
					aliaslookup=this.hArr.cityList[i].alias;
				}
				 if(lookup.toLowerCase().indexOf(str.toLowerCase()) != -1 || aliaslookup.toLowerCase().indexOf(str.toLowerCase()) != -1){
					var displayName=this.hArr.cityList[i].city;
					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);
					hpL.push(output);
					ctr++;
				}
			if(ctr == (display))
			break;
		}
		// set Default Message if not result return by query
						if(!hpL.length ){
							hpL.push("<font color='#000000'>No matching city found</font>")
						}

	}
/* =======================================================================================
		Air Specific Code
/=========================================================================================*/
if(isAjax){
		for(var i in this.hArr){
				if((this.hArr[i].CI) && (this.hArr[i].CO)) {
					lookup=this.hArr[i].CI + ', ' + this.hArr[i].CO + ' (' + this.hArr[i].APC + ') - '+this.hArr[i].AP.toLowerCase().replace('airport', '');
				}
				if(this.hArr[i].alias){
					aliaslookup=this.hArr[i].alias;
				}
				if((lookup.toLowerCase().replace('airport','').indexOf(str.toLowerCase().replace('airport','')) != -1  || aliaslookup.toLowerCase().indexOf(str.toLowerCase())!= -1) && lookup.length ){
					var displayName=this.hArr[i].CI + ', ' + this.hArr[i].CO + ' (' + this.hArr[i].APC + ') - '+this.hArr[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);
					hpL.push(output);
					ctr++;
				}
				if(ctr == (display))
					break;
		}

		var allairportTag=false;
		 for(var j in hpL){
			if((hpL[j].toString().toLowerCase().indexOf('all airport') != -1) && !allairportTag){
				allairportTag=true;
				var tmpAllap=hpL[j]
				delete hpL[j];
				hpL.unshift(tmpAllap);
			}
		}

		// set Default Message if not result return by query
						if((searchKey.readyState == 4 && searchKey.responseText==99) || (arpCodeNames.length && !hpL.length && searchKey.readyState == 4)){
							hpL.push("<font color='#000000'>No match found. Try entering first 3 letters </font>")
						}

	}
}
/* =======================================================================================
		Set Default Message if No Matching Results Found
/=========================================================================================*/
function hpSearch(hArr,oText,sText,oDiv,country_code,match_type,display,width,isAjax,minChar,Product,ismodify){
	this.oText=oText;
	this.sText=sText;
	if(!ismodify)
		this.sText.value=defaultval;

	this.oProduct=Product;
	if(this.oProduct=='air' && this.oText.value==""){this.oText.value=defaultval;this.sText.value=defaultval};

	this.oDiv=oDiv;
	this.oAjax=isAjax;
	this.oCountery=country_code
	this.oMatch=match_type
	this.oDisplay=display;
	this.cur=-1;
	this.ominChar=minChar;
	this.db=new hpLookup();
	this.oWidth=width;
	this.db.assignArray(hArr);
	this.oDiv.style.width=this.oWidth+'px';
	if(ie){this.oDiv.style.overflow="hidden";}
	this.oDiv.style.top=getTop(this.oText) +'px';
	this.oDiv.style.left=getLeft(oText) +'px';
	oText.hpSearch=this;
	oText.onkeyup=this.keyUp;
	oText.onkeydown=this.keyDown;
	oText.onblur=this.hideairport;
	//if(ie && this.oAjax){document.onclick=this.hidecitylookup;}else{oText.onblur=this.hideairport;}
}
/* was written for IE 6 and 7 But now not is Use can be removed */
hpSearch.prototype.setVal=function(){
	tab=false;
	if(this.oProduct=="air"){
		if(this.oText.value.indexOf('(')!=-1){
		  this.sText.value=(this.oText.value.match("\\([A-Z]{3}\\)\ \-\ ")[0]).match("[A-Z]{3}")[0];
		}
	}

}

hpSearch.prototype.hidecitylookup=function (){


	/*
	var evt = (e)?e:window.event;
	var theElem = (evt.srcElement)?evt.srcElement:evt.target;
	while(theElem!=null){
          if(theElem.id == this.oDiv.id && this.oDiv.style.display=='none') {
				this.oDiv.style.display = 'block';
				ID("showlookupframe").style.display = 'block';
				if(!_click_full){this.hpSearch.oText.value=this.oDiv.childNodes[0].innerHTML.stripHTML();}
			 return true;
		  }

        }

		*/
		if(!_click_full && this.hpSearch.oDiv.childNodes.length){
		if(this.hpSearch.oText.value!=''){this.hpSearch.oText.value=this.hpSearch.oDiv.childNodes[0].innerHTML.stripHTML()};
		this.hpSearch.oDiv.style.display = 'none';
		ID("showlookupframe").style.display = 'none';
	}


return true
};
hpSearch.prototype.hideairport=function(){

	if(!this.hpSearch.oText.value.length && this.hpSearch.oProduct=='air'){this.hpSearch.oText.value=defaultval;}
	if(this.hpSearch.oText.value.length >= 1 && this.hpSearch.oDiv.childNodes.length){
		if(!_click_full && this.hpSearch.oText.value.length>=3) {
			//this.hpSearch.oText.value=this.hpSearch.oDiv.childNodes[0].innerHTML.stripHTML();
			
		this.hpSearch.hideairportions();

		}

	}



	this.hpSearch.oDiv.style.display="none";
	ID("showlookupframe").style.display = "none";
};
hpSearch.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();
};
hpSearch.prototype.textComplete=function(sFirstMatch){
	if(this.oText.createTextRange || this.oText.setSelectionRange){
		var iStart=this.oText.value.length;
		this.selectText(iStart,sFirstMatch.length);
	}
};
hpSearch.prototype.keyDown=function(oEvent){

        oEvent=window.event || oEvent;
        iKeyCode=oEvent.keyCode;
		switch(iKeyCode){
                case 38: //up arrow
                        this.hpSearch.moveUp();
                        break;
                case 40: //down arrow
                        this.hpSearch.moveDown();
                        break;
		case 9: //tab key
					tab=true;
					this.hpSearch.moveForward();
					break;
        case 13: //return key
						this.hpSearch.hideairportions();

						return false;
                        break;
		case 27: //escape key
						this.hpSearch.moveForward();
					    break;
                }

};
hpSearch.prototype.moveDown=function(){

	if((this.oDiv.childNodes.length == 1) && (_found == 0)) {
		this.oText.value = "";
		this.oDiv.style.display="none";
		ID("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";
				if(this.oProduct=='air'){onChang(this.oText,this.oDiv.childNodes[i].innerHTML.stripHTML())}
					if(i >11){this.oDiv.scrollTop =this.oDiv.scrollTop + 18}
				this.oText.value=this.oDiv.childNodes[i].innerHTML.stripHTML();
				this.setVal();// To Set Airport Code in hidden field
			}else{
				this.oDiv.childNodes[i].className="";
			}

		}
	}
};
hpSearch.prototype.moveForward=function(){

	if(this.oText.value != "") {
		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();
					if(this.oProduct=='air'){onChang(this.oText,this.oDiv.childNodes[i].innerHTML.stripHTML())}
					this.setVal();// To Set Airport Code in hidden field
					_click_full = 1;
					ID("showlookupframe").style.display = "none";
					this.oDiv.style.display = "none";
					return false;
				}else{
					this.oDiv.childNodes[i].className="";
					ID("showlookupframe").style.display = "none";
					this.oDiv.style.display = "none";
					return false;
				}
			}
		}
	  }
if(!_click_full && this.oDiv.childNodes.length){this.oText.value=this.oDiv.childNodes[0].innerHTML.stripHTML();this.setVal();}
};
hpSearch.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";
				if(this.oProduct=='air'){onChang(this.oText,this.oDiv.childNodes[i].innerHTML.stripHTML())}

				 if(i>11){this.oDiv.scrollTop =this.oDiv.scrollTop-=18}else{this.oDiv.scrollTop=0;}
					this.oText.value=this.oDiv.childNodes[i].innerHTML.stripHTML();
					this.setVal();// To Set Airport Code in hidden field
			}else{
				this.oDiv.childNodes[i].className="";
			}
		}
	}
};
hpSearch.prototype.hideairportions = function (){

	if((!this.oDiv.childNodes.length) && (!_found)) {
		this.oText.value = "";
		this.oDiv.style.display="none";
		ID("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) {
									if(this.oProduct=='air'){onChang(this.oText,this.oDiv.childNodes[i].innerHTML.stripHTML())}
									if(this.oDiv.childNodes.length>1){this.oText.value=this.oDiv.childNodes[i].innerHTML.stripHTML();}else{this.oText.value=this.oDiv.childNodes[0].innerHTML.stripHTML();}
									_click_full = 1;
									this.oDiv.style.display = "none";
									ID("showlookupframe").style.display = "none";

								} else {
									this.oText.value="";
									this.oDiv.style.display = "none";
									ID("showlookupframe").style.display = "none";
								}
                        }else{

				this.oDiv.childNodes[i].className="";
				this.oDiv.style.display = "none";
				ID("showlookupframe").style.display = "none";

                        }

       		}
	}
	this.setVal();
	this.oDiv.innerHTML="";
	this.oDiv.style.display="none";
	ID("showlookupframe").style.display = "none";

};
hpSearch.prototype.keyUp=function(oEvent){
	var sKey=escape(this.hpSearch.oText.value);
	var ajaxKey=sKey.substring(0,1).toLowerCase();
	if(this.hpSearch.oAjax)this.ajax=new hpLookup();
		// to set text box class //
	if(ID(this.hpSearch.oText.id).style.color!='#000000'){ID(this.hpSearch.oText.id).style.color='#000000'}
	// to hide validation div//
	if(!this.hpSearch.oAjax){if(ID('CityValidations').innerHTML!=''){ID('CityValidations').innerHTML='';}}
	//this.ajax=new hpLookup();
	oEvent=oEvent || window.event;
	var iKeyCode=oEvent.keyCode;
	if((iKeyCode >= 47 && iKeyCode <= 57)) {
		this.hpSearch.oText.value = "";
		this.hpSearch.onTextChange(false);
	}else if((sKey.indexOf('%')==0)) {
	   this.hpSearch.oText.value = "";
	   this.hpSearch.onTextChange(false);
	}
	else if(iKeyCode==8 || iKeyCode==46){
		this.hpSearch.onTextChange(false);
	}else if (iKeyCode < 32 || (iKeyCode >= 33 && iKeyCode <= 46) || (iKeyCode >= 112 && iKeyCode <= 123) || ((iKeyCode >= 96 && iKeyCode <= 105))){
    	}
		else{
				if(sKey.length==3 && this.hpSearch.oAjax){
						this.hpSearch.oText.className = "loading";
						arpCodeNames.length = 0;
					    this.ajax.ajaxRequest(sKey,this.hpSearch.oText,this.hpSearch.sText,this.hpSearch.oDiv,this.hpSearch.oCountery,this.hpSearch.oMatch,this.hpSearch.oDisplay,this.hpSearch.oWidth,this.hpSearch.oAjax,this.hpSearch.ominChar,this.hpSearch.oProduct)
				}
				this.hpSearch.onTextChange(true);
		}
};
hpSearch.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;
	if(ie && this.oProduct=='air' && V<8){y=y+8;x=x+5;}
	this.oDiv.style.top=y+1+"px";
	this.oDiv.style.left=x+"px";
	ID("showlookupframe").style.top = this.oDiv.style.top;
	ID("showlookupframe").style.left = this.oDiv.style.left;

}
hpSearch.prototype.onTextChange=function(bTextComplete){
	var txt=this.oText.value;
	var oThis=this;
	this.cur=-1;
	_click_full = 0;
        this.oDiv.style.display = "none";
		ID("showlookupframe").style.display='none'
	if(!txt.length){
		this.oDiv.style.display = "none";
		ID("showlookupframe").style.display='none'
	}
	//this.oText.className = "";
	if(txt.length > this.ominChar ){
		while(this.oDiv.hasChildNodes())
		this.oDiv.removeChild(this.oDiv.firstChild);
		var aStr=new Array();
		this.db.getMatches(txt,aStr,this.oText,this.sText,this.oCountery,this.oMatch,this.oDisplay,this.oWidth,this.oAjax,this.ominChar,this.oProduct)

		if(!aStr.length) {this.hideairportions ;return}
		if(bTextComplete) this.textComplete(aStr[0]);
		this.positionairport();
		if(ie){
			var height=parseInt(aStr.length)*16;
			if(height>300){height=300}
			this.oDiv.style.height=height+5;
			ID("showlookupframe").style.height=height+5;

		}
		//if(aStr.length<=5){this.oDiv.style.overflow='hidden'}
		for(i in aStr){
				var oNew=document.createElement('div');
				if (aStr[i].length>61)
					oNew.style.height = "30px";
				else
					oNew.style.height = "17px";

				oNew.style.paddingLeft = "3px";
				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();
							if(oThis.oProduct=='air'){onChang(oThis.oText,this.innerHTML.stripHTML())};
							oThis.setVal();// To Set Airport Code in hidden field
							_click_full = 1;
							oThis.oDiv.style.display="none";
							ID("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];

				}
				if(i==0){oNew.className='over'};
		}
			//if(this.sText && this.sText.value.length){tab=false}
			this.oText.className='';
			this.oDiv.style.display = "block";
			ID("showlookupframe").style.height = parseInt(this.oDiv.offsetHeight);
			ID("showlookupframe").style.width = parseInt(this.oDiv.offsetWidth);
			ID("showlookupframe").style.display = "block";
			if(tab){this.oDiv.style.display = "none";ID("showlookupframe").style.display = "none";};
			if(!_click_full && tab){this.oText.value=this.oDiv.childNodes[0].innerHTML.stripHTML();this.setVal();}

	}
		else{

				this.oDiv.innerHTML="";
				this.oDiv.style.display="none";
				ID("showlookupframe").style.display = "none";
				ID("showlookupframe").style.top = 0+"px";
				ID("showlookupframe").style.left = 0+"px";
				ID("showlookupframe").style.height = 0 + "px";
		}
};
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 = ID(obj);var x = 0;
	while (obj != null){
		x += obj.offsetLeft;obj = obj.offsetParent;
	}
	return x-580;
}
function getTop(obj){
	if ('string' == typeof obj)
	obj = ID(obj);var y = 0;
	while (obj != null){
		y += obj.offsetTop;obj = obj.offsetParent;
	}
	return y-485;
}

/* Form airport lookup functionality end here */



