almeno1=0
sel="vuoto"
var reservedField = new Array(
                new Array("required_","Campo obbligatorio"),
                new Array("check_","Campo checkato"),
                new Array("valid_","Campo da validare"),
                new Array("numeric_","Campo numerico"),
                new Array("address_","Campo email"),
                new Array("select_","Campo select impostato"),
                new Array("oneselect_","almeno un campo select impostato")
);



function findName(key){

     var stringaTemp = "";

     for(i=0;i<key.length;i++){    //*** elimina gli eventuali suffissi - vedi array reservedField

          stringaTemp = stringaTemp + key.charAt(i);
          for(x=0;x<reservedField.length;x++){
               if(stringaTemp == reservedField[x][0]){
                    stringaTemp = "";
               }
          }
     }

     for(i=0;i<FieldName.length;i++){
          if(FieldName[i][0]==stringaTemp){
               return FieldName[i][1];
               break;
          }
     }
     return false;
}

//***************************************************************//
//***** ciclo principale per controllare i campi della form *****//
//***** se rientrano nei casi:                              *****//
//***** - "required_"         -> il campo è obbligatorio    *****//
//***** - "select_"           -> controllo della selezione  *****//
//***** - "valid_"            -> controllo del contenuto    *****//
//***************************************************************//

function checkForm(objForm){

     for (i=0; i<objForm.elements.length; i++){

          var tempobj=objForm.elements[i];
                //alert(i + " " + tempobj.name + " " + objForm.elements[i].type);
          if ((tempobj.type.indexOf("button") != -1)||
               (tempobj.type.indexOf("submit") != -1)||
               (tempobj.type.indexOf("reset") != -1)||
               (tempobj.type.indexOf("image") != -1))
               continue;

          if (tempobj.name.indexOf(reservedField[0][0]) != -1){ // required
                if((tempobj.name.indexOf(reservedField[1][0]) != -1) && (!checkChecked(tempobj))){
                    return false;
                    break;
               }
               if((tempobj.name.indexOf(reservedField[5][0]) != -1) && (!checkSelected(tempobj))){
                    return false;
                    break;
               }
               if(!checkNotEmpty(tempobj)){
                      return false;
                      break;
               }
			   	if(!checkValid(tempobj)){
                      return false;
                      break;
               }
          }
          if((tempobj.name.indexOf(reservedField[6][0])!= -1)){
                  checkSelect(i,objForm);
                    continue;
               }
          if ((tempobj.name.indexOf(reservedField[2][0]) != -1) && (!checkValid(tempobj))){
               return false;
               break;
          }
          continue;
     }
	 
     if(almeno1==0 && sel=="ko"){
         alert("selezionare almeno un campo opzione");
         return false;
       }
	
     return true;
}

//***************************************************************//
//***** funzione di lancio per il controllo dei campi       *****//
//***************************************************************//

function checkValid(component){
     if(component.name.indexOf(reservedField[3][0]) != -1)
          return checkNumeric(component);
     if(component.name.indexOf(reservedField[4][0]) != -1)
          return checkEMail(component);
     return true;
}

//****************************************//
//***** controllo campo non vuoto    *****//
//****************************************//

function checkNotEmpty(component){

     if(component.value.length == 0){
          if(component.type.indexOf("hidden") == -1){
		  alert('Compila il campo obbligatorio: "' + findName(component.name) +'"');
		  component.focus();
          //component.select();
		  }
		  else{
		  alert('Selezionare la "' + findName(component.name) +'"');
		  }
          return false;
     }
     return true;
}

//****************************************//
//***** controllo campo numerico     *****//
//****************************************//

function checkNumeric(component){
     var string = component.value;
     if(string == "")
                return true;

     if(!checkNumericValue(string)){
          alert('Il campo obbligatorio "' + findName(component.name) + '" deve essere numerico.');
          component.focus();
          component.select();
          return false;
     }
     return true;
}

function checkNumericValue(string){
     var decimale = false;
     for(j=0; j<string.length;j++){
          s = string.charAt(j);
          if(isNaN(parseInt(s))){
               if ((s == ".")&&(decimale == false)){
                    decimale = true;
               }
               else if ((s == ",")&&(decimale == false)){
                    decimale = true;
               }
               else{
                    return false;
                    break;
               }
          }
     }
     return true;
}

//****************************************//
//***** controllo data               *****//
//****************************************//

function checkDate(objectDay,objectMonth,objectYear){
     var day = objectDay.value;
     var month = objectMonth.value;
     var year = objectYear.value;

       if(day == "gg"){
                objectDay.value = "";
                day = "";
       }
       if(month == "mm"){
                objectMonth.value = "";
                month = "";
       }
       if(year == "aaaa"){
                objectYear.value = "";
                year = "";
       }
       if((day=="") && (month=="") && (year=="")){
                return true;
       }

       if(!checkNumericValue(day)){
          alert("Campo Giorno errato");
          objectDay.focus();
          objectDay.select();
          return false;
     }

     if(!checkNumericValue(month)){
          alert("Campo Mese errato");
          objectMonth.focus();
          objectMonth.select();
          return false;
     }

     if(!checkNumericValue(year)){
          alert("Campo Anno errato");
          objectYear.focus();
          objectYear.select();
          return false;
     }

   if ((month < 1) || (month > 12)) { // check month range
      alert("Il Mese deve essere compreso tra 1 e 12.");
      objectMonth.focus();
      objectMonth.select();

      return false;
   }
   if ((day < 1) || (day > 31)) {
      alert("Il Giorno deve essere compreso tra 1 e 31.");
      objectDay.focus();
      objectDay.select();
      return false;
   }
   if (((month==4) || (month==6) || (month==9) || (month==11)) && (day==31)) {
      alert("Il Mese "+month+" non può avere 31 giorni!")
      objectDay.focus();
      objectDay.select();
      return false
   }
   if (month == 2) { // check for february 29th
      var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
      if (day>29 || (day==29 && !isleap)) {
         alert("Febbraio " + year + " non ha " + day + " giorni!");
           objectMonth.focus();
           objectMonth.select();
         return false;
      }
   }
   if (year < 1000) {
     alert("Campo Anno errato.");
      objectYear.focus();
      objectYear.select();
     return false;
   }
   return true;  // date is valid
}

//****************************************//
//***** controllo campo ora          *****//
//****************************************//

function checkHour(objectHours,objectMinutes){
     var hours = objectHours.value;
     var minutes = objectMinutes.value;
     if((!checkNumericValue(hours))||((hours < 0 || hours > 23))){
          alert("Campo Ore errato");
          objectHours.focus();
          objectHours.select();
      return false;
     }
     if((!checkNumericValue(minutes))||((minutes < 0 || minutes > 59))){
          alert("Campo Minuti errato");
          objectMinutes.focus();
          objectMinutes.select();
      return false;
     }
     return true;
}

//****************************************//
//***** controllo campo Checked      *****//
//****************************************//

function checkChecked(component){
     if(!component.checked){
          alert("Please enter your information in the obligatory field " + findName(component.name));
          component.focus();
          component.select();
          return false;
     }
     return true;
}

//****************************************//
//***** controllo campo Selected     *****//
//****************************************//

function checkSelected(component){
     //alert(component.value);
     if(component.value == ""){
          alert("Please enter your information in the obligatory field " + findName(component.name));
          component.focus();
          //component.select();
          return false;
     }
     return true;
}

//****************************************//
//***** controllo campo email        *****//
//****************************************//

function checkEMail(component){
     if(component.value.lenth == 0){
          alert("Please enter your information in the obligatory field e-mail address.");
          component.focus();
          component.select();
          return false;
     }
     if(component.value.indexOf('@') == -1 ||
          component.value.indexOf('@') == 0 ||
          component.value.indexOf('.') == (component.value.length -1)||
		  component.value.indexOf('.') == -1 ||
		  component.value.indexOf('.') == (component.value.length) ||
		  component.value.indexOf('@') == (component.value.length) ||
		  component.value.indexOf('.') == (component.value.indexOf('@')-1)
		  ){
          alert('Il campo obbligatorio "E-mail" non è corretto');
          component.focus();
          component.select();
          return false;
     }
     return true;
}


//*******************************************************//
//***** controllo di riempimento di un campo almeno *****//
//*******************************************************//

function checkAtLeastOne(objForm){
     for (i=0; i<objForm.elements.length; i++){
          var tempobj=objForm.elements[i];

         if ((tempobj.type.indexOf("hidden") != -1)||
               (tempobj.type.indexOf("button") != -1)||
               (tempobj.type.indexOf("submit") != -1)||
               (tempobj.type.indexOf("reset") != -1)||
               (tempobj.type.indexOf("image") != -1))
               continue;

         if (objForm.elements[i].value == "")
                    continue;
               else{
                    return true;
                    break;
               }
          }
          alert("Compilare almeno uno dei campi");
          return false;
}

function textCounter(field, countfield, maxlimit) {
          if (field.value.length > maxlimit) // if too long...trim it!
               field.value = field.value.substring(0, maxlimit);
          else
               countfield.value = maxlimit - field.value.length;
}
//*******************************************************//
//***** controllo delle oneselect                   *****//
//*******************************************************//
function checkSelect(i,objForm) {
      if ((TheForm.elements[i].selectedIndex == 0))
      {
       sel="ko"
       return true;
      }
      else
      {
      almeno1=almeno1+1
       return true;
      }
}