﻿var Roi = {}
Roi.Javascript = {}

Roi.Javascript.CalendarHelper = function() {
    this.Months = new Array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
    this.MonthsMM = new Array('01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12');
    this.Days = new Array('31', '28', '31', '30', '31', '30', '31', '31', '30', '31', '30', '31');
    this.LeapDays = new Array('31', '29', '31', '30', '31', '30', '31', '31', '30', '31', '30', '31');
    this.CurrentDate = new Date();
    this.Today = new Date();
    this.AllowedGoNextMonth = false;
    this.Separator = "-";
    this.Value = "";
}
Roi.Javascript.CalendarHelper.prototype =
    {
        CalculateDate: function() {
            with (this) {
                var currentYear = CurrentDate.getFullYear();
                var currentMonth = CurrentDate.getMonth();
                var currentDays = (currentYear % 4 == 0) ? LeapDays[currentMonth] : Days[currentMonth];

                var currentFirstDate = CurrentDate;
                currentFirstDate.setDate(1);
                var currentFirstDay = currentFirstDate.getDay();

                var currentLastDayNum = parseInt(currentFirstDay, 10) + parseInt(currentDays, 10);
                var rows = (currentLastDayNum % 7 == 0) ? currentLastDayNum / 7 : parseInt(currentLastDayNum / 7, 10) + 1;

                var pFullYear = currentYear;
                var pMonth = currentMonth - 1;
                if (currentMonth == 0) {
                    pFullYear = currentYear - 1;
                    pMonth = 11;
                }
                var pDays = (pFullYear % 4 == 0) ? LeapDays[pMonth] : Days[pMonth];
                var todayDate = Today.getDate();
                return [rows, currentFirstDay, currentDays, pDays, todayDate];
            }
        },
        CanGoNextMonth: function() {
            with (this) {
                if (AllowedGoNextMonth)
                    return true;
                else {
                    var currentYear = CurrentDate.getFullYear();
                    var currentMonth = CurrentDate.getMonth();

                    var tYear = Today.getFullYear();
                    var tMonth = Today.getMonth();
                    if (currentYear < tYear)
                        return true;
                    else if (currentYear == tYear)
                        return tMonth > currentMonth;
                    else
                        return false;
                }
            }
        },
        SetPreviousMonth: function() {
            with (this) {
                var currentYear = CurrentDate.getFullYear();
                var currentMonth = CurrentDate.getMonth();
                var pFullYear = currentYear;
                var pMonth = currentMonth - 1;
                if (currentMonth == 0) {
                    pFullYear = currentYear - 1;
                    pMonth = 11;
                }
                CurrentDate.setFullYear(pFullYear);
                CurrentDate.setMonth(pMonth);
                return CalculateDate();
            }

        },
        SetNextMonth: function() {
            with (this) {
                var currentYear = CurrentDate.getFullYear();
                var currentMonth = CurrentDate.getMonth();
                var nFullYear = currentYear;
                var nMonth = currentMonth + 1;
                if (currentMonth == 11) {
                    nFullYear = currentYear + 1;
                    nMonth = 0;
                }
                CurrentDate.setFullYear(nFullYear);
                CurrentDate.setMonth(nMonth);
                return CalculateDate();
            }
        },
        DisplayMonthYear: function() {
            with (this) {
                return Months[CurrentDate.getMonth()] + Separator + CurrentDate.getFullYear();
            }
        },
        DisplayFullFormat: function(day) {
            with (this) {
                Value = CurrentDate.getFullYear() + Separator + (CurrentDate.getMonth() + 1) + Separator + day;
                return MonthsMM[CurrentDate.getMonth()] + Separator + (day > 9 ? day : ('0' + day)) + Separator + CurrentDate.getFullYear();
            }
        },
        DisplayToday: function() {
            with (this) {
                Value = Today.getFullYear() + Separator + (Today.getMonth() + 1) + Separator + Today.getDate();
                return MonthsMM[Today.getMonth()] + Separator + (Today.getDate() > 9 ? Today.getDate() : ('0' + Today.getDate())) + Separator + Today.getFullYear();
            }
        }
    }

Roi.Javascript.JavascriptHelper = function() { }
Roi.Javascript.JavascriptHelper.prototype =
    {
        Left: function(obj) {
            var left = 0;
            if (obj.offsetParent) {
                left = obj.offsetLeft;
                while (obj = obj.offsetParent)
                    left += obj.offsetLeft;
            }
            return left;
        },
        Top: function(obj) {
            var top = 0;
            if (obj.offsetParent) {
                top = obj.offsetTop;
                while (obj = obj.offsetParent)
                    top += obj.offsetTop;
            }
            return top;
        },
        EventObject: function(e) {
            if (!e)
                e = window.event;
            return e;
        },
        EventTarget: function(e) {
            var targe;
            if (e.target)
                target = e.target;
            else if (e.srcElement)
                target = e.srcElement;
            if (target.nodeType == 3)
                target = target.parentNode;
            return target;
        },
        IsChild: function(child, parent) {
            while (child) {
                if (child == parent)
                    return true;
                child = child.parentNode;
            }
            return false;
        }
    }

Roi.Javascript.CalendarStyle = function() { }
Roi.Javascript.CalendarStyle.prototype =
    {
        SetStyleForLink: function(obj) {
            obj.style.cursor = "pointer";
            obj.style.font = 'bold 12px Arial';
            obj.style.textAlign = "center";
            obj.style.color = '#0000FF';
            obj.style.textDecoration = 'underline';
        },
        SetStyleForDisplay: function(obj) {
            obj.style.font = 'bold 12px Arial';
            obj.style.textAlign = "center";
        },
        SetStyleForWeekAbbr: function(obj) {
            obj.style.background = "#ABABAB";
            obj.style.font = '12px Arial';
            obj.style.textAlign = "center";
            obj.style.width = "21px";
        },
        SetStyleForTable: function(obj) {
            obj.style.background = "#FFFFFF";
            obj.style.position = "absolute";
            obj.style.zIndex = "1";
            obj.style.borderCollapse = "collapse";
            obj.style.border = "1px solid #ABABAB";
            obj.style.display = "block";
        },
        SetSytleNotCurrentMonth: function(obj) {
            obj.style.background = '#C4D3EA';
            obj.style.font = '10px Arial';
            obj.style.color = '#ABABAB';
            obj.style.textAlign = 'center';
            obj.style.textDecoration = 'line-through';
            obj.style.border = '1px solid #6487AE';
            obj.style.cursor = 'default';
            obj.style.width = "20px";
            obj.style.height = "20px";
        },
        SetStyleCurrentMonthTillToday: function(obj) {
            obj.style.background = '#C4D3EA';
            obj.style.font = '10px Arial';
            obj.style.color = '#333333';
            obj.style.textAlign = 'center';
            obj.style.textDecoration = 'none';
            obj.style.border = '1px solid #6487AE';
            obj.style.cursor = 'pointer';
            obj.style.width = "20px";
            obj.style.height = "20px";
        },
        SetStyleCurrentMonthAfterToday: function(obj) {
            obj.style.background = '#C4D3EA';
            obj.style.font = '10px Arial';
            obj.style.color = '#333333';
            obj.style.textAlign = 'center';
            obj.style.textDecoration = 'line-through';
            obj.style.border = '1px solid #6487AE';
            obj.style.cursor = 'default';
            obj.style.width = "18px";
            obj.style.height = "18px";
        },
        SetStyleCurrentMonthToday: function(obj) {
            obj.style.background = '#99CC33';
            obj.style.font = '10px Arial';
            obj.style.color = '#333333';
            obj.style.textAlign = 'center';
            obj.style.textDecoration = 'none';
            obj.style.border = '1px solid #6487AE';
            obj.style.cursor = 'pointer';
            obj.style.width = "18px";
            obj.style.height = "18px";
        }
    }

Roi.Javascript.CalendarFrame = function() {
    this.Controls = { "Body": "body", "Table": "brTable" };
    this.WeekAbbr = new Array('Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa');
    this.Style = new Roi.Javascript.CalendarStyle();
    this.CalendarHelper = new Roi.Javascript.CalendarHelper();
    this.JavascriptHelper = new Roi.Javascript.JavascriptHelper();
    this.Owner = new Object();
}
Roi.Javascript.CalendarFrame.prototype =
    {
        CreateHeaderTable: function() {
            with (this) {
                var table = document.getElementById(Controls.Table);
                if (table === null) {
                    var body = document.getElementsByTagName(Controls.Body)[0];
                    table = document.createElement("table");
                    table.id = Controls.Table;
                    body.appendChild(table);
                }
                else
                    table.style.display = "block";

                table.setAttribute("cellpadding", "2");
                table.style.top = JavascriptHelper.Top(Owner) + Owner.offsetHeight + "px";
                table.style.left = JavascriptHelper.Left(Owner) + "px";
                Style.SetStyleForTable(table);

                var tbody = document.createElement("tbody");
                var tr = document.createElement("tr");
                tr.style.width = "100%";
                //the First Cell
                var cell = document.createElement("td");
                Style.SetStyleForLink(cell);
                cell.innerHTML = "&lt;&lt;";
                cell.onclick = function() {
                    var params = CalendarHelper.SetPreviousMonth();
                    document.getElementById("tDisplay").innerHTML = CalendarHelper.DisplayMonthYear();
                    CreateTableBody(table, params);
                }
                tr.appendChild(cell);
                //the Second Cell
                cell = document.createElement("td");
                cell.colSpan = "5";
                cell.setAttribute("id", "tDisplay");
                cell.innerHTML = CalendarHelper.DisplayMonthYear();
                Style.SetStyleForDisplay(cell);
                tr.appendChild(cell);
                //the Last Cell
                cell = document.createElement("td");
                Style.SetStyleForLink(cell);
                cell.innerHTML = "&gt;&gt;";
                cell.onclick = function() {
                    if (CalendarHelper.CanGoNextMonth()) {
                        var params = CalendarHelper.SetNextMonth();
                        document.getElementById("tDisplay").innerHTML = CalendarHelper.DisplayMonthYear();
                        CreateTableBody(table, params);
                    }
                }
                tr.appendChild(cell);
                tbody.appendChild(tr);

                tr = document.createElement("tr");
                tr.style.width = "100%";
                for (w in WeekAbbr) {
                    cell = document.createElement("td");
                    var text = document.createTextNode(WeekAbbr[w]);
                    Style.SetStyleForWeekAbbr(cell);
                    cell.appendChild(text);
                    tr.appendChild(cell);
                }
                tbody.appendChild(tr);
                table.appendChild(tbody);
            }
            return table;
        },
        CreateTableBody: function(table, params) {
            rows = params[0];
            firstDay = params[1];
            DaysInCurrentMonth = params[2];
            DaysInLastMonth = params[3];
            todayDate = params[4];

            while (table.rows.length > 2) {
                table.deleteRow(table.rows.length - 1);
            }

            with (this) {
                var tbody = table.tBodies[0];
                var previous = 0, current = 0, total = 0, next = 0;
                for (var row = 0; row < rows; row++) {
                    tr = document.createElement("tr");
                    for (var col = 0; col < 7; col++) {
                        cell = document.createElement("td");
                        if (total < firstDay) {
                            Style.SetSytleNotCurrentMonth(cell);
                            previous++;
                            var text = document.createTextNode(DaysInLastMonth - firstDay + previous);

                        }
                        else if (total < (DaysInCurrentMonth + firstDay) && current < DaysInCurrentMonth) {
                            current++;
                            var text = document.createTextNode(current);
                            if (CalendarHelper.CanGoNextMonth() || current <= todayDate) {
                                Style.SetStyleCurrentMonthTillToday(cell);

                                cell.onmouseover = function(e) {
                                    JavascriptHelper.EventTarget(JavascriptHelper.EventObject(e)).style.background = '#FFCC66';
                                };
                                cell.onmouseout = function(e) {
                                    JavascriptHelper.EventTarget(JavascriptHelper.EventObject(e)).style.background = "#C4D3EA";
                                };
                                cell.onclick = function(e) {
                                    table.style.display = "none";
                                    Owner.value = CalendarHelper.DisplayFullFormat(JavascriptHelper.EventTarget(JavascriptHelper.EventObject(e)).innerHTML);
                                };
                            }
                            else {
                                Style.SetStyleCurrentMonthAfterToday(cell);
                            }
                        }
                        else {
                            Style.SetSytleNotCurrentMonth(cell);
                            next++;
                            var text = document.createTextNode(next);

                        }
                        cell.appendChild(text);
                        tr.appendChild(cell);
                        total++;
                    }
                    tbody.appendChild(tr);
                }
                tr = document.createElement("tr");
                cell = document.createElement("td");
                Style.SetStyleForLink(cell);
                cell.colSpan = 7;
                cell.innerHTML = "Today";
                cell.onclick = function() {
                    Owner.value = CalendarHelper.DisplayToday();
                    table.style.display = "none";
                }
                tr.appendChild(cell);
                tbody.appendChild(tr);

                table.appendChild(tbody);
            }
        }
    }

Roi.Javascript.Registrar = function() { }
Roi.Javascript.Registrar.prototype =
    {
        OnClick: function(e) {
            var javascriptHelper = new Roi.Javascript.JavascriptHelper();
            var target = javascriptHelper.EventTarget(javascriptHelper.EventObject(e));
            var table = document.getElementById("brTable");
            if (parent) {
                if (!javascriptHelper.IsChild(target, table))
                    table.style.display = "none";
            }
        }
    }

Roi.Javascript.Calendar = function() {
    this.calendarFrame = new Roi.Javascript.CalendarFrame();
}
Roi.Javascript.Calendar.prototype =
    {
        Display: function(obj) {
            this.calendarFrame.Owner = obj;
            var table = this.calendarFrame.CreateHeaderTable();
            var params = this.calendarFrame.CalendarHelper.CalculateDate();
            this.calendarFrame.CreateTableBody(table, params);

            var registrar = new Roi.Javascript.Registrar();
            document.all ? document.attachEvent('onclick', registrar.OnClick) : document.addEventListener('click', registrar.OnClick, false);

        },
        Show: function(owner) {
            var calTab = document.getElementById("brTable");
            if (calTab && calTab.style.display == "block") {
                calTab.style.display = "none";
                if (owner != this.calendarFrame.Owner)
                    this.Display(owner);
            }
            else {
                this.Display(owner);

            }
        },
        ShowById: function(Id) {
            var owner = $(Id);
            var calTab = document.getElementById("brTable");
            if (calTab && calTab.style.display == "block") {
                calTab.style.display = "none";
                if (owner != this.calendarFrame.Owner)
                    this.Display(owner);
            }
            else {
                this.Display(owner);

            }
        }
    }

var calendar = new Roi.Javascript.Calendar();




