//alert ("Hi - Welcome to validate.js");

function trim( str ) {
	return str.replace(/^\s+|\s+$/g, '') ;
}

function clearElement( e ) {
	while ( e.hasChildNodes() ) {
		e.removeChild( e.firstChild );
	}
}
 
// added (RWS) 031209:
function email_check(str)
{
	var at 		= "@";
	var dot 	= ".";
	var lat 	= str.indexOf(at);
	var lstr 	= str.length;
	var ldot 	= str.indexOf(dot);
	
	if (str.indexOf(at) == -1) {
	   alert("Invalid Email Address");
	   return false;
	}
	
	if ((str.indexOf(at) == -1) || (str.indexOf(at) == 0) || (str.indexOf(at) == lstr)) {
	   alert("Invalid Email Address");
	   return false;
	}
	
	if ((str.indexOf(dot) == -1) || (str.indexOf(dot) == 0) || (str.indexOf(dot) == lstr)) {
		alert("Invalid Email Address");
		return false;
	}
	
	 if (str.indexOf(at, (lat + 1)) != -1) {
		alert("Invalid Email Address");
		return false;
	 }
	
	 if ((str.substring(lat - 1, lat) == dot) || (str.substring(lat + 1,lat + 2) == dot)) {
		alert("Invalid Email Address");
		return false;
	 }
	
	 if (str.indexOf(dot, (lat + 2)) == -1) {
		alert("Invalid Email Address");
		return false;
	 }
	
	 if (str.indexOf(" ") != -1) {
		alert("Invalid Email Address");
		return false;
	 }
	
	 return true;					
}
	
function checkFormOld()
{
	//alert ("checkForm called");
	
	// get all label tags
	var requiredLabelsArray = document.getElementsByTagName( 'label' );
	var error = false;
	
	// loop through labels
	for ( var i=0; i<requiredLabelsArray.length; i++ ) {
		var currentLabel = requiredLabelsArray[i];
		
		//alert ("currentLabel = " + currentLabel);

		// see if the 'required' class is applied
		if ( currentLabel.className.search( /required/ig ) != -1 ) {

			// get the field the label refers to (the 'for' attribute specifies the id of the field...IE calls it 'htmlFor')
			var field = document.getElementById( currentLabel.getAttribute('for') ? currentLabel.getAttribute('for') : currentLabel.getAttribute('htmlFor') );

			if ( field ) {			
				// check type of input
				var fieldType = field.getAttribute( 'type' );
				
				if ( fieldType ) {
					fieldType = fieldType.toLowerCase();
				} else {
					fieldType = field.tagName.toLowerCase();
				}
					
				//alert ("fieldType = " + fieldType);
				
				if ( fieldType == 'select-one' )  {
					fieldType = 'select';
				}
				
				//alert(' field type ' + fieldType + ' and value ' + field.value );
				
				if ( ( fieldType == 'select' && ( field.value == '0' || field.value == '' ) )  ||  ( fieldType == 'text' && trim( field.value ) == '' ) ) {
				
						//alert(' field type = ' + fieldType + ' and value = ' + field.value );
						//alert(' currentLabel.className = ' + currentLabel.className );
						
						currentLabel.className += ' headerColor';
					//	msg += '<br />- ' + currentLabel.firstChild.nodeValue;
						error = true;
				} else {
					//currentLabel.className = currentLabel.className.replace( /headerColor/ig, '' );
				
					// added (RWS) 031209:
					if ((fieldType == 'text') && (field.name.toLowerCase() == 'email')) {
						if (email_check(trim(field.value)) == false) {
							currentLabel.className += ' headerColor';
							error = true;
						} else {
							currentLabel.className = currentLabel.className.replace( /headerColor/ig, '' );
						}
					} else {
						currentLabel.className = currentLabel.className.replace( /headerColor/ig, '' );
					}
				}
			}
		// added (RWS) 031209:
		} else {
			// NOT required:
			var field = document.getElementById( currentLabel.getAttribute('for') ? currentLabel.getAttribute('for') : currentLabel.getAttribute('htmlFor') );
			
			if ( field ) {			
				// check type of input
				var fieldType = field.getAttribute( 'type' );
				
				if ( fieldType ) {
					fieldType = fieldType.toLowerCase();
				} else {
					fieldType = field.tagName.toLowerCase();
				}
				
				if ((fieldType == 'text') && (field.name.toLowerCase() == 'email') && (trim(field.value) != '')) {
					if (email_check(trim(field.value)) == false) {
						currentLabel.className += ' headerColor';
						error = true;
					} else {
						currentLabel.className = currentLabel.className.replace( /headerColor/ig, '' );
					}
				}
			}
		}
	}
	

	//alert ("error = " + error);


	var infoSpan = document.getElementById( 'requiredMsg' );
	
	// show or hide the error message
	if ( infoSpan ) {
		//alert ("inside infospan IF");
		
		infoSpan.style.display = error ? 'block' : 'none';
	}
	
	// scroll to error message
	if ( error ) {
		//alert ("inside error IF");
		alert("Please fill out all form fields.");		
		window.location.hash = "requiredMsg";
	}
	
	return !(error);
}

function checkForm()
{
	//alert ("checkForm called");

	// get required fields
	var requiredFields = document.getElementById( 'required_fields' ).value;
	var requiredFieldsArray = requiredFields.split(",");

	var error = false;

	// loop through fields
	for ( var i=0; i<requiredFieldsArray.length; i++ ) {
		var currentField = requiredFieldsArray[i];
		
		//alert ("currentField = " + currentField);

		// get the field the label refers to (the 'for' attribute specifies the id of the field...IE calls it 'htmlFor')
		var field = document.getElementById( currentField );

		if ( field ) {
			// check type of input
			var fieldType = field.getAttribute( 'type' );

			if ( fieldType ) {
				fieldType = fieldType.toLowerCase();
			} else {
				fieldType = field.tagName.toLowerCase();
			}

			//alert ("fieldType = " + fieldType);

			if ( fieldType == 'select-one' )  {
				fieldType = 'select';
			}

			//alert(' field type ' + fieldType + ' and value ' + field.value );

			if ( ( fieldType == 'select' && ( field.value == '0' || field.value == '-1' || field.value == '' ) )  ||  ( fieldType == 'text' && trim( field.value ) == '' ) ) {

				//alert(' field type = ' + fieldType + ' and value = ' + field.value );
				//alert(' currentLabel.className = ' + currentLabel.className );

				//currentLabel.className += ' headerColor';
				//	msg += '<br />- ' + currentLabel.firstChild.nodeValue;
				error = true;
			} else {
				//currentLabel.className = currentLabel.className.replace( /headerColor/ig, '' );

				// added (RWS) 031209:
				if ((fieldType == 'text') && (field.name.toLowerCase() == 'email')) {
					if (email_check(trim(field.value)) == false) {
						//currentLabel.className += ' headerColor';
						error = true;
					} 
					//else {
						//currentLabel.className = currentLabel.className.replace( /headerColor/ig, '' );
					//}
				}
				//else {
					//currentLabel.className = currentLabel.className.replace( /headerColor/ig, '' );
				//}
			}
		}
		// added (RWS) 031209:
	}
/*		else {
			// NOT required:
			var field = document.getElementById( currentLabel.getAttribute('for') ? currentLabel.getAttribute('for') : currentLabel.getAttribute('htmlFor') );
			
			if ( field ) {			
				// check type of input
				var fieldType = field.getAttribute( 'type' );
				
				if ( fieldType ) {
					fieldType = fieldType.toLowerCase();
				} else {
					fieldType = field.tagName.toLowerCase();
				}
				
				if ((fieldType == 'text') && (field.name.toLowerCase() == 'email') && (trim(field.value) != '')) {
					if (email_check(trim(field.value)) == false) {
						currentLabel.className += ' headerColor';
						error = true;
					} else {
						currentLabel.className = currentLabel.className.replace( /headerColor/ig, '' );
					}
				}
			}
		}
	} */

	//alert ("error = " + error);

	var infoSpan = document.getElementById( 'requiredMsg' );

	// show or hide the error message
	if ( infoSpan ) {
		//alert ("inside infospan IF");
		
		infoSpan.style.display = error ? 'block' : 'none';
	}
	
	// scroll to error message
	if ( error ) {
		//alert ("inside error IF");
		//alert("Please fill out all form fields.");		
		window.location.hash = "requiredMsg";
	}
	
	return !(error);
}
