/* Form data handling and verification */

/* if we need it, we can use this to set the focus to the first data entry field we wnat the user to start with*/
function setFocus() {
  document.getElementById("name").focus();
}

/* Moving along now, simple verification functions*/
function isblank(s) {
  for(var i = 0; i < s.length; i++) {
    var c = s.charAt(i);
    if ((c != ' ') && (c != '\n') && (c != '')) return false;
  }
  return true;
}

function verify(f) {
  var msg;
  var empty_fields = "";
  var errors = "";
  
  for(var i = 0; i < f.length; i++) {
    var e = f.elements[i];
    if (((e.type == "text") || (e.type == "textarea")) && (!e.optional)) {
      if ((e.value == null) || (e.value == "") || isblank(e.value)) {
        empty_fields += "\n        " + e.name;
        continue;
      }
  
      if (e.numeric || (e.min != null) || (e.max != null)) {
        var v = parseFloat(e.value);
        if (isNaN(v) || ((e.min != null) && (v < e.min)) || ((e.max != null) && (v > e.max))) {
          errors += "- The field " + e.name + " must be a number";
          if (e.min != null) errors += " that is greater than " + e.min;
          if (e.max != null && e.min != null) errors += " and less than " + e.max;
          else if (e.max != null) errors += "that is less than " + e.max;
          errors += ".\n";
        }
      }
    }
  }
  var at=document.getElementById("email").value.indexOf("@")
  if (at==-1) {
    errors += "Invalid E-Mail address.\n";
  }
  
  if (!empty_fields && !errors) {
    /* Do more stuff*/
    return true;
  }
  
  msg = "******************************************************\n\n";
  msg += "The form was not submitted because of the\n";
  msg += "following errors:\n\n";
  if (empty_fields) {
    msg += "- The required field(s) are empty:" + empty_fields + "\n";
    if (errors) msg += "\n";
  }
  msg += errors;
  msg += "Please correct errors and re-submit\n\n";
  msg += "Thank You!\n\n";
  msg += "******************************************************\n\n";

  alert(msg);
  return false;
   
}

/* misc pop-up windows */
function open_info(what) {
  var page = what;
  window.open (page, "wbITConsulting,LLC InfoPanel", "status=no, toolbar=no, scrollbars=yes, resizable=no, width=400, height=300", false);
}

function open_nosfacts(what) {
  window.open (what, "wbITC NoS Facts","status=no, toolbar=no, scrollbars=no, resizable=no, width=800, height=600", false);
}

/* login page stuff */
function writeAppDetails(appId, appName, appDescr){
    var id = document.getElementById('appId');
    var name = document.getElementById('appName');
    var description = document.getElementById('appDescr');
    
    id.innerHTML = appId;
    name.innerHTML = appName;
    description.innerHTML = appDescr;
}
function forgotPasswd(){
    //if the errorDiv is present, increase the heigth of the container
    if(document.getElementById('errorDiv')){
	document.getElementById('container').style.height='540px';
	document.getElementById('form').style.height='180px';
    }

    var forgotPasswdForm = document.getElementById('forgotpasswd');
    forgotPasswdForm.style.display='block';
}
function openId(){
    document.openIdForm.style.display='block';
}
function hide_openId(){
    document.openIdForm.style.display='none';
}
function hide_forgotpasswd(){
    var forgotPasswdForm = document.getElementById('forgotpasswd');
    forgotPasswdForm.style.display='none';
}
