/*
        A "Reservation Date" example using two datePickers
        --------------------------------------------------

        * Functionality
        
        1. When the page loads:
                - We clear the value of the two inputs (to clear any values cached by the browser)
                - We set "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
                
        * Caveats (aren't there always)

        - This demo has been written for dates that have NOT been split across three inputs
        
*/

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("date_effet").value = "";
        //document.getElementById("date_echeance").value = "";

        // Add the onchange event handler to the start date input
        document.getElementById("date_effet").onchange = setReservationDates;
        
        //setLowRangeToBeSixWeeksAfterToday();
}
function setLowRangeToBeSixWeeksAfterToday() {
        if(!("date_effet" in datePickerController.datePickers)) {
                setTimeout("setLowRangeToBeSixWeeksAfterToday()", 50);
                return;
        }
        
        var dt = new Date();
        dt.setDate(dt.getDate() +  (6 * 7));
        
        var stringDt = dt.getFullYear() + String(makeTwoChars(dt.getMonth())) + makeTwoChars(dt.getDate());

        datePickerController.datePickers["date_effet"].setRangeLow(stringDt);
        datePickerController.datePickers["date_echeance"].setRangeLow(stringDt);
}

function setReservationDates(e) {
        // Check the associated datePicker object is available (be safe)
        if(!("date_effet" in datePickerController.datePickers)) {
                return;
        }
        
        // Check the value of the input is a date of the correct format
        var dt = datePickerController.dateFormat(this.value, datePickerController.datePickers["date_effet"].format.charAt(0) == "m");
        
        // If the input's value cannot be parsed as a valid date then return
        if(dt == 0) return;

        // 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("date_echeance").value, datePickerController.datePickers["date_echeance"].format.charAt(0) == "m");

        // Grab the end date datePicker Objects
        var ed = datePickerController.datePickers["date_echeance"];

        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("date_echeance").value = "";
        }
}

datePickerController.addEvent(window, 'load', initialiseInputs);