var emailAlert = 1 ;

function IsEmailValid(oEmail)
{
  email = oEmail.value
  if (email!='') {
    r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)")
    r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$")


    retVal = (!r1.test(email) && r2.test(email) )
    if (!retVal) {
      if (emailAlert==1) {
        alert('Error - Not a valid email address')
      }
      oEmail.select();
      oEmail.focus();
    }
    return retVal
  } else {
    return false;
  }
  emailAlert = 1 ;
}

function maskPhoneNumber(objEvent) {
 var iKeyCode, strKey;

//browser detection
var strUserAgent = navigator.userAgent.toLowerCase();
var isIE = strUserAgent.indexOf("msie") > -1;
var isNS6 = strUserAgent.indexOf("netscape6") > -1;
var isNS4 = !isIE && !isNS6  && parseFloat(navigator.appVersion) < 5;

//regular expressions
var reValidChars = /\d/;
var reKeyboardChars = /[\s\x08\x0D\x00\+ext\(\)]/;

 if (isIE) {
   iKeyCode = objEvent.keyCode;
 } else {
   iKeyCode = objEvent.which;
 }
 strKey = String.fromCharCode(iKeyCode);

if (!reValidChars.test(strKey) && !reKeyboardChars.test(strKey)) {
   return false;
 }
}//end maskKeyPress Function


function IsPhoneNumber(sVal) {
  //regular expressions
  var reValidChars = /\d/;
  var reKeyboardChars = /[\s\x08\x0D\x00\+\(\)]/;
  var reExtension = /[ext]/ ;
  var stringLength = sVal.length
  retVal = true
  //loop through each char in the phone number string
  for(i=1;i<stringLength;i++) {
    if (!reValidChars.test(sVal.charAt(i)) && !reKeyboardChars.test(sVal.charAt(i)) && !reExtension.test(sVal.charAt(i)) ) {
     retVal = false;
     //stop the looping by making i = max length
     i=stringLength + 1
    }
  }
  return retVal
}


function validateContactUs(oForm) {

  //start with the assumption that everything is good
  ret = true;
  //start with standard error text
  errString = "Warning - there was a problem with the information you provided\n\r-----------------------------------------------\n\r\n\r" ;
  //is the email address valid ?
  oEmail = oForm.email ;
  emailAlert = 0 ;
  if (!IsEmailValid(oEmail)) {
    errString+="- Email Address is not valid\n\r"
    ret = false;
  }

  //test phonenumbers
  if (!IsPhoneNumber(oForm.tel.value) ) {
    errString+="- Telephone Number is not valid, can only contain numbers,+,() or 'ext' \n\r"
    ret = false;
  }
  //fax number
  if (!IsPhoneNumber(oForm.fax.value) ) {
    errString+="- Fax Number is not valid, can only contain numbers,+,() or 'ext' \n\r"
    ret = false;
  }

  if(!ret) {
    alert(errString)
  }

  return ret;

}