var cmbInMonth_Org;
var cmbInDay_Org;
var cmbOutMonth_Org;
var cmbOutDay_Org;
var thisWinFrm;
var Chk_In_Date;
var Chk_Out_Date;
var InDay;
var InMon;
var InYr;
var DayIn;
var MonthIn;
var YearIn;
var OutDay;
var OutMon;
var OutYr;
var DayOut;
var MonthOut;
var YearOut;
var Timer;
var tmp;
var nightCmb;
var isYrApp;
var myMinLOS=1;
var SelectedNights=1;
//var myMinLOS1=1;
var myMaxLOS=30;
var dtDiff;
tmp = 0;
//FUNCTION USED TO OPEN NEW WINDOW WITH MAPED LINK
//*****************************************************************************************************************************************
	function GoToCalendar(CalName,LinkName) {
		var myFrm;
		var selDate;
		var selDate1;
		var thisWinFrm;
		
		thisWinFrm=document.forms['frmTajRes'];
		myFrm=thisWinFrm;
				
		if(LinkName=='CHECK-IN') {
			selDate=MonthIn[MonthIn.selectedIndex].text + ' ' +DayIn[DayIn.selectedIndex].text
		}

		if(LinkName=='CHECK-OUT') {
			selDate1=MonthIn[MonthIn.selectedIndex].text
			selDate1 = selDate1.replace(' ','-')+'-'+DayIn[DayIn.selectedIndex].text
			selDate=MonthOut[MonthOut.selectedIndex].text + ' '+DayOut[DayOut.selectedIndex].text
		}
		
		myselDate=selDate.split(' ')
		selDate=''
		
		for(i=0;i<myselDate.length;i++) {
			if(selDate=='') {
				selDate=myselDate[i]
			}
			else {
				selDate=selDate+ '-' +myselDate[i]
			}
		}
		
		if(LinkName=='CHECK-OUT') 
		{
		selDate+='-' + YearOut[YearOut.selectedIndex].text;
		selDate1+='-' + YearIn[YearIn.selectedIndex].text;
		}
		else
		{
		  selDate+='-' + YearIn[YearIn.selectedIndex].text;
		}
		
		
		calendar_url="Res_Calendar.aspx?formName=frmTajRes&formCtrl="+LinkName+"&selDate="+selDate+"&selDate1="+selDate1
		
		var MyCalWindow_Width=(window.screen.width/3-60)+10
		var MyCalWindow_Height=window.screen.height/2-170
		var MyCalWindow_Left=MyCalWindow_Width/2
		var MyCalWindow_Top=MyCalWindow_Height/2
		
		this.MyCalWindow_Open=true;
		
		MyCalWindow=window.open(calendar_url,CalName,"toolbar=no,location=no,status=no,menubar=no,scrollbars=auto,resizable=yes,alwaysRaised,titlebar=no,width=265,height=214,left="+MyCalWindow_Left+",top="+MyCalWindow_Top+"");
	}
//*****************************************************************************************************************************************
	//FUNCTION WILL BE CALLED FROM THE OPENED CALENDAR WINDOW TO SET THE DATE DROP DOWN LIST BOXES		
//*****************************************************************************************************************************************
	
	function CloseCalendarWindow() {
		if(this.MyCalWindow_Open==true) {
			MyCalWindow.close();
			MyCalWindow_Open=false;
		}
	}
	function calendarCall(formCtrl,myMonthYear,dtD) {
	   
	   myYr = myMonthYear.split(' ');  
	   
	   SetDate(formCtrl,myYr[1],myYr[0],dtD);
	   /*
		var frm;
		var thisWinFrm;
		var myYr;
		thisWinFrm=document.forms['frmTajRes'];
		frm=thisWinFrm;
		myYr = myMonthYear.split(' ');

		if(formCtrl=="CHECK-IN") {
			for(i=0;i<MonthIn.length;i++) {
				if (myYr[1] == YearIn[YearIn.selectedIndex].value) {
					if(MonthIn[i].value==myMonthYear) {
						MonthIn.selectedIndex = i;
						break;
					}
				}
				else {
					if(MonthIn[i].value!=myMonthYear) {
						if (myYr[1] > YearIn[YearIn.selectedIndex].value) {
							//alert(YearIn[YearIn.selectedIndex].value + "\n" + myYr[1] + "\n" + (YearIn[YearIn.selectedIndex].value - myYr[1]));
							YearIn.selectedIndex = myYr[1] - YearIn[YearIn.selectedIndex].value;
							load_Months_In_Year(frm);
						}
						MonthIn.selectedIndex = i;
						break;
					}
				}
			}

			cmbMonth_Onchange(MonthIn,DayIn)
			
			for(j=0;j<DayIn.length;j++) {
				if(DayIn[j].text==dtD) {
					DayIn.selectedIndex = j;
					break;
				}								
			}
		}

		if(formCtrl=="CHECK-OUT") {
			for(i=0;i<MonthOut.length;i++) {
				if(MonthOut[i].value==myMonthYear) {
					MonthOut.selectedIndex = i;
					break;
				}
			}

			cmbMonth_Onchange(MonthOut,DayOut)
			
			for(j=0;j<DayOut.length;j++) {			  
				if(DayOut[j].text==dtD) {
					DayOut.selectedIndex = j;
					break;
				}
			}
		}
		cmbInMonth_Onchange(frm);
		
		*/
		
	}
//*****************************************************************************************************************************************
	//THIS COMMON FUNCTION WILL SET THE CHECK-IN AND CHECK-OUT DAY DROP DOWN LIST BASED ON SELECTED MONTH
//*****************************************************************************************************************************************
	
	function cmbMonth_Onchange(cmbMonth,cmbDay) {
		(cmbMonth.selectedIndex==-1)?cmbMonth.selectedIndex=0:0;
		var myMonth=cmbMonth[cmbMonth.selectedIndex].value;
		for(i=0;i<this.arrayCal.length;i++) {
			if(this.arrayCal[i]==myMonth) {
			 
				var myDay=this.arrayCal[this.arrayCal[i]]
				cmbDay.options.length=myDay.length;			
				
				}
			}
		      
				var StrMonthYear=new String(myMonth);
				var ArrMonthYear=StrMonthYear.split(' ');
				var MonthName=ArrMonthYear[0];
				var Month=GetMonthNumber(ArrMonthYear[0]);
				var Year=ArrMonthYear[1];     
				var MyDate=new Date(Year,Month,1);
				var StrDate=new String(MyDate);
				var StrDates=StrDate.split(' ');
				
				var NoOfDays=getMonthLen(Year,Month)
				
				    
	    
				cmbDay.options.length=NoOfDays;				
				for(j=0;j<NoOfDays;j++) {
					cmbDay.options[j].text=j+1;
					cmbDay.options[j].value=j+1;
					
				}
		
	}
//*****************************************************************************************************************************************
	//FOLLOWING FUNCTIONS WILL BE CALLED WHEN THE MONTH DROP DOWN LIST BOX CHANGES
//*****************************************************************************************************************************************
	function cmbInMonth_Onchange(frm) {	    
	   
		cmbMonth_Onchange(MonthIn, DayIn);
		(isYrApp)?Chk_And_Set_Check_Out_Date(frm):0;
	}

	function cmbOutMonth_Onchange(frm) {	
		cmbMonth_Onchange(MonthOut,DayOut)
		Chk_And_Set_Check_Out_Date(frm);
	}
	
	function Set_In_Out_Combo(frm) {
		for (i=0;i<document.forms[0].length;i++) {
			if((frm.elements[i].name).substring(6,16) == "cmbInMonth")
				MonthIn = frm.elements[i];
			if((frm.elements[i].name).substring(6,16) == "cmbInDay")
				DayIn = frm.elements[i];
			if((frm.elements[i].name).substring(6,16) == "cmbInYear") 
				YearIn = frm.elements[i];
			if((frm.elements[i].name).substring(6,17) == "cmbOutMonth") 
				MonthOut = frm.elements[i];
			if((frm.elements[i].name).substring(6,16) == "cmbOutDay") 
				DayOut = frm.elements[i];
			if((frm.elements[i].name).substring(6,16) == "cmbOutYear") 
				YearOut = frm.elements[i];
			if((frm.elements[i].name).substring(0,8) == "cmbNight") 
				nightCmb = frm.elements[i];
		}
		isYrApp = true;
	}
	function Set_In_Out_Search_Combo(frm) {
		for (i=0;i<document.forms[0].length;i++) {
			if (((frm.elements[i].name).substring(6,18) == "cmbChkInMnth") || (frm.elements[i].name == "cmbChkInMnth"))
				MonthIn = frm.elements[i];
			if (((frm.elements[i].name).substring(6,18) == "cmbChkInDt") || (frm.elements[i].name == "cmbChkInDt"))
				DayIn = frm.elements[i];
		}
		isYrApp = false;
	}
//*****************************************************************************************************************************************
	//FUNCTION USED TO CONTROL THE CHECK-OUT DATE BASED ON RESTRICTIONS	
//*****************************************************************************************************************************************
	function Chk_And_Set_Check_Out_Date(frm) {	
	if(myMinLOS1>myMinLOS)	
	     myMinLOS=myMinLOS1;
		//DECLARE VARIABLE TO HOLD MINLOS,MAXLOS,AND APPLICABLE DAYS
		var myAppDays="1111111";

		//DECLARE VARIABLES TO HOLD MAX AND MIN CHECK OUT DAY BASED ON RESTRICTION
		var Chk_Out_Date_Min;
		var Chk_Out_Date_Max;
		
		//ARRAY WILL GIVE THE JAVASCRIPT MONTH NAME BY INDEX NUMBER
		var GetJs_Month_Name_By_Index=new Array('January','February','March','April','May','June','July','August','September','October','November','December')
		
		//GETTING THE CHECK-IN MONTH AND DAY TO CREATE CHECK-OUT MIN AND MAX DATE
		Check_In_Month=MonthIn[MonthIn.selectedIndex].value;
		Check_In_Day=DayIn[DayIn.selectedIndex].text;	
		var arrayInMnYr=Check_In_Month.split(' ')
		
		//GETTING THE CHECK OUT MONTH
		Check_Out_Month=MonthOut[MonthOut.selectedIndex].value;
		Check_Out_Day=DayOut[DayOut.selectedIndex].text;	
		var arrayOutMnYr=Check_Out_Month.split(' ')
		
		//CREATING ACTUAL JAVASCRIPT DATES FOR MANIPULATION
		Chk_In_Date=new Date(arrayInMnYr[1],GetJs_Month_Index_By_Name(arrayInMnYr[0]),parseInt(Check_In_Day))
		Chk_Out_Date=new Date(arrayOutMnYr[1],GetJs_Month_Index_By_Name(arrayOutMnYr[0]),parseInt(Check_Out_Day))
		
		Chk_Out_Date_Min=new Date(arrayInMnYr[1],GetJs_Month_Index_By_Name(arrayInMnYr[0]),parseInt(Check_In_Day)+parseInt(myMinLOS))
		Set_Chk_Out_Month_Min=GetJs_Month_Name_By_Index[Chk_Out_Date_Min.getMonth()]+' '+Chk_Out_Date_Min.getFullYear()
		Set_Chk_Out_Day_Min=Chk_Out_Date_Min.getDate()

		Chk_Out_Date_Max=new Date(arrayInMnYr[1],GetJs_Month_Index_By_Name(arrayInMnYr[0]),parseInt(Check_In_Day)+parseInt(myMaxLOS))
		Set_Chk_Out_Month_Max=GetJs_Month_Name_By_Index[Chk_Out_Date_Max.getMonth()]+' '+Chk_Out_Date_Max.getFullYear()
		Set_Chk_Out_Day_Max=Chk_Out_Date_Max.getDate()

		var myLastCheckOutDays=arrayCal[MonthOut[MonthOut.length-1].value];
		var myLastCheckOutDay=myLastCheckOutDays[myLastCheckOutDays.length-1];
		var arrayLastCheckOutMnYr=MonthOut[MonthOut.length-1].value.split(' ')
		myLastCheckOutDate=new Date(arrayLastCheckOutMnYr[1],GetJs_Month_Index_By_Name(arrayLastCheckOutMnYr[0]),parseInt(myLastCheckOutDay)+1)
		
		//WHEN SELECTED (CHECK OUT DATE-CHECK IN DATE) IS LESS THAN THE MINLOS
		//--------------------------------------------------------------------------
		if(Chk_Out_Date<Chk_Out_Date_Min) { 
			if(Chk_Out_Date_Min<myLastCheckOutDate) {
				for(i=0;i<MonthOut.length;i++) {
					if(MonthOut[i].value==Set_Chk_Out_Month_Min) {
						MonthOut.selectedIndex=i;
						cmbMonth_Onchange(MonthIn,DayIn);
						cmbMonth_Onchange(MonthOut,DayOut);
						for(j=0;j<DayOut.length;j++) {
							if(DayOut[j].value==Set_Chk_Out_Day_Min) {
								DayOut.selectedIndex=j;
								break;
							}
						}
						break;
					}
				}
			}
			else {
				if (isYrApp) {
					if (YearIn.selectedIndex == 0) {
						YearOut.selectedIndex = 1;
						load_Months_Out_Year();
					} else {
						//if (MonthOut.selectedIndex>0)
							//alert("Check out date is exceeding the maximum reservation span. Please check your check-in date.")
					}
				} else {
					//alert("Check out date is exceeding the maximum reservation span. Please check your check-in date")
				}
				MonthIn.selectedIndex=parseInt(cmbInMonth_Org);
				cmbMonth_Onchange(MonthIn,DayIn);
				cmbMonth_Onchange(MonthOut,DayOut);
				DayIn.selectedIndex=parseInt(cmbInDay_Org);
				MonthOut.selectedIndex=parseInt(cmbOutMonth_Org);
				cmbMonth_Onchange(MonthIn,DayIn);
				cmbMonth_Onchange(MonthOut,DayOut);
				DayOut.selectedIndex=parseInt(cmbOutDay_Org);
				DayIn.focus();
				if (isYrApp) {
					if ( (DayIn.selectedIndex == DayIn.options.length) && (MonthOut.selectedIndex == 0) && (YearIn.selectedIndex < YearOut.selectedIndex) ) {
						DayOut.selectedIndex = 0;
					}
				}
				setNights();
				return;
			}
		}

		//WHEN SELECTED (CHECK OUT DATE-CHECK IN DATE) IS GREATER THAN THE MAXLOS
		if(Chk_Out_Date>Chk_Out_Date_Max) {
			//alert("Maximum length of stay is restricted to "+ myMaxLOS +" day(s) only.");
			for(i=0;i<MonthOut.length;i++) {
				if(MonthOut[i].value==Set_Chk_Out_Month_Max) {
					MonthOut.selectedIndex=i;
					cmbMonth_Onchange(MonthIn,DayIn);
					cmbMonth_Onchange(MonthOut,DayOut);
					for(j=0;j<DayOut.length;j++) {
						if(DayOut[j].text==Set_Chk_Out_Day_Max) {
							DayOut.selectedIndex=j;
							break;
						}
					}
					break;
				}
			}
		}
		//--------------------------------------------------------------------------
		//CHECKING WHETHER THE SELECTED CHECK IN DAY IS FALL IN RESTRICTED DAYS
		//--------------------------------------------------------------------------
		MyChkInDayNo=Chk_In_Date.getDay();
		chk_inday_flag=false;
		valid_days='';
		var Rate_Days_Appl=new String(myAppDays);
		 
		if((Rate_Days_Appl != '1111111')&&(Rate_Days_Appl != '0000000')) {
			for(j=0;j<Rate_Days_Appl.length;j++) {
				if(Rate_Days_Appl.charAt(j)=='1') {
					if(j==MyChkInDayNo) {
						chk_inday_flag=true;
					}
					else {
						switch (j) {
							case 0:
								if(valid_days!=''){valid_days=valid_days+',Sunday';}
								else{valid_days='Sunday';}
								break;
							case 1:
								if(valid_days!=''){valid_days=valid_days+',Monday';}
								else{valid_days='Monday';}
								break;
							case 2:
								if(valid_days!=''){valid_days=valid_days+',Tuesday';}
								else{valid_days='Tuesday';}
								break;
							case 3:
								if(valid_days!=''){valid_days=valid_days+',Wednesday';}
								else{valid_days='Wednesday';}
								break;
							case 4:
								if(valid_days!=''){valid_days=valid_days+',Thursday';}
								else{valid_days='Thursday';}
								break;
							case 5:
								if(valid_days!=''){valid_days=valid_days+',Friday';}
								else{valid_days='Friday';}
								break;
							case 6:
								if(valid_days!=''){valid_days=valid_days+',Saturday';}
								else{valid_days='Saturday';}
								break;
						}
					}
				}
			}
			if(chk_inday_flag==false) {
				alert("For the selected rate, the applicable check-in day(s) are "+valid_days)
				MonthIn.selectedIndex=cmbInMonth_Org;
				cmbMonth_Onchange(MonthIn,DayIn);
				cmbMonth_Onchange(MonthOut,DayOut);
				DayIn.selectedIndex=cmbInDay_Org;
				MonthOut.selectedIndex=cmbOutMonth_Org;
				cmbMonth_Onchange(MonthIn,DayIn);
				cmbMonth_Onchange(MonthOut,DayOut);
				DayOut.selectedIndex=cmbOutDay_Org;
				DayIn.focus();
				setNights();
				return;
			}
		}		
		//--------------------------------------------------------------------------
		//PERSISTING THE SELECTED VALUES		
		cmbInMonth_Org=MonthIn.selectedIndex;
		cmbInDay_Org=DayIn.selectedIndex;
		cmbOutMonth_Org=MonthOut.selectedIndex;
		cmbOutDay_Org=DayOut.selectedIndex;
		setNights();
	}
//*****************************************************************************************************************************************
	//FUNCTION USED TO GET THE JAVASCRIPT MONTH INDEX NUMBER BASED ON MONTH NAME
//*****************************************************************************************************************************************
	function GetJs_Month_Index_By_Name(MonthName) {
		switch(MonthName) {
			case "January":
				return "0";
				break;
			case "February":
				return "1";
				break;
			case "March":
				return "2";
				break;
			case "April":
				return "3";
				break;
			case "May":
				return "4";
				break;
			case "June":
				return "5";
				break;
			case "July":
				return "6";
				break;
			case "August":
				return "7";
				break;
			case "September":
				return "8";
				break;
			case "October":
				return "9";
				break;
			case "November":
				return "10";
				break;
			case "December":
				return "11";
				break;
		}
	}
//************************************************************************
	function load_Months_In_Year() {
		var myYear=YearIn[YearIn.selectedIndex].value;
		var j;
		j=0;
		for(i=0;i<this.arrayCal.length;i++) {
			arrCal = this.arrayCal[i].split(' ')
			if(arrCal[1]==myYear)
				j++;
		}
		MonthIn.options.length=j;
		j=0;
		for(i=0;i<this.arrayCal.length;i++) {
			arrCal = this.arrayCal[i].split(' ')
			if(arrCal[1]==myYear) {
				MonthIn.options[j].text=arrCal[0]
				MonthIn.options[j].value=this.arrayCal[i]
				j++;
			}
		}
		cmbInMonth_Onchange(document.frmTajRes)
		if (YearIn.selectedIndex > YearOut.selectedIndex) {
			YearOut.selectedIndex = YearIn.selectedIndex;
		}
		load_Months_Out_Year()
	}
//************************************************************************
	function load_Months_Out_Year() {
		if (YearIn.selectedIndex > YearOut.selectedIndex) {
			YearOut.selectedIndex = YearIn.selectedIndex
		}
		var myYear=YearOut[YearOut.selectedIndex].value;
		var j;
		j=0;
		for(i=0;i<this.arrayCal.length;i++) {
			arrCal = this.arrayCal[i].split(' ')
			if(arrCal[1]==myYear)
				j++;
		}
		MonthOut.options.length=j;
		j=0;
		for(i=0;i<this.arrayCal.length;i++) {
			arrCal = this.arrayCal[i].split(' ')
			if(arrCal[1]==myYear) {
				MonthOut.options[j].text=arrCal[0]
				MonthOut.options[j].value=this.arrayCal[i]
				j++;
			}
		}
		cmbOutMonth_Onchange(document.frmTajRes)
		if (MonthIn.selectedIndex == -1) {
			setMonth();
		}
	}
//************************************************************************
	function setMonth() {
		Timer=setTimeout("setMonth()",200);
		if (tmp==1) {
			clearTimeout(Timer);
			(MonthIn.selectedIndex==-1)?MonthIn.selectedIndex=0:0;
			(MonthOut.selectedIndex==-1)?MonthOut.selectedIndex=0:0;
		}
		tmp++;
	}
	function y2k(number) {  //called in daysElapsed function if the year is NOT passed as getFullYear then Y2k will take care
		return (number < 1000) ? number + 1900 : number; 
	}
	function daysElapsed(date1,date2) {
		var difference = Date.UTC(y2k(date1.getYear()),date1.getMonth(),date1.getDate(),0,0,0) - Date.UTC(y2k(date2.getYear()),date2.getMonth(),date2.getDate(),0,0,0);
		return difference/1000/60/60/24;
	}
//************************************************************************
	function chngCmbOutDay() {
		var nw_Chk_Out_Date, dtMn, dtYer, dtDy;
		dtDy = nightCmb.selectedIndex+1
		nw_Chk_Out_Date = DayAdd(Chk_In_Date, dtDy)
		dtMn = retMnth(nw_Chk_Out_Date.getMonth()) + ' ' + nw_Chk_Out_Date.getFullYear();
		calendarCall('CHECK-OUT',dtMn,nw_Chk_Out_Date.getDate())
	}
	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;
	}
	function DayAdd(startDate, numDays) {
		return DateAdd(startDate,numDays,0,0);
	}
	function retMnth(val) {
		var retVal;
		switch(val) {
			case 0:
				retVal='January';
				break;
			case 1:
				retVal='February';
				break;
			case 2:
				retVal='March';
				break;
			case 3:
				retVal='April';
				break;
			case 4:
				retVal='May';
				break;
			case 5:
				retVal='June';
				break;
			case 6:
				retVal='July';
				break;
			case 7:
				retVal='August';
				break;
			case 8:
				retVal='September';
				break;
			case 9:
				retVal='October';
				break;
			case 10:
				retVal='November';
				break;
			case 11:
				retVal='December';
				break;
		}
		return retVal;
	}
	function setNights() {
		
		dysEls = daysElapsed(Chk_Out_Date, Chk_In_Date)
		if(myMinLOS1>myMinLOS)
	         myMinLOS=myMinLOS1;	
		if ((dysEls<=30) && (dysEls>=1) )
		{ 
		   if (dysEls<=myMinLOS)		   
		      nightCmb.selectedIndex = myMinLOS-1;		
		   else
		    	nightCmb.selectedIndex = dysEls - 1;
		}	
		if (dysEls>=30)
		{
		    if (dysEls<=myMinLOS)		   
		      nightCmb.selectedIndex = myMinLOS-1;		
		    else
		    	nightCmb.selectedIndex = 29;
		 }   	
	}
	
	dtDiff = window.location.href;
	dtDiff = dtDiff.split("|")
	if (dtDiff.length > 9) {
		myMinLOS = daysElapsed(new Date(dtDiff[4]), new Date(dtDiff[3]))
	}






