///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// FORM VERIFICATION //--------------------------------------------------------------------------------------------------------

// RULES //--------------------------------------------------------------------------

//- required fields
function validate_required( field, alerttxt ) {

	with ( field ) {
	
		//-
		dcheck = value.charAt( 0 );
		
		//- input has no value
		if ( value == null || value == "" || dcheck == "[" ) {
		
		//- error message
			alert( alerttxt );
			
			//- fail
			return false;
			
		} else {
		
			//- pass
			return true;
		}
	}
	
}

//- valid email address
function validate_email( field, alerttxt ) {

	with ( field ) {
		
		//- define
		apos = value.indexOf( "@" );
		dotpos = value.lastIndexOf( "." );
		
		//- check valid email
		if ( apos < 1 || dotpos - apos < 2 ) {
		
			//- error message
			alert( alerttxt );
			
			//- fail
			return false;
			
		} else {
			
			//- pass
			return true;
		}
	}
	
}

//- numeric field
function validate_numeric( field ) {

	var stripped = field.value.replace( /[\(\)\.\-\ ]/g, '' );
	
	//- validate only if field not blank
	if ( field.value ) {
		
		//- illegal characters
		if ( isNaN( parseInt( stripped ) ) ) {
			
			//- error message
			alert( "The phone number contains illegal characters" );
			
			//- fail
			return false;
			
		
		//- unrealistic length
		} else if ( !( stripped.length > 10 ) ) {
			
			//- error message
			alert( "The phone number is the wrong length. Make sure you included an area code" );
			
			//- fail
			return false;
			
		} else {
			
			//- pass
			return true;
			
		}
	}
	
	//- pass
	return true;
}

//-----------------------------------------------------------------------------------


// FUNCTIONS //----------------------------------------------------------------------

//-  Run check
function validate_form( thisform ) {

	with ( thisform ) {
	
		//- check firstname
		if ( validate_required( firstname, "Must enter a First Name!" ) == false ) {
		
			//- focus
			firstname.focus();
			firstname.select();
			
			//- highlight
			firstname.style.color = '#ff0000';
			
			//- fail
			return false;
		
		//- check surname
		} else if ( validate_required( surname, "Must enter a Surname!" ) == false ) {
		
			//- focus
			surname.focus();
			surname.select();
			
			//- highlight
			surname.style.color = '#ff0000';
			
			//- fail
			return false;
		
		//- chack email
		} else if ( validate_email( email, "Not a valid e-mail address!" ) == false ) {
		
			//- focus
			email.focus();
			email.select();
			
			//- highlight
			email.style.color = '#ff0000';
			
			//- fail
			return false;
		
		//- check body
		} else if ( validate_required( body, "Do you really have nothing to say?...Must enter a message!" ) == false ) {
		
			//- focus
			body.focus();
			body.select();
			
			//- highlight
			body.style.color = '#ff0000';
			
			//- fail
			return false;
		
		//- check phone
		} else if ( validate_numeric( phone, "Not a valid Phone Number!" ) == false ) {
		
			//- focus
			phone.focus();
			phone.select();
			
			//- highlight
			phone.style.color = '#ff0000';
			
			//- fail
			return false;
		
		}
	}
}

//-----------------------------------------------------------------------------------


// END //----------------------------------------------------------------------------------------------------------------------
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////