// These functions validate the form before submission.
// Preload images
var empty = new Image(); empty.src = "./form-fieldempty.gif";
var email = new Image(); email.src = "./form-emailerror.gif";
var phone = new Image(); phone.src = "./form-phoneerror.gif";
var haveerrors = 0;
 
function showImage(imagename, imageurl, errors) {
  document[imagename].src = imageurl;
  if (!haveerrors && errors) {
     haveerrors = errors;
     }
}
 
 
function validateForm(f) {
  haveerrors = 0;
  if (f.fname.value.length < 1) {
     showImage("nameerror", "./form-fieldempty.gif", true);
     }
  else {
     showImage("nameerror", "./form-blankimage.gif", false);
     }
  phonenumlength = f.area.value.length + f.exchange.value.length + f.number.value.length;
  if (phonenumlength == 10) { // phone structure
     phoneval = "(" + f.area.value + ") " + f.exchange.value + "-" + f.number.value;
     f.phonenum.value = phoneval;
     }
  if (f.email.value.search("@") == -1 || f.email.value.search("[.*]") == -1) {
     showImage("emailerror", "./form-emailerror.gif", true);
     }
  else {
     showImage("emailerror", "./form-blankimage.gif", false);
     }
  return (!haveerrors);
}
 
function keyCheck(input,len, e) {
  // Funtion only allows entry of numeric data and auto tabs to next field
  var isNN = (navigator.appName.indexOf("Netscape")!=-1);
  var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
  var str=input.value
  if (window.event) {
     keyCode=e.keyCode;
     }
  else {
     if (e.which) {
        keyCode=e.which;
        }
     else {
        return
        }
     }
  if ((keyCode<48 || keyCode >58)) { // Allow only integers
     return false;
     }
  if (input.value.length >= len && !containsElement(filter,keyCode)) {
     input.value = input.value.slice(0, len);
     input.form[(getIndex(input)+1) % input.form.length].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;
}
 
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;
        }
  return true;
}

function showForm(idx) {
  formId = "theFormDiv" + idx;
  oDiv = document.getElementById(formId);
  oDiv.style.display='block';
  return;
}

function hideForm(idx) {
  formId = "theFormDiv" + idx;
  oDiv = document.getElementById(formId);
  oDiv.style.display='none';
}


