/*
 * Universal Form validation script.
 *
 * @package     JS Validation
 * @category    JS 
 * @author      Adam James Evans
 * @copyright   Motortrak
 * @link        http://www.motortrak.com
 * @version     Version 1
 * @date		12/2008
 *  
 * @Last Change:
 * 
 *  
 *  
 */


/* 
Instructions:
1. Output the below before including this validation script to define what fields to validate and the first line of the alert.
===========================================================
<script type="text/javascript" language="javascript">
	function validateFormOnSubmit(theForm) {
		var reason = "";
		
		reason += validateEmail(theForm.email);
		reason += validatePhone(theForm.phone);
		reason += validateNumber(theForm.phone);
		reason += validateEmpty(theForm.from, "error message");
		reason += validateDropdown(theForm.from, "error message");
		reason += validateRadio(theForm.from, "error message");
		
		if (reason != "") {
			alert("Some fields need correction:\n" + reason);
			return false;
		}
		
		return true;
	}
</script>

2. Use the below code to include this file.
===========================================================
<script type="text/javascript" language="javascript" src="/common_files/js_validation/formvalidation.plugin.js"></script>

3.
===========================================================
on the form use the following element value
<form onsubmit="return validateFormOnSubmit(this)">
*/



//Config settings
if( !okcolor ){ var okcolor = 'White'; }
if( !errorcolor ){ var errorcolor = 'Yellow'; }
/* PLEASE NOTE: The error highlight color does not apply to radio buttons for the moment. */

function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
}
//validate empty alphanumeric field(s) func
function validateEmpty(fld,error) {
    /*var error = "";*/
  
    if (fld.value.length == 0 && error != "") {
        fld.style.background = errorcolor; 
		error = error;
	} else if (fld.value.length == 0 && error == ""){
		fld.style.background = errorcolor;
		error = "The required field has not been filled in.\n"; //default error message for empty fields
    } else {
        fld.style.background = okcolor;
		error = "";
    }
    return error;   
}
//Validate numeric field(s) func
function validateNumber(fld) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');     

   if (fld.value == "") {
        error = "You didn't enter a phone number.\n";
        fld.style.background = errorcolor;
    } else if (isNaN(parseInt(stripped))) {
        error = "The number field contains illegal characters.\n";
        fld.style.background = errorcolor;
    }
    return error;
}
//Validate phone number fields
function validatePhone(fld) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');     

   if (fld.value == "") {
        error = "You didn't enter a phone number.\n";
        fld.style.background = 'Yellow';
    } else if (isNaN(parseInt(stripped))) {
        error = "The phone number contains illegal characters.\n";
        fld.style.background = 'Yellow';
    } else if (!(stripped.length > 6)) {
        error = "The phone number is the wrong length. Make sure you included an area code.\n";
        fld.style.background = 'Yellow';
    } else {
        fld.style.background = 'White';
    }
    return error;
}
 
//Validate email field(s) func
function validateEmail(fld) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
    
    if (fld.value == "") {
        fld.style.background = errorcolor;
        error = "You didn't enter an email address.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = errorcolor;
        error = "Please enter a valid email address.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = errorcolor;
        error = "The email address contains illegal characters.\n";
    } else {
        fld.style.background = okcolor;
    }
    return error;
}
//Validate group and single radio buttons
function validateRadio(fld, error) {
    //var error = "";
	for(i = 0; i < fld.length; i++){
		if (fld[i].checked == true) {
			error = "";
			break;
		} else if (fld[i].checked == false && error != ""){
			error = error;
			//fld[i].style.background = "yellow";
		} else {
			error = "You didn't enter an option.\n";
			//fld[i].style.background = "yellow";
		}   
	}
	return error;
}
//validate empty dropdown field(s) func
function validateDropdown(fld,error) {
    //var error = "";
  
    if (fld.selectedIndex == 0 && error != "") {
        fld.style.background = errorcolor; 
		error = error;
	} else if (fld.selectedIndex == 0 && error == ""){
		fld.style.background = errorcolor; 
		error = "The required field has not been filled in.\n"; //default error message for empty fields
    } else {
        fld.style.background = okcolor; 
		error = "";
    }
    return error;   
}
