var gShow; //variable holding the id where feedback will be sent to.
var gErrors = 0; //number of errors is set to none to begin with
var objInput = document.getElementsByTagName('input');
var ninputs = 0;

function localChecks(){
	localValidate();
	return false;
}

function localValidate()
{
   for (i=0; i<objInput.length; i++){
   	sVal = objInput[i].value; //get value inside of input field
   	sRules = objInput[i].className.split(' '); // get all the rules from the input box classname
   	sValidate = sRules[0]; // determines if field is to be validated or not   
   	sRequired = sRules[1]; // determines if field is required or not
   	sTypeCheck = sRules[2]; //typecheck are additional validation rules (ie. email, phone, date)
   	gShow = sRules[3]; //gShow is the td id where feedback is sent to.      
      
      if (sVal=="Submit"){
         continue;
      }
      
      if (sValidate != "validate"){
         continue;
      }
      
      var result = "";
      if ((sRequired == "required") && (sVal == ""))
      {
         result = "Required";
      }
      else
      {
         switch (sTypeCheck) 
         {
            case "username":
               if (sVal.match(/^[a-zA-Z0-9_-]{5,18}$/))
               {
                  result = "Ok"; 
               }		
               else
               {
                  result = "Invalid Username";
               }
               break;
            case "password":
               if (sVal.match(/^([a-zA-Z0-9@*#]{5,18})$/))
               {
                  result = "Ok";				
               }		
               else
               {
                  result = "Invalid Password";
               }
               break;
            case "email":
               if (sVal.match(/^([0-9a-zA-Z]+[-._+&])*[0-9a-zA-Z]+@([-0-9a-zA-Z]+[.])+[a-zA-Z]{2,6}$/))
               {
                  result = "Ok";				
               }		
               else
               {
                  result = "Invalid Email";
               }
               break;           
            default:
         } 	
      }

      document.getElementById(gShow).innerHTML = "";
      document.getElementById(gShow).appendChild(document.createTextNode(result));
      
   	// se il campo non è valido lo tolgo
   	if (result != "Ok")
   	{
   		objInput[i].value = "";
   	}      
   }

   validate();
}

function validate(){
   // called by onsubmit, it is only used to format cells'colors
   var tables; 
   tables = document.getElementsByTagName('td')
	for (i=0; i<tables.length; i++){//loop through all the <td> elements 
		// if the class name of that td element is rules check to see if there are error warnings
		if (tables[i].className == "rules"){
			if (tables[i].innerHTML == "Ok"){
				tables[i].style.color = '#FFFFFF';//the color is changed to black or stays black
			}
			else {
				gErrors = gErrors + 1; //the error count increases by 1
				tables[i].style.color = '#F5C116';//error messages are changed to red
			}
		}
	}
		
	if (gErrors > 0){
		//if there are any errors give an optional message
		//alert ("Please make sure all fields are properly completed.");
		gErrors = 0;// reset errors to 0
		return false;
	}
	else {
		document.forms[0].submit();
	}
}


