//Rollover images script
function MM_preloadImages(){//v3.0
  var d=document; if(d.images){ if(!d.MM_p)d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments;for(i=0;i<a.length;i++)
    if (a[i].indexOf("#")!=0){d.MM_p[j]=new Image;d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore(){//v3.0
  var i,x,a=document.MM_sr;for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++)x.src=x.oSrc;
}

function MM_findObj(n,d){//v4.01
  var p,i,x;if(!d)d=document;if((p=n.indexOf("?"))>0&&parent.frames.length){
    d=parent.frames[n.substring(p+1)].document;n=n.substring(0,p);}
  if(!(x=d[n])&&d.all)x=d.all[n];for(i=0;!x&&i<d.forms.length;i++)x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++)x=MM_findObj(n,d.layers[i].document);
  if(!x&&d.getElementById)x=d.getElementById(n);return x;
}

function MM_swapImage(){//v3.0
  var i,j=0,x,a=MM_swapImage.arguments;document.MM_sr=new Array;for(i=0;i<(a.length-2);i+=3)
   if((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x;if(!x.oSrc)x.oSrc=x.src;x.src=a[i+2];}
}

//Random image loader
function spotlight(img,alt){this.img=img;this.alt=alt;}
var spotlightArray=new Array();
spotlightArray[0]=new spotlight('/images/splash/spotlight1.jpg','Every journey is a rewarding experience.');
//spotlightArray[1]=new spotlight('/images/splash/spotlight2.jpg','needs alt tag');
//spotlightArray[2]=new spotlight('/images/splash/spotlight3.jpg','needs alt tag');
var randomNumber=Math.floor(Math.random()*spotlightArray.length)
function randomSpotlight(){
	var spotlightImg=document.createElement("img");
	spotlightImg.setAttribute('src',spotlightArray[randomNumber].img); 
	spotlightImg.setAttribute('width','950px');
	spotlightImg.setAttribute('height','500px');
	spotlightImg.setAttribute('border','0px');
	spotlightImg.setAttribute('alt',spotlightArray[randomNumber].alt);
	document.getElementById("spotlight").appendChild(spotlightImg);
}

//Toggle IBE widget
function toggleWidget(){
	var widgetBg=document.getElementById('widgetBg');
	var widget=document.getElementById('widget');
	var widgetBgStyle=getStyle(document.getElementById('widgetBg'),'display');
	var widgetStyle=getStyle(document.getElementById('widget'),'display');
	widgetBgStyle=='none'?widgetBg.style.display='inline':widgetBg.style.display='none'
	widgetStyle=='none'?widget.style.display='inline':widget.style.display='none'
}	
			
function getStyle(oElm,strCssRule){
	var strValue="";
	if(document.defaultView&&document.defaultView.getComputedStyle){
		strValue=document.defaultView.getComputedStyle(oElm,"").getPropertyValue(strCssRule);
	}else if(oElm.currentStyle){
		strCssRule=strCssRule.replace(/\-(\w)/g,function(strMatch, p1){return p1.toUpperCase();});
		strValue = oElm.currentStyle[strCssRule];
	}
	return strValue;
}//http://robertnyman.com/2006/04/24/get-the-rendered-style-of-an-element/

//Dynamic date generation
var today=new Date();
var day=today.getDate();
var month=today.getMonth();
var monthName=new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
var year=today.getFullYear();
var dayNum=daysInMonth(month,year);
var initialDepDay=day+5;
if(initialDepDay>dayNum)initialDepDay-=dayNum;
var initialRetDay=initialDepDay+2;
if(initialRetDay>dayNum)initialRetDay-=dayNum;

function daysInMonth(iMonth,iYear){return 32-new Date(iYear,iMonth,32).getDate();}

function generateDays(ddID){
	var dropDown=document.getElementById(ddID);
	month=today.getMonth();
	var selected=initialDepDay;
	if(ddID=='retuDay')selected=initialRetDay;
	if((ddID=='deptDay'&&initialDepDay<day)||(ddID=='retuDay'&&initialRetDay<day))dayNum=daysInMonth(month+1,year);
	while(dropDown.hasChildNodes()){dropDown.removeChild(dropDown.childNodes[0]);}
	for(i=1;i<dayNum+1;i++){
 		var option=document.createElement('option');
		if(i>9){option.setAttribute('value',i);}else{option.setAttribute('value','0'+i);}
		if(i==selected)option.setAttribute('selected','selected');
		var optionTxt=document.createTextNode(i);
		option.appendChild(optionTxt);
		dropDown.appendChild(option);
	}
}

function generateMonths(ddID){
	var dropDown=document.getElementById(ddID);
	month=today.getMonth();
	if((ddID=='deptYr'&&initialDepDay<day)||(ddID=='retuYr'&&initialRetDay<day))month++;
	for(i=month;i<month+12;i++){
		var option=document.createElement('option');
		var optionLbl='';
		var optionVal='';
		if((i<9)||(i>11))optionVal='0';
		if(i<12){
			optionVal=optionVal+((i+1)+'/'+year);
			optionLbl=monthName[i]+' '+year;
		}else{
			optionVal=optionVal+((i-11)+'/'+(year+1));
			optionLbl=monthName[i-12]+' '+(year+1);
		}
		option.setAttribute('value',optionVal);
		if(i==month)option.setAttribute('selected','selected');
		var optionTxt=document.createTextNode(optionLbl);
		option.appendChild(optionTxt);
		dropDown.appendChild(option);
	}
}

//Widget valdition and parameter URI formulation
function validate(){
	var from=document.getElementById('originLabel');
	var to=document.getElementById('destLabel');
	var missing="0";
	if(document.getElementById('origin').options[document.getElementById('origin').selectedIndex].value=="select"){
		from.style.fontWeight="bold";from.style.color="red";missing="1";
	}else{from.style.fontWeight="normal";from.style.color="black";}
	if(document.getElementById('destination').options[document.getElementById('destination').selectedIndex].value=="select"){
		to.style.fontWeight="bold";to.style.color="red";missing="1";
	}else{to.style.fontWeight="normal";to.style.color="black";}
	if(missing=="0")submitFltSearch();
}

function submitFltSearch(){
	var formAction;
	formAction="http://booking.qatarairways.com/qribe-web/public/showIBE.do?L=en&hdnData=SEARCH";
	var departureLoc=document.getElementById('origin').options[document.ibe.origin.selectedIndex];
	var destinationLoc=document.getElementById('destination').options[document.ibe.destination.selectedIndex];
	var classLoc=document.getElementById('bClass').options[document.ibe.bClass.selectedIndex];
	var tripType="^"+document.getElementById('tripType').options[document.ibe.tripType.selectedIndex].value;
 	var today=new Date();
	var day=today.getDate();
	var depIataCode="^"+departureLoc.value;
	var destIataCode="^"+destinationLoc.value;
	var deptDay="^"+document.ibe.deptDay.options[document.ibe.deptDay.selectedIndex].value;
	var deptYr="^"+document.ibe.deptYr.options[document.ibe.deptYr.selectedIndex].value;
	var deptTime="^ANY";
	var retuDay="^"+document.ibe.retuDay.options[document.ibe.retuDay.selectedIndex].value;
	var retuYr="^"+document.ibe.retuYr.options[document.ibe.retuYr.selectedIndex].value;
	var retuTime="^ANY";
	var adult="^"+document.ibe.adult.options[document.ibe.adult.selectedIndex].value;
	var child="^"+document.ibe.child.options[document.ibe.child.selectedIndex].value;
	var infant="^"+document.ibe.infant.options[document.ibe.infant.selectedIndex].value;
	var searchBy="^F";
	var bClass="^"+classLoc.value;
	var flexDates="^F";
	var depCity="^"+departureLoc.text;
	var destCity="^"+destinationLoc.text;
	var bClassFull="^"+classLoc.text;
	var tax="^Y";
	var hdnMode="^&hdnMode=SEARCH";
	var searchString="";
		
	searchString=formAction+" "+tripType+" "+depIataCode+" "+destIataCode+" "+deptDay+" "+deptYr+" "+deptTime+" "+retuDay+" "+retuYr+" "+retuTime+" "+adult+" "+child+" "+infant+" "+searchBy+" "+bClass+" "+flexDates+" "+depCity+" "+destCity+" "+bClassFull+" ^1 ^3 "+tax+" ^EXTERNALURL"+" "+hdnMode;
	document.ibe.action=searchString;
	document.ibe.method="post";
	document.ibe.target="_top";
	document.ibe.submit();
}

//Country/Language code
function buildUrl(){
	var country=document.getElementById('country');
	var countryValue=country.options[country.selectedIndex].value;
	if(countryValue==""){//If no value selected
		country.selectedIndex=0;
		country.focus();
		return false;
	}
	document.getElementById('remember').checked==true?createCookie('country',countryValue,1000):eraseCookie('country');
	var uri="/"+countryValue+"/homepage.html";
	window.location=uri;
}

//Loading scripts after the page has finished loading
window.onLoad=functions();

function functions(){
	randomSpotlight();
	generateDays('deptDay');
	generateMonths('deptYr');
	generateDays('retuDay');
	generateMonths('retuYr');
}