// Declaring required variables
var digits = "0123456789";
// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "( )-";
// characters which are allowed in international phone numbers
// (a leading + is OK)
var validWorldPhoneChars = phoneNumberDelimiters + "+";
// Minimum no of digits in an international phone no.
var minDigitsInIPhoneNumber = 10;

function req(contactField, contactLabel) {
<!-- Check for non-blank field -->
  var result = true;
  if (contactField.value == "") {
    alert('Please enter a value for the "' + contactLabel +'" field.');
    contactField.focus();
    result = false;
  }
  return result;
}

function checkMessage(contactField, contactLabel) {
<!-- Check for "valid" message (no links) -->
  var result = true;
  if (contactField.value != "") {
    var tempstr = new String(contactField.value);
    var httpindex = tempstr.indexOf("http");
    var htmlindex = tempstr.indexOf("html");
	var hrefindex = tempstr.indexOf("href");
	var urlindex = tempstr.indexOf("url");
    if ((httpindex >= 0) || (htmlindex >= 0) || (hrefindex >= 0) || (urlindex >= 0)) {
	  result = false;
      }
    }
  if (!result) {
    alert("No links or urls allowed in message please!");
    contactField.focus();
    contactField.value = "";
    }
  return result;
}

function echeck(checkit) {
  var str=checkit.value;
  var at="@"
  var dot="."
  var lat=str.indexOf(at)
  var lstr=str.length
  var ldot=str.indexOf(dot)
  if ((str == "") || (str == null)) {
    alert("Please enter a value for the email field");
	checkit.focus();
	return false;
  }
  if (str.indexOf(at)==-1){
    alert("Please enter a valid email address in the form: yourname@yourdomain.com");
	checkit.focus();
	checkit.value = "";
    return false;
  }

  if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
    alert("Please enter a valid email address in the form: yourname@yourdomain.com");
	checkit.focus();
	checkit.value = "";
    return false;
  }

  if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
    alert("Please enter a valid email address in the form: yourname@yourdomain.com");
	checkit.focus();
	checkit.value = "";
    return false;
  }

  if (str.indexOf(at,(lat+1))!=-1){
    alert("Please enter a valid email address in the form: yourname@yourdomain.com");
	checkit.focus();
	checkit.value = "";
    return false;
  }

  if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
    alert("Please enter a valid email address in the form: yourname@yourdomain.com");
	checkit.focus();
	checkit.value = "";
    return false;
  }

  if (str.indexOf(dot,(lat+2))==-1){
    alert("Please enter a valid email address in the form: yourname@yourdomain.com");
	checkit.focus();
	checkit.value = "";
    return false;
  }
		
  if (str.indexOf(" ")!=-1){
    alert("Please enter a valid email address in the form: yourname@yourdomain.com");
	checkit.focus();
	checkit.value = "";
    return false;
  }
  return true;				
}

function isInteger(s)
{
  var i;
  for (i = 0; i < s.length; i++) {   
    // Check that current character is number.
    var c = s.charAt(i);
    if (((c < "0") || (c > "9"))) return false;
  }
  // All characters are numbers.
  return true;
}

function stripCharsInBag(s, bag)
{
  var i;
  var returnString = "";
  // Search through string's characters one by one.
  // If character is not in bag, append to returnString.
  for (i = 0; i < s.length; i++) {   
    // Check that current character isn't whitespace.
    var c = s.charAt(i);
    if (bag.indexOf(c) == -1) returnString += c;
  }
  return returnString;
}

function checkInternationalPhone(strPhone) {
  s=stripCharsInBag(strPhone.value, validWorldPhoneChars);
  return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}

function validatePhone(phoneNumber) {
  if (checkInternationalPhone(phoneNumber) == false) {
    alert("Please enter a valid phone number.");
	phoneNumber.focus();
	phoneNumber.value = "";
    return false;
  }
  return true;
}

function valform (validateform) {
<!-- Validate form fields as specified below -->
<!-- Validate name -->
  if (!req(validateform.name, "Name")) {
     return false;
  }
  <!-- Validate email -->
  if (!echeck(validateform.email)) {
    return false;
  }
  <!-- Check message -->
  if (!checkMessage(validateform.message, "Message")) {
    return false;
  }
  <!-- Check phone number if it exists -->
  if (validateform.phone.value != "") {
    if (!validatePhone(validateform.phone)) return false;
  }
  return true;
}