var cerca_libro1 = null;
var cerca_libro2 = null;

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

var filtro = "";
function set_filtri() {
    filtro = "";
    filtro += "&titolo="+document.getElementById("titolo").value;
    filtro += "&full_titolo="+document.getElementById("full_titolo").checked;
    filtro += "&autori="+document.getElementById("autori").value;
    filtro += "&full_autori="+document.getElementById("full_autori").checked;
    if (getCheckedValue(cerca.tipo) == "materie")
        filtro += "&idtipo="+document.getElementById("materia").value;
    if (getCheckedValue(cerca.tipo) == "dizionari")
        filtro += "&idtipo="+document.getElementById("dizionario").value;
    if (getCheckedValue(cerca.tipo) == "generi")
        filtro += "&idtipo="+document.getElementById("genere").value;
    filtro += "&editrice="+document.getElementById("editrice").value;
    
    //popola_cerca_libro();
    location.href = "cerca_risultati.php?visu=1"+filtro;
}

function set_filtri_search(titolo_search) {
    filtro += "&titolo="+titolo_search;
    filtro += "&full_titolo=false";
    popola_cerca_libro();
}

function popola_cerca_libro() {
    intest[0] = new Array(
        new Array("Risultati Ricerca", "cerca_libro"), // id per azioni bottone
        new Array("&nbsp;", "l.id", 0, false), // id link
        new Array("ISBN", "isbn", 0, false),
        new Array("titolo", "l.titolo", 0, false),
        new Array("argomento", "g.nome", 0, false),
        new Array("editrice", "editrice", 0, false),
        new Array("prezzo di copertina", "l.prezzo", 0, false),
        new Array("numero venditori", "l.vendita", 0, false)
    );
    
    crea_tabella_intestazione();
    lista_cerca_libro();
    crea_tabella_piede();
    conta_cerca_libro();
    //controlla_primo_piano();
}

function lista_cerca_libro(XY) {
    if (XY == null) XY = 0;
    var dati_post = "op=list_cerca_libro&inizio="+inizio[XY]+"&numXpag="+numXpag[XY]+filtro+"&ordine="+ordine[XY]+"&rand="+Math.random();
    cerca_libro1 = CreateXmlHttpReq(result_lista_cerca_libro);
    spedisci_richiesta(cerca_libro1, liv+'engineMYSQL/q_cerca_libro.php', dati_post);
}

function result_lista_cerca_libro() {
    if (cerca_libro1.readyState == 4 && cerca_libro1.status == 200) {
        if (cerca_libro1.responseText == "errore")
            visualizza_errore();
        else {
            if (cerca_libro1.responseText == "false")
                crea_tabella_righe_figo(null, null, null);
            else {
                var matrice = converti(cerca_libro1.responseText);
                var righe = new Array();
                var link = new Array();
                var url1 = liv+"cerca/libro/dati_libro.php?id=";
                var id_img = new Array();

                for (x = 0; x < matrice.length; x++) {
                    righe[x] = new Array(matrice[x][0], matrice[x][2], matrice[x][3], matrice[x][4], matrice[x][5], matrice[x][8], matrice[x][9]);
                    link[x] = new Array(url1+matrice[x][1], url1+matrice[x][1], url1+matrice[x][1], url1+matrice[x][1], url1+matrice[x][1], url1+matrice[x][1], url1+matrice[x][1]);
                    id_img[x] = new Array(matrice[x][1], matrice[x][7]);
                }
                crea_tabella_righe_figo(righe, link, id_img);
            }
        }
    }
}

function conta_cerca_libro() {
    var dati_post = "op=c_cerca_libro"+filtro+"&rand="+Math.random();
    cerca_libro2 = CreateXmlHttpReq(result_conta_cerca_libro);
    spedisci_richiesta(cerca_libro2, liv+'engineMYSQL/q_cerca_libro.php', dati_post);
}

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