﻿
function Checkfielddetails(theForm) {
var reason = "";

  reason += validateContactName(theForm.ContactName);
  reason += validateEmailAddress(theForm.EmailAddress);
  reason += validateTelephoneNumber(theForm.TelephoneNumber);
      

  if (reason != "") {
    alert("Please correct the following fields:\n" + reason);
    return false;
  }

  return true;
}


function validateContactName(fld) {
    var error = "";
    var illegalChars = /\W/; // allow letters, numbers, and underscores
 
    if (fld.value == "") {
        fld.style.background = '#FFD700'; 
        error = "You did not enter a Contact Name.\n";
    } else if ((fld.value.length < 3) || (fld.value.length > 30)) {
        fld.style.background = '#FFD700'; 
        error = "The contact name is the wrong length.\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = '#FFD700'; 
        error = "The contact name contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    } 
    return error;
}



function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
} 


function validateEmailAddress(fld) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
    
    if (fld.value == "") {
        fld.style.background = '#FFD700';
        error = "You did not enter an Email Address.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = '#FFD700';
        error = "Please enter a valid Email Address.\n";
        
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = '#FFD700';
        error = "The Email Address contains illegal characters.\n";
        
    } else {
        fld.style.background = 'White';
    }
    return error;
}



function validateTelephoneNumber(fld) {
   var error="";
   var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');    


   if (fld.value== "") {
        fld.style.background = '#FFD700';
        error = "You did not enter a Telephone Number.\n";     
    } else if (isNaN(parseInt(stripped))) {
        fld.style.background = '#FFD700';
        error = "The Telephone Number contains illegal characters.\n";  
    } else if ((fld.value.length < 10) || (fld.value.length > 11)) {
        fld.style.background = '#FFD700'; 
        error = "The Telephone Number is the wrong length. \n";
    }
    return error;
}


