var sub_f_go = true;
var cont_camp = false;

String.prototype.trim = function(){

	return this.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
	
}

function empty( mixed_var ) {

	return ( mixed_var === "" || mixed_var === 0   || mixed_var === "0" || mixed_var === null  || mixed_var === false  ||  mixed_var === undefined || mixed_var.length === 0  );
	
}

function azienda( id ) {
  
    document.getElementById(id).innerHTML = "";

    campi = document.createElement('div');

    campi.id = 'mine'; //assegna al div un nome

    document.getElementById(id).appendChild(campi);

    campi.innerHTML = "<fieldset><legend>ANAGRAFICA CLIENTE:</legend><table cellpadding='3' cellspacing='0' style='width:100%; border:0px;'><tr align='left'><td align='left' style='width:50%; font family:Verdana; font-size:11px; color:#000; font-weight:bold;'><label for='Cognome'>Cognome Cliente</label><input type='text' name='cognome' value='' size='20' class='campi'></td><td align='left' style='width:50%; font family:Verdana; font-size:11px; color:#000; font-weight:bold;'><label for='Nome'>Nome Cliente</label><input type='text' name='nome' value='' size='20' class='campi'></td></tr><tr align='left'><td>&nbsp;</td></tr><tr align='left'><td align='left' style='width:50%; font family:Verdana; font-size:11px; color:#000; font-weight:bold;'><label for='CF'>Codice Fiscale <font color='red'>*</font></label><input type='text' name='cod_fisc' value='' size='20' class='campi'></td><td align='left' style='width:50%; font family:Verdana; font-size:11px; color:#000; font-weight:bold;'><label for='PI'>Partita Iva <font color='red'>*</font></label><input type='text' name='piva' value='' size='20' class='campi'></td></tr><tr align='left'><td>&nbsp;</td></tr><tr align='left'><td align='left' style='width:50%; font family:Verdana; font-size:11px; color:#000; font-weight:bold;'><label for='RS'>Ragione Sociale <font color='red'>*</font></label><input type='text' name='rag_soc' value='' size='20' class='campi'></td><td align='left' style='width:50%; font family:Verdana; font-size:11px; color:#000; font-weight:bold;'>&nbsp;</td></tr></table></fieldset><br /><br /><fieldset><legend>REFERENTE AZIENDA:</legend><table cellpadding='3' cellspacing='0' style='width:100%; border:0px;'><tr align='left'><td align='left' style='width:50%; font family:Verdana; font-size:11px; color:#000; font-weight:bold;'><label for='C_Ref'>Cognome Referente <font color='red'>*</font></label><input type='text' name='cognome_refer' value='' size='20' class='campi'></td><td align='left' style='width:50%; font family:Verdana; font-size:11px; color:#000; font-weight:bold;'><label for='N_Ref'>Nome Referente <font color='red'>*</font></label><input type='text' name='nome_refer' value='' size='20' class='campi'></td></tr></table></fieldset>";

}

function privato( id ) {

    document.getElementById(id).innerHTML = "";

    campi = document.createElement('div');

    campi.id = 'mine'; //assegna al div un nome

    document.getElementById(id).appendChild(campi);

    campi.innerHTML = "<fieldset><legend>ANAGRAFICA CLIENTE:</legend><table cellpadding='3' cellspacing='0' style='width:100%; border:0px;'><tr align='left'><td align='left' style='width:50%; font family:Verdana; font-size:11px; color:#000; font-weight:bold;'><label for='C_Cliente'>Cognome Cliente <font color='red'>*</font></label><input type='text' name='cognome' value='' size='20' class='campi'></td><td align='left' style='width:50%; font family:Verdana; font-size:11px; color:#000; font-weight:bold;'><label for='N_Cliente'>Nome Cliente <font color='red'>*</font></label><input type='text' name='nome' value='' size='20' class='campi'></td></tr><tr align='left'><td>&nbsp;</td></tr><tr align='left'><td align='left' style='width:370px; font family:Verdana; font-size:11px; color:#000; font-weight:bold;'><label for='CF_Cliente'>Codice Fiscale <font color='red'>*</font></label><input type='text' name='cod_fisc' value='' size='20' class='campi'></td><td align='left' style='width:50%; font family:Verdana; font-size:11px; color:#000; font-weight:bold;'><label for='PI_Cliente'>Partita Iva</label><input type='text' name='piva' value='' size='20' class='campi'></td></tr></table></fieldset>";

}

function Cont_User() {

    var rb_scelto = false;

		if ( document.frmPreventivi.r_type_p.checked ) {

      Cont_Tipo( document.frmPreventivi.r_type_p.value );

        document.getElementById("not_privato").innerHTML = "";
        document.getElementById("not_azienda").innerHTML = "";

			rb_scelto = true;
			
		}
			
		for ( i = 0; i < document.frmPreventivi.r_type_p.length; i++ ) {
		
			if ( document.frmPreventivi.r_type_p[i].checked ) {
	
        Cont_Tipo( document.frmPreventivi.r_type_p[i].value );

        document.getElementById("not_privato").innerHTML = "";
        document.getElementById("not_azienda").innerHTML = "";

        rb_scelto = true;
				
			}
			
		}
			
      if ( ! ( rb_scelto ) ) {
      
        alert("Selezionare la Tipologia di Utente che richiede il Preventivo");
        document.getElementById("not_privato").innerHTML = "<span style='font-size:11px; color:red;'>&#9668;</span>";
        document.getElementById("not_azienda").innerHTML = "<span style='font-size:11px; color:red;'>&#9668;</span>";
        return false;
      
      }

  if ( cont_camp === true ) {
  
    return true;
  
  }

}

function Cont_Tipo( myradio ) {

  switch ( myradio ) {
  
    case 'Privato':

      var cogn = document.frmPreventivi.cognome.value.trim();
      var nom = document.frmPreventivi.nome.value.trim();
      var cdf = document.frmPreventivi.cod_fisc.value.trim();

      if ( empty( cogn ) ) {

        document.frmPreventivi.cognome.className = "campi_red";
        document.frmPreventivi.cognome.focus();
        cont_camp = false;
       
      } else if ( cogn.length < 2 ) {

        document.frmPreventivi.cognome.className = "campi_red";
        document.frmPreventivi.cognome.focus();
        cont_camp = false;
       
      } else {
      
        document.frmPreventivi.cognome.className = "campi_green";
        cont_camp = true;
      
      }
      
      if ( empty( nom ) ) {

        document.frmPreventivi.nome.className = "campi_red";
        document.frmPreventivi.nome.focus();
        cont_camp = false;
       
      } else if ( nom.length < 3 ) {

        document.frmPreventivi.nome.className = "campi_red";
        document.frmPreventivi.nome.focus();
        cont_camp = false;
       
      } else {
      
        document.frmPreventivi.nome.className = "campi_green";
        cont_camp = true;
      
      }
      
      if ( empty( cdf ) ) {
      
        document.frmPreventivi.cod_fisc.className = "campi_red";
        document.frmPreventivi.cod_fisc.focus();
        cont_camp = false;
        
      } else if ( cdf.length != 16 && cdf.length > 0 ) {
      
        document.frmPreventivi.cod_fisc.className = "campi_red";
        document.frmPreventivi.cod_fisc.focus();
        cont_camp = false;
        
      } else if ( cdf.match(/[^a-zA-Z0-9]/) ) {
      
        document.frmPreventivi.cod_fisc.className = "campi_red";
        document.frmPreventivi.cod_fisc.focus();
        cont_camp = false;
     
      } else {
      
        document.frmPreventivi.cod_fisc.className = "campi_green";
        cont_camp = true;
      
      }

    break;
    
    case 'Azienda':

      var rags = document.frmPreventivi.rag_soc.value.trim();      
      var merc = rags.replace( new RegExp( /['|\s|,|\/|\.|\-]/g),'' );
      var cdf = document.frmPreventivi.cod_fisc.value.trim();
      var pva = document.frmPreventivi.piva.value.trim();
      var cref = document.frmPreventivi.cognome_refer.value.trim();
      var nref = document.frmPreventivi.nome_refer.value.trim();
            
      if ( empty( rags ) ) {
      
           document.frmPreventivi.rag_soc.className = "campi_red";
           document.frmPreventivi.rag_soc.focus();
           cont_camp = false;
           
       } else if ( rags.length < 2 && rags.length > 0 ) {
      
           document.frmPreventivi.rag_soc.className = "campi_red";
           document.frmPreventivi.rag_soc.focus();
           cont_camp = false;
           
       } else if ( rags.length > 50 ) {
      
           document.frmPreventivi.rag_soc.className = "campi_red";
           document.frmPreventivi.rag_soc.focus();
           cont_camp = false;
     
      } else if (  merc.match(/\W/) || merc.match(/\_/) ) {
      
           document.frmPreventivi.rag_soc.className = "campi_red";
           document.frmPreventivi.rag_soc.focus();
           cont_camp = false;
     
      } else {
      
        document.frmPreventivi.rag_soc.className = "campi_green";
        cont_camp = true;
      
      }
      
      if ( empty( cdf ) ) {
      
        document.frmPreventivi.cod_fisc.className = "campi_red";
        document.frmPreventivi.cod_fisc.focus();
        cont_camp = false;
        
      } else if ( cdf.length != 16 && cdf.length > 0 ) {
      
        document.frmPreventivi.cod_fisc.className = "campi_red";
        document.frmPreventivi.cod_fisc.focus();
        cont_camp = false;
        
      } else if ( cdf.match(/[^a-zA-Z0-9]/) ) {
      
        document.frmPreventivi.cod_fisc.className = "campi_red";
        document.frmPreventivi.cod_fisc.focus();
        cont_camp = false;
     
      } else {
      
        document.frmPreventivi.cod_fisc.className = "campi_green";
        cont_camp = true;
      
      }
      
      if ( empty( pva ) ) {
      
           document.frmPreventivi.piva.className = "campi_red";
           document.frmPreventivi.piva.focus();
           cont_camp = false;
           
       } else if ( pva.length < 2 && pva.length > 0 ) {
      
           document.frmPreventivi.piva.className = "campi_red";
           document.frmPreventivi.piva.focus();
           cont_camp = false;
           
       } else if ( pva.length > 11 ) {
      
           document.frmPreventivi.piva.className = "campi_red";
           document.frmPreventivi.piva.focus();
           cont_camp = false;
     
      } else if ( pva.match(/[^0-9]/) ) {
      
           document.frmPreventivi.piva.className = "campi_red";
           document.frmPreventivi.piva.focus();
           cont_camp = false;
     
      } else {
      
        document.frmPreventivi.piva.className = "campi_green";
        cont_camp = true;
      
      }
      
      if ( empty( cref ) ) {

        document.frmPreventivi.cognome_refer.className = "campi_red";
        document.frmPreventivi.cognome_refer.focus();
        cont_camp = false;
       
      } else if ( cref.length < 2 ) {

        document.frmPreventivi.cognome_refer.className = "campi_red";
        document.frmPreventivi.cognome_refer.focus();
        cont_camp = false;
       
      } else {
      
        document.frmPreventivi.cognome_refer.className = "campi_green";
        cont_camp = true;
      
      }
      
      if ( empty( nref ) ) {

        document.frmPreventivi.nome_refer.className = "campi_red";
        document.frmPreventivi.nome_refer.focus();
        cont_camp = false;
       
      } else if ( nref.length < 3 ) {

        document.frmPreventivi.nome_refer.className = "campi_red";
        document.frmPreventivi.nome_refer.focus();
        cont_camp = false;
       
      } else {
      
        document.frmPreventivi.nome_refer.className = "campi_green";
        cont_camp = true;
      
      }
      
    break;
   
   }

    return ( cont_camp );

}

function Cont_Tel( myphone ) {

  if ( empty( myphone ) ) {
  
    document.frmPreventivi.telefono.className = "campi_red";
    document.frmPreventivi.telefono.focus();
    return false;
       
  }
  
  if ( myphone.length < 6 && myphone.length > 0 ) {
  
    document.frmPreventivi.telefono.className = "campi_red";
    document.frmPreventivi.telefono.focus();
    return false;
    
  }
  
  if ( myphone.match(/[^0-9]/) ) {
  
    document.frmPreventivi.telefono.className = "campi_red";
    document.frmPreventivi.telefono.focus();
    return false;
     
  }
  
  document.frmPreventivi.telefono.className = "campi_green";
  return true;

}

function Cont_Mail ( mymail ) {

  if ( empty ( mymail ) ) {
  
    document.frmPreventivi.mail.className = "campi_red";
    document.frmPreventivi.mail.focus();
    return false;
		
  }
  
  if ( !mymail.match (/^[a-z0-9_\-\.]+@[a-z0-9]+[a-z0-9_\-\.]*\.[a-z]{2,}$/ ) || mymail.length > 256 ) {
  
    document.frmPreventivi.mail.className = "campi_red";
    document.frmPreventivi.mail.focus();
    return false;
  
  }
  
  document.frmPreventivi.mail.className = "campi_green";
	return true;

}

function Cont_City ( mycity ) {

  var mcasa = mycity.replace( new RegExp(/[\s|\']/g),'' );

  if ( empty( mycity ) ) {
  
    document.frmPreventivi.city.className = "campi_red";
    document.frmPreventivi.city.focus();
    return false;
       
  }
   
  if ( mycity.length < 2 && mycity.length > 0 ) {
  
    document.frmPreventivi.city.className = "campi_red";
    document.frmPreventivi.city.focus();
    return false;
       
  }
   
  if ( mycity.length > 40 ) {
   
    document.frmPreventivi.city.className = "campi_red";
    document.frmPreventivi.city.focus();
    return false;
       
  }

  if ( mcasa.match(/[^a-zA-Z]/) ) {
  
    document.frmPreventivi.city.className = "campi_red";
    document.frmPreventivi.city.focus();
    return false;
       
  }
  
  document.frmPreventivi.city.className = "campi_green";
  return true;

}

function Cont_Prov ( myprov ) {

  if ( empty( myprov ) ) {
  
    document.frmPreventivi.provincia.className = "campi_red";
    document.frmPreventivi.provincia.focus();
    return false;
       
  }
  
  document.frmPreventivi.provincia.className = "campi_green";
  return true;

}

function Cont_Cap ( mycap ) {

  if ( empty( mycap ) ) {
  
    document.frmPreventivi.cap.className = "campi_red";
    document.frmPreventivi.cap.focus();
    return false;
    
  }
  
  if ( mycap.length != 5 && mycap.length > 0 ) {
  
    document.frmPreventivi.cap.className = "campi_red";
    document.frmPreventivi.cap.focus();
    return false;
       
  }
   
  if ( mycap.match(/[^0-9]/) ) { 
  
    document.frmPreventivi.cap.className = "campi_red";
    document.frmPreventivi.cap.focus();
    return false;
     
  }
  
  document.frmPreventivi.cap.className = "campi_green";
  return true;

}

function Cont_Prod( myprod ) {

  if ( empty( myprod ) ) {
  
    document.frmPreventivi.prodotti_prev.className = "campi_red";
    document.frmPreventivi.prodotti_prev.focus();
    return false;
    
  }

  document.frmPreventivi.prodotti_prev.className = "campi_green";
  return true;

}

function Cont_Desc( mydesc ) {

  if ( empty( mydesc ) ) {
  
    document.frmPreventivi.desc.className = "campi_red";
    document.frmPreventivi.desc.focus();
    return false;
    
  }
  
  if ( mydesc.length < 5 ) {

    document.frmPreventivi.desc.className = "campi_red";
    document.frmPreventivi.desc.focus();
    return false;
       
  }

  document.frmPreventivi.desc.className = "campi_green";
  return true;

}

function Cont_Privacy () {

  if ( !document.frmPreventivi.privacy.checked ) {
  
    document.getElementById("not_privacy").innerHTML = "<span style='font-size:11px; color:red;'>&#9668;</span>";
    document.frmPreventivi.privacy.focus();
    return false;
  
  }
  
  document.getElementById("not_privacy").innerHTML = "";
  return true;

}

function ContAbbForm() {

		if( sub_f_go ) {
    
      sub_f_go = false;
      
    } else {
		
			return false;
			
		}
		
		var check = true;
		
		if( !( Cont_User() ) ) {

      check = false;
      
    }
    
		if( !( Cont_Tel( document.frmPreventivi.telefono.value.trim() ) ) ) {

      check = false;
      
    }
    
		if( !( Cont_Mail( document.frmPreventivi.mail.value.trim() ) ) ) {

      check = false;
      
    }
    
		if( !( Cont_City( document.frmPreventivi.city.value.trim() ) ) ) {

      check = false;
      
    }
    
		if( !( Cont_Prov( document.frmPreventivi.provincia.value.trim() ) ) ) {

      check = false;
      
    }
    
		if( !( Cont_Cap( document.frmPreventivi.cap.value.trim() ) ) ) {

      check = false;
      
    }
    
		if( !( Cont_Prod( document.frmPreventivi.prodotti_prev.value.trim() ) ) ) {

      check = false;
      
    }
    
		if( !( Cont_Desc( document.frmPreventivi.desc.value.trim() ) ) ) {

      check = false;
      
    }
    
    if( !( Cont_Privacy() ) ) {
    
      check = false;
    
    }
		
    if ( check === true ) {
    
			document.frmPreventivi.submit();
			
			check = false;
			
		}

		if( !( check ) ) {
      
      sub_f_go = true;
      
    }
		
		return( check );

}
