  function validate_login(theForm){
    if (theForm.email.value.length == 0){
        theForm.email.focus();
        theForm.email.parentNode.style.border = '1px solid #00F';
        return false;
    }

    if (theForm.password.value.length == 0){
        theForm.password.focus();
        theForm.password.parentNode.style.border = '1px solid #00F';
        return false;
    }
    return true;  
  }  
  function validate_registration(theForm){

       var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
       var emailaddressVal = theForm.email.value;
       if(emailaddressVal == '') {
            $("#reg_mes").html('Devi specificare l\'indirizzo email');
            theForm.email.style.border = '1px solid #00F';
            theForm.email.focus();
            return false;
       }
       else if(!emailReg.test(emailaddressVal)) {
            $("#reg_mes").html('Inserisci un indirizzo email valido.');
            theForm.email.style.border = '1px solid #00F';
            theForm.email.focus();
            return false;
       }
       if (theForm.password.value.length == 0){
        theForm.password.focus();
        $("#reg_mes").html('Devi specificare la password.');
        theForm.password.style.border = '1px solid #00F';
        return false;
       } 
       
       if ($("input[name='r1']:checked").val() != '1'){
        $("#reg_mes").html('Per poter usufruire del nostro servizio occorre dare il consenso al trattamento dei dati personali.');
        return false;
       }
       
     return true;
  }  
  function validate_registration_update(theForm){

       var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
       var emailaddressVal = theForm.email.value;
       if(emailaddressVal == '') {
            $("#reg_mes").html('Devi specificare l\'indirizzo email');
            theForm.email.style.border = '1px solid #00F';
            theForm.email.focus();
            return false;
       }
       else if(!emailReg.test(emailaddressVal)) {
            $("#reg_mes").html('Inserisci un indirizzo email valido.');
            theForm.email.style.border = '1px solid #00F';
            theForm.email.focus();
            return false;
       }
       if (theForm.password.value.length == 0){
        theForm.password.focus();
        $("#reg_mes").html('Devi specificare la password.');
        theForm.password.style.border = '1px solid #00F';
        return false;
       } 
       
     return true;
  } 
  
  function search_category(){

  $('#search_categoria').val('');
  if ($("input[@name='r0']:checked").val() == 'categoria'){
     $("#sub-search-bar-text-1").hide(); 
     $("#sub-search-table-1").empty();
     
  var search = $("#what_to_search_1").val();
  if (search.length > 0){
    $.post('/moduli_new/ajax/categoria_json.php',{wts:search}, function(data) {
     var per = eval('('+data.replace(/[\n\r\t]/g,' ')+')');
     $("#sub-search-table-1").empty();
     for (var i=0; i<per.categories.length;i++){
       $("#sub-search-table-1").append('<tr><td onclick="set_categoria(\''+per.categories[i].scat.replace("'","\\'")+'\');">'+per.categories[i].scat+'</td></tr>');
     }
     return false;
   });
   $("#sub-search-bar-text-1").show();
  }
  }
  }
  function set_categoria(cat){
    $("#sub-search-bar-text-1").hide();
    $("#search_categoria").val(cat);
    $("#what_to_search_1").val(cat);
  }
  function search_dove(){

  $('#search_comune').val('');
  $('#search_prov').val('');

     $("#sub-search-bar-text-2").hide(); 
     $("#sub-search-table-2").empty();
     
  var search = $("#what_to_search_2").val();
  if (search.length > 0){
    $.post('/moduli_new/ajax/dove_json.php',{wts:search}, function(data) {
     var per = eval('('+data.replace(/[\n\r\t]/g,' ')+')');
     $("#sub-search-table-2").empty();
     for (var i=0; i<per.dove.length;i++){
       if (per.dove[i].type == '2'){
          $("#sub-search-table-2").append('<tr><td onclick="set_dove(this,\''+per.dove[i].prov.replace("'","\\'")+'\',\''+per.dove[i].comune.replace("'","\\'")+'\',\''+per.dove[i].capoluogo.replace("'","\\'")+'\');">'+per.dove[i].comune+' ('+per.dove[i].prov+')</td></tr>'); 
       }
       if (per.dove[i].type == '1'){
          $("#sub-search-table-2").append('<tr><td onclick="set_dove(this,\''+per.dove[i].prov.replace("'","\\'")+'\',\''+per.dove[i].comune.replace("'","\\'")+'\',\''+per.dove[i].capoluogo.replace("'","\\'")+'\');">'+per.dove[i].capoluogo+' ('+per.dove[i].prov+')</td></tr>'); 
       } 
       if (per.dove[i].type == '0'){
          $("#sub-search-table-2").append('<tr><td onclick="set_dove(this,\''+per.dove[i].prov.replace("'","\\'")+'\',\'0\',\''+per.dove[i].capoluogo.replace("'","\\'")+'\');">Provincia di '+per.dove[i].capoluogo+' ('+per.dove[i].prov+')</td></tr>'); 
       }
    }
     return false;
   });
   $("#sub-search-bar-text-2").show();
  }

  }
  function set_dove(obj, prov, comune, capoluogo){
    $("#sub-search-bar-text-2").hide();
    $("#search_prov").val(prov);
    if (comune == '0'){
      $("#search_comune").val('');  
    }
    else {
      $("#search_comune").val(comune);  
    }
    var search = $(obj).html();
    $("#what_to_search_2").val(search);
    $("#search_capoluogo").val(capoluogo);
  }
  function search(){
     var url = '';
     var tmp = '';
     if ($("input[@name='r0']:checked").val() == 'nome' ){
         if ($("#what_to_search_1").val() == ''  || $("#what_to_search_1").val() == 'Cosa vuoi trovare?'){
             $("#search_error").html('Devi specificare il Nome o la Ragione Sociale di quello che cerchi oppure la categoria');
             $("#what_to_search_1").focus();
             return false;
         }
         else {
            tmp = $("#what_to_search_1").val(); 
            tmp.replace("?","");
            url += '/nome_'+ tmp;
         }
     } 
     
     if ($("#search_prov").val() == ''){
         $("#search_error").html('Devi specificare la provincia');
         $("#what_to_search_2").focus();
         return false;
     }
     else {
         tmp = $("#search_capoluogo").val(); 
         url += '/prov_'+ tmp;
     }
     
     if ($("#search_comune").val() != ''){
         tmp = $("#search_comune").val(); 
         url += '/comune_'+ tmp;
     }
     
     if ($("input[@name='r0']:checked").val() == 'categoria'){
         if ($("#search_categoria").val() == ''){
             $("#search_error").html('Devi specificare il Nome o la Ragione Sociale di quello che cerchi oppure la categoria');
             $("#what_to_search_1").focus();
             return false;
         }
         else {
             tmp = $("#search_categoria").val(); 
             url += '/categoria_'+ tmp;
         }
     } 
     document.location.href = url+'/p1';
  }
  function hide_categoria(){
      $("#sub-search-bar-text-1").hide();
  }
  
  function validate_contatti(theForm){
       
       if (theForm.azienda.value.length == 0 || theForm.azienda.value == 'Ragione Sociale'){
        theForm.azienda.focus();
        $("#err_mes").html('Devi compilare tutti i campi del modulo.');
        theForm.azienda.style.border = '1px solid #00F';
        return false;
       } 
       
      if (theForm.nome.value.length == 0 || theForm.nome.value == 'Your Full Name'){
        theForm.nome.focus();
        $("#err_mes").html('Devi compilare tutti i campi del modulo.');
        theForm.nome.style.border = '1px solid #00F';
        return false;
       } 

       var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
       var emailaddressVal = theForm.email.value;
       if(emailaddressVal == '') {
            $("#err_mes").html('Devi compilare tutti i campi del modulo.');
            theForm.email.style.border = '1px solid #00F';
            theForm.email.focus();
            return false;
       }
       else if(!emailReg.test(emailaddressVal)) {
            $("#err_mes").html('Inserisci un indirizzo email valido.');
            theForm.email.style.border = '1px solid #00F';
            theForm.email.focus();
            return false;
       }
       
       if (theForm.comune.value.length == 0 || theForm.comune.value == 'Città'){
        theForm.comune.focus();
        $("#err_mes").html('Devi compilare tutti i campi del modulo.');
        theForm.comune.style.border = '1px solid #00F';
        return false;
       } 
       if (theForm.tellavo.value.length == 0 || theForm.tellavo.value == 'Telephone'){
        theForm.tellavo.focus();
        $("#err_mes").html('Devi compilare tutti i campi del modulo.');
        theForm.tellavo.style.border = '1px solid #00F';
        return false;
       }
       if (theForm.richiesta.value.length == 0  || theForm.richiesta.value == 'Message'){
        theForm.richiesta.focus();
        $("#err_mes").html('Devi compilare tutti i campi del modulo.');
        theForm.richiesta.style.border = '1px solid #00F';
        return false;
       }
       
       if (theForm.recaptcha_response_field.value.length == 0){
        theForm.recaptcha_response_field.focus();
        $("#err_mes").html('Devi compilare tutti i campi del modulo.');
        theForm.recaptcha_response_field.style.border = '1px solid #00F';
        return false;
       }
       
       if (theForm.accedo.checked == false){
        theForm.accedo.focus();
        $("#err_mes").html('Per poter usufruire del nostro servizio occorre dare il consenso al trattamento dei dati personali.');
        theForm.accedo.style.border = '1px solid #00F';
        return false;
       }
       
     return true;
  }  
 function search_provincia(){
  $('#provincia_id').val('');
  $("#sub-provincia").hide(); 
  $("#sub-provincia-table").empty();
  var search = $("#provincia").val();
  if (search.length > 0){
    $.post('moduli_new/ajax/provincia_json.php',{wts:search}, function(data) {
     var per = eval('('+data.replace(/[\n\r\t]/g,' ')+')');
     $("#sub-provincia-table").empty();
     for (var i=0; i<per.dove.length;i++){
          $("#sub-provincia-table").append('<tr><td onclick="set_provincia(\''+per.dove[i].prov.replace("'","\\'")+'\',\''+per.dove[i].capoluogo.replace("'","\\'")+'\');">'+per.dove[i].capoluogo+' ('+per.dove[i].prov.replace("'","\\'")+')</td></tr>'); 
    }
     return false;
   });
   $("#sub-provincia").show();
  }
  }
  function set_provincia(prov, capoluogo){
    $("#sub-provincia").hide();
    $("#provincia_id").val(prov);
    $("#provincia").val(capoluogo);
    $("#comune_id").val('');
    $("#comune").val('Comune');
  }
  
 function search_comune(){
  $('#comune_id').val('');
  $("#sub-comune").hide(); 
  $("#sub-comune-table").empty();
  var prov = $("#provincia_id").val();
  var search = $("#comune").val();
  if (search.length > 0){
    $.post('moduli_new/ajax/comune_json.php',{wts:search, provincia:prov}, function(data) {
     var per = eval('('+data.replace(/[\n\r\t]/g,' ')+')');
     $("#sub-comune-table").empty();
     for (var i=0; i<per.dove.length;i++){
          $("#sub-comune-table").append('<tr><td onclick="set_comune('+per.dove[i].id+',\''+per.dove[i].comune.replace("'","\\'")+'\');">'+per.dove[i].comune+'</td></tr>'); 
    }
     return false;
   });
   $("#sub-comune").show();
  }
  }
  function set_comune(id, comune){
    $("#sub-comune").hide();
    $("#comune_id").val(id);
    $("#comune").val(comune);
  }
  
  function search_categoria(){
  $('#categoria_id').val('');
  $("#sub-categoria").hide(); 
  $("#sub-categoria-table").empty();

  var search = $("#categoria").val();
  if (search.length > 0){
    $.post('/moduli_new/ajax/categoria_json.php',{wts:search}, function(data) {
     var per = eval('('+data.replace(/[\n\r\t]/g,' ')+')');
     $("#sub-categoria-table").empty();
     for (var i=0; i<per.categories.length;i++){
          $("#sub-categoria-table").append('<tr><td onclick="set_categoria_field(\''+per.categories[i].scat.replace("'","\\'")+'\');">'+per.categories[i].scat+'</td></tr>'); 
    }
     return false;
   });
   $("#sub-categoria").show();
  }
  }
  function set_categoria_field(categoria){
    $("#sub-categoria").hide();
    $("#categoria_id").val(categoria);
    $("#categoria").val(categoria);
  }
  
  function validate_creainserzioni(theForm){
       
       if (theForm.provincia_id.value.length == 0){
        theForm.provincia.focus();
        $("#err_mes").html('Devi compilare tutti i campi del modulo.');
        theForm.provincia.style.border = '1px solid #00F';
        return false;
       } 
       
      if (theForm.comune_id.value.length == 0){
        theForm.comune.focus();
        $("#err_mes").html('Devi compilare tutti i campi del modulo.');
        theForm.comune.style.border = '1px solid #00F';
        return false;
       } 
       
       if (theForm.categoria_id.value.length == 0){
        theForm.categoria.focus();
        $("#err_mes").html('Devi compilare tutti i campi del modulo.');
        theForm.categoria.style.border = '1px solid #00F';
        return false;
       } 

      
       if (theForm.ragsoc.value.length == 0 || theForm.ragsoc.value == 'Denominazione'){
        theForm.ragsoc.focus();
        $("#err_mes").html('Devi compilare tutti i campi del modulo.');
        theForm.ragsoc.style.border = '1px solid #00F';
        return false;
       } 
         
     return true;
  } 
  function validate_elimina_attivita(theForm){
      var answer = confirm("Sei sicuro di voler continuare?")
	if (answer){
		return true;
	}
	else{
		return false;
	}
  }
  function city_filter_attivita(){
      $("div[a_city]").hide();
      $("input.a_city[type=checkbox]").each( 
        function() { 
          if($(this).attr("checked") == "checked"){
            var id = $(this).attr('city');  
            if(id == -1){
             $("div[a_city]").show();  
            }
            $("div[a_city='"+id+"']").show();
          }
        } 
      );
  }
  function cat_filter_attivita(cat){
    $("div[a_cat]").hide();  
    var is_city_check = false;
    $("input.a_city[type=checkbox]").each( 
        function() { 
          if($(this).attr("checked") == "checked"){
            is_city_check = true;  
            var id = $(this).attr('city');  
            if(id == -1){
             $("div[a_cat='"+cat+"']").show(); 
            }
            $("div[a_city='"+id+"'][a_cat='"+cat+"']").show();
          }
        } 
    );
    if(!is_city_check) {
       $("input.a_city[type=checkbox][city=-1]").attr('checked', true) ;
       $("div[a_cat='"+cat+"']").show();
       $("input.a_city[type=checkbox][city=-1]").attr('checked', false) ;  
    }   
  }
  function city_filter_consigliati(){
      $("div[b_city]").hide();
      $("input.b_city[type=checkbox]").each( 
        function() { 
          if($(this).attr("checked") == "checked"){
            var id = $(this).attr('city');  
            if(id == -1){
             $("div[b_city]").show();  
            }
            $("div[b_city='"+id+"']").show();
          }
        } 
      );
  }
  function cat_filter_consigliati(cat){
    $("div[b_cat]").hide(); 
    $("input.b_city[type=checkbox]").each( 
        function() { 
          if($(this).attr("checked") == "checked"){
            var id = $(this).attr('city');  
            if(id == -1){
             $("div[b_cat='"+cat+"']").show(); 
            }
            $("div[b_city='"+id+"'][b_cat='"+cat+"']").show();
          }
        } 
    );
  }
  function validate_contatto(theForm){
       
      if (theForm.nome.value.length == 0 || theForm.nome.value == 'Your Full Name'){
        theForm.nome.focus();
        $("#err_mes").html('Devi compilare tutti i campi del modulo.');
        theForm.nome.style.border = '1px solid #00F';
        return false;
       } 

       var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
       var emailaddressVal = theForm.email.value;
       if(emailaddressVal == '') {
            $("#err_mes").html('Devi compilare tutti i campi del modulo.');
            theForm.email.style.border = '1px solid #00F';
            theForm.email.focus();
            return false;
       }
       else if(!emailReg.test(emailaddressVal)) {
            $("#err_mes").html('Inserisci un indirizzo email valido.');
            theForm.email.style.border = '1px solid #00F';
            theForm.email.focus();
            return false;
       }
       
       if (theForm.telefono.value.length == 0 || theForm.telefono.value == 'Telephone'){
        theForm.telefono.focus();
        $("#err_mes").html('Devi compilare tutti i campi del modulo.');
        theForm.telefono.style.border = '1px solid #00F';
        return false;
       }
       
       if (theForm.richiesta.value.length == 0  || theForm.richiesta.value == 'Message'){
        theForm.richiesta.focus();
        $("#err_mes").html('Devi compilare tutti i campi del modulo.');
        theForm.richiesta.style.border = '1px solid #00F';
        return false;
       }
       
       if (theForm.recaptcha_response_field.value.length == 0){
        theForm.recaptcha_response_field.focus();
        $("#err_mes").html('Devi compilare tutti i campi del modulo.');
        theForm.recaptcha_response_field.style.border = '1px solid #00F';
        return false;
       }
       
     return true;
  }  
  function search_prov_settore(){
      document.location.href = '/elenco-aziende-e-attività-preferite-'+$("#settore").val()+'-'+$("#prov").val();
  }
  
  function search_prov(){
      document.location.href = '/preferiti_'+$("#prov").val();
  }
  function add_photo(xid){
    $("#photo_xid").val(xid);
    $("#div_photo_form").dialog('open');  
  }
  function setCookie (name, value, expires, path, domain, secure) {
      document.cookie = name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
  }
  function go_to_creainserzioni(url){
      setCookie("creainserzioni", url);
      document.location.href = '/creainserzioni';
  }
  function badges_open(){
    $("#badges").css('height','auto');  
    $("#badges").css('background','url(../images_new/html-bg.jpg)');
  }
  function badges_close(){
    $("#badges").css('height','90px');  
    $("#badges").css('background','url(../images_new/down-arrow.png) right top no-repeat');
  }
  function right_buttom_div_down(){
      var exdate=new Date();
      exdate.setDate(exdate.getDate() + 360);
      setCookie("rbd", "1", exdate.toUTCString());
      $("#botright").addClass('botright_down');
  }
  function right_buttom_div_up(){
      var exdate=new Date();
      exdate.setDate(exdate.getDate() + 360);
      setCookie("rbd", "0", exdate.toUTCString());
      $("#botright").removeClass('botright_down');
  }
  function right_buttom_div_close(){
//      var exdate=new Date();
//      exdate.setDate(exdate.getDate());
      setCookie("rbd", "3", 0);
      $("#botright").addClass('botright_close');
  }
