var cerca_utente1 = null;
var cerca_utente2 = null;

function aggiorna() {
    // funzioni che deve chiamare l'utility
    lista_cerca_utente();
    conta_cerca_utente();
    //controlla_primo_piano();
}

var filtro = "";

function popola_cerca_utente() {
    intest[0] = new Array(
        new Array("Risultati Ricerca", "cerca_utente"), // id per azioni bottone
        new Array("&nbsp;", "u.id", 0, false), // id link
        new Array("nick", "u.nick", 0, false),
        new Array("comune", "c.nome", 0, false),
        new Array("occupazione", "o.nome", 0, false),
        new Array("sesso", "u.sesso", 0, false)
        //new Array("prezzo di copertina", "l.prezzo", 0, false),
        //new Array("numero venditori", "l.vendita", 0, false)
    );
    
    crea_tabella_intestazione();
    lista_cerca_utente();
    crea_tabella_piede();
    conta_cerca_utente();
    //controlla_primo_piano();
}

function cerca_alfabeto(lettera) {
    filtro = "&alfa=si&nick="+lettera;
    location.href = "cerca_risultati.php?visu=1"+filtro;
    //popola_cerca_utente();
}

function cerca_nick() {
    var d = document.getElementById('nick').value;
    if (d == null || d == "")
        visualizza_warning("Inserire il nick o parte di esso")
	else {
        filtro = "&alfa=no&nick="+d;
        location.href = "cerca_risultati.php?visu=1"+filtro;
        //popola_cerca_utente();
    }
}

function cerca_comune(id) {
    if (id <= 0 || id == "")
        visualizza_warning("Selezionare un comune per poter effettuare la ricerca")
	else {
        filtro = "&id="+id;
        location.href = "cerca_risultati.php?visu=1"+filtro;
        //popola_cerca_utente();
    }
}

function lista_cerca_utente(XY) {
    if (XY == null) XY = 0;
    var dati_post = "op=list_cerca_utente&inizio="+inizio[XY]+"&numXpag="+numXpag[XY]+filtro+"&ordine="+ordine[XY]+"&rand="+Math.random();
    cerca_utente1 = CreateXmlHttpReq(result_lista_cerca_utente);
    spedisci_richiesta(cerca_utente1, liv+'engineMYSQL/q_cerca_utente.php', dati_post);
}

function result_lista_cerca_utente() {
    if (cerca_utente1.readyState == 4 && cerca_utente1.status == 200) {
        if (cerca_utente1.responseText == "errore")
            visualizza_errore();
        else {
            if (cerca_utente1.responseText == "false")
                crea_tabella_righe_figo(null, null, null);
            else {
            /*
            0 id utente
            1 nick
            2 comune      $row->comune."<br>(".$row->provincia.")".$row->regione
            3 provincia
            4 regione
            5 occupazione $row->occupazione."<br>(".$row->tipo
            6 occupazione tipo
            7 sesso
            8 immagine
            */
            
                var matrice = converti(cerca_utente1.responseText);
                var righe = new Array();
                var link = new Array();
                var url1 = liv+"cerca/utente/profilo.php?iduser=";

                for (x = 0; x < matrice.length; x++) {
                    righe[x] = matrice[x];
                    link[x] = url1+matrice[x][1];
                }
                crea_tabella_utenti(righe, link);
            }
        }
    }
}

function conta_cerca_utente() {
    var dati_post = "op=c_cerca_utente"+filtro+"&rand="+Math.random();
    cerca_utente2 = CreateXmlHttpReq(result_conta_cerca_utente);
    spedisci_richiesta(cerca_utente2, liv+'engineMYSQL/q_cerca_utente.php', dati_post);
}

function result_conta_cerca_utente() {
    if (cerca_utente2.readyState == 4 && cerca_utente2.status == 200) {
        if (cerca_utente2.responseText == "errore")
            visualizza_errore();
        else
            conta_tabella(cerca_utente2.responseText);
    }
}
