/* ************************************
*
* Form Validator
* v1.0
* 
* Last update: 8/3/2008
*
* Written by Arthur Kay
* www.akawebdesign.com
*
* Reuse or Modification without
* expressed written consent is
* strictly prohibited.
*
************************************ */


/* ##################################################################

INSTRUCTIONS

 (1) All form attributes and values must be lowercase!
 
 (2) To validate an input, add these attributes to your input tag:

   >>> <input req="true" err="Error Message"... />

     *** note: if this is a radio or checkbox input 
         (and there are multiple options) then only the first
         option needs to have these attributes.
         
         <input type="radio" req="true" err="Error Message" ... />
         <input type="radio" ... />
         <input type="radio" ... />

 (3) If you're using and input with ** type="text" **
    you'll also need to tell the validator what type of field
    you're trying to validate. Add one of these attributes:
    
   >>> <input req="true" err="Error Message" fld="xxxx" ... />
    - text
    - numeric
    - email

 (4) <select> boxes which require validation will need the first option
     set to a blank value:
   
   >>> <select req="true" err="Error Message">
         <option value="">Make a selection</option>
         ...
       </select>


################################################################## */


// =====================================
// TO VALIDATE NUMBER-ONLY INPUTS
// =====================================
function numericField(val)
{
	var returnVal = true;
	
	var inputStr = val.split("");

	//use Regular Expression to find only numbers
	var patt1 = new RegExp("[0-9]");

	for (var i=0; i < inputStr.length; i++) {
	  if (!patt1.test(inputStr[i])) {
	    returnVal = false;
	    break;
	  }

	}
	
	return returnVal;
}


// =====================================
// TO VALIDATE EMAIL ADDRESSES
// =====================================
function checkEmail(emailaddress)
{
	var returnValue = true;

	// make sure value is not blank
	if (emailaddress.value == "")
	{
		returnValue = false;
		alert(emailaddress.getAttribute("err"));	
		emailaddress.focus();
		return returnValue;
	}
	
	// there must be >= 1 character before @, so we
	// start looking at character position 1 
	// (i.e. second character)
	var i = 1;
	var email = emailaddress.value;
	var sLength = email.length;

	// look for @
	while ((i < sLength) && (email.charAt(i) != "@"))
	{
	  i++;
	}

	if ((i >= sLength) || (email.charAt(i) != "@"))
	{
		returnValue = false;
		alert("Email field is invalid");
		emailaddress.focus();
		return returnValue;
	}
	
	else
		i += 2;

	// look for .
	while ((i < sLength) && (email.charAt(i) != "."))
	{ 
	  i++;
	}

	// there must be at least one character after the .
	if ((i >= sLength - 1) || (email.charAt(i) != "."))
	{
		returnValue = false;
		alert("Email field is invalid");
		emailaddress.focus();
		return returnValue;
	}
	
	else
		return returnValue;

}

// =====================================
// TO VALIDATE RADIO AND CHECKBOX INPUTS
// =====================================
function validateSelectForm(question)
{	
	var returnValue = false;
	
	// if only one campaign is listed, QUESTION is not an array
	// check to see if this one item is checked
	if (question.value)
	{		
		if (question.checked)
		{
			returnValue = true;
			return returnValue;
		}
		
		else
		{
			alert(question.getAttribute("err"));
			question.focus();
			return returnValue;
		}
	}
	
	
	// if QUESTION is an array, check to see if any are checked
	else
	{
		for (var i=0; i<question.length; i++)
		{
			if (question[i].checked)
			{
				returnValue = true;
				return returnValue;
			}
		}

		alert(question[0].getAttribute("err"));
		question[0].focus();
		return returnValue;
	}	
}

// ******************************
// ******************************
// function called from the form
// ******************************
// ******************************

function validate(form) {

	returnValue = true;

	// total number of elements in this form
	formarray = form.length;

	// loop through each element in the form	
	for (i=0; i < formarray; i++) {
	
		// don't validate hidden input fields
		if (form.elements[i].type == "hidden") { continue; }

		// don't validate the submit field		
		if (form.elements[i].type == "submit") { continue; }
		
		if (form.elements[i].type == "radio"
		    && form.elements[i].getAttribute("req") == "true") {

			radioArray = new Array();
			count = 0;

			// create an array of all radio button
			// elements with the same name attribute
			do {
				radioArray[count] = form.elements[i];
				count++;
				i++
			}
			
			while (form.elements[i].name == form.elements[i-1].name);
		
			returnValue = validateSelectForm(radioArray);
			i--;
		}

		if (form.elements[i].type == "checkbox"
		    && form.elements[i].getAttribute("req") == "true") {

			checkArray = new Array();
			count = 0;
			
			// create an array of all checkbox
			// elements with the same name attribute
			do {
				checkArray[count] = form.elements[i];
				count++;
				i++;
			}

			while (form.elements[i].name == form.elements[i-1].name);
			
			returnValue = validateSelectForm(checkArray);
			i--;
		}

		if (form.elements[i].type == "select-one"
		    && form.elements[i].getAttribute("req") == "true") {
		
			if (form.elements[i].value == "") {
				returnValue = false;
				alert(form.elements[i].getAttribute("err"));
				form.elements[i].focus();
				return returnValue;

			}
		}

		if (form.elements[i].type == "text"
		    && form.elements[i].getAttribute("req") == "true"
		    && form.elements[i].getAttribute("fld") == "text") {
		
			if (form.elements[i].value == "") {
				returnValue = false;
				alert(form.elements[i].getAttribute("err"));
				form.elements[i].focus();
				return returnValue;

			}
		}

		if (form.elements[i].type == "text"
		    && form.elements[i].getAttribute("req") == "true"
		    && form.elements[i].getAttribute("fld") == "numeric") {

			if (form.elements[i].value == "") {
				returnValue = false;
				alert(form.elements[i].getAttribute("err"));
				form.elements[i].focus();
				return returnValue;

			}

			if (!numericField(form.elements[i].value))
			{
				returnValue = false;
				alert("Field must only use numbers 0-9");	
				form.elements[i].focus();
				return returnValue;

			}
		}

		if (form.elements[i].type == "text"
		    && form.elements[i].getAttribute("req") == "true"
		    && form.elements[i].getAttribute("fld") == "email") {

			if (!checkEmail(form.elements[i]))
			{
				returnValue = false;
				form.elements[i].focus();
				return returnValue;

			}

		}
	}

	return returnValue;

}