function checkValidDate(dateStr) {
    // dateStr must be of format month day year with either slashes
    // or dashes separating the parts. Some minor changes would have
    // to be made to use day month year or another format.
    // This function returns True if the date is valid.
    var slash1 = dateStr.indexOf("/");
    if (slash1 == -1) { slash1 = dateStr.indexOf("-"); }
    // if no slashes or dashes, invalid date
    if (slash1 == -1) { return false; }
    var dateMonth = dateStr.substring(0, slash1)
    var dateMonthAndYear = dateStr.substring(slash1+1, dateStr.length);
    var slash2 = dateMonthAndYear.indexOf("/");
    if (slash2 == -1) { slash2 = dateMonthAndYear.indexOf("-"); }
    // if not a second slash or dash, invalid date
    if (slash2 == -1) { return false; }
    var dateDay = dateMonthAndYear.substring(0, slash2);
    var dateYear = dateMonthAndYear.substring(slash2+1, dateMonthAndYear.length);
    if ( (dateMonth == "") || (dateDay == "") || (dateYear == "") ) { return false; }
    // if any non-digits in the month, invalid date
    for (var x=0; x < dateMonth.length; x++) {
        var digit = dateMonth.substring(x, x+1);
        if ((digit < "0") || (digit > "9")) { return false; }
    }
    // convert the text month to a number
    var numMonth = 0;
    for (var x=0; x < dateMonth.length; x++) {
        digit = dateMonth.substring(x, x+1);
        numMonth *= 10;
        numMonth += parseInt(digit);
    }
    if ((numMonth <= 0) || (numMonth > 12)) { return false; }
    // if any non-digits in the day, invalid date
    for (var x=0; x < dateDay.length; x++) {
        digit = dateDay.substring(x, x+1);
        if ((digit < "0") || (digit > "9")) { return false; }
    }
    // convert the text day to a number
    var numDay = 0;
    for (var x=0; x < dateDay.length; x++) {
        digit = dateDay.substring(x, x+1);
        numDay *= 10;
        numDay += parseInt(digit);
    }
    if ((numDay <= 0) || (numDay > 31)) { return false; }
    // February can't be greater than 29 (leap year calculation comes later)
    if ((numMonth == 2) && (numDay > 29)) { return false; }
    // check for months with only 30 days
    if ((numMonth == 4) || (numMonth == 6) || (numMonth == 9) || (numMonth == 11)) { 
        if (numDay > 30) { return false; } 
    }
    // if any non-digits in the year, invalid date
    for (var x=0; x < dateYear.length; x++) {
        digit = dateYear.substring(x, x+1);
        if ((digit < "0") || (digit > "9")) { return false; }
    }
    // convert the text year to a number
    var numYear = 0;
    for (var x=0; x < dateYear.length; x++) {
        digit = dateYear.substring(x, x+1);
        numYear *= 10;
        numYear += parseInt(digit);
    }
    // Year must be a 2-digit year or a 4-digit year
    if ( (dateYear.length != 2) && (dateYear.length != 4) ) { return false; }
    // if 2-digit year, use 50 as a pivot date
    if ( (numYear < 50) && (dateYear.length == 2) ) { numYear += 2000; }
    if ( (numYear < 100) && (dateYear.length == 2) ) { numYear += 1900; }
    if ((numYear <= 0) || (numYear > 9999)) { return false; }
    // check for leap year if the month and day is Feb 29
    if ((numMonth == 2) && (numDay == 29)) {
        var div4 = numYear % 4;
        var div100 = numYear % 100;
        var div400 = numYear % 400;
        // if not divisible by 4, then not a leap year so Feb 29 is invalid
        if (div4 != 0) { return false; }
        // at this point, year is divisible by 4. So if year is divisible by
        // 100 and not 400, then it's not a leap year so Feb 29 is invalid
        if ((div100 == 0) && (div400 != 0)) { return false; }
    }
    // date is valid
    return true;
}

function MM_validateForm() { //v4.0
	var strMonth = parseInt(document.Reservation_Form.month.value);
	var strDay = parseInt(document.Reservation_Form.day.value);
	var strYear = parseInt(document.Reservation_Form.year.value);
	var ReservationDate1 = strMonth+"/"+strDay+"/"+strYear;	
	var ReservationDate = new Date(Date.parse(ReservationDate1));
	var strWeekDay = ReservationDate.getDay();
	var errors='';
	//alert(strMonth);
	//alert(strDay);
	//alert(strYear);
	//alert(ReservationDate1);
	//alert(ReservationDate);
	//alert(strWeekDay);
   if (strWeekDay == 0)
  {
    errors=errors+"We are not open on Sunday.\n";
  }
   if (!checkValidDate(ReservationDate1)) {
      errors=errors+"The date you supplied was invalid. Please try again.\n";
   }
  var i,p,q,nm,test,num,min,max,args=MM_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=val.name; if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
      } else if (test!='R') { num = parseFloat(val);
        if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
  } if (errors) alert('The following error(s) occurred:\n'+errors);
  document.MM_returnValue = (errors == '');
}
var localForm; 
function initForm(theForm) 
{ 
   localForm = theForm;
}

// Checks if browser is Netscape 2.0x since the options array properties don't work with Netscape 2.0x
function isBrowserSupp() {

    // Get the version of the browser
    version =  parseFloat( navigator.appVersion );

    if ( ( version >= 2.0 ) && ( version < 2.1 ) && ( navigator.appName.indexOf( "Netscape" ) != -1 ) ) {
        return false;
    }
    else {
	return true;
    }
}


function isLeapYear(yrStr)
{
var leapYear=false;
var year = parseInt(yrStr, 10);
// every fourth year is a leap year
if (year%4 == 0)
	{
	leapYear=true;
	// unless it's a multiple of 100
	if (year%100 == 0)
		{
		leapYear=false;
		// unless it's a multiple of 400
		if (year%400 == 0)
			{
			leapYear=true;
			}
		}
	}
return leapYear;
}


function getDaysInMonth(mthIdx, YrStr)
{
// all the rest have 31
var maxDays=31
// expect Feb. (of course)
if (mthIdx==1) 
	{
	if (isLeapYear(YrStr))
		{
		maxDays=29;
		}
	else 
		{
		maxDays=28;
		}
	}
// thirty days hath...
if (mthIdx==3 || mthIdx==5 || mthIdx==8 || mthIdx==10)
	{
	maxDays=30;
	}
return maxDays;
}


//the function which does some magic to the date fields
// return non-zero if it is the last day of the month
function adjustDate(mthIdx, Dt, Yr) 
{
var value=0; 		
var numDays=getDaysInMonth(mthIdx, Yr.options[Yr.options.selectedIndex].text);

if (mthIdx==1) 
	{
	if (Dt.options.selectedIndex + 1 < numDays)
		{
		return 0;
		}
	else 
		{
		Dt.options.selectedIndex=numDays - 1;
		//check for leap year
		if (numDays==29)
			{
			return 99;
			}
		else 
			{
			return 1;
			}
		}
	}
if (Dt.options.selectedIndex + 1 < numDays)
	{
	value=0;
	}
else 
	{
	if (Dt.options.selectedIndex + 1 > numDays)
		{
		Dt.options.selectedIndex--;
		value=3;
		}
	else 
		{
		//index is 31 or 30
		value=2;
		}
	}
return value;
}

// Get the index of the corresponding option in the field's list
function getIndex (val, field) {
	var i;
	for (i = 0; i < field.length; i++) {
		if (val == field.options[i].text) {
			return i;
		}
	}
	return -1;
}


//changes departure month when arrival month is changed
function inMonthChange( inM, outM, inD, outD, inY, outY ) {

	if ( !isBrowserSupp() ) {

		return;
	}

	var res = adjustDate(inM.options.selectedIndex, inD,inY);

	if ( res != 0 ) {
		outD.options.selectedIndex=0;
		outM.options.selectedIndex = inM.options.selectedIndex + 1;
	} else {
		outM.options.selectedIndex = inM.options.selectedIndex;
		outD.options.selectedIndex = inD.options.selectedIndex + 1;
	}

	// Set the out year to the same as the in year
	outY.options.selectedIndex = inY.options.selectedIndex;

	var currentMonth = ( new Date() ).getMonth();
    
	// If the in date is 12/31/?? set the out date to 01/01/??
	if ( ( inM.options.selectedIndex == 11 ) && ( inD.options.selectedIndex == 30 ) ) {
		outM.options.selectedIndex=0;
		outY.options.selectedIndex++;
	}

	// If the selected month is before the current month, increment the in year.
	if ( ( inM.options.selectedIndex < currentMonth ) && (inY.options.selectedIndex == 0 ) ) {

		// First make sure it is a valid selection
		if ( inY.selectedIndex < ( inY.options.length - 1 ) )
			inY.options.selectedIndex++;

		// Now set the out year to the same as the in year
		outY.options.selectedIndex = inY.options.selectedIndex;		
	}

	return;
}
	

//changes departure day when arrival day is changed
function inDayChange(inD, outD, inM, outM, inY, outY) 
{
if (!isBrowserSupp())
	{
 	return;
	}			
var Inmth = inM.options.selectedIndex;

var res =adjustDate(Inmth, inD, inY)
if (res != 0)
	{
	outD.options.selectedIndex=0;
	outM.options.selectedIndex=inM.options.selectedIndex + 1;
	}
else
	{
	outM.options.selectedIndex = inM.options.selectedIndex;
	outD.options.selectedIndex = inD.options.selectedIndex+1;
	}
outY.options.selectedIndex = inY.options.selectedIndex;
if ((inM.options.selectedIndex == 11) && (inD.options.selectedIndex == 30))
	{
	outM.options.selectedIndex=0;
	outY.options.selectedIndex++;
	}
return;
}
	

//changes departure year when arrival year is changed
function inYearChange(inY, outY, inM, outM, inD, outD) 
{
if (!isBrowserSupp()) 
	{
	return;			
	}

outY.options.selectedIndex = inY.options.selectedIndex;
adjustDate(inM.options.selectedIndex, inD,inY);
return;
}	

function closeCalendar(io)
{
if (io=='CheckIn')
{
var arrMonthField = document.frmRoomInventory.elements["CheckIn-month"];
var arrDayField   = document.frmRoomInventory.elements["CheckIn-day"];
var arrYearField  = document.frmRoomInventory.elements["CheckIn-year"];
var depMonthField = document.frmRoomInventory.elements["CheckOut-month"];
var depDayField   = document.frmRoomInventory.elements["CheckOut-day"];
var depYearField  = document.frmRoomInventory.elements["CheckOut-year"];

var res = adjustDate(arrMonthField, arrDayField, arrYearField); 
if (res != 0)
	{
	depDayField.options.selectedIndex=0;
	depMonthField.options.selectedIndex = arrMonthField.options.selectedIndex+1;
	}
else 
	{
	depMonthField.options.selectedIndex = arrMonthField.options.selectedIndex;
	depDayField.options.selectedIndex = arrDayField.options.selectedIndex+1;
	}
depYearField.options.selectedIndex = arrYearField.options.selectedIndex;
if ((arrMonthField.options.selectedIndex == 11) && (arrDayField.options.selectedIndex == 30))
	{
	depMonthField.options.selectedIndex=0;
	depYearField.options.selectedIndex++;
	}
}
return;
}

function Validate(theForm) {
	var lngInDay = parseInt(theForm.elements["CheckIn-day"].value);
	var lngInMonth = parseInt(theForm.elements["CheckIn-month"].value)+1;
	var lngInYear = parseInt(theForm.elements["CheckIn-year"].value);
	var lngOutDay = parseInt(theForm.elements["CheckOut-day"].value);
	var lngOutMonth = parseInt(theForm.elements["CheckOut-month"].value)+1;
	var lngOutYear = parseInt(theForm.elements["CheckOut-year"].value);
	var blnDateValid = false;
	var blnDateValid1 = false;
	var blnDateValid2 = false;
	var objDate = new Date();
	var lngTodayMonth = parseInt(objDate.getMonth());
	var lngTodayYear = parseInt(objDate.getFullYear());
	var lngTodayDay = parseInt(objDate.getDate());
	var checkInDate = lngInMonth + "/" + lngInDay + "/" + lngInYear
	var checkOutDate = lngOutMonth + "/" + lngOutDay + "/" + lngOutYear
	//alert(checkOutDate);
   	if (!checkValidDate(checkInDate)) 
	{
      		alert("The check in date you supplied was invalid. Please try again.");
 			theForm.elements["CheckIn-day"].focus();
    		return (false);
	}
   	if (!checkValidDate(checkOutDate)) 
	{
      		alert("The check out date you supplied was invalid. Please try again.");
 			theForm.elements["CheckOut-day"].focus();
    		return (false);
	}

	if (lngTodayYear < lngInYear )
	{
		blnDateValid1 = true;
	}
	else if (lngTodayMonth < lngInMonth && lngTodayYear == lngInYear )
	{
		blnDateValid1 = true;
	}
		
	else if (lngTodayDay <= lngInDay && lngTodayMonth == lngInMonth && lngTodayYear <= lngInYear)
	{
		blnDateValid1 = true;
	}

	if(!blnDateValid1 )

  	{
    		alert("The check in date can not occur before today.");
    		theForm.elements['CheckIn-month'].focus();
    		return (false);
  	}

	if (lngTodayYear < lngOutYear )
	{
		blnDateValid2 = true;
	}
	else if (lngTodayMonth < lngOutMonth && lngTodayYear == lngOutYear )
	{
		blnDateValid2 = true;
	}
		
	else if (lngTodayDay <= lngOutDay && lngTodayMonth == lngOutMonth && lngTodayYear <= lngOutYear)
	{
		blnDateValid2 = true;
	}

	if(!blnDateValid2 )

  	{
    		alert("The check out date can not occur before today.");
    		theForm.elements['CheckOut-month'].focus();
    		return (false);
  	}

	if (lngInYear < lngOutYear )
	{
		blnDateValid = true;
	}
	else if (lngInMonth < lngOutMonth && lngInYear == lngOutYear )
	{
		blnDateValid = true;
	}
		
	else if (lngInDay <= lngOutDay && lngInMonth == lngOutMonth && lngInYear <= lngOutYear)
	{
		blnDateValid = true;
	}

	if(!blnDateValid )

  	{
    		alert("The check out date can not occur before the check in date.");
    		theForm.elements['CheckIn-month'].focus();
    		return (false);
  	}
  	
		return (true);
}