function makeTwoChars(inp) {
	return String(inp).length < 2 ? "0" + inp : inp;
}

function initialiseDatePickers() {
       // Attempt to grab the datePicker objects
       var sd = datePickerController.datePickers["dcc_calendar_Q_FROM_DATE"];
       var ed = datePickerController.datePickers["dcc_calendar_Q_TO_DATE"];
       	        
       
       // For Internet Explorer: If they are not created then call this function 500 milliseconds later
       if(!sd || !ed) {
               setTimeout("initialiseDatePickers()", 200);
               return;
       }
       // Add the onchange event handler to the start date input
       //document.getElementById("dcc_calendar_Q_FROM_DATE").onchange = setReservationDatesDeb;
       datePickerController.addEvent(document.getElementById("dcc_calendar_Q_FROM_DATE"), 'change', setReservationDatesDeb);
       datePickerController.addEvent(document.getElementById("dcc_calendar_Q_TO_DATE"), 'change', setReservationDatesFin);
       
       //document.getElementById("dcc_calendar_Q_TO_DATE").onchange = setReservationDatesFin;
}

function setReservationDatesDeb(e) {
      	// Check the associated datePicker object is available (be safe)
       if(!("dcc_calendar_Q_FROM_DATE" 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["dcc_calendar_Q_FROM_DATE"].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("Q_TO_DATE").value, datePickerController.datePickers["dcc_calendar_Q_TO_DATE"].format.charAt(0) == "m");

       // Grab the end date datePicker Objects
       var ed = datePickerController.datePickers["dcc_calendar_Q_TO_DATE"];

       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("dcc_calendar_Q_TO_DATE").value = "";
         document.getElementById("Q_TO_DATE").value = "";
       }
}

function setReservationDatesFin(e) {
       // Check the associated datePicker object is available (be safe)
       if(!("dcc_calendar_Q_TO_DATE" 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["dcc_calendar_Q_TO_DATE"].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("Q_FROM_DATE").value, datePickerController.datePickers["dcc_calendar_Q_FROM_DATE"].format.charAt(0) == "m");

       // Grab the end date datePicker Objects
       var ed = datePickerController.datePickers["dcc_calendar_Q_FROM_DATE"];

       ed.setRangeHigh( 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("dcc_calendar_Q_FROM_DATE").value = "";
       		document.getElementById("Q_FROM_DATE").value = "";
       }
}

function consistency_date(chaine){	
	if (chaine == ""){
		return true;
	}	
	var reg=new RegExp("^[0-9]{2}[/]{1}[0-9]{2}[/]{1}[0-9]{4}$","g");
	return reg.test(chaine);
}

datePickerController.addEvent(window, 'load', initialiseDatePickers);
