﻿// start order timer and refresh every second:
function startTheTimer(gmtOffset, timeZone) {
    globalGmtOffset = gmtOffset;
    globalTimeZone = timeZone;
    getTheDate();
    if (document.all || document.getElementById)
        setInterval("getTheDate()", 1000);
}
// get date & time from local machine, and adjust according to GMT offset difference:
function getTheDate() {
    var myDate = new Date();
    var day = myDate.getDay();
    var hours = myDate.getHours();
    var minutes = myDate.getMinutes();
    var seconds = myDate.getSeconds();
    var timeZone = -myDate.getTimezoneOffset() / 60;
    // Adjust for those time zones ahead of us: 
    if (timeZone > globalGmtOffset) {
        var timeZoneDiff = (timeZone - globalGmtOffset);
        hours -= timeZoneDiff;
        if (hours < 0) {
            hours += 24;
            day -= 1;
        }
    }
    // Adjust for those time zones behind us:
    if (timeZone < globalGmtOffset) {
        timeZoneDiff = (globalGmtOffset - timeZone);
        hours += timeZoneDiff;
        if (hours > 23) {
            hours -= 24;
            day += 1;
        }
    }
    var dn = "AM"
    if (hours >= 12)
        dn = "PM"
    if (hours > 12)
        hours = hours - 12
    if (hours == 0)
        hours = 12
    if (minutes <= 9)
        minutes = "0" + minutes
    if (seconds <= 9)
        seconds = "0" + seconds
    var cdate = "<span style=\"color: #444444; font-size: 1.1em; font-weight: bold; border-bottom: solid 1px #444444;\">Current Time at Cables for Less:</span><br /><span style=\"font-size: 1.1em; font-weight: bold; color: #cc0000;\">" + hours + ":" + minutes + ":" + seconds + " " + dn + " " + globalTimeZone + "</span>"
    // now write it:
    if (document.all)
        document.all.clock.innerHTML = cdate
    else if (document.getElementById)
        document.getElementById("clock").innerHTML = cdate
    else
        document.write(cdate);
}
// show the estimated ship date below the current time:
function estimateShipDate(intHours, intMinutes, textTime) {
    var dayArray = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday")
    var myDate = new Date();
    var day = myDate.getDay();
    var hours = myDate.getHours();
    var minutes = myDate.getMinutes();
    var timeZone = -myDate.getTimezoneOffset() / 60;
    // alert("timezone = " + timeZone + ", globalGmtOffset = " + globalGmtOffset + "\r\rday = " + day);
    // Adjust for those time zones ahead of us:
    if (timeZone > globalGmtOffset) {
        // alert("hit ahead");
        var timeZoneDiff = (timeZone - globalGmtOffset);
        hours -= timeZoneDiff;
        if (hours < 0) {
            hours += 24;
            if (day > 0)
                day -= 1;
            else
                day = 6;
        }
    }
    // Adjust for those time zones behind us:
    if (timeZone < globalGmtOffset) {
        // alert("hit behind");
        timeZoneDiff = (globalGmtOffset - timeZone);
        hours += timeZoneDiff;
        if (hours > 23) {
            hours -= 24;
            if (day < 6)
                day += 1;
            else
                day = 0;
        }
    }
    // alert("now, day = " + day);
    var shipDate = "<p style=\"font-size: 12px; font-weight: bold; padding-top: 6px;";
    if (day == 0 || day == 6 || (day == 5 && hours > intHours) || (day == 5 && hours == intHours && minutes >= intMinutes))
        shipDate += " color: #cc0000;\">In-stock orders are currently<br />scheduled to ship on Monday.</p>";
    else if (hours > intHours || (hours == intHours && minutes >= intMinutes))
        shipDate += " color: #cc0000;\">In-stock orders are currently<br />scheduled to ship on " + dayArray[day + 1] + ".</p>";
    else
        shipDate += " color: #009900;\">In-stock orders placed by " + textTime + " " + globalTimeZone + "<br />are scheduled to ship today!</p>";
    return shipDate;
}
