// $Id: validate.js,v 1.4 2008/12/01 16:43:40 dferruggia Exp $

//#############################################################################
//#
//# disableSubmit - prevent the user from submitting form multiple times
//#
function disableSubmit(theform)
{
	//if IE 4+ or NS 6+
	if (document.all||document.getElementById)
	{
		//screen thru every element in the form, and hunt down "submit"
		for (i=0;i<theform.length;i++)
		{
			var tempobj=theform.elements[i];
			if(tempobj.type.toLowerCase()=="submit")
				tempobj.disabled=true;
		}
	}                             
}

//#############################################################################
//#
//# enableSubmit - activate submit button
//#
function enableSubmit(theform)
{
	//if IE 4+ or NS 6+
	if (document.all||document.getElementById)
	{
		//screen thru every element in the form, and hunt down "submit"
		for (i=0;i<theform.length;i++)
		{
			var tempobj=theform.elements[i];
			if(tempobj.type.toLowerCase()=="submit")
				tempobj.disabled=false;
		}
  }                             
}

//#############################################################################
//#
//#	setErrorBgrnd - set the background color of form field
//#
function setErrorBgrnd( form, field )
{
	var errorColor   = 'red';
	
	if(document.getElementById)
		document.getElementById(field).style.background  = errorColor;
	else if(document.all)
		document.all.form.field.style.background = errorColor;
}

//#############################################################################
//#
//#	validateForm - start function for validation routines
//#
function validateForm( form )
{
	var noErrorColor = 'white';
	var errorMessage = '<font class=errorTrace><b>There are errors in your input. Please correct the items marked in red</b></font>';
	var errors = 0;
	
	disableSubmit(form);
	// Set background color of form elements with errors back to white
	var stateField;
	var zipField;

	if(document.getElementById)
	{
		document.getElementById('Name').style.background  = noErrorColor;
		document.getElementById('EMail').style.background = noErrorColor;
		document.getElementById('Phone').style.background = noErrorColor;
		stateField = document.getElementById('State');
		if (stateField) stateField.style.background = noErrorColor;
		zipField = document.getElementById('Zip');
		if (zipField) zipField.style.background = noErrorColor;
	}
	else if(document.all)
	{
		document.all.form.Name.style.background  = noErrorColor;
		document.all.form.EMail.style.background = noErrorColor;
		document.all.form.Phone.style.background = noErrorColor;
		document.all.form.State.style.background = noErrorColor;
		document.all.form.Zip.style.background   = noErrorColor;
		stateField = document.all.form.State;
		if (stateField) stateField.style.background = noErrorColor;
		zipField = document.all.form.Zip;
		if (zipField) zipField.style.background = noErrorColor;
	}
	
	if( !validateName( form.Name.value ) )
	{
		setErrorBgrnd( form, 'Name' )
		errors = 1;
	}
	if( !validateEmail( form.EMail.value ) )
	{
		setErrorBgrnd( form, 'EMail' )
		errors = 1;
	}
	if( !validatePhone( form.Phone.value ) )
	{
		setErrorBgrnd( form, 'Phone' )
		errors = 1;
	}
	if( stateField && !validateState( form.State.value ) )
	{
		setErrorBgrnd( form, 'State' )
		errors = 1;
	}
	if( zipField && !validateZipcode( form.Zip.value ) )
	{
		setErrorBgrnd( form, 'Zip' )
		errors = 1;
	}
	
	if( errors )
	{
		if (document.all)
        	document.all.formErrors.innerHTML = errorMessage;
    	else if (document.layers) 
    	{
	        document.layers['formErrors'].document.open();
	        document.layers['formErrors'].document.write(errorMessage);
	        document.layers['formErrors'].document.close();
    	}
	    else if(document.getElementById)
	    	document.getElementById('formErrors').innerHTML = errorMessage;
	    enableSubmit(form);
		return false;
	}
	else return true;	
	
}

//#############################################################################
//#
//#	validateEmail - check for a syntactically valid email address
//#
function validateEmail( emailField )
{
	var emailFilter=/^.+@.+\..{2,4}$/;
	var illegalChars= /[\(\)\<\>\,\;\:\\\/\"\[\]]/;
	
	if ( !(emailFilter.test( emailField )) || emailField.match( illegalChars ))
		return false;
	
	return true;
}

//#############################################################################
//#
//#	validateName - checks for input in form field
//#
function validateName( nameField )
{
	if( nameField.length <=0 )
		return false;
	
	return true;
}

//#############################################################################
//#
//#	validatePhone - checks for valid phone number
//#
function validatePhone( phoneField )
{
	var stripped = phoneField.replace(/[\(\)\.\-\ ]/g, '');
	//strip out acceptable non-numeric characters
	if ( isNaN(parseInt(stripped)) || stripped.length != 10 )
	{
		return false;
	}
	
	return true;
}

//#############################################################################
//#
//#	validateState - checks if a state is selected
//#
function validateState( stateField )
{
	if( stateField.length <= 0 )
		return false;
	
	return true;
}

//#############################################################################
//#
//#	validateZip - checks for a valid 5 digit zipcode
//#
function validateZipcode( zipcodeField )
{
	if( zipcodeField.length != 5 )
		return false;
	
	return true;
}
