/*******************************************************************
* Name: formSentry.js
* Purpose: To validate required form fields
* Date: 11/29/2006
* Author: Gabe B. (christiangeek[nospam]@gmail.com), ...
*******************************************************************/
var reqFields = new Array;

//*** Function: checkInput, Purpose: Check values of all non-radio button inputs ***
function checkInput(obj){
	//First check for a value
	if (obj.value == "")
	{
        return "This field cannot be left empty";
	}
	
	//Does the field have a 'accept="command:parameters"'?
	if(obj.accept) {
		//If so
		command = obj.accept;
		//Split mutiple commands like this command:parameters;command:parameters
		if(command.indexOf(";")) {command = command.split(";");}
		for(var i=0;i<command.length;i++) {  
			//Split at :
			params = command[i].split(":");
			switch (params[0]) {
				
				//*** Command: field, Purpose: Compares one field against another, Usage: 'accept="field:fieldname"'***
				case "field":
					fieldObj = document.getElementById(params[1]);
					if(obj.value != fieldObj.value) {
					return "This field does not match "+fieldObj.id;
					}
					break;
				
				//*** Command: minimum, Purpose: Compares field length, Usage: 'accept="minimum:8"'***
				case "minimum":
					//What is the length?
					if(obj.value.length < params[1]) {
					return "This field must be "+params[1]+" characters or longer";
					}
					break;
				
				//*** Command: filter, Purpose: Checks the input against a filter, Usage: 'accept="filter:filtername"'***
				case "filter":
				switch(params[1]) {		
					//*** Parameter: email, Purpose: Tells if the input is a email, Usage: 'accept="filter:email"'***      
					case "email":
						// Regular expression
						var RE = /^[\w-\.\']{1,}\@([\da-zA-Z-]{1,}\.){1,}[\da-zA-Z-]{2,}$/;
						//Run regular expression
						if (!RE.test(obj.value)) { return "Valid email required"; }
						break;
					
					//*** Parameter: alpahanumeric, Purpose: Checks if the input contains only alphanumeric & _  characters, Usage: 'accept="filter:alphanumeric"'***      
					case "alphanumeric":  
						// Regular expression
						var RE = /\W/;
						//Run regular expression
						if (RE.test(obj.value)) { return "Please use only letters, numbers, and underscore (_)"; }
						break;
									
					//*** Parameter: phone, Purpose: Checks if the input contains only a valid 10 digit phone number, Usage: 'accept="filter:phone"'***      
					case "phone":
						// Get the length
						var length = obj.value.length;
						// Strip common non numeric phone characters
						for(var i = 1; i<length; ++i){
							obj.value = obj.value.replace(" ", "");
							obj.value = obj.value.replace("-", "");  
							obj.value = obj.value.replace("(", "");
							obj.value = obj.value.replace(")", "");
							obj.value = obj.value.replace(".", "");
						}
						// Check numeric & length after strip
						if (isNaN(obj.value) || obj.value.length > 10 || obj.value.length < 10) { return "Valid phone number required"; }
						break;
				
					
					//*** Parameter: numeric, Purpose: Checks if the input contains only numeric characters, Usage: 'accept="filter:numeric"'***      
					case "numeric":
						//No need for regular expressions
						if (isNaN(obj.value)) { return "Please use only numbers"; }
						break;    
				}
				break;
			}
		}
	}
	return true;
}


//*** Function: checkRadio, Purpose: Check values of all radio button inputs ***
function checkRadio(obj){
	//Loop through radios
	for(var i=0;i<obj.length;i++)
	{
		//Check
		if(obj[i].checked == true)
		{
			//Return cool
			return true;
		};
	}
	//Return a error code
	return "Please check a button";
}

function cleanErrors() {
    $("#error").fadeOut("fast").remove();
}

function writeError(field, code) {
    //Write out error info
    error = document.createElement('div');
    error.className = 'error';
    error.id = 'error';
    error.name = 'error';
    error.innerHTML = code;
    labels = field.getElementsByTagName('label');
    labels[0].appendChild(error);
    $('#error').fadeIn("fast");
    return false;
}

//*** Function: checkFields, Purpose: Determines whether the field is a radio button or not ***
function checkFields(){
    //Clean up old errors.
	cleanErrors();
	
	var reqFields = $(".required input");
	
    //Loop through required fields
    for(var i=0;i<reqFields.length;i++)
    {
        
        obj2 = reqFields[i];
        //Run the checkRadio function
            code = checkInput(obj2);
            if(code != true)
            {
                //Tell the error handler which form did not register
                var top = obj2.parentNode;
            }
        
        if (top) {
            return writeError(top, code);
        }
    }
	return true;
}
