/*-----------------------------------------------------------------*/
/* JavaScript Document: CVA srl --*/
/*-----------------------------------------------------------------*/
function menu() {
	if (document.all&&document.getElementById) {
		if (document.getElementById("nav")) {
			navRoot = document.getElementById("nav");
			for (i=0; i<navRoot.childNodes.length; i++) {
				node = navRoot.childNodes[i];
				if (node.nodeName=="LI") {
					node.onmouseover=function() {
						this.className+=" over";
	 				 	}
	 			 node.onmouseout=function() {
	  		this.className=this.className.replace("over", "");
	   				}
	   			}
	  		}
		}
	}
}
function open_url(page) {
	if (page != "none") {
		location.href = page;
	} 
}
function sel_cliente(value) {
	if (value=="n") {
		location.href = "?";
	}else {
		location.href = "?id="+value;
	}
}

function write_mail(user, domain, tld) {  
    var mail;
    mail = '<a href="' + 'ma' + 'il' + 'to:' + user;  
    mail += '&#64;' + domain + '.' + tld;  
    mail += '">' + user + '&#64;' + domain + '.' + tld + '<' + '/a>';  
    document.write(mail);  
} 

// JavaScript Document
function ValidateForm(NameForm, Action) {

 //imposto la variabile che segnala  l'errore
 var error=0;
 
 //imposto la variabile del nome form
 var form = document.forms[NameForm]
 
 //prendo il contenuto html compreso nel mio form 
 var form_html = form.innerHTML;


 //controllo che il browser supporti questo tipo di funzione
 if(document.getElementsByTagName)
 {
   //predispongo un array con tutti i possibili elementi che conterr  la form
   var tipo_dati=new Array('input','textarea','select');

   for(j=0; j < tipo_dati.length; j++)
   {
	  
     var x=document.getElementsByTagName(tipo_dati[j]);

     for(i=0; i <x.length; i++)
        {
          var campo=x.item(i).name;

          //controllo che il nome del campo sia impostato
          if(campo!='')
            {
              //istruzione uguale per entrambi i browser
              var valore=x.item(i).value;
              
              if(navigator.appName.indexOf("Netscape")>=0)
               {

                 var test=x.item(i).attributes;
              
                 for(var a=test.length-1; a>=0; a--) 
                  {
        	       switch(test[a].name)
        	        {
        	          case "obb":  
        	            var obbligatorio=test[a].value; 
        	          break;
        	          
        	          case "error":
        	             var msg=test[a].value;       	             
        	          break;
        	          
        	          case "dato":
        	             var tipo_dato=test[a].value;
        	          break;
        	          
        	          case "regex":
        	             var regex=test[a].value;
        	          break;

        	          case "min_val":
        	             var min_val=test[a].value;
        	          break;
					  
        	          case "max_val":
        	             var max_val=test[a].value;
        	          break;

        	        }
        	       
                  }
                 
               }
              else
               {
                var obbligatorio=x.item(i).obb;
                var msg=x.item(i).error;
                var tipo_dato=x.item(i).dato;
                var regex=x.item(i).regex;
				var min_val=x.item(i).min_val;
				var max_val=x.item(i).max_val;
               }
              
              if(obbligatorio==1 && valore=='')
               {
                alert(msg);	
				x.item(i).focus();
                error=1;
				}
			   
              if(obbligatorio==1 && min_val!='')
               {
				var cont = valore.replace("È ","");
				cont = cont.replace(".","");
				cont = cont.replace(",",".");
				cont = parseInt(cont);
				if (cont<min_val) {
					alert("Il valore non pu  essere inferiore a È " + min_val);	
					x.item(i).select();
					error=1;
					}
               }

              if(obbligatorio==1 && max_val!='')
               {
				var cont = valore.replace("È ","");
				cont = cont.replace(".","");
				cont = cont.replace(",",".");
				cont = parseInt(cont);
				if (cont>max_val) {
					alert("Il valore non pu  essere superiore a È " + max_val);	
					x.item(i).select();
					error=1;
					}
               }
			   
              if(obbligatorio==1 && valore!='' && regex=='')
               {

                if(tipo_dato=='int' && isNaN(valore))
                  {
                   alert(msg);
                   error=1;
				   }
                if(tipo_dato=='char' && !isNaN(valore))
                  {
                   alert(msg);
                   error=1;
				   }
               }


              if(obbligatorio==1 && valore!='' && regex!='')
               {
                 //prendo il pattern passato dal mio elemento HTML
                 var pattern=new RegExp(regex);
                 var result=valore.search(pattern);

                 if(result!=0)
                  {
                   alert(msg);
                   error=1;
				   }
               }

           }//campo

       }//for i

     }//for j

  }
 else
  {
    alert('Attenzione! Il browser in uso non supporta le funzioni per inviare il form');
   }

  //Se tutto   andato a buon fine invia la form
  if(error==0)
   {	
     form.action = Action;
     form.submit();
   }

}//END
