window.onload = function () {
  if (document.getElementById && document.createTextNode) {
    switch(window.location.pathname.substring(window.location.pathname.lastIndexOf("/") + 1, window.location.pathname.lastIndexOf('.'))) {//highlight current page (where appropriate)
      case "index":
        activateLink("lHome");
        break;
      case "about":
        activateLink("lAbout");
        break;
      case "lgm":
        activateLink("lLGM");
        break;
      case "lgw":
        activateLink("lLGW");
        break;
      default:
        if (window.location.pathname.substring(window.location.pathname.lastIndexOf("/")) == "/") {
          activateLink("lHome");
          break;
        }
    }
    if (document.getElementById("Login")) {
      document.getElementById("Login").onsubmit = function(){return validateForm(this);};
    }
  }
}

function activateLink(id) {//set link parent's class to active
  var el = document.getElementById(id);
  if (el && el.parentNode) {
    el.parentNode.className += " active";
  }
}

function removeClass(element, className) {
  element.className = element.className.replace(className, "");
};

//simplified validation - only two fields and both are required.
function validateInput(el) {
  var valid = true;
  var p = el.parentNode;
  removeClass(p,"invalid");
  if (p.getElementsByTagName("br").length>0) {
    p.removeChild(p.getElementsByTagName("br")[0]);//remove previous br tag
  }
  if (p.getElementsByTagName("em").length>0) {
    p.removeChild(p.getElementsByTagName("em")[0]);//remove previous em tag
  }
  valid = el.value.length!=0;
  if (!valid) {
    p.appendChild(document.createElement("br"));
    var em = document.createElement("em");
    em.appendChild(document.createTextNode("This field is required"))
    p.appendChild(em);
    p.className+=" invalid";
  }
  return valid;
}

function validateForm(f) {
  try {
    var valid = true;
    if (!validateInput(f.UN)) { valid = false; }
    if (!validateInput(f.PW)) { valid = false; }
    if (valid) {
      //alert("submit!");return false;//debug
      return true;
    } else {
      alert("The form could not be submitted. Please ensure all fields are entered.");
      return false;
    }
  } catch(e) {
    alert("An error has occurred:\n"+e);//display the error
    return true;//submit form
  }
}