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

window.onload = attachFormHandlers;

function attachFormHandlers()
{
	
	var form = document.getElementById('form1') // 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 "password":
					sFeedback = validatePassword(sVal);
					break;
				case "name":
					sFeedback = validateName(sVal);
					break;
				case "numeric":
					sFeedback = validateNumeric(sVal);
					break;
				case "address":
					sFeedback = validateAddress(sVal);
					break;
				case "codf":
					sFeedback = validateCodf(sVal);
					break;
				case "username":
					sFeedback = validateUsername(sVal);
					break;
				case "prov":
					sFeedback = validateProv(sVal);
					break;
				case "prvradio":
					sFeedback = validatePrvradio();
					break;
				case "citta":
					sFeedback = validateCitta();
					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 "Data non valida";
	}

}

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 "Indirizzo Email non valido";
	}
}

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]
 var regex=/^(\d{3}-\d{3}-\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 "Numero non valido";
	}
}

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{5})(\d{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 ZipCode";
	}
}

function validatePassword(sVal)
{ 
//Description: The password's first character must be a letter, it must contain at least 6 characters
//and no more than 15 characters and no characters other than letters, numbers and the underscore may be used
//Matches: 	[abcdef], [aBc45DSD_sdf], [password] (http://www.regexlib.com)
 var regex=/^[a-zA-Z]\w{5,14}$/;
 
	// do the comparison, if we have a match write thank you or else the email is invalid
	if (regex.test(sVal))
	{
      return "*";
	}
	else
	{
      return "Password non valida. La password non deve contenere spazi o caratteri speciali.";
	}
}

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 3 & 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]{3,32})$/;
 
	// do the comparison, if we have a match write thank you or else the email is invalid
	if (regex.test(sVal))
	{
      return "*";
	}
	else
	{
      return "Valore non valido";
	}
}


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 "Valore non valido";
	}
}

function validateAddress(sVal)
{ 
//Description: The password's first character must be a letter, it must contain at least 6 characters
//and no more than 15 characters and no characters other than letters, numbers and the underscore may be used
//Matches: 	[abcdef], [aBc45DSD_sdf], [password] (http://www.regexlib.com)
 var regex=/^[a-zA-Z\d\s]{3,31}$/;
 
	// do the comparison, if we have a match write thank you or else the email is invalid
	if (regex.test(sVal))
	{
      return "*";
	}
	else
	{
      return "Valore non valido";
	}
}

function validateCodf(sVal)
{ 
//Description: The password's first character must be a letter, it must contain at least 16 characters
//and no more than 16 characters and no characters other than letters, numbers and the underscore may be used
//Matches: 	[abcdef], [aBc45DSD_sdf], [password] (http://www.regexlib.com)
 var regex=/^[a-zA-Z]\w{15,15}$/;
 
	// do the comparison, if we have a match write thank you or else the email is invalid
	if (regex.test(sVal))
	{
      return "*";
	}
	else
	{
      return "Codice Fiscale non valido.";
	}
}

function validateUsername(sVal)
{ 
//Description: The password's first character must be a letter, it must contain at least 6 characters
//and no more than 15 characters and no characters other than letters, numbers and the underscore may be used
//Matches: 	[abcdef], [aBc45DSD_sdf], [password] (http://www.regexlib.com)
 var regex=/^[a-zA-Z]\w{5,14}$/;
 
	// do the comparison, if we have a match write thank you or else the email is invalid
	if (regex.test(sVal))
	{
      return "*";
	}
	else
	{
      return "Nome utente non valido. Il nome non può contenere spazi o caratteri speciali.";
	}
}

function validateProv(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 3 & 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]{2,32})$/;
 
	// do the comparison, if we have a match write thank you or else the email is invalid
	if (regex.test(sVal))
	{
      return "*";
	}
	else
	{
      return "Valore non valido";
	}
}

function validatePrvradio()
{ 

//Description: The password's first character must be a letter, it must contain at least 6 characters
//and no more than 15 characters and no characters other than letters, numbers and the underscore may be used
//Matches: 	[abcdef], [aBc45DSD_sdf], [password] (http://www.regexlib.com)
 //var regex=/^s/;
 
	// do the comparison, if we have a match write thank you or else the email is invalid
	//if (regex.test(sVal))
	//{
      //return "*";
	//}
	//else
	//{
      //return "&Eacute; necessario accettare le condizioni per poter eseguire la registrazione.";
	//}
	if (document.form1.prvRadio[0].checked){
		return "*";
	}
	else
	{
		if (document.form1.prvRadio[1].checked){
			return "&Eacute; necessario accettare le condizioni per poter eseguire la registrazione.";
		}
	}
}

function validateCitta(sVal)
{ 

 var regex=/^[a-zA-Z\s]{3,31}$/;
 
	if (regex.test(sVal))
	{
      return "*";
	}
	else
	{
      return "Valore non valido";
	}
}

