// <![CDATA[ 
// An object that ties the two datePickers together 
var bookingPod = window.bookingPod = {
        // Default departure cursor date : todays date + 7
        "OUTWARD_CURSOR"    : 1,
        // Default return cursor date : departure date + 7
        "RETURN_CURSOR"     : 1, 
        // Min days between departure and return dates      
        "MIN_DAYS_BETWEEN"  : 1,                     
        // Default messages
        "MESSAGES"          : { "MD1" : "Please select a departure date", "MD2" : " Please select a return date" },
        // Maximum days ahead that a flight can be booked
        "MAX_DATE_LIMIT"    : 730,
        // Minimum days ahead that a flight can be booked
        "MIN_DATE_LIMIT"    : 0,
        createYYYYMMDDString: function(dt) {
                return dt ? dt.getFullYear() + String(dt.getMonth() + 1 < 10 ? "0" + (dt.getMonth() + 1) : dt.getMonth() + 1) + (dt.getDate() < 10 ? "0" + dt.getDate() : dt.getDate()) : "";                                                             
        },
        checkOutwardDate: function(cbObj) {              
                // Show the date 
              //  bookingPod.showEnglishDate(cbObj);                
                // An invalid or no outward date was entered
                if(!cbObj.date) {                                            
                        // Set the default cursor dates on the return element
                        var today       = new Date(),
                            defaultOut  = new Date(today.getFullYear(), today.getMonth(), today.getDate() + bookingPod.OUTWARD_CURSOR),
                            defaultRet  = new Date(defaultOut.getFullYear(), defaultOut.getMonth(), defaultOut.getDate() + bookingPod.RETURN_CURSOR),                                                                           
                            rangeHigh   = new Date(today.getFullYear(), today.getMonth(), today.getDate() + bookingPod.MAX_DATE_LIMIT),                
                            rangeLow    = new Date(today.getFullYear(), today.getMonth(), today.getDate() + bookingPod.MIN_DATE_LIMIT);                
                        // Reset the outward default ranges        
                        datePickerController.setRangeHigh("departure", bookingPod.createYYYYMMDDString(rangeHigh));
                        datePickerController.setRangeLow("departure", bookingPod.createYYYYMMDDString(rangeLow));  
                        // Set the outward cursor to be the default value : today's date + OUTWARD_CURSOR     
                        datePickerController.setCursorDate("arrival", bookingPod.createYYYYMMDDString(defaultOut)); 
                        // If no return date set then reset the cursor position to be today's date + OUTWARD_CURSOR + RETURN_CURSOR  
                        if(!datePickerController.getSelectedDate("departure")) {                                                                                                                                               
                                datePickerController.setCursorDate("departure", bookingPod.createYYYYMMDDString(defaultRet)); 
                        };                                             
                        return;
                };                                           
                // Set the rangeLow of the return to be the outward date + MIN_DAYS_BETWEEN                                               
                datePickerController.setRangeLow("departure", bookingPod.createYYYYMMDDString(new Date(cbObj.yyyy, +cbObj.mm - 1, (+cbObj.dd + bookingPod.MIN_DAYS_BETWEEN), 5, 0, 0, 0)));  
                // If no valid return date set 
                if(!datePickerController.getSelectedDate("departure")) {                         
                        // Set the return date to be departure date + RETURN_CURSOR                        
                        datePickerController.setSelectedDate("departure", bookingPod.createYYYYMMDDString(new Date(cbObj.yyyy, +cbObj.mm - 1, (+cbObj.dd + bookingPod.RETURN_CURSOR), 5, 0, 0, 0)));  
                };
        },
        checkReturnDate: function(cbObj) {
                // Set the return cursor date if none set
                if(!cbObj.date) {                          
                        var depDate     = datePickerController.getSelectedDate("arrival") || new Date();                                              
                        // Set the outward cursor to be the default value : todays date + OUTWARD_CURSOR     
                        datePickerController.setCursorDate("departure", bookingPod.createYYYYMMDDString(new Date(depDate.getFullYear(), depDate.getMonth(), depDate.getDate() + bookingPod.RETURN_CURSOR)));                   
                }; 
                // Show the date                 
                //bookingPod.showEnglishDate(cbObj);                
        }
}
// Utility function - not needed by the datepicker script but used by a few of the demos below   
function pad(value, length) { 
	length = length || 2; 
  return "0000".substr(0,length - Math.min(String(value).length, length)) + value; 
};
function maskDate(textbox,loc,delim,pattern,event)
{
	if (event.keyCode == 8 || event.keyCode == 46 || event.keyCode == 39 || event.keyCode == 37)
		return;
	strInput = textbox.value;
	for (var j=0; j<=strInput.length;j++){
		if (pattern.substring(j,j+1)=='#'){
			if (strInput.substring(j,j+1).match(/\D/,'')){
				strInput = strInput.substring(0,j)+strInput.substring(j+1,strInput.length);
			}
		}
	}	
	str=strInput;
	var locs = loc.split(',');
	for (var i = 0; i <= locs.length; i++){
		for (var k = 0; k <= str.length; k++){
			if (k == locs[i]){
				if (str.substring(k, k+1) != delim){
					str = str.substring(0,k) + delim + str.substring(k,str.length);
				}
			}
		}
	}
	textbox.value = str;
}
// ]]>
