//Static declarations
var now = new Date();
var firstyear = now.getFullYear(); 	//Number of years back to display.
var lastyear = (now.getFullYear() + 1) + 1; //Number of years forward to display.
var colour = "#BEBEBE"; 					//Text color of greyed out days.
var coltoday = "#D61010"; 				//Text color of today.

//Set up todays date
var day = now.getDate();
var month = now.getMonth();
var year = now.getFullYear();
//Not needed yet --> 'if (year < 2000) year = year + 1900;'

//Set up calendar display vars
var daycell = new Array();
var cellday = new Array();
var n;

//Draw calendar function (param = div tag object to display it in)
function calendar(div, gridmode) {
    calobj = "";
    if (gridmode) {
        calobj += '<form name="' + cal_formname + '">';
    }
    var calobj
    calobj += '<table border="0" cellspacing="0" cellpadding="0">'
    calobj += '<tr><td>'
    calobj += '<table border="0" cellspacing="2" cellpadding="0" class="calender">'
    calobj += '<tr><td>'
    calobj += '<select name="calmonth_' + div.id + '" onChange="updatecalendar()" class="caldropdowns">'
    calobj += '<option>January</option><option>February</option><option>March</option>'
    calobj += '<option>April</option><option>May</option><option>June</option>'
    calobj += '<option>July</option><option>August</option><option>September</option>'
    calobj += '<option>October</option><option>November</option><option>December</option>'
    calobj += '</select></td>'
    calobj += '<td align="right">'
    calobj += '<select name="calyear_' + div.id + '" onChange="updatecalendar()" class="caldropdowns">'
    for (n = firstyear; n < lastyear; n++) {
        if (n == year) {
            calobj += '<option selected value="' + n + '">' + n + '</option>'
        }
        else {
            calobj += '<option value="' + n + '">' + n + '</option>'
        }
    }
    calobj += '</select>'
    calobj += '</td></tr>'

    calobj += '<tr><td colspan="2" class="calinsideborder">'
    calobj += '<table cellspacing="1" class="calinsidesettings" border="0">'
    calobj += '<tr align="center">'
    calobj += '<td width="29" height="15" class="caldays">Sun</td>'
    calobj += '<td width="29" height="15" class="caldays">Mon</td>'
    calobj += '<td width="29" height="15" class="caldays">Tue</td>'
    calobj += '<td width="29" height="15" class="caldays">Wed</td>'
    calobj += '<td width="29" height="15" class="caldays">Thu</td>'
    calobj += '<td width="29" height="15" class="caldays">Fri</td>'
    calobj += '<td width="29" height="15" class="caldays">Sat</td>'
    calobj += '</tr>'
    var calcell, calrow
    for (calcell = 0; calcell < 6; calcell++) {
        calobj += '<tr>'
        calobj += '<td width="29" height="15" class="calwkdy" id="cal' + (calcell * 7) + '_' + thecaldiv.id + '" onclick=message(' + (calcell * 7) + ',thecaldiv,' + gridmode + ');>&nbsp;</td>'
        calobj += '<td width="29" height="15" class="calwkdy" id="cal' + (calcell * 7 + 1) + '_' + thecaldiv.id + '" onclick=message(' + (calcell * 7 + 1) + ',thecaldiv,' + gridmode + ');>&nbsp;</td>'
        calobj += '<td width="29" height="15" class="calwkdy" id="cal' + (calcell * 7 + 2) + '_' + thecaldiv.id + '" onclick=message(' + (calcell * 7 + 2) + ',thecaldiv,' + gridmode + ');>&nbsp;</td>'
        calobj += '<td width="29" height="15" class="calwkdy" id="cal' + (calcell * 7 + 3) + '_' + thecaldiv.id + '" onclick=message(' + (calcell * 7 + 3) + ',thecaldiv,' + gridmode + ');>&nbsp;</td>'
        calobj += '<td width="29" height="15" class="calwkdy" id="cal' + (calcell * 7 + 4) + '_' + thecaldiv.id + '" onclick=message(' + (calcell * 7 + 4) + ',thecaldiv,' + gridmode + ');>&nbsp;</td>'
        calobj += '<td width="29" height="15" class="calwknd" id="cal' + (calcell * 7 + 5) + '_' + thecaldiv.id + '" onclick=message(' + (calcell * 7 + 5) + ',thecaldiv,' + gridmode + ');>&nbsp;</td>'
        calobj += '<td width="29" height="15" class="calwknd" id="cal' + (calcell * 7 + 6) + '_' + thecaldiv.id + '" onclick=message(' + (calcell * 7 + 6) + ',thecaldiv,' + gridmode + ');>&nbsp;</td>'
        calobj += '</tr>'
    }
    calobj += '</table>'
    calobj += '</td></tr>'

    calobj += '<tr>'
    calobj += '<td align="center" colspan="2">'
    calobj += '<table cellpadding="0" cellspacing="0" border="0">'
    calobj += '<tr>'

    calobj += '<td width=18>'
    calobj += '<input type="button" name="backyear" value="&lt;&lt;" class="calbutts" onclick=gobackyear();updatecalendar()>'
    calobj += '</td>'
    calobj += '<td width=24 align=right>'
    calobj += '<input type="button" name="backmonth" value="&lt;" class="calbutts" onclick=gobackmonth();updatecalendar()>'
    calobj += '</td>'
    calobj += '<td width=80 align=center>'
    calobj += '<input type="button" name="TODAY" value="TODAY" class="calbutts" onclick=caltoday()>'
    calobj += '</td>'
    calobj += '<td width=24 align=left>'
    calobj += '<input type="button" name="onmonth" value="&gt;" class="calbutts" onclick=goonmonth();updatecalendar()>'
    calobj += '</td>'
    calobj += '<td width=18>'
    calobj += '<input type="button" name="onyear" value="&gt;&gt;" class="calbutts" onclick=goonyear();updatecalendar()>'
    calobj += '</td>'
    calobj += '</tr></table>'
    calobj += '</td></tr>'
    calobj += '</table></td>'
    calobj += '<td width=2 class=shadow valign=top>'
    calobj += '<table cellpadding="0" cellspacing="0" border="0">'
    calobj += '<tr>'
    calobj += '<td height=3 width=2>'


    calobj += '<table cellpadding="0" cellspacing="0" border="0"><tr><td height=1 width=1 class=white></td><td height=1 width=1 class=white></td></tr>'
    calobj += '<tr><td height=1 width=1></td><td height=1 width=1 class=white></td></tr></table>'

    calobj += '</td>'
    calobj += '</tr></table>'
    calobj += '</td>'
    calobj += '</tr>'
    calobj += '<tr>'
    calobj += '<td height=2 colspan="2" class=shadow align=left>'
    calobj += '<table cellpadding="0" cellspacing="0" border="0">'
    calobj += '<tr>'
    calobj += '<td width=3>'

    calobj += '<table cellpadding="0" cellspacing="0" border="0"><tr><td height=1 width=1 class=white></td><td height=1 width=1></td></tr><tr><td height=1 width=1 class=white></td><td height=1 width=1 class=white></td></tr></table>'

    calobj += '</td>'
    calobj += '</tr></table>'
    calobj += '</td></tr>'
    calobj += '</table>'

    if (gridmode) {
        calobj += '</form>';
    }
    /////////////////////////////////////////////////////////////////////
    //Writes the calender to div tag object
    div.innerHTML = calobj;
    /////////////////////////////////////////////////////////////////////

    //Assign cells of calendar days to an array
    /*Changed from eval --> document.getElementById*/
    daycell[0] = document.getElementById('cal0_' + thecaldiv.id);
    daycell[1] = document.getElementById('cal1_' + thecaldiv.id);
    daycell[2] = document.getElementById('cal2_' + thecaldiv.id);
    daycell[3] = document.getElementById('cal3_' + thecaldiv.id);
    daycell[4] = document.getElementById('cal4_' + thecaldiv.id);
    daycell[5] = document.getElementById('cal5_' + thecaldiv.id);
    daycell[6] = document.getElementById('cal6_' + thecaldiv.id);
    daycell[7] = document.getElementById('cal7_' + thecaldiv.id);
    daycell[8] = document.getElementById('cal8_' + thecaldiv.id);
    daycell[9] = document.getElementById('cal9_' + thecaldiv.id);
    daycell[10] = document.getElementById('cal10_' + thecaldiv.id);
    daycell[11] = document.getElementById('cal11_' + thecaldiv.id);
    daycell[12] = document.getElementById('cal12_' + thecaldiv.id);
    daycell[13] = document.getElementById('cal13_' + thecaldiv.id);
    daycell[14] = document.getElementById('cal14_' + thecaldiv.id);
    daycell[15] = document.getElementById('cal15_' + thecaldiv.id);
    daycell[16] = document.getElementById('cal16_' + thecaldiv.id);
    daycell[17] = document.getElementById('cal17_' + thecaldiv.id);
    daycell[18] = document.getElementById('cal18_' + thecaldiv.id);
    daycell[19] = document.getElementById('cal19_' + thecaldiv.id);
    daycell[20] = document.getElementById('cal20_' + thecaldiv.id);
    daycell[21] = document.getElementById('cal21_' + thecaldiv.id);
    daycell[22] = document.getElementById('cal22_' + thecaldiv.id);
    daycell[23] = document.getElementById('cal23_' + thecaldiv.id);
    daycell[24] = document.getElementById('cal24_' + thecaldiv.id);
    daycell[25] = document.getElementById('cal25_' + thecaldiv.id);
    daycell[26] = document.getElementById('cal26_' + thecaldiv.id);
    daycell[27] = document.getElementById('cal27_' + thecaldiv.id);
    daycell[28] = document.getElementById('cal28_' + thecaldiv.id);
    daycell[29] = document.getElementById('cal29_' + thecaldiv.id);
    daycell[30] = document.getElementById('cal30_' + thecaldiv.id);
    daycell[31] = document.getElementById('cal31_' + thecaldiv.id);
    daycell[32] = document.getElementById('cal32_' + thecaldiv.id);
    daycell[33] = document.getElementById('cal33_' + thecaldiv.id);
    daycell[34] = document.getElementById('cal34_' + thecaldiv.id);
    daycell[35] = document.getElementById('cal35_' + thecaldiv.id);
    daycell[36] = document.getElementById('cal36_' + thecaldiv.id);
    daycell[37] = document.getElementById('cal37_' + thecaldiv.id);
    daycell[38] = document.getElementById('cal38_' + thecaldiv.id);
    daycell[39] = document.getElementById('cal39_' + thecaldiv.id);
    daycell[40] = document.getElementById('cal40_' + thecaldiv.id);
    daycell[41] = document.getElementById('cal41_' + thecaldiv.id);

    caltoday()
}

//Set today function
function caltoday() {
    day = now.getDate();
    eval('document.' + cal_formname + '.calmonth_' + thecaldiv.id).selectedIndex = now.getMonth();
    eval('document.' + cal_formname + '.calyear_' + thecaldiv.id).value = now.getFullYear();
    updatecalendar()
}

//Refresh calendar display function
function updatecalendar() {
    month = eval('document.' + cal_formname + '.calmonth_' + thecaldiv.id).selectedIndex
    year = eval('document.' + cal_formname + '.calyear_' + thecaldiv.id).value

    var firstOfMonth = new Date(year, month, 1);
    var startingPos = firstOfMonth.getDay();
    var curday = 1
    var days = monthdays(month, year)
    var prevdays = monthdays(month - 1, year)
    var str
    for (n = 0; n < 42; n++) {
        if (startingPos == n) {

            setday(n, month, year, curday);
            var selDate = cellday[n].substr(4, 4) + cellday[n].substr(2, 2) + cellday[n].substr(0, 2);
            var strike = "<strike><font style='cursor:default;' color='" + colour + "'>";
            var estrike = "</font></strike>";

var dt = new Date (cellday[n].substr(4, 4),(cellday[n].substr(2, 2))-1,cellday[n].substr(0, 2))
            if ((dt.getDay() != 5 || selDate == (cellday[n].substr(4, 4) + '0212')) && selDate >= document.getElementById("req").value && selDate != (cellday[n].substr(4, 4) + '1225') && selDate != (cellday[n].substr(4, 4) + '1226') && selDate != (cellday[n].substr(4, 4) + '0101') && selDate != (cellday[n].substr(4, 4) + '1205') && selDate != (cellday[n].substr(4, 4) + '1226') && selDate != (cellday[n].substr(4, 4) + '0102') && selDate != (cellday[n].substr(4, 4) + '0719') && selDate != (cellday[n].substr(4, 4) + '1128') && selDate != (cellday[n].substr(4, 4) + '1202')) {
                strike = "";
                estrike = "";
            }

if (selDate != (cellday[n].substr(4, 4) + '0213') || selDate != (cellday[n].substr(4, 4) + '0214')){
//document.shipping.deliverytime.disabled = false
}

/*if (selDate == (cellday[n].substr(4, 4) + '0212') || selDate == (cellday[n].substr(4, 4) + '0213') || selDate == (cellday[n].substr(4, 4) + '0214')){
				document.shipping.deliverytime.selectedIndex = 0
				document.shipping.deliverytime.disabled = true
			} else {
				//document.shipping.deliverytime.disabled = false
			}*/
            /*if (month == now.getMonth() & year == now.getFullYear() & curday == now.getDate()) {
            str = curday
            daycell[n].innerHTML = strike + "<font color='" + coltoday + "'>" + str.toString() + "</font>" + estrike
            }
            else {*/
            daycell[n].innerHTML = strike + curday + estrike
            //}

            startingPos++
            curday++
        }
        else {
            if (startingPos == 66) {
                setday(n, month + 1, year, curday);
                str = curday
                curday++
            }
            else {
                setday(n, month - 1, year, prevdays - startingPos + n + 1);
                str = prevdays - startingPos + n + 1
            }
            var selDate = cellday[n].substr(4, 4) + cellday[n].substr(2, 2) + cellday[n].substr(0, 2);
            var strike = "<strike>";
            var estrike = "</strike>";

var dt = new Date (cellday[n].substr(4, 4),(cellday[n].substr(2, 2))-1,cellday[n].substr(0, 2))
            if ((dt.getDay() != 5 || selDate == (cellday[n].substr(4, 4) + '0213')) && selDate >= document.getElementById("req").value && selDate != (cellday[n].substr(4, 4) + '1225') && selDate != (cellday[n].substr(4, 4) + '1226') && selDate != (cellday[n].substr(4, 4) + '0101') && selDate != (cellday[n].substr(4, 4) + '1205')  && selDate != (cellday[n].substr(4, 4) + '1226') && selDate != (cellday[n].substr(4, 4) + '0102') && selDate != (cellday[n].substr(4, 4) + '0719') && selDate != (cellday[n].substr(4, 4) + '1128') && selDate != (cellday[n].substr(4, 4) + '1202')) {
                strike = "";
                estrike = "";
            }

if (selDate != (cellday[n].substr(4, 4) + '0213') || selDate != (cellday[n].substr(4, 4) + '0214')){
//document.shipping.deliverytime.disabled = false
}
/*if (selDate == (cellday[n].substr(4, 4) + '0212') || selDate == (cellday[n].substr(4, 4) + '0213') || selDate == (cellday[n].substr(4, 4) + '0214')){
				document.shipping.deliverytime.selectedIndex = 0
				document.shipping.deliverytime.disabled = true
			} else {
				//document.shipping.deliverytime.disabled = false
			}*/

            daycell[n].innerHTML = strike + "<font color='" + colour + "'>" + str.toString() + "</font>" + estrike
        }
        if (curday > days) {
            curday = 1
            startingPos = 66
        }
    }
}

//Set day variables function
function setday(cell, month, year, day) {
    month++
    if (month == 13) { month = 1; year++ }
    if (month == 0) { month = 12; year-- }

    var strmon;
    var stryear;
    var strday;

    strmon = month.toString();
    if (strmon.length == 1) { strmon = "0" + strmon }
    strday = day.toString();
    if (strday.length == 1) { strday = "0" + strday }
    stryear = year.toString();
    cellday[cell] = strday + strmon + stryear
}

//Function to check number of days in a month
function monthdays(month, year) {
    var days;
    if (month == 0 || month == 2 || month == 4 || month == 6 || month == 7 || month == 9 || month == 11 || month == -1 || month == 12) days = 31;
    else if (month == 3 || month == 5 || month == 8 || month == 10) days = 30;
    else if (month == 1) {
        if (leapyear(year)) { days = 29; }
        else { days = 28; }
    }
    return (days);
}

//Leap year checking function
function leapyear(Year) {
    if (((Year % 4) == 0) && ((Year % 100) != 0) || ((Year % 400) == 0)) {
        return (true);
    }
    else {
        return (false);
    }
}

//Display toggle functions
function goonmonth() {
    month = eval('document.' + cal_formname + '.calmonth_' + thecaldiv.id).selectedIndex;
    month++;
    if (month == 12) {
        month = 0;
        goonyear();
    }
    eval('document.' + cal_formname + '.calmonth_' + thecaldiv.id).selectedIndex = month;
}

function goonyear() {
    year = eval('document.' + cal_formname + '.calyear_' + thecaldiv.id).value;
    year++;
    if (year == lastyear) { year = firstyear }
    eval('document.' + cal_formname + '.calyear_' + thecaldiv.id).value = year;
}

function gobackmonth() {
    month = eval('document.' + cal_formname + '.calmonth_' + thecaldiv.id).selectedIndex;
    month--;
    if (month == -1) {
        month = 11;
        gobackyear();
    }
    eval('document.' + cal_formname + '.calmonth_' + thecaldiv.id).selectedIndex = month;
}

function gobackyear() {
    year = eval('document.' + cal_formname + '.calyear_' + thecaldiv.id).value;
    year--;
    if (year < firstyear) { year = lastyear - 1 }
    eval('document.' + cal_formname + '.calyear_' + thecaldiv.id).value = year;
}

///////////////////////////////////////////////////////////////////////////////////
//THE ACTUAL INTERFACE TO THE CALENDAR STARTS HERE								 //
///////////////////////////////////////////////////////////////////////////////////
//Vars for the storage of the currently open calendar
var cal_formname = '';
var cal_textboxname = '';
var cal_divname = '';
var thecalspan;
var thecaldiv;

/*
The entry point function
Parameters :	nameOfForm
nameOfTextBoxContainingDate
divTagObject
displaySpanShowingSelectedDate
*/
function showCalendar(form, textbox, div, span, gridmode) {
    var proceedflag = false;
    //If a calendar is currently open...try this...
    if (thecaldiv) {
        //...if it the same calendar trying to close itself,allow it to...
        if (thecaldiv.id == div.id) {
            proceedflag = true;
        }
    }
    else {
        //else no calendar is open Open it
        proceedflag = true;
    }
    if (proceedflag) {
        //Set the current calendar div as active
        thecaldiv = div;
        //If it was hidden show it and load it..
        if (div.style.display == 'none') {
            div.style.display = '';
            cal_formname = form;
            cal_textboxname = textbox;
            cal_divname = div.id;
            thecalspan = span;
            calendar(div, gridmode);
        }
        //...else hide it and unload the calendar.
        else {
            div.innerHTML == '';
            div.style.display = 'none';
            cal_formname = '';
            cal_textboxname = '';
            cal_divname = '';
            thecalspan = null;
            thecaldiv = null;
        }
    }
    else {
        alert("Please close the other open calendar first.");
    }
}

//Wrapping handler for a speficic date click event.
function message(cell, div, gridmode) {
    var selDate = cellday[cell].substr(4, 4) + cellday[cell].substr(2, 2) + cellday[cell].substr(0, 2);

if (selDate == (cellday[cell].substr(4, 4) + '0213') || selDate == (cellday[cell].substr(4, 4) + '0214')){
				document.shipping.deliverytime.selectedIndex = 0
				document.shipping.deliverytime.disabled = true
			} else {
				document.shipping.deliverytime.disabled = false
			}

var dt = new Date (cellday[cell].substr(4, 4),(cellday[cell].substr(2, 2))-1,cellday[cell].substr(0, 2))
         
    if ((dt.getDay() == 5 && selDate != (cellday[cell].substr(4, 4) + '0212')) || selDate < document.getElementById("req").value || selDate == (cellday[cell].substr(4, 4) + '1225') || selDate == (cellday[cell].substr(4, 4) + '1226') || selDate == (cellday[cell].substr(4, 4) + '0101') || selDate == (cellday[cell].substr(4, 4) + '1205') || selDate == (cellday[cell].substr(4, 4) + '1226') || selDate == (cellday[cell].substr(4, 4) + '0102') || selDate == (cellday[cell].substr(4, 4) + '0719') || selDate == (cellday[cell].substr(4, 4) + '1128') || selDate == (cellday[cell].substr(4, 4) + '1202')) {
        //alert("Delivery date selection is invalid. Please try again!");
    } else {
        storeSelectedDate(cellday[cell], div, gridmode);
    }
}

//The function used to save the selected date to the specified textbox.
function storeSelectedDate(date, div, gridmode) {
    //var formattedDate = date.substr(0, 2) + "-" + nameMonth(date.substr(2, 2)) + "-" + date.substr(4, 4);
    var formattedDate = date.substr(0, 2) + "/" + date.substr(2, 2) + "/" + date.substr(4, 4);
    try {
        if (gridmode) {
            document.getElementById(cal_textboxname).value = formattedDate;
        }
        else {
            eval('document.' + cal_formname + '.' + cal_textboxname + '.value="' + formattedDate + '"');
            thecalspan.innerHTML = formattedDate;
        }
        showCalendar(cal_formname, cal_textboxname, div, thecalspan);
    }
    catch (ex) {
        alert(ex.message);
    }
}

//Fuction returns the first three letters of month
function nameMonth(month) {
    switch (month) {
        case '01': return "Jan";
        case '02': return "Feb";
        case '03': return "Mar";
        case '04': return "Apr";
        case '05': return "May";
        case '06': return "Jun";
        case '07': return "Jul";
        case '08': return "Aug";
        case '09': return "Sep";
        case '10': return "Oct";
        case '11': return "Nov";
        case '12': return "Dec";
    }
}
