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

window.onload = attachFormHandlers;

function attachFormHandlers()
{
	
	var form = document.getElementById('enquiryForm') // 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 "email":
					sFeedback = validateEmail(sVal);
					break;
				case "name":
					sFeedback = validateName(sVal);
					break;
				case "phone":
					sFeedback = validatePhone(sVal);
					break;
				case "fax":
					sFeedback = validateFax(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 "Thank You";
		}
	}
}

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 "Thank You";
	}
	else
	{
      return "Invalid Email Address";
	}
}

function validateName(sVal)
{ 
//This is the simplest RegEx for validating someone's name. The name can contain only alphabets(in either case) &amp; 
//should be of minimum length 4 &amp; maximum length 32. Only white spaces are allowed apart from alphabets.
//Matches: 	[some body], [hey there], [hello] (http://www.regexlib.com)
 var regex=/^([a-zA-z\s\t]{4,82})$/;
 
	// do the comparison, if we have a match write thank you or else the email is invalid
	if (regex.test(sVal))
	{
      return "Thank You";
	}
	else
	{
      return "Invalid Name";
	}
}


function validatePhone(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|,|\s)*\.?\d*$/;
 
	// do the comparison, if we have a match write thank you or else the email is invalid
	if (regex.test(sVal))
	{
      return "Thank You";
	}
	else
	{
      return "Invalid Phone Number";
	}
}

function validateFax(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|,|\s)*\.?\d*$/;
 
	// do the comparison, if we have a match write thank you or else the email is invalid
	if (regex.test(sVal))
	{
      return "Thank You";
	}
	else
	{
      return "Invalid Fax Number";
	}
}

