var d = new Date();
var date_c_day = d.getDate();
var date_c_month = d.getMonth() + 1;
var date_c_year = d.getFullYear();
var date_f_day = date_c_day;
var date_f_month = date_c_month;
var date_f_year = date_c_year;
var date_n_day = date_c_day;
var date_n_month = date_c_month;
var date_n_year = date_c_year;
var date_cal; // output element
var date_con; // container element
var date_ob = '';
var date_input; // text Element
var date_months = ['January', 'February', 'March', 'April', 'May', 'June','July', 'August', 'September', 'October', 'November', 'December'];
var date_days = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];

function getleft(el) { // get the left and the top of the element
    var tmp = el.offsetLeft;
    el = el.offsetParent
    while(el) {
        tmp += el.offsetLeft;
        el = el.offsetParent;
    }
    return tmp;
}

function gettop(el) {
    var tmp = el.offsetTop;
    el = el.offsetParent
    while(el) {
        tmp += el.offsetTop;
        el = el.offsetParent;
    }
    return tmp;
}

function date_echo(t) {
    date_ob += t;
}

function date_template_main_above(t) { // calendar template
    return '<table cellpadding="3" cellspacing="1" class="date_tbl">'
         + '<tr>'
         + '<td class="date_head" style="cursor: pointer" onclick="date_py();">&lt;&lt;</td>'
         + '<td class="date_head" style="cursor: pointer" onclick="date_pm();">&lt;</td>'
         + '<td class="date_head" style="cursor: pointer" onclick="date_hide();" colspan="3">[Close]</td>'
         + '<td class="date_head" style="cursor: pointer" onclick="date_nm();">&gt;</td>'
         + '<td class="date_head" style="cursor: pointer" onclick="date_ny();">&gt;&gt;</td>'
         + '</tr>'
         + '<tr>'
         + '<td colspan="7" class="date_head">' + t + '</td>'
         + '</tr>'
         + '<tr>';
}

function date_template_day_row(t) {
    return '<td class="date_subhead">' + t + '</td>'; // define width in CSS, XHTML 1.0 Strict doesn't have width property for it
}

function date_template_new_week() {
    return '</tr><tr>';
}

function date_template_blank_cell(colspan) {
    return '<td colspan="' + colspan + '"></td>'
}

function date_template_day(d, m, y) {
    if (d == date_f_day && m == date_f_month && y == date_f_year) {
        date_cell_style = 'date_cell_cur';
    } else if (d == date_n_day && m == date_n_month && y == date_n_year) {
        date_cell_style = 'date_cell_now';
    } else date_cell_style = 'date_cell';

    // define width the day row
    return '<td class="'+date_cell_style+'" onclick="date_onclick(' + d + ',' + m + ',' + y + ')">' + d + '</td>';
}

function date_template_main_below() {
    return '</tr>' + '</table>';
}

function date_draw_calendar(m, y) { // this one draws calendar
    date_ob = ''; // first clean the output buffer
    date_echo (date_template_main_above(date_months[m - 1] + ' ' + y)); // here we go, do the header
    for (i = 0; i < 7; i ++) {
        date_echo (date_template_day_row(date_days[i]));
    }
    if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12) {
        days = 31;
    } else if (m == 4 || m == 6 || m == 9 || m == 11) {
        days = 30;
    } else {
        days = (y % 4 == 0) ? 29 : 28;
    }
    var date_dc_date = new Date(); // make a date object
    date_dc_date.setDate(1); // m/b pre-setMonth()
    date_dc_date.setMonth((m - 1));
    date_dc_date.setFullYear(y);
    var first_day = date_dc_date.getDay();
    var first_loop = 1;
    date_echo (date_template_new_week()); // start the first week
    if (first_day != 0) { // if sunday is not the first day of the month, make a blank cell
        date_echo (date_template_blank_cell(first_day));
    }
    var j = first_day;
    for (i = 0; i < days; i ++) {
        // today is sunday, make a new week
        // if this sunday is the first day of the month,
        // we've made a new row for you already
        if (j == 0 && !first_loop) {
            date_echo (date_template_new_week()); // new week!!
        }
        date_echo (date_template_day(i + 1, m, y)); // make a row of that day!
        first_loop = 0; // this is not first loop anymore
        j ++; // what is the next day?
        j %= 7;
    }
    date_echo (date_template_main_below()); // do the footer
    date_cal.innerHTML = date_ob; // display all
    date_ob = ''; // clear
}

function date_show(id) {
    date_con = document.getElementById('date_container');
    date_cal = document.getElementById('date_calendar');
    if (date_con.style.display != 'none' && date_input == document.getElementById(id)) {
        date_con.style.display = 'none';
        return;
    }
    date_input = document.getElementById(id); // set the element to set
    t = date_input;
    var dv = t.value; // set the month and year
    var date_sh_date = new Date();
    var div0=dv.indexOf(dateDiv); // dateDiv: see validate.js
    var div1=dv.indexOf(dateDiv,div0+1);
    do {
        if (div0 == -1 || div1 == -1) break;
        var strMonth=dv.substring(0,div0);
        var strDay=dv.substring(div0+1,div1);
        var strYear=dv.substring(div1+1);
        if (strDay.length < 1 || strDay.length > 2 || strMonth.length < 1 || strMonth.length > 2 || strYear.length < 1 || strYear.length > 2) break;
        if (!isInt(strDay) || !isInt(strMonth) || !isInt(strYear)) break;
        if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1);
        if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1);
        var year=parseInt(strYear);
        var month=parseInt(strMonth);
        var day=parseInt(strDay);
        if (day < 1 || day > 31 || month < 1 || month > 12 || (month == 2 && day > daysFeb(year)) || (month30(month) && day > 30)) break;
        y = (1 * strYear);
        if (y<57) {
            y = (2000 + y);
        } else y = (1900 + y);
        date_sh_date = new Date(y,(month - 1),day);
    } while(false);
    date_c_day = date_sh_date.getDate();
    date_c_month = date_sh_date.getMonth() + 1;
    date_c_year = date_sh_date.getFullYear();
    date_f_day = date_c_day;
    date_f_month = date_c_month;
    date_f_year = date_c_year;
    date_draw_calendar(date_c_month, date_c_year); // draw the calendar
    date_con.style.display = 'block';
    the_left = getleft(t); // move the calendar
    the_top = gettop(t) + t.offsetHeight;
    date_con.style.left = the_left + 'px';
    date_con.style.top = the_top + 'px';
    date_con.style.display = 'block';

    /* DO NOT ALTER THIS BLOCK */
    var tbl = document.getElementById('date_table');
    var w = tbl.offsetWidth;
    var h = tbl.offsetHeight;
    date_con.style.width = w+'px';
    date_con.style.height = h+'px';
    var ifm = document.getElementById('date_iframe');
    ifm.style.width = w+'px';
    ifm.style.height = h+'px';
    ifm.style.zIndex = 1000;
    date_con.style.zIndex = 1001;
    tbl.style.zIndex = 1002;
    /* END BLOCK */
}

function date_hide() {
    date_con.style.display = 'none';
}

function date_nm() { // moves to the next month
    date_c_month++; // increase the current month
    // we have passed December, let's go to the next year
    // increase the current year, and set the current month to January
    if (date_c_month > 12) {
        date_c_month = 1; 
        date_c_year++;
    }
    date_draw_calendar(date_c_month, date_c_year); // redraw the calendar
}

function date_pm() { // moves to the previous month
    date_c_month = date_c_month - 1;
    // we have passed January, let's go back to the previous year
    // decrease the current year, and set the current month to December
    if (date_c_month < 1) {
        date_c_month = 12; 
        date_c_year = date_c_year - 1;
    }
    date_draw_calendar(date_c_month, date_c_year); // redraw the calendar
}

function date_ny() { // moves to the next year
    date_c_year++; // increase the current year
    date_draw_calendar(date_c_month, date_c_year); // redraw the calendar
}

function date_py() { // moves to the previous year
    date_c_year = date_c_year - 1; // decrease the current year
    date_draw_calendar(date_c_month, date_c_year); // redraw the calendar
}

function date_format(d, m, y) { // format the date to output
    m2 = '00' + m; // 2 digits month
    m2 = m2.substr(m2.length - 2);
    d2 = '00' + d; // 2 digits day
    d2 = d2.substr(d2.length - 2);
    y2 = '00' + y;
    y2 = y2.substr(y2.length - 2);
    return m2 + '/' + d2 + '/' + y2; // MM/DD/YY
}

function date_onclick(d, m, y) { // when the user clicks the day
    date_hide();
    if (typeof(date_input.value) != 'undefined') { // set the value of it, if we can
        date_input.value = date_format(d, m, y);
    } else if (typeof(date_input.innerHTML) != 'undefined') { // maybe we want to set the HTML in it
        date_input.innerHTML = date_format(d, m, y);
    } else { // alert it to user
        alert (date_format(d, m, y));
    }
}


