﻿var timeBuilt = false;
var sLastTimeBox = "";
var timeInterval;
var iTimeStart;

var iCurrentlySelectedTime;

function findIndexOfTime(oBox) {
    var sStart = oBox.value;
    for (var i = 0; i < arrTimes.length; i++) {
        if (arrTimes[i] === sStart) {
            return i;
        }
    }
    //if we don't find the time, start with the first time 
    return 0;
}
//moves the time popup either forward or backward
function incrementTime(increment, sBox) {
    bLeaveTime = true;
    //we are at the end of the array, don't build anymore
    if (increment === 1) {
        if ((iTimeStart + iNumOfTimeCells + increment) > arrTimes.length - 1) {
            return;
        }
    }

    for (var i = 0; i < iNumOfTimeCells + 1; i++) {
        var n = (i + iTimeStart) + increment;

        if (n < 0)
            return;

        var oCell = document.getElementById("t" + i);

        if (oCell) {
            var sOnClick = "selectTime(&quot;" + sBox + "&quot;, " + n + ");";
            if (n == iCurrentlySelectedTime)
                oCell.innerHTML = "<span class='timeDDSelected'><a href='javascript:" + sOnClick + "'>" + arrTimes[n] + "</a></span>";
            else
                oCell.innerHTML = "<a href='javascript:" + sOnClick + "'>" + arrTimes[n] + "</a>"
        }
    }
    iTimeStart = iTimeStart + increment;
}

function incrementCall(increment, sBox) {
    clearInterval(timeInterval);
    timeInterval = setInterval("incrementTime(" + increment + ", '" + sBox + "')", 30);
}

function selectTime(sBox, iTimeIndex) {
    var oBox = document.getElementById(sBox);
    oBox.value = arrTimes[iTimeIndex];
    document.getElementById("timeDrop").style.display = "none";

    if (typeof (displaySelectBoxes) === "function") {
        displaySelectBoxes();
    }
    else if (typeof (Dea.displaySelectBoxes) === "function") {
        Dea.displaySelectBoxes();
    }

    if (typeof (setEndTime) === "function") {
        if (sBox.indexOf("Start") > 0)
            setEndTime();
        else {
            if (typeof (changeDuration) === "function") {
                if (sBox.indexOf("End") > 0) {
                    changeDuration();
                }
            }
        }
    }

    bLeaveTime = false;
    timeBuilt = false;
}

var bLeaveTime = false;
var iNumOfTimeCells = 10;
function buildTime(sBox) {
    if (Dea.Get(sBox).disabled)
        return;
        
    if (typeof (hideSelectBoxes) === "function") {
        hideSelectBoxes();
    }
    else if (typeof (Dea.hideSelectBoxes) === "function") {
        Dea.hideSelectBoxes();
    }

    var o = document.getElementById(sBox);
    if (o.disabled) {
        return;
    }
    var oTime = document.getElementById("timeDrop");
    if (sBox == sLastTimeBox) {
        hideTime();
    }

    sLastTimeBox = sBox;

    iCurrentlySelectedTime = findIndexOfTime(document.getElementById(sBox));
    var timeHtml = "";
    timeHtml = "<tr>";
    timeHtml += "<td onmouseover='this.className=\"timeDDActive\"' ";
    timeHtml += "onmouseout='clearInterval(timeInterval);this.className=\"timeDDNormal\"' ";
    timeHtml += "onmousedown='incrementCall(-1,  &quot;" + sBox + "&quot;);' ";
    timeHtml += "onmouseup='clearInterval(timeInterval)' ";
    timeHtml += "><a href='javascript:incrementTime(-1, &quot;" + sBox + "&quot;);'>-</a></td></tr>";


    //Set the min, don't allow it to go lower than 0
    iTimeStart = iCurrentlySelectedTime - 5;
    if (iTimeStart < 0)
        iTimeStart = 0;

    var iMin = iTimeStart;

    //Set the max (don't allow it to go over the end of the arrary)     
    var iMax = iCurrentlySelectedTime + 5;
    if (iMax > arrTimes.length - 1)
        iMax = arrTimes.length - 1;

    iNumOfTimeCells = iMax - iMin;

    j = 0; //used for cell ids
    for (var i = iMin; i <= iMax; i++) {
        sTime = arrTimes[i];
        if (i == iCurrentlySelectedTime)
            sTime = "<span class='timeDDSelected'>" + sTime + "</span>"

        var sOnClick = "selectTime(&quot;" + sBox + "&quot;, " + i + ");";

        timeHtml += "<tr>";
        timeHtml += "<td id='t" + j + "' ";
        timeHtml += "onmouseover='this.className=\"timeDDActive\"' ";
        timeHtml += "onmouseout='this.className=\"timeDDNormal\"' ";
        timeHtml += ">";
        timeHtml += "<a href='javascript:" + sOnClick + "'>" + sTime + "</a>";
        timeHtml += "</td></tr>";
        j++;

    }

    timeHtml += "<tr>";
    timeHtml += "<td onmouseover='this.className=\"timeDDActive\"' ";
    timeHtml += "onmouseout='clearInterval(timeInterval);this.className=\"timeDDNormal\"' ";
    timeHtml += "onmousedown='incrementCall(1, &quot;" + sBox + "&quot;);' ";
    timeHtml += "onmouseup='clearInterval(timeInterval)' ";
    timeHtml += "><a href='javascript:incrementTime(1, &quot;" + sBox + "&quot;);'>+</a></td></tr>";

    var inputBox = document.getElementById(sBox);

    oTime.style.width = inputBox.offsetWidth - 4 + "px";
    oTime.style.display = "";


    var aLoc = findTimePos(inputBox)
    var leftpos = aLoc[0];
    var toppos = aLoc[1];

    oTime.style.position = "absolute";
    //oTime.style.left = inputBox.offsetLeft + leftpos + 2 + "px";
    //oTime.style.top = inputBox.offsetTop + toppos + inputBox.offsetHeight + "px";
    oTime.style.left = leftpos + "px";
    oTime.style.top = toppos + inputBox.offsetHeight + "px";

    oTime.innerHTML = "<table width='100%' class='timeDDTable'>" + timeHtml + "</table>";


    timeBuilt = true;
    bLeaveTime = false;
}

function findTimePos(obj) {
    var curleft = curtop = 0;
    if (obj.offsetParent) {
        curleft = obj.offsetLeft
        curtop = obj.offsetTop
        while (obj = obj.offsetParent) {
            curleft += obj.offsetLeft
            curtop += obj.offsetTop
        }
    }
    return [curleft, curtop];
}

function hideTime() {
    var oTime = document.getElementById("timeDrop");
    if (timeBuilt == true) {
        oTime.style.display = "none";
        timeBuilt = false;
        bLeaveTime = false;
        return;
    }
}

//called when key is pressed in time box
function timeBoxKeyPressed(event, box) {
    hideTime();
    var key = getKey(event);

    checkKeyDownTime(event, box);
    if (!timeKeyDownFired) //The did NOT push +, -, or d
    {
        if (key < 32 || (key >= 33 && key < 46) || (key >= 112 && key <= 123))
            return true;
    }
    else
        return false;
}

var timeKeyDownFired = false;
function checkKeyDownTime(event, box) {
    var key = getKey(event);
    var iTimeIndex = findIndexOfTime(box);

    timeKeyDownFired = false;
    switch (key) {
        case 61: //+				
        case 43:
            iTimeIndex = iTimeIndex + 1;
            timeKeyDownFired = true;
            break;
        case 45: // -
            iTimeIndex = iTimeIndex - 1;
            timeKeyDownFired = true;
            break;
    }
    if (timeKeyDownFired) {
        if (iTimeIndex < 0)
            iTimeIndex = 0;

        if (iTimeIndex > arrTimes.length - 1)
            iTimeIndex = arrTimes.length - 1;

        box.value = arrTimes[iTimeIndex]

        return false;
    }
    else {
        return true;
    }
}


function invalidTime(oBox) {
    oBox.value = "";
    alert(ems_InvalidTime);
}

//formats the time to display in localized format the user
function formatTime(hours, minutes, designator) {
    var newTime = new String(timeFormat.toLowerCase());

    if (newTime.indexOf("t") > 0) {
        if (hours === 0)
            hours = 12;

        if (hours > 12) {
            hours = hours % 12;
            designator = "pm";
        }
        else {
            if (!designator)
                designator = "am"
        }
        if (Number(minutes) < 10 && minutes != "00") {
            minutes = "0" + minutes;
        }
        if (minutes === 0 || minutes === "0") {
            minutes = "00";
        }

        newTime = newTime.replace("hh", hours);
        newTime = newTime.replace("h", hours);

        newTime = newTime.replace("mm", minutes);
        newTime = newTime.replace("m", minutes);

        newTime = newTime.replace("tt", designator == "am" ? amFull : pmFull);
        newTime = newTime.replace("t", designator == "am" ? amSingle : pmSingle);
    }
    else { //no am/pm designator...we need to pad ours with a zero
        if (hours < 10) {
            hours = "0" + hours;
        }
        if (Number(minutes) < 10 && minutes != "00") {
            minutes = "0" + minutes;
        }
        if (minutes === 0 || minutes === "0") {
            minutes = "00";
        }

        newTime = newTime.replace("hh", hours);
        newTime = newTime.replace("h", hours);
        newTime = newTime.replace("mm", minutes);
        newTime = newTime.replace("m", minutes);

    }

    return newTime.toUpperCase();
}


//validates that the time user entered is valid for their culture
function validateTime(oBox) {
    var t = new String();
    t = oBox.value.toLowerCase();
    t = t.replace(" ", "");
    var timeStringLength = t.length;

    if (timeStringLength > 1) {
        var timeWithoutDesignator = t;
        var desig = "";
        if (t.substring(timeStringLength - 1, timeStringLength) == amSingle) {
            timeWithoutDesignator = t.substring(0, timeStringLength - 1);
            desig = "am";
        }
        else if (t.substring(timeStringLength - 1, timeStringLength) == pmSingle) {
            timeWithoutDesignator = t.substring(0, timeStringLength - 1);
            desig = "pm";
        }
        else if (t.substring(timeStringLength - amFull.length, timeStringLength) == amFull) {
            timeWithoutDesignator = t.substring(0, timeStringLength - amFull.length);
            desig = "am";
        }
        else if (t.substring(timeStringLength - pmFull.length, timeStringLength) == pmFull) {
            timeWithoutDesignator = t.substring(0, timeStringLength - pmFull.length);
            desig = "pm";
        }
    }
    else
        timeWithoutDesignator = t;


    if (timeWithoutDesignator === "")
        return;

    var timeParts = timeWithoutDesignator.split(timeSeparator)

    if (timeParts.length == 1) {
        try {
            if (timeParts[0].length > 2) {
                invalidTime(oBox);
                return;
            }
            var hours = Number(timeParts[0]);

            if (hours >= 0 && hours <= 12) {

                oBox.value = formatTime(hours, "00", (desig == "" ? "am" : desig));
                return;
            }
            else if (hours > 12 && hours < 24) {
                oBox.value = formatTime(hours, "00", "pm");
                return;
            }
            else {
                invalidTime(oBox);
                return;
            }
        }
        catch (badTime) {
            invalidTime(oBox);
            return;
        }
    } //end if no sep
    else if (timeParts.length == 2) {
        try {
            var hours = Number(timeParts[0]);
            var minutes = Math.floor(Number(timeParts[1]));

            if (minutes >= 0 && minutes < 60) {
                if ((hours > 0 && hours <= 12)) {
                    oBox.value = formatTime(hours, minutes, (desig == "" ? "am" : desig));
                    return;
                }
                else if (hours >= 12 && hours < 24) {
                    oBox.value = formatTime(hours, minutes, "pm");
                    return;
                }
                else {
                    invalidTime(oBox);
                    return;
                }
            }
            else {
                invalidTime(oBox);
                return;
            }
        }
        catch (badTime) {
            invalidTime(oBox);
            return;
        }
    }
    else {
        invalidTime(oBox);
        return;
    }

}


function selectTimeText(box) {
    if (box) {
        try {
            box.select();
        }
        catch (cantSelect) {
        }
    }
}
