//Created By: Chris Campbell
//www.particletree.com

window.onload = attachFormHandlers;

function attachFormHandlers()
{
	
	var forms = document.getElementsByTagName('form') // get the form
	for (var iCounter=0; iCounter<forms.length; iCounter++) {
	//console.log(forms[iCounter].getAttribute("id"));
	//forms[iCounter].onsubmit = function(){return validate(forms[iCounter].getAttribute("id"));} //attach validate() to the form
	//forms[iCounter].
	}
}
var valido = true;
function validate(form) {
	var objInputs = document.getElementById(form).getElementsByTagName('input'); // store all input fields
	for (var iCounter=0; iCounter<objInputs.length; iCounter++) {
		objInput=objInputs[iCounter];
		//document.getElementById('myElement').type = 'text';
		if (objInput.type=="text" || objInput.type=="password") {
			sVal = objInput.value; //get value inside of input field
			var sFeedBack; //feedback is the feedback message sent back to the user
			
			sRules = objInput.className.split(' '); // get all the rules from the input box classname
			sRequired = sRules[0]; // required means field is required
			sTypeCheck = sRules[1]; //typecheck are additional validation rules (ie. email, phone, date)
			sFeedback = validateRequired (sRequired, sVal); //validateRequired() checks if it is required and then sends back feedback
			//alert(valido);
			if (valido) //if it is required and blank gContinue is false and we don't validate anymore.  // this is done because if it is blank
			{
				switch (sTypeCheck){
					case "date":
					case "email":
					case "password":
					case "name":
					case "numeric":
						sFeedback = validateType(sVal, sTypeCheck);
						break;
					case "uguale":
						var sVal2=objInputs[iCounter-1].value;
						sFeedback = validateUguali(sVal, sVal2);
						break;
				}
			}
			// after validation is complete return the feedback 
			document.getElementById('validate_'+objInput.getAttribute('id')).innerHTML = sFeedback;
		}
	}
	return valido;
}

function validateRequired(sRequired, sVal)
{
	if (sRequired == "req")  //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
		{
			valido = false;
			return  "Campo obbligatorio";
	 	}
  		
	} 
	return "";
}

function validateType(sVal,type)
{
var regex=[];
// our date regular expression (http://www.regexlib.com)
regex["date"]=/(((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])))/;
regex["email"]=/^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;
regex["password"]=/^[a-zA-Z]\w{3,14}$/;
regex["name"]=/^([a-zA-z\s]{4,32})$/;
regex["numeric"]=/^(\d|-)?(\d|.)*\,?\d*$/;

 
	// do the comparison, if we have a match write thank you or else the date is invalid
	if (regex[type].test(sVal))
	{
      return "";
	}
	else 
	{
	  valido = false;
      return "Campo non valido";
	}

}
function validateUguali(sVal, sVal2)
{
	if (sVal==sVal2)
	{
      return "";
	}
	else 
	{
	  valido = false;
      return "Il valore immesso non corrisponde";
	}

}

/*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 
	{
	  valido = false;
      return "Data errata";
	}

}

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
	{
	  valido = false;
      return "Indirizzo Email non valido";
	}
}

function validatePassword(sVal)
{ 
//Description: The password's first character must be a letter, it must contain at least 4 characters
//and no more than 15 characters and no characters other than letters, numbers and the underscore may be used
//Matches: 	[abcd], [aBc45DSD_sdf], [password] (http://www.regexlib.com)
 var regex=/^[a-zA-Z]\w{3,14}$/;
 
	// do the comparison, if we have a match write thank you or else the email is invalid
	if (regex.test(sVal))
	{
      return "";
	}
	else
	{
	  valido = false;
      return "Password non corretta";
	}
}

function validateName(sVal)
{ 
//This is the simplest RegEx for validating someone's name. The name can contain only alphabets(in either case) & 
//should be of minimum length 4 & 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]{4,32})$/;
 
	// do the comparison, if we have a match write thank you or else the email is invalid
	if (regex.test(sVal))
	{
      return "";
	}
	else
	{
	  valido = false;
      return "Nome non corretto";
	}
}


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
	{
	  valido = false;
      return "Numero non corretto";
	}
}*/
