function IsEmailValid(thisForm,ElemName)
{
var EmailOk  = true
var Temp     = thisForm.elements[ElemName]
var AtSym    = Temp.value.indexOf('@')
var Period   = Temp.value.lastIndexOf('.')
var Space    = Temp.value.indexOf(' ')
var Length   = Temp.value.length - 1   // Array is from 0 to length-1

if ((AtSym < 1) ||                     // '@' cannot be in first position
    (Period <= AtSym+1) ||             // Must be atleast one valid char btwn '@' and '.'
    (Period == Length ) ||             // Must be atleast one valid char after '.'
    (Space  != -1))                    // No empty spaces permitted
   {  
      EmailOk = false
   }
return EmailOk
}

function validateForm(f)
{
	// Require Name
	if (f.name.value == '')
	{
		alert('Please enter your name.')
		f.name.focus()
  		return false;
	}


	// check the phone number if they enter it
	if (validPhone(f.phone.value) != true)
	{
		f.phone.focus()
  		return false;
	}
	
	// If they entered an email address, then edit it before submitting the form
	if (f.email.value.length == 0)  {
		alert('Please enter your Email address.')
		f.email.focus()
  		return false;
	}

	if (IsEmailValid(f, 'email') !== true)
	{
		alert('Please enter a valid e-mail address!')
		f.email.focus()		
  		return false;
	}
		
	// Require Street Address
	if (f.streetaddr.value =='')
	{
		alert('Please enter your street address.')
		f.streetaddr.focus()
  		return false;
	}
	// Require City, State, Zip
	if (f.citystatezip.value == '')
	{
		alert('Please enter your City, State and Zip code')
		f.citystatezip.focus()
  		return false;
	}

	  return true;
	// Passed the edits, submit the form
	f.submit()
	
}

function validPhone(phone)
{
    var validchars = '0123456789()- ';
    var badchars = "";
    for (var j=0; j<phone.length; j++) {
         if (validchars.indexOf(phone.charAt(j)) == -1) {
           badchars += phone.charAt(j);
         }
    }
    if (badchars.length > 0) {
      	alert ('Phone Number contains the following invalid characters. "' + badchars + '"');
	   	return false;
	}
	if (phone.length < 10)  {
		alert ('The phone number must be at least 10 characters.');
		return false;
	}
	return true;
}


