// JavaScript Document
var RangeDates = ["11/18/2011"];
var RangeDatesIsDisable = true;
function DisableDays(date) {
    var isd = RangeDatesIsDisable;
    var rd = RangeDates;
    var m = date.getMonth();
    var d = date.getDate();
    var y = date.getFullYear();
    for (i = 0; i < rd.length; i++) {
        var ds = rd[i].split(',');
        var di, df;
        var m1, d1, y1, m2, d2, y2;

        if (ds.length == 1) {
            di = ds[0].split('/');
            m1 = parseInt(di[0]);
            d1 = parseInt(di[1]);
            y1 = parseInt(di[2]);
            if (y1 == y && m1 == (m + 1) && d1 == d) return [!isd];
        } else if (ds.length > 1) {
            di = ds[0].split('/');
            df = ds[1].split('/');
            m1 = parseInt(di[0]);
            d1 = parseInt(di[1]);
            y1 = parseInt(di[2]);
            m2 = parseInt(df[0]);
            d2 = parseInt(df[1]);
            y2 = parseInt(df[2]);

            if (y1 >= y || y <= y2) {
                if ((m + 1) >= m1 && (m + 1) <= m2) {
                    if (m1 == m2) {
                        if (d >= d1 && d <= d2) return [!isd];
                    } else if (m1 == (m + 1)) {
                        if (d >= d1) return [!isd];
                    } else if (m2 == (m + 1)) {
                        if (d <= d2) return [!isd];
                    } else return [!isd];
                }
            }
        }
    }
    return [isd];
}



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);
