﻿var popCalDstFld;
var temp;
var popCalWin;
var rightArrow;
var leftArrow;
function popupCal() {

    var tmpDate = new Date();
    var tmpString = "";
    var tmpNum = 0;
    var popCalDateVal;
    var dstWindowName = "";
    popCalWin = new Object();

    if (arguments.length < 2) {
        alert("popupCal(): Wrong number of arguments.");
        return void (0);
    }

    dstWindowName = popupCal.arguments[0];
    popCalDstFld = popupCal.arguments[1];
    temp = popupCal.arguments[1];
    popCalDstFmt = popupCal.arguments[2]; 
    popCalMonths = popupCal.arguments[3]; 
    popCalDays = popupCal.arguments[4];   
    popCalToday = popupCal.arguments[5];  
    popCalClose = popupCal.arguments[6];  
    popCalTitle = popupCal.arguments[7];  
    popCalFirstDayWeek = popupCal.arguments[8]; 

    
    if (popCalDstFld != "")
        popCalDstFld = document.getElementById(popCalDstFld);

    
    if (popCalDstFmt == "")
        popCalDstFmt = "m/d/yyyy";

    
    if (popCalMonths == "")
        popCalMonths = "January,February,March,April,May,June,July,August,September,October,November,December";

    
    if (popCalDays == "")
        popCalDays = "Sun,Mon,Tue,Wed,Thu,Fri,Sat";

    if (popCalToday == "" || typeof popCalToday == "undefined")
        popCalToday = "Today";

    if (popCalClose == "" || typeof popCalClose == "undefined")
        popCalClose = "Close";

    if (popCalTitle == "" || typeof popCalTitle == "undefined")
        popCalTitle = "Calendar";

    tmpString = new String(popCalDstFld.value);
    
    if (tmpString == "")
        popCalDateVal = new Date()
    else {
       
        tmpNum = tmpString.lastIndexOf("/");
        if ((tmpString.length - tmpNum) == 3) {
            tmpString = tmpString.substring(0, tmpNum + 1) + "20" + tmpString.substr(tmpNum + 1);
            popCalDateVal = new Date(tmpString);
        }
        else {
          
            popCalDateVal = getDateFromFormat(tmpString, popCalDstFmt);
        }
    }

    if (popCalDateVal == 0 || popCalDateVal.toString() == "NaN") {
        popCalDateVal = new Date();
        popCalDstFld.value = "";
    }

    var dateString = String(popCalDateVal.getMonth() + 1) + "/" + String(popCalDateVal.getDate()) + "/" + String(popCalDateVal.getFullYear());
    reloadCalPopup(dateString, dstWindowName);

    return void (0);
}

function closeCalPopup() {
    popCalWin.close();
    return void (0);
}

function reloadCalPopup() //[0]dateString, [1]dstWindowName
{

    var windowFeatures = "toolbar=no, location=no, status=no, menubar=no, scrollbars=no, resizable=no, height=270, width=270, top=" + ((screen.height - 270) / 2).toString() + ",left=" + ((screen.width - 270) / 2).toString();
    var tmpDate = new Date(reloadCalPopup.arguments[0]);

    if (tmpDate.toString() == "Invalid Date")
        tmpDate = new Date();

    tmpDate.setDate(1);

    var popCalData = calPopupSetData(tmpDate, reloadCalPopup.arguments[1]);

    if (popCalWin.toString() == "[object Object]") {
        popCalWin = window.open("", reloadCalPopup.arguments[1], windowFeatures);
        popCalWin.opener = self;
        popCalWin.focus();
    }
    else {
        popCalWin.document.close();
        popCalWin.document.clear();
    }

    popCalWin.document.write(popCalData);

    return void (1);
}

function calPopupSetData(firstDay, dstWindowName) {
    var popCalData = "";
    var lastDate = 0;
    var fnt = new Array("<FONT SIZE=\"1\">", "<B><FONT SIZE=\"2\">", "<FONT SIZE=\"2\" COLOR=\"#EF741D\"><B>");
    var dtToday = new Date();
    var thisMonth = firstDay.getMonth();
    var thisYear = firstDay.getFullYear();
    var nPrevMonth = (thisMonth == 0) ? 11 : (thisMonth - 1);
    var nNextMonth = (thisMonth == 11) ? 0 : (thisMonth + 1);
    var nPrevMonthYear = (nPrevMonth == 11) ? (thisYear - 1) : thisYear;
    var nNextMonthYear = (nNextMonth == 0) ? (thisYear + 1) : thisYear;
    var sToday = String((dtToday.getMonth() + 1) + "/01/" + dtToday.getFullYear());
    var sPrevMonth = String((nPrevMonth + 1) + "/01/" + nPrevMonthYear);
    var sNextMonth = String((nNextMonth + 1) + "/01/" + nNextMonthYear);
    var sPrevYear1 = String((thisMonth + 1) + "/01/" + (thisYear - 1));
    var sNextYear1 = String((thisMonth + 1) + "/01/" + (thisYear + 1));
    var tmpDate = new Date(sNextMonth);


    tmpDate = new Date(tmpDate.valueOf() - 1001);
    lastDate = tmpDate.getDate();
    var monthNames;
    var dayNames
    if (this.popCalMonths.split) // javascript 1.1 defensive code
    {
        monthNames = this.popCalMonths.split(",");
        dayNames = this.popCalDays.split(",");
    }
    else  // Need to build a js 1.0 split algorithm, default English for now
    {
        monthNames = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
        dayNames = new Array("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat")
    }

    var styles = "<style><!-- body{font-family:Arial,Helvetica,sans-serif;font-size:9pt}; td {  font-family: Arial, Helvetica, sans-serif; font-size: 9pt; color: #666666}; A { text-decoration: none; };TD.day { border-bottom: solid black; border-width: 0px; }--></style>"
    var cellAttribs = "align=\"center\" class=\"day\" BGCOLOR=\"#F1F1F1\"onMouseOver=\"temp=this.style.backgroundColor;this.style.backgroundColor='#CCCCCC';\" onMouseOut=\"this.style.backgroundColor=temp;\""
    var cellAttribs2 = "align=\"center\" BGCOLOR=\"#F1F1F1\" onMouseOver=\"temp=this.style.backgroundColor;this.style.backgroundColor='#CCCCCC';\" onMouseOut=\"this.style.backgroundColor=temp;\""
    var htmlHead = "<HTML><HEAD><TITLE>" + popCalTitle + "</TITLE>" + styles + "</HEAD><BODY BGCOLOR=\"#F1F1F1\" TEXT=\"#000000\" LINK=\"#364180\" ALINK=\"#FF8100\" VLINK=\"#424282\">";
    var htmlTail = "</BODY></HTML>";
    var closeAnchor = "<CENTER><input type=button value=\"" + popCalClose + "\" onClick=\"javascript:window.opener.closeCalPopup()\"></CENTER>";
    var todayAnchor = "<A HREF=\"javascript:window.opener.reloadCalPopup('" + sToday + "','" + dstWindowName + "');\">" + popCalToday + "</A>";
    var prevMonthAnchor = "<A HREF=\"javascript:window.opener.reloadCalPopup('" + sPrevMonth + "','" + dstWindowName + "');\">" + monthNames[nPrevMonth] + "</A>";
    var nextMonthAnchor = "<A HREF=\"javascript:window.opener.reloadCalPopup('" + sNextMonth + "','" + dstWindowName + "');\">" + monthNames[nNextMonth] + "</A>";
    var prevYear1Anchor = "<A HREF=\"javascript:window.opener.reloadCalPopup('" + sPrevYear1 + "','" + dstWindowName + "');\">" + (thisYear - 1) + "</A>";
    var nextYear1Anchor = "<A HREF=\"javascript:window.opener.reloadCalPopup('" + sNextYear1 + "','" + dstWindowName + "');\">" + (thisYear + 1) + "</A>";

    popCalData += (htmlHead + fnt[1]);
    popCalData += ("<DIV align=\"center\">");
    popCalData += ("<TABLE BORDER=\"0\" cellspacing=\"0\" callpadding=\"0\" width=\"250\"><TR><TD width=\"45\">&nbsp</TD>");
    popCalData += ("<TD width=\"45\" align=\"center\" " + cellAttribs2);
    popCalData += (" >");
    popCalData += (fnt[0] + prevYear1Anchor + "</FONT></TD>");
    popCalData += ("<TD width=\"70\" align=\"center\" " + cellAttribs2);
    popCalData += (" >");
    popCalData += (fnt[0] + todayAnchor + "</FONT></TD>");
    popCalData += ("<TD width=\"45\" align=\"center\" " + cellAttribs2);
    popCalData += (" >");
    popCalData += (fnt[0] + nextYear1Anchor + "</FONT></TD><TD width=\"45\">&nbsp</TD>");
    popCalData += ("</TR></TABLE>");
    popCalData += ("<TABLE BORDER=\"0\" cellspacing=\"0\" callpadding=\"0\" width=\"250\">");
    popCalData += ("<TR><TD width=\"55\" align=\"center\" " + cellAttribs2);
    popCalData += (" >");
    popCalData += (fnt[0] + prevMonthAnchor + "</FONT></TD>");
    popCalData += ("<TD width=\"140\" align=\"center\">");
    popCalData += ("&nbsp;&nbsp;" + fnt[1] + "<FONT COLOR=\"#000000\">" + monthNames[thisMonth] + ", " + thisYear + "&nbsp;&nbsp;</FONT></TD>");
    popCalData += ("<TD width=\"55\" align=\"center\" " + cellAttribs2);
    popCalData += (" >");
    popCalData += (fnt[0] + nextMonthAnchor + "</FONT></TD></TR></TABLE><BR>");
    popCalData += ("<TABLE BORDER=\"0\" cellspacing=\"2\" cellpadding=\"1\"  width=\"245\">");
    popCalData += ("");
    popCalData += ("<TR>");

    var xday = 0;
    for (xday = 0; xday < 7; xday++) {
        popCalData += ("<TD width=\"35\" align=\"center\">" + fnt[1] + "<FONT COLOR=\"#000000\">" + dayNames[(xday + popCalFirstDayWeek) % 7] + "</FONT></TD>");
    };
    popCalData += ("</TR>");

    var calDay = 0;
    var monthDate = 1;
    var weekDay = firstDay.getDay();
    do {
        popCalData += ("<TR>");
        for (calDay = 0; calDay < 7; calDay++) {
            if (((weekDay + 7 - popCalFirstDayWeek) % 7 != calDay) || (monthDate > lastDate)) {
                popCalData += ("<TD width=\"35\">" + fnt[1] + "&nbsp;</FONT></TD>");
                continue;
            }
            else {
                anchorVal = "<A HREF=\"javascript:window.opener.calPopupSetDate(window.opener.popCalDstFld,'" + (thisMonth + 1) + "/" + monthDate + "/" + thisYear + "');window.opener.closeCalPopup()\">";
                jsVal = "javascript:window.opener.calPopupSetDate(window.opener.popCalDstFld,'" + constructDate(monthDate, thisMonth + 1, thisYear) + "');window.opener.closeCalPopup()";

                popCalData += ("<TD width=\"35\" " + cellAttribs + " onClick=\"" + jsVal + "\">");

                if ((firstDay.getMonth() == dtToday.getMonth()) && (monthDate == dtToday.getDate()) && (thisYear == dtToday.getFullYear()))
                    popCalData += (anchorVal + fnt[2] + monthDate + "</A></FONT></TD>");
                else
                    popCalData += (anchorVal + fnt[1] + monthDate + "</A></FONT></TD>");

                weekDay++;
                monthDate++;
            }
        }
        weekDay = popCalFirstDayWeek;
        popCalData += ("</TR>");
    } while (monthDate <= lastDate);

    popCalData += ("</TABLE></DIV><BR>");

    popCalData += (closeAnchor + "</FONT>" + htmlTail);

    return (popCalData);
}

function calPopupSetDate() {
    calPopupSetDate.arguments[0].value = calPopupSetDate.arguments[1];
}

function padZero(num) {
    return ((num <= 9) ? ("0" + num) : num);
}
function constructDate(d, m, y) {
    var fmtDate = this.popCalDstFmt
    fmtDate = fmtDate.replace('dd', padZero(d))
    fmtDate = fmtDate.replace('d', d)
    fmtDate = fmtDate.replace('MM', padZero(m))
    fmtDate = fmtDate.replace('M', m)
    fmtDate = fmtDate.replace('yyyy', y)
    fmtDate = fmtDate.replace('yy', padZero(y % 100))
    return fmtDate;
}


function _isInteger(val) {
    var digits = "1234567890";
    for (var i = 0; i < val.length; i++) {
        if (digits.indexOf(val.charAt(i)) == -1) { return false; }
    }
    return true;
}
function _getInt(str, i, minlength, maxlength) {
    for (var x = maxlength; x >= minlength; x--) {
        var token = str.substring(i, i + x);
        if (token.length < minlength) { return null; }
        if (_isInteger(token)) { return token; }
    }
    return null;
}


function getDateFromFormat(val, format) {
    val = val + "";
    format = format + "";
    var i_val = 0;
    var i_format = 0;
    var c = "";
    var token = "";
    var x, y;
    var now = new Date();
    var year = now.getYear();
    var month = now.getMonth() + 1;
    var date = 1;

    while (i_format < format.length) {
        // Get next token from format string
        c = format.charAt(i_format);
        token = "";
        while ((format.charAt(i_format) == c) && (i_format < format.length)) {
            token += format.charAt(i_format++);
        }
        if (token == "yyyy" || token == "yy" || token == "y") {
            if (token == "yyyy") { x = 4; y = 4; }
            if (token == "yy") { x = 2; y = 2; }
            if (token == "y") { x = 2; y = 4; }
            year = _getInt(val, i_val, x, y);
            if (year == null) { return 0; }
            i_val += year.length;
            if (year.length == 2) {
                if (year > 70) { year = 1900 + (year - 0); }
                else { year = 2000 + (year - 0); }
            }
        }
        else if (token == "MM" || token == "M") {
            month = _getInt(val, i_val, token.length, 2);
            if (month == null || (month < 1) || (month > 12)) { return 0; }
            i_val += month.length;
        }
        else if (token == "dd" || token == "d") {
            date = _getInt(val, i_val, token.length, 2);
            if (date == null || (date < 1) || (date > 31)) { return 0; }
            i_val += date.length;
        }
        else {
            if (val.substring(i_val, i_val + token.length) != token) { return 0; }
            else { i_val += token.length; }
        }
    }
    if (i_val != val.length) { return 0; }
    if (month == 2) {
        if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) { // leap year
            if (date > 29) { return 0; }
        }
        else { if (date > 28) { return 0; } }
    }
    if ((month == 4) || (month == 6) || (month == 9) || (month == 11)) {
        if (date > 30) { return 0; }
    }
    var newdate = new Date(year, month - 1, date);
    return newdate;
}


var w_d = new Date()
//var w_monthname = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "Octber", "November", "December");
//var w_dayname = new Array("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
var w_StartOfWeek = 1; // Monday

var prev_month_title = "Previous month";
var next_month_title = "Next month";
var close_title = "Close";

var weekend_pos = new Array(6, 0);

var w_min_year = 1960;
var w_max_year = 2060;

var gx = 0;
var gy = 0;

var w_linkedInputText_1;
var w_linkedInputText_2;
var HideWeekCol = false;

function w_funMouseMove(evnt) {
    gx = evnt.pageX;
    gy = evnt.pageY;

    return true;
}


if ((navigator.appName.indexOf("Netscape") != -1) || (navigator.appName.indexOf("Opera") != -1)) {
    //if (document.onmousemove == undefined)
        document.onmousemove = w_funMouseMove;
}


function w_changeMonth(id) {
    var box = document.getElementById(id);
    var mm = box.options[box.selectedIndex].value;
    w_d.setMonth(mm);
    w_renderCalendar(0);
}


function w_changeYear(id) {
    var box = document.getElementById(id);
    var yy = box.options[box.selectedIndex].value;
    w_d.setFullYear(yy);
    w_renderCalendar(0);
}


function w_getWeek(year, month, day) {
    month += 1; //use 1-12
    var a = Math.floor((14 - (month)) / 12);
    var y = year + 4800 - a;
    var m = (month) + (12 * a) - 3;

    var jd = day + Math.floor(((153 * m) + 2) / 5) +
                 (365 * y) + Math.floor(y / 4) - Math.floor(y / 100) +
                 Math.floor(y / 400) - 32045;      // (gregorian calendar)

   
    var d4 = (jd + 31741 - (jd % 7)) % 146097 % 36524 % 1461;
    var L = Math.floor(d4 / 1460);
    var d1 = ((d4 - L) % 365) + L;
    NumberOfWeek = Math.floor(d1 / 7) + 1;
    return NumberOfWeek;
}



//
//
//
function w_writeDayNumber(d) {
    var dd = new Date(d);
    var temp = new Date(d);

    for (i = 0; i < 6; i++) {
        for (j = 0; j < 7; j++) {
            document.getElementById("w_c" + i + "" + j).innerHTML = "";
            document.getElementById("w_c" + i + "" + j).className = "day_out";
        }
        document.getElementById("week_" + i).innerHTML = '';
    }

    dd.setDate(1);

    i = 0;
    j = 0;

    j = dd.getDay() - w_StartOfWeek;
    if (j < 0)
        j = 7 + j;

    if (j > 0) {
        temp.setDate(dd.getDate() - 1);
        for (k = j - 1; k >= 0; k--) {
            document.getElementById("w_c" + i + "" + k).innerHTML = temp.getDate();
            if ((weekend_pos[0] == k) || (weekend_pos[1] == k))
                document.getElementById("w_c" + i + "" + k).className = "weekends_out";
            else
                document.getElementById("w_c" + i + "" + k).className = "day_out";

            temp.setDate(temp.getDate() - 1);
        }
    }

    var week = -1;
    var iStartWeek = -1;
    var iEndWeek = -1;
    var weekEl;
    var dayval;

    do {
        j = dd.getDay() - w_StartOfWeek;
        if (j < 0)
            j = 7 + j;

        if (iStartWeek == -1)
            iStartWeek = j;
        iEndWeek = j;

        dayval = dd.getDate();



        if (week < 0)
            week = w_getWeek(w_d.getFullYear(), w_d.getMonth(), dayval);

        document.getElementById("w_c" + i + "" + j).innerHTML = dayval;

        if ((weekend_pos[0] == j) || (weekend_pos[1] == j))
            document.getElementById("w_c" + i + "" + j).className = "weekends";
        else
            document.getElementById("w_c" + i + "" + j).className = "day";
        {
            var today = new Date();

            if ((today.getDate() == dayval) && (today.getMonth() == w_d.getMonth()) && (today.getFullYear() == w_d.getFullYear()))
                document.getElementById("w_c" + i + "" + j).className = "today";
        }

       
        var object = document.getElementById("w_c" + i + "" + j);
        object.rownumber = i;
        object.colnumber = j;

        if (window.addEventListener) { // Mozilla, Netscape, Firefox
            object.addEventListener('click', w_setDate, false);
        } else { // IE
            object.attachEvent('onclick', w_setDate);
        }

        if (week < 0)
            document.getElementById("week_" + i).innerHTML = '';
        else if (w_StartOfWeek != 1)
            document.getElementById("week_" + i).innerHTML = "&lt;";
        else
            document.getElementById("week_" + i).innerHTML = week;

        weekEl = document.getElementById("week_" + i);

        if (HideWeekCol) {
            weekEl.className = "weekhidden";
            document.getElementById("weekHeader").className = "weekhidden";
        }
        else {
            document.getElementById("weekHeader").className = "week";
            weekEl.className = "weeksel";
        }

        if (j == 6) {
         
            weekEl.startweek = iStartWeek;
            weekEl.endweek = iEndWeek;
            weekEl.rowweek = i;

            if (window.addEventListener) { // Mozilla, Netscape, Firefox
                weekEl.addEventListener('click', w_SetWeekDate, false);
            } else { // IE
                weekEl.attachEvent('onclick', w_SetWeekDate);
            }
            ///////

            week = -1;
            iStartWeek = -1;
            iEndWeek = -1;
            i++;
        }

        dd.setDate(dd.getDate() + 1);

    } while (dd.getDate() != 1);

    if ((iStartWeek != -1) && (iEndWeek != -1)) {
        weekEl = document.getElementById("week_" + i);

        weekEl.startweek = iStartWeek;
        weekEl.endweek = iEndWeek;
        weekEl.rowweek = i;

        if (window.addEventListener) { // Mozilla, Netscape, Firefox
            weekEl.addEventListener('click', w_SetWeekDate, false);
        } else { // IE
            weekEl.attachEvent('onclick', w_SetWeekDate);
        }
    }

   
    if ((j < 7)) {
        temp = dd;
        for (k = j + 1; k < 7; k++) {
            document.getElementById("w_c" + i + "" + k).innerHTML = temp.getDate();
            if ((weekend_pos[0] == k) || (weekend_pos[1] == k))
                document.getElementById("w_c" + i + "" + k).className = "weekends_out";
            else
                document.getElementById("w_c" + i + "" + k).className = "day_out";
            temp.setDate(temp.getDate() + 1);
        }
    }


}


function w_renderCalendar(k) {
    try {
        var monthsel_html = '';

        w_d.setMonth(w_d.getMonth() + k);

        monthsel_html += '<select class="nav" id="w_sel_month" onchange="w_changeMonth(\'w_sel_month\')">';
        for (im = 0; im < 12; im++) {
            monthsel_html += '<option value="' + im + '" ' + ((im == w_d.getMonth()) ? 'selected ' : '') + '>' + w_monthname[im] + '</option>';
        }
        monthsel_html += '</select>';
        monthsel_html += ' ';


        monthsel_html += '<select class="nav" id="w_sel_year" onchange="w_changeYear(\'w_sel_year\')">';
        for (im = w_min_year; im <= w_max_year; im++) {
            monthsel_html += '<option value="' + im + '" ' + ((im == w_d.getFullYear()) ? 'selected ' : '') + '>' + im + '</option>';
        }
        monthsel_html += '</select>';
        monthsel_html += ' ';

        document.getElementById('w_month_year').innerHTML = monthsel_html;

        // write days number
        w_writeDayNumber(w_d);
    } catch (exception) { }
}


function w_setDate(evt) {

    var m = "";
    var g = "";
    var mMonth;
    var mDay;
    var i, j;


    var e_out;
    var ie_var = "srcElement";
    var moz_var = "target";
    var prop_var = "rownumber";

    evt[moz_var] ? e_out = evt[moz_var][prop_var] : e_out = evt[ie_var][prop_var];
    i = e_out;
    prop_var = "colnumber";
    evt[moz_var] ? e_out = evt[moz_var][prop_var] : e_out = evt[ie_var][prop_var];
    j = e_out;

    mMonth = (w_d.getMonth() + 1);
    mDay = document.getElementById("w_c" + i + j).innerHTML;

    if (mMonth < 10)
        m = "0" + mMonth
    else
        m = mMonth

    if (mDay < 10)
        g = "0" + mDay
    else
        g = mDay

    // set the selected date
    try {
        document.getElementById(w_linkedInputText_1).value = m + "/" + g + "/" + w_d.getFullYear();
        document.getElementById(w_linkedInputText_2).value = '';
    }
    catch (e) { }

    w_hiddenCalendar();
}


function w_SetWeekDate(evt) {
    var m = "";
    var g = "";
    var mMonth;
    var mDay;
    var result = '';
    var startW = '';
    var endW = '';


    var e_out;
    var ie_var = "srcElement";
    var moz_var = "target";
    var prop_var = "startweek";
    var istartWeek, iendWeek;
    var rowWeek;

    evt[moz_var] ? e_out = evt[moz_var][prop_var] : e_out = evt[ie_var][prop_var];
    istartWeek = e_out;
    prop_var = "endweek";
    evt[moz_var] ? e_out = evt[moz_var][prop_var] : e_out = evt[ie_var][prop_var];
    iendWeek = e_out;

    prop_var = "rowweek";
    evt[moz_var] ? e_out = evt[moz_var][prop_var] : e_out = evt[ie_var][prop_var];
    rowWeek = e_out;


    mMonth = (w_d.getMonth() + 1);

    if (mMonth < 10)
        m = "0" + mMonth
    else
        m = mMonth

    mDay = document.getElementById("w_c" + rowWeek + istartWeek).innerHTML;
    if (mDay < 10)
        g = "0" + mDay
    else
        g = mDay

    startW = m + "/" + g + "/" + w_d.getFullYear();

    mDay = document.getElementById("w_c" + rowWeek + iendWeek).innerHTML;
    if (mDay < 10)
        g = "0" + mDay
    else
        g = mDay

    endW = m + "/" + g + "/" + w_d.getFullYear();

    // set the selected date
    try {
        document.getElementById(w_linkedInputText_1).value = startW;
        document.getElementById(w_linkedInputText_2).value = endW;
    }
    catch (e)
    { };

    w_hiddenCalendar();
}


function w_displayDatePicker(linkedId1) {
    w_linkedInputText_1 = linkedId1;
    w_linkedInputText_2 = null;

    HideWeekCol = true;
    w_displayCal();
}


function w_displayCalendar(linkedId1, linkedId2) {
    w_linkedInputText_1 = linkedId1;
    w_linkedInputText_2 = linkedId2;

    HideWeekCol = false;
    w_displayCal();
}


function w_displayCal() {
    w_renderCalendar(0);
    if (navigator.userAgent.indexOf("MSIE") != -1) {
        document.getElementById('weeklyCalendar').style.left = window.event.x + document.body.scrollLeft - 5;
        document.getElementById('weeklyCalendar').style.top = window.event.y + document.body.scrollTop + 10;
    }
    else if ((navigator.appName.indexOf("Netscape") != -1) || (navigator.appName.indexOf("Opera") != -1)) {
        document.getElementById('weeklyCalendar').style.left = gx + 10 + 'px';
        document.getElementById('weeklyCalendar').style.top = gy + -5 + 'px';
    }

    document.getElementById('weeklyCalendar').style.visibility = "visible";

}


function w_hiddenCalendar() {


    document.getElementById('weeklyCalendar').style.visibility = 'hidden';

    var i, j;
    var week;
    var daycol;

    for (i = 0; i < 7; i++) {
        try {
            week = document.getElementById("week_" + i);
            if (window.removeEventListener()) { // Mozilla, Netscape, Firefox
                week.removeEventListener('click', w_SetWeekDate, false);
            } else { // IE
                week.detachEvent('onclick', w_SetWeekDate);
            }
        }
        catch (e) { };

        try {
            for (j = 0; j < 7; j++) {
                daycol = document.getElementById("w_c" + i + "" + j);
                if (window.removeEventListener()) { // Mozilla, Netscape, Firefox
                    daycol.removeEventListener('click', w_setDate, false);
                } else { // IE
                    daycol.detachEvent('onclick', w_setDate);
                }
            }
        }
        catch (e) { };
    }

}


function w_writeDayname() {
    var mDay;
    document.write('<tr>');
    for (wd = 0; wd < 7; wd++) {
        mDay = wd + w_StartOfWeek;

        if (mDay > 6)
            mDay = mDay - 7;
        document.write('<td class="wd">' + w_dayname[mDay] + '</td>');

        if (w_dayname[mDay] == 'Sat')
            weekend_pos[0] = wd;
        if (w_dayname[mDay] == 'Sun')
            weekend_pos[1] = wd;

    }

    document.write('<td class="week" id="weekHeader">Week</td>');

    document.write('</tr>');

}

function buildWeeklyCalendar(WeekStart) {
    try {

        if (WeekStart != undefined)
            w_StartOfWeek = WeekStart;

        document.write('<div id="weeklyCalendar" class="calendar" onmouseout="javascript:PopUpMOut();">');
        document.write('<table class="calendar" >');
        document.write('<tr class="firstrow"><td colspan="8">');
        // header table
        document.write('<table width="100%" cellpading="0" cellspacing="0">');

        document.write('<tr class="firstrow"><td width="8px" onClick="w_renderCalendar(-1);" align="right" ><img src="' + leftArrow + '"  title="' + prev_month_title + '" border="0"></td>');
        document.write('<td width="8px" onClick="w_renderCalendar(1);" align="left" ><img src="' + rightArrow + '"  title="' + next_month_title + '" border="0"></td>');
        document.write('<td colspan="4"   id="w_month_year" align="center">');

        document.write('<select id="w_sel_month" >');

        for (im = 0; im < 12; im++) {
            document.write('<option value="' + im + '" ' + ((im == w_d.getMonth()) ? 'selected ' : '') + '>' + w_monthname[im] + '</option>');
        }
        document.write('</select>');
        document.write(' ');
        document.write('<select id="w_sel_year">');

        for (im = w_min_year; im <= w_max_year; im++) {
            document.write('<option value="' + im + '" ' + ((im == w_d.getFullYear()) ? 'selected ' : '') + '>' + im + '</option>');
        }
        document.write('</select>');

        document.write('</td>');

        document.write('<td align="left" onClick="w_hiddenCalendar()"  class="imgClose"></td>');

        document.write('</tr>');

        document.write('</table>');
        // end header table

        document.write('</td></tr>');

        w_writeDayname();

        for (i = 0; i < 6; i++) {
            document.write('  <tr>');

            for (j = 0; j < 7; j++) {
                document.write('<td  class="day_out" onmouseover="return escape(\'This is area 1\')" id="w_c' + i + j + '">&nbsp;</td>');
            }

            document.write('<td class="weeksel" id="week_' + i + '">&nbsp;Select&nbsp;</td>');
            document.write('  </tr>');
        }
        document.write('</table></div>');

        w_hiddenCalendar();
    }
    catch (exception) { }
}

function hideCal() {
    pos = findPoscal(document.getElementById('weeklyCalendar'));
    xy = getXYCal();


    //	            
    w1 = document.getElementById('weeklyCalendar').offsetWidth;
    h1 = document.getElementById('weeklyCalendar').offsetHeight;
    //                



    if ((xy[0] < pos[0]) ||
                    (xy[0] > pos[0] + w1) ||
                  (xy[1] < pos[1]) ||
                  (xy[1] > pos[1] + h1)) {
        w_hiddenCalendar();
        return;
    }
    else {

    }

}

function RemoveCalenderContextMenu()// to remove context menu after mouse over on it
{
    try {
        var pos;
        var xy;

        pos = findPoscal(document.getElementById('weeklyCalendar'));
        xy = getXYCal();
        w1 = document.getElementById('weeklyCalendar').offsetWidth;
        h1 = document.getElementById('weeklyCalendar').offsetHeight;

        if (xy[0] < 0 || xy[1] < 0)
            return;

        if ((xy[0] < pos[0]) ||
                    (xy[0] > pos[0] + w1) ||
                  (xy[1] < pos[1]) ||
                  (xy[1] > pos[1] + h1)) {
            w_hiddenCalendar();
            return;
        }
        else {

            return;
        }
        //	       }
    } catch (exception) { }
}


window.onload = init;
function init() {
    if (window.Event) {
        document.captureEvents(Event.MOUSEMOVE);
    }
    document.onmousemove = getXYCal;
}

function getXYCal(e) 
{
    if (!e) e = window.event; 

    if (e) {
        if (e.pageX || e.pageY) { // this doesn't work on IE6!! (works on FF,Moz,Opera7)
            mousex = e.pageX;
            mousey = e.pageY;
            algor = '[e.pageX]';
            if (e.clientX || e.clientY) algor += ' [e.clientX] '
        }
        else if (e.clientX || e.clientY) { // works on IE6,FF,Moz,Opera7
            
            mousex = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
            mousey = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
            algor = '[e.clientX]';
            if (e.pageX || e.pageY) algor += ' [e.pageX] '
        }
    }
    return [mousex, mousey];
}

function findPoscal(obj) {
    try {
        var curleft = curtop = 0;

        if (obj.offsetParent) {
            curleft = obj.offsetLeft

            curtop = obj.offsetTop

            while (obj == obj.offsetParent) {
                curleft += obj.offsetLeft

                curtop += obj.offsetTop

            }

        }
    } catch (exception) { }
    return [curleft, curtop];
}




function PopUpMOut() {

    if (!document.all) {
        RemoveCalenderContextMenu();
    }

}
function PopUpMLeave() {

    w_hiddenCalendar();
}


function setCurrentDate() {
    var currentTime = new Date();
    var month = currentTime.getMonth() + 1;
    var day = currentTime.getDate();
    var year = currentTime.getFullYear();
    var currentDate = month + '/' + day + '/' + year;
    try {
        if (document.getElementById(tbStartDate))
            document.getElementById(tbStartDate).value = currentDate;
        if (document.getElementById(tbEndDate))
            document.getElementById(tbEndDate).value = currentDate;
    }
    catch (exception) { }
}
function getStartDate() {
    var dtrangeSelector = document.getElementById(rangeSelector);
    var value = dtrangeSelector.options[dtrangeSelector.selectedIndex].value;

    if (value == '0')
        return sNullDate;
    if (value == '1')
        return sToday;
    if (value == '2')
        return sYesterDay;
    if (value == '3')
        return WeekStartDate;
    if (value == '4')
        return MonthStartDate;
    if (value == '5')
        return LastWeekStartDate;
    if (value == '6')
        return LastMonthStartDate;
    if (value == '7')
        return document.getElementById(tbStartDate).value + " 12:00:00 AM";


}
function getEndDate() {
    var dtrangeSelector = document.getElementById(rangeSelector);
    var value = dtrangeSelector.options[dtrangeSelector.selectedIndex].value;

    if (value == '0')
        return eNullDate;
    if (value == '1')
        return eToday;
    if (value == '2')
        return eYesterDay;
    if (value == '3')
        return WeekEndDate;
    if (value == '4')
        return MonthEndDate;
    if (value == '5')
        return LastWeekEndDate;
    if (value == '6')
        return LastMonthEndDate;
    if (value == '7')
        return document.getElementById(tbEndDate).value + " 11:59:59 PM";

}
