offerte = {
    
    esito_KO: function() {
        alert('Attenzione! si sono verificati degli errori');
    },



    esito_OK: function(obj) {
        // alert(obj.responseText);
        // return;
       var dati = eval('(' + obj.responseText + ')');
       if (dati['popola_zona']) {
            // CONTRIBUISCI_TESTO
            var cont = document.getElementById('cont_zone');
            if (!cont) {
                return;
            }
            if (dati['errore']) {
                alert(dati['errore']);
                cont.style.display = '';
                cont.innerHTML = '';
            } else {
                var combo_zone = '';
                var i = '';
                if (dati['info']) {
                    combo_zone = '<b>Zona</b> <select name="zona" id="o_zona">' +
                        '<option value="0">Tutte</option>';
                    for (i in dati['info']) {
                        if (i == 'indexOf') { // bug IE
                            continue;
                        }
                        combo_zone += '<option value="' + i + '">' +  dati['info'][i] + '</option>';
                    }
                    combo_zone += '</select>';
                }    
                cont.innerHTML = combo_zone;
                cont.style.display = '';
            }
       } 

    },



    cercazona: function(id) {
        if (isNaN(id)) {
            alert('id non valido');
            return;
        }
        if (id==0 || id =='-1') {
            var cont = document.getElementById('cont_zone');
            if (!cont) {
                return;
            }
            cont.style.display = 'none';
            return;
        }
        
        var ar_parameter = new Array(1);
        ar_parameter[0] = id;

        var callback = { success: offerte.esito_OK, failure: offerte.esito_KO, argument: (ar_parameter) };
        YAHOO.util.Connect.asyncRequest('POST', '/ajax/cerca_zona.php', callback, 'id=' + escape(id) );
    },



    save_short_form: function() {
        var sottometti = document.getElementById('ins_offerta');
        if (!sottometti) {
            sottometti = document.getElementById('mod_offerta');
            if (!sottometti) {
                return;
            }    
        }
        sottometti.submit();
    },



    getObject: function(obj) {
      var theObj;
      if(document.all) {
        if(typeof obj=="string") {
          return document.all(obj);
        } else {
          return obj.style;
        }
      }
      if(document.getElementById) {
        if(typeof obj=="string") {
          return document.getElementById(obj);
        } else {
          return obj.style;
        }
      }
      return null;
    },



    //Contador de caracteres.
    contar: function(entrada,salida,texto,caracteres) {
        var entradaObj = offerte.getObject(entrada);
        var salidaObj = offerte.getObject(salida);
        var longitud=caracteres - entradaObj.value.length;
        if(longitud < 0) {
            longitud=0;
//            texto='<span class="disable"> '+texto+' </span>';
            entradaObj.value=entradaObj.value.substr(0,caracteres);
        } 
        salidaObj.innerHTML = texto.replace("{CHAR}",longitud);
    },



    home_cerca: function() {
        var scelta = document.getElementById('o_citta');
        if (!scelta) {
            return false;
        }
        if (isNaN(scelta.value) || scelta.value<1) {
            alert('Seleziona un citta\'');
            return false;
        }

        return true;
    }

}

function qf_lunghezza_titolo()
{
    var titolo = document.getElementById('o_titolo');
    if (!titolo) {
        return true;
    }
    if (titolo.value.length > 50 || titolo.value.length<10) {
        return false;
    }
    return true;
}
