//---------------------------------------------------------------------//
//	Javascript code to display the form, validate and submit.
//---------------------------------------------------------------------//


// Reservation - popup window
//-------------------------------------

function validateData() 
{
	var err = "";
	var day = new Date();
	var id = day.getTime();

	// Prompt user if date range exceeds 'rangePrompt' number of days.
	var rangePrompt = 20; // 20 days

	// Do no allow if date range exceed 'rangeMax' number of days.
	var rangeMax = 60;	// 60 days

	// reformat the current date for error checking.
	var this_year = day.getFullYear();
	var this_month = day.getMonth() + 1;
	if (this_month < 10) this_month = "0"+this_month;
	var this_date = day.getDate();
	if (this_date < 10) this_date = "0"+this_date;
	var current = this_year+""+this_month+""+this_date;

	var URL = "http://www.skylineinnniagarafalls.com/single.php";
	var property_vals = document.frmCS.fProperty.value;

	// get the id, name, country, prov, city from the property_vals. 
	prop = property_vals.split("|");
	var prop_id = prop[0];
	var prop_name = prop[1];
	var prop_country = prop[2];
	var prop_prov = prop[3];
	var prop_city = prop[4];

	var checkin = document.frmCS.fCheckin.value;
	var checkout = document.frmCS.fCheckout.value;
	var adults = document.frmCS.fAdults.value;
	var children = document.frmCS.fChildren.value;
	var aid = document.frmCS.fAssignedid.value;
	var smoke = document.frmCS.fSmoking.value;
	var rooms = document.frmCS.fRoomsearch.checked;
	var pkg = document.frmCS.fPackagesearch.checked;

	// reformat the check-in date for error checking.
	var ci = new Date(checkin);
	var ci_year = ci.getFullYear();
	var ci_month = ci.getMonth() + 1;
	if (ci_month < 10) ci_month = "0"+ci_month;
	var ci_date = ci.getDate();
	if (ci_date < 10) ci_date = "0"+ci_date;
	var ci_current = ci_year+""+ci_month+""+ci_date;
	var ci_time = ci.getTime();

	// reformat the check-out date for error checking.
	var co = new Date(checkout);
	var co_year = co.getFullYear();
	var co_month = co.getMonth() + 1;
	if (co_month < 10) co_month = "0"+co_month;
	var co_date = co.getDate();
	if (co_date < 10) co_date = "0"+co_date;
	var co_current = co_year+""+co_month+""+co_date;
	var co_time = co.getTime();

	if (prop_id == "null") err += "Please select a property to search.\n";
	if (checkin == "" || checkout == "") {
		if (checkin == "") err += "Check-in date is required.\n";
		if (checkout == "") err += "Check-out date is required.\n";
	} else {
		if (ci_current < current) err += "Check-in date may not occur before today.\n";
		if (co_current <= ci_current) err += "Check-out date must be after check-in date.\n";
	}

//	if (rooms == false && pkg == false) {
//		err += "Either Packages, Rooms or both should be selected.\n";
//	}
	
	if (err.length > 1) {
		eval(alert(err));
	} else {

		var timeDiff = co_time - ci_time;

		// check the date range.
		if (timeDiff > (rangeMax * 86400000)) 
		{
			alert("The maximum date range you can search is " + rangeMax + " days.");
		} else if (timeDiff > (rangePrompt * 86400000) & timeDiff < (rangeMax * 86400000)) {
			var conf = confirm("You selected a date range greater than " + rangePrompt + " days. Do you wish to proceed?");

			if (conf) {
				// Submit the form.
				document.frmCS.submit();
			}
		} else {
			// Submit the form.
			document.frmCS.submit();
		}
	}
}


//---------------------------------------------------------------------//
//	Javascript Calendar Code.
//---------------------------------------------------------------------//

var calendar = null;

// Update the date in the input field.
//-------------------------------------

function selected(cal, date) {
	cal.sel.value = date; 
}

// Hide the calendar.
//-------------------------------------

function closeHandler(cal) {
	cal.hide();                        
	removeEvent(document, "mousedown", checkCalendar);
}

// Close calendar.
//-------------------------------------

function checkCalendar(ev) {
	var el = is_ie ? getElement(ev) : getTargetElement(ev);
	for (; el != null; el = el.parentNode)
    if (el == calendar.element || el.tagName == "A") break;
	if (el == null) {
		calendar.callCloseHandler();
		stopEvent(ev);
	}
}

// Display calendar.
//-------------------------------------

function showCalendar(n) {
	if (n == 1) var id = "fCheckin";
	if (n == 2) var id = "fCheckout";
	if (n == 3) var id = "ContactEventdate";
	if (n == 4) var id = "ContactAltEventdate";
	// Do Not Change Date Format - or movie will not be able to process it.
	var format = "M d, y";
	var el = document.getElementById(id);
	if (calendar != null) {
		calendar.hide();                
	} else {
		// first-time call, create the calendar.
		var cal = new Calendar(true, null, selected, closeHandler);
		calendar = cal;                  // remember it in the global var
		cal.setRange(2004, 2070);        // min/max year allowed.
	}
	calendar.setDateFormat(format);    // set the specified date format
	calendar.parseDate(el.value);      // try to parse the text in field
	calendar.sel = el;                 // inform it what input field we use
	calendar.showAtElement(el);        // show the calendar below it

	// catch "mousedown" on document
	addEvent(document, "mousedown", checkCalendar);
	return false;
}
