﻿/// <reference name="~/static/Main.js"/>
///<reference path="~/static/scripts/Jquery.IntelliSense.js" />

function HDate(year, month, day) {
    this.year = year * 1; this.month = month * 1; this.day = day * 1;
    this.toString = function() { return this.day + "/" + this.month + "/" + this.year }
    this.getFullYear = function() { return year * 1 }; this.getMonth = function() { return month - 1 }; this.getDate = function() { return day * 1 };
}
function strToDate(str) {
    var year = 1;var month = 1;var day = 1;
    year = str / 10000000000;
    month = (str % 10000000000) / 100000000;
    day = (str % 100000000) / 1000000;
    return    parseInt(month)+ "/" + parseInt(day) + "/" + parseInt(year);
}
function IDate(day, date, hdate, hour, minute, holiday) { this.day = day * 1; this.date = date; this.hour = hour * 1; this.minute = minute * 1; this.hdate = hdate; if (day == 6 && holiday.split(",")[1] * 1 != 1) holiday = ',1'; if (!holiday) holiday = ",0"; this.holiday = holiday.split(","); this.dates = [hdate, new HDate(date.getFullYear(), date.getMonth() + 1, date.getDate())]; }
function nIDate(a, b, c, d, e, f, g, h, i, j) { return new IDate(a, new Date(b, c, d), new HDate(e, f, g), h, i, j) };
IDate.prototype = { yesterday: {},
    monthYear: function(cal) { return Dates.GetMonthName(this, cal, false) + " " + Dates.GetYear(this, cal, false); },
    greDate: function() { return Dates.DateToString(this, 1, 1) },
    numGreDate: function(notyear) { return this.dates[1].day + "/" + this.dates[1].month + (!notyear ? "/" + (this.dates[1].getFullYear() + "").substring(2) : ""); },
    shortGreDate: function() { return this.dates[1].day + " " + Dates.GMonths2[this.dates[1].month - 1] },
    short2GreDate: function() { return this.dates[1].day + " ב" + Dates.GMonths[this.dates[1].month - 1] },
    shortHebDate: function() { return Dates.HDateToMinString(this) },
    hebDate: function() { return Dates.HDateToString(this) },
    time: function() { return View.AddZero(this.hour * 1) + ":" + View.AddZero(this.minute); },
    miniDate: function(notime, noyear) {
        var sDay = ""; if (this.hdate.year == dDate.hdate.year || noyear) sDay = this.shortHebDate(); else sDay = this.hebDate();
        if (this.intDate() == dDate.intDate()) sDay = "היום"; if (this.intDate() == this.yesterday.intDate()) sDay = "אתמול"; if (this.intDate() == this.tomorrow.intDate()) sDay = "מחר";
        return sDay + (!notime ? " " + this.time() : "");
    },
    intDate: function() { return (this.dates[1].year + "" + View.AddZero(this.dates[1].month) + "" + View.AddZero(this.dates[1].day)) * 1 } 
}
Date.prototype.intDate = function() { return (this.getFullYear() + "" + View.AddZero(this.getMonth() + 1) + "" + View.AddZero(this.getDate())) * 1 }
var d = new Date(); d.setDate(d.getDate() - 1);
IDate.prototype.yesterday = new Date(d.getFullYear(), d.getMonth(), d.getDate());
d = new Date(); d.setDate(d.getDate() + 1)
IDate.prototype.tomorrow = new Date(d.getFullYear(), d.getMonth(), d.getDate());

delete d;

Dates = {
    CalNames: ["עברי", "לועזי"], DayChars: ["א", "ב", "ג", "ד", "ה", "ו", "ש"], DayNames: ["ראשון", "שני", "שלישי", "רביעי", "חמישי", "שישי", "שבת"], HMonths: ["תשרי", "חשוון", "כסלו", "טבת", "שבט", "אדר", "ניסן", "אייר", "סיוון", "תמוז", "אב", "אלול"], HMonths2: ["תשרי", "חשוון", "כסלו", "טבת", "שבט", "אדר א", "אדר ב", "ניסן", "אייר", "סיוון", "תמוז", "אב", "אלול"], GMonths: ["ינואר", "פברואר", "מרץ", "אפריל", "מאי", "יוני", "יולי", "אוגוסט", "ספטמבר", "אוקטובר", "נובמבר", "דצמבר", "-"], GMonths2: ["ינו", "פבו", "מרץ", "אפר", "מאי", "יונ", "יול", "אוג", "ספט", "אוק", "נוב", "דצמ", "-"],
    DateToString: function(date, useCal, cal) { if (!useCal) cal = CurrentCal; if (!cal) return Dates.HDateToString(date); else return date.dates[1].day + " " + Dates.GMonths2[date.dates[1].month - 1] + " " + (date.dates[1].getFullYear() + "").substring(2); },
    DateToStringNums: function(date, useCal, cal) { if (!useCal) cal = CurrentCal; if (!cal) return Dates.HDateToString(date); else return date.dates[1].day + "/" + date.dates[1].month + "/" + date.dates[1].getFullYear() },
    DateToMinString: function(date, useCal, cal) { if (!useCal) cal = CurrentCal; cal *= 1; if (!cal) return Dates.HDateToMinString(date); else return date.dates[1].day + " " + Dates.GMonths2[date.dates[1].month - 1] },
    GetMonthName: function(date, cal, greShort) {
        date = date.dates;
        if (!cal) {
            months = Dates.isHLeapYear(date[0].year) ? Dates.HMonths2 : Dates.HMonths;
            return months[date[0].month - 1]
        }
        else if (greShort) return Dates.GMonths2[date[1].getMonth()]; else return Dates.GMonths[date[1].getMonth()];
    },
    HDateToString: function(date) {
        return Dates.Gimatria(date.hdate.day, true) + " ב" + this.GetMonthName(date, 0, 0) + " " + Dates.Gimatria(date.hdate.year - 5000, true);
    },
    GetYear: function(date, cal, greShort) { if (!cal) return Dates.Gimatria(date.dates[0].year - 5000, 1); else { if (greShort) return date.dates[1].getFullYear().toString().substring(2); else return date.dates[1].getFullYear(); } },
    GetDay: function(date, cal, hebGeresh) { if (arguments.length == 2) hebGeresh = 1; if (!cal) return Dates.Gimatria(date.dates[0].day, hebGeresh); else return date.dates[1].getDate(); },
    HDateToMinString: function(date) { return Dates.GetDay(date, 0) + " " + Dates.GetMonthName(date, 0); },
    Gimatria: function(n, useQ) {
        var p = "";
        while (n >= 400) { p = p + "ת"; n = n - 400; }

        if (n >= 100) {
            p = p + "קרשת".substring(n / 100 - 1, n / 100);
            n = n % 100;
        }

        if (n >= 10) {
            if (n == 15 || n == 16) n = n - 9;
            if (n >= 10) p = p + "יכלמנסעפצ".substring(n / 10 - 1, n / 10);
            else p = p + "ט";
            n = n % 10;
        }

        if (n > 0) p = p + "אבגדהוזחט".substring(n - 1, n);
        if (useQ) p = p.length > 1 ? p.substring(0, p.length - 1) + "\"" + p.substring(p.length - 1) : p = p + "'";
        return p;
    },
    isHLeapYear: function(year) { y = year % 19; return (y == 0 || y == 3 || y == 6 || y == 8 || y == 11 || y == 14 || y == 17); },
    Get: function(start, end, cal, func) { ExecuteJS("/My/Calendar/DateData.aspx?act=range&saveWeek=1&start=" + start + "&end=" + end + "&cal=" + cal, func); },
    GetMonth: function(mdate, cal, func) {
        var date1 = "1/" + mdate.month + "/" + mdate.year;
        var date2 = "32/" + mdate.month + "/" + mdate.year;
        this.Get(date1, date2, cal, func);
    },
    CreateMinTable: function(movecommand, monthid, tblid, tbdid) {
        return '<div class=Left><div class=Year onclick=' + movecommand + '(Dates.Compare.NextYear)>&#9668;&#9668;</div><div class=Month onclick=' + movecommand + '(Dates.Compare.NextMonth)>&#9668;</div></div><div class=Right><div class=Year onclick=' + movecommand + '(Dates.Compare.BackYear)>&#9658;&#9658;</div><div class=Month onclick=' + movecommand + '(Dates.Compare.BackMonth)>&#9658;</div></div><div id=' + monthid + '></div><table id=' + tblid + '><thead><tr><th>א</th><th>ב</th><th>ג</th><th>ד</th><th>ה</th><th>ו</th><th>ש</th></tr></thead><tbody id=' + tbdid + '></tbody></table>';
    },
    FillMinTable: function(tbl, aDates, renderFunc, ClickFunc, cal) {
        $(tbl).empty();
        var CurrentDate = dDate;
        for (var i = 0; i < aDates.length; i++) {
            var today = dDate.dates[cal], html, currCell, currRow, rowIndex;
            var currday = aDates[i].dates[cal];
            currCell = document.createElement("td");

            if (!renderFunc) html = Dates.GetDay(aDates[i], cal, 0)
            else html = renderFunc(aDates[i], currCell);

            rowIndex = tbl.childNodes.length;
            if (rowIndex == 0) {
                currRow = document.createElement("tr");
                tbl.appendChild(currRow);
            }
            else {
                if (tbl.childNodes[rowIndex - 1].childNodes.length < 7)
                    currRow = tbl.childNodes[rowIndex - 1];
                else {
                    currRow = document.createElement("tr");
                    tbl.appendChild(currRow);
                }
            }
            var cc = $(currCell);
            cc.addClass("Day" + aDates[i].holiday[1] + (aDates[i].dates[cal].month != aDates[7].dates[cal].month ? ' Gray' : '')).addClass("tdDay" + aDates[i].intDate());
            if (currday.day == today.day && currday.month == today.month && currday.year == today.year) { cc.addClass('Today') }
            currCell.innerHTML = html;
            var f = function(d) { return function() { ClickFunc(d); } }
            currCell.onclick = f(aDates[i])
            currRow.appendChild(currCell);
        }

    }, Compare: {
        NextYear: function(date, cal) {
            var crdate = date.dates[cal]
            var year = crdate.getFullYear() + 1;
            var month = crdate.getMonth() + 1;

            if (!cal) {
                if (Dates.isHLeapYear(year - 1) && month > 6) month--;
                if (Dates.isHLeapYear(year) && month > 5) month++;
            }
            return new HDate(year, month)
        },
        BackYear: function(date, cal) {
            var crdate = date.dates[cal], year = crdate.getFullYear() - 1, month = crdate.getMonth() + 1;

            if (!cal) {
                if (Dates.isHLeapYear(year + 1) && month > 6) month--;
                if (Dates.isHLeapYear(year) && month > 5) month++;
            }
            return new HDate(year, month)
        },
        NextMonth: function(date, cal) {
            var crdate = date.dates[cal], year = crdate.getFullYear(), month = crdate.getMonth() + 1;

            if (cal) {
                if (month < 12) month++;
                else { month = 1; year++; }
            }
            else {
                if (Dates.isHLeapYear(year)) {
                    if (month < 13) month++;
                    else { month = 1; year++; }
                } else {
                    if (month != 12) month++;
                    else { month = 1; year++; } 
                }
            }
            return new HDate(year, month);
        },
        BackMonth: function(date, cal) {
            var crdate = date.dates[cal], year = crdate.getFullYear(), month = crdate.getMonth() + 1;
            if (cal == 1) {
                if (month > 1) month--;
                else { month = 12; year--; } 
            }
            else {
                if (month > 1) month--;
                else { year--; month = 12 + (Dates.isHLeapYear(year) * 1) }
            }
            return new HDate(year, month);
        } 
    }
}

/*function GetDateFromServer(str,el) {
a = str.split(",");
if (el==Date) a[1]--;
return new el(a[0],a[1],a[2]);
}
function GetTimeLenFromServer(str) {
a = str.split(":"); 
for (i=0;i<3;i++) if ((a[i]*1).toString()=="NaN") a[i]=0;
return new TimeLen(a[0],a[1],a[2]);
}*/


