

function CalendarPair(cal0Base, cal1Base, hidden0, hidden1,minDate,maxDate)
  {
  this.today = new Date();

  this.disabledTxt = "---------------------------------------";
  //this.maxDaysOut = 210;
  this.maxDaysOut = 330;

  var minDate = minDate;
  var maxDate = maxDate;

  var pageDate = (this.today.getMonth() + 1) + "/" + this.today.getFullYear();
  //var minDate = (this.today.getMonth() + 1) + "/" + this.today.getDate() + "/" + this.today.getFullYear();
  //var minDate = (this.today.getMonth() + 1) + "/28/" + this.today.getFullYear();
  
  if(minDate == "") {
    minDate = (this.today.getMonth() + 1) + "/" + this.today.getDate() + "/" + this.today.getFullYear();
  }

  if(maxDate == "") {
    var terminalDate = new Date(this.today.valueOf() + (this.maxDaysOut*24*60*60*1000));
    var maxDate = (terminalDate.getMonth() + 1) + "/" + terminalDate.getDate() + "/" + terminalDate.getFullYear();

  }

 

  this.cal0 = new YAHOO.widget.Calendar(cal0Base, 
                                        cal0Base+"Container",
                                        {pagedate: pageDate, mindate: minDate, maxdate : maxDate});

  this.cal0Base = cal0Base;
  this.cal0.renderCellNotThisMonth = this.renderNotThisMonth;
  this.cal0.renderFooter = this.buildFooter;

	this.cal0.changePageEvent.subscribe(this.changePage0, this);
	this.cal0.selectEvent.subscribe(this.dateSelect0, this);
  this.customize(this.cal0);
  this.cal0.render();

  // the hidden variable that 
  this.cal0Name = hidden0; 
  this.cal0Hidden = $(hidden0);

  this.cal0DefaultText = $(this.cal0Base + "Input").innerHTML;

  YAHOO.util.Event.addListener($(this.cal0Base + "Trigger"), 
                               "click", 
                               this.showCalendar0, 
                               this);

  
  this.cal1 = new YAHOO.widget.Calendar(cal1Base, cal1Base+"Container",
                                        {pagedate: pageDate, mindate: minDate, maxdate : maxDate});
  
  this.cal1Base = cal1Base;
  this.cal1.renderCellNotThisMonth = this.renderNotThisMonth;
  this.cal1.renderFooter = this.buildFooter;
	this.cal1.changePageEvent.subscribe(this.changePage1, this);
	this.cal1.selectEvent.subscribe(this.dateSelect1, this);
  this.customize(this.cal1);
  this.cal1.render();

  // skylights compatible variables
  this.cal1Name = hidden1; 
  this.cal1Hidden = $(hidden1);

  this.cal1DefaultText = $(cal1Base + "Input").innerHTML;

  YAHOO.util.Event.addListener($(cal1Base + "Trigger"), 
                               "click", 
                               this.showCalendar1, 
                               this);

  YAHOO.util.Event.addListener(document.body, "click", this.offClick, this);
    return;


  }

CalendarPair.prototype.renderNotThisMonth = function(workingDate, cell) {
  cell.innerHTML = "";
  return YAHOO.widget.Calendar_Core.STOP_RENDER;
  }

CalendarPair.prototype.customize = function(calendar) {
  calendar.cfg.setProperty("MDY_DAY_POSITION", 1); 

  // local
  calendar.cfg.setProperty("WEEKDAYS_SHORT", ["S", "M", "T", "W", "T", "F", "S"]);

  // styles
  calendar.cfg.setProperty("CSS_ROW_HEADER", "calendarHeader");
  calendar.cfg.setProperty("CSS_HEADER", "calendarHeader");
  calendar.cfg.setProperty("CSS_HEADER_TEXT", "calendarHeaderText");
  calendar.cfg.setProperty("CSS_WEEKDAY_ROW", "calendarWeekRow");
  calendar.cfg.setProperty("CSS_CALENDAR", "calendar");
  calendar.cfg.setProperty("CSS_CELL_SELECTED", "cellSelected");

  // options
  calendar.cfg.setProperty("LOCALE_MONTHS", calendar.cfg.MONTHS_SHORT);
}

CalendarPair.prototype.buildFooter = function(html) {
  var obj = this;
  var cid = obj.oDomContainer.id;
  var wrap = document.createElement("div");
  var foot = document.createElement("TFOOT");
  var row = document.createElement("TR");
  var cell = document.createElement("TD"); cell.colSpan = 7;
  var link = document.createElement("a");
  var img = document.createElement("img");

  img.src = "/i/closePop.gif";
  link.href = "javascript:void(null)";
  link.id = cid+"closer";

  // hack - if we use the yahoo event, the scope gets wiped out by something 
  // 
  link.setAttribute("onclick", "_hide('"+cid+"')");
  link.appendChild(img);
  cell.appendChild(link); 
  row.appendChild(cell); 
  foot.appendChild(row);
  wrap.appendChild(foot);
  html.push(wrap.innerHTML);
  return html;
  }

function _hide(cid) {
  $(cid).style.display = "none";
}


CalendarPair.prototype.showCalendar0 = function(e, obj) {
  var correctDate;

  if(obj.cal0.getSelectedDates().length == 0 && obj.cal1.getSelectedDates().length !=0) 
    correctDate = obj.cal1.cfg.getProperty("pageDate");
  else if(obj.cal0.getSelectedDates().length !=0) 
    correctDate = obj.cal0.getSelectedDates()[0];
  else 
    correctDate = obj.today;

  obj.cal0.setMonth(correctDate.getMonth()); 
  obj.cal0.setYear(correctDate.getFullYear());

  var diff = Date.parse(correctDate.toString()) - Date.parse(obj.today);

  if(obj.cal0.cfg.getProperty("pageDate").getMonth() == obj.today.getMonth() && obj.cal0.cfg.getProperty("pageDate").getYear() == obj.today.getYear())
    {
    obj.cal0.Style.CSS_NAV_LEFT = "calnavH"; 
    obj.cal0.Style.CSS_NAV_RIGHT = "calnavright";
    }
  else if(diff > (1000*60*60*24*obj.maxDaysOut)) 
    {
    obj.cal0.Style.CSS_NAV_LEFT = "calnavleft"; 
    obj.cal0.Style.CSS_NAV_RIGHT = "calnavH";
    }
  else 
    {
    obj.cal0.Style.CSS_NAV_LEFT = "calnavleft"; 
    obj.cal0.Style.CSS_NAV_RIGHT = "calnavright";
    }

  obj.cal0.render();
  $(obj.cal0.oDomContainer.id).style.display = "block";
}

CalendarPair.prototype.showCalendar1 = function(e, obj) {
  var theInput = $(obj.cal1Base + "Input");
  if(theInput.innerHTML == obj.disabledTxt)
    return;
  if(theInput.getElementsByTagName("a")[0] && theInput.getElementsByTagName("a")[0].innerHTML == obj.disabledTxt)
    return;

  var correctDate;
  if(obj.cal0.getSelectedDates().length != 0)
    correctDate = obj.cal0.getSelectedDates()[0];

  else if(obj.cal0.getSelectedDates().length == 0 && obj.cal1.getSelectedDates().length != 0)
    correctDate = obj.cal1.getSelectedDates()[0];

  else
    correctDate = obj.today;
  
  obj.cal1.setMonth(correctDate.getMonth()); 
  obj.cal1.setYear(correctDate.getFullYear());

  var diff = Date.parse(correctDate.toString()) - Date.parse(this.today);
  
  if(obj.cal1.cfg.getProperty("pageDate").getMonth() == obj.today.getMonth() && obj.cal1.cfg.getProperty("pageDate").getYear() == obj.today.getYear()) 
    {
    obj.cal1.Style.CSS_NAV_LEFT = "calnavH"; 
    obj.cal1.Style.CSS_NAV_RIGHT = "calnavright";
    }
  else if(diff > (1000*60*60*24* obj.maxDaysOut)) 
    {
    obj.cal1.Style.CSS_NAV_LEFT = "calnavleft"; 
    obj.cal1.Style.CSS_NAV_RIGHT = "calnavH";
    }
  else 
    {
    obj.cal1.Style.CSS_NAV_LEFT = "calnavleft"; 
    obj.cal1.Style.CSS_NAV_RIGHT = "calnavright";
    }
  
  obj.cal1.render();

  $(obj.cal1.oDomContainer.id).style.display = "block";
}

CalendarPair.prototype.hide0 = function(e, obj) {
  obj.cal0.style.display = "none";
  }

CalendarPair.prototype.hide1 = function(e, obj) {
  obj.cal1.style.display = "none";
  }

CalendarPair.prototype.hide = function(id) {
//function hide(id) {
  $(id).style.display = "none";
}

CalendarPair.prototype.changePage0 = function(type,args,obj) {
  var currDate = obj.today;
  var nextDate = obj.cal0.cfg.getProperty("pageDate");

  var diff = Date.parse(nextDate.toString()) - Date.parse(currDate.toString());

  if(diff <= 0) {
    obj.cal0.Style.CSS_NAV_LEFT = "calnavH"; 
    obj.cal0.Style.CSS_NAV_RIGHT = "calnavright";
    }

  else if(diff > (1000*60*60*24*this.maxDaysOut)) {
    obj.cal0.Style.CSS_NAV_LEFT = "calnavleft"; 
    obj.cal0.Style.CSS_NAV_RIGHT = "calnavH";
    }

  else {
    obj.cal0.Style.CSS_NAV_LEFT = "calnavleft"; 
    obj.cal0.Style.CSS_NAV_RIGHT = "calnavright";
  }

  obj.cal0.render();
}

CalendarPair.prototype.changePage1 = function(type, args, obj) {
  var currDate = obj.today; 
  var nextDate = obj.cal1.cfg.getProperty("pageDate");

  var diff = Date.parse(nextDate.toString()) - Date.parse(currDate.toString());

  if(diff <= 0) 
    {
    obj.cal1.Style.CSS_NAV_LEFT = "calnavH"; 
    obj.cal1.Style.CSS_NAV_RIGHT = "calnavright";
    }
  else if(diff > (1000*60*60*24*obj.maxDaysOut)) 
    {
    obj.cal1.Style.CSS_NAV_LEFT = "calnavleft"; 
    obj.cal1.Style.CSS_NAV_RIGHT = "calnavH";
    }
  else 
    {
    obj.cal1.Style.CSS_NAV_LEFT = "calnavleft"; 
    obj.cal1.Style.CSS_NAV_RIGHT = "calnavright";
    }

  obj.cal1.render();
  }


CalendarPair.prototype.dateSelect0 = function(type, args, obj) {
  var currDate = obj.cal0.getSelectedDates()[0]; 
  var parsed = Date.parse(currDate.toString());
  var jbDate = obj.cal0.Locale.WEEKDAYS_MEDIUM[currDate.getDay()] + ", " +
               obj.cal0.Locale.MONTHS_SHORT[currDate.getMonth()] + " " +
               currDate.getDate() + ", " +
               currDate.getFullYear();

  $(obj.cal0Base + "Input").innerHTML = jbDate;
  obj.cal0Hidden.value = (currDate.getMonth() + 1) + "/" + currDate.getDate() + "/" + currDate.getFullYear();

  if( obj.cal1Hidden.value == "" ) {
    obj.cal1.setMonth(currDate.getMonth()); 
    obj.cal1.setYear(currDate.getFullYear()); 
    obj.cal1.render();
    }

  else if( Date.parse( obj.cal1Hidden.value ) < parsed ) {
    $(obj.cal1Base + "Input").innerHTML = obj.cal1DefaultText;
    obj.cal1Hidden.value = "";
    obj.cal1.clear();
    }
  
  obj.cal1.cfg.setProperty("mindate", currDate);
  
  obj.hide(obj.cal0.oDomContainer.id);
  }

CalendarPair.prototype.dateSelect1 = function(type,args,obj) {
  var currDate = obj.cal1.getSelectedDates()[0];
  var jbDate = obj.cal1.Locale.WEEKDAYS_MEDIUM[currDate.getDay()] + ", " +
               obj.cal1.Locale.MONTHS_SHORT[currDate.getMonth()] + " " +
               currDate.getDate() + ", " +
               currDate.getFullYear();
    
  $(obj.cal1Base + "Input").innerHTML = jbDate;
  
  obj.cal1Hidden.value = (currDate.getMonth() + 1) + "/" + currDate.getDate() + "/" + currDate.getFullYear();

  obj.hide(obj.cal1.oDomContainer.id);
  }

CalendarPair.prototype.reenableCal = function() {
  var disabledTxt = "---------------------------------------";
  var theInput = $(this.cal1Base + "Input");
  if(theInput.innerHTML == disabledTxt || 
     (theInput.getElementsByTagName("a")[0] && 
     theInput.getElementsByTagName("a")[0].innerHTML == disabledTxt)) {
    theInput.style.color = "#4b90cd";
    theInput.innerHTML = "Date to return?";
    if(theInput.firstChild.nodeName.toLowerCase() == "a")
      theInput.firstChild.style.fontSize = "13px";
    }
  }


CalendarPair.prototype.disableCal = function() {
  //function disableCal(cal, hid) {
  var disabledTxt = "---------------------------------------";
  var theInput = $(this.cal1Base + "Input");
  theInput.innerHTML = disabledTxt;
  theInput.style.color = "#cccccc";
  
  if(theInput.getElementsByTagName("a")[0]) 
    theInput.getElementsByTagName("a")[0].style.color = "#cccccc";
  this.cal1.clear(); 
  $(this.cal1Name).value = "xxx";
  var trigger = $(this.cal1Base + "Trigger");
 
  }

CalendarPair.prototype.offClick = function(e, obj) {
  var caller = YAHOO.util.Event.getTarget(e);
  var chain0 = false;
  var chain1 = false;
  
  while(caller) {
    if(caller.id == obj.cal0.oDomContainer.id.replace("Container", "Root")) {
      chain0 = true;
      break;
    }
    else if(caller.id == obj.cal1.oDomContainer.id.replace("Container", "Root")) {
      chain1 = true;
      break;
    }

    caller = caller.parentNode;
  }

  if(!chain0) 
    obj.hide(obj.cal0.oDomContainer.id);
  if(!chain1) 
    obj.hide(obj.cal1.oDomContainer.id);
}

CalendarPair.prototype.preset0 = function (date) 
  {
  this.cal0.select(new Date(date));
  var firstDate = this.cal0.getSelectedDates()[0];
  this.cal0.cfg.setProperty("pagedate", (firstDate.getMonth()+1) + "/" + firstDate.getFullYear());
  this.cal0.render();
  }
			
CalendarPair.prototype.preset1 = function (date) 
  {
  this.cal1.select(new Date(date));
  var firstDate = this.cal1.getSelectedDates()[0];
  this.cal1.cfg.setProperty("pagedate", (firstDate.getMonth()+1) + "/" + firstDate.getFullYear());
  this.cal1.render();
  }

CalendarPair.prototype.getReadable1 = function() {
// this cant be explained... firefox somehow unexpectedly surrounds the raw text in <a> tags. 
// IE does not do this...  we have to fudge around this fact here
  if ($(this.cal0Base + "Input").getElementsByTagName("a").length > 0)
    return $(this.cal0Base + "Input").getElementsByTagName("a")[0].innerHTML;
  else
    return $(this.cal0Base + "Input").innerHTML;
  }

CalendarPair.prototype.getValue1 = function() {
  return $(this.cal0Name).value;
  }

CalendarPair.prototype.getName1 = function() {
  return this.cal0Name;
  }

CalendarPair.prototype.getReadable2 = function() {
//  return $(this.cal1.oDomContainer.id.replace("Container", "Input")).getElementsByTagName("a")[0].innerHTML;
// this cant be explained... firefox somehow unexpectedly surrounds the raw text in <a> tags. 
// IE does not do this...  we have to fudge around this fact here
  if ($(this.cal1Base + "Input").getElementsByTagName("a").length > 0)
    return $(this.cal1Base + "Input").getElementsByTagName("a")[0].innerHTML;
  else
    return $(this.cal1Base + "Input").innerHTML;
  }

CalendarPair.prototype.getValue2 = function() {
  return $(this.cal1Name).value;
  }

CalendarPair.prototype.getName2 = function() {
  return this.cal1Name;
  }


