// JavaScript Document

// -----------------------------------------------------------------
// Function    : IsEmailValid
// Language    : JavaScript
// Description : Checks if given email address is of valid syntax
// Copyright   : (c) 1998 Shawn Dorman
// http://www.goodnet.com/~sdorman/web/IsEmailValid.html
// -----------------------------------------------------------------
// Ver    Date    Description of modification
// --- ---------- --------------------------------------------------
// 1.0 09/04/1996 Original write
// 1.1 09/30/1998 CHG: Use standard header format
// -----------------------------------------------------------------
// Source: Webmonkey Code Library
// (http://www.hotwired.com/webmonkey/javascript/code_library/)
// -----------------------------------------------------------------

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;
	}

	// check the phone number if they enter it
	if (validPhone(f.phone.value) != true)
	{
		f.phone.focus()
		return;
	}
	
	// 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;
	}

	if (IsEmailValid(f, 'email') !== true)
	{
		alert('Please enter a valid e-mail address!')
		f.email.focus()		
		return;
	}
		
	// Passed the edits, submit the form
	f.submit()
	
}

function validateFormSansPhone(f)
{
	// Require Name
	if (f.name.value == '')
	{
		alert('Please enter your name.')
		f.name.focus()
		return;
	}
	
	// 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;
	}

	if (IsEmailValid(f, 'email') !== true)
	{
		alert('Please enter a valid e-mail address!')
		f.email.focus()		
		return;
	}
		
	// Passed the edits, submit the form
	f.submit()
	
}


function validPhone(phone) {
    var validother = '()-. ';
    var validnums = '0123456789';
    var badchars = "";
    var numcount = 0;
    for (var j=0; j<phone.length; j++) {
         if (validnums.indexOf(phone.charAt(j)) != -1) {
               numcount += 1;  
         }
         else if(validother.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 (numcount != 10)  {
		alert ('The phone number must contain ten digits.');
		return false;
	}
	return true;
}

function validateNewsletter(f)
{
	// 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;
	}

	if (IsEmailValid(f, 'email') !== true)
	{
		alert('Please enter a valid e-mail address!')
		f.email.focus()		
		return;
	}
		
	// Passed the edits, submit the form
	f.submit()
	
}