// etx.js
// Javascript for the ETX public web site

// Cookie variables (not used yet)
var never = new Date()
never.setTime(never.getTime() + 2000*24*60*60*1000);
// name is a string of the name of your cookie
// value is the value corresponding to name
function SetCookie(name, value) {
 var expString = "; expires=" + never.toGMTString();
 document.cookie = name + "=" + escape(value) + expString;
}

// returns value of cookie or null if cookie does not exist
function GetCookie(name) {
 var result = null;
 var myCookie = " " + document.cookie + ";";
 var searchName = " " + name + "=";
 var startOfCookie = myCookie.indexOf(searchName);
 var endOfCookie;
 if (startOfCookie != -1) {
  startOfCookie += searchName.length; // skip past name of cookie
  endOfCookie = myCookie.indexOf(";", startOfCookie);
  result = unescape(myCookie.substring(startOfCookie, endOfCookie));
 }
 return result;
}

function openBrochureWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function processOnLoad() {
  window.focus();
  var curr_url = location.href;
  if (curr_url.search("auto_brochure.html") != -1 ) {
    // alert("Current URL is " + curr_url);
    openBrochureWindow('brochure.htm','','scrollbars=yes,resizable=yes,width=478,height=360');
  }
  if (document.forms.length > 0) {
    for (var i = 0; i < document.forms[0].elements.length; i++) {
      if (document.forms[0].elements[i].type != "hidden") {
        document.forms[0].elements[i].focus();
        break;
      }
    }
    // Login form may have used a persistent cookie
    if (document.forms[0].name == "login") {
      var userid = GetCookie("userid");
      var phone = GetCookie("phone");   
      //alert("Cookie values. userid: " + userid + "  phone: " + phone); 
      if ((userid != null) && (phone != null)) {
        document.login.login_userid.value = userid;
        document.login.login_phone.value = phone;
        document.login.remember_me.value = '1';
        // document.login.submit();
      }
    }
    // timesheet entry has to do some calculations
    if (document.forms[0].name == "tsheet_details") {
      calc_hrs(document.tsheet_details.mon_start_hrs);
      calc_hrs(document.tsheet_details.tue_start_hrs);
      calc_hrs(document.tsheet_details.wed_start_hrs);
      calc_hrs(document.tsheet_details.thu_start_hrs);
      calc_hrs(document.tsheet_details.fri_start_hrs);
      calc_hrs(document.tsheet_details.sat_start_hrs);
      calc_hrs(document.tsheet_details.sun_start_hrs);
    }
  }
}

function submitJoin() {
  if (document.join.user_email.value == "") {
    alert("You must supply an email address to join");
    return false;
  }
  if (document.join.user_phone.value == "") {
    alert("You must supply some sort of telephone number to join");
    return false;
  }

  document.join.submit();
}

function submitLogin() {
  if (document.login.login_userid.value == "") {
    alert("You must supply your membership number to login");
    return false;
  }
  if (document.login.login_phone.value == "") {
    alert("You must supply the telephone number to login");
    return false;
  }

  document.login.submit();
}

function validateUploadCVFormData() {
  if (document.upload.userfile.value == "") {
    alert("You must select a file on your hard drive to upload!");
    return false;
  }
  return true;
}

function submitUploadCV() {
  // Validate all the form inputs
  if (!validateUploadCVFormData()) {
    return false;
  }

  // Now submit...
  document.upload.submit();
}

function validateApplyFormData() {
  if (document.upload.userfile.value == "") {
    alert("You must select a file on your hard drive to upload!");
    return false;
  }
  if (document.upload.FirstName.value == "") {
    alert("First name field is empty - all fields must be completed");
    return false;
  }
  if (document.upload.Lastname.value == "") {
    alert("Last name field is empty - all fields must be completed");
    return false;
  }
  if (document.upload.Phone.value == "") {
    alert("Phone field is empty - all fields must be completed");
    return false;
  }
  if (document.upload.Email.value == "") {
    alert("Email field is empty - all fields must be completed");
    return false;
  }
  return true;
}


function submitApplyCV() {
  // Validate all the form inputs
  if (!validateApplyFormData()) {
    return false;
  }

  // Now submit...
  document.upload.submit();
}

function jsreplace(string,text,by) {
// Replaces text with by in string
    var strLength = string.length, txtLength = text.length;
    if ((strLength == 0) || (txtLength == 0)) return string;

    var i = string.indexOf(text);
    if ((!i) && (text != string.substring(0,txtLength))) return string;
    if (i == -1) return string;

    var newstr = string.substring(0,i) + by;

    if (i+txtLength < strLength)
        newstr += replace(string.substring(i+txtLength,strLength),text,by);

    return newstr;
}

function valid_tsheet(form) {
  var hrs = form.total_hrs.value;  
  if (isNaN(hrs) || (hrs == "")) { hrs = 0; }
  if ((hrs < 0) || (hrs >= 168)) {
    alert("Your timesheet hours are incomplete.");
    return false;
  }

  if (!valid_day_hrs(form.mon_hrs.value)) { return false; }
  if (!valid_day_hrs(form.tue_hrs.value)) { return false; }

  return true;
}

function valid_day_hrs(hrs) {
  if (isNaN(hrs) || (hrs == "")) { hrs = 0; }
  if ((hrs < 0) || (hrs > 24)) {
    alert("Your timesheet hours are incomplete.");
    return false;
  }
  return true;
}

function calc_hrs(target) {
  var item = target.name;
  var result = Math.round(target.value);
  if (target.value > 0) { target.value = result; } // Ensure no decimals
  // Validate the hours and minutes
  if (item.substring(item.length - 3) == "hrs") {
    if ((result > 23) || (result < 0)) {
      alert("Hours must be between 0 and 23");
      target.value = "";
      return false;
    }
  }
  if (item.substring(item.length - 3) == "ins") {
    if ((result > 60) || (result < 0)) {
      alert("Minutes must be between 0 and 59");
      target.value = "";
      return false;
    }
  }
  // Get the name of the current "day" and the values
  var form = "tsheet_details.";
  var prefix = form + target.name.substring(0, 3);
  var start_hrs = eval(prefix + "_start_hrs.value");
  var start_mins = eval(prefix + "_start_mins.value");
  var finish_hrs = eval(prefix + "_finish_hrs.value");
  var finish_mins = eval(prefix + "_finish_mins.value");
  var break_hrs = eval(prefix + "_break_hrs.value");
  var break_mins = eval(prefix + "_break_mins.value");
  
  var day_hrs = calc_day_hrs(start_hrs,start_mins,finish_hrs,finish_mins,break_hrs,break_mins);
  eval(prefix + "_hrs.value = day_hrs");
  // alert(eval(prefix + "_hrs.name"));
  
  calc_total_hrs();
  
  // Tab to first form field
  var curr_idx = getIndex(target);
  if (target.form[curr_idx].name == "sun_break_mins") { target.form[1].focus(); }
  return true;
}

function calc_total_hrs() {
  var mon_hrs = tsheet_details.mon_hrs.value;
  var tue_hrs = tsheet_details.tue_hrs.value;
  var wed_hrs = tsheet_details.wed_hrs.value;
  var thu_hrs = tsheet_details.thu_hrs.value;
  var fri_hrs = tsheet_details.fri_hrs.value;
  var sat_hrs = tsheet_details.sat_hrs.value;
  var sun_hrs = tsheet_details.sun_hrs.value;

  if (isNaN(mon_hrs) || (mon_hrs == "")) { mon_hrs = 0; }
  if (isNaN(tue_hrs) || (tue_hrs == "")) { tue_hrs = 0; }
  if (isNaN(wed_hrs) || (wed_hrs == "")) { wed_hrs = 0; }
  if (isNaN(thu_hrs) || (thu_hrs == "")) { thu_hrs = 0; }
  if (isNaN(fri_hrs) || (fri_hrs == "")) { fri_hrs = 0; }
  if (isNaN(sat_hrs) || (sat_hrs == "")) { sat_hrs = 0; }
  if (isNaN(sun_hrs) || (sun_hrs == "")) { sun_hrs = 0; }

  var result = parseFloat(mon_hrs) + parseFloat(tue_hrs) + parseFloat(wed_hrs) + parseFloat(thu_hrs) + parseFloat(fri_hrs) + parseFloat(sat_hrs) + parseFloat(sun_hrs);
  tsheet_details.total_hrs.value = (Math.round(result * 100))/100;
}


function calc_day_hrs(start_hrs,start_mins,finish_hrs,finish_mins,break_hrs,break_mins) {
  var startTime = new Date();
  var finishTime = new Date();

  startTime.setHours(0, 0);
  finishTime.setHours(0, 0);
  startTime.setHours(start_hrs, start_mins);
  finishTime.setHours(finish_hrs, finish_mins);

  var breakMsec = (break_hrs * 60 * 60 * 1000) + (break_mins * 60 * 1000);
  
  var timeDifference = finishTime - startTime;
  var timeWorkedMsec = timeDifference - breakMsec;
  
  var timeWorked = timeWorkedMsec / (1000 * 60 * 60);
  var hoursWorked = (Math.round(timeWorked * 100))/100;
  return hoursWorked;
}

function autoTab(input,len, e) {
var isNN = (navigator.appName.indexOf("Netscape")!=-1);
var keyCode = (isNN) ? e.which : e.keyCode; 
var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
// Window.alert("In autoTab");
if(input.value.length >= len && !containsElement(filter,keyCode)) {
input.value = input.value.slice(0, len);
// Remove spaces (not working with autotabbibg, yet...)
// input.value=jsreplace(input.value,' ','');
var next_idx = (getIndex(input)+1) % input.form.length;
// If disabled, skip. (Should cycle through but know there is only one... for now.)
if (input.form[next_idx].disabled==true) {
  if (input.form[next_idx].name == "sun_hrs") { next_idx = 0; }
  next_idx = next_idx + 1;
}
input.form[next_idx].focus();
}
function containsElement(arr, ele) {
var found = false, index = 0;
while(!found && index < arr.length)
if(arr[index] == ele)
found = true;
else
index++;
return found;
}
return true;
}

function getIndex(input) {
var index = -1, i = 0, found = false;
while (i < input.form.length && index == -1)
if (input.form[i] == input)index = i;
else i++;
return index;
}

function formOnChange (target) {
  alert("Form on Change " + target.name);
}
