<!--
/* **************************************************************************
Creato da Rosario Sensale
Mail rosario.sensale@virgilio.it
Creato il 21/12/2004
Ver 1.10.00

==============================================
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")


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.
*****************************************************************************/
/* 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 validate(this_form) {
var object_input_number;
var object_index;
var this_object;
var this_object_type;
var this_object_required;
var this_object_value;
var this_object_length;
var this_object_alarm;
var this_object_minlenght;
var this_object_maxlenght;
var this_object_len;

object_input_number=this_form.getElementsByTagName('input').length;
object_index=0;

// start for TYPE: INPUT
 for (object_index=0;object_index<object_input_number;object_index++){
    this_object = this_form.getElementsByTagName('input')[object_index];
    this_object_type=this_object.getAttribute('type').toLowerCase();
    


    //object must be not submit o reset
    if ((this_object_type != "submit") && (this_object_type != "reset")){

        
        //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";
        }

        //only required
        if (this_object_required="yes"){
            this_object_name=this_object.name;
            this_object_value=this_object.value;
            this_object_len=this_object.value.length;

            //check is not null
            if (this_object_value == ''){
                this_object_alarm=this_object.getAttribute('alarm');

                if (this_object_alarm == null) {
                    this_object_alarm = IS_REQUIRED + this_object_name;
                }

                show_message(this_object_alarm);
                //go to object
                eval("this_form."+ this_object_name +".focus()");
                return false;
            } // IF is null
            

            //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;
               }
            }


            //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;
               }
            }

            //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;
               }
            }

            //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;
                }
             }




        }
    } // IF object must be not submit o reset
    } // For




 // start for per object SELECT
 for (object_index=0;object_index<object_input_number;object_index++){
    this_object = this_form.getElementsByTagName('select')[object_index];
    //if not an select fields
    if (this_object != null) {
        this_object_type=this_object.getAttribute('type').toLowerCase();

        //object must be not submit o reset
        if ((this_object_type != "submit") && (this_object_type != "reset")){


            //check if is required
            if (this_object.getAttribute('required')) {
                this_object_required=this_object.getAttribute('required').toLowerCase();
            }else{
                this_object_required="no";
            }

            //only required
            if (this_object_required="yes"){
                this_object_name=this_object.name;
                this_object_value=this_object.value;
                this_object_len=this_object.value.length;

                //check is not null
                if (this_object_value == ''){
                    this_object_alarm=this_object.getAttribute('alarm');

                    if (this_object_alarm == null) {
                        this_object_alarm = IS_REQUIRED + this_object_name;
                    }

                    show_message(this_object_alarm);
                    //go to object
                    eval("this_form."+ this_object_name +".focus()");
                    return false;
                } // IF is null
        }
        } //End if
     }
}//end for


return true;
} //end function

-->
