// Copyright © 2001 by Apple Computer, Inc., All Rights Reserved.
//
// You may incorporate this Apple sample code into your own code
// without restriction. This Apple sample code has been provided "AS IS"
// and the responsibility for its operation is yours. You may redistribute
// this code, but you are not permitted to redistribute it as
// "Apple sample code" after having made changes.

function validateForm(theForm) 
{
  var why = "";
  why += checkName(theForm.name.value);
  why += checkAddress(theForm.address.value);
  why += checkCity(theForm.city.value);
  why += checkState(theForm.state.value);
  why += checkZip(theForm.zip.value);
  why += checkPhone(theForm.phone.value);
  why += checkEmail(theForm.email.value);
  why += checkDropdown(theForm.how.selectedIndex);
  if (why != "") {
      alert(why);
      return false;
   }
return true;
}

// name - uc, lc, and underscore only.

function checkName (strng) {
var error = "";
if (strng == "") {
   error = "Please enter your name.\n";
   }
return error;
}       

// address - uc, lc, and underscore only.

function checkAddress (strng) {
var error = "";
if (strng == "") {
   error = "Please enter your address.\n";
   }
return error;
}       

// city - uc, lc, and underscore only.

function checkCity (strng) {
var error = "";
if (strng == "") {
   error = "Please enter the name of your city.\n";
   }
return error;
}       

// state - uc, lc, and underscore only.

function checkState (strng) {
var error = "";
if (strng == "") {
   error = "Please enter the name of your state.\n";
   }
return error;
}       

// zip - uc, lc, and underscore only.

function checkZip (strng) {
var error = "";
if (strng == "") {
   error = "Please enter your zip code.\n";
   }
return error;
}       

// phone number - strip out delimiters and check for 10 digits

function checkPhone (strng) {
var error = "";
if (strng == "") {
   error = "Please enter a phone number.\n";
}

var stripped = strng.replace(/[\(\)\.\-\ ]/g, ''); //strip out acceptable non-numeric characters
    if (isNaN(parseInt(stripped))) {
       error = "The phone number you entered contains illegal characters.\n";
  
    }
    if (!(stripped.length == 10)) {
	error = "Does the phone number you entered include an area code?\n";
    } 
return error;
}

// email

function checkEmail (strng) {
var error="";
if (strng == "") {
   error = "Please enter an email address.\n";
}

    var emailFilter=/^.+@.+\..{2,3}$/;
    if (!(emailFilter.test(strng))) { 
       error = "Please enter a valid email address.\n";
    }
    else {
//test email for illegal characters
       var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/
         if (strng.match(illegalChars)) {
          error = "The email address contains illegal characters.\n";
       }
    }
return error;    
}

// valid selector from dropdown list

function checkDropdown(choice) {
var error = "";
    if (choice == 0) {
    error = "Please select an option from the drop-down list.\n";
    }    
return error;
}    
