/* For return date autofill on homepage start here */

function datefillch(fn){
	var date=new Date();
	var d=date.getDate();
	var m=date.getMonth()+1;
	var y=date.getFullYear();

	if(d<10){d="0"+d}
	if(m<10){m="0"+m}

	var t=new Date(m+"/"+d+"/"+y)
	var r=new Date(m+"/"+d+"/"+y)

	t.setDate(date.getDate()+3)
	r.setDate(date.getDate()+14)

	var rD=r.getDate();
	var rM=r.getMonth()+1;
	var rY=r.getFullYear();
	if(rD<10){rD="0"+rD}
	if(rM<10){rM="0"+rM}
	var rDate=rM+"/"+rD+"/"+rY;
	var tD=t.getDate();
	var tM=t.getMonth()+1;
	var tY=t.getFullYear();
	var tDate=tM+"/"+tD+"/"+tY;

	document.forms[""+fn].rdate.value=rDate;
}
/* For return date autofill on homepage end here */


/* Form airport lookup functionality start here */
var arpCodeNames=new Array();
var load= new Image();
load.src="/images/UK/homepage/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", '/CommonIB/include/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,country){
	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;		
	 }

	// 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('bclass').style.display='none';}
	else{document.getElementById('bclass').style.display='';}
		
	if(apL.length > 0){
		document.getElementById('arrival').style.display='none';
		document.getElementById('numADT').style.display='none';
		document.getElementById('numINF').style.display='none';
		document.getElementById('numCHD').style.display='none';
	}else{
		document.getElementById('arrival').style.display='';
		document.getElementById('numADT').style.display='';
		document.getElementById('numINF').style.display='';
		document.getElementById('numCHD').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)){
		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 matching Indian city/airport found. Try entering first <br />3 letters and select from the list.")
		}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.")
		}
	}
		
};

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(){
	if(this.apSearch.oText.value.length==3){
		this.apSearch.hideairportions();
	}
	this.apSearch.oDiv.style.visibility="hidden";
	document.getElementById('arrival').style.display='';
	document.getElementById('numADT').style.display='';
	document.getElementById('numINF').style.display='';
	document.getElementById('numCHD').style.display='';
	document.getElementById('bclass').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 9: //tab key
                        this.apSearch.moveDown();
			break;
                case 13: //return key
                        this.apSearch.hideairportions();
                        return false;
                        break;
		case 27: //escape key
                        this.apSearch.hideairportions();
                        return false;
                        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('arrival').style.display='';
	document.getElementById('numADT').style.display='';
	document.getElementById('numINF').style.display='';
	document.getElementById('numCHD').style.display='';
	document.getElementById('bclass').style.display='';
	this.oisC=false;
	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);
	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('arrival').style.display='';
	document.getElementById('numADT').style.display='';
	document.getElementById('numINF').style.display='';
	document.getElementById('numCHD').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,this.oCountery)
		
		if(!aStr.length) {this.hideairportions ;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('arrival').style.display='';
	document.getElementById('numADT').style.display='';
	document.getElementById('numINF').style.display='';
	document.getElementById('numCHD').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;
}

/* Form airport lookup functionality end here */

/*functionality for Holidays Theme pannel start here */

function togeldiv1(){
document.getElementById('firstdiv').style.display='block';
document.getElementById('secdiv').style.display='none';
document.getElementById('thrdiv').style.display='none';
document.getElementById('holidaytab1').className='deactive';
document.getElementById('holidaytab3').className='active';
document.getElementById('holidaytab4').className='active';
}

function togeldiv2(){
document.getElementById('firstdiv').style.display='none';
document.getElementById('secdiv').style.display='block';
document.getElementById('thrdiv').style.display='none';
document.getElementById('holidaytab1').className='active';
document.getElementById('holidaytab3').className='deactive';
document.getElementById('holidaytab4').className='active';
}
function togeldiv3(){
document.getElementById('firstdiv').style.display='none';
document.getElementById('secdiv').style.display='none';
document.getElementById('thrdiv').style.display='block';
document.getElementById('holidaytab1').className='active';
document.getElementById('holidaytab3').className='active';
document.getElementById('holidaytab4').className='deactive';
}

/*functionality for Holidays Theme pannel end here */


/*functionality for Best Air fare Panel Start here */
function bestairfare(divName,tabdiv)
{
for(i=0; i<=1; i++)
	{
		document.getElementById('bestairfare'+i).style.display='none';
		document.getElementById(divName).style.display='';
		document.getElementById('bflighttab'+i).className='active';
		document.getElementById(tabdiv).className='deactive';
	}
		
}
/*functionality for Best Air fare Panel End here */