






// safari work around function for Date.setDate()
function addDays(daysToAdd,date) {
  var x = new Date(2000,1,1);
  var y = new Date(2000,1,1);
  if(x.setDate(128) > y.valueOf())  {
    date.setDate(date.getDate()+daysToAdd);
    return date;
  }
  // Safari setDate(uint8) workaround
  if(daysToAdd < 0) {
    for(var i= -97; daysToAdd < i; daysToAdd-= i) {
      date.setDate(date.getDate()+i);
    }
  }
  else {
    for(var i= 96; daysToAdd > i; daysToAdd-= i) {
      date.setDate(date.getDate()+i);
    }
  }
  date.setDate(date.getDate()+daysToAdd);
  return date;
}

// Static date constants for today (not overwritten by functions)
var mcCalsToDisplay = 2;
var mcMaxBookDays   = 32;
var mcDisplayLimit  = 549;
var mcToday         = new Date();
    mcToday         = new Date(mcToday.getFullYear(),mcToday.getMonth(),mcToday.getDate());
var mcMaxDisplay    = new Date();
// safari fix
    mcMaxDisplay    = addDays(mcDisplayLimit, mcMaxDisplay); //today + 549 days = 550 days to display
var moMonthDayCountArrayCiCo = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

/*  set up language object to display the correct strings on the cal for internationalization (can and should be overwritten on calling page)
    default to english and get populated on the page with the translated strings
 */
var moLanguageObjCiCo = {
    calendarMonthNameArray    : ["January","February","March","April","May","June","July","August","September","October","November","December"],
    calendarShortMonthArray   : ["JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"],
    calendarShortDayNameArray : ["Sun","Mon","Tue","Wed","Thu","Fri","Sat"],
    calendarDayNameArray      : ["Su","M","Tu","W","Th","F","Sa"],
    errorArray                : [
                                  "For stays longer than 31 nights, please call 866-539-3446.",
                                  "You can only book up to 550 days in advance.",
                                  "Arrival date cannot be earlier than today.",
                                  "Departure date cannot be earlier than arrival date.",
                                  "Arrival and departure date cannot be on the same day.",
                                  "Please select a check in and check out date.",
                                  "Please select a checkout date."
                                ],
    pickCheckInText           : "Select check in date above",
    pickCheckOutText          : "Select check out date above",
    checkInText               : "Check in",
    checkOutText              : "Check out",
    clickCheckInTitle         : "Click for this check in",
    clickCheckOutTitle        : "Click for this check out",
    calendarClose             : "Close",
    calendarClear             : "Reset Calendar",
    resetSelectedDate         : "Reset",
    calendarPrev              : "Previous",
    calendarNext              : "Next",
    calendarContinue          : "Continue",
    submitDates               : "Submit dates",
    day                       : "Su",
    year                      : "Year"
};
    // function to determine the offsetLeft of an element that is passed in

function fnGetOffsetLeftCiCo (pElement) {
  var lsLeftOffset = pElement.offsetLeft;

  while ((pElement = pElement.offsetParent) != null){
      lsLeftOffset  += pElement.offsetLeft;
  }
  return lsLeftOffset;
}

// function to determine the offsetTop of an element that is passed in
function fnGetOffsetTopCiCo (pElement) {
  var lsTopOffset = pElement.offsetTop;
  while ((pElement = pElement.offsetParent) != null){
      lsTopOffset +=pElement.offsetTop;
  }
  return lsTopOffset;
}
 
var moCalendarObjCiCo = null;

function CalendarManager(config){
  this.calArray = new Array();
}

//Calendar Manager method to add a new calendar to the array
CalendarManager.prototype.addCalendar = function(pCalendarObj){
  this.calArray[pCalendarObj.calendarId] = pCalendarObj;
}

var calRightArrow = new Image();
calRightArrow.src = "/element/images/calendar/rightArrow.gif";
var calLeftArrow = new Image();
  calLeftArrow.src = "/element/images/calendar/leftArrow.gif";
var calControlBackground = new Image();
  calControlBackground.src = "/element/images/calendar/calendarcontrolbackground.gif";
var calCloseBox = new Image();
  calCloseBox.src = "/element/images/calendar/closebox.gif";
var calCells = new Image();
  calCells.src = "/element/images/calendar/el_calendar_cells.gif";

// declare and set module variables
var moCalendarManagerCiCo = new CalendarManager();

/**
 * Availability Calendar Constructor
 * @constructor
 * @param {String} pCalendarId Unique ID for instance of Calendar
 * @param {Object} pCIDate Function to retrieve check in date
 * @param {Object} pCODate Function to retrieve check out date
 * @param {Object} pRetrunFunction Function to set check-in and check-out dates
*/
function multiDisplayCalendar(pCalendarId, pCIDate, pCODate, pReturnFunction, settings){
// backwards compatibility
    this.settings = settings || {
                                  calendarsToDisplay    : mcCalsToDisplay,
                                  maxBookDays           : mcMaxBookDays,
                                  displayLimit          : mcDisplayLimit,
                                  today                 : mcToday,
                                  limitDate             : mcMaxDisplay
                                };
  this.settings.firstOfStartMonth = new Date(this.settings.today.getFullYear(), this.settings.today.getMonth(), 1)

	this.isRendered				= 0;
	this.isCreated				= 0;

  this.handleClass = function(className, tag, root, func, params) {
    var elements = yuiDom.getElementsByClassName(className, tag, root);
    if(func) {
      for(var i = 0; i < elements.length; i++) {
        func(elements[i],params);
      }
    }
  }
  this.userLanguage = "en";
  if(moCalendarManagerCiCo.calArray[pCalendarId]){
    var loCalendarObj = moCalendarManagerCiCo.calArray[pCalendarId];
    this.getCIDateFunction 	= pCIDate;
    this.getCODateFunction 	= pCODate;
    loCalendarObj.ciDateInitial  = this.getCIDateFunction();
    loCalendarObj.coDateInitial  = this.getCODateFunction();
    loCalendarObj.initializeObject();
    return loCalendarObj;
  }
  else{
    this.calendarId     	= pCalendarId;
    this.returnFunction 	= pReturnFunction;
    this.getCIDateFunction 	= pCIDate;
    this.getCODateFunction 	= pCODate;
    this.calLocation        = "bottomRight";
    this.checkIn          = null;
    this.checkOut         = null;
    this.calStartDate   	= null;
    this.display = 'none';
    this.calendarDivMonth = new Array();
    this.errorMessageDiv = null;
    this.calendarContinue = null;
    this.calendarPrevious = null;
    this.calendarPreviousAction = null;
    this.calendarNext = null;
    this.calendarNextAction = null;
    this.hasError   = null;
    this.errorArray = null;
    this.initializeObject();
    this.ciCocalendarIframe = null;
    this.calendarContainer = null;
    this.renderedMonths = {}; // object literal -> store already-rendered months (avoids re-rendering months);

    this.prevButton = document.createElement("div");  // container for prev button
    yuiDom.addClass(this.prevButton,"calendarNavigation");
    yuiDom.addClass(this.prevButton,"calendarNavigationPrevious")
    yuiEvent.addListener(this.prevButton,"click",this.changeMonth,-1,this);

    this.nextButton = document.createElement("div");  // container for next button
    yuiDom.addClass(this.nextButton,"calendarNavigation");
    yuiDom.addClass(this.nextButton,"calendarNavigationNext");
    yuiEvent.addListener(this.nextButton,"click",this.changeMonth,1,this);

    moCalendarManagerCiCo.addCalendar(this);
    return this;
  }
}

// gets the check-in and check-out from input fields, get calendarStartDate
multiDisplayCalendar.prototype.initializeObject = function(){
  this.checkIn = this.getCIDateFunction();
  this.checkOut = this.getCODateFunction();
  this.calStartDate = this.getCalStartDate();
  this.hasError = false;
}

// get calendar start date based on check-in and check-out
multiDisplayCalendar.prototype.getCalStartDate = function(){
  var calStartDate = new Date(this.settings.firstOfStartMonth);
  var currentCalStartDate = (this.calStartDate) ? new Date(this.calStartDate.getFullYear(), this.calStartDate.getMonth(), 1) : null;
  if(!currentCalStartDate || currentCalStartDate == calStartDate || (this.checkIn && this.checkOut)) {
    return (this.checkIn && this.checkOut) ? new Date(this.checkIn.getFullYear(),this.checkIn.getMonth(), 1) : calStartDate;
  }
  return currentCalStartDate;

}

// toggle between show and hide
multiDisplayCalendar.prototype.toggleDisplay = function(pCalendarIcon, calLocation){
  if(this.display != "block") {
    this.show(pCalendarIcon, calLocation);
  }
  else {
    this.hide();
  }
}

// show calendar, get check-in and check-out, get calendar start date, validate values
multiDisplayCalendar.prototype.show = function(pCalendarIcon, calLocation){
  this.initializeObject();
	if(!this.isCreated) {
        this.createFramework();
		this.isCreated = 1;
	}
  if(this.checkIn && this.checkOut) {
    this.validateCalendar();
  }
  if(this.hasError) {
    this.resetCheckInDate();
    this.resetCheckOutDate();
    this.calStartDate = new Date(this.settings.firstOfStartMonth);

  }
  this.renderCalDisplay();
  this.isRendered = 1;

  this.display = 'block';
  this.calendarContainer.style.display = this.display;
  // set the iframe dimensions, needs to be done after the calendar is displayed
  if(this.display == 'block'){
      this.ciCocalendarIframe.width           = this.calendarContainer.offsetWidth;
      this.ciCocalendarIframe.height          = this.calendarContainer.offsetHeight;
      this.ciCocalendarIframe.style.zIndex    = this.calendarContainer.style.zIndex - 1;
  }
  this.highlight();
  this.ciCocalendarIframe.style.display   = this.display;
	if(pCalendarIcon!=undefined) this.setCalendarPosition(pCalendarIcon, calLocation);
}

// hide calendar
multiDisplayCalendar.prototype.hide = function(event){
  this.display 				= 'none';
  this.calendarContainer.style.display 	= this.display;
  this.ciCocalendarIframe.style.display   	= this.display;
}

// set calendar position based on calendar icon
multiDisplayCalendar.prototype.setCalendarPosition = function(pCalendarIcon, calLocation){
	this.calLocation = calLocation;

  if (this.calLocation == "topLeft") {
      // display to the top left of the icon
      this.calendarContainer.style.top    = (fnGetOffsetTopCiCo(pCalendarIcon, this.calendarContainer) - this.calendarContainer.offsetHeight) + 'px';
      this.calendarContainer.style.left   = (fnGetOffsetLeftCiCo(pCalendarIcon, this.calendarContainer) - this.calendarContainer.offsetWidth) + 'px';
  } else if (this.calLocation == "topRight") {
       // display to the top left of the icon
      this.calendarContainer.style.top    = (fnGetOffsetTopCiCo(pCalendarIcon, this.calendarContainer) - this.calendarContainer.offsetHeight) + 'px';
      this.calendarContainer.style.left   = fnGetOffsetLeftCiCo(pCalendarIcon, this.calendarContainer) + 'px';
  } else if (this.calLocation == "bottomRight") {
      // display below and to the right of the icon
      this.calendarContainer.style.top    = fnGetOffsetTopCiCo(pCalendarIcon, this.calendarContainer) + 'px';
      this.calendarContainer.style.left   = fnGetOffsetLeftCiCo(pCalendarIcon, this.calendarContainer) + 'px';
  } else if (this.calLocation == "bottomLeft") {
       // display below and to the right of the icon
      this.calendarContainer.style.top    = fnGetOffsetTopCiCo(pCalendarIcon, this.calendarContainer) + 'px';
      this.calendarContainer.style.left   = (fnGetOffsetLeftCiCo(pCalendarIcon, this.calendarContainer) - this.calendarContainer.offsetWidth) + 'px';
  } else if (this.calLocation == "centerLeft") {
       // display center and to the left of the icon
      this.calendarContainer.style.top    = fnGetOffsetTopCiCo(pCalendarIcon, this.calendarContainer) - parseFloat(this.calendarContainer.offsetHeight)/2 +  'px';
      this.calendarContainer.style.left   = (fnGetOffsetLeftCiCo(pCalendarIcon, this.calendarContainer) - this.calendarContainer.offsetWidth) + 'px';
  } else if (this.calLocation == "centerRight") {
       // display center and to the right of the icon
      this.calendarContainer.style.top    = fnGetOffsetTopCiCo(pCalendarIcon, this.calendarContainer) - parseFloat(this.calendarContainer.offsetHeight)/2 +  'px';
      this.calendarContainer.style.left   = (fnGetOffsetLeftCiCo(pCalendarIcon, this.calendarContainer) ) + 'px';
  } else {
      // the user tells it where to be placed
      // expecting a string with x and y seperated by a comma
      var laUserLocation = this.calLocation.split(",");
      this.calendarContainer.style.top    =  laUserLocation[1] + 'px';
      this.calendarContainer.style.left   =  laUserLocation[0] + 'px';
  }
  this.ciCocalendarIframe.style.top       = this.calendarContainer.style.top;
  this.ciCocalendarIframe.style.left      = this.calendarContainer.style.left;
}

// create framework of the calendar
multiDisplayCalendar.prototype.createFramework = function(){
  var lsText;
  var loTextNode;

  //create the iframe to sit behind the layer - workaround for IE and select boxes
  var loCalenderHolderIframe = document.createElement("iframe");
      loCalenderHolderIframe.name = "calendarHolder_Iframe";
      loCalenderHolderIframe.id = "calendarHolder_Iframe_" + this.calendarId;
      loCalenderHolderIframe.width = 0;
      loCalenderHolderIframe.height = 0;
      loCalenderHolderIframe.style.zIndex = "99998";
      yuiDom.addClass(loCalenderHolderIframe, 'calendarHolderIframe');
      loCalenderHolderIframe.src = "/common/blank.jsp";
      this.ciCocalendarIframe = loCalenderHolderIframe;
      document.body.appendChild(this.ciCocalendarIframe);
  // create main calendar div

  var loCalenderHolder = document.createElement("div");
  this.calendarContainer = loCalenderHolder;
      loCalenderHolder.style.zIndex = "20000";
      loCalenderHolder.className = "calendarHolder";
      loCalenderHolder.id = "calendarHolder_" + this.calendarId;
      document.body.appendChild(loCalenderHolder);

  var loCalendarSubContainer = document.createElement("div");
      yuiDom.addClass(loCalendarSubContainer,"calendarSubContainer");
      this.calendarContainer.appendChild(loCalendarSubContainer);
      this.calendarContainer.subContainer = loCalendarSubContainer;
      
// create close button
  var loClose = document.createElement("div");
      this.calendarContainer.close = loClose;
      yuiDom.addClass(loClose, 'closeButton');
      var loCloseAction = document.createElement("a");
          loCloseAction.href = "javascript:void(0);";
          loCloseAction.target = "_self";
          yuiEvent.addListener(loCloseAction,"click",this.close,{},this);
          loCloseAction.title = moLanguageObjCiCo.calendarClose;
          lsText = moLanguageObjCiCo.calendarClose;
          loTextNode = document.createTextNode(lsText);
          loCloseAction.appendChild(loTextNode);
          var loCloseActionImg = document.createElement("img");
              loCloseActionImg.src = calCloseBox.src;
              loCloseActionImg.border = '0';
              loCloseActionImg.alt = moLanguageObjCiCo.calendarClose;
//            loCloseAction.appendChild(loCloseActionImg);
      loClose.appendChild(loCloseAction);
      this.calendarContainer.subContainer.appendChild(loClose);

  // clear calendar
  var loClearCal = document.createElement("div");
  this.calendarContainer.reset = loClearCal;
  yuiDom.addClass(loClearCal, 'reset')

  var resetButton = document.createElement("div");
  this.calendarContainer.reset.resetButton = resetButton;
  yuiDom.addClass(resetButton,'resetButton');

  this.calendarContainer.reset.appendChild(resetButton);

  var loResetAction = document.createElement("a");
  this.calendarContainer.reset.resetDates = loResetAction;
  loResetAction.href = "javascript:void(0);";
  loResetAction.target = "_self";
  yuiEvent.addListener(loResetAction,"click",this.reset,{},this);
  loResetAction.title = moLanguageObjCiCo.calendarClear;
  lsText = moLanguageObjCiCo.calendarClear;
  loTextNode = document.createTextNode(lsText);
  loResetAction.appendChild(loTextNode);

  this.calendarContainer.reset.resetButton.appendChild(loResetAction);
  this.calendarContainer.subContainer.appendChild(loClearCal);


  // create the main calendar portion
  var loCalendarContainer = document.createElement("div");
      this.calendarContainer.calendarOuterContainer = loCalendarContainer;
      loCalendarContainer.className = "calendarOuterContainer";

      // container

  var loCalendarInnerContainer = document.createElement("div");
      this.calendarContainer.calendarOuterContainer.calendarInnerContainer = loCalendarInnerContainer;
      loCalendarInnerContainer.className = "calendarInnerContainer";

      /* ****************CAL BODY HERE******************* */

      // mcCalsToDisplay --> var to hold number of calendar to be displayed...

      var spacerCount = 1;
      for(var i = 0; i < this.settings.calendarsToDisplay; i++) {
          var calEl = document.createElement("div");
          yuiDom.addClass(calEl, "calendarDiv");
          loCalendarInnerContainer.appendChild(calEl);
          this.calendarDivMonth[i] = calEl;
          spacerCount++;
          if(spacerCount % 2 == 0) {
              var loSpacerDiv = document.createElement("div");
              loSpacerDiv.className = "spacerDiv";
              loCalendarInnerContainer.appendChild(loSpacerDiv);
          }
       }
       var loClearDiv1 = document.createElement("div");
       loClearDiv1.className = "divClear";
       loCalendarInnerContainer.appendChild(loClearDiv1);

/* ****************END CAL BODY******************** */
      // clear div
  var loClearDiv2 = document.createElement("div");
      loClearDiv2.className = "divClear";
      loCalendarContainer.appendChild(loClearDiv2);

  this.calendarContainer.subContainer.appendChild(loCalendarContainer);
  this.calendarContainer.calendarOuterContainer.appendChild(loCalendarInnerContainer);
   // clear div

  var clearDiv = document.createElement("div");
  yuiDom.addClass(clearDiv,'divClear');
  this.calendarContainer.subContainer.appendChild(clearDiv);

  loCalendarContainer.appendChild(loClearDiv2);
  // create menuHolder Div

  var menuHolder = document.createElement("div");
  this.calendarContainer.menuHolder = menuHolder;
  yuiDom.addClass(menuHolder, 'menuHolder');
  this.calendarContainer.subContainer.appendChild(menuHolder);

  // create error display
  var loError = document.createElement("div");
      loError.className = "calendarError";
      loError.id = "calendarError_"+this.calendarId;
      this.errorMessageDiv = loError;
      this.calendarContainer.menuHolder.appendChild(loError);

  // create travel dates display
  var travelDateHolder = document.createElement("div");
  this.calendarContainer.menuHolder.travelDateHolder = travelDateHolder;
  yuiDom.addClass(travelDateHolder,"travelDateHolder");

  var instructions = document.createElement("ol");
  this.calendarContainer.menuHolder.travelDateHolder.instructions = instructions;
  yuiDom.addClass(instructions,"instructions");

  var checkInInstructions = document.createElement("li");
  this.calendarContainer.menuHolder.travelDateHolder.instructions.checkInInstructions = checkInInstructions;
  checkInInstructions.id = "checkInInstructions_"+this.calendarId;
  yuiDom.addClass(checkInInstructions, "checkInInstructions")
  yuiDom.addClass(checkInInstructions,"active");
  yuiDom.addClass(checkInInstructions,"checkInInstructionsActive");
  var checkInText = document.createTextNode(moLanguageObjCiCo.pickCheckInText);
  checkInInstructions.appendChild(checkInText);

  var checkOutInstructions = document.createElement("li");
  this.calendarContainer.menuHolder.travelDateHolder.instructions.checkOutInstructions = checkOutInstructions;
  checkOutInstructions.id = "checkOutInstructions_"+this.calendarId;
  yuiDom.addClass(checkOutInstructions, "checkOutInstructions")
  var checkOutText = document.createTextNode(moLanguageObjCiCo.pickCheckOutText); // code decode
  checkOutInstructions.appendChild(checkOutText);

  var submitInstructions = document.createElement("li");
  this.calendarContainer.menuHolder.travelDateHolder.instructions.submitInstructions = submitInstructions;
  submitInstructions.id = "submitInstructions_"+this.calendarId;
  yuiDom.addClass(submitInstructions, "submitInstructions")
  var submitText = document.createTextNode(moLanguageObjCiCo.submitDates); // code decode
  submitInstructions.appendChild(submitText);

  var continueButton = document.createElement("div");
  this.calendarContainer.menuHolder.travelDateHolder.instructions.submitInstructions.continueButton = continueButton;
  yuiDom.addClass(continueButton,"continueButton");
  yuiDom.addClass(continueButton,"buttonLink");

  var continueLink = document.createElement("a");
  this.calendarContainer.menuHolder.travelDateHolder.instructions.submitInstructions.continueButton.continueLink = continueLink;
  yuiDom.addClass(continueLink,"continueLink");
  continueLink.href = "javascript:void(0);";
  continueLink.target = "_self";
  yuiEvent.addListener(continueLink,"click",this.submit,{checkIn: this.checkIn,checkOut: this.checkOut},this);
  continueLink.title = moLanguageObjCiCo.calendarContinue;
  var continueText = document.createTextNode(moLanguageObjCiCo.calendarContinue);
  continueLink.appendChild(continueText);
  continueButton.appendChild(continueLink);
  this.calendarContainer.menuHolder.travelDateHolder.instructions.appendChild(checkInInstructions);
  this.calendarContainer.menuHolder.travelDateHolder.instructions.appendChild(checkOutInstructions);
  this.calendarContainer.menuHolder.travelDateHolder.instructions.appendChild(submitInstructions);

  this.calendarContainer.menuHolder.travelDateHolder.appendChild(instructions);
  this.calendarContainer.menuHolder.appendChild(travelDateHolder);

  this.checkInToolTip = document.createElement("div");
  yuiDom.addClass(this.checkInToolTip, "calendarToolTip");
  yuiDom.addClass(this.checkInToolTip, "checkInToolTip");
  this.checkInToolTip.innerHTML = moLanguageObjCiCo.clickCheckInTitle;
  document.body.appendChild(this.checkInToolTip)

  this.checkOutToolTip = document.createElement("div");
  yuiDom.addClass(this.checkOutToolTip, "calendarToolTip");
  yuiDom.addClass(this.checkOutToolTip, "checkOutToolTip");
  this.checkOutToolTip.innerHTML = moLanguageObjCiCo.clickCheckOutTitle;
  document.body.appendChild(this.checkOutToolTip)

  return loCalenderHolder;
}
// End createFramework

// render the calendar
multiDisplayCalendar.prototype.renderCalendar = function() {

// clear all child nodes first, never should need to do replaceChild for this.calendarDivMonth[calIndex]
    for(var calIndex = 0; calIndex < this.settings.calendarsToDisplay; calIndex++) {
      if(this.calendarDivMonth[calIndex].firstChild) {
        this.calendarDivMonth[calIndex].removeChild(this.calendarDivMonth[calIndex].firstChild);
      }
    }
    for (var calIndex = 0; calIndex < this.settings.calendarsToDisplay; calIndex++) {
      var columnIndex   = 0;
      var workingDate   = new Date(this.calStartDate);
      workingDate       = new Date(workingDate.setMonth(workingDate.getMonth() + calIndex));
      var currentMonth  = workingDate.getMonth();
      var currentYear   = workingDate.getFullYear();
      var currentDay    = new Date(currentYear,currentMonth,1).getDay();

      if(!this.renderedMonths[currentYear]){
          this.renderedMonths[currentYear] = {};
      }
      var calendarTable;
      if(!this.renderedMonths[currentYear][currentMonth]) {
          //Write the newly constructed table to the div on the page
          calendarTable = this.createMonth(calIndex);
          this.calendarDivMonth[calIndex].appendChild(calendarTable);
          this.renderedMonths[currentYear][currentMonth] = calendarTable;
      }
      else {
        if(this.renderedMonths[currentYear]) {
          if(!this.renderedMonths[currentYear][currentMonth]) {
          // create calendar for the month if not created already
            calendarTable = this.createMonth(calIndex);
            this.renderedMonths[currentYear][currentMonth] = calendarTable;
          }
          if(this.renderedMonths[currentYear][currentMonth].calendarHeader) {
            if(calIndex == 0) {
//              add previous button control for first calendar
              if(this.prevButton.parentNode && this.prevButton.parentNode != this.renderedMonths[currentYear][currentMonth].calendarHeader) {
                 this.prevButton.parentNode.removeChild(this.prevButton);
              }
              if(workingDate > this.settings.today) {
                if(this.renderedMonths[currentYear][currentMonth].calendarHeader.firstChild && this.renderedMonths[currentYear][currentMonth].calendarHeader.firstChild != this.prevButton) {
                  this.renderedMonths[currentYear][currentMonth].calendarHeader.insertBefore(this.prevButton,this.renderedMonths[currentYear][currentMonth].calendarHeader.firstChild);
                }
              }
            }
            if(calIndex == 1) {
//              add next button control first second calendar
              if(this.nextButton.parentNode && this.nextButton.parentNode != this.renderedMonths[currentYear][currentMonth].calendarHeader) {
                this.nextButton.parentNode.removeChild(this.nextButton);
              }
              var limitDateCheck = workingDate.setMonth(workingDate.getMonth()+1);
              if(limitDateCheck < this.settings.limitDate) {
                if(this.renderedMonths[currentYear][currentMonth].calendarHeader.firstChild && this.renderedMonths[currentYear][currentMonth].calendarHeader.firstChild != this.nextButton) {
                  this.renderedMonths[currentYear][currentMonth].calendarHeader.insertBefore(this.nextButton,this.renderedMonths[currentYear][currentMonth].calendarHeader.firstChild);
                }
              }
            }
          }
//          put the month back into container
          this.calendarDivMonth[calIndex].appendChild(this.renderedMonths[currentYear][currentMonth]);
        }
      }
      this.resetHightlight(this.calendarDivMonth[calIndex]);
    }
}

multiDisplayCalendar.prototype.renderCalDisplay = function(){
    this.renderCalendar();
    this.highlight();
}

multiDisplayCalendar.prototype.createMonth = function(calIndex) {
  var columnIndex   = 0;
  var workingDate   = new Date(this.calStartDate);
  workingDate       = new Date(workingDate.setMonth(workingDate.getMonth() + calIndex));
  var currentMonth  = workingDate.getMonth();
  var currentYear   = workingDate.getFullYear();
  var currentDay    = new Date(currentYear,currentMonth,1).getDay();
  // calendarTable - to be returned by this function, main day holder
  var calendarTable        = document.createElement("div");
  yuiDom.addClass(calendarTable, "calendarTable");

  var calendarHeader       = document.createElement("div");
  yuiDom.addClass(calendarHeader, "calendarHeader");

  var clearHdr = document.createElement("div");
  yuiDom.addClass(clearHdr,"divClear");

  var calendarMonth        = document.createElement("div");
  var calendarMonthText    = document.createTextNode(moLanguageObjCiCo.calendarMonthNameArray[currentMonth] + " " + currentYear);
  yuiDom.addClass(calendarMonth, "month");
  calendarMonth.appendChild(calendarMonthText);
  if(calIndex == 0 && this.prevButton.parentNode) {
    this.prevButton.parentNode.removeChild(this.prevButton);
  }
  if(calIndex == 1 && this.nextButton.parentNode) {
    this.nextButton.parentNode.removeChild(this.nextButton);
  }
  if(calIndex == 0 && (workingDate > this.settings.today)) {
    this.calendarPrevious = this.prevButton;
    calendarHeader.appendChild(this.prevButton);
  }
  var dateCheck = workingDate;
  dateCheck.setMonth(workingDate.getMonth()+1);

  if(calIndex == 1 && (dateCheck < this.settings.limitDate)) {
    this.calendarNext = this.nextButton;
    calendarHeader.appendChild(this.nextButton);
  }

  calendarTable.appendChild(calendarHeader);
//  attach calendar header to calendarTable so we can use it as reference when adding next and previous buttons
  calendarTable.calendarHeader = calendarHeader;
  calendarHeader.appendChild(calendarMonth);
  calendarHeader.appendChild(clearHdr);

  var calendarDays   = document.createElement("div");
  yuiDom.addClass(calendarDays,"calendarTableInner");
  calendarTable.appendChild(calendarDays);

  // create day headers
  for (var i = 0; i < 7; i++){
      var day     = document.createElement("div");
      var dayText = document.createTextNode(moLanguageObjCiCo.calendarDayNameArray[i]);
      yuiDom.addClass(day,"daysoftheweek");
      day.appendChild(dayText);
      calendarDays.appendChild(day);
  }

  var clearDiv = document.createElement("div");
  yuiDom.addClass(clearDiv,"divClear");
  calendarDays.appendChild(clearDiv);

  // Figure out if it is a leap year and set accordingly
  moMonthDayCountArrayCiCo[1] = (((currentYear % 4 == 0) && (currentYear % 100 != 0)) || (currentYear % 400 == 0)) ? 29 : 28;
  for (var i=0; i<currentDay; i++, columnIndex++){
      var emptyDay = document.createElement("div");
      yuiDom.addClass(emptyDay,"day");
      yuiDom.addClass(emptyDay,"emptyDay");
      calendarDays.appendChild(emptyDay);
  }
  for (var i=0; i<=moMonthDayCountArrayCiCo[currentMonth]-1; i++, columnIndex++) {
    var currentDate = new Date(currentYear,currentMonth,1);
    currentDate = new Date(currentDate.setDate(currentDate.getDate() + i));
    if(currentDate < this.settings.limitDate && currentDate >= this.settings.today){ // day is valid
        var validDay = document.createElement("div");
        yuiDom.addClass(validDay,"day");
        yuiDom.addClass(validDay,"available");
        yuiEvent.addListener(validDay,"click",this.selectDay,{calendarId:this.calendarId,element: validDay, currentDate:currentDate.getFullYear() + '/' + (currentDate.getMonth()+1) + '/' + currentDate.getDate()},this);
        yuiEvent.addListener(validDay,"mouseover",this.hover,{hover: true,element: validDay},this);
        yuiEvent.addListener(validDay,"mouseout",this.hover,{hover: false,element: validDay},this);
        var dayLink     = document.createElement("a");
        dayLink.href    = "javascript:void(0);";
        dayLink.target = "_self";
        var dayLinkText = document.createTextNode(currentDate.getDate());
        dayLink.appendChild(dayLinkText);
        validDay.appendChild(dayLink);
        validDay.id= currentDate.getFullYear() + "/" + (currentDate.getMonth()+1) + "/" + currentDate.getDate() + "_" + this.calendarId;
        calendarDays.appendChild(validDay);
    }
    else if(currentDate > this.settings.limitDate && currentDate >= this.settings.today){ //day is past the book limit
        var pastBooking = document.createElement("div");
        yuiDom.addClass(pastBooking,"day");
        yuiDom.addClass(pastBooking,"invalidDay");
        var pastBookingText = document.createTextNode(currentDate.getDate());
        pastBooking.appendChild(pastBookingText);
        calendarDays.appendChild(pastBooking);
    }
    if(currentDate < this.settings.today){ // day is less than today
        var pastDay     = document.createElement("div");
        yuiDom.addClass(pastDay,"day");
        yuiDom.addClass(pastDay,"pastDay");
        var pastDayText = document.createTextNode(currentDate.getDate());
        pastDay.appendChild(pastDayText);
        calendarDays.appendChild(pastDay);
    }
    if (columnIndex == 6) {
        var clearWeek = document.createElement("div");
        yuiDom.addClass(clearWeek,"divClear");
        calendarDays.appendChild(clearWeek);
        columnIndex = -1;
    }
  }
  if (columnIndex > 0) {
      for (var i=1; columnIndex<7; i++, columnIndex++){
          var emptyDay = document.createElement("div");
          yuiDom.addClass(emptyDay,"day");
          yuiDom.addClass(emptyDay,"emptyDay");
          calendarDays.appendChild(emptyDay);
      }
  }

  var clearDays = document.createElement("div");
  yuiDom.addClass(clearDays,"divClear");
  calendarDays.appendChild(clearDays);

  calendarTable.appendChild(calendarDays);

  var clearBody = document.createElement("div");
  yuiDom.addClass(clearBody,"divClear");
  calendarTable.appendChild(clearBody);
  return calendarTable;
}

multiDisplayCalendar.prototype.setNavigation = function(){

    var lvWorkingDate   = new Date(this.calStartDate);
        lvWorkingDate   = new Date(lvWorkingDate.setMonth(lvWorkingDate.getMonth() + 1));
    var loMaxDisplay    = new Date(this.settings.limitDate);
    var loDisableArray = new Array(false, false);

    // Prev button
    if(this.calStartDate.getMonth() == this.settings.today.getMonth() && this.calStartDate.getFullYear() == this.settings.today.getFullYear()){
        this.calendarPrevious.className = 'calendarNavigationPrevDisabled';
        this.calendarPrevious.href = "javascript:void(0);";
        yuiEvent.removeListener(this.calendarPrevious,"click",this.changeMonth);
    }
    else{
        this.calendarPrevious.className = 'calendarNavigationPrevActive';
        this.calendarPrevious.href = this.calendarPreviousAction;
        yuiEvent.addListener(this.calendarPrevious,"click",this.changeMonth,-1,this);
    }
    // next button
    if(lvWorkingDate.getMonth() == loMaxDisplay.getMonth() && lvWorkingDate.getFullYear() == loMaxDisplay.getFullYear()){
        this.calendarNext.className = 'calendarNavigationNextDisabled';
        this.calendarNext.href = "javascript:void(0);";
        yuiEvent.removeListener(this.calendarNext,"click",this.changeMonth);
    }
    else{
        this.calendarNext.className = 'calendarNavigationNextActive';
        this.calendarNext.href = this.calendarNextAction;
        yuiEvent.addListener(this.calendarNext,"click",this.changeMonth,1,this);
    }
}

multiDisplayCalendar.prototype.getLengthOfStay = function(startDate,endDate){
  return Math.round(Math.abs(endDate - startDate)/(1000 * 60 * 60 * 24)) +1;
}

multiDisplayCalendar.prototype.changeMonth = function(e, direction){
//    var loCalendarObj = moCalendarManagerCiCo.calArray[pCalendarId];
  var loNewStart = new Date(this.calStartDate);
  var newMonth = loNewStart.getMonth() + direction;
  // safari fix
  if(newMonth < 0) {
    loNewStart.setFullYear(loNewStart.getFullYear()-1);
    newMonth = 11;
  }
  loNewStart.setMonth(newMonth);
  if(loNewStart >=  this.settings.firstOfStartMonth && this.settings.limitDate >= loNewStart ) {
    this.calStartDate = loNewStart;
    this.renderCalDisplay();
  }
}

multiDisplayCalendar.prototype.showToolTip = function(e,tip,element) {
  var coords  = yuiEvent.getXY(e);
  yuiDom.setStyle(tip, "display", "block");
  yuiDom.setStyle(tip, "left", coords[0]+15 +"px");
  yuiDom.setStyle(tip, "top", coords[1]+10 +"px");
}
multiDisplayCalendar.prototype.hover = function(e, params) {

  var hoverDate = new Date(params.element.id.split("_")[0]);

  if(params.hover) { // mouseover

    var coords  = yuiDom.getXY(params.element);
    if(this.checkIn && !this.checkOut) {
      if(this.checkIn<hoverDate) {
        yuiDom.addClass(params.element,"hoverCheckOut");
        this.showToolTip(e,this.checkOutToolTip,params.element);
      }
      else {
        yuiDom.addClass(params.element,"hoverCheckIn");
        this.showToolTip(e,this.checkInToolTip,params.element);
      }
      return;
    }
    else if(this.checkOut && !this.checkIn) {
      if(this.checkOut<hoverDate) {
        yuiDom.addClass(params.element,"hoverCheckOut");
        this.showToolTip(e,this.checkOutToolTip,params.element);
      }
      else {
        yuiDom.addClass(params.element,"hoverCheckIn");
        this.showToolTip(e,this.checkInToolTip,params.element);
      }
      return;
    }
    else if(!this.checkIn) {
      yuiDom.addClass(params.element,"hoverCheckIn");
      this.showToolTip(e,this.checkInToolTip,params.element);
      return;
    }
    else if(!this.checkOut) {
      yuiDom.addClass(params.element,"hoverCheckOut");
        this.showToolTip(e,this.checkOutToolTip,params.element);
      return;
    }

    if((this.checkIn && this.checkOut) && (this.checkIn < hoverDate && this.checkOut > hoverDate)) {
      var daysBetween = Math.round(Math.abs(this.checkOut - this.checkIn)/(1000 * 60 * 60 * 24)) - 1;
      var halfWay = parseInt(daysBetween/2);
      var halfWayDate = new Date(this.checkIn.getFullYear(), this.checkIn.getMonth(), this.checkIn.getDate())
      halfWayDate = addDays(halfWay,halfWayDate);
      if(hoverDate <= halfWayDate) {
        yuiDom.addClass(params.element,"hoverCheckIn");
        this.showToolTip(e,this.checkInToolTip,params.element);
      }
      else {
        yuiDom.addClass(params.element,"hoverCheckOut");
        this.showToolTip(e,this.checkOutToolTip,params.element);
      }
    }
    else if(this.checkIn > hoverDate) {
      yuiDom.addClass(params.element,"hoverCheckIn");
      this.showToolTip(e,this.checkInToolTip,params.element);
    }
    else if(this.checkOut < hoverDate) {
      yuiDom.addClass(params.element,"hoverCheckOut");
      this.showToolTip(e,this.checkOutToolTip,params.element);
    }
  }
  else { // mouseout
    // remove all hover classes
    this.checkInToolTip.style.display = "none";
    this.checkOutToolTip.style.display = "none";
    if(!this.checkIn) {
      yuiDom.removeClass(params.element, "hoverCheckIn");
    }
    else if(!this.checkOut) {
      yuiDom.removeClass(params.element, "hoverCheckOut");
    }

    if(this.checkIn){
      yuiDom.removeClass(params.element,"hoverCheckIn");
    }
    if(this.checkOut){
      yuiDom.removeClass(params.element,"hoverCheckOut");
    }
  }
}

multiDisplayCalendar.prototype.selectDay = function(e,params) {
    var selectedDate = new Date(params.currentDate);

    // validate the selectedDate first
    this.validateSelectedDate(selectedDate);
    if(this.hasError) {
      return;
    }
    // no dates selected, treat as checkin
    if(!(this.checkIn&&this.checkOut)) {
      if(!this.checkIn&&!this.checkOut) {
        this.checkIn = selectedDate;
        this.resetHightlight(this.calendarContainer)
        this.highlight();
      }
      // check-in but no check-out
      else if(this.checkIn && !this.checkOut  && (selectedDate > this.checkIn || selectedDate < this.checkIn)) {
        if(selectedDate > this.checkIn) {
          this.checkOut = selectedDate;
        }
        else {
          this.checkOut = this.checkIn;
          this.checkIn  = selectedDate;
        }
        this.resetHightlight(this.calendarContainer)
        this.highlight();
      }
      // check-out but no check-in
      else if(!this.checkIn && this.checkOut && (selectedDate > this.checkOut || selectedDate < this.checkOut)) {
        if(selectedDate < this.checkOut) {
          this.checkIn = selectedDate;
        }
        else {
          this.checkIn = this.checkOut;
          this.checkOut  = selectedDate;
        }
        this.resetHightlight(this.calendarContainer)
        this.highlight();
      }
      return;
    }
    
    // user selects dates between already selected check in and check out dates
    if((this.checkIn && this.checkOut) && (this.checkIn < selectedDate && this.checkOut > selectedDate)) {
      var daysBetween = Math.round(Math.abs(this.checkOut - this.checkIn)/(1000 * 60 * 60 * 24)) - 1;
      var halfWay = parseInt(daysBetween/2);
      var tempCheckInDate = new Date(this.checkIn.getFullYear(),this.checkIn.getMonth(),this.checkIn.getDate());
      tempCheckInDate = addDays(halfWay,tempCheckInDate);
      if(selectedDate <= tempCheckInDate) {
        this.checkIn = selectedDate;
      }
      else {
        this.checkOut = selectedDate;
      }
    }
    else {
      if(this.checkIn > selectedDate) {
        this.checkIn = selectedDate;
      }
      if(this.checkOut < selectedDate) {
        this.checkOut = selectedDate;
      }
    }
    this.resetHightlight(this.calendarContainer)
    this.highlight();
}

// do all the highlighting logic if any dates are selected, while we're at it, set instructions also
multiDisplayCalendar.prototype.highlight = function() {
  if(this.checkIn) {
    var hightlightElementId = this.checkIn.getFullYear() + "/" + (this.checkIn.getMonth()+1) + "/" + (this.checkIn.getDate())+"_"+this.calendarId;
    yuiDom.addClass(yuiDom.get(hightlightElementId), "selectedCheckIn");
    yuiDom.removeClass(yuiDom.get(hightlightElementId), "validCheckOut");
    yuiDom.removeClass(yuiDom.get(hightlightElementId), "unselected");
    var calCheckIn = this.checkIn;
    this.handleClass("available","div",this.calendarContainer,function(element){
      var elementIdDate = new Date(element.id.split("_")[0]);
      if(!yuiDom.hasClass(element,"selectedCheckIn")) {
        if(elementIdDate > calCheckIn) {
          yuiDom.addClass(element,"validCheckOut");
        }
        else {
          yuiDom.addClass(element, "unselected");
        }
      }
    });
        // Chinese & Japanese language support
    if(this.userLanguage == "zh" || this.userLanguage == "ja") {
        this.calendarContainer.menuHolder.travelDateHolder.instructions.checkInInstructions.innerHTML = moLanguageObjCiCo.checkInText + " - " + moLanguageObjCiCo.calendarShortDayNameArray[this.checkIn.getDay()]
        + ", " + this.checkIn.getFullYear()
        + " " + moLanguageObjCiCo.year
        + " " + moLanguageObjCiCo.calendarShortMonthArray[this.checkIn.getMonth()]
        + " " + this.checkIn.getDate()
        + " " + moLanguageObjCiCo.day;
    }
    else {
        this.calendarContainer.menuHolder.travelDateHolder.instructions.checkInInstructions.innerHTML = moLanguageObjCiCo.checkInText + " - " + moLanguageObjCiCo.calendarShortDayNameArray[this.checkIn.getDay()]
        + ', ' + this.checkIn.getDate()
        + ' ' + moLanguageObjCiCo.calendarShortMonthArray[this.checkIn.getMonth()]
        + ' ' + this.checkIn.getFullYear()
    }
    this.calendarContainer.menuHolder.travelDateHolder.instructions.checkInInstructions.innerHTML += ' ' + '<a href="javascript:void(0);" target="_self" class="resetDateLink" id="resetCheckInDateLink_'+this.calendarId+'">'+moLanguageObjCiCo.resetSelectedDate+'</a>';    // display check in date in check in div


    yuiEvent.addListener(yuiDom.get("resetCheckInDateLink_"+this.calendarId), "click", this.resetCheckInDate,yuiDom.get("resetCheckInDateLink_"+this.calendarId),this);
    yuiDom.removeClass(this.calendarContainer.menuHolder.travelDateHolder.instructions.checkInInstructions, "active"); // remove active class from check in div
    yuiDom.removeClass(this.calendarContainer.menuHolder.travelDateHolder.instructions.checkInInstructions, "checkInInstructionsActive");
    yuiDom.addClass(this.calendarContainer.menuHolder.travelDateHolder.instructions.checkOutInstructions, "active");   // apply active class to check out div
    yuiDom.addClass(this.calendarContainer.menuHolder.travelDateHolder.instructions.checkOutInstructions, "checkOutInstructionsActive");   // apply active class to check out div
  }
  else {
    this.calendarContainer.menuHolder.travelDateHolder.instructions.checkInInstructions.innerHTML = moLanguageObjCiCo.pickCheckInText;
  }

  if(this.checkOut) {
    var hightlightElementId = this.checkOut.getFullYear() + "/" + (this.checkOut.getMonth()+1) + "/" + (this.checkOut.getDate())+"_"+this.calendarId;
    yuiDom.addClass(yuiDom.get(hightlightElementId), "selectedCheckOut");
    yuiDom.removeClass(yuiDom.get(hightlightElementId), "validCheckOut");
    yuiDom.removeClass(yuiDom.get(hightlightElementId), "unselected");
    yuiDom.removeClass(yuiDom.get(hightlightElementId), "selectedInterval");
    var calCheckOut = this.checkOut;
    this.handleClass("available","div",this.calendarContainer,function(element){
      var elementIdDate = new Date(element.id.split("_")[0]);
      yuiDom.removeClass(element,"validCheckOut");
      if(elementIdDate > calCheckOut) {
        yuiDom.addClass(element,"unselected");
      }
    });
        // Chinese & Japanese language support 
    if(this.userLanguage == "zh" || this.userLanguage == "ja") {
        this.calendarContainer.menuHolder.travelDateHolder.instructions.checkOutInstructions.innerHTML = moLanguageObjCiCo.checkOutText + " - "
        + moLanguageObjCiCo.calendarShortDayNameArray[this.checkOut.getDay()]
        + ", " + this.checkOut.getFullYear()
        + " " + moLanguageObjCiCo.year
        + " " + moLanguageObjCiCo.calendarShortMonthArray[this.checkOut.getMonth()]
        + " " + this.checkOut.getDate()
        + " " + moLanguageObjCiCo.day;
    }
    else {
        this.calendarContainer.menuHolder.travelDateHolder.instructions.checkOutInstructions.innerHTML = moLanguageObjCiCo.checkOutText + " - "
        + moLanguageObjCiCo.calendarShortDayNameArray[this.checkOut.getDay()]
        + ', ' + this.checkOut.getDate()
        + ' ' + moLanguageObjCiCo.calendarShortMonthArray[this.checkOut.getMonth()]
        + ' ' + this.checkOut.getFullYear();
    }
    this.calendarContainer.menuHolder.travelDateHolder.instructions.checkOutInstructions.innerHTML += ' '
    + '<a href="javascript:void(0);" target="_self" class="resetDateLink" id="resetCheckOutDateLink_'
    + this.calendarId +'">'
    +moLanguageObjCiCo.resetSelectedDate+'</a>';      // display check out date in check in div

    yuiEvent.addListener(yuiDom.get("resetCheckOutDateLink_"+this.calendarId), "click", this.resetCheckOutDate,yuiDom.get("resetCheckOutDateLink_"+this.calendarId),this);
    yuiDom.removeClass(this.calendarContainer.menuHolder.travelDateHolder.instructions.checkOutInstructions, "active"); // remove active class from check in div
    yuiDom.removeClass(this.calendarContainer.menuHolder.travelDateHolder.instructions.checkOutInstructions, "checkOutInstructionsActive"); // remove active class from check in div
  }
  else {
    this.calendarContainer.menuHolder.travelDateHolder.instructions.checkOutInstructions.innerHTML = moLanguageObjCiCo.pickCheckOutText;
  }

  if(this.checkIn && this.checkOut) {
    yuiDom.addClass(this.calendarContainer.menuHolder.travelDateHolder.instructions.submitInstructions, "active");   // apply active class to check out div
    yuiDom.addClass(this.calendarContainer.menuHolder.travelDateHolder.instructions.submitInstructions, "submitInstructionsActive");   // apply active class to check out div
    this.calendarContainer.menuHolder.travelDateHolder.instructions.submitInstructions.appendChild(this.calendarContainer.menuHolder.travelDateHolder.instructions.submitInstructions.continueButton);
    var daysBetween = Math.round(Math.abs(this.checkOut - this.checkIn)/(1000 * 60 * 60 * 24)) - 1;
    var  tempDate = new Date(this.checkIn.getFullYear(), this.checkIn.getMonth(), this.checkIn.getDate())
    for(var i = 1; i <= daysBetween; i++) {
      tempDate = addDays(1,tempDate);
      var highlightInterval = tempDate.getFullYear() + "/" + (tempDate.getMonth()+1) + "/" + (tempDate.getDate())+"_"+this.calendarId;
      yuiDom.addClass(yuiDom.get(highlightInterval),"selectedInterval");
      yuiDom.removeClass(yuiDom.get(highlightInterval),"validCheckOut");
      yuiDom.removeClass(yuiDom.get(highlightInterval),"unselected");
    }
  }
  else {
    yuiDom.removeClass(this.calendarContainer.menuHolder.travelDateHolder.instructions.submitInstructions, "active");   // apply active class to check out div
    yuiDom.removeClass(this.calendarContainer.menuHolder.travelDateHolder.instructions.submitInstructions, "submitInstructionsActive");   // apply active class to check out div
    if(this.calendarContainer.menuHolder.travelDateHolder.instructions.submitInstructions.continueButton.parentNode == this.calendarContainer.menuHolder.travelDateHolder.instructions.submitInstructions) {
      this.calendarContainer.menuHolder.travelDateHolder.instructions.submitInstructions.removeChild(this.calendarContainer.menuHolder.travelDateHolder.instructions.submitInstructions.continueButton);
    }
    if(!this.checkIn && !this.checkOut) {
      yuiDom.addClass(this.calendarContainer.menuHolder.travelDateHolder.instructions.checkInInstructions, "active");   // apply active class to check out div
      yuiDom.addClass(this.calendarContainer.menuHolder.travelDateHolder.instructions.checkInInstructions, "checkInInstructionsActive");   // apply active class to check out div
    }
  }
}

multiDisplayCalendar.prototype.resetHightlight = function(monthElement) {
  this.handleClass("day","div",monthElement,function(element) {
      yuiDom.removeClass(element,"validCheckOut");
      yuiDom.removeClass(element,"selectedCheckIn");
      yuiDom.removeClass(element,"selectedCheckOut");
      yuiDom.removeClass(element,"selectedInterval");
      yuiDom.removeClass(element,"unselected");
  });
}

multiDisplayCalendar.prototype.validateCalendar = function(pFromContinue){
    this.hasError = false;
    this.errorArray = new Array();

    if(this.checkIn){
      if(this.checkIn > this.settings.limitDate){
        this.errorArray[this.errorArray.length] = moLanguageObjCiCo.errorArray[1];
        this.hasError = true;
      }
      if(this.checkIn < this.settings.today){
        this.errorArray[this.errorArray.length] = moLanguageObjCiCo.errorArray[2];
        this.hasError = true;
      }
    }

    if(this.checkIn && this.checkOut){
      if(this.checkOut > this.settings.limitDate && this.checkIn < this.settings.limitDate){
        this.errorArray[this.errorArray.length] = moLanguageObjCiCo.errorArray[1];
        this.hasError = true;
      }
      if(this.getLengthOfStay(this.checkIn, this.checkOut) > this.settings.maxBookDays){
        this.errorArray[this.errorArray.length] = moLanguageObjCiCo.errorArray[0];
        this.hasError = true;
      }
      if(this.checkOut < this.checkIn){
        this.errorArray[this.errorArray.length] = moLanguageObjCiCo.errorArray[3];
        this.hasError = true;
      }
      if(this.checkIn == this.checkOut){
        this.errorArray[this.errorArray.length] = moLanguageObjCiCo.errorArray[4];
        this.hasError = true;
      }
    }


    if(pFromContinue){
      if(!this.checkIn){
        this.errorArray[this.errorArray.length] = moLanguageObjCiCo.errorArray[5];
        this.hasError = true;
      }
      else if(!this.checkOut){
        this.errorArray[this.errorArray.length] = moLanguageObjCiCo.errorArray[6];
        this.hasError = true;
      }
    }
    this.renderErrors(pFromContinue);
}
   
multiDisplayCalendar.prototype.validateSelectedDate = function(selectedDate) {
    var selectedRange = null;

    if(this.checkIn&&!this.checkOut) {
      if(selectedDate > this.checkIn) {
        selectedRange = this.getLengthOfStay(this.checkIn, selectedDate);
      }
      else {
        selectedRange = this.getLengthOfStay(selectedDate, this.checkIn);
      }
      if(selectedRange > this.settings.maxBookDays) {
        this.errorArray = new Array();
        this.errorArray[this.errorArray.length] = moLanguageObjCiCo.errorArray[0];
        this.hasError = true;
        this.renderErrors();
        return;
      }
    }

    if(this.checkOut&&!this.checkIn) {
      if(selectedDate > this.checkOut) {
        selectedRange = this.getLengthOfStay(this.checkOut,selectedDate);
      }
      else {
        selectedRange = this.getLengthOfStay(selectedDate,this.checkOut);
      }
      if(selectedRange>this.settings.maxBookDays) {
        this.errorArray = new Array();
        this.errorArray[this.errorArray.length] = moLanguageObjCiCo.errorArray[0];
        this.hasError = true;
        this.renderErrors();
        return;
      }
    }

    if(this.checkIn && this.checkOut) {
      if(selectedDate < this.checkOut) {
        selectedRange = this.getLengthOfStay(selectedDate, this.checkOut);
      }
      else if(selectedDate > this.checkIn) {
        selectedRange = this.getLengthOfStay(selectedDate, this.checkIn);
      }
      if(selectedRange>this.settings.maxBookDays) {
        this.errorArray = new Array();
        this.errorArray[this.errorArray.length] = moLanguageObjCiCo.errorArray[0];
        this.hasError = true;
        this.renderErrors();
        return;
      }
    }
    this.errorMessageDiv.innerHTML = "";
    this.errorMessageDiv.style.visibility = 'hidden';
    this.hasError = false;
}

multiDisplayCalendar.prototype.renderErrors = function(pFromContinue){
//    this.validateCalendar(pFromContinue);

    if(this.hasError){
        var lsOutput = '';
        for(var i=0; i<this.errorArray.length; i++){
            lsOutput += '<div>' + this.errorArray[i] +' </div>'
        }
        this.errorMessageDiv.innerHTML = lsOutput;
        this.errorMessageDiv.style.visibility = 'visible';
    }
    else{
       this.errorMessageDiv.style.visibility = 'hidden';
       this.errorMessageDiv.innerHTML = '';
    }
}

multiDisplayCalendar.prototype.resetCheckInDate = function(e) {
  this.checkIn = null;
  this.calendarContainer.menuHolder.travelDateHolder.instructions.checkInInstructions.innerHTML = moLanguageObjCiCo.pickCheckInText;
  this.resetHightlight(this.calendarContainer);
  this.highlight();
  yuiDom.addClass(this.calendarContainer.menuHolder.travelDateHolder.instructions.checkInInstructions,"active");
  yuiDom.addClass(this.calendarContainer.menuHolder.travelDateHolder.instructions.checkInInstructions,"checkInInstructionsActive");
  yuiDom.removeClass(this.calendarContainer.menuHolder.travelDateHolder.instructions.checkOutInstructions,"active");
  yuiDom.removeClass(this.calendarContainer.menuHolder.travelDateHolder.instructions.checkOutInstructions,"checkOutInstructionsActive");
  yuiDom.removeClass(this.calendarContainer.menuHolder.travelDateHolder.instructions.submitInstructions,"active");
  yuiDom.removeClass(this.calendarContainer.menuHolder.travelDateHolder.instructions.submitInstructions,"submitInstructionsActive");
}

multiDisplayCalendar.prototype.resetCheckOutDate = function(e) {
  this.checkOut = null;
  this.calendarContainer.menuHolder.travelDateHolder.instructions.checkOutInstructions.innerHTML = moLanguageObjCiCo.pickCheckOutText;

  this.resetHightlight(this.calendarContainer);
  this.highlight();
  if(this.checkIn) {
    yuiDom.removeClass(this.calendarContainer.menuHolder.travelDateHolder.instructions.checkInInstructions,"active");
    yuiDom.removeClass(this.calendarContainer.menuHolder.travelDateHolder.instructions.checkInInstructions,"checkInInstructionsActive");
    yuiDom.addClass(this.calendarContainer.menuHolder.travelDateHolder.instructions.checkOutInstructions,"active");
    yuiDom.addClass(this.calendarContainer.menuHolder.travelDateHolder.instructions.checkOutInstructions,"checkOutInstructionsActive");
  }
  else {
    yuiDom.addClass(this.calendarContainer.menuHolder.travelDateHolder.instructions.checkInInstructions,"active");
    yuiDom.addClass(this.calendarContainer.menuHolder.travelDateHolder.instructions.checkInInstructions,"checkInInstructionsActive");
    yuiDom.removeClass(this.calendarContainer.menuHolder.travelDateHolder.instructions.checkOutInstructions,"active");
    yuiDom.removeClass(this.calendarContainer.menuHolder.travelDateHolder.instructions.checkOutInstructions,"checkOutInstructionsActive");
  }
  yuiDom.removeClass(this.calendarContainer.menuHolder.travelDateHolder.instructions.submitInstructions,"active");
  yuiDom.removeClass(this.calendarContainer.menuHolder.travelDateHolder.instructions.submitInstructions,"submitInstructionsActive");
}

multiDisplayCalendar.prototype.resetInstructionDisplay = function() {

  if(!this.checkOut) {
    yuiDom.removeClass(this.calendarContainer.menuHolder.travelDateHolder.instructions.checkOutInstructions,"active");
    yuiDom.removeClass(this.calendarContainer.menuHolder.travelDateHolder.instructions.checkOutInstructions,"checkOutInstructionsActive");
  }
  if(this.checkIn && this.checkOut) {
    yuiDom.removeClass(this.calendarContainer.menuHolder.travelDateHolder.instructions.submitInstructions,"active");
    yuiDom.removeClass(this.calendarContainer.menuHolder.travelDateHolder.instructions.submitInstructions,"submitInstructionsActive");
    if(this.calendarContainer.menuHolder.travelDateHolder.instructions.submitInstructions.continueButton.parentNode == this.calendarContainer.menuHolder.travelDateHolder.instructions.submitInstructions) {
      this.calendarContainer.menuHolder.travelDateHolder.instructions.submitInstructions.removeChild(this.calendarContainer.menuHolder.travelDateHolder.instructions.submitInstructions.continueButton);
    }
  }
  yuiDom.addClass(this.calendarContainer.menuHolder.travelDateHolder.instructions.checkInInstructions,"active");
  yuiDom.addClass(this.calendarContainer.menuHolder.travelDateHolder.instructions.checkInInstructions,"checkInInstructionsActive");

  this.calendarContainer.menuHolder.travelDateHolder.instructions.checkInInstructions.innerHTML = moLanguageObjCiCo.pickCheckInText;
  this.calendarContainer.menuHolder.travelDateHolder.instructions.checkOutInstructions.innerHTML = moLanguageObjCiCo.pickCheckOutText;

}

multiDisplayCalendar.prototype.reset = function() {
    this.resetInstructionDisplay(); // !!! important that this comes before object clearing
    this.clearObject();
    this.renderCalDisplay();
}

// Availability Calendar method to clear the calendar object, used in "Reset"
multiDisplayCalendar.prototype.clearObject = function(){
  this.hasError           = null;
  this.errorMessageDiv.innerHTML = "";
  this.errorMessageDiv.style.visibility = 'hidden';
  this.errorArray         = null;
  this.checkIn            = null;
  this.checkOut           = null;
  this.renderedMonths     = {};
}

multiDisplayCalendar.prototype.close = function() {
    this.toggleDisplay();
}

multiDisplayCalendar.prototype.submit = function(){
    this.renderErrors(true);
    if(!this.hasError){
      this.returnFunction(this.checkIn, this.checkOut, true);
      this.close();
    }
}
