<!--

// Function    : IsEmailValid
// Copyright   : (c) 1998 Shawn Dorman
// http://www.goodnet.com/~sdorman/web/IsEmailValid.html

function IsEmailValid(FormName,ElemName)
{
var EmailOk  = true
var Temp     = document.forms[FormName].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
	(Temp.value == null) ||
	(Temp.value == ""))                // Sean doesn't want empty field, either
   {  
      EmailOk = false
   }
return EmailOk
}

// setStyleById: given an element id, style property and 
// value, apply the style.
// args:
//  i - element id
//  p - property
//  v - value
//
function setStyleById(i, p, v) 
{
	if (document.getElementById)
	{
		var n = document.getElementById(i);
		if (n) n.style[p] = v;
	}
}

// If any one of the fields is empty, the form is invalid.
// So, start true and change to false if an item is empty.
function checkForValue(FormName,fieldsToCheck)
{
	var success = true;
//alert(fieldsToCheck.length);
	for (var n = 0; fieldsToCheck.length > n; n++)
	{
		var field = document.forms[FormName].elements[fieldsToCheck[n]];
//alert(field.name+" "+field+" "+field.value);
		if  ((field == null) || (field.value == null) || (field.value == '') || (field.value == 'undefined'))
		{
			success = false;
			//change the style on the offending element
			var changeToRed = eval('"' + fieldsToCheck[n] + 'Label"');
			setStyleById(changeToRed,'color','red');
		} else {
			// reset any highlighted fields to black
			var changeToBlk = eval('"' + fieldsToCheck[n] + 'Label"');
			setStyleById(changeToBlk,'color','black');
		}
	}
	return success;
}

function checkForChecked(FormName,buttonsToCheck)
{
	var success = true;
	// cycle through the array of button sets
	for (var n = 0; buttonsToCheck.length > n; n++)
	{
		// If any one of the buttons is checked, the set is valid.
		// So, start false and change to true if an item is checked.
		var setSuccess = false;
		var buttonSet = document.forms[FormName].elements[buttonsToCheck[n]];
		if (buttonSet == null) continue;    // ignore this set if the element does not really exist.
		if (!(buttonSet.length))            // if there is just one element in this set, check it.
		{
			if (buttonSet.checked) setSuccess = true;
		}
		for (var o = 0; buttonSet.length > o; o++)   // if the Set contains more than one item, check for at least one checked element.
		{
			if (buttonSet[o].checked) setSuccess = true;
		}
		
		// if the set failed, change the items label to red.
		if (!(setSuccess))
		{
			success = false;
			//change the style on the offending element's label
			var changeToRed = eval('"' + buttonsToCheck[n] + 'Label"');
			setStyleById(changeToRed,'color','red');
		} else {
			// reset any highlighted fields to black
			var changeToBlk = eval('"' + buttonsToCheck[n] + 'Label"');
			setStyleById(changeToBlk,'color','black');
		}
	}
	return success;
}

function VerifyForm(FormName)
{	
    /* 
	** These were defined in the form **
	var IWantToCheckFields = true;        // set to true if you want to check some form fields
	var fieldsToCheck = ['btFirstName','stZip'];   // list all the req. fields 
    var IWantToCheckButtonSets = true;    // set to true if you want to check some button/checkbox sets
	var buttonsToCheck = ['payMethod'];  // list all the button and/or checkbox sets you want to check.
    var IWantToCheckAnEmail = true;       // set to true if you want to validate (appx.) an email address' syntax.
	var emailToCheck = 'cEmail';          // list one and only one field which should have a valid email address.
	*/
	if (document.forms[FormName].elements[emailToCheck] == null) { IWantToCheckAnEmail = false; alert('can\'t find email field'); }  // abort email check if field does not exist
	
	var reportAnInvalidField = IWantToCheckFields && !(checkForValue(FormName,fieldsToCheck));
    var reportAnInvalidSet = IWantToCheckButtonSets && !(checkForChecked(FormName,buttonsToCheck));
	var reportAnInvalidEmail = IWantToCheckAnEmail && !(IsEmailValid(FormName,emailToCheck));

if (reportAnInvalidField || reportAnInvalidSet)
	{
		alert('Registration Failed.\n\nPlease resubmit after filling out all of the required fields (marked with a *).');
		return false;
	}
	// check for a valid email if field has a value
	var emailsToCheck = eval('[\''+emailToCheck+'\']');
	if (reportAnInvalidEmail && checkForValue(FormName,emailsToCheck))
	{
		alert('This email address does not appear to be valid.\n\nPlease supply a valid and active email address so that we can contact you.\n\nThanks!');
		document.forms[FormName].elements[emailToCheck].focus();
		var changeToRed = eval('"' + emailToCheck + 'Label"');
		setStyleById(changeToRed,'color','red');
		return false;
	}

	return true;
}


function checkForOtherState(prefix)
{
	if (document.getElementById)
	{
		var n = document.getElementById(prefix+'State');
		if (n.value == 'other')
		{ setStyleById(prefix+'StateOther','display','inline'); } else { setStyleById(prefix+'StateOther','display','none'); }
	}
}

// -->

