//Created By: Chris Campbell
//www.particletree.com

window.onload = attachFormHandlers;

function attachFormHandlers()
{
	
	var form = document.getElementById('validate_form') // get the form

	if (document.getElementsByTagName)//make sure were on a newer browser
	{
		var objInput = document.getElementsByTagName('input'); // store all input fields
		for (var iCounter=0; iCounter<objInput.length; iCounter++)
	
		objInput[iCounter].onchange = function(){return attach(this);} //attach the onchange to each input field
	}
  
	form.onsubmit = function(){return validate();} //attach validate() to the form
  
}

var gContinue = true;
function attach(objInput)
{
	sVal = objInput.value; //get value inside of input field
	var sFeedBack; //feedback is the feedback message sent back to the user
	gContinue = true; 
	
	sRules = objInput.className.split(' '); // get all the rules from the input box classname
	sValidate = sRules[0]; //validate means we will validate the field
	sRequired = sRules[1]; // required means field is required
	sTypeCheck = sRules[2]; //typecheck are additional validation rules (ie. email, phone, date)
	sFeedbackLoc = sRules[3]; //feedbackLoc is the td id where feedback is sent to.
	sFeedback = validateRequired (sRequired, sVal, sTypeCheck); //validateRequired() checks if it is required and then sends back feedback
		
			
		if (gContinue) //if it is required and blank gContinue is false and we don't validate anymore.  // this is done because if it is blank
		//it will also fail other tests.  We don't want to spam the user with INVALID EMAIL!! if the field is still blank.
		{
			// check the different validation cases (ie: email, phone, etc.)
			switch (sTypeCheck)
			
			{
				case "date":
					sFeedback = validateDate(sVal);
					break;
				case "email":
					sFeedback = validateEmail(sVal);
					break;
				case "phone":
					sFeedback = validatePhone(sVal);
					break;
				case "zip":
					sFeedback = validateZip(sVal);
					break;
				case "numeric":
					sFeedback = validateNumeric(sVal);
					break;
			}
		}
			// after validation is complete return the feedback 
			document.getElementById(sFeedbackLoc).innerHTML = sFeedback;	
}


function validateRequired(sRequired, sVal, sTypecheck)
{
	if (sRequired == "required")  //check if required if not, continue validation script
	{
   		if (sVal == "") //if it is rquired and blank then it is an error and continues to be required
		{
			gContinue = false;
			return  "Required";
	 	}
  		else if (sTypecheck == "none")  //if its not blank and has no other validation requirements the field passes
		{
			return "";
		}
	}
}


function validateDate(sVal)
{
	// our date regular expression (http://www.regexlib.com)
 var regex=/(((0[13578]|10|12)([-.\/])(0[1-9]|[12][0-9]|3[01])([-.\/])(\d{4}))|((0[469]|11)([-.\/])([0][1-9]|[12][0-9]|30)([-.\/])(\d{4}))|((2)([-.\/])(0[1-9]|1[0-9]|2[0-8])([-.\/])(\d{4}))|((2)(\.|-|\/)(29)([-.\/])([02468][048]00))|((2)([-.\/])(29)([-.\/])([13579][26]00))|((2)([-.\/])(29)([-.\/])([0-9][0-9][0][48]))|((2)([-.\/])(29)([-.\/])([0-9][0-9][2468][048]))|((2)([-.\/])(29)([-.\/])([0-9][0-9][13579][26])))/;
 
	// do the comparison, if we have a match write thank you or else the date is invalid
	if (regex.test(sVal))
	{
      return "";
	}
	else 
	{
      return "Invalid Date";
	}

}

function validateEmail(sVal)
{
	
// our email regular expression (http://www.regexlib.com)
 var regex=/^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;
 
	// do the comparison, if we have a match write thank you or else the email is invalid
	if (regex.test(sVal))
	{
      return "";
	}
	else
	{
      return "Invalid Email Address";
	}
}

function validatePhone(sVal)
{
	
// our phone regular expression
// This expression is a very simplex expression that allows null values or 3 digits, dash, 
//3 digits, dash, 4 digits. It validates a basic US phone number. Written by Jason N. Gaylord.(http://www.regexlib.com)
// Matches:  	 [555-555-1212], [123-456-7890]

// ^(\d{3}-\d{3}-\d{4})*$
 var regex=/^(\d{1,3} \(?\d\d\d\)?)?( |-|\.)?\d\d\d( |-|\.)?\d{4,4}(( |-|\.)?[ext\.]+ ?\d+)?$/;
 
	// do the comparison, if we have a match write thank you or else the email is invalid
	if (regex.test(sVal))
	{
      return "";
	}
	else
	{
      return "Invalid Phone - Ex. 1 (888) 123-4567";
	}
}


function validateZip(sVal)
{
	
// our email regular expression
//Javascript matches US zipcodes not allowing all zeros in first 5 or +4 (http://www.regexlib.com)
// Matches:  	 [12345], [12345-6789], [123456789]
 var regex=/(^(?!0{4,5})(\d{4,5})(?!-?0{4})(-?\d{4})?$)/;
 
	// do the comparison, if we have a match write thank you or else the email is invalid
	if (regex.test(sVal))
	{
      return "";
	}
	else
	{
      return "Invalid Postal Code";
	}
}




function validateNumeric(sVal)
{ 
//Input for Numeric values. Handles negatives, and comma formatted values. Also handles a single decimal point
//Matches: 	[5,000], [-5,000], [100.044] (http://www.regexlib.com)
 var regex=/^(\d|-)?(\d|,)*\.?\d*$/;
 
	// do the comparison, if we have a match write thank you or else the email is invalid
	if (regex.test(sVal))
	{
      return "";
	}
	else
	{
      return "Invalid Number";
	}
}
