/*yesterday: 2009/10/20*/
/*4 days from now: 2009/10/25*/
/*6 days ago: 2009/10/15*/
/*341 days from now: 2010/9/27*/

var booking_min_date = new Date(2009, 10, 20);
var booking_max_date = new Date(2010, 9, 27);
function calConfig(itdUniqueId, cal, minDate, maxDate) {
cal.cfg.setProperty("close", true);
cal.cfg.setProperty("iframe", true); 
cal.cfg.setProperty("START_WEEKDAY", "0");
cal.itdIdLink = itdUniqueId;
cal.cfg.setProperty("DATE_FIELD_DELIMITER", "/");
cal.cfg.setProperty("LOCALE_MONTHS", "long");
cal.cfg.setProperty("MONTHS_LONG", ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]);
cal.cfg.setProperty("LOCALE_WEEKDAYS", "short");
cal.cfg.setProperty("WEEKDAYS_SHORT", ["Su", "Mo", "Tu", "We", "Th", "Fr", "Sa"]);
var itd_cal = cal;
YAHOO.util.Event.addListener("wdfdate"+itdUniqueId, "focus", calShow, itd_cal, true);
YAHOO.util.Event.addListener("calLink"+itdUniqueId, "click", calShow, itd_cal, true);
itd_cal.selectEvent.subscribe(handleSelect, itd_cal, true);
if (itdUniqueId!=2) {
cal.cfg.setProperty("title", "Departure Date");
} else {
cal.cfg.setProperty("title", "Return Date");
}
cal.cfg.setProperty("mindate", minDate)
cal.cfg.setProperty("maxdate", maxDate)
}
var returnDaysAhead = 7;

var itd_cal1 = new YAHOO.widget.Calendar("itd_cal1", "calContainer1");
calConfig("1", itd_cal1, new Date(2009, 10, 20), new Date(2010, 9, 27));

var defTime=null;
function updateTimeState() {
	
 if (defTime==null){
  // this block of code stores off the default values of any timefields present on the page, 
  //  this is only done once for a page, and only if the user changes a date field (now amended to also call this if they change a time field)
  defTime=new Array("");
  // Caution!
  // remember that the id of the first time field on the page is >> "wdftime1"
  // Starting with an id of zero, >> var id=0;
  // preIncrement to 1, then check if field exists >> getField('wdftime',++id)!=null
  // or >> ||
  // again preIncrement, so value goes to 2, then check if field exists >> getField('wdftime',++id)!=null
  // loop continues as long as condition evaluates to true.
  // the double condition means time fields 1,2,4 can be saved off.. so if one number is missing from the sequence it will still work. I'm not sure why this is required.
  //  It may be used to break up field pairs, so one does not advance the next field. A missing digit could be used as a pair separator.
  for (var id=0; getField('wdftime',++id)!=null || getField('wdftime',++id)!=null; ) {
   defTime[id]=getField('wdftime',id).value;
  }
 }
 
 for (var id=0; getField('wdftime',++id)!=null || getField('wdftime',++id)!=null; ) {
  var select=getField('wdftime',id);
 
  // this returns the contents of the specified DATE field in Y,M,D format.. it is not concerned with the time field.
  // it validates crudely that the month is 1-12, and days are 1-31, if a value is invalid it is set to zero.
  var array=getValidatedDateArray(id);
 
  // this checks if a zero was returned for any of the Y,M,D values..
  // an empty date field is considered invalid
  if (!isValidDate(array)){
    // set to default value which we stored earlier
    setTimeField(select,defTime[id]);
  }
  
 }
}
 
function setTimeField(fld,val) {
 for (var i=0; i<fld.options.length; i++) {
  if (fld.options[i].value==val) {
   fld.selectedIndex=i;
   break;
  }
 }
}
 
function timeChanged() {
 
}

var itd_cal2 = new YAHOO.widget.Calendar("itd_cal2", "calContainer2");
calConfig("2", itd_cal2, new Date(2009, 10, 20), new Date(2010, 9, 27));

var itd_cal3 = new YAHOO.widget.Calendar("itd_cal3", "calContainer3");
calConfig("3", itd_cal3, new Date(2009, 10, 15), new Date(2009, 10, 25));

var itd_cal5 = new YAHOO.widget.Calendar("itd_cal5", "calContainer5");
calConfig("5", itd_cal5, new Date(2009, 10, 20), new Date(2010, 9, 27));