////////////////////////////////////////////////////////////////
//
//	Validation Function
//	@date        February 2006
//	@modified    8 May 2006
//  @authors     Jonah Paul ( jonah[=at=]jobportals.com.au )
//               Ben Blanks ( benny[=at=]sinwolf.com )
//
// == Authors Notes ==
// - (Ben): Browser Compatibility Testing:
//            - Firefox 1.5
//            - Safari (Panther)
//            - Internet Explorer 6
//
////////////////////////////////////////////////////////////////
//
// If you wish to use this validation script you may do so as
// long as you leave the information above intact. If you wish
// to change this file the authors would like to know what
// modifications you made. It would be appreciated if you
// let them know.
//
////////////////////////////////////////////////////////////////
//
// == How to use ==
//
// This function can be used for any form to ensure that
// required fields are not blank or are checked. This
// function also has very basic email validation.
//
//
// Types: text , password , select , textarea
// ------------------------------------------
// These types can be set to required by adding a
// class="required" to the input tag.
//
// Types: checkboxes , radio buttons
// ---------------------------------
// These types can be checked by putting a
// id="[Element Name Here]" in a div that
// surrounds the radio buttons or checkboxes.
//
// The required and leftitblank styles
// -----------------------------------
// These styles are used so that its easy to change
// the colours and styles being used for the required
// form elements.
//
////////////////////////////////////////////////////////////////

	function validation(form) {
		var formElements       = form.elements;
		var firstBlankField    = 0;
		var incompleteForm     = false;
		var doFocus            = true;
		var fieldName          = false;

		var lastCheckedElement = null;
		var elementsChecked    = Array();
		var elementCount       = 0;

		for ( var i = 0; i < formElements.length; i++ ) {

////////////////////////////////
// Checks Element Class Names
// for required or leftitblank

			if ( formElements[i].className == 'required'      ||
			     formElements[i].className == 'leftitblank'
			) {


////////////////////////////////
// Checks the if the length of
// the required input fields
// are correct. Ignores fields
// that do not have a maxlength
// attribute set.

				
////////////////////////////////
// If the element is empty or
// the length is not correct
// then set the elements class
// to leftitblank.

				if ( formElements[i].value == '' ) {
					formElements[i].className = 'leftitblank';
					incompleteForm = true;
					if (!firstBlankField) {
						firstBlankField = formElements[i];
					}
				}

////////////////////////////////
// If the class name is
// leftitblank then set it to
// required

				else if ( formElements[i].className == 'leftitblank' ) {
					formElements[i].className = 'required';
				}
			}

////////////////////////////////
// Checks the element classname
// then decides it is a radio
// or checkbox which is
// required or leftitblank

			else if ( formElements[i].className == 'rcrequired' ||
			          formElements[i].className == 'rcleftitblank' // Dont really need but its there anyway
			) {

////////////////////////////////
// Checks if the last element
// checked doesnt equal the
// current element being checked

				if ( formElements[i].name != lastCheckedElement ) {
					elementCount++;
					elementsChecked[elementCount] = Array( false , formElements[i].name );
				}

////////////////////////////////
// Checks if the radio or
// checkbox is checked

				if ( formElements[i].checked ) {
					elementsChecked[elementCount] = Array( true , formElements[i].name );
				}

				lastCheckedElement = formElements[i].name;
			}
		}

////////////////////////////////
// Checks each of the checked
// radio buttons or checkboxes
// and decides if it is complete
// or incomplete.

		for ( var i=1; i < elementsChecked.length; i++ ) {
			if ( !elementsChecked[i][0] ) {
				if ( ! firstBlankField ) {
					doFocus = false;
				}
				incompleteForm = true;
				elementStyle = 'leftitblank';
			}
			else {
				elementStyle = 'required';
			}

			var lengthID = elementsChecked[i][1].indexOf( '[]' );
			if ( lengthID != -1 ) {
				fieldName = elementsChecked[i][1].substr( 0, lengthID );
			}
			else {
				fieldName = elementsChecked[i][1];
			}

////////////////////////////////
// Checks the javascript method
// being used and then acts
// accordingly to change the
// class of the element.

			if ( fieldName ) {
				if ( document.layers ){
					if ( document.layers[fieldName] ) {
						document.layers[fieldName].className = elementStyle;
					}
				}
				else if ( document.all || document.getElementById ) {
					if ( document.getElementById(fieldName) ) {
						document.getElementById(fieldName).className = elementStyle;
					}
				}
			}
		}

	////////////////////////////////
	// Checks for basic valid email
	// address.

		if ( form.email ) {
			if (form.email.value != "" && isValidEmail(form.email.value) == false) {
				alert("Please enter a valid email address");
				form.email.className = "leftitblank";
				form.email.focus();
				return false;
			}
		}


////////////////////////////////
// Checks if the form registered
// an incompleteForm and then
// acts accordingly.

		if ( incompleteForm == true ) {
			alert("please fill in all required fields");

			if ( doFocus ) {
				firstBlankField.focus();
			}
			return false;
		}
		else {
			return true;
		}
	}

	function isValidEmail(str) {
		
		var email=/^[A-Za-z0-9]+([_\.-][A-Za-z0-9]+)*@[A-Za-z0-9]+([_\.-][A-Za-z0-9]+)*\.([A-Za-z]){2,20}$/i;
		
		return(email.test(str));

	}