﻿/// <reference path="jquery-1.3.2.js" />

function processTimesheetTotal() {
    calcTimeDailyHours("Monday");
    calcTimeDailyHours("Tuesday");
    calcTimeDailyHours("Wednesday");
    calcTimeDailyHours("Thursday");
    calcTimeDailyHours("Friday");
    calcTimeDailyHours("Saturday");
    calcTimeDailyHours("Sunday");

    totalAlloc();

    setRate1x("Monday");
    setRate1x("Tuesday");
    setRate1x("Wednesday");
    setRate1x("Thursday");
    setRate1x("Friday");
    setRate1x("Saturday");
    setRate1x("Sunday");

    calcUnAllocDayTotal("Monday");
    calcUnAllocDayTotal("Tuesday");
    calcUnAllocDayTotal("Wednesday");
    calcUnAllocDayTotal("Thursday");
    calcUnAllocDayTotal("Friday");
    calcUnAllocDayTotal("Saturday");
    calcUnAllocDayTotal("Sunday");
}

function calcTimeDailyHours(dayOfWeek) {
    var elapsedHrs = 0;
    var elapsedMins = 0;
    var endDay = 1;

    var startTime = $(getStartTimeControlId(dayOfWeek)).val();
    var endTime = $(getEndTimeControlId(dayOfWeek)).val();
    var breaks = $("#ctl00_MainContent_" + dayOfWeek + "Break_TextBox1").val();

    startAMPM = startTime.substr(startTime.indexOf(" ") + 1);
    endAMPM = endTime.substr(endTime.indexOf(" ") + 1);

    if (startTime != "" &&
                endTime != "") {

        startHrs = Number(startTime.substr(0, startTime.indexOf(":")));

        if (startAMPM == "PM" && startHrs < 12)
            startHrs += 12;
        startMins = Number(startTime.substr(startTime.indexOf(":") + 1, 2));

        endHrs = Number(endTime.substr(0, endTime.indexOf(":")));

        // turn the time into 24hr for calculating total hrs worked
        var hrsFinishAt = endTime.indexOf(":");
        var hrs = endTime.substring(0, hrsFinishAt);
        if (endAMPM == "PM" && hrs != '12')
            endHrs += 12;
        // assume the end time is actually the next day like 1am
        // in the morning
        // so the end date needs to be changed
        // for caluclating the total hours worked
        else if (endAMPM == "AM" && ((endHrs < startHrs) || endHrs == 12)) {
            endDay += 1

            // take 12 hours off becuase it's midnight so the time shoudl read 00:xx 
            // on a 24hr clock NOT 12:xx as that would be midday
            if (endHrs == 12)
                endHrs -= 12;
        }

        endMins = Number(endTime.substr(endTime.indexOf(":") + 1, 2));

        var start = new Date(1970, 1, 1, startHrs, startMins);
        var end = new Date(1970, 1, endDay, endHrs, endMins);

        var elapsedTimeInMins = roundNumber((end.getTime() - start.getTime()) / 1000 / 60, 2);
        elapsedTimeInMins -= breaks;
        var elpasedTimeAsDec = elapsedTimeInMins / 60;

        $(getDayOfWeekTotalHoursControlId(dayOfWeek)).val(roundNumber(elpasedTimeAsDec, 2));
    }
}

function totalAlloc() {

    var hrs = 0;
    var preIncrementRound 

    hrs = getAllocNumber("Monday") + getAllocNumber("Tuesday") + getAllocNumber("Wednesday") +
                getAllocNumber("Thursday") + getAllocNumber("Friday") + getAllocNumber("Saturday") + getAllocNumber("Sunday");

    if (hrs > 0) {
        var preIncrementRound = roundNumber(hrs, 2);
        var postIncrementRound = round(hrs, .25);
        var roundDifference = postIncrementRound - preIncrementRound;

        if (roundDifference != 0) {
            AjustLastDayOfWorkTotalHoursColumn(roundDifference);
        }
        

        $("#ctl00_MainContent_TotalHoursFooter").val(postIncrementRound);
    }
}

function AjustLastDayOfWorkTotalHoursColumn(roundDifference){

    if (getAllocNumber("Sunday") != 0) {
        AdjustValue("Sunday", roundDifference);
    }
    else if (getAllocNumber("Saturday") != 0) {
        AdjustValue("Saturday", roundDifference);
    }
    else if (getAllocNumber("Friday") != 0) {
        AdjustValue("Friday", roundDifference);
    }
    else if (getAllocNumber("Thursday") != 0) {
        AdjustValue("Thursday", roundDifference);
    }
    else if (getAllocNumber("Wednesday") != 0) {
        AdjustValue("Wednesday", roundDifference);
    }
    else if (getAllocNumber("Tuesday") != 0) {
        AdjustValue("Tuesday", roundDifference);
    }
    else if (getAllocNumber("Monday") != 0) {
        AdjustValue("Monday", roundDifference);
    }
}

function AdjustValue(dayOfWeek, roundDifference) {
    var roundedTotal = 0;
    
    roundedTotal = $(getDayOfWeekTotalHoursControlId(dayOfWeek)).val();
    roundedTotal = roundNumber(roundedTotal, 2) + roundDifference;

    $(getDayOfWeekTotalHoursControlId(dayOfWeek)).val(roundNumber(roundedTotal, 2));
}

function totalUnAlloc() {

    var hrs = 0;

    hrs += getUnAllocNumber("Monday");
    hrs += getUnAllocNumber("Tuesday");
    hrs += getUnAllocNumber("Wednesday");
    hrs += getUnAllocNumber("Thursday");
    hrs += getUnAllocNumber("Friday");
    hrs += getUnAllocNumber("Saturday");
    hrs += getUnAllocNumber("Sunday");

    $("#ctl00_MainContent_TotalUnAllocFooter").val(roundNumber(hrs, 2));
}

function getAllocNumber(day) {
    return Number($(getDayOfWeekTotalHoursControlId(day)).val())
}

function getUnAllocNumber(day) {
    return Number($(getDayOfWeekControlId(day, "UnAllocHours")).val())
}


function roundNumber(rnum, rlength) { // Arguments: number to round, number of decimal places
    var newnumber = Math.round(rnum * Math.pow(10, rlength)) / Math.pow(10, rlength);

    return newnumber;
}

function previousDayValue(previousControl) {
    var prevTime = $("#" + previousControl).val();

    var targ;
    if (!e) var e = window.event;
    if (e.target) targ = e.target;
    else if (e.srcElement) targ = e.srcElement;
    if (targ.nodeType == 3) // defeat Safari bug
        targ = targ.parentNode;

    if (targ.value == "") targ.value = prevTime;
}

function calcAllRateTotals() {

    calcRateColumnTotal("1x");
    calcRateColumnTotal("125x");
    calcRateColumnTotal("15x");
    calcRateColumnTotal("2x");
    calcRateColumnTotal("25x");
    calcRateColumnTotal("OtherHours");
}

function calcRateColumnTotal(rateId) {
    var footerControlId = "#ctl00_MainContent_Rate" + rateId + "Footer";

    var monControlId = getRateControlId("Monday", rateId);
    var tueControlId = getRateControlId("Tuesday", rateId);
    var wedControlId = getRateControlId("Wednesday", rateId);
    var thuControlId = getRateControlId("Thursday", rateId);
    var friControlId = getRateControlId("Friday", rateId);
    var satControlId = getRateControlId("Saturday", rateId);
    var sunControlId = getRateControlId("Sunday", rateId);

    var mon = roundNumber($(monControlId).val(), 2);
    var tue = roundNumber($(tueControlId).val(), 2);
    var wed = roundNumber($(wedControlId).val(), 2);
    var thu = roundNumber($(thuControlId).val(), 2);
    var fri = roundNumber($(friControlId).val(), 2);
    var sat = roundNumber($(satControlId).val(), 2);
    var sun = roundNumber($(sunControlId).val(), 2);

    var total = mon + tue + wed + thu + fri + sat + sun;

    $(footerControlId).val(roundNumber(total,2));
}

function calcUnAllocAndRateTotal(dayOfWeek, rateId) {
    calcUnAllocDayTotal(dayOfWeek);
    calcRateColumnTotal(rateId);
}

function calcUnAllocDayTotal(dayOfWeek) {

    var totalDayHoursControlId = getDayOfWeekTotalHoursControlId(dayOfWeek);
    var unAllocControlId = getDayOfWeekControlId(dayOfWeek, "UnAllocHours");

    // quick check to see if admin controls are displayed
    // if not don't bother with these totals
    if ($(unAllocControlId).length == 0)
        return;

    var alloc1xControlId = getRateControlId(dayOfWeek, "1x");
    var alloc125xControlId = getRateControlId(dayOfWeek, "125x");
    var alloc15xControlId = getRateControlId(dayOfWeek, "15x");
    var alloc2xControlId = getRateControlId(dayOfWeek, "2x");
    var alloc25xControlId = getRateControlId(dayOfWeek, "25x");
    var allocOtherControlId = getRateControlId(dayOfWeek, "OtherHours");

    var total = roundNumber($(totalDayHoursControlId).val(), 2);
    var unAlloc = roundNumber($(totalDayHoursControlId).val(), 2);
    var x1 = roundNumber($(alloc1xControlId).val(), 2);
    var x125 = roundNumber($(alloc125xControlId).val(), 2);
    var x15 = roundNumber($(alloc15xControlId).val(), 2);
    var x2 = roundNumber($(alloc2xControlId).val(), 2);
    var x25 = roundNumber($(alloc25xControlId).val(), 2);
    var otherHours = roundNumber($(allocOtherControlId).val(), 2);

    if (!isNaN(x1)) {
        unAlloc = roundNumber(unAlloc - x1, 2);
    }

    if (!isNaN(x125)) {
        unAlloc = roundNumber(unAlloc - x125, 2);
    }

    if (!isNaN(x15)) {
        unAlloc = roundNumber(unAlloc - x15, 2);
    }

    if (!isNaN(x2)) {
        unAlloc = roundNumber(unAlloc - x2, 2);
    }

    if (!isNaN(x25)) {
        unAlloc = roundNumber(unAlloc - x25, 2);
    }

    if (!isNaN(otherHours)) {
        unAlloc = roundNumber(unAlloc - otherHours, 2);
    }

    if (!isNaN(unAlloc)) {
        $(unAllocControlId).val(roundNumber(unAlloc, 2));
    }

    totalUnAlloc();
}

function clearRateColumnTotals(rowCount, rateId) {
    var footerControlId = "#ctl00_MainContent_TimesheetHoursView_TimesheetHoursRate" + rateId + "Footer";

    $(footerControlId).val(0);
}

function zeroPad(num, count) {
    var numZeropad = num + '';
    while (numZeropad.length < count) {
        numZeropad = "0" + numZeropad;
    }
    return numZeropad;
}

function setRate1x(dayOfWeek) {
    var totalHoursControlId = getDayOfWeekTotalHoursControlId(dayOfWeek);
    var rate1xControlId = getRateControlId(dayOfWeek, "1x");
    var rate125xControlId = getRateControlId(dayOfWeek, "125x");
    var rate15xControlId = getRateControlId(dayOfWeek, "15x");
    var rate2xControlId = getRateControlId(dayOfWeek, "2x");
    var rate25xControlId = getRateControlId(dayOfWeek, "25x");
    var rateOtherHoursControlId = getRateControlId(dayOfWeek, "OtherHours");

    var dayTotalHours = roundNumber($(totalHoursControlId).val(), 2);
    var cur1xValue = roundNumber($(rate1xControlId).val(), 2);
    var cur125xValue = roundNumber($(rate125xControlId).val(), 2);
    var cur15xValue = roundNumber($(rate15xControlId).val(), 2);
    var cur2xValue = roundNumber($(rate2xControlId).val(), 2);
    var cur25xValue = roundNumber($(rate25xControlId).val(), 2);
    var curOtherHoursValue = roundNumber($(rateOtherHoursControlId).val(), 2);

    // only populate the 1x rate when there has been NO rates entered at all
    if (dayTotalHours > 0
        && cur1xValue == 0
        && cur125xValue == 0
        && cur15xValue == 0
        && cur2xValue == 0
        && cur25xValue == 0
        && curOtherHoursValue == 0) {
        $(rate1xControlId).val(dayTotalHours);
        $(rate1xControlId).effect('highlight', { color: '#AFCEE5' }, 3000);
    }
}

function getRateControlId(dayOfWeek, rate) {
    return getDayOfWeekControlId(dayOfWeek, rate);
}

function getStartTimeControlId(dayOfWeek) {
    return getDayOfWeekControlId(dayOfWeek, "StartTime_dateInput_text");
}

function getEndTimeControlId(dayOfWeek) {
    return getDayOfWeekControlId(dayOfWeek, "EndTime_dateInput_text");
}

function getDayOfWeekTotalHoursControlId(dayOfWeek) {
    return getDayOfWeekControlId(dayOfWeek, "TotalHours");
}

function getDayOfWeekControlId(dayOfWeek, controlSuffix) {
    return "#ctl00_MainContent_" + dayOfWeek + controlSuffix;
}

function round(value, increment) {
    // value will be the number we'll round
    // increment will be the 'round to' - in your case, .25
    var remain;
    var roundvalue;
    var result;
    remain = value % increment; // this will be somewhere between 0 and .25
    roundvalue = increment / 2;

    if (remain >= roundvalue) { // rounding up
        result = value - remain;
        result += increment;
    }
    else { // rounding down
        result = value - remain;
    }
    return result;
}
