	//<![CDATA[

	/*
        * Functionality

        1. When the page loads:
                - We clear the value of the two inputs (to clear any values cached by the browser)
                - We set an "onchange" event handler on the startDate input to call the setReservationDates function
        2. When a start date is selected
                - We set the low range of the endDate datePicker to be the start date the user has just selected
                - If the endDate input already has a date stipulated and the date falls before the new start date then we clear the input's value
	*/

	function makeTwoChars(inp) {
        return String(inp).length < 2 ? "0" + inp : inp;
	}

	function initialiseInputs() {
        // Clear any old values from the inputs (that might be cached by the browser after a page reload)
        document.getElementById("sd").value = "";
        document.getElementById("ed").value = "";

        // Add the onchange event handler to the start date input
        datePickerController.addEvent(document.getElementById("sd"), "change", setReservationDates);
	}

	var initAttempts = 0;

	function setReservationDates(e) {
        // Internet Explorer will not have created the datePickers yet so we poll the datePickerController Object using a setTimeout
        // until they become available (a maximum of ten times in case something has gone horribly wrong)

        try {
                var sd = datePickerController.getDatePicker("sd");
                var ed = datePickerController.getDatePicker("ed");
        } catch (err) {
                if(initAttempts++ < 10) setTimeout("setReservationDates()", 50);
                return;
        }

        // Check the value of the input is a date of the correct format
        var dt = datePickerController.dateFormat(this.value, sd.format.charAt(0) == "m");

        // If the input's value cannot be parsed as a valid date then return
        if(dt == 0) return;

        // At this stage we have a valid YYYYMMDD date

        // Grab the value set within the endDate input and parse it using the dateFormat method
        // N.B: The second parameter to the dateFormat function, if TRUE, tells the function to favour the m-d-y date format
        var edv = datePickerController.dateFormat(document.getElementById("ed").value, ed.format.charAt(0) == "m");

        // Set the low range of the second datePicker to be the date parsed from the first
        ed.setRangeLow( dt );
        
        // If theres a value already present within the end date input and it's smaller than the start date
        // then clear the end date value
        if(edv < dt) {
                document.getElementById("ed").value = "";
        }
	}

	function removeInputEvents() {
        // Remove the onchange event handler set within the function initialiseInputs
        datePickerController.removeEvent(document.getElementById("sd"), "change", setReservationDates);
	}

	datePickerController.addEvent(window, 'load', initialiseInputs);
	datePickerController.addEvent(window, 'unload', removeInputEvents);
	function searchRes(search){
		var resnumber = search.reservation.value; var resname = search.surname.value;
		var newError = "";
		if (resnumber==""){
			newError = "You did not specify your reservation number. ";
		}
		if (resname==""){
			newError+= "You did not enter your surname. ";
		}
		if (newError!=""){
			alert("Errors were detected with your submission. "+newError+"The search will not be run at this time, please try again.");
		}else{
			var searchthis = "http://www.choicehotelsuk.co.uk/reservation/"+resnumber+"/"+resname;
			window.open(searchthis,"reservations","toolbar=no,width=800,height=600,menubar=no,scrollbars=yes,resizable=yes,alwaysRaised=yes"); 
		}
	}
	function sendBooking(myform){
		var checkin = myform.sd.value; var checkout = myform.ed.value;
		var error = "";
		if(!isValidDate(checkin)){
			error = "Please enter your desired check-in date in the format DD/MM/YYYY. ";
		}
		if(!isValidDate(checkout)){
			error += "Please enter your desired check-out date in the format DD/MM/YYYY.";
		}
		checkin = checkin.replace(/\//g, '-');
		checkout = checkout.replace(/\//g, '-');
		checkin = checkin.replace(/\./g, '-');
		checkout = checkout.replace(/\./g, '-');
		if (error!=""){
			alert (error);
			return false;
		}else{
			var booking = "http://www.choicehotelsuk.co.uk/rooms/GB176?srp=RACK&checkin="+checkin+"&checkout="+checkout;
			window.open(booking,"reservations","toolbar=no,menubar=no,scrollbars=yes,resizable=yes,alwaysRaised=yes");
		}
	}
	 function isValidDate(date) {
		var pattern = new RegExp(/(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\d\d/);
		return pattern.test(date);
	}

	//]]>