/*-----------------------------------------------------------------
Function Name : setErrorText
Description   : To show the error msg
Input         : id for container for error, Error Msg
Output        : Show the error msgs
------------------------------------------------------------------*/
function setErrorText(inputID,textValue)
{
	if(document.all)
{
		document.getElementById(inputID).innerText=textValue;}
else
{
	document.getElementById(inputID).textContent=textValue;}
}
/*-----------------------------------------------------------------
Function Name : isValidHotel
Description   : To validate the hotel form fields
Input         : Form name, Checkin Date, Checkout Date
Output        : boolean true if all fields are valid else false
------------------------------------------------------------------*/

function isValidHotel(formName,obDate,ibDate)
{
 var nOr = document.getElementById('NumberOfRooms_Numbers').value;
 var totPaxes = 0;
 var ADT = new Array(3);
 var CHD = new Array(3);
 for(var x=0; x<Number(nOr); x++)
 {
  ADT[x] = document.getElementById('Adult'+''+(x+1)+'_Numbers').value;
  CHD[x] = document.getElementById('Child'+''+(x+1)+'_Numbers').value;
  //totPaxes = totPaxes +	Number(ADT[x]) + Number(CHD[x]);
 }
	var totPaxesR1 = Number(ADT[0]) + Number(CHD[0]);
	var totPaxesR2 = Number(ADT[1]) + Number(CHD[1]);
	var totPaxesR3 = Number(ADT[2]) + Number(CHD[2]);
	var totPaxesR4 = Number(ADT[3]) + Number(CHD[3]);
	//alert('TotPaxesR1: '+totPaxesR1+'\nTotPaxesR2:'+totPaxesR2+'\nTotPaxesR3:'+totPaxesR3+'\nTotPaxesR4:'+totPaxesR4)
	if(totPaxesR1>4 || totPaxesR2>4 || totPaxesR3>4 || totPaxesR4>4){
	setErrorText("PaxValidations","You are allowed a maximum of 4 guests per room booking.");
	return false;
	}else{
		setErrorText("PaxValidations","");
	}

	//var CHD=document.forms[""+formName].chdroom1.value;
	//var ADT=document.forms[""+formName].adt1.value;
	//var totPax=parseInt(ADT)+parseInt(CHD);
	var isvalidD=isDate(obDate,formName)
	if(!isvalidD){
		return false;
	}
	var isvalidR=isDate(ibDate,formName)
	if(!isvalidR){
		return false;
	}

	var obDate=obDate.split('/');
	var ibDate=ibDate.split('/');
	var obDay=obDate[0];
	var obYear=obDate[2];
	var obMonth=(obDate[1]-1)
	var ibDay=ibDate[0];
	var ibYear=ibDate[2];
	var ibMonth=(ibDate[1]-1)
	var today=new Date();
	var D=today.getDate();if(D<10){D="0"+D;}
	var M=today.getMonth();if(M<10){M="0"+M;}
	var Y=today.getFullYear();
	var objDiff=daysElapsed(new Date(ibYear,ibMonth,ibDay),new Date(obYear,obMonth,obDay));
	var objDt=daysElapsed(new Date(obYear,obMonth,obDay),new Date(Y,M,D));

	if(objDt<0){
		alert("Depart date can't be in past")
		return false;
	}
    if(document.forms[""+formName].cityname) {
		if((document.forms[""+formName].cityname.value=="") || (document.forms[""+formName].cityname.value == "No matching city found")|| (document.forms[""+formName].cityname.value == "Enter your destination city"))
		{
			document.getElementById('arrowimg').style.display='';
			setErrorText("CityValidations","City name can not be blank.");
			//alert("City name can't be blank");
			document.forms[""+formName].cityname.focus();
			document.forms[""+formName].cityname.value = "";
			//setErrorText("CityValidations","");
			return false;
		}else{
			setErrorText("CityValidations","");
		}
		var cityContent=document.getElementById('city').value;
		if(Trim(cityContent)=='')
		{
			document.getElementById('arrowimg').style.display='';
			setErrorText("CityValidations","City name cannot start with a space.");
			return false;
		}else{
			setErrorText("CityValidations","");
		}
		if(cityContent.length<=2)
		{
			document.getElementById('arrowimg').style.display='';
			setErrorText("CityValidations","Please enter the first 3 letters of the city name.");
			return false;
		}else{
			setErrorText("CityValidations","");
		}


		var validCity = validate_city_name(document.forms[""+formName].cityname.value);
		//if city is not valid display the error message.
		if (validCity == 0){
		   alert("Numeric city:Please enter valid city name");
		   document.forms[""+formName].cityname.focus();
		   return false;
		}
		//if(document.getElementById('hdFindCityStatus')){
		//if(document.getElementById('hdFindCityStatus').value==1){
			//return false;
		//}
		//}

	}


	if(objDiff<0)
	{
		setErrorText("DateValidations","Checkout date must occur after the Checkin date.");
		//alert("Checkout date must occur after the Checkin date.")
		return false;
		}else{
					setErrorText("DateValidations","");
	}
	if(objDiff>14)
	{
		setErrorText("DateValidations","Your stay period can not be more than a fortnight for online hotel booking. If your stay period is more than this, please call our call center.");
		return false;
		}else{
					setErrorText("DateValidations","");
	}
	 if(objDiff==0)
        {
		 setErrorText("DateValidations","Checkout and Checkin date can't be same.");
            //    alert("Checkout and Checkin date can't be same.")
                return false;
		}else{
					setErrorText("DateValidations","");
				}

		document.getElementById('ChildAgeErrorDiv').style.display="none";
		var agelist = new Array(8);
		agelist[0] = document.getElementById('ChildAgeControl11_AgeList');
		agelist[1] = document.getElementById('ChildAgeControl12_AgeList');
		agelist[2] = document.getElementById('ChildAgeControl13_AgeList');
		agelist[3] = document.getElementById('ChildAgeControl14_AgeList');
		agelist[4] = document.getElementById('ChildAgeControl15_AgeList');
		agelist[5] = document.getElementById('ChildAgeControl16_AgeList');
		agelist[6] = document.getElementById('ChildAgeControl17_AgeList');
		agelist[7] = document.getElementById('ChildAgeControl18_AgeList');
			for (var y = 1; y <= 4; y++){
					for (var x = 1; x <= 2; x++){
						if(document.getElementById('Child'+y+'_Numbers').value>0){
							if(document.getElementById('ChildAge'+y+''+x).style.display=="block" && agelist[x-1+((y-1)*2)].selectedIndex==0){
								if(document.getElementById('ChildAgeErrorDiv').style.display!="block"){
										document.getElementById('ChildAgeErrorDiv').style.display="block";
										return false;
								}
							}
							}
						}
				}
      return true;
}
function isDate(dtStr,formName){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strDay=dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if(formName=="modifysearchform"){
	if (document.modifysearchform.cidate.value=="" && document.modifysearchform.codate.value==""){
		setErrorText("DateValidations","Select the Check-in and Check-out dates.");
		//alert("The date format should be : dd/mm/yyyy")
		return false;
		}else{
					setErrorText("DateValidations","");
	}
	if (document.modifysearchform.cidate.value==""){
		setErrorText("DateValidations","Select a Check-in date.");
		//alert("The date format should be : dd/mm/yyyy")
		return false;
		}else{
					setErrorText("DateValidations","");
	}
	if (document.modifysearchform.codate.value==""){
		setErrorText("DateValidations","Select a Check-out date.");
		//alert("The date format should be : dd/mm/yyyy")
		return false;
		}else{
					setErrorText("DateValidations","");
	}
	}else{
		if (pos1==-1 || pos2==-1){
		setErrorText("DateValidations","Select a Check-out date.");
		//alert("The date format should be : dd/mm/yyyy")
		return false;
		}else{
					setErrorText("DateValidations","");
	}
	}
	if (strMonth.length<1 || month<1 || month>12){
		setErrorText("DateValidations","Please enter a valid month");
		//alert("Please enter a valid month")
		return false;
	}else{
					setErrorText("DateValidations","");
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>parseInt(daysInFebruary(year))) || day > parseInt(daysInMonth[month])){
		setErrorText("DateValidations","Please enter a valid day");
		//alert("Please enter a valid day")
		return false;
		}else{
					setErrorText("DateValidations","");
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		setErrorText("DateValidations","Please enter a valid 4 digit year between "+minYear+" and "+maxYear);
		//alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false;
		}else{
					setErrorText("DateValidations","");
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		setErrorText("DateValidations","Please enter a valid date");
		//alert("Please enter a valid date")
		return false;
		}else{
					setErrorText("DateValidations","");
	}

return true
}
function isValidpopup(formName,obDate,ibDate)
{
if(formName=='mapviewform'){
	var nOr = document.getElementById('NumberOfRooms_PopupMap').value;
}else if(formName=='popupform1'){
	var nOr = document.getElementById('NumberOfRooms_Popup').value;
}else if(formName=='popupform2'){
	var nOr = document.getElementById('NumberOfRooms_PopupCY').value;
}else if(formName=='popupform3'){
	var nOr = document.getElementById('NumberOfRooms_PopupCH').value;
}else{
	if(document.getElementById('videotourform')){
	var nOr = document.getElementById('NumberOfRooms_PopupVT').value;
	}
}
 var totPaxes = 0;
 var ADT = new Array(3);
 var CHD = new Array(3);
 for(var x=0; x<Number(nOr); x++)
 {
if(formName=='mapviewform'){
  ADT[x] = document.getElementById('Adult'+''+(x+1)+'_PopupMap').value;
  CHD[x] = document.getElementById('Child'+''+(x+1)+'_PopupMap').value;
}else if(formName=='popupform1'){
  ADT[x] = document.getElementById('Adult'+''+(x+1)+'_Popup').value;
  CHD[x] = document.getElementById('Child'+''+(x+1)+'_Popup').value;
}else if(formName=='popupform2'){
  ADT[x] = document.getElementById('Adult'+''+(x+1)+'_PopupCY').value;
  CHD[x] = document.getElementById('Child'+''+(x+1)+'_PopupCY').value;
}else if(formName=='popupform3'){
  ADT[x] = document.getElementById('Adult'+''+(x+1)+'_PopupCH').value;
  CHD[x] = document.getElementById('Child'+''+(x+1)+'_PopupCH').value;
}else{
	if(document.getElementById('videotourform')){
	ADT[x] = document.getElementById('Adult'+''+(x+1)+'_PopupVT').value;
	CHD[x] = document.getElementById('Child'+''+(x+1)+'_PopupVT').value;
	}
}
 }
	var totPaxesR1 = Number(ADT[0]) + Number(CHD[0]);
	var totPaxesR2 = Number(ADT[1]) + Number(CHD[1]);
	var totPaxesR3 = Number(ADT[2]) + Number(CHD[2]);
	var totPaxesR4 = Number(ADT[3]) + Number(CHD[3]);
	//alert('TotPaxesR1: '+totPaxesR1+'\nTotPaxesR2:'+totPaxesR2+'\nTotPaxesR3:'+totPaxesR3+'\nTotPaxesR4:'+totPaxesR4)
	if(totPaxesR1>4 || totPaxesR2>4 || totPaxesR3>4 || totPaxesR4>4){
	alert("You are allowed a maximum of 4 guests per room booking.");
	return false;
	}
	//var CHD=document.forms[""+formName].HCHD.value;
	//var ADT=document.forms[""+formName].HADT.value;
	//var totPax=parseInt(ADT)+parseInt(CHD);
	var isvalidD=isDatepopup(obDate)
	if(!isvalidD){
		return false;
	}
	var isvalidR=isDatepopup(ibDate)
	if(!isvalidR){
		return false;
	}
	var obDate=obDate.split('/');
	var ibDate=ibDate.split('/');
	var obDay=obDate[0];
	var obYear=obDate[2];
	var obMonth=(obDate[1]-1)
	var ibDay=ibDate[0];
	var ibYear=ibDate[2];
	var ibMonth=(ibDate[1]-1)
	var today=new Date();
	var D=today.getDate();if(D<10){D="0"+D;}
	var M=today.getMonth();if(M<10){M="0"+M;}
	var Y=today.getFullYear();
	var objDiff=daysElapsed(new Date(ibYear,ibMonth,ibDay),new Date(obYear,obMonth,obDay));
	var objDt=daysElapsed(new Date(obYear,obMonth,obDay),new Date(Y,M,D));

	if(objDt<0){
		alert("Depart date can't be in past")
		return false;
	}

	if(objDiff<0)
	{
		alert("Checkout date must occur after the Checkin date.")
		return false;
	}
	if(objDiff>14)
	{
		alert("Your stay period can not be more than a fortnight for online hotel booking. \n If your stay period is more than this, please call our call center.");
		return false;
	}
	 if(objDiff==0)
        {
			alert("Checkout and Checkin date can't be same.")
                return false;
		}

	//if((totPax>3)&&(document.forms[""+formName].chdage1.value>4) && (document.forms[""+formName].chdage2.value>4)){
       // alert("You are allowed a maximum of 3 guests per booking online.\n If your booking is larger than this , please call our call centre.")
        //return false;
       // }
/***********************************************************************
	Code for validation for child age select start here (Find Hotels on Map)
 ************************************************************************/
  if(document.getElementById('mapviewform')){
		document.getElementById('ChildAgeErrorDivpopupmap').style.display="none";
		var agelistch = new Array(8);
		agelistch[0] = document.getElementById('PopupMapChildAgeControl11_AgeList');
		agelistch[1] = document.getElementById('PopupMapChildAgeControl12_AgeList');
		agelistch[2] = document.getElementById('PopupMapChildAgeControl13_AgeList');
		agelistch[3] = document.getElementById('PopupMapChildAgeControl14_AgeList');
		agelistch[4] = document.getElementById('PopupMapChildAgeControl15_AgeList');
		agelistch[5] = document.getElementById('PopupMapChildAgeControl16_AgeList');
		agelistch[6] = document.getElementById('PopupMapChildAgeControl17_AgeList');
		agelistch[7] = document.getElementById('PopupMapChildAgeControl18_AgeList');
			for (var y = 1; y <= 4; y++){
					for (var x = 1; x <= 2; x++){
						if(document.getElementById('Child'+y+'_PopupMap').value>0){
							if(document.getElementById('PopupMapChildAge'+y+''+x).style.display=="block" && agelistch[x-1+((y-1)*2)].selectedIndex==0){
								if(document.getElementById('ChildAgeErrorDivpopupmap').style.display!="block"){
										document.getElementById('ChildAgeErrorDivpopupmap').style.display="block";
										return false;
								}
							}
						}
						}
				}
  }
 /***********************************************************************
	Code for validation for child age select start here (Weekend Breaks)
 ************************************************************************/
 if(document.getElementById('popupform1')){
		document.getElementById('ChildAgeErrorDivpopup').style.display="none";
		var agelist = new Array(8);
		agelist[0] = document.getElementById('PopupChildAgeControl11_AgeList');
		agelist[1] = document.getElementById('PopupChildAgeControl12_AgeList');
		agelist[2] = document.getElementById('PopupChildAgeControl13_AgeList');
		agelist[3] = document.getElementById('PopupChildAgeControl14_AgeList');
		agelist[4] = document.getElementById('PopupChildAgeControl15_AgeList');
		agelist[5] = document.getElementById('PopupChildAgeControl16_AgeList');
		agelist[6] = document.getElementById('PopupChildAgeControl17_AgeList');
		agelist[7] = document.getElementById('PopupChildAgeControl18_AgeList');
			for (var y = 1; y <= 4; y++){
					for (var x = 1; x <= 2; x++){
						if(document.getElementById('Child'+y+'_Popup').value>0){
							if(document.getElementById('PopupChildAge'+y+''+x).style.display=="block" && agelist[x-1+((y-1)*2)].selectedIndex==0){
								if(document.getElementById('ChildAgeErrorDivpopup').style.display!="block"){
										document.getElementById('ChildAgeErrorDivpopup').style.display="block";
										return false;
								}
							}
						}
						}
				}
 }
/***********************************************************************
	Code for validation for child age select start here (Best Business Stays)
 ************************************************************************/
 if(document.getElementById('popupform2')){
		document.getElementById('ChildAgeErrorDivpopupcy').style.display="none";
		var agelistcy = new Array(8);
		agelistcy[0] = document.getElementById('PopupCYChildAgeControl11_AgeList');
		agelistcy[1] = document.getElementById('PopupCYChildAgeControl12_AgeList');
		agelistcy[2] = document.getElementById('PopupCYChildAgeControl13_AgeList');
		agelistcy[3] = document.getElementById('PopupCYChildAgeControl14_AgeList');
		agelistcy[4] = document.getElementById('PopupCYChildAgeControl15_AgeList');
		agelistcy[5] = document.getElementById('PopupCYChildAgeControl16_AgeList');
		agelistcy[6] = document.getElementById('PopupCYChildAgeControl17_AgeList');
		agelistcy[7] = document.getElementById('PopupCYChildAgeControl18_AgeList');
			for (var y = 1; y <= 4; y++){
					for (var x = 1; x <= 2; x++){
						if(document.getElementById('Child'+y+'_PopupCY').value>0){
							if(document.getElementById('PopupCYChildAge'+y+''+x).style.display=="block" && agelistcy[x-1+((y-1)*2)].selectedIndex==0){
								if(document.getElementById('ChildAgeErrorDivpopupcy').style.display!="block"){
										document.getElementById('ChildAgeErrorDivpopupcy').style.display="block";
										return false;
								}
							}
						}
						}
				}
 }
/***********************************************************************
	Code for validation for child age select start here (Video tour)
 ************************************************************************/
 if(document.getElementById('videotourform')){
		document.getElementById('ChildAgeErrorDivpopupvt').style.display="none";
		var agelistvt = new Array(8);
		agelistvt[0] = document.getElementById('PopupVTChildAgeControl11_AgeList');
		agelistvt[1] = document.getElementById('PopupVTChildAgeControl12_AgeList');
		agelistvt[2] = document.getElementById('PopupVTChildAgeControl13_AgeList');
		agelistvt[3] = document.getElementById('PopupVTChildAgeControl14_AgeList');
		agelistvt[4] = document.getElementById('PopupVTChildAgeControl15_AgeList');
		agelistvt[5] = document.getElementById('PopupVTChildAgeControl16_AgeList');
		agelistvt[6] = document.getElementById('PopupVTChildAgeControl17_AgeList');
		agelistvt[7] = document.getElementById('PopupVTChildAgeControl18_AgeList');
			for (var y = 1; y <= 4; y++){
					for (var x = 1; x <= 2; x++){
						if(document.getElementById('Child'+y+'_PopupVT').value>0){
							if(document.getElementById('PopupVTChildAge'+y+''+x).style.display=="block" && agelistvt[x-1+((y-1)*2)].selectedIndex==0){
								if(document.getElementById('ChildAgeErrorDivpopupvt').style.display!="block"){
										document.getElementById('ChildAgeErrorDivpopupvt').style.display="block";
										return false;
								}
							}
						}
						}
				}
 }
 /***********************************************************************
	Code for validation for child age select start here (Chain Hotel)
 ************************************************************************/
  if(document.getElementById('popupform3')){
		document.getElementById('ChildAgeErrorDivpopupch').style.display="none";
		var agelistch = new Array(8);
		agelistch[0] = document.getElementById('PopupCHChildAgeControl11_AgeList');
		agelistch[1] = document.getElementById('PopupCHChildAgeControl12_AgeList');
		agelistch[2] = document.getElementById('PopupCHChildAgeControl13_AgeList');
		agelistch[3] = document.getElementById('PopupCHChildAgeControl14_AgeList');
		agelistch[4] = document.getElementById('PopupCHChildAgeControl15_AgeList');
		agelistch[5] = document.getElementById('PopupCHChildAgeControl16_AgeList');
		agelistch[6] = document.getElementById('PopupCHChildAgeControl17_AgeList');
		agelistch[7] = document.getElementById('PopupCHChildAgeControl18_AgeList');
			for (var y = 1; y <= 4; y++){
					for (var x = 1; x <= 2; x++){
						if(document.getElementById('Child'+y+'_PopupCH').value>0){
							if(document.getElementById('PopupCHChildAge'+y+''+x).style.display=="block" && agelistch[x-1+((y-1)*2)].selectedIndex==0){
								if(document.getElementById('ChildAgeErrorDivpopupch').style.display!="block"){
										document.getElementById('ChildAgeErrorDivpopupch').style.display="block";
										return false;
								}
							}
						}
						}
				}
  }
return true;
}
function isDatepopup(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strDay=dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		alert("The date format should be : dd/mm/yyyy")
		return false;
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Please enter a valid month")
		return false;
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid day")
		return false;
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false;
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date")
		return false;
	}
return true
}
function daysElapsed(date1,date2)
{

 var difference =
        Date.UTC(date1.getYear(),date1.getMonth(),date1.getDate(),0,0,0)
      - Date.UTC(date2.getYear(),date2.getMonth(),date2.getDate(),0,0,0);
   return  difference=(difference)/(1000*60*60*24);

}
//to check if date is valid or not;
var dtCh= "/";
var minYear=1900;
var maxYear=2100;
function isInteger(s){
var i;
    for (i = 0; i < s.length; i++){
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    for (i = 0; i < s.length; i++){
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}
function daysInFebruary (year){
	 return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   }
   return this
}



