function setSupervisorInfo() {
        var client = document.timesheet.formClientName.options[document.timesheet.formClientName.selectedIndex].text;
        var x;
        for (x = 0; x < clients.length; x++) {
                if (clients[x].clientName == client) {
                        document.timesheet.formSupervisorName.value = clients[x].supervisorName;
                        document.timesheet.formSupervisorEmail.value = clients[x].supervisorEmail;
                        break;
                }
        }
}

function validateHours(e) {
        var validNumbers = new Array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.');
        if (e.value == "") e.value = "0";
        var x = 0;
        for (x = 0; x < e.value.length; x++) {
                var y = 0;
                for (y = 0; y < validNumbers.length; y++) {
                        if (e.value.charAt(x) == validNumbers[y]) {
                                break;
                        }
                }
                if (y == validNumbers.length) {
                        e.value = e.value.substring(0, x) + e.value.substring(x+1, e.value.length);
                        x--;
                }
        }
        if (e.value == "") e.value = "0";
}

function writeDateLabels(onLoad) {
        var month;
        var day;
        var year;
        var date;
        if (document.timesheet.formDateText.value != "") { date = document.timesheet.formDateText.value; }
        else { date = document.timesheet.formDateSelect.options[document.timesheet.formDateSelect.selectedIndex].text; }
        month = date.substr(0, 2);
        day = date.substr(3, 2);
        year = date.substr(6, 4);

        month *= 1; //trick javascript into making month a number and not a string
        day *= 1; //same
        year *= 1; //same

        day -= 6;
        
        if (day < 1) {
                month--;
                if (month < 1) { month = 12; year--;}
                day = day + getDaysInMonth(month, year);
        }

        var formLabels = new Array(document.timesheet.labelMon, document.timesheet.labelTue, document.timesheet.labelWed, document.timesheet.labelThu, document.timesheet.labelFri, document.timesheet.labelSat, document.timesheet.labelSun);

        var x;
        for (x = 0; x < 7; x++) {
                year+='';
                if (month < 10 && day < 10) {
                        formLabels[x].value = "0" + month + "/0" + day + "/" + year.substr(2, 2);
                }
                else if (month < 10) {
                        formLabels[x].value = "0" + month + "/" + day + "/" + year.substr(2, 2);
                }
                else if (day < 10) {
                        formLabels[x].value = month + "/0" + day + "/" + year.substr(2, 2);
                }
                else {
                        formLabels[x].value = month + "/" + day + "/" + year.substr(2, 2);
                }

                day++;
                if (day > getDaysInMonth(month, year)) {
                        day = 1;
                        month++;
                        if (month > 12) { month = 1; year++; }
                }
        }
        if (!onLoad) {
                document.timesheet.formMon.focus();
                document.timesheet.formMon.select();
        }
}

//NO longer used
/*function printerFriendly(text)
{
      win = window.open("","","toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,width=600,height=650");
      doc = win.document;
      doc.write(text);
}*/

function validateTimesheet(holidays) {
        addHours(document.timesheet);
        var errors = "";
        if (document.timesheet.formEmail.value.search("@") == -1) {
                errors += "Your e-mail address is invalid.\n";
        }
        if (document.timesheet.formSupervisorEmail.value.search("@") == -1) {
                errors += "Your supervisor e-mail address is invalid.\n";
        }
        errors += validateDateText() + "\n";
        if (document.timesheet.formMon.value < 0 || document.timesheet.formMon.value > 24) errors += "Invalid number of hours for Monday.\n";
        if (document.timesheet.formTue.value < 0 || document.timesheet.formTue.value > 24) errors += "Invalid number of hours for Tuesday.\n";
        if (document.timesheet.formWed.value < 0 || document.timesheet.formWed.value > 24) errors += "Invalid number of hours for Wednesday.\n";
        if (document.timesheet.formThu.value < 0 || document.timesheet.formThu.value > 24) errors += "Invalid number of hours for Thursday.\n";
        if (document.timesheet.formFri.value < 0 || document.timesheet.formFri.value > 24) errors += "Invalid number of hours for Friday.\n";
        if (document.timesheet.formSat.value < 0 || document.timesheet.formSat.value > 24) errors += "Invalid number of hours for Saturday.\n";
        if (document.timesheet.formSun.value < 0 || document.timesheet.formSun.value > 24) errors += "Invalid number of hours for Sunday.\n";

        if (trim(errors) != "") {
                alert("The following errors have occurred:\n" + errors);
                return false;
        }
        var date;
        if (document.timesheet.formDateText.value != "") { 
          date = document.timesheet.formDateText.value; 
        }
        else { date = document.timesheet.formDateSelect.options[document.timesheet.formDateSelect.selectedIndex].text; }
        var str = "";

        var holiday_array = holidays.split("|");
        for (var i = 0; i < holiday_array.length; i++) {
          holiday_array[i] = holiday_array[i].split(" - ");
        }
        var timesheet = document.timesheet;
        var days = new Array();
        days[0] = new Array(timesheet.labelMon.value, timesheet.formMon.value);
        days[1] = new Array(timesheet.labelTue.value, timesheet.formTue.value);
        days[2] = new Array(timesheet.labelWed.value, timesheet.formWed.value);
        days[3] = new Array(timesheet.labelThu.value, timesheet.formThu.value);
        days[4] = new Array(timesheet.labelFri.value, timesheet.formFri.value);
        days[5] = new Array(timesheet.labelSat.value, timesheet.formSat.value);
        days[6] = new Array(timesheet.labelSun.value, timesheet.formSun.value);

        for (i = 0; i < holiday_array.length; i++) {
          for (var j = 0; j < days.length; j++) {
            if (days[j][0] == holiday_array[i][0] && days[j][1] != "0") {
              if (str == "") {
                str = "Note also that you have entered hours for the holiday: ";
                str += holiday_array[i][1];
              }
              else {
                str += ", " + holiday_array[i][1];
              }
            }
          }
        }
        if (str != "") str += ".\n";
        var result = confirm('You have prepared a timesheet for the week ending ' + date + '.\n' + str + 'If all is correct, click OK.  If not, click Cancel to make changes.');
        return result;
}

function resetDateText() {
         document.timesheet.formDateText.value = "";
}

var numb = '0123456789';
var lwr = 'abcdefghijklmnopqrstuvwxyz';
var upr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';

function isValid(parm,val) {
        if (parm == "") return true;
        for (i=0; i<parm.length; i++) {
            if (val.indexOf(parm.charAt(i),0) == -1) return false;
        }
        return true;
}

function isNum(parm) {return isValid(parm,numb);}

function DayOfWeek(day,month,year) {
    var a = Math.floor((14 - month)/12);
    var y = year - a;
    var m = month + 12*a - 2;
    var d = (day + y + Math.floor(y/4) - Math.floor(y/100) +
             Math.floor(y/400) + Math.floor((31*m)/12)) % 7;
    return d;
}

function validateDateText() {
        var date = document.timesheet.formDateText.value;
        var x;
        if (date == "") return "";

        for (x = 0; x < date.length; x++) {
                if (x == 2 || x == 5) continue;
                if (!isNum(date.charAt(x))) break;
        }

        if (x != 10 || date.charAt(2) != '/' || date.charAt(5) != '/') {
                return "You have not entered the date in the right format: MM/DD/YYYY";
        }

        var month = parseFloat(date.substr(0, 2));
        var day = parseFloat(date.substr(3, 2));
        var year = parseFloat(date.substr(6, 4));

        if (month < 1 || month > 12 || day < 1 || day > 31) {
                return "Invalid work date";
        }

        if (DayOfWeek(day, month, year) != 0) {
                return "The work date given is not a Sunday.";
        }
        return "";
}

function addHours(form) {
        document.timesheet.formTotal.value = parseFloat(document.timesheet.formMon.value) + parseFloat(document.timesheet.formTue.value) + parseFloat(document.timesheet.formWed.value) + parseFloat(document.timesheet.formThu.value) + parseFloat(document.timesheet.formFri.value) + parseFloat(document.timesheet.formSat.value) + parseFloat(document.timesheet.formSun.value);
}