function PadDigits(n, totalDigits) 
    { 
        n = n.toString(); 
        var pd = ''; 
        if (totalDigits > n.length) 
        { 
            for (i=0; i < (totalDigits-n.length); i++) 
            { 
                pd += '0'; 
            } 
        } 
        return pd + n.toString(); 
    } 
 

function disableRDate()
{	
	document.FRM.toDate.disabled=true;
}

function enableRDate()
{
	document.FRM.toDate.disabled=false;
}

function disableRDate()
{	
	document.FRM.toDate.disabled=true;
}

function enableRDate()
{
	document.FRM.toDate.disabled=false;
}

function checkErrors()
{
	//Checking for From & To E-Ticket Enabled stations
	if(
		//(document.FRM.fmCode[document.FRM.fmCode.selectedIndex].value == "BAH") ||
		//(document.FRM.fmCode[document.FRM.fmCode.selectedIndex].value == "COK") ||
		//(document.FRM.fmCode[document.FRM.fmCode.selectedIndex].value == "DOH") ||
		//(document.FRM.fmCode[document.FRM.fmCode.selectedIndex].value == "AUH") ||
		(document.FRM.fmCode[document.FRM.fmCode.selectedIndex].value == "AAN") //||
		//(document.FRM.fmCode[document.FRM.fmCode.selectedIndex].value == "DMM") ||
		//(document.FRM.fmCode[document.FRM.fmCode.selectedIndex].value == "JED") ||
		//(document.FRM.fmCode[document.FRM.fmCode.selectedIndex].value == "RUH") ||
		//(document.FRM.fmCode[document.FRM.fmCode.selectedIndex].value == "CCJ") ||
		//(document.FRM.fmCode[document.FRM.fmCode.selectedIndex].value == "BLR") ||
		//(document.FRM.fmCode[document.FRM.fmCode.selectedIndex].value == "KHI") ||
		//(document.FRM.fmCode[document.FRM.fmCode.selectedIndex].value == "CGP") ||
		//(document.FRM.fmCode[document.FRM.fmCode.selectedIndex].value == "BKK")
		//||(document.FRM.fmCode[document.FRM.fmCode.selectedIndex].value == "KHS")
	){
		//isError = isError + 1;
		//strErrorMsg = strErrorMsg + "\n\n'FROM' station is not setup for Web booking. Please contact nearest Oman Air office for all your bookings.";
		//alert ("Inside From -->" + isError);
		}
	else if(
		//(document.FRM.toCode[document.FRM.toCode.selectedIndex].value == "BAH") ||
		//(document.FRM.toCode[document.FRM.toCode.selectedIndex].value == "COK") ||
		//(document.FRM.toCode[document.FRM.toCode.selectedIndex].value == "DOH") ||
		//(document.FRM.toCode[document.FRM.toCode.selectedIndex].value == "AUH") ||
		(document.FRM.toCode[document.FRM.toCode.selectedIndex].value == "AAN") //||
		//(document.FRM.toCode[document.FRM.toCode.selectedIndex].value == "DMM") ||
		//(document.FRM.toCode[document.FRM.toCode.selectedIndex].value == "JED") ||
		//(document.FRM.toCode[document.FRM.toCode.selectedIndex].value == "RUH") ||
		//(document.FRM.toCode[document.FRM.toCode.selectedIndex].value == "CCJ") ||
		//(document.FRM.toCode[document.FRM.toCode.selectedIndex].value == "BLR") ||
		//(document.FRM.toCode[document.FRM.toCode.selectedIndex].value == "KHI") ||
		//(document.FRM.toCode[document.FRM.toCode.selectedIndex].value == "CGP") ||
		//(document.FRM.toCode[document.FRM.toCode.selectedIndex].value == "BKK") 
		//||(document.FRM.toCode[document.FRM.toCode.selectedIndex].value == "KHS")

	){
		//isError = isError + 1;
		//strErrorMsg = strErrorMsg + "\n\n'To' station is not setup for Web booking. Please contact nearest Oman Air office for all your bookings.";
		//alert ("Inside To -->" + isError);
		}
	else{
		//alert ("All is OK for checkErrors -->" + isError);
		return;
		}
}
function changeReturnDate(){
	var fromDate = (document.FRM.fromDate.value).split("-"); 
	var toDate = (document.FRM.toDate.value).split("-");
	var day = fromDate[0];
	strDepDay = day;
	var month = fromDate[1];
	strDepMonth = month;
	var year = fromDate[2];
	var day1 = toDate[0];
	strRetDay = day1;
	var month1 = toDate[1];
	strRetMonth = month1;
	var year1 = toDate[2];	
	var jsFlyDate = new Date(year, month, day);
	var jsReturnDate = new Date(year1, month1, day1);


		if( (document.FRM.RET[1].checked) && ( (year1<year) || (year1==year && parseInt(month1,10)<parseInt(month,10)) || (year1==year && parseInt(month1,10)==parseInt(month,10)&& parseInt(day1,10)<parseInt(day,10)) )){	

			var temp = new Date(year, month, jsFlyDate.getDate()+1); 
			
			
			isError = isError + 1;
			alert("Return date should not be earlier than Departure date");
		} else {
			return true;
		}	

}


function searchFlights()
{
        //added by azad
       
        valid=ValidateTravelReturnDates();		
        if(!valid){
            return false;
        }
        //end 
	setFlyDay();
	
	if (isError == 0){                
                document.FRM.submit();
        }
	
}

function bookOnline()
{
		//additional code by azad
        
        valid=ValidateTravelReturnDates("book_online");
        if(!valid){
            return false;
        }
        
		//added by Sajeer start...
        //var onlineBookingUnableFlights = ["KUL"];
		var frmStation = document.FRM.fmCode[document.FRM.fmCode.selectedIndex].value;
		var toStation = document.FRM.toCode[document.FRM.toCode.selectedIndex].value;
		
		if(frmStation == toStation){
			alert('Departure and Arrival station cannot be the same.');
			return false;
		}
		
		//for(var i = 0; i < onlineBookingUnableFlights.length; i++) {
			//if(onlineBookingUnableFlights[i] == frmStation){
				//alert('At present we are unable to offer online ticketing for flights originating from KUALA LUMPUR.  //Hence, we appreciate if you can contact us on 968-24531111. Our contact centre agent will be at your service');
        		//return false;
			//}
		//}
		//added by Sajeer end...

        //Each infant should be accompanied with an adult meaning if you request 3 infant seats, 
        //there should be 3 or more adult passengers in the group.
        var adult= document.getElementById("Adults");
        var child= document.getElementById("Children");
        var infant= document.getElementById("Infant");
        if( adult.options[adult.selectedIndex].value  < infant.options[infant.selectedIndex].value  ){
        	alert('Each infant should be accompanied with an adult');
        	return false;
        }
        //Total number of adult+child should be less than or = 6 
        var totalPass = (adult.selectedIndex+1) + child.selectedIndex;        
        if(totalPass > 6){
        	alert('Total number of adult and child should not exceed 6');
        	return false;
        }
        //end of additional code
        
        setFlyDay();
		
		
	
	if(isError == 0)
	{
		checkErrors();
		//alert ("After checkErrors-->" + isError);
		if(isError == 0)
		{
			// Generate Sabre Query String
			strSabreQS = "" ;
			strSabreQS = strSabreQS + "https://cat.sabresonicweb.com/meridia" ;
			strSabreQS = strSabreQS + "?posid=" 			+ "WYWY" ;
			strSabreQS = strSabreQS + "&page=" 				+ "requestAirMessage_air" ;
			strSabreQS = strSabreQS + "&action=" 			+ "airRequest" ;
			strSabreQS = strSabreQS + "&currency=" 			+ "OMR" ; //To be changed according to Source City
			strSabreQS = strSabreQS + "&departCity=" 		+ document.FRM.fmCode[document.FRM.fmCode.selectedIndex].value ;
			strSabreQS = strSabreQS + "&depMonth=" 			+ arrMonth[parseInt(strDepMonth-1)] ;
			strSabreQS = strSabreQS + "&depDay=" 			+ strDepDay ;
			strSabreQS = strSabreQS + "&depTime=" 			+ "0700" ; //Defaulted to 0700 HRS
			strSabreQS = strSabreQS + "&returnCity=" 		+ document.FRM.toCode[document.FRM.toCode.selectedIndex].value ;
			strSabreQS = strSabreQS + "&retMonth=" 			+ arrMonth[parseInt(strRetMonth-1)] ;
			strSabreQS = strSabreQS + "&retDay=" 			+ strRetDay ;
			strSabreQS = strSabreQS + "&retTime=" 			+ "0700" ; //Defaulted to 0700 HRS
			strSabreQS = strSabreQS + "&direction=" 		+ getDirection() ; //Get this value from what is selected valid values are(returntravel / onewaytravel)
			strSabreQS = strSabreQS + "&contractID=" 		+ "" ; //Not required
			strSabreQS = strSabreQS + "&ADT=" 	+ document.FRM.Adults[document.FRM.Adults.selectedIndex].value ;
			strSabreQS = strSabreQS + "&CHD=" 	+ document.FRM.Children[document.FRM.Children.selectedIndex].value ;
			strSabreQS = strSabreQS + "&STU=" 	+ "" ; //Not required
			strSabreQS = strSabreQS + "&YTH=" 	+ "" ; //Not required
			strSabreQS = strSabreQS + "&INF=" 	+ infant.options[infant.selectedIndex].value;
			strSabreQS = strSabreQS + "&IFS=" 	+ "" ; //Not required
			strSabreQS = strSabreQS + "&UMNR=" 	+ "" ; //Not required
			strSabreQS = strSabreQS + "&MIL=" 	+ "" ; //Not required
			strSabreQS = strSabreQS + "&SCR=" 	+ "" ; //Not required
			strSabreQS = strSabreQS + "&classService=" 	+ getClassService() ;
			strSabreQS = strSabreQS + "&flightType=" 		+ "1" ; //Always look for Connecting flights (0=non-stop, 1=connecting)
			strSabreQS = strSabreQS + "&rem=" 				+ "" ; //Not required
			strSabreQS = strSabreQS + "&actionType=" 		+ "nonFlex" ; //Can be changed to (nonFlex / semiFlex / flex)
			strSabreQS = strSabreQS + "&realRequestAir=" + "realRequestAir" ; //Default
			//alert(strSabreQS);
			window.open(strSabreQS);
		}
		else
		{
			//alert ("All is NOT OK -->" + isError);
			showErrorMsg();
		}
	}
}

function showErrorMsg()
{
	strErrorMsg = "Online booking is not available to this route." + strErrorMsg;
	if(isError > 0)
	{
		alert(strErrorMsg);
	}
	else
	{
		return;
	}
}

function getClassService()
{
	for (var i=0; i < document.FRM.classService.length; i++)
	{
		if (document.FRM.classService[i].checked)
		{
			var classService = document.FRM.classService[i].value;
			return(classService);
		}
	}
}

function getDirection()
{
	for (var i=0; i < document.FRM.RET.length; i++)
	{
		if (document.FRM.RET[i].checked)
		{
			var strRetDir = document.FRM.RET[i].value;
			if(strRetDir == "Y")
				return("returntravel");
			else
				return("onewaytravel");
		}
	}
}

    