// JavaScript Document

function validate_form() {

	var myDate = new Date();
	myDate.setFullYear(document.f1.CIY.value,document.f1.CIM.value-1,document.f1.CID.value);

	var today = new Date();

	if (!myDate>today) {
		alert('Please select later date.');
		return;
	}


	x = isDate(document.f1.CIY.value + ' ',document.f1.CIM.value + ' ',document.f1.CID.value + '');
	if (x==false) {
		alert("Please select a valid date.");
		return false;
	}

    // Old
	//document.f1.action = "http://www.snrhotels.com/hotels/displayhotelrates.asp";

    // New
	document.f1.action = "http://www.snrhotels.com/hotels/generic/gb/displayhotelrates.asp";

	// change target to suit your web site
	document.f1.target = "_blank";

	document.f1.submit();
	return;
}


var defaultEmptyOK = false;


function daysInFebruary (year) { // February has 29 days in any year evenly divisible by four,
	// EXCEPT for centurial years which are not also divisible by 400.
	return ( ((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0) ) ) ? 29 : 28 );
}


function makeArray(n) {
	//*** BUG: If I put this line in, I get two error messages:
	//(1) Window.length can't be set by assignment
	//(2) daysInMonth has no property indexed by 4
	//If I leave it out, the code works fine.
	// this.length = n;
	for (var i = 1; i <= n; i++) {
		this[i] = 0
	}
	return this;
}


function isEmpty(s) {
	return ((s == null) || (s.length == 0))
}

var daysInMonth = makeArray(12);
daysInMonth[1] = 31;
daysInMonth[2] = 29; // must programmatically check this
daysInMonth[3] = 31;
daysInMonth[4] = 30;
daysInMonth[5] = 31;
daysInMonth[6] = 30;
daysInMonth[7] = 31;
daysInMonth[8] = 31;
daysInMonth[9] = 30;
daysInMonth[10] = 31;
daysInMonth[11] = 30;
daysInMonth[12] = 31;


	// isDate (STRING year, STRING month, STRING day)
	//
	// isDate returns true if string arguments year, month, and day
	// form a valid date.
	//

function isDate (year, month, day) {
	// catch invalid years (not 2- or 4-digit) and invalid months and days.
	//if (! (isYear(year, false) && isMonth(month, false) && isDay(day, false)))
	//return false;

	// Explicitly change type to integer to make code work in both
	// JavaScript 1.1 and JavaScript 1.2.
	var intYear = parseInt(year);
	var intMonth = parseInt(month);
	var intDay = parseInt(day);

	// catch invalid days, except for February

	if ((intMonth == 2) && (intDay > daysInFebruary(intYear))) return false;

	if (intDay > daysInMonth[intMonth]) return false;

	return true;
}


var selected_day = new Date().getDate();


function setDaysInMonth() {

	var year	= document.f1.CIY;
	var month	= document.f1.CIM;
	var day		= document.f1.CID;

	var days = daysInMonth[month.value];

	if ( month.value == 2 ) {
		days = daysInFebruary(year.value);
	}

	while( day.options.length > 0 ) {
		day.removeChild( day.firstChild );
	}

    var j=0;
	for( var i=0 ; i<days ; i++ ) {
		j++;
    	day.options[i] = new Option(j,j);
		if( j == selected_day || ( selected_day>days && j===days ) ) {
	    	day.options[i].defaultSelected = true;
	    	day.options[i].selected = true;
		}

	}

}

function setSelectedDay( day ) {
	selected_day = parseInt(day);
}
