// JavaScript Document


 function trim(str) {
   return str.replace(/^\s*|\s*$/g,"");
}
/***************************************************************************************************************************/
//function syncCheckoutMonth() {
//	// make check-out month dropdown same as check-in month dropdown
//	var targetValue = GetDropdownValueByID("ddlCheckInMonthYear");
//	SetDropdownByID("ddlCheckOutMonthYear", targetValue ) 
//}
/***************************************************************************************************************************/
function cleanDates() {
	// ensure that selected checkin/checkout date is within the possible range for given month/year

	var sCheckInMonthYear = GetDropdownValueByID("ddlMonthYear");
	var sCheckInDate = GetDropdownValueByID("ddlDate");
//	var sCheckOutMonthYear = GetDropdownValueByID("ddlCheckOutMonthYear");
//	var sCheckOutDate = GetDropdownValueByID("ddlCheckOutDate");
	
	var dCheckInDate = new Date(Right(sCheckInMonthYear, 4), Left(sCheckInMonthYear, 2) - 1, 1);
//	var dCheckOutDate = new Date(Right(sCheckOutMonthYear, 4), Left(sCheckOutMonthYear, 2) - 1, 1);

	var MaxCheckInDays = getMonthLen(dCheckInDate) //, MaxCheckOutDays = getMonthLen(dCheckOutDate);

	if (sCheckInDate > MaxCheckInDays)  { SetDropdownByID("ddlDate", MaxCheckInDays) }

//	if (sCheckOutDate > MaxCheckOutDays)  {	SetDropdownByID("ddlCheckOutDate", MaxCheckOutDays) }

	var dToday = new Date();
	dToday = new Date( dToday.getFullYear(), dToday.getMonth(), dToday.getDate() );
	//dTomorrow = new Date( Number(dToday) +  (1000 * 60 * 60 * 24) );

	dCheckInDate = new Date(Right(sCheckInMonthYear, 4), Left(sCheckInMonthYear, 2) - 1, sCheckInDate);
//	dCheckOutDate = new Date(Right(sCheckOutMonthYear, 4), Left(sCheckOutMonthYear, 2) - 1, sCheckOutDate);

	if (dCheckInDate < dToday) { 
		// then set checkin date to today 
		SetDateDDLs('ddlMonthYear', 'ddlDate', dToday);
	}
//	if (dCheckOutDate < dTomorrow) { 
		// then set checkin date to tomorrow
//		SetDateDDLs('ddlCheckOutMonthYear', 'ddlCheckOutDate', dTomorrow);
//	}
	
}
/***************************************************************************************************************************/
function getMonthLen(theDate) {
	// returns number of days in the month of the date (theDate)
	// heavily modified from http://www.dannyg.com/support/update8.html

	var oneHour = 1000 * 60 * 60, oneDay = oneHour * 24;

	var thisMonth = new Date(theDate.getFullYear(), theDate.getMonth(), 27); // Jan 27 2007
	var sMonth = theDate.getMonth();
    var curDay = new Date(Number(thisMonth) + (oneDay * 7)); // add a week, putting us sometime in the following month
	
	// backtrack until the month has changed (back to the original month)
	while (sMonth != curDay.getMonth()) {
//		alert(curDay.getMonth() + '...' + curDay.getFullYear() );
    	curDay = new Date(Number(curDay) - oneDay) ; // go back a day
	}
			
    return curDay.getDate();
}
/***************************************************************************************************************************/
function Left(str, n){
	if (n <= 0)
	    return "";
	else if (n > String(str).length)
	    return str;
	else
	    return String(str).substring(0,n);
}
/***************************************************************************************************************************/
function Right(str, n){
    if (n <= 0)
       return "";
    else if (n > String(str).length)
       return str;
    else {
       var iLen = String(str).length;
       return String(str).substring(iLen, iLen - n);
    }
}
/***************************************************************************************************************************/