﻿var showCalendarInLeft = true;

function Calendar(dA, coN) {

    this.isOpen = false;
    this.coN = coN;
    this.monthDaysCountArray = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
    this.monthNames = new Array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
    this.weekDays = new Array('S', 'M', 'T', 'W', 'T', 'F', 'S');

    this.currentData = new Date();
    this.currentData.setHours(0, 0, 0, 0);
    this.currentDay = this.currentData.getDate();
    this.currentMonth = this.currentData.getMonth();

    this.currentMonth = this.currentData.getMonth();
    this.d = dA;

    this.calendarSrc = "";

    this.checkinDate = new Date();
    this.checkinDateClicked = false;
    this.checkoutDate = new Date();
    this.checkoutDateClicked = false;
    this.goLeftPosition = 2;
    this.calendarTitle = "";

    this.Cal1Date = new Date();
    this.Cal2Date = new Date();

    this.OnReset = function() {
        this.checkinDate.setDate(this.currentData.getFullYear(), this.currentData.getMonth(), this.currentData.getDate());
        this.checkinDateClicked = false;
        this.checkoutDate.setDate(this.currentData.getFullYear(), this.currentData.getMonth(), this.currentData.getDate());
        this.checkoutDateClicked = false;
        this.goLeftPosition = 1;
        this.calendarSrc = this.calendarTitle;
        if (this.d.getMonth() == 0) {
            this.d.setFullYear(this.d.getFullYear() - 1, 12 - this.goLeftPosition, 1);
        }
        else {
            this.d.setMonth(this.d.getMonth() - this.goLeftPosition, 1);
        }
        this.ShowCalendars();
        this.goLeftPosition = 2;
    };

    this.OnMouseOverDay = function(y, m, d) {
        var oTd = document.getElementById(y + "," + m + "," + d);
        oTd.className = "checked_day";
    };

    this.OnMouseOutDay = function(y, m, d) {
        var oTd = document.getElementById(y + "," + m + "," + d);
        oTd.className = "";
    };

    this.OnDayClick = function(y, m, d) {
        var oTd = document.getElementById(y + "," + m + "," + d);
        this.checkinDate.setFullYear(y, m, d);
        this.checkinDate.setHours(0, 0, 0, 0);
        this.checkinDateClicked = true;
        oTd.className = "checked_day";
        OnCalendarClose(this);
        return;
    };

    this.ShowCalendars = function() {
        this.d.setHours(0, 0, 0, 0);
        calendarSrc = this.calendarTitle;
        this.RenderDubleCalendar(this.d);
        var oCalendarDiv = document.getElementById("calendar");
        oCalendarDiv.innerHTML = calendarSrc;
    };

    this.calendarWrite = function(src) {
        calendarSrc = calendarSrc + src;
    };

    this.GoRight = function() {
        this.calendarSrc = "<h1>" + this.calendarTitle + "</h1>";
        this.ShowCalendars();
    };

    this.GoLeft = function() {
        this.calendarSrc = "<h1>" + this.calendarTitle + "</h1>";
        if (this.d.getMonth() == 0) {
            this.d.setFullYear(this.d.getFullYear() - 1, 12 - this.goLeftPosition, 1);
        }
        else {
            this.d.setMonth(this.d.getMonth() - this.goLeftPosition, 1);
        }
        this.ShowCalendars();
    };

    this.RenderDubleCalendar = function(d) {
        this.Cal1Date = c1.checkinDate
        this.Cal2Date = c2.checkinDate
        this.RenderCalendar(d, true);
        if (this.d.getMonth() == 11) {
            this.d.setFullYear(this.d.getFullYear() + 1, 0, 1);
        }
        else {
            this.d.setMonth(this.d.getMonth() + 1, 1);
        }
        this.RenderCalendar(d, false);


        this.calendarWrite("<div class='clear'></div><div style='text-align:center; border-top:1px #c6e8f0 solid; padding-top:5px;'><a href='javascript:bodyClick(0);' style='font-size:12px; color:#0463a1; text-decoration:none;'><b>Close</b></a></div>");
    };

    this.RenderCalendar = function(d, rl) {
        var month = this.monthNames[this.d.getMonth()];
        var year = this.d.getFullYear();
        var nD = new Date();
        this.calendarWrite("<table cellpadding='0' cellspacing='2'>");

        this.calendarWrite("<tr>");
        if (rl) {
            if (nD.getMonth() == this.d.getMonth() && nD.getYear() == this.d.getYear()) {
                this.calendarWrite("<th colspan='7' class='th_month'><a href='javascript:void(0)' title='Previous month'><img src='" + UnsecureURL + "images/check-cal-left-arrow-passive.png' alt='' class='left_arrow' /></a>" + month + " " + year + "</th>");
            }
            else {
                this.calendarWrite("<th colspan='7' class='th_month'><a href='javascript:" + this.coN + ".GoLeft();' title='Previous month'><img src='" + UnsecureURL + "images/check-cal-left-arrow.png' alt='' class='left_arrow' /></a>" + month + " " + year + "</th>");
            }
        }
        else {
            this.calendarWrite("<th colspan='7' class='th_month'><a href='javascript:" + this.coN + ".GoRight();' title='Next month'><img src='" + UnsecureURL + "images/check-cal-right-arrow.png' alt='' class='right_arrow' /></a>" + month + " " + year + "</th>");
        }
        this.calendarWrite("</tr>");

        this.calendarWrite("<tr><th class='next_month'>su</th><th class='next_month'>mo</th><th class='next_month'>tu</th><th class='next_month'>we</th><th class='next_month'>th</th><th class='next_month'>fr</th><th class='next_month'>sa</th></tr>");
        var rows = 0;
        var monthDaysCount = this.monthDaysCountArray[this.d.getMonth()];
        if (this.d.getMonth() == 1) {
            if (this.d.getFullYear() % 400 == 0) {
                monthDaysCount++;
            }
            else if (this.d.getFullYear() % 100 == 0) {
                monthDaysCount--;
            }
            else if (this.d.getFullYear() % 4 == 0) {
                monthDaysCount++;
            }
        }
        for (sd = 1; sd <= monthDaysCount; sd++) {
            this.d.setDate(sd);
            if (this.d.getDay() == 0 || sd == 1) {
                if (sd <= 6) {
                    this.calendarWrite("<tr>");
                    rows++;
                }
                else {
                    this.calendarWrite("</tr>");
                    this.calendarWrite("<tr>");
                    rows++;
                }
            }
            if (sd == 1) {
                for (ec = 0; ec < this.d.getDay(); ec++) {
                    this.calendarWrite("<td>");
                    this.calendarWrite("&nbsp;");
                    this.calendarWrite("</td>");
                }
            }
            if (d < this.currentData) {
                this.calendarWrite("<td class='past_day'>");
                this.calendarWrite(this.d.getDate());
                this.calendarWrite("</td>");
            }
            else {
                if (this.checkinDateClicked) {
                    if (this.d.getFullYear() == this.checkinDate.getFullYear() && this.d.getMonth() == this.checkinDate.getMonth() && this.d.getDate() == this.checkinDate.getDate()) {
                        this.calendarWrite("<td class='checked_day' id='" + this.d.getFullYear() + "," + this.d.getMonth() + "," + this.d.getDate() + "' onclick='" + this.coN + ".OnDayClick(" + this.d.getFullYear() + "," + this.d.getMonth() + "," + this.d.getDate() + ")'>");
                        this.calendarWrite("<a href='javascript:void(0)'>");
                        this.calendarWrite(this.d.getDate());
                        this.calendarWrite("</a></td>");
                    }
                    else {
                        if (this.d > this.Cal1Date && this.d < this.Cal2Date && c1.checkinDateClicked && c2.checkinDateClicked) {
                            this.calendarWrite("<td class='selected_day' id='" + this.d.getFullYear() + "," + this.d.getMonth() + "," + this.d.getDate() + "' onclick='" + this.coN + ".OnDayClick(" + this.d.getFullYear() + "," + this.d.getMonth() + "," + this.d.getDate() + ")' >");
                        }
                        else {
                            if (this.Cal1Date < this.Cal2Date && ((this.d.getFullYear() == this.Cal1Date.getFullYear() && this.d.getMonth() == this.Cal1Date.getMonth() && this.d.getDate() == this.Cal1Date.getDate()) || (this.d.getFullYear() == this.Cal2Date.getFullYear() && this.d.getMonth() == this.Cal2Date.getMonth() && this.d.getDate() == this.Cal2Date.getDate())) && c1.checkinDateClicked && c2.checkinDateClicked) {
                                this.calendarWrite("<td class='checked_day' id='" + this.d.getFullYear() + "," + this.d.getMonth() + "," + this.d.getDate() + "' onclick='" + this.coN + ".OnDayClick(" + this.d.getFullYear() + "," + this.d.getMonth() + "," + this.d.getDate() + ")' >");
                            }
                            else {
                                this.calendarWrite("<td class='active_day' id='" + this.d.getFullYear() + "," + this.d.getMonth() + "," + this.d.getDate() + "' onclick='" + this.coN + ".OnDayClick(" + this.d.getFullYear() + "," + this.d.getMonth() + "," + this.d.getDate() + ")' >");
                            }
                        }
                        this.calendarWrite("<a href='javascript:void(0)'>");
                        this.calendarWrite(this.d.getDate());
                        this.calendarWrite("</a></td>");
                    }
                }
                else {
                    //this.calendarWrite("<td class='active_day' id='" + this.d.getFullYear() + "," + this.d.getMonth() + "," + this.d.getDate() + "' onclick='" + this.coN + ".OnDayClick(" + this.d.getFullYear() + "," + this.d.getMonth() + "," + this.d.getDate() + ")' >");
                    if (((c1.checkinDateClicked && this.d.getFullYear() == this.Cal1Date.getFullYear() && this.d.getMonth() == this.Cal1Date.getMonth() && this.d.getDate() == this.Cal1Date.getDate()) || (c2.checkinDateClicked && this.d.getFullYear() == this.Cal2Date.getFullYear() && this.d.getMonth() == this.Cal2Date.getMonth() && this.d.getDate() == this.Cal2Date.getDate()))) {
                        this.calendarWrite("<td class='checked_day' id='" + this.d.getFullYear() + "," + this.d.getMonth() + "," + this.d.getDate() + "' onclick='" + this.coN + ".OnDayClick(" + this.d.getFullYear() + "," + this.d.getMonth() + "," + this.d.getDate() + ")' >");
                    }
                    else {
                        this.calendarWrite("<td class='active_day' id='" + this.d.getFullYear() + "," + this.d.getMonth() + "," + this.d.getDate() + "' onclick='" + this.coN + ".OnDayClick(" + this.d.getFullYear() + "," + this.d.getMonth() + "," + this.d.getDate() + ")' >");
                    }
                    this.calendarWrite("<a href='javascript:void(0)'>");
                    this.calendarWrite(this.d.getDate());
                    this.calendarWrite("</a></td>");
                }
            }

            if (sd == monthDaysCount) {
                for (ec = 0; ec < 6 - this.d.getDay(); ec++) {
                    this.calendarWrite("<td>");
                    this.calendarWrite("&nbsp;");
                    this.calendarWrite("</td>");
                }
                this.calendarWrite("</tr>");
            }
        }
        while (rows < 6) {
            this.calendarWrite("<tr>");
            rows++;
            for (ec = 0; ec <= 6; ec++) {
                this.calendarWrite("<td>");
                this.calendarWrite("&nbsp;");
                this.calendarWrite("</td>");
            }
            this.calendarWrite("</tr>");
        }

        this.calendarWrite("</table>");

    };
}

/**
* DHTML date validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
*/
// Declaring valid date character, minimum year and maximum year
var dtCh = "/";
var minYear = 1900;
var maxYear = 2100;

function isInteger(s) {
    var i;
    for (i = 0; i < s.length; i++) {
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag) {
    var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++) {
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary(year) {
    // February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ((!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28);
}
function DaysArray(n) {
    var daysinmonth = new Array(n);
    for (var j = 0; j <= n; j++) {
        daysinmonth[j] = 0;
    }
    for (var i = 1; i <= n; i++) {
        daysinmonth[i] = 31
        if (i == 4 || i == 6 || i == 9 || i == 11) { daysinmonth[i] = 30 }
        if (i == 2) { daysinmonth[i] = 29 }        
    }
    return daysinmonth
}

function isDate(dtStr) {
    var daysInMonth = DaysArray(12)
    var pos1 = dtStr.indexOf(dtCh)
    var pos2 = dtStr.indexOf(dtCh, pos1 + 1)
    var strMonth = dtStr.substring(0, pos1)
    var strDay = dtStr.substring(pos1 + 1, pos2)
    var strYear = dtStr.substring(pos2 + 1)
    strYr = strYear
    if (strDay.charAt(0) == "0" && strDay.length > 1) strDay = strDay.substring(1)
    if (strMonth.charAt(0) == "0" && strMonth.length > 1) strMonth = strMonth.substring(1)
    for (var i = 1; i <= 3; i++) {
        if (strYr.charAt(0) == "0" && strYr.length > 1) strYr = strYr.substring(1)
    }
    month = parseInt(strMonth)
    day = parseInt(strDay)
    year = parseInt(strYr)
    if (pos1 == -1 || pos2 == -1) {
        //alert("The date format should be : mm/dd/yyyy")
        return false
    }
    if (strMonth.length < 1 || month < 1 || month > 12) {
        //alert("Please enter a valid month")
        return false
    }
    if (strDay.length < 1 || day < 1 || day > 31 || (month == 2 && day > daysInFebruary(year)) || day > daysInMonth[month]) {
        //alert("Please enter a valid day")
        return false
    }
    if (strYear.length != 4 || year == 0 || year < minYear || year > maxYear) {
        //alert("Please enter a valid 4 digit year between " + minYear + " and " + maxYear)
        return false
    }
    if (dtStr.indexOf(dtCh, pos2 + 1) != -1 || isInteger(stripCharsInBag(dtStr, dtCh)) == false) {
        //alert("Please enter a valid date")
        return false
    }
    return true
}

/*
End Calendar Block
*/


var c1 = new Calendar(new Date(), 'c1');
var c2 = new Calendar(new Date(), 'c2');

function OnPickDate(o) {
    if (o.isOpen) {
        OnCalendarClose(o);
    }
    else {
        OnPickDateTb(o);
    }
}

function OnPickDateTb(o) {
    try {
        closeDdls(0);
    }
    catch (e) { }    
    c1.isOpen = false;
    c2.isOpen = false;
    if (isDate(document.getElementById(c1.coN + "t").value)) {
        c1.checkinDate = new Date(document.getElementById(c1.coN + "t").value);
        c1.checkinDateClicked = true;
    }
    if (isDate(document.getElementById(c2.coN + "t").value)) {
        c2.checkinDate = new Date(document.getElementById(c2.coN + "t").value);
        c2.checkinDateClicked = true;
    }
    
    var to = document.getElementById(o.coN + "t");
    if (isDate(to.value)) {
        o.checkinDate = new Date(to.value);
        o.checkinDateClicked = true;
    }
    else {
        o.checkinDate = new Date();
        o.checkinDateClicked = false;
    }

    //if checkin
    if (o.coN == c1.coN) {
        if (isDate(to.value)) {
            o.d.setFullYear(o.checkinDate.getFullYear(), o.checkinDate.getMonth(), o.checkinDate.getDate());
        }
        else {
            o.d = new Date();
        }
    }
    //if checkout
    else if (o.coN == c2.coN) 
    {
        if (!c1.checkinDateClicked && !c2.checkinDateClicked) {
            o.d = new Date();
        }
        else if (c1.checkinDateClicked && !c2.checkinDateClicked) {
            o.d.setFullYear(c1.checkinDate.getFullYear(), c1.checkinDate.getMonth(), c1.checkinDate.getDate());
        }
        else if (!c1.checkinDateClicked && c2.checkinDateClicked) {
            o.d.setFullYear(c2.checkinDate.getFullYear(), c2.checkinDate.getMonth() - 1, c2.checkinDate.getDate());
        }
        else if (c1.checkinDateClicked && c2.checkinDateClicked) {
            if (c1.checkinDate.getMonth() == c2.checkinDate.getMonth()) {
                o.d.setFullYear(c1.checkinDate.getFullYear(), c1.checkinDate.getMonth(), c1.checkinDate.getDate());
            }
            else if (c1.checkinDate.getMonth() < c2.checkinDate.getMonth()) {
                o.d.setFullYear(c2.checkinDate.getFullYear(), c2.checkinDate.getMonth() - 1, c2.checkinDate.getDate());
            }
            else if (c1.checkinDate.getMonth() > c2.checkinDate.getMonth()) {
                o.d.setFullYear(c1.checkinDate.getFullYear(), c1.checkinDate.getMonth(), c1.checkinDate.getDate());
            }
        }
    }
    var oCalendarDiv = document.getElementById('calendar');
    oCalendarDiv.style.display = '';
    o.ShowCalendars();
    o.isOpen = true;
    var c = document.getElementById('calendar');
    var x = findPos(to)[0];
    var y = findPos(to)[1];
    if (showCalendarInLeft) {
        c.style.top = (y + to.clientHeight) + "px";
        c.style.marginLeft = x + "px";
    }
    else {
        c.style.top = (y + to.clientHeight + 4) + "px";
        c.style.marginLeft = x - 375 + "px";
    }
}

function OnCalendarClose(o) {
    var oCalendarDiv = document.getElementById('calendar');
    oCalendarDiv.style.display = 'none';
    if (o.checkinDateClicked) {
        document.getElementById(o.coN + "t").value = formatDate2(o.checkinDate, "MM/DD/YYYY");
        try {
            isChanged(o);
        }
        catch (e) { }
    }
    o.isOpen = false;
}

function f_clientWidth() {
    return f_filterResults(
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
	);
}
function f_clientHeight() {
    return f_filterResults(
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}


function f_scrollLeft() {
    return f_filterResults(
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
	);
}

function f_scrollTop() {
    return f_filterResults(
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}

function f_filterResults(n_win, n_docel, n_body) {
    var n_result = n_win ? n_win : 0;
    if (n_docel && (!n_result || (n_result > n_docel)))
        n_result = n_docel;
    return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}


function ClosCalendars(e) {
    try {
        e = e || event;
        if (e) {
            var oElement = document.elementFromPoint(e.clientX, e.clientY);
            var c = document.getElementById('calendar');

            var y = c.offsetTop - f_scrollTop();
            
            if (oElement) {
                if (oElement.id != "cb1" && oElement.id != "cb2" && oElement.id != "c1t" && oElement.id != "c2t"
                && !(
                       e.clientX > c.offsetLeft && e.clientX < c.offsetLeft + c.clientWidth
                    && e.clientY > y && e.clientY < c.offsetTop + c.clientHeight)) {
                    var oCalendarDiv = document.getElementById('calendar');
                    oCalendarDiv.style.display = 'none';
                    c1.isOpen = false;
                    c2.isOpen = false;
                }
            }
            else {
                var oCalendarDiv = document.getElementById('calendar');
                oCalendarDiv.style.display = 'none';
                c1.isOpen = false;
                c2.isOpen = false;
            }
        }
        else {
            var oCalendarDiv = document.getElementById('calendar');
            oCalendarDiv.style.display = 'none';
            c1.isOpen = false;
            c2.isOpen = false;
        }

        
    }
    catch (e) {
        var oCalendarDiv = document.getElementById('calendar');
        oCalendarDiv.style.display = 'none';
        c1.isOpen = false;
        c2.isOpen = false;
    }
}

function findPos(obj) {
    var curleft = curtop = 0;
    if (obj.offsetParent) {
        curleft = obj.offsetLeft;
        curtop = obj.offsetTop;
        while (obj = obj.offsetParent) {
            curleft += obj.offsetLeft;
            curtop += obj.offsetTop;
        }
    }
    return [curleft, curtop];
   }

function setShowCalendarInLeft(value) {
	showCalendarInLeft = value;
   }
