/**
 * NlsCalendar Pro 1.0
 * Copyright 2005-2007, addobject.com. All Rights Reserved
 * Author Jack Hermanto, www.addobject.com
 */
var $ao$DIM = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
var $ao$WD = [
    [$calI18n.d0, $calI18n.ds0],
    [$calI18n.d1, $calI18n.ds1],
    [$calI18n.d2, $calI18n.ds2],
    [$calI18n.d3, $calI18n.ds3],
    [$calI18n.d4, $calI18n.ds4],
    [$calI18n.d5, $calI18n.ds5],
    [$calI18n.d6, $calI18n.ds6]
];
var $ao$MIY = [
    [$calI18n.m01, $calI18n.ms01],
    [$calI18n.m02, $calI18n.ms02],
    [$calI18n.m03, $calI18n.ms03],
    [$calI18n.m04, $calI18n.ms04],
    [$calI18n.m05, $calI18n.ms05],
    [$calI18n.m06, $calI18n.ms06],
    [$calI18n.m07, $calI18n.ms07],
    [$calI18n.m08, $calI18n.ms08],
    [$calI18n.m09, $calI18n.ms09],
    [$calI18n.m10, $calI18n.ms10],
    [$calI18n.m11, $calI18n.ms11],
    [$calI18n.m12, $calI18n.ms12]
];
var $ao$HM = [$calI18n.AM, $calI18n.PM];
$ao$DMS = 86400000;
$cl = navigator.userAgent;
$clIE = ($cl.indexOf("MSIE") >= 0);
$clIE5 = ($cl.indexOf("MSIE 5.0") >= 0);
$clOpera = ($cl.indexOf("Opera") >= 0);
if ($clOpera) {
    $clIE = false;
    $clIE5 = false
};
$clSfri = ($cl.indexOf("Safari") >= 0);
AONumber = function (bs) {
    this.bs = bs;
    this.cs = 0;
    this.add = function (a, b) {
        var i = a + b;
        this.ca = 0;
        if (i < 0) {
            this.ca = -Math.ceil(-i / this.bs);
            return this.bs + i % this.bs
        }
        if (i >= this.bs) {
            this.ca = Math.ceil((i + 1 - this.bs) / this.bs);
            return i % this.bs
        }
        return i
    }
};
$ao$BS7 = new AONumber(7);
$ao$BS12 = new AONumber(12);
$ao$BS60 = new AONumber(60);
$ao$BS24 = new AONumber(24);
$aoCS = ["<table cellpadding='0' cellspacing='0' "];
aoCals = new Object();

function AOCalendar(cId) {
    this.cId = cId;
    this.rt = {};
    this.opt = {
        enableTime: false,
        firstDayOfWeek: 0,
        dtFormat: "$dm/$dd/$YY",
        themeDir: "themes/default/",
        cellspc: 0,
        mode: 0,
        sClose: true,
        sSelect: true,
        autoClose: true,
        autoUpdate: false,
        topSelDate: true,
        showAdjcDates: false,
        disableSel: false,
        selOnEvent: false,
        multiSel: false,
        sMonthDD: true,
        sYearDD: true,
        sMonthNv: true,
        sYearNv: true,
        sToday: true,
        yrMode: 1,
        yrRange: [0, 9999],
        yrInc: 2
    };
    this.dtDisabled = {};
    this.dtSpecials = {};
    this.dtEvents = {};
    this.setDate(new Date());
    this.tdyKey = (new Date()).$ao_key();;
    this.dtUrl = null;
    this.urlQry = "crDate=";
    this.ca = [];
    this.yr = [];
    aoCals[cId] = this
};
AOCAL = AOCalendar.prototype;
AOCalendar.CEvent = function (url, attr, data) {
    this.url = url;
    this.attr = attr;
    this.data = data
};
AOCalendar.$DIM = function (y, m) {
    if (m == 1) return (y % 4 == 0 ? 29 : 28);
    return $ao$DIM[m]
};
AOCAL.$calArray = function (d, m, y, fd) {
    this.cai = new Object();
    var md = AOCalendar.$DIM(y, m);
    var m1 = new Date(y, m, 1),
        m2 = this.getDateObj(),
        d1 = m1.getDay(),
        d2 = d1;
    var s = $ao$BS7.add(d1, -fd),
        v = 1;
    for (var i = s; i < s + md; i++) {
        m2.setDate(v);
        this.cai[m2.$ao_key()] = i;
        this.ca[i] = [v++, m2.$ao_clone(), !this.disabledDates(m2.$ao_key()), (this.selDate[m2.$ao_key()] != null), (d2 + i - s) % 7, true]
    }
    d1 = m1.$ao_add("D", -s).getDate();
    for (var i = 0; i < s; i++) {
        m1.setDate(d1);
        this.cai[m1.$ao_key()] = i;
        this.ca[i] = [d1++, m1.$ao_clone(), false, false, (7 + d2 + i - s) % 7, false]
    }
    m1 = new Date(y, m, 1);
    m1.$ao_add("M", 1);
    v = 1;
    for (var i = s + md; i < 42; i++) {
        m1.setDate(v);
        this.cai[m1.$ao_key()] = i;
        this.ca[i] = [v++, m1.$ao_clone(), false, false, (d2 + i - s) % 7, false]
    }
    return this.ca
};
AOCalendar.$yearArr = function (c, y) {
    var yr = [0],
        ty = [],
        n = c.opt.yrInc,
        f = 11;
    var r = c.opt.yrRange,
        s = 0;
    e = 0;
    if (c.opt.yrMode == 1) {
        if (y < r[0]) y = r[0];
        ty[f] = y;
        for (var i = 0; i < f; i++) {
            ty[f - i - 1] = y - (i + 1) * n
        }
        for (var i = 0; i < ty.length; i++) {
            if (ty[i] >= r[0]) {
                yr[yr.length] = ty[i]
            }
        }
        if (!yr[1] || yr[1] - n < r[0]) yr[0] = -1;
        else yr[0] = y - (f * 2 * n);
        s = yr.length;
        for (var i = s; i < 24; i++) {
            yr[i] = y + (i - s + 1) * n;
            if (yr[i] > r[1]) {
                yr[i] = -1;
                break
            }
        }
        if (i == 24) yr[i] = (yr[i - 1] + n > r[1] ? -1 : y + (f * 2 * n));
    }
    return yr
};
AOCalendar.$calHeader = function (cal) {
    var s = [],
        j = 0,
        o = cal.opt;
    s[j++] = $aoCS[0] + " id=\"" + cal.cId + "$header\" class='nlscalheader' width='100%'>";
    s[j++] = "<tr>" + (o.topSelDate ? "<td rowspan='2' class='nlscaltopseldate'>" + cal.date.getDate() + "</td>" : "") + "<td class='nlscaltitle' style='" + (o.mode == 0 ? "cursor:move;" : "") + "' onmousedown=\"$ao$ddmsdown(event, this, '" + cal.cId + "')\">" + $ao$MIY[cal.date.getMonth()][0] + ", " + cal.date.getFullYear(); + "</td></tr>";
    s[j++] = "<tr><td class='nlscalnav' style='cursor:pointer'>" + $aoCS[0] + " id='" + cal.cId + "$btns' width='100%'><tr>";
    var cs = "<td class='nlscalnavbtn' onmouseover=\"$ao$btnover(this)\" onmouseout=\"$ao$btnout(this)\" onclick=";
    var ss = " width='9%'><img src='" + o.themeDir,
        sd = "style=display:none";
    s[j++] = cs + "\"$ao$myclick('" + cal.cId + "','M',-1)\" title='" + $calI18n.ttPvMn + "' " + (o.sMonthNv ? "" : sd) + ss + "pm.gif'/></td>";
    s[j++] = cs + "\"$ao$monthdd('" + cal.cId + "')\" title='" + $calI18n.ttSelMn + "' " + (o.sMonthDD ? "" : sd) + ss + "mdd.gif'/></td>";
    s[j++] = cs + "\"$ao$myclick('" + cal.cId + "','M',1)\" title='" + $calI18n.ttNxMn + "' " + (o.sMonthNv ? "" : sd) + ss + "nm.gif'/></td>";
    s[j++] = cs + "\"$ao$today('" + cal.cId + "')\" width='28%' " + (o.sToday ? "" : sd) + ">" + $calI18n.today + "</td>";
    s[j++] = cs + "\"$ao$myclick('" + cal.cId + "','Y',-1)\"  title='" + $calI18n.ttPvYr + "' " + (o.sYearNv ? "" : sd) + ss + "py.gif'/></td>";
    s[j++] = cs + "\"$ao$yeardd('" + cal.cId + "')\" title='" + $calI18n.ttSelYr + "' " + (o.sYearDD ? "" : sd) + ss + "ydd.gif'/></td>";
    s[j++] = cs + "\"$ao$myclick('" + cal.cId + "','Y',1)\" title='" + $calI18n.ttNxYr + "' " + (o.sYearNv ? "" : sd) + ss + "ny.gif'/></td>";
    s[j++] = "</tr></table></td></tr>";
    s[j++] = "</table>";
    return s.join("");
};
AOCalendar.$calFooter = function (cal) {
    var s = [],
        j = 0;
    s[j++] = $aoCS[0] + " id=\"" + cal.cId + "$footer\" width='100%' height='100%'>";
    var sb = "<td class='nlscalsysbtn' onmouseover=\"$ao$sysover(this)\" onmouseout=\"$ao$sysout(this)\" onclick=";
    if (cal.opt.mode == 0 && cal.opt.sClose) s[j++] = sb + "\"$ao$close('" + cal.cId + "')\" title='" + $calI18n.btnClose + "'><img src='" + cal.opt.themeDir + "cls.gif' /></td>";
    s[j++] = "<td class='nlscalstatus' id='" + cal.cId + "$status'>&nbsp;</td>";
    if (cal.opt.mode == 0 && cal.opt.sSelect) s[j++] = sb + "\"$ao$select('" + cal.cId + "')\" title='" + $calI18n.btnSel + "'><img src='" + cal.opt.themeDir + "sel.gif' /></td>";
    s[j++] = "</table>";
    return s.join("");
};
AOCalendar.$calTime = function (cal) {
    var s = [],
        j = 0;
    s[j++] = $aoCS[0] + "id=\"" + cal.cId + "$time\" class='nlscaltime' width='100%'><tr><td align='center'>";
    s[j++] = $aoCS[0] + "><tr>";
    var tm = ["<td align='center' rowspan='2'><input type='text' class='nlscaltime_inp' maxlength='2' id='" + cal.cId, "<td class='nlscalud' onmouseover=\"$ao$udover(this);\" onmouseout=\"$ao$udout(this);\" onmousedown=\"$ao$tmAutoUpDown(aoCals." + cal.cId, " onmouseup=\"$ao$tmUpDown(aoCals." + cal.cId];
    var im = ["<img src='" + cal.opt.themeDir + "up.gif'/>", "<img src='" + cal.opt.themeDir + "down.gif'/>"];
    s[j++] = tm[0] + "$tmh' value='" + $aoPad("00", cal.date.getHours()) + "'/ onblur=\"$ao$valtime(aoCals." + cal.cId + ", this, 'tmh');\" title='" + $calI18n.bxHour + "'></td>" + tm[1] + ",'tmh',1)\"" + tm[2] + ",'tmh',1);\" valign='bottom' title='" + $calI18n.btnUp + "'>" + im[0] + "</td><td rowspan='2'>&nbsp;:&nbsp;</td>";
    s[j++] = tm[0] + "$tmm' value='" + $aoPad("00", cal.date.getMinutes()) + "'/ onblur=\"$ao$valtime(aoCals." + cal.cId + ", this, 'tmm');\" title='" + $calI18n.bxMinutes + "' ></td>" + tm[1] + ",'tmm',1)\"" + tm[2] + ",'tmm',1);\" valign='bottom' title='" + $calI18n.btnUp + "'>" + im[0] + "</td>";
    s[j++] = "</tr><tr>";
    s[j++] = tm[1] + ",'tmh',1)\"" + tm[2] + ",'tmh',-1)\" valign='top' title='" + $calI18n.btnDown + "'>" + im[1] + "</td>";
    s[j++] = tm[1] + ",'tmm',1)\"" + tm[2] + ",'tmm',-1)\" valign='top' title='" + $calI18n.btnDown + "'>" + im[1] + "</td>";
    s[j++] = "</tr></table></td></tr></table>";
    return s.join("");
};

function ao$getStyle(c, od, sd, sp) {
    var t = "";
    var spc = (typeof(sp) != "undefined" ? sp : c.specialDates(od.$ao_key()));
    if (spc) t += "_special";
    else if (sd[4] == 0 || sd[4] == 6) t += "_weekend";
    if (!sd[2]) {
        t += "_disabled"
    }
    return t
}
AOCalendar.$calHTML = function (cal) {
    var s = [],
        j = 0,
        d = null,
        t, dk = cal.date,
        spc = null,
        o = cal.opt,
        isTdy = false,
        a;
    cal.$calArray(dk.getDate(), dk.getMonth(), dk.getFullYear(), o.firstDayOfWeek);
    s[j++] = "<table width='100%' cellpadding='0' cellspacing='" + cal.opt.cellspc + "'>";
    s[j++] = "<tr>";
    for (var k = 0; k < 7; k++) {
        s[j++] = "<td class='nlscalday' style='cursor:default'>" + $ao$WD[$ao$BS7.add(o.firstDayOfWeek, k)][1] + "</td>"
    }
    s[j++] = "</tr>";
    for (var i = 0; i < 6; i++) {
        s[j++] = "<tr style='cursor:pointer;'>";
        for (var k = 0; k < 7; k++) {
            d = cal.ca[i * 7 + k];
            dk = d[1].$ao_key();
            isTdy = (dk == cal.tdyKey);
            s[j++] = "<td width='14.28%' class='";
            t = "nlscaldate";
            spc = cal.specialDates(dk);
            if (d[3] && !o.disableSel) t += "_sel";
            else {
                t += ao$getStyle(cal, d[1], d, spc);
            }
            s[j++] = t + "' ";
            s[j++] = "title='" + (spc ? spc.toString() : "") + "' ";
            s[j++] = "onmouseover=\"$aoCalMO(event, this, '" + cal.cId + "')\" onmouseout=\"$aoCalMT(event, this, '" + cal.cId + "')\" onclick=\"$aoCalCL(event, this, '" + cal.cId + "');\" ondblclick=\"$aoCalDbl(event, this, '" + cal.cId + "');\" ";
            s[j++] = " id='" + cal.cId + "$" + dk + "'>";
            a = (cal.dtUrl ? "<a href='" + $addParam(cal.dtUrl, cal.urlQry + dk) + "' class='nlscaldate_link'>" + d[0] + "</a>" : d[0]);
            if (cal.getEvents) spc = cal.getEvents(dk);
            else spc = null;
            if ((spc || isTdy) && d[5]) {
                s[j++] = $aoCS[0] + "style='height:100%;width:100%;'><tr><td class='" + (isTdy ? "nlscaldate_today" : "nlscaldate_event") + "'>";
                if (spc && spc.url) {
                    s[j++] = "<a class='nlscaldate_event_link' href='" + $addParam(spc.url, cal.urlQry + dk) + "' " + (spc.attr ? spc.attr : "") + ">" + d[0] + "</a>"
                } else {
                    s[j++] = a
                }
                s[j++] = "</td></tr></table>"
            } else {
                s[j++] = $aoCS[0] + "style='height:100%;width:100%;'><tr><td class='nlscaldate_normal'>" + (!o.showAdjcDates && !d[5] ? "&nbsp;" : a) + "</td></tr></table>"
            }
            s[j++] = "</td>"
        }
        s[j++] = "</tr>";
        d = cal.ca[(i + 1) * 7];
        if (d && !d[5] && !o.showAdjcDates) break



    }
    s[j++] = "</table>";
    return s.join("");
};
AOCalendar.$monthDD = function (cal) {
    var s = [],
        j = 0;
    s[j++] = $aoCS[0] + "id='" + cal.cId + "$mdd' width=\"100%\" style='display:none;cursor:pointer;'>";
    var ts = "<td class='nlsmydd_norm' onmouseover=\"$ao$myddover(this);\" onmouseout=\"$ao$myddout(this);\" onclick=\"$ao$myddclick(this, '" + cal.cId + "','@midx','M')\">@mdd</td>";
    for (var i = 0; i < 6; i++) {
        s[j++] = "<tr>" + ts.replace(/@mdd/gi, $ao$MIY[i][0]).replace(/@midx/gi, i) + ts.replace(/@mdd/gi, $ao$MIY[i + 6][0]).replace(/@midx/gi, i + 6) + "</tr>"
    }
    s[j++] = "</table>";
    return s.join("");
};
AOCalendar.$yearDD = function (cal, iy) {
    var s = [],
        j = 0,
        c1, c2, ym = cal.opt.yrMode;
    var fy = iy ? iy : cal.date.getFullYear(),
        yr = AOCalendar.$yearArr(cal, fy);
    s[j++] = $aoCS[0] + "width='100%' style='height:100%;cursor:pointer;'><tr>";
    c1 = ["<td class='nlsmydd_norm' onmouseover=\"$ao$myddover(this);\" onmouseout=\"$ao$myddout(this);\" onclick=\"$ao$yddpager(this, '" + cal.cId + "',", ")\">...</td>"];
    c2 = "<td class='nlsmydd_norm' style='cursor:default;'>...</td>";
    for (var i = 0; i < yr.length; i++) {
        if (i == 0 || i == yr.length - 1) {
            if (yr[i] == -1) s[j++] = c2;
            else {
                s[j++] = c1[0] + yr[i] + c1[1]
            }
        } else {
            s[j++] = "<td id='" + cal.cId + yr[i] + "' class='nlsmydd_norm' onmouseover=\"$ao$myddover(this);\" onmouseout=\"$ao$myddout(this);\" onclick=\"$ao$myddclick(this,'" + cal.cId + "'," + yr[i] + ",'Y')\">" + yr[i] + "</td>"
        }
        if ((i + 1) % 5 == 0) s[j++] = "</tr><tr>"
    }
    s[j++] = "</tr>";
    if (ym == 1) s[j++] = "<tr><td colspan='5' class='nlsmydd_norm'><table><tr><td class='nlsmydd_go'>" + $calI18n.litGo + "&nbsp;</td><td><input type='text' id='" + cal.cId + "$yrinp' class='nlsydd_inp' size=4 maxlength='4' /></td><td><input type='button' class='nlsydd_btn' value='" + $calI18n.btnGo + "' onclick=\"$ao$yddgo('" + cal.cId + "')\" /></td></tr></table></td></tr>";
    s[j++] = "</table>";
    return s.join("");
};
AOCalendar.$toHTML = function (cal) {
    var s = [],
        j = 0;
    s[j++] = $aoCS[0] + "class='nlscal'><tr><td>";
    s[j++] = $aoCS[0] + " width='100%'>";
    s[j++] = "<tr><td>" + AOCalendar.$calHeader(cal) + "</td></tr>";
    s[j++] = "<tr><td valign='top'>";
    s[j++] = "<div id='" + cal.cId + "$dates'>" + AOCalendar.$calHTML(cal) + "</div>";
    s[j++] = AOCalendar.$monthDD(cal);
    s[j++] = "<div id='" + cal.cId + "$ydd' style='display:none;'>" + AOCalendar.$yearDD(cal) + "</div>";
    s[j++] = "</td></tr>";
    if (cal.opt.enableTime) {
        s[j++] = "<tr><td>" + AOCalendar.$calTime(cal) + "</td></tr>"
    }
    s[j++] = "<tr><td class='nlscalfooter'>" + AOCalendar.$calFooter(cal) + "</td></tr>";
    s[j++] = "</table>";
    s[j++] = "</td></tr></table>";
    return s.join("");
};
AOCAL.render = function (plc) {
    var s = [],
        j = 0;
    s[j++] = "<div id='" + this.cId + "' " + (this.opt.mode == 0 ? "style='position:absolute;z-index:9000;visibility:hidden;'" : (!$clIE ? "style='position:relative'" : "")) + ">";
    s[j++] = "<div id='" + this.cId + "$shd' class='nlscalshield' style='display:none;width:100%;height:100%;position:absolute;'>&nbsp;</div>";
    s[j++] = AOCalendar.$toHTML(this);
    if ($clIE && !$clIE5 && this.opt.mode == 0) {
        s[j++] = "<iframe id='" + this.cId + "$fr' scrolling='no' frameborder=0 style='position:absolute;top:0px;left:0px;z-index:-1;' src='" + this.opt.themeDir + "blank.gif" + "'></iframe>"
    }
    s[j++] = "</div>";
    if (plc) {
        $aoGE(plc).innerHTML = s.join("");
    } else document.write(s.join(""));
    this.init();
};
AOCAL.init = function () {
    var rt = this.rt;
    var id = this.cId;
    rt.cal = $aoGE(id);
    rt.hdr = $aoGE(id + "$header");
    rt.dts = $aoGE(id + "$dates");
    rt.sts = $aoGE(id + "$status");
    rt.mdd = $aoGE(id + "$mdd");
    rt.ydd = $aoGE(id + "$ydd");
    rt.btns = $aoGE(id + "$btns");
    rt.frm = $aoGE(id + "$fr");
    rt.tmh = $aoGE(id + "$tmh");
    rt.tmm = $aoGE(id + "$tmm");
    rt.shd = $aoGE(id + "$shd");
    rt.cp = "dts";
    var me = this;
    if ($clIE || $clIE5) {
        window.attachEvent("onload", function () {
            $ao$initDim(me);
        });
    } else $ao$initDim(me);
};

function $ao$initDim(c) {
    var rt = c.rt;
    var tmp = ($clIE5 ? rt.cal.children(1) : rt.cal.childNodes[1]);
    rt.cW = tmp.offsetWidth;
    rt.cH = tmp.offsetHeight;
    tmp = rt.dts.parentNode.style;
    tmp.width = rt.dts.offsetWidth + "px";
    tmp.height = rt.dts.offsetHeight + "px";
    if (c.opt.mode != 1 && c.rt.frm) {
        tmp = c.rt.frm.style;
        tmp.width = rt.cW;
        tmp.height = rt.cH
    }
    if ($clIE) {
        rt.shd.style.height = rt.cH + "px";
        rt.shd.style.width = rt.cW + "px"
    }
}
AOCAL.reloadDate = function () {
    var de = this.rt.dts;
    de.innerHTML = AOCalendar.$calHTML(this);
    var mt = this.rt.hdr.rows[0],
        i = (this.opt.topSelDate ? 1 : 0),
        dt = this.date;
    mt.cells[i].innerHTML = $ao$MIY[dt.getMonth()][0] + ", " + dt.getFullYear();
    if (i == 1) mt.cells[0].innerHTML = dt.getDate();
    this.refreshTime();
};
AOCAL.reloadYear = function (iy) {
    this.rt.ydd.innerHTML = AOCalendar.$yearDD(this, iy);
};
AOCAL.refreshTime = function () {
    var rt = this.rt;
    if (rt.tmh) rt.tmh.value = $aoPad("00", this.date.getHours());
    if (rt.tmm) rt.tmm.value = $aoPad("00", this.date.getMinutes());
};
AOCAL.$IYR = function (f, v) {
    var fy = this.getDateObj().$ao_add(f, v).getFullYear();
    return this.isYearInRange(fy);
};
AOCAL.isYearInRange = function (y, a) {
    if (this.opt.yrMode == 1) {
        var r = this.opt.yrRange;
        if (y < r[0] || y > r[1]) {
            if (a == true) alert($calI18n.msgYrRange + r.join("-"));
            return false
        }
    }
    return true
};
AOCAL.dateAdd = function (f, v) {
    if (!this.$IYR(f, v)) return false;
    this.date.$ao_add(f, v);
    return true
};
AOCAL.inActiveMonth = function (d) {
    if (d.getMonth() == this.date.getMonth() && d.getFullYear() == this.date.getFullYear()) return true;
    return false
};
AOCAL.selectDate = function (dt) {
    var cd = this.getDateObj();
    var k = dt.$ao_key();
    if (this.inActiveMonth(dt)) {
        for (var it in this.selDate) {
            var x = this.cai[it];
            if (x != null) {
                var sd = this.ca[x];
                var sem = $aoGE(this.cId + "$" + it);
                sd[3] = false;
                t = "nlscaldate" + ao$getStyle(this, sd[1], sd);
                if (sem) sem.className = t
            }
        }
        this.selDate = {};
        AOCalendar.clearObj(this.selDate);
        this.date.setDate(dt.getDate());
        var sem = $aoGE(this.cId + "$" + k);
        sem.className = "nlscaldate_sel";
        this.ca[this.cai[k]][3] = true;
        var mt = this.rt.hdr.rows[0];
        if (this.opt.topSelDate) mt.cells[0].innerHTML = dt.getDate();
    } else {
        this.date = dt;
        ao$show(this, "dts");
    }
    this.selDate[k] = this.getDateObj();
    if (this.opt.mode == 0 && this.opt.autoClose) $ao$select(this.cId);
    $ao$dateChange(this, cd);
};
AOCAL.getDateObj = function () {
    return this.date.$ao_clone();
};
AOCAL.setDate = function (dt) {
    this.date = dt;
    this.selDate = {};
    AOCalendar.clearObj(this.selDate);
    this.selDate[dt.$ao_key()] = dt.$ao_clone();
};
AOCAL.getSelDate = function () {
    if (this.opt.multipSel) return this.selDate;
    for (var it in this.selDate) {
        return this.selDate[it]
    }
};
AOCAL.gotoDate = function (d) {
    this.date = d;
    if (this.rt.cal) ao$show(this, "dts");
};
AOCalendar.getMatch = function (d, o) {
    var v = null,
        a = d.split("-");
    v = o[d];
    if (!v) {
        v = o["XXXX-XX-" + a[2]]
    }
    if (!v) {
        v = o["XXXX-" + a[1] + "-XX"]
    }
    if (!v) {
        v = o["XXXX-" + a[1] + "-" + a[2]]
    }
    if (!v) {
        v = o[a[0] + "-XX-XX"]
    }
    if (!v) {
        v = o[a[0] + "-XX-" + a[2]]
    }
    if (!v) {
        v = o[a[0] + "-" + a[1] + "-XX"]
    }
    return v
};
AOCAL.disabledDates = function (d) {
    return AOCalendar.getMatch(d, this.dtDisabled);
};
AOCAL.specialDates = function (d) {
    return AOCalendar.getMatch(d, this.dtSpecials);
};
AOCAL.getEvents = function (d) {
    return ev = AOCalendar.getMatch(d, this.dtEvents);
};
AOCAL.getSelEvent = function () {
    return this.getEvents(this.date.$ao_key());
};

function $aoPrsVal(ds, d) {
    var v = ds.substring(0, d);
    if (isNaN(v)) {
        d--;
        v = ds.substring(0, d);
        if (isNaN(v)) v = null
    }
    return [v, ds.substr(d)]
};
AOCalendar.parseDate = function (d, f) {
    var r1 = /^\s+|\s+$/gi;
    var r2 = /\$[a-zA-z][a-zA-z]/i;
    var v = "",
        ds = String(d).replace(r1, ""),
        fs = String(f).replace(r1, "");
    var arr, a = [],
        j = 0,
        li = 0,
        b = 0,
        ff = fs;
    while ((arr = r2.exec(ff)) != null) {
        if (arr.index > 0) a[j++] = ff.substring(0, arr.index);
        a[j++] = arr[0];
        ff = ff.substr(arr.index + arr[0].length);
    }
    if (ff && ff.length > 0) a[j++] = ff;
    var dt = new Date();
    dt.setDate(1);
    var fl = true;
    for (var i = 0; i < a.length; i++) {
        switch (a[i]) {
        case "$yy":
            ;
        case "$YY":
            ;
        case "$dm":
            ;
        case "$dd":
            ;
        case "$hh":
            ;
        case "$HH":
            ;
        case "$mi":
            ;
        case "$ss":
            v = $aoPrsVal(ds, (a[i] == "$YY" ? 4 : 2));
            ds = v[1];
            if (v[0]) {
                switch (a[i]) {
                case "$yy":
                    dt.setFullYear(1900 + v[0]);
                    break;
                case "$YY":
                    dt.setFullYear(v[0]);
                    break;
                case "$dm":
                    dt.setMonth(v[0] - 1);
                    break;
                case "$dd":
                    dt.setDate(v[0]);
                    break;
                case "$hh":
                case "$HH":
                    dt.setHours(v[0]);
                    break;
                case "$mi":
                    dt.setMinutes(v[0]);
                    break;
                case "$ss":
                    dt.setSeconds(v[0]);
                    break
                }
            } else fl = false;
            break
        }
        switch (a[i]) {
        case "$mm":
        case "$MM":
            b = 0;
            if (a[i] == "$mm") b = 1;
            for (j = 0; j < $ao$MIY.length; j++) {
                li = ds.indexOf($ao$MIY[j][b]);
                if (li != -1) {
                    ds = ds.substr(li + $ao$MIY[j][b].length);
                    dt.setMonth(j);
                    break
                }
            }
            if (li == -1) fl = false;
            break;
        case "$DW":
        case "$dw":
            b = 1;
            if (a[i] == "$DW") b = 0;
            for (j = 0; j < $ao$WD.length; j++) {
                li = ds.indexOf($ao$WD[j][b]);
                if (li != -1) {
                    ds = ds.substr(li + $ao$WD[j][b].length);
                    break
                }
            }
            if (li == -1) fl = false;
            break;
        case "$hm":
            v = ds.substring(0, 2).toLowerCase();
            ds = ds.substr(2);
            if (v == "pm") {
                if (dt.getHours() < 12) dt.setHours(12 + dt.getHours());
            }
            break;
        default:
            li = ds.indexOf(a[i]);
            if (li != -1) {
                ds = ds.substr(li + a[i].length);
            }
        }

    }
    if (!fl) try {
        dt = Date.parse(d);
    } catch (e) {
        dt = new Date();
    }
    return dt
};
AOCalendar.format = function (d, f) {
    var s = String(f);
    var a = f.match(/\$[a-zA-z][a-zA-z]/gi);
    for (var i = 0; i < a.length; i++) {
        switch (a[i]) {
        case "$yy":
            s = s.replace(/\$yy/gi, String(d.getFullYear()).substr(2));
            break;
        case "$YY":
            s = s.replace(/\$YY/gi, d.getFullYear());
            break;
        case "$dm":
            s = s.replace(/\$dm/gi, $aoPad("00", String(d.getMonth() + 1)));
            break;
        case "$mm":
            s = s.replace(/\$mm/gi, $ao$MIY[d.getMonth()][1]);
            break;
        case "$MM":
            s = s.replace(/\$MM/gi, $ao$MIY[d.getMonth()][0]);
            break;
        case "$dd":
            s = s.replace(/\$dd/gi, $aoPad("00", d.getDate()));
            break;
        case "$DW":
            s = s.replace(/\$DW/gi, $ao$WD[d.getDay()][0]);
            break;
        case "$dw":
            s = s.replace(/\$dw/gi, $ao$WD[d.getDay()][1]);
            break;
        case "$hh":
            var h = d.getHours() % 12;
            if (h == 0) h = 12;
            s = s.replace(/\$hh/gi, $aoPad("00", String(h)));
            break;
        case "$HH":
            s = s.replace(/\$HH/gi, $aoPad("00", d.getHours()));
            break;
        case "$mi":
            s = s.replace(/\$mi/gi, $aoPad("00", d.getMinutes()));
            break;
        case "$ss":
            s = s.replace(/\$ss/gi, $aoPad("00", d.getSeconds()));
            break;
        case "$hm":
            s = s.replace(/\$hm/gi, $ao$HM[d.getHours() < 12 ? 0 : 1]);
            break
        }
    }
    return s
};
AOCalendar.formatSelDates = function (c, dl, fmt) {
    var d = null,
        r = [],
        f = (fmt ? fmt : c.opt.dtFormat);
    for (var it in c.selDate) {
        d = c.selDate[it];
        r[r.length] = AOCalendar.format(d, f);
    }
    return r.join(dl)
};
AOCalendar.clearObj = function (o) {
    for (var it in o) {
        delete o[it]
    }
};
AOCAL.$onSelect = function () {
    return true
};
AOCAL.$onChange = function () {
    return true
};
AOCAL.onChange = function () {
    return true
};
AOCAL.onSelect = function () {
    return true
};
AOCAL.onShow = function () {
    return true
};
AOCAL.onHide = function () {
    return true
};
AOCAL.onMonthChange = function () {
    return true
};
AOCAL.onYearChange = function () {
    return true
};
AOCAL.onMYChange = function () {
    return true
};

function ao$show(cal, p) {
    if (p == "dts") cal.reloadDate();
    var d = ["dts", "mdd", "ydd"];
    for (var i = 0; i < d.length; i++) {
        if (cal.rt[d[i]]) cal.rt[d[i]].style.display = "none"
    }
    cal.rt[p].style.display = "";
    cal.rt.cp = p
};

function $ao$dateChange(c, a) {
    if (a) {
        var m = 0,
            y = 0;
        if (a.getMonth() != c.date.getMonth()) {
            m = 1;
            c.onMonthChange();
        }
        if (a.getFullYear() != c.date.getFullYear()) {
            y = 1;
            c.onYearChange();
        }
        if (m || y) c.onMYChange();
        if (a.$ao_key() == c.date.$ao_key()) return
    }
    $ao$onChange(c);
}
function $ao$onChange(c) {
    c.$onChange();
    c.onChange();
}
function $ao$setState(btn, v) {
    if (v) {
        if (btn.className == "nlscalnavbtn_dis") btn.className = "nlscalnavbtn"
    } else {
        btn.className = "nlscalnavbtn_dis"
    }
}
function $ao$updateBtns(c) {
    var bt = c.rt.btns.rows[0].cells;
    $ao$setState(bt[0], c.$IYR("M", -1));
    $ao$setState(bt[2], c.$IYR("M", 1));
    $ao$setState(bt[4], c.$IYR("Y", -1));
    $ao$setState(bt[6], c.$IYR("Y", 1));
};

function $ao$myclick(cId, m, v) {
    var c = aoCals[cId];
    var cd = c.getDateObj();
    if (c.dateAdd(m, v)) {
        ao$show(c, "dts");
        $ao$dateChange(c, cd);
    }
    $ao$updateBtns(c);

};

function $ao$myddover(t) {
    if (t.className != "nlsmydd_sel") t.className = "nlsmydd_over"
};

function $ao$myddout(t) {
    if (t.className != "nlsmydd_sel") t.className = "nlsmydd_norm"
};

function $ao$today(cId) {
    var c = aoCals[cId],
        cd = c.getDateObj();
    c.setDate(new Date());
    ao$show(c, "dts");
    $ao$dateChange(c, cd);
};

function $ao$monthdd(cId) {
    var cal = aoCals[cId];
    var cm = cal.date.getMonth();
    $mddselect(cal, cal.rt.mdd.rows[Math.floor(cm % 6)].cells[Math.floor(cm / 6)]);
    if (cal.rt.cp == "mdd") ao$show(cal, "dts");
    else ao$show(cal, "mdd");
};

function $ao$yeardd(cId) {
    var c = aoCals[cId];
    var cy = c.date.getFullYear(),
        yt = $aoGE(cId + cy);
    if (!yt) {
        c.reloadYear(cy);
        yt = $aoGE(cId + cy);
    }
    $yddselect(c, yt);
    if (c.rt.cp == "ydd") ao$show(c, "dts");
    else ao$show(c, "ydd");
};

function $mddselect(c, t) {
    t.className = "nlsmydd_sel";
    var lst = c.rt.$selMth;
    if (lst && lst != t) lst.className = "nlsmydd_norm";
    c.rt.$selMth = t;
    $ao$updateBtns(c);
};

function $yddselect(c, t) {
    if (!t) return;
    t.className = "nlsmydd_sel";
    var lst = c.rt.$selYr;
    if (lst && lst != t) lst.className = "nlsmydd_norm";
    c.rt.$selYr = t;
    $ao$updateBtns(c);
};

function $ao$myddclick(t, cId, i, m) {
    var c = aoCals[cId],
        cd = c.getDateObj();
    if (m == "M") {
        $mddselect(c, t);
        c.date.$ao_setMonth(i);
    } else {
        $yddselect(c, t);
        c.date.$ao_setFullYear(i);
    }
    ao$show(c, "dts");
    $ao$dateChange(c, cd);
};

function $ao$yddpager(t, cId, y) {
    var c = aoCals[cId];
    c.reloadYear(y);
    $yddselect(c, $aoGE(cId + String(c.date.getFullYear())));
};

function $ao$yddgo(cId) {
    var y = $aoGE(cId + "$yrinp").value;
    if (isNaN(y) || y == "") return;
    y = parseInt(y, 10);
    var c = aoCals[cId];
    if (!c.isYearInRange(y, true)) return;
    c.reloadYear(y);
};

function $ao$close(cId) {
    aoCals[cId].hide();
};

function $ao$select(cId) {
    var c = aoCals[cId];
    c.hide();
    c.$onSelect();
    c.onSelect();
}
function $ao$btnover(t) {
    if (t.className != "nlscalnavbtn_dis") t.className = "nlscalnavbtn_over"
};

function $ao$btnout(t) {
    if (t.className != "nlscalnavbtn_dis") t.className = "nlscalnavbtn"
}
function $ao$udover(t) {
    t.className = "nlscalud_over"
};

function $ao$udout(t) {
    t.className = "nlscalud"
};

function $ao$sysover(t) {
    t.className = "nlscalsysbtn_over"
};

function $ao$sysout(t) {
    t.className = "nlscalsysbtn"
};

function $ao$tmUpDown(cal, tm, f) {
    $ao$tmAdj(cal, tm, f);
    var rt = cal.rt;
    if (rt.$tmo != null) clearTimeout(rt.$tmo);
    rt.$tmo = null;
    if (rt.$Intv != null) clearInterval(rt.$Intv);
    rt.$Intv = null
};

function $ao$tmAdj(cal, tm, f) {
    var v = (tm == "tmh" ? $ao$BS24 : $ao$BS60).add(parseInt(cal.rt[tm].value, 10), f);
    cal.rt[tm].value = $aoPad("00", v);
    $ao$setTime(cal, v, tm);
};

function $ao$tmAutoUpDown(cal, tm, f) {
    var v = cal.rt[tm].value;
    if (isNaN(v) || v == "") cal.rt[tm].value = 0;
    cal.rt.$tmo = setTimeout(function () {
        cal.rt.$Intv = setInterval(function () {
            $ao$tmAdj(cal, tm, f)
        }, 60);
    }, 300);
};

function $ao$setTime(c, v, tm) {
    var d = [c.date];
    if (!c.opt.multiSel) d[1] = c.getSelDate();
    for (var i = 0; i < d.length; i++) {
        if (tm == "tmh") {
            d[i].setHours(v);
        } else if (tm == "tmm") {
            d[i].setMinutes(v);
        }
    }
    $ao$onChange(c);
}
function $ao$valtime(cal, t, f) {
    var v = t.value;
    if (isNaN(v) || v == "") v = 0;
    v = (f == "tmh" ? $ao$BS24 : $ao$BS60).add(0, parseInt(v, 10));
    t.value = $aoPad("00", v);
    $ao$setTime(cal, v, f);
};

function $ao$getScroll() {
    var w = window,
        d = w.document,
        de = d.documentElement;
    var x = w.scrollX || (de && de.scrollLeft ? de.scrollLeft : d.body.scrollLeft);
    var y = w.scrollY || (de && de.scrollTop ? de.scrollTop : d.body.scrollTop);
    return {
        "x": x,
        "y": y
    }
}
function $ao$ddmsdown(e, el, cId) {
    var c = aoCals[cId],
        d = document;
    var sc = $ao$getScroll();
    var of = [(e.offsetX + el.offsetLeft + el.offsetParent.offsetLeft) || e.layerX, (e.offsetY || e.layerY) + el.offsetTop];
    c.rt.clOff = [of[0] - sc.x, of[1] - sc.y];
    d.onmousemove = function (e) {
        $ao$clDrag(e || event, c, $aoGE(cId));
    };
    d.onmouseup = function (e) {
        $ao$clDrop(e || event, c)
    };
    d.onselectstart = function () {
        return false
    };
    d.onmousedown = function () {
        return false
    };
    d.ondragstart = function () {
        return false
    }
}
function $ao$clDrag(e, c, oCl) {
    oCl.style.left = e.clientX - c.rt.clOff[0] + "px";
    oCl.style.top = e.clientY - c.rt.clOff[1] + "px"
}
function $ao$clDrop() {
    var d = document;
    d.onmousemove = null;
    d.onmouseup = null;
    d.onmousedown = function () {
        return true
    };
    d.onselectstart = function () {
        return true
    };
    d.ondragstart = function () {
        return true
    }
}
function $aoGE(id) {
    if (document.all) {
        return document.all(id);
    } else if (document.getElementById) {
        return document.getElementById(id);
    }
};

function $aoPad(f, v) {
    var sf = String(v);
    return f.substring(0, f.length - sf.length) + sf
};

function $addParam(url, prm) {
    var s = url.indexOf("?") > 0 ? "&" : "?";
    return url + s + prm
};

function $aoGDT(ri, ci) {
    return (ri - 1) * 7 + ci
}
function $aoCalMO(e, em, cId) {
    var pr = em.parentNode,
        cal = aoCals[cId];
    var x = $aoGDT(pr.rowIndex, em.cellIndex);
    var ls = em.className;
    if (ls == "nlscaldate" || ls == "nlscaldate_weekend") em.className = "nlscaldate_over";
    var l = cal.rt.lst;
    if (l) {
        if (l[0].className == "nlscaldate_over" && em != l[0]) l[0].className = l[1]
    }
    if (!l || l[0] != em) cal.rt.lst = [em, ls];
    if (x >= 0 && x < 42) {
        cal.rt.sts.innerHTML = $ao$WD[em.cellIndex][1] + ", " + $ao$MIY[cal.ca[x][1].getMonth()][1] + " " + cal.ca[x][0]
    }
};

function $aoCalMT(e, em, cId) {
    var c = aoCals[cId];
    var l = c.rt.lst;
    if (l && l[0].className == "nlscaldate_over") l[0].className = l[1];
    c.rt.sts.innerHTML = "&nbsp;"
};

function $aoCalCL(e, em, cId) {
    var c = aoCals[cId],
        d = c.ca[$aoGDT(em.parentNode.rowIndex, em.cellIndex)];
    if (c.opt.disableSel) return;
    if (c.opt.selOnEvent && !c.getEvents(d[1].$ao_key())) return;
    if (d[2]) {
        if (c.opt.multiSel) c.toggleSelection(d[1]);
        else c.selectDate(d[1]);
    }
};

function $aoCalDbl(e, em, cId) {
    var c = aoCals[cId];
    if (!c.opt.autoClose) $ao$select(c.cId);
};
$AODate = Date.prototype;
$AODate.$ao_setMY = function (y, m) {
    var cd = this.getDate();
    this.setDate(1);
    this.setMonth(m);
    this.setFullYear(y);
    if (cd > AOCalendar.$DIM(y, m)) cd = AOCalendar.$DIM(y, m);
    this.setDate(cd);
};
$AODate.$ao_setMonth = function (m) {
    this.$ao_setMY(this.getFullYear(), m);
};
$AODate.$ao_setFullYear = function (y) {
    this.$ao_setMY(y, this.getMonth());
};
$AODate.$ao_add = function (f, v) {
    switch (f) {
    case "D":
        this.setTime(this.getTime() + v * $ao$DMS);
        break;
    case "M":
    case "Y":
        var nm = this.getMonth(),
            ny = this.getFullYear();
        if (f == "M") {
            nm = $ao$BS12.add(nm, v);
            ny += $ao$BS12.ca
        } else {
            ny += v
        }
        this.$ao_setMY(ny, nm);
    }
    return this
};
$AODate.$ao_key = function () {
    return AOCalendar.format(this, "$YY-$dm-$dd");
};
$AODate.$ao_clone = function () {
    var d = new Date();
    d.setTime(this.getTime());
    return d
};
