
/* - checkForm.js - */
 
	var temp, numeratore, denominatore, rata, ris, str
	var TassoMin = 0.0001;
	var TassoMax = 15.0;
	var DurataMax = 7;
	var DurataMin = 0;
	var arrayDurate = [12,18,24,30,36,42,48,60];
	var PercAss = 1.029;
//----------------------------------------------------------
//-----------------------------------------------------
function checkFormMaschera()
{
 //alert(document.maschera.nome.value)
  if (document.maschera.nome.value =="" || document.maschera.cognome.value == ""
    || document.maschera.indirizzo.value =="" || document.maschera.localita.value ==""
    || document.maschera.provincia.value =="" || document.maschera.cap.value ==""
    || document.maschera.giornonascita.value =="" || document.maschera.mesenascita.value =="" || document.maschera.annonascita.value ==""
    || (!document.maschera.nazione[0].checked && !document.maschera.nazione[1].checked)
    || document.maschera.telefono.value =="" || document.maschera.email.value ==""  
    || document.maschera.prodotto.value =="" 
    || (!document.maschera.clientesilf[0].checked && !document.maschera.clientesilf[1].checked)
    || (document.maschera.clientesilf[0].checked && document.maschera.quale.value =="")
   
)
  {
   alert('Tutti i campi sono obbligatori!');
   return false;
  }
 

  if (!document.maschera.bymail[0].checked && !document.maschera.byemail[0].checked)
  {
   alert('Attenzione non hai rilasciato il consenso al trattamento dei tuoi dati!');
   return false;
  } 
return true;

}


//--------------------------------------------------------
function checkContactForm()
{
//  alert(document.maschera.ragionesociale.value)
//  alert(document.maschera.indirizzo.value) 
//  alert(document.maschera.localita.value)
//  alert(document.maschera.provincia.value)
//  alert(document.maschera.cap.value)
//  alert(document.maschera.telefono.value) 
//  alert(document.maschera.fax.value) 
//  alert(document.maschera.email.value)
//  alert(document.maschera.merceologica.value) 
//  alert(document.maschera.attivita.value)
//  alert(document.maschera.clientesilf[0].checked)
//  alert(document.maschera.clientesilf[1].checked)
//  alert(document.maschera.quale.value)
//  alert(document.maschera.testoemail.value)   

  if (document.maschera.ragionesociale.value == "" 
    || document.maschera.indirizzo.value =="" || document.maschera.localita.value ==""
    || document.maschera.provincia.value =="" || document.maschera.cap.value ==""
    || document.maschera.telefono.value =="" || document.maschera.fax.value =="" || document.maschera.email.value ==""
    || document.maschera.merceologica.value == "" || document.maschera.attivita.value == "" || document.maschera.clienti.value == ""
    || (!document.maschera.clientesilf[0].checked && !document.maschera.clientesilf[1].checked)
    || (document.maschera.clientesilf[0].checked && document.maschera.quale.value == "") 
)
  {
   alert('Tutti i campi sono obbligatori!');
   return false;
  }
 

  if (!document.maschera.bymail[0].checked && !document.maschera.byemail[0].checked)
  {
   alert('Attenzione non hai rilasciato il consenso al trattamento dei tuoi dati!');
   return false;
  } 
return true;

}

//-----------------------------------------

//--------------------------------------------------------
function test()
{
  alert("test ok");
  return 1;
}

//--------------------------------------------------------------------------------------------------------
// funzione che verifica che nei campi  vengano immessi solo caratteri numerici
function numerical_press(wich,campo)
{
  var arrayName = ["tasso","importo","rata"]
  var sString = wich.value.toString()
  var len = sString.length
  var bName = false;
  var contatore = 0;
    for (i=0;i<len;i++)
    {
       caracter = sString.charCodeAt((i))

      if (caracter  < 48 || caracter >57)
      {
         for (nn in arrayName){(arrayName[nn]==wich.name)?bName=true:a=1}

         if (String.fromCharCode(caracter) == "."  &&  bName==true )//&& contatore<1)
          {
            //tutto ok incremento contatore dei punti
            contatore++;
          }         
          else
           {
              alert(" Carattere '" + String.fromCharCode(caracter) +"' non valido nel campo:"+campo);
              wich.value= wich.value.toString().replace(String.fromCharCode(caracter),'');
              return false;
           }
      }
        
    }

       if (contatore>1) 
        {
           alert("Attenzione ci sono "+contatore + " '.' nel campo:"+campo);
           return false;
        }

  
        //wich.focus();
        return true;
}
//----------------------------------------------------------------------------------------------------------
function verifica_campi(tipo)
{
  capitale = document.maschera.importo.value
  tasso    = document.maschera.tasso.value
  rate   = document.maschera.rate.value
  rata   = document.maschera.rata.value

 if (tipo == "rata")
  if (capitale =="" || tasso == "" || rate =="") return false;
 
 if (tipo == "durata")
  if (capitale =="" || tasso == "" || rata =="") return false; 

  return true;

}

//----------------------------------------------------------------------------------------------------------

//----------------------------------------------------------------------------------------------------------
function verifica_validita_campi(tipo)
{

  capitale = document.maschera.importo
  tasso    = document.maschera.tasso
  rate   = document.maschera.rate
  rata   = document.maschera.rata

 if (tipo == "rata")
  {
   if (!numerical_press(capitale,"Importo") ||  !numerical_press(tasso,"Tasso") || !numerical_press(rate,"Rate")) return false;
  }
 
 if (tipo == "durata")
   if (!numerical_press(capitale,"Importo") ||  !numerical_press(tasso,"Tasso") || !numerical_press(rata,"Rata")) return false;

  return true;

}

//----------------------------------------------------------------------------------------------------------






   function calc_rata ( capitale, tasso, rate )
    {
        document.maschera.rata.value="";

        if (verifica_campi("rata") == false)
        {
           alert("Inserire tutti i campi necessari")
           return;
        }     
  
        if (verifica_validita_campi("rata") == false)
        {
           //alert("Inserire tutti i campi necessari")
           return;
        }     


       k = capitale; // controllo dai input
       int_val = parseInt(k);
       str = new String (int_val);
       if (( str == "NaN") || (int_val <= 0))
          {
          alert( "Importo finanziato non valido")
          document.maschera.importo.focus()
          return;
          }
       k = tasso; // controllo dai input
       int_val = parseInt(k);
       str = new String (int_val);
       if ( str == "NaN") 
          { 
          alert( "Tasso non valido")
          document.maschera.tasso.focus()
          return;
          }
     
   	  if ( int_val > TassoMax  || k > TassoMax) 
          {
          alert( "Il tasso nominale deve essere inferiore al "+TassoMax+" %")
          document.maschera.tasso.focus()
          return;
          }

        if ( int_val < TassoMin ) 
          {
          alert( "Il tasso nominale deve essere superiore al "+TassoMin+" %")
          document.maschera.tasso.focus()
          return;
          }
    	  

         
         k = rate; // controllo dai input
        
           
         if (!numerical_press(document.maschera.rate,"Numero Rate"))
          {
            //alert( "Numero delle rate non valido")
            document.maschera.rate.focus()
            return;
          }


         
           
          int_val = parseInt(k);
  
         

        str = new String (int_val);

       if (( str == "NaN") || (int_val <= 0)  || int_val > 60)
          {
            alert( "Numero delle rate non valido o durata del contratto maggiore di 60 mesi")
            document.maschera.rate.focus()
            return;
          }

     
       tasso=tasso/12;
       tasso=tasso/100
       temp=Math.pow(1+tasso, rate);
       numeratore=capitale*tasso*temp;
       denominatore=temp-1;
       ris=numeratore/denominatore;
       ris=ris*100
       arr=Math.round(ris)
   
       str = new String (arr);
       
       if ( str == "NaN") 
          { 
          alert( "Tasso non valido. Usare punto per separare decimali")
          document.maschera.tasso.focus()
          return;
          }

       
       document.maschera.rata.value=arr/100
       
       
       
       
  
      }
 function logar(val)
{
     return (Math.log(val)/Math.log(10))
}
//-----------------------------------------------------------------------------------------------------
function calc_durata ( capitale, tasso, rata )
{
     

       document.maschera.rate.value=""
      if (verifica_campi("durata") == false)
        {
           alert("Inserire tutti i campi necessari")
           return;
        }    
     
     if (verifica_validita_campi("durata") == false)
       {
          //alert("Completare i campi in maniera corretta")
          return;
       }    
     



         C = capitale
	 int_val = parseInt(C);
	 str = new String(int_val);
	 if ((str == "NaN")||(int_val <= 0))
  	 {
		alert("Importo finanziato non valido");
		return;
	 }
           
 	int_val = parseInt(tasso);
	str = new String(int_val);
	if ((str == "NaN")||(int_val <= 0))
  	 {
		alert("Tasso non valido");
		return;
	 }

     i = tasso/1200;
     if (i==0)
          i = TassoMin;
     R = rata;
	 if (R <= 0)
  	 {
		alert("Importo della rata non valido");
		return;
	 }
     numeratore = logar(R)-logar(R-C*i);
     denominatore = logar(1+i);
      
    
    if (new String(numeratore) == "NaN")
    {
         alert("errore nel calcolo della durata del contratto, verificare valore della rata");
         return;
     }

     if (denominatore == 0)
     {
         alert("errore nel calcolo della durata del contratto");
         return;
     }
	 str = new String(numeratore/denominatore);
	 if (str.indexOf(".") != -1)
	    n = parseInt(numeratore/denominatore)+1;
	 else
	    n = parseInt(numeratore/denominatore);

         //alert("n durata contratto="+n)

 	 if (n > arrayDurate[DurataMax])
 	 {
 		 alert("La durata del contratto ricavata è superiore a" + arrayDurate[DurataMax]+" mesi");
		 return;
	 }
	 else if (n < arrayDurate[DurataMin])
	 {
		 alert("La durata del contratto ricavata è inferiore a "+arrayDurate[DurataMin]+" mesi");
		 return;
	 }
     
  document.maschera.rate.value=n
  str = new String (n);

       if ( str == "NaN") 
          { 
          alert( "Tasso non valido. Usare punto per separare decimali")
          document.maschera.tasso.focus()
          return;
          }

  calc_rata(document.maschera.importo.value, document.maschera.tasso.value, document.maschera.rate.value)
  return;
}
function pulisci(importo, rate, tasso, rata)
   {
   document.maschera.rate.value="";
   document.maschera.importo.value="";
   document.maschera.tasso.value="";
   document.maschera.rata.value="";
   return;
   }  
// disabilito il tasto destro del  mouse   
function right(e)
 {

if (navigator.appName == 'Netscape' && 

(e.which == 3 || e.which == 2))

return false;

else if (navigator.appName == 'Microsoft Internet Explorer' && 

(event.button == 2 || event.button == 3)) {

alert("Spiacenti, il tasto destro del mouse e' disabilitato");

return false;

}

return true;

}

document.onmousedown=right;

if (document.layers) window.captureEvents(Event.MOUSEDOWN);

window.onmousedown=right;



