
// REGULAR JS FUNCTIONS

function r_on(i) {
  if (document.images) { 
    if (document[i].src.indexOf("down") < 0) {
      document[i].src = "images/" + i + "_over.gif"; 
    }
  }
}

function r_off(i) {
  if (document.images) { 
    if (document[i].src.indexOf("down") < 0) {
      document[i].src = "images/" + i + ".gif"; 
    }
  }
}


function s_on(i) {
  if (document.images) { 
    if (document[i].src.indexOf("down") < 0) {
      document[i].src = "/images/" + i + "_over.gif"; 
    }
  }
}

function s_off(i) {
  if (document.images) { 
    if (document[i].src.indexOf("down") < 0) {
      document[i].src = "/images/" + i + ".gif"; 
    }
  }
}


function writeDate() {
  document.write(new Date().getFullYear());
}

// FORM VALIDATION

function checkContact(which) {
  if (document.contact.firstname.value.length < 1) {
      alert("The FIRST NAME field is required. Please enter a name and try again.");
      return false;
  }
  if (!isAlphabet(document.contact.firstname.value)) {
      alert("You have entered an invalid FIRST NAME. Please enter a name and try again.");
      return false;
  }
  if (document.contact.lastname.value.length < 2) {
      alert("The LAST NAME field is required. Please enter a name and try again.");
      return false;
  }
  if (!isAlphabet(document.contact.lastname.value)) {
      alert("You have entered an invalid LAST NAME. Please enter a name and try again.");
      return false;
  }
  if (document.contact.address.value.length < 2) {
      alert("The ADDRESS field is required. Please enter a name and try again.");
      return false;
  }
  if (document.contact.city.value.length < 2) {
      alert("The CITY field is required. Please enter a name and try again.");
      return false;
  }
  if (document.contact.state.value.length < 2) {
      alert("The STATE field is required. Please enter a name and try again.");
      return false;
  }
  if (document.contact.zipcode.value.length < 5) {
      alert("The ZIP CODE field is required. Please enter a name and try again.");
      return false;
  }
  if (document.contact.phone.value.length == 0) {
      alert("The PHONE NUMBER field is required. Please enter a phone number and try again.");
      return false;
  }
  if (!checkPhone(document.contact.phone.value)) {
      alert("You have entered an invalid PHONE NUMBER. Please make sure the number you have entered is at least 10 digits long, does not include any errors and try again.");
      return false;	      
  }
  if (document.contact.email.value.length == 0) {
      alert("The EMAIL field is required. Please enter an e-mail address and try again.");
      return false;
  }
  if (!echeck(document.contact.email.value)) {
      alert("You have entered an invalid EMAIL. Please enter a valid e-mail address and try again.");
      return false;
  }
  return true;
}

// TELEPHONE VALIDATION

var digits = "0123456789";
var phoneNumberDelimiters = "()- ";
var minDigitsInIPhoneNumber = 10;

function isInteger(s) {   var i;
  for (i = 0; i < s.length; i++) {   
    var c = s.charAt(i);
      if (((c < "0") || (c > "9"))) return false;
  }
  return true;
}

function trim(s) {
  var i;
  var returnString = "";
  for (i = 0; i < s.length; i++) {   
    var c = s.charAt(i);
    if (c != " ") returnString += c;
  }
  return returnString;
}

function stripCharsInBag(s, bag) {
  var i;
  var returnString = "";
  for (i = 0; i < s.length; i++) {   
    var c = s.charAt(i);
    if (bag.indexOf(c) == -1) returnString += c;
  }
  return returnString;
}

function checkPhone(strPhone){
  var bracket=3
  strPhone=trim(strPhone)
  s=stripCharsInBag(strPhone,phoneNumberDelimiters);
  return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}

//EMAIL VALIDATION

function echeck(str) {
  var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
  if (filter.test(str)) {
    return true;
  } else {
    return false;
  }
}

// NAME VALIDATION
function isAlphabet(elem){
  var i = stripCharsInBag(elem,phoneNumberDelimiters);
  var alphaExp = /^[a-zA-Z]+$/;
  if(i.match(alphaExp)){
    return true;
  } else {
    return false;
  }
}

