function validateField(field)

//This function checks for whitespace in a form field

{
 var messageString =  field;

 if (messageString.replace(/\s+$|^\s*/g, "") == "")
     {
      // there is nothing but whitespace in the field
      return false;
     }
 else
      return true;
}

//--------------------

function isAlphabetic(s)
{
var RefString = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
s+="";
for (Count=0; Count < s.length; Count++)
{
var TempChar=s.substring (Count, Count+1);
if (RefString.indexOf (TempChar, 0)==-1) return false;
}
return true;
}

//--------------------

function isAlphabeticPlus(s)
{
var RefString = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ. ";
s+="";
for (Count=0; Count < s.length; Count++)
{
var TempChar=s.substring (Count, Count+1);
if (RefString.indexOf (TempChar, 0)==-1) return false;
}
return true;
}

//--------------------

function isDigit(c)
{
var test = "" + c;
if (test =="0" || test == "1" || test == "2" || test == "3" || test == "4" || test == "5" || test == "6" || test == "7" || test == "8" || test == "9")
{
return true;
}
return false;
}

//--------------------

function isAllDigits(s)
{
var test = "" + s;
for (var k = 0; k < test.length; k++)
{
var c = test.substring(k, k+1);
if (isDigit(c) == false) return false;
}
return true;
}

//Is the email address valid?

function validEmail(email)
{
var em = email;

if (em.indexOf('@')  == -1)
   return false;
else
     if (em.indexOf('.') ==  -1 )
          return false;
    else
          return true;
}

//Is the home phone number a valid length, if so, format it

function phoneHFormat(FCPHONEH) {
var n = FCPHONEH.replace(/\D/g,''); // remove all non-digits
if (n.length==10)
   {
    document.cu_form.dphone.value=n.substr(0,3)+'-'+n.substr(3,3)+'-'+n.substr(6); // format
    return true;
   }
   else
        return false;
 }

//Is the State code valid?

function ValidateState(s)
{
var ValidUsaState = "AA,AE,AK,AL,AP,AR,AS,AZ,CA,CO,CT,DC,DE,FL,GA,HI,IA,ID,IL,IN,KS,KY,LA,MA,MD,ME,MI,MN,MO,MS,MT,NC,ND,NE,NH,NJ,NM,NV,NY,OH,OK,OR,PA,PR,RI,SC,SD,TN,TX,UT,VA,VI,VT,WA,WI,WV,WY";
s+="";
if (isAlphabetic(s) == false) return false;
s = s.toUpperCase();
if (ValidUsaState.indexOf(s,0) == -1) return false;
return true;
}

function checkCustomerData(form)
{
//Begin form validation

//validate the Name field

  if (validateField(document.cu_form.name.value) == false)
     {
      alert("You must enter your name");
      form.name.select();
      return false;
     }

  if (isAlphabeticPlus(form.name.value) == false)
     {
      alert("Your name must be alphabetic");
      form.name.select();
      return false;
     }

//validate the email field

  if (validateField(document.cu_form.emailadd.value) == false) 
     {
      alert("You must enter an email address");
      form.emailadd.select();
      return false;
     }

  if (validEmail(form.emailadd.value) == false)
     {
      alert("You must enter a valid email address");
      form.emailadd.select();
      return false;
     }

//validate the City field

  if (document.cu_form.city.value.length >=1)
     {
      if (validateField(document.cu_form.city.value) == false) 
         {
          alert("You must enter a city");
          form.city.select();
          return false;
         }

      if (isAlphabetic(form.city.value) == false)
         {
          alert("The city name can only be alphabetic");
          form.city.select();
          return false;
         }
      }

//validate the State field

  if (document.cu_form.state.value.length >=1)
     { 
      if (validateField(document.cu_form.state.value) == false)
         {
          alert("You must enter a State");
          form.state.select();
          return false;
         }
 
      if (form.state.value.length != 2)
         {
          alert("Invalid State.  Please enter a valid State code");
          form.state.select();
          return false;
         }
   
      if (ValidateState(form.state.value) == false)
         {
         alert("Invalid State.  Please enter a valid State code");
         form.state.select();
         return false;
         }
     }
     
//Validate ZIP code field
      
  if (document.cu_form.zip.value.length >=1) 
     {
       var chkZIP1 = form.zip.value;
  
       if (chkZIP1.length < 5)
          {
           alert("Invalid ZIP code, ZIP code must be 5 digits");
           form.zip.select();
           return false;
          }
     
      if (isAllDigits(form.zip.value) == false)
         {
          alert("Invalid ZIP code, ZIP code must contain 5 digits and no alphabetic characters");
          form.zip.select();
          return false;
         }
     }

//validate home phone field

  if (document.cu_form.dphone.value.length >=1)
     { 
      if (phoneHFormat(form.dphone.value) == false)
         {
          alert("Invalid phone number, the number must be 10 digits long");
          form.dphone.select();
          return false;
         }
     }

//validate message field
     
  if (validateField(document.cu_form.messagebody.value) == false)
     {
      alert("You must enter a message");
      form.messagebody.select();
      return false;
     }
     
//End form validation    

}
