/** Validation Init */

// Check for W3CDOM
var W3CDOM = (document.getElementsByTagName && document.createElement);

//function validate_init()
//{
	addLoadEvent ( 
		function () {


			for ( var i = 0; i < document.forms.length; i++ )
			{
				// Check to see if the form already has a function assigned to it
				if ( document.forms[i].onsubmit == undefined )
				{
					document.forms[i].onsubmit = function () {
						return SubmitForm();
					}

				}
				
			} 
	} );
//}

function writeError(obj,message)
{
	if (obj.hasError) return;
	if (W3CDOM)
	{
		obj.className += ' error';
		obj.onchange = removeError;
		var sp = document.createElement('span');
		sp.className = 'error';
		sp.appendChild(document.createTextNode(message));
		obj.parentNode.appendChild(sp);
		obj.hasError = sp;
	}
	else
	{
		errorstring += obj.name + ': ' + message + '\n';
		obj.hasError = true;
	}
	if (validForm)
		firstError = obj;
	validForm = false;
}

function removeError()
{
	this.className = this.className.substring(0,this.className.lastIndexOf(' '));
	this.parentNode.removeChild(this.hasError);
	this.hasError = null;
	this.onchange = null;
}


// email
function checkEmail (strng) {

    var emailFilter=/^.+@.+\..{2,3}$/;
    if (!(emailFilter.test(strng))) { 
       return false;
    }
    else {
//test email for illegal characters
       var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/
         if (strng.match(illegalChars)) {
          return false;
       }
    }
return true;    
}

function checkNumeric ( strng )
{
	var numericFilter = /^[0-9]*$/;
	if ( !(numericFilter.test(strng)) ){
		return false;
	}

	return true;
}

function checkDate1( strng )
{
	var dateFilter = /^[0-9]{2}\/[0-9]{2}\/[0-9]{4}$/;
	if ( !(dateFilter.test(strng)) ){
		return false;
	}

	return true;
}

function checkCorrect( value1, value2 )
{
	if ( !value1 || !value2 )
		return true;
	
	if ( value1.value != value2.value )
		return false;

	return true;
}

function SubmitForm2( formName )
{
	var form = document.forms[formName];

	validForm = true;
	// Our error String
	errorstring = '';

	for (var i=0;i<form.length;i++)
	{
		if (form[i].getAttribute('required') && !form[i].value)
			writeError(form[i],'This field is required');
		else if ( form[i].type =='select-one' && form[i].value =="-1")
		{
			alert("You must select an option" );
			form[i].focus();
			validForm = false;
			return validForm;
		}
		
	}

	if ( form['email'] && !checkEmail(form['email'].value))
	{
		writeError(form['email'],'This is not a valid email address');
	}

	if ( !checkCorrect( form['Email'], form['Email2']  ) )
	{
		alert( "Your email addresses do not match" );
		return false;
	}

	if ( !checkCorrect( form['Password'], form['Password2']  ) )
	{
		alert( "Your passwords do not match" );
		return false;
	}

	if (!W3CDOM)
	{
		alert( "One or more of the required fields have not been filled in." );	
	}

	if (firstError)
	{
		firstError.focus();
	}

	return validForm;
	
}

function SubmitForm()
{
	
	// Find the submitted form
	var x = document.forms[0].name;

	return SubmitForm2( x );

		
}

