var NN = document.layers;
var IE = document.all;
var oForm = document.frmBook;

function fnUpdBooking(oHidden){
    //oForm = window.opener.document.frmBook;
    oForm = document.frmBook;
    
    sSelectedDate = oHidden.value;
    aSelectedDate = sSelectedDate.split('/');
    
    sDay = arrSelectedDate[1];
    sMonth = arrSelectedDate[2] + arrSelectedDate[0];
    
    switch(oHidden.name.toLowerCase()){
		case 'depdate':{
      		sChanged = 'd';
      		oDeptMonth = oForm.departure_month;
			oDeptDay = oForm.departure_day;
			fnChangeMonth(oDeptMonth,oDeptDay,arrSelectedDate[0],sDay);
			fnMonthChanged('d');
			break;
		}
		case 'retdate':{
			sChanged = 'r';
			oRetMonth = oForm.return_month;
			oRetDay = oForm.return_day;
			fnChangeMonth(oRetMonth,oRetDay,arrSelectedDate[0],sDay);
			fnMonthChanged('r');
        	break;
		}
	}
}

function fnAdultsChanged(){
	var oSelAdults;
	var oSelChildren;
	var iOldChildrenValue;
	
	oSelAdults = oForm.number_passenger;
	oSelChildren = oForm.number_children;
	
	iOldChildrenValue = oForm.number_children.selectedIndex;
	
	//CLEAR CHILDREN DROPDOWN
	for(i=0;i<oSelChildren.options.length;i++){
		oSelChildren.options.remove(i);
		i--;
	}
	//REFILL CHIDREN DROPDOWN
	for(i=0;i<(5 - oSelAdults.options[oSelAdults.selectedIndex].value);i++){
		var oOption = document.createElement("OPTION");
		oSelChildren.options.add(oOption);
		oOption.innerText = i;
		oOption.value = i;
	}
	//RESELECT CHILDREN
	if(iOldChildrenValue<oSelChildren.options.length){
		oSelChildren.options[iOldChildrenValue].selected = true;
	}
	else{
		oSelChildren.options[oSelChildren.options.length - 1].selected = true;
		alert(sComment1 + oSelChildren.options[oSelChildren.options.length - 1].value + sComment2);
	}
}

function fnHourChanged(id){
	var dToday = new Date();
	var oSelHour;
	var oSelMonth;
	var oSelDay;
	var iHour = dToday.getHours();
	var iSelHour;
	var sMonth;
	var sYear;
	
	//CHECK IF HOUR DROPDOWN PRESENT
	if((oForm.departure_hour)||(oForm.return_hour)){
		if(id=='d'){
			oSelHour = oForm.departure_hour;
			oSelMonth = oForm.departure_month;
			oSelDay = oForm.departure_day;
		}
		else{
			oSelHour = oForm.return_hour;
			oSelMonth = oForm.return_month;
			oSelDay = oForm.return_day;
		}
		
		iSelHour = oSelHour.options[oSelHour.selectedIndex].value;
		iSelHour = iSelHour.substr(0,2);
		
		// Extract month and year
	   	sMonth = parseFloat(oSelMonth[oSelMonth.selectedIndex].value.substr(4,2));
	  	sYear = parseFloat(oSelMonth[oSelMonth.selectedIndex].value.substr(0,4));
	  	if(oSelHour.selectedIndex == 0){
	  		dSelectedDate = new Date(sYear,sMonth-1,oSelDay.options[oSelDay.selectedIndex].value,0,0);
	  	}
	  	else{
	   		dSelectedDate = new Date(sYear,sMonth-1,oSelDay.options[oSelDay.selectedIndex].value,iSelHour,0);
		}
		
		dToday.setDate(dToday.getDate()+2)
		if(dSelectedDate<dToday){
			oSelHour.options.selectedIndex = parseInt(iHour,10) + 2;
		}
	}
}

function fnMonthChanged(id){
	var oDeptMonth;
	var oRetMonth;
	var oDeptDay;
	var oRetDay;
	var iDeptMonth;
	var iRetMonth;
	var iDeptDay;
	var iRetDay;
	var iDeptYear;
	var iRetYear;
	var iDeptPrevSelDayIdx;
	var iRetPrevSelDayIdx;
	
	oDeptMonth = oForm.departure_month;
	oRetMonth = oForm.return_month;
	oDeptDay = oForm.departure_day;
	oRetDay = oForm.return_day;
	iDeptMonth = fnGetSelMonth(oDeptMonth);
	iRetMonth = fnGetSelMonth(oRetMonth);
	iDeptDay = fnGetSelDay(oDeptDay);
	iRetDay = fnGetSelDay(oRetDay);
	iDeptYear = fnGetSelYear(oDeptMonth);
	iRetYear = fnGetSelYear(oRetMonth);

	iDeptPrevSelDayIdx = oDeptDay.selectedIndex;
	iRetPrevSelDayIdx = oRetDay.selectedIndex;
	if(id=='d'){
		fnRefillMonth(oDeptDay,fnGetDaysInMonth(iDeptMonth,iDeptYear));
		//Check if previously selected day exists in new month
		//If not don't select a day
		if(iDeptPrevSelDayIdx<oDeptDay.options.length){
			oDeptDay.selectedIndex = iDeptPrevSelDayIdx;
		}
		else{
			oDeptDay.selectedIndex = oDeptDay.options.length - 1;
		}
	}
	else{
		fnRefillMonth(oRetDay,fnGetDaysInMonth(iRetMonth,iRetYear));
		//Check if previously selected day exists in new month
		//If not don't select a day
		if(iRetPrevSelDayIdx<oRetDay.options.length){
			oRetDay.selectedIndex = iRetPrevSelDayIdx;
		}
		else{
			oRetDay.selectedIndex = oRetDay.options.length - 1;
		}
	}
	//Check if selected day not in the pas
	if(id=='d'){
		if (fnCheckIfInThePast(oDeptMonth,oDeptDay)){
			fnChangeMonth(oDeptMonth,oDeptDay,iDeptMonth + 1,0);
			fnMonthChanged('d');
		}
	}
	else{
		if (fnCheckIfInThePast(oRetMonth,oRetDay)){
			fnChangeMonth(oRetMonth,oRetDay,iRetMonth + 1,0);
			fnMonthChanged('r');
		}
	}
	
	//Check if the unchanged day is valid against the changed day
	if(id=='d'){
		if((iDeptMonth>=iRetMonth)&&(iDeptYear==iRetYear)){
			if(iDeptDay>iRetDay){
				fnChangeMonth(oRetMonth,oRetDay,iDeptMonth,iDeptDay);
			}
			else{
				fnChangeMonth(oRetMonth,oRetDay,iDeptMonth,0);
			}
		}
		else if(iDeptYear>iRetYear){
			fnChangeMonth(oRetMonth,oRetDay,iDeptMonth,iDeptDay);
		}
		//fnHourChanged('d');
	}
	else{
		if((iRetMonth<=iDeptMonth)&&(iDeptYear==iRetYear)){
			if(iDeptDay>iRetDay){
				fnChangeMonth(oDeptMonth,oDeptDay,iRetMonth,iRetDay);
			}
			else{
				fnChangeMonth(oDeptMonth,oDeptDay,iRetMonth,0);
			}
		}
		else if(iDeptYear>iRetYear){
			fnChangeMonth(oDeptMonth,oDeptDay,iRetMonth,iRetDay);
		}
		//fnHourChanged('r');
	}
	fnRefreshHiddenFlds();
}

function fnCheckIfInThePast(oSelM, oSelD){
	var dToday = new Date();
	var dSelectedDate;
	var sMonth;
	var sYear;

	// Extract month and year
   	sMonth = parseFloat(oSelM[oSelM.selectedIndex].value.substr(4,2));
  	sYear = parseFloat(oSelM[oSelM.selectedIndex].value.substr(0,4));
   	dSelectedDate = new Date(sMonth + '/' + oSelD.options[oSelD.selectedIndex].value + '/' + sYear);
	
	dToday.setDate(dToday.getDate()+1)
	if (dSelectedDate < dToday){
		return(true);
	}
	return(false);
}

function fnChangeMonth(oSel,oDay,iMonth,iDay){
	//Store previously selected day
	var iDayIdx = oDay.selectedIndex;
	for(i=0;i<oSel.options.length;i++){
		sMon = parseInt(oSel.options[i].value.substr(4,2),10);
		if(sMon==iMonth){
			oSel.selectedIndex = i;
		}
	}
	//Refill month with number of days of new month
	fnRefillMonth(oDay,fnGetDaysInMonth(iMonth,parseInt(oSel.options[oSel.selectedIndex].value.substr(0,4),10)));
	//Check if previously selected day in new month
	if(iDay==0){
		if(iDayIdx<oDay.options.length){
			oDay.selectedIndex = iDayIdx;
		}
		else{
			//If day not in month, select last day of the month
			oDay.selectedIndex = oDay.options.length - 1;
		}
	}
	else{
		oDay.selectedIndex = iDay - 1;
	}
}

function fnDayChanged(id){
	var oDeptMonth;
	var oRetMonth;
	var oDeptDay;
	var oRetDay;
	var iDeptMonth;
	var iRetMonth;
	var iDeptDay;
	var iRetDay;
	
	oDeptMonth = oForm.departure_month;
	oRetMonth = oForm.return_month;
	oDeptDay = oForm.departure_day;
	oRetDay = oForm.return_day;
	iDeptMonth = fnGetSelMonth(oDeptMonth);
	iRetMonth = fnGetSelMonth(oRetMonth);
	iDeptDay = fnGetSelDay(oForm.departure_day);
	iRetDay = fnGetSelDay(oForm.return_day);
	
	iDeptYear = fnGetSelYear(oDeptMonth);
	iRetYear = fnGetSelYear(oRetMonth);
	
	if(iDeptMonth==iRetMonth){
		if(id=='d'){
			if(iDeptDay>iRetDay){
				oForm.return_day.selectedIndex = oForm.departure_day.selectedIndex;
			}
		}
		else{
			if(iRetDay<iDeptDay){
				if(iDeptMonth != iRetMonth){
					oForm.departure_day.selectedIndex = oForm.return_day.selectedIndex;
				}
			}
		}
	}
	
	//Check if selected day not in the pas
	if(id=='d'){
		if (fnCheckIfInThePast(oDeptMonth,oDeptDay)){
			fnChangeMonth(oDeptMonth,oDeptDay,iDeptMonth + 1,0);
			fnMonthChanged('d');
		}
	}
	else{
// 		if (fnCheckIfInThePast(oRetMonth,oRetDay)){
// 			fnChangeMonth(oRetMonth,oRetDay,iRetMonth + 1,0);
// 			fnMonthChanged('r');
// 		}
		if((iRetMonth<=iDeptMonth)&&(iDeptYear==iRetYear)){
			if(iDeptDay>iRetDay){
				//fnChangeMonth(oDeptMonth,oDeptDay,iRetMonth,iRetDay);
				oForm.return_month.selectedIndex += 1;
			}
		}
		else if(iDeptYear>iRetYear){
			fnChangeMonth(oDeptMonth,oDeptDay,iRetMonth,iRetDay);
		}
	}
	
	fnRefreshHiddenFlds();
}

function fnRefreshHiddenFlds(){
	var oHid_D_Date;
	var oHid_R_Date;
	var oSelect_Day_D;
	var oSelect_Day_R;
	var oSelect_Month_D;
	var oSelect_Month_R;
     
	var sD_Date = '';
	var sR_Date = '';
     
	oHid_D_Date = oForm.depDate;
	oHid_R_Date = oForm.retDate;
	oSelect_Day_D = oForm.departure_day;
	oSelect_Day_R = oForm.return_day;
	oSelect_Month_D = oForm.departure_month;
	oSelect_Month_R = oForm.return_month;
     
	sD_Date = oSelect_Month_D.options[oSelect_Month_D.selectedIndex].value.substr(4,2) + '/' + (oSelect_Day_D.selectedIndex + 1) + '/' + oSelect_Month_D.options[oSelect_Month_D.selectedIndex].value.substr(0,4);
	sR_Date = oSelect_Month_R.options[oSelect_Month_R.selectedIndex].value.substr(4,2) + '/' + (oSelect_Day_R.selectedIndex + 1) + '/' + oSelect_Month_R.options[oSelect_Month_R.selectedIndex].value.substr(0,4);
	
	oHid_D_Date.value = sD_Date;
	oHid_R_Date.value = sR_Date;
}

function fnGetDaysInMonth(month, year) {
	var daysInMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
	var lDaysInMonth = [31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
    
	if ((year % 4) == 0){
		if ((year % 100) == 0 && (year % 400) != 0){
			return daysInMonth[month-1];
		}
		else{
	    	return lDaysInMonth[month-1];
		}
	} 
	else{
		return daysInMonth[month-1];
	}
}

function fnRefillMonth(oSel,iDaysInMonth){
	var oOption;
	
	//Empty dropdown
	while(oSel.options.length){
		d = oSel.options.length - 1;
		oSel[d] = null;
	}
	
	//Refill
	for(i=1 ; i <= iDaysInMonth ; i++){
		if (IE){
			oOption = oForm.document.createElement("OPTION");
		}
		else{
			oOption = new Option(i,i);
		}
      	oOption.value = i;
      	oOption.text = i;
      
		oSel.options[oSel.options.length] = oOption;
      
		oOption = null; 
    }
}

function fnGetSelMonth(oSel){
	var sMonth;
	sMonth = oSel.options[oSel.selectedIndex].value
	sMonth = parseInt(sMonth.substr(4,2),10);
	return sMonth;
}

function fnGetSelYear(oSel){
	var sYear;
	sYear = oSel.options[oSel.selectedIndex].value
	sYear = parseInt(sYear.substr(0,4),10);
	return sYear;
}

function fnGetSelDay(oSel){
	var sDay;
	sDay = oSel.options[oSel.selectedIndex].value
	sDay = parseInt(sDay,10);
	return sDay;
}

function DateAdd(startDate, numDays, numMonths, numYears)
{
	var returnDate = new Date(startDate.getTime());
	var yearsToAdd = numYears;
	
	var month = returnDate.getMonth()	+ numMonths;
	if (month > 11)
	{
		yearsToAdd = Math.floor((month+1)/12);
		month -= 12*yearsToAdd;
		yearsToAdd += numYears;
	}
	returnDate.setMonth(month);
	returnDate.setFullYear(returnDate.getFullYear()	+ yearsToAdd);
	
	returnDate.setTime(returnDate.getTime()+60000*60*24*numDays);
	
	return returnDate;
}