<!--
/* **************************************************************************
==============================================
CODICE DA INSERIRE NELL'HEAD DELLA PAGINA HTML
==============================================

<script type="text/javascript" src="Language.js"></script>
<script type="text/javascript" src="FormValidator.js"></script>

==============================================
CODICE DA INSERIRE NEL MODULO
==============================================

onsubmit="javascript:return validate(this);"

==============================================
TAG DA INSERIRE NEL CAMPI DEL MODULO
==============================================

required="yes"                       => Se il campo č obbligatorio
ismail="yes"                         => Se č un campo mail
minchar="x"                          => Se deve contenere almeno x caratteri
maxchar="x"                          => Se deve contenere al max x caratteri
isnumeric="yes"                      => Deve contenere un valore numerico
                                        (per default isnumeric="no")

nameobject="name"                    => Deve contenere il nome dell'oggeto che
                                        verrā visualizzato nel msg.


Versioni:

1.00.00 alla 1.10.00                 => E' stato corretto un bug dovuto ad eventuali
                                     oggetti SELECT che non erano presenti su
                                     un form.

1.10.00 alla 1.20.00                 => E' stato aggiunto il controllo sugli oggetti
                                     textarea (controllo su richiesto, min char, 
				     maxchar).

*****************************************************************************/
/* Show error message */
function show_message(report){
	alert(report);
}

//check mail
function check_email(address) {
  if ( (address == "") || (address.indexOf ('@') == -1) || (address.indexOf ('.') == -1)) {
     return false;
  }else{
        return true;
  }
}

//Is number? TRUE => Is number
function ValidNumber(item) {
 if (isNaN(item) == true) return false;
    return true;
}



function validateForm(this_form) {
var objArrayObject;
var this_object;
var index;

var this_object_required;
var this_object_ismail;
var this_object_minlenght;
var this_object_maxlenght;
var this_object_name_msg;


//get all
var objArrayObject = this_form.getElementsByTagName('*');
for (index=0; index<objArrayObject.length; index++) {

    if (objArrayObject[index].type == 'text' || objArrayObject[index].type == 'password') {

          this_object = this_form.getElementsByTagName('*')[index];
          this_object_name=objArrayObject[index].name;
          this_object_value=objArrayObject[index].value;
          this_object_len=objArrayObject[index].value.length;

          //check if is required
          if (this_object.getAttribute('required')) {
              this_object_required=this_object.getAttribute('required').toLowerCase();
          }else{
              this_object_required="no";
          }

           //is mail?
           if (this_object.getAttribute('ismail')) {
              this_object_ismail=this_object.getAttribute('ismail').toLowerCase();
           }else{
              this_object_ismail="no";
           }

           //min char
           if (this_object.getAttribute('minchar')) {
               this_object_minlenght=this_object.getAttribute('minchar').toLowerCase();
           }else{
               this_object_minlenght=0;
           }

           //max char
           if (this_object.getAttribute('maxchar')) {
              this_object_maxlenght=this_object.getAttribute('maxchar').toLowerCase();
           }else{
              this_object_maxlenght=0;
           }

           //is numeric?
           if (this_object.getAttribute('isnumeric')) {
              this_object_isnumeric=this_object.getAttribute('isnumeric').toLowerCase();
           }else{
               this_object_isnumeric="no";
           }

           //name of object
           if (this_object.getAttribute('nameobject')) {
               this_object_name_msg=this_object.getAttribute('nameobject'); //.toLowerCase();
           }else{
                this_object_name_msg=this_object.name;
           }

           //only required
           if (this_object_required=="yes"){
               //check is null
               if (this_object_len == 0){
                   this_object_alarm=this_object.getAttribute('alarm');

                   if (this_object_alarm == null) {
                      this_object_alarm = IS_REQUIRED + this_object_name_msg;
                   }
                   show_message(this_object_alarm);
                   //go to object
                   eval("this_form."+ this_object_name +".focus()");
                   return false;
               }//end of len
               
               
               //check is mail
               if (this_object_ismail == "yes"){
                   if (check_email(this_object_value) == false){
                       //get error message
                       this_object_alarm=this_object.getAttribute('alarm');
                       if (this_object_alarm == null) {
                           this_object_alarm = IS_NOT_MAIL;
                           }
                       show_message(this_object_alarm);
                       //go to object
                       eval("this_form."+ this_object_name +".focus()");
                       return false;
                       }
               } // end fro check mail

               //check min char
               if (this_object_minlenght > 0){
                   if (this_object_len < this_object_minlenght){
                       //get error message
                       this_object_alarm=this_object.getAttribute('alarm');
                       if (this_object_alarm == null) {
                           this_object_alarm = IS_MIN_CHAR_FIRST + this_object_minlenght + IS_MIN_CHAR_LAST;
                           }
                       show_message(this_object_alarm);
                       //go to object
                       eval("this_form."+ this_object_name +".focus()");
                       return false;
                       }
                   }//end of min char

               //check max char
               if (this_object_maxlenght > 0){
                   if (this_object_len > this_object_maxlenght){
                       //get error message
                       this_object_alarm=this_object.getAttribute('alarm');
                       if (this_object_alarm == null) {
                           this_object_alarm = IS_MAX_CHAR_FIRST + this_object_maxlenght + IS_MAX_CHAR_LAST;
                           }
                       show_message(this_object_alarm);
                       //go to object
                       eval("this_form."+ this_object_name +".focus()");
                       return false;
                       }
                   }//end of max char

                //Is numeric?
                if (this_object_isnumeric == "yes"){
                    if (ValidNumber(this_object_value)==false){
                        //get error message
                        this_object_alarm=this_object.getAttribute('alarm');
                        if (this_object_alarm == null) {
                            this_object_alarm = IS_NOT_NUMERIC;
                            }
                        show_message(this_object_alarm);
                        //go to object
                        eval("this_form."+ this_object_name +".focus()");
                        return false;
                        }
                    } // end of numeric
           } // end if of required

    }else if (objArrayObject[index].type == 'select-one') {
          this_object = this_form.getElementsByTagName('*')[index];
          this_object_name=objArrayObject[index].name;
          this_object_value=objArrayObject[index].value;
          this_object_len=objArrayObject[index].value.length;

          //check if is required
          if (this_object.getAttribute('required')) {
              this_object_required=this_object.getAttribute('required').toLowerCase();
          }else{
              this_object_required="no";
          }

           //name of object
           if (this_object.getAttribute('nameobject')) {
               this_object_name_msg=this_object.getAttribute('nameobject');//.toLowerCase();
           }else{
                this_object_name_msg=this_object.name;
           }


           //only required
           if (this_object_required=="yes"){
               //check is null
               if (this_object_len == 0){
                   this_object_alarm=this_object.getAttribute('alarm');

                   if (this_object_alarm == null) {
                      this_object_alarm = IS_REQUIRED + this_object_name_msg;
                   }
                   show_message(this_object_alarm);
                   //go to object
                   eval("this_form."+ this_object_name +".focus()");
                   return false;
               }//end of len
           } // end if of required


    }else if (objArrayObject[index].type == 'textarea') {
          this_object = this_form.getElementsByTagName('*')[index];
          this_object_name=objArrayObject[index].name;
          this_object_value=objArrayObject[index].value;
          this_object_len=objArrayObject[index].value.length;


          //check if is required
          if (this_object.getAttribute('required')) {
              this_object_required=this_object.getAttribute('required').toLowerCase();
          }else{
              this_object_required="no";
          }


           //min char
           if (this_object.getAttribute('minchar')) {
               this_object_minlenght=this_object.getAttribute('minchar').toLowerCase();
           }else{
               this_object_minlenght=0;
           }

           //max char
           if (this_object.getAttribute('maxchar')) {
              this_object_maxlenght=this_object.getAttribute('maxchar').toLowerCase();
           }else{
              this_object_maxlenght=0;
           }


           //check min char
           if (this_object_minlenght > 0){
              if (this_object_len < this_object_minlenght){
                 //get error message
                 this_object_alarm=this_object.getAttribute('alarm');
                 if (this_object_alarm == null) {
                    this_object_alarm = IS_MIN_CHAR_FIRST + this_object_minlenght + IS_MIN_CHAR_LAST;
                 }
                 show_message(this_object_alarm);
                 //go to object
                 eval("this_form."+ this_object_name +".focus()");
                 return false;
                 }
              }//end of min char

              //check max char
              if (this_object_maxlenght > 0){
                 if (this_object_len > this_object_maxlenght){
                    //get error message
                    this_object_alarm=this_object.getAttribute('alarm');
                    if (this_object_alarm == null) {
                        this_object_alarm = IS_MAX_CHAR_FIRST + this_object_maxlenght + IS_MAX_CHAR_LAST;
                    }
                    show_message(this_object_alarm);
                    //go to object
                    eval("this_form."+ this_object_name +".focus()");
                    return false;
                 }
              }//end of max char


              //only required
              if (this_object_required=="yes"){
                  //check is null
                  if (this_object_len == 0){
                     this_object_alarm=this_object.getAttribute('alarm');

                      if (this_object_alarm == null) {
                          this_object_alarm = IS_REQUIRED + this_object_name_msg;
                      }
                      show_message(this_object_alarm);
                      //go to object
                      eval("this_form."+ this_object_name +".focus()");
                      return false;
                   }//end of len
              } // end if of required

    }//end if of select
}

return true;
} //end function

-->

