// declare and set module variables
var moCalendarManager = new CalendarManager();
// Static date constants for today (not overwritten by functions)
var mcCalsToDisplay = 2;
var mcMaxBookDays   = 30;
var mcDisplayLimit  = 549;
var mcToday         = new Date();
    mcToday         = new Date(mcToday.getFullYear(),mcToday.getMonth(),mcToday.getDate());
var mcMaxDisplay    = new Date();
    mcMaxDisplay    = mcMaxDisplay.setDate(mcToday.getDate() + mcDisplayLimit); //today + 549 days = 550 days to display

var moMonthDayCountArray = new Array(31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

var moLeftArrow = new Image();
var moRightArrow = new Image();
var moCloseX = new Image();
/*  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 moLanguageObj = {
        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'],
        calendarDayNameArray : ['Su','M','Tu','W','Th','F','Sa'],

        calendarErrorArray : [
                "Your stay cannot be more than one month.",
                "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 checkout date.",
                "Please select a checkout date."
        ],
        menuNotSelected : 'Not Selected',
        menuNoLOS : '--',
        menuTravelDatesTo : 'to',
        menuLOSUnit : 'nights',
        calendarClose : 'Close',
        calendarInstructionLabel :  '',
        calendarInstructionText : 'Click your check-in date, and then click on your check-out date.',
        calendarClear : 'Clear Calendar',
        calendarPrev : 'Previous',
        calendarNext : 'Next',
        menuTravelDates : 'Travel Dates:',
        menuLengthOfStay : 'Length of Stay:',
        calendarContinue : 'Continue'
};

function CalendarManager(){
    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;
}

// Availability Calendar Constructor - takes in an id as a handle, a date string for ci and a date string for co in yyyy-mm-dd format
function multiDisplayCalendar(pCalendarId, pCIDate, pCODate, pReturnFunction){
    if(moCalendarManager.calArray[pCalendarId]){
        var loCalendarObj = moCalendarManager.calArray[pCalendarId];
        loCalendarObj.ciDateInitial  = pCIDate;
        loCalendarObj.coDateInitial  = pCODate;
        loCalendarObj.initializeObject();
        return loCalendarObj;
    }
    else{
        this.calendarId     = pCalendarId;
        this.returnFunction = pReturnFunction;
        this.ciDateInitial  = pCIDate;
        this.coDateInitial  = pCODate;
        this.ciDate         = null;
        this.coDate         = null;
        this.calStartDate   = null;
        this.selectedLOS    = null;
        this.dateDisplayMode = null;

        this.display = 'none';
        this.calendarDivMonth = new Array();
        this.calendarTravelDates = null;
        this.calendarLengthOfStay = null;

        this.calendarTravelDates = null;
        this.calendarLengthOfStay = null;
        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.calendarIframe = null;
        this.calendarContainer = this.createFramework();
        moCalendarManager.addCalendar(this);
    }
}

multiDisplayCalendar.prototype.initializeObject = function(){
    this.ciDate = this.formatDateOnImport(this.ciDateInitial);
    this.coDate = this.formatDateOnImport(this.coDateInitial);
    this.calStartDate = this.getCalStartDate();
    this.dateDisplayMode = this.getDisplayMode();
    this.selectedLOS = this.getLengthOfStay();
    this.hasError = false;
}

multiDisplayCalendar.prototype.getCalStartDate = function(){
    var loStartDate = (this.ciDate) ? new Date(this.ciDate.getFullYear(),this.ciDate.getMonth(), 1) : new Date(mcToday.getFullYear(), mcToday.getMonth(), 1);
    return loStartDate;
}

multiDisplayCalendar.prototype.getDisplayMode = function(){
    var lsDisplayMode;
    if(this.ciDateInitial != '' && this.coDateInitial != ''){
        lsDisplayMode = 2;
    }
    else if(this.ciDateInitial != '' && this.coDateInitial == ''){
        lsDisplayMode = 1;
    }
    else{
        lsDisplayMode = 0;
    }
    return lsDisplayMode;
}

//Availability Calendar method to show and hide the coresponding div
multiDisplayCalendar.prototype.toggleDisplay = function(){
    this.display = (this.display == 'none') ? 'block' : 'none';
    this.calendarContainer.style.display = this.display;
    // set the iframe dimensions, needs to be done after the calendar is displayed
    if(this.display == 'block'){
        this.calendarIframe.width           = this.calendarContainer.offsetWidth;
        this.calendarIframe.height          = this.calendarContainer.offsetHeight;
        this.calendarIframe.style.zIndex    = this.calendarContainer.style.zIndex - 1;
    }
    this.calendarIframe.style.display   = this.display;
}

multiDisplayCalendar.prototype.setCalendarPosition = function(pCalendarIcon){
    this.calendarContainer.style.top    = fnGetOffsetTop(pCalendarIcon, this.calendarContainer) + 'px';
    this.calendarContainer.style.left   = fnGetOffsetLeft(pCalendarIcon, this.calendarContainer) + 'px';
    this.calendarIframe.style.top       = this.calendarContainer.style.top;
    this.calendarIframe.style.left      = this.calendarContainer.style.left;
}

//Availability Calendar method to clear the calendar object, used in "Reset"
multiDisplayCalendar.prototype.clearObject = function(){
    this.ciDateInitial      = '';
    this.coDateInitial      = '';
    this.ciDate             = null;
    this.coDate             = null;
    this.dateDisplayMode    = 0;
    this.selectedLOS        = null;
    this.hasError           = null;
    this.errorArray         = null;
    this.returnFunction(this.ciDate, this.coDate, false);
}

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";
        loCalenderHolderIframe.width = 0;
        loCalenderHolderIframe.height = 0;
        loCalenderHolderIframe.className = "calendarHolderIframe";
        this.calendarIframe = loCalenderHolderIframe;
        document.body.appendChild(this.calendarIframe);

    // create main calendar div
    var loCalenderHolder = document.createElement("div");
        loCalenderHolder.style.zIndex = "2000";
        loCalenderHolder.className = "calendarHolder";
        loCalenderHolder.id = "calendarHolder";
        document.body.appendChild(loCalenderHolder);
    // create close button
    var loClose = document.createElement("div");
        loClose.className = "closeButton";
        var loCloseAction = document.createElement("a");
            loCloseAction.href = "javascript:fnCloseCalendar('" + this.calendarId + "');";
            loCloseAction.title = moLanguageObj.calendarClose;
            lsText = moLanguageObj.calendarClose;
            loTextNode = document.createTextNode(lsText);
            loCloseAction.appendChild(loTextNode);
            var loCloseActionImg = document.createElement("img");
                loCloseActionImg.src = moCloseX.src;
                loCloseActionImg.border = '0';
                loCloseActionImg.alt = moLanguageObj.calendarClose;
            loCloseAction.appendChild(loCloseActionImg);
        loClose.appendChild(loCloseAction);
        loCalenderHolder.appendChild(loClose);
    // create instruction header
    var loInstructions = document.createElement("div");
        loInstructions.className = "calendarInstructions";
        var loInstructionLabel = document.createElement("div");
            loInstructionLabel.className = "calendarInstructionsLabel";
                lsText = moLanguageObj.calendarInstructionLabel;
                loTextNode = document.createTextNode(lsText);
            loInstructionLabel.appendChild(loTextNode);
            loInstructions.appendChild(loInstructionLabel);
        var loInstructionText = document.createElement("div");
                lsText = moLanguageObj.calendarInstructionText;
                loTextNode = document.createTextNode(lsText);
                loInstructionText.appendChild(loTextNode);
        loInstructions.appendChild(loInstructionText);
        loCalenderHolder.appendChild(loInstructions);
    // create the main calendar portion
    var loCalendarContainer = document.createElement("div");
        loCalendarContainer.className = "calendarOuterContainer";
        // Prev button
        var loPreviousButton = document.createElement("div");
            loPreviousButton.className = "calendarNavigation";
            var loPrevActionHolder = document.createElement("div");
                loPrevActionHolder.id = "calendarNavigationPrev";
                var loPrevAction = document.createElement("a");
                    this.calendarPreviousAction = "javascript:fnChangeMonth('" + this.calendarId + "',-1);";
                    loPrevAction.className = "calendarNavigationPrevActive"
                    loPrevAction.href = this.calendarPreviousAction;
                    loPrevAction.title = moLanguageObj.calendarPrev;
                    this.calendarPrevious = loPrevAction;
                    var loPrevActionImg = document.createElement("img");
                        loPrevActionImg.src = moLeftArrow.src;
                        loPrevActionImg.border = '0';
                        loPrevActionImg.alt = moLanguageObj.calendarPrev;
                    loPrevAction.appendChild(loPrevActionImg);
                loPrevActionHolder.appendChild(loPrevAction);
            loPreviousButton.appendChild(loPrevActionHolder);
            //this.calendarPrevious = loPreviousButton;
            loCalendarContainer.appendChild(loPreviousButton);
        // container
        var loCalendarInnerContainer = document.createElement("div");
            loCalendarInnerContainer.className = "calendarInnerContainer";
            /* ****************CAL BODY HERE******************* */
            // left cal
            var loLeftCal = document.createElement("div");
                loLeftCal.className = "calendarDiv";
                loCalendarInnerContainer.appendChild(loLeftCal);
                this.calendarDivMonth[0] = loLeftCal;
            // spacer div
            var loSpacerDiv = document.createElement("div");
                loSpacerDiv.className = "spacerDiv";
                loCalendarInnerContainer.appendChild(loSpacerDiv);
            // right cal
            var loRightCal = document.createElement("div");
                loRightCal.className = "calendarDiv";
                loCalendarInnerContainer.appendChild(loRightCal);
                this.calendarDivMonth[1] = loRightCal;
            // clear div
            var loClearDiv1 = document.createElement("div");
                loClearDiv1.className = "divClear";
                loCalendarInnerContainer.appendChild(loClearDiv1);
            // clear calendar
            var loClearCal = document.createElement("div");
                loClearCal.className = "resetDates";
                var loResetAction = document.createElement("a");
                    loResetAction.href = "javascript:fnResetCalendar('" + this.calendarId + "');";
                    loResetAction.title = moLanguageObj.calendarClear;
                    lsText = moLanguageObj.calendarClear;
                    loTextNode = document.createTextNode(lsText);
                    loResetAction.appendChild(loTextNode);
                loClearCal.appendChild(loResetAction);
                loCalendarInnerContainer.appendChild(loClearCal);
            loCalendarContainer.appendChild(loCalendarInnerContainer);
            /* ****************END CAL BODY******************** */
        // Next button
        var loNextButton = document.createElement("div");
            loNextButton.className = "calendarNavigation";
            var loNextActionHolder = document.createElement("div");
                loNextActionHolder.id = "calendarNavigationNext";
                var loNextAction = document.createElement("a");
                    this.calendarNextAction = "javascript:fnChangeMonth('" + this.calendarId + "',1);";
                    loNextAction.href = this.calendarNextAction;
                    loNextAction.className = "calendarNavigationNextActive"
                    loNextAction.title = moLanguageObj.calendarNext;
                    this.calendarNext = loNextAction;
                    var loNextActionImg = document.createElement("img");
                        loNextActionImg.src = moRightArrow.src;
                        loNextActionImg.border = '0';
                        loNextActionImg.alt = moLanguageObj.calendarNext;
                    loNextAction.appendChild(loNextActionImg);
                loNextActionHolder.appendChild(loNextAction);
            loNextButton.appendChild(loNextActionHolder);
            //this.calendarNext = loNextButton;
            loCalendarContainer.appendChild(loNextButton);
        // clear div
        var loClearDiv2 = document.createElement("div");
            loClearDiv2.className = "divClear";
            loCalendarContainer.appendChild(loClearDiv2);
    loCalenderHolder.appendChild(loCalendarContainer);
    // create travel dates display
    var loMenuHolder = document.createElement("div");
        loMenuHolder.className = 'menuHolder';
        var loTravelDateHolder = document.createElement("div");
            loTravelDateHolder.className = "travelDateHolder";
            var loTravelDateLabel = document.createElement("div");
                loTravelDateLabel.className = "travelDateLabel";
                    lsText = moLanguageObj.menuTravelDates + ' ';
                    loTextNode = document.createTextNode(lsText);
                    loTravelDateLabel.appendChild(loTextNode);
                loTravelDateHolder.appendChild(loTravelDateLabel);
            var loTravelDates = document.createElement("div");
                loTravelDates.className = "travelDates";
                loTravelDates.id = "travelDates";
                this.calendarTravelDates = loTravelDates;
                loTravelDateHolder.appendChild(loTravelDates);
            var loClearDiv3 = document.createElement("div");
                loClearDiv3.className = "divClear";
                loTravelDateHolder.appendChild(loClearDiv3);
            var loLengthOfStayLabel = document.createElement("div");
                loLengthOfStayLabel.className = "travelDateLabel";
                    lsText = moLanguageObj.menuLengthOfStay + ' ';
                    loTextNode = document.createTextNode(lsText);
                    loLengthOfStayLabel.appendChild(loTextNode);
                loTravelDateHolder.appendChild(loLengthOfStayLabel);
            var loLengthOfStay = document.createElement("div");
                loLengthOfStay.className = "lengthOfStay";
                loLengthOfStay.id = "lengthOfStay";
                this.calendarLengthOfStay = loLengthOfStay;
                loTravelDateHolder.appendChild(loLengthOfStay);
        loMenuHolder.appendChild(loTravelDateHolder);
        // create close button
        var loContinueButton = document.createElement("div");
            loContinueButton.className = "continueButton";
            var loContinueAction = document.createElement("a");
                loContinueAction.className = "button_a";
                loContinueAction.href = "javascript:fnContinue('" + this.calendarId + "');";
                loContinueAction.title = moLanguageObj.calendarContinue;
                lsText = moLanguageObj.calendarContinue;
                loTextNode = document.createTextNode(lsText);
                loContinueAction.appendChild(loTextNode);
            loContinueButton.appendChild(loContinueAction);
            this.calendarContinue = loContinueButton;
        loMenuHolder.appendChild(loContinueButton);
        var loClearDiv4 = document.createElement("div");
            loClearDiv4.className = "divClear";
            loMenuHolder.appendChild(loClearDiv4);
    loCalenderHolder.appendChild(loMenuHolder);
    // create error display
    var loError = document.createElement("div");
        loError.className = "calendarError";
        loError.id = "calendarError";
        this.errorMessageDiv = loError;
        loCalenderHolder.appendChild(loError);

    return loCalenderHolder;
}

//Availability Calendar method to render the calendar, different results are shown based on the display mode
multiDisplayCalendar.prototype.renderCalendar = function(){
    for(var calIndex=0; calIndex<mcCalsToDisplay; calIndex++){
        var lsOutput        = '';
        var lvColumnIndex   = 0;
        var lvWorkingDate   = new Date(this.calStartDate);
        lvWorkingDate       = new Date(lvWorkingDate.setMonth(lvWorkingDate.getMonth() + calIndex));
        var loCurrentMonth  = lvWorkingDate.getMonth();
        var loCurrentYear   = lvWorkingDate.getFullYear();
        var lvCurrentDay    = new Date(loCurrentYear,loCurrentMonth,1).getDay();

        // Construct calendar table
        lsOutput += '<div class="CalendarTable">'
            // Header
        lsOutput += '<div class="month">' + moLanguageObj.calendarMonthNameArray[loCurrentMonth];
        lsOutput += ' ' + loCurrentYear;
        lsOutput += '</div>';
            // Day Names
        lsOutput += '<div class="CalendarTableInner">';
        for (i=0; i<7; i++){
            lsOutput += '<div class="daysoftheweek">'+ moLanguageObj.calendarDayNameArray[i] +'</div>';
        }
        // Calendar Days
        lsOutput += '<div class="divClear">&nbsp;</div>';
        // Figure out if it is a leap year and set accordingly
        moMonthDayCountArray[1] = (((loCurrentYear % 4 == 0) && (loCurrentYear % 100 != 0)) || (loCurrentYear % 400 == 0)) ? 29 : 28;
        for (i=0; i<lvCurrentDay; i++, lvColumnIndex++){
            lsOutput += '<div class="emptyDay">&nbsp;</div>';
        }

        switch (this.dateDisplayMode){
            case 0:
                for (var i=0; i<=moMonthDayCountArray[loCurrentMonth]-1; i++, lvColumnIndex++) {
                    var loCurrentDate = new Date(loCurrentYear,loCurrentMonth,1);
                    loCurrentDate = new Date(loCurrentDate.setDate(loCurrentDate.getDate() + i));
                    var lsTempOutput = '';
                    if(loCurrentDate < mcMaxDisplay){ // day is valid
                        lsTempOutput = '<div class="available" onclick="fnSelectDay(\''+ this.calendarId +'\',\''+ loCurrentDate.getFullYear() + '/' + (loCurrentDate.getMonth()+1) + '/' + loCurrentDate.getDate() + '\');"><a href="javascript:void(0);">' + loCurrentDate.getDate() +'</a></div>';
                    }
                    else{ //day is past the book limit
                        lsTempOutput = '<div class="emptyDay">' + loCurrentDate.getDate() +'</div>';
                    }
                    if(loCurrentDate < mcToday){ // day is less than today
                        lsTempOutput = '<div class="pastday">' + loCurrentDate.getDate() +'</div>';
                    }
                    lsOutput += lsTempOutput;
                    if (lvColumnIndex == 6) {
                        lsOutput += '<div class="divClear">&nbsp;</div>';
                        lvColumnIndex = -1;
                    }
                }
                break;
            case 1:
                 var loSelectedCheckInDate = this.ciDate;
                 for (var i=0; i<=moMonthDayCountArray[loCurrentMonth]-1; i++, lvColumnIndex++) {
                    var loCurrentDate = new Date(loCurrentYear,loCurrentMonth,1);
                    loCurrentDate = new Date(loCurrentDate.setDate(loCurrentDate.getDate() + i));
                    var lsTempOutput = '';
                    if(loCurrentDate < mcMaxDisplay){ // day is valid
                        lsTempOutput = '<div class="available" onclick="fnSelectDay(\''+ this.calendarId +'\',\''+ loCurrentDate.getFullYear() + '/' + (loCurrentDate.getMonth()+1) + '/' + loCurrentDate.getDate() + '\');"><a href="javascript:void(0);">' + loCurrentDate.getDate() +'</a></div>';
                    }
                    else{ //day is past the book limit
                        lsTempOutput = '<div class="emptyDay">' + loCurrentDate.getDate() +'</div>';
                    }
                    if(loCurrentDate.getTime() == loSelectedCheckInDate.getTime()){
                        lsTempOutput = '<div class="checkInOut"><a href="javascript:void(0);">' + loCurrentDate.getDate() +'</a></div>';
                    }
                    if(loCurrentDate < mcToday){ // day is less than today
                        lsTempOutput = '<div class="pastday">' + loCurrentDate.getDate() +'</div>';
                    }
                    lsOutput += lsTempOutput;
                    if (lvColumnIndex == 6) {
                        lsOutput += '<div class="divClear">&nbsp;</div>';
                        lvColumnIndex = -1;
                    }
                }
                break;
            case 2:
                var loSelectedCheckInDate = this.ciDate;
                var loSelectedCheckOutDate = this.coDate;
                for (var i=0; i<=moMonthDayCountArray[loCurrentMonth]-1; i++, lvColumnIndex++) {
                    var loCurrentDate = new Date(loCurrentYear,loCurrentMonth,1);
                    loCurrentDate = new Date(loCurrentDate.setDate(loCurrentDate.getDate() + i));
                    var lsTempOutput = '';
                    if (loCurrentDate.getTime() == loSelectedCheckInDate.getTime()){ // day is check in date
                        lsTempOutput = '<div class="checkInOut" onclick="fnSelectDay(\''+ this.calendarId +'\',\''+ loCurrentDate.getFullYear() + '/' + (loCurrentDate.getMonth()+1) + '/' + loCurrentDate.getDate() + '\');"><a href="javascript:void(0);">' + loCurrentDate.getDate() +'</a></div>';
                    }
                    else if (loCurrentDate.getTime() == loSelectedCheckOutDate.getTime()){ // day is check out date
                        lsTempOutput = '<div class="checkInOut" onclick="fnSelectDay(\''+ this.calendarId +'\',\''+ loCurrentDate.getFullYear() + '/' + (loCurrentDate.getMonth()+1) + '/' + loCurrentDate.getDate() + '\');"><a href="javascript:void(0);">' + loCurrentDate.getDate() +'</a></div>';
                    }
                    else if (loCurrentDate.getTime() > loSelectedCheckInDate.getTime() && loCurrentDate.getTime() < loSelectedCheckOutDate.getTime() ){ // day is a booked date
                        lsTempOutput = '<div class="booked" onclick="fnSelectDay(\''+ this.calendarId +'\',\''+ loCurrentDate.getFullYear() + '/' + (loCurrentDate.getMonth()+1) + '/' + loCurrentDate.getDate() + '\');"><a href="javascript:void(0);">' + loCurrentDate.getDate() +'</a></div>';
                    }
                    else{
                        if(loCurrentDate < mcMaxDisplay){ // day is valid
                            lsTempOutput = '<div class="available" onclick="fnSelectDay(\''+ this.calendarId +'\',\''+ loCurrentDate.getFullYear() + '/' + (loCurrentDate.getMonth()+1) + '/' + loCurrentDate.getDate() + '\');"><a href="javascript:void(0);">' + loCurrentDate.getDate() +'</a></div>';
                        }
                        else{ //day is past the book limit
                            lsTempOutput = '<div class="emptyDay">' + loCurrentDate.getDate() +'</div>';
                        }
                    }
                    if(loCurrentDate < mcToday){ // day is less than today
                        lsTempOutput = '<div class="pastday">' + loCurrentDate.getDate() +'</div>';
                    }
                    lsOutput += lsTempOutput;
                    if (lvColumnIndex == 6) {
                        lsOutput += '<div class="divClear">&nbsp;</div>';
                        lvColumnIndex = -1;
                    }
                }
                break;
        }
        if (lvColumnIndex > 0) {
            for (i=1; lvColumnIndex<7; i++, lvColumnIndex++){
                lsOutput += '<div class="emptyDay">&nbsp;</div>';
            }
        }
        lsOutput += '</div></div>';
        //Write the newly constructed table to the div on the page
        this.calendarDivMonth[calIndex].innerHTML = lsOutput;
    }
}

multiDisplayCalendar.prototype.renderTravelDates = function(){
    var lsOutput = '';
    var lsCIDisplay = fnGetDisplayDate(this.ciDate);
    var lsCODisplay = fnGetDisplayDate(this.coDate);

    lsOutput += (lsCIDisplay) ? lsCIDisplay : moLanguageObj.menuNotSelected;
    lsOutput += '<div class="calendarStandard">&nbsp;' + moLanguageObj.menuTravelDatesTo + '&nbsp;</div>';
    lsOutput += (lsCODisplay) ? lsCODisplay : moLanguageObj.menuNotSelected;
    // ouput the Travel Dates to the div
    this.calendarTravelDates.innerHTML = lsOutput;
}

multiDisplayCalendar.prototype.renderStayLength = function(){
    var lsOutput = (this.selectedLOS) ? this.selectedLOS + ' ' + moLanguageObj.menuLOSUnit : moLanguageObj.menuNoLOS;
    this.calendarLengthOfStay.innerHTML = lsOutput;
}

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.display = 'block';
    }
    else{
        this.errorMessageDiv.style.display = 'none';
        this.errorMessageDiv.innerHTML = '';
    }
}

multiDisplayCalendar.prototype.validateCalendar = function(pFromContinue){
    this.hasError = false;
    this.errorArray = new Array();
    if(this.ciDate){
        if(this.selectedLOS > mcMaxBookDays){
            this.errorArray[this.errorArray.length] = moLanguageObj.calendarErrorArray[0];
            this.hasError = true;
        }
        if(this.ciDate > mcMaxDisplay){
            this.errorArray[this.errorArray.length] = moLanguageObj.calendarErrorArray[1];
            this.hasError = true;
        }
        if(this.ciDate < mcToday){
            this.errorArray[this.errorArray.length] = moLanguageObj.calendarErrorArray[2];
            this.hasError = true;
        }
    }
    if(this.coDate){
        if(this.coDate > mcMaxDisplay && this.ciDate < mcMaxDisplay){
            this.errorArray[this.errorArray.length] = moLanguageObj.calendarErrorArray[1];
            this.hasError = true;
        }
        if(this.coDate < this.ciDate){
            this.errorArray[this.errorArray.length] = moLanguageObj.calendarErrorArray[3];
            this.hasError = true;
        }
        if(this.ciDate == this.coDate){
            this.errorArray[this.errorArray.length] = moLanguageObj.calendarErrorArray[4];
            this.hasError = true;
        }
    }
    if(pFromContinue){
        if(!this.ciDate){
            this.errorArray[this.errorArray.length] = moLanguageObj.calendarErrorArray[5];
            this.hasError = true;
        }
        else if(!this.coDate){
            this.errorArray[this.errorArray.length] = moLanguageObj.calendarErrorArray[6];
            this.hasError = true;
        }
    }
}

multiDisplayCalendar.prototype.setNavigation = function(){
    var lvWorkingDate   = new Date(this.calStartDate);
        lvWorkingDate   = new Date(lvWorkingDate.setMonth(lvWorkingDate.getMonth() + 1));
    var loMaxDisplay    = new Date(mcMaxDisplay);
    var loDisableArray = new Array(false, false);

    // Prev button
    if(this.calStartDate.getMonth() == mcToday.getMonth() && this.calStartDate.getFullYear() == mcToday.getFullYear()){
        this.calendarPrevious.className = 'calendarNavigationPrevDisabled';
        this.calendarPrevious.href = "javascript:void(0);";
    }
    else{
        this.calendarPrevious.className = 'calendarNavigationPrevActive';
        this.calendarPrevious.href = this.calendarPreviousAction;
    }
    // next button
    if(lvWorkingDate.getMonth() == loMaxDisplay.getMonth() && lvWorkingDate.getFullYear() == loMaxDisplay.getFullYear()){
        this.calendarNext.className = 'calendarNavigationNextDisabled';
        this.calendarNext.href = "javascript:void(0);";
    }
    else{
        this.calendarNext.className = 'calendarNavigationNextActive';
        this.calendarNext.href = this.calendarNextAction;
    }
}

multiDisplayCalendar.prototype.renderCalDisplay = function(){
    this.renderCalendar();
    this.setNavigation();
    this.renderTravelDates();
    this.renderStayLength();
    this.renderErrors();
}

multiDisplayCalendar.prototype.formatDateOnImport = function(pDate){
    var loDateObj = null;
    if(pDate != ''){
        var lsDateString = pDate;
        var loDateArray = new Array();
        loDateArray = lsDateString.split("-");
        loDateObj = new Date(loDateArray[1] + "/" + loDateArray[2] + "/" + new Number(loDateArray[0]));
    }
    return loDateObj;
}

multiDisplayCalendar.prototype.getLengthOfStay = function(){
    var lnLengthOfStay = null;
    if(this.ciDate && this.coDate){
        lnLengthOfStay = fnGetDateDifference(this.ciDate,this.coDate);
    }
    return lnLengthOfStay;
}

function fnCloseCalendar(pCalendarId){
    var loCalendarObj = moCalendarManager.calArray[pCalendarId];
    //loCalendarObj.clearObject();
    //loCalendarObj.initializeObject();
    loCalendarObj.toggleDisplay();
}

// function to select the day, different actions are taken based on the display mode (0-select check in, 1-select checkout,  2-detwermine if new date is Checkin or Checkout)
function fnSelectDay(pCalendarId, pNewDate) {
    var loCalendarObj = moCalendarManager.calArray[pCalendarId];
    var loSelectedDate = new Date(pNewDate);
    var lvMode = loCalendarObj.dateDisplayMode;

    switch(lvMode) {
        case 0:
            loCalendarObj.ciDate = loSelectedDate;
            loCalendarObj.dateDisplayMode = 1;
            break;
        case 1:
            if(loSelectedDate < loCalendarObj.ciDate){
                loCalendarObj.ciDate = loSelectedDate;
                loCalendarObj.dateDisplayMode = 1;
            }
            else{
                loCalendarObj.coDate = loSelectedDate;
                loCalendarObj.dateDisplayMode = 2;
            }
            break;
        case 2:
            if(loSelectedDate < loCalendarObj.ciDate){
                loCalendarObj.ciDate = loSelectedDate;
            }
            else if(loSelectedDate > loCalendarObj.coDate){
                loCalendarObj.coDate = loSelectedDate;
            }
            else{
                var loMidPoint = loCalendarObj.selectedLOS / 2;
                var loDateDifference = fnGetDateDifference(loCalendarObj.ciDate, loSelectedDate);
                if(loDateDifference >= loMidPoint){
                    loCalendarObj.coDate = loSelectedDate;
                }
                else{
                    loCalendarObj.ciDate = loSelectedDate;
                }
            }
            loCalendarObj.dateDisplayMode = 2;
            break;
    }
    loCalendarObj.selectedLOS = loCalendarObj.getLengthOfStay();
    loCalendarObj.renderCalDisplay();
}

function fnGetDateDifference(pCIDay, pCODay){
    var loStartDate = new Date(pCIDay);
    var loEndDate = new Date(pCODay);
    var lnDayDiff = Math.round((loEndDate.getTime() - loStartDate.getTime()) / (1000 * 60 * 60 * 24));
    return Math.abs(lnDayDiff);
}

// function to change the month.  The month buttons pass pDirection to tell the function to move up or down one month and a new calendar is drawn
function fnChangeMonth(pCalendarId,pDirection){
    var loCalendarObj = moCalendarManager.calArray[pCalendarId];
    var loNewStart = new Date(loCalendarObj.calStartDate);
    loCalendarObj.calStartDate = new Date(loNewStart.setMonth(loNewStart.getMonth() + pDirection));
    loCalendarObj.renderCalDisplay();
}

// function to select today.  Reset day/month/year to today from the constants above and call fnChangeDay to reset the styles
function fnResetCalendar(pCalendarId){
    var loCalendarObj = moCalendarManager.calArray[pCalendarId];
    loCalendarObj.clearObject();
    loCalendarObj.initializeObject();
    loCalendarObj.renderCalDisplay();
}

function fnGetDisplayDate(pDateToFormat){
    var lsFormatDate = null;
    if(pDateToFormat){
        var loDate = new Date(pDateToFormat);
        //var lsYear = loDate.getFullYear().toString().substring(2,4);
        var lsYear = loDate.getFullYear();
        var lsMonth = moLanguageObj.calendarShortMonthArray[loDate.getMonth()]
        lsFormatDate = loDate.getDate() + ' ' + lsMonth + ' ' + lsYear;
    }
    return lsFormatDate;
}

function fnContinue(pCalendarId){
    var loCalendarObj = moCalendarManager.calArray[pCalendarId];
    loCalendarObj.renderErrors(true);
    if(!loCalendarObj.hasError){
        // send back info call passed in function, pass the checkin and checkout date objs
        loCalendarObj.returnFunction(loCalendarObj.ciDate, loCalendarObj.coDate, true);
        fnCloseCalendar(loCalendarObj.calendarId);
    }
}

function fnFormatForExport(pDateToFormat){
    var loFormatDate = new Date(pDateToFormat);
    if(!isNaN(loFormatDate)){
        var lsDateString = loFormatDate.getFullYear() + '-' + padZero(loFormatDate.getMonth() +1) + '-' + padZero(loFormatDate.getDate());
        return lsDateString;
    }
}

function padZero(pNumber) {
    return (pNumber<0 || pNumber>9 ? "" : "0") + pNumber;
}

// function to determine the offsetLeft of an element that is passed in
function fnGetOffsetLeft (pElement, pDiv) {
    var lsLeftOffset = pElement.offsetLeft;
    while ((pElement = pElement.offsetParent) != null){
        lsLeftOffset  += pElement.offsetLeft;
    }
    var lsViewPort = document.documentElement.clientWidth;
    if(pDiv){
        if((pDiv.offsetWidth + lsLeftOffset) > (lsViewPort)){
            lsLeftOffset = (lsLeftOffset - ((lsLeftOffset + pDiv.offsetWidth) - lsViewPort) - 10);
        }
    }
    return lsLeftOffset;
}
// function to determine the offsetTop of an element that is passed in
function fnGetOffsetTop (pElement, pDiv) {
    var lsTopOffset = pElement.offsetTop;
    while ((pElement = pElement.offsetParent) != null){
        lsTopOffset +=pElement.offsetTop;
    }
    var lsDocHeight = document.documentElement.scrollTop;
    var lsViewPort = document.documentElement.clientHeight;
    if(pDiv){
        if((pDiv.offsetHeight + lsTopOffset) > (lsDocHeight + lsViewPort)){
            lsTopOffset = (lsTopOffset - ((lsTopOffset + pDiv.offsetHeight) - (lsDocHeight + lsViewPort)) - 10);
        }
    }
    return lsTopOffset;
}
