function trimLeft(str) {
  var whitespace = new String(" \t\n\r");
  var s = new String(str);
  if (whitespace.indexOf(s.charAt(0)) != -1) {
    var j = 0;
    var i = s.length;
    while (j < i && whitespace.indexOf(s.charAt(j)) != -1) {
      j++;
      s = s.substring(j, i);
    }
  }
  return s;
}

function trimRight(str) {
  var whitespace = new String(" \t\n\r");
  var s = new String(str);
  if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
    var i = s.length - 1;
    while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1) {
      i--;
      s = s.substring(0, i+1);
    }
  }
  return s;
}

function trimBoth(str) {
  return trimRight(trimLeft(str));
}

function checkContactForm() {
  var fNameVal = document.forms['contactForm'].first_name.value;
  if (!fNameVal || trimBoth(fNameVal).length == 0) {
    alert("Please provide your first name before clicking submit");
    document.forms['contactForm'].first_name.focus();
    return false;
  }

  var lNameVal = document.forms['contactForm'].last_name.value;
  if (!lNameVal || trimBoth(lNameVal).length == 0) {
    alert("Please provide your last name before clicking submit");
    document.forms['contactForm'].last_name.focus();
    return false;
  }
  
  var cVal = document.forms['contactForm'].city.value;
  if (!cVal || trimBoth(cVal).length == 0) {
    alert("Please provide your city before clicking submit");
    document.forms['contactForm'].city.focus();
    return false;
  }
  
  var sVal = document.forms['contactForm'].state.value;
  if (!sVal || trimBoth(sVal).length == 0) {
    alert("Please provide your state before clicking submit");
    document.forms['contactForm'].state.focus();
    return false;
  }
  
  var pVal = document.forms['contactForm'].phone.value;
  if (!pVal || trimBoth(pVal).length == 0) {
    alert("Please provide your phone number before clicking submit");
    document.forms['contactForm'].phone.focus();
    return false;
  }
  
  var eVal = document.forms['contactForm'].email.value;
  if (!eVal || trimBoth(eVal).length == 0) {
    alert("Please provide your email address before clicking submit");
    document.forms['contactForm'].email.focus();
    return false;
  }
  
  return true;
}
