var mesi = new Array("Sem", "Gen", "Feb", "Mar", "Apr", "Mag", "Giu", "Lug", "Ago", "Set", "Ott", "Nov", "Dic");

//var inizio = 0;
//var numXpag = 10;
//var ordine = "";
//var intestazione;
var inizio = new Array(0, 0, 0, 0);
var numXpag = new Array(10, 10, 10, 10);
var ordine = new Array("", "", "", "");
var intest = new Array(null, null, null, null);
var div_intest = new Array("intestazione", "intestazione1", "intestazione2", "intestazione3");
var div_righe = new Array("righe", "righe1", "righe2", "righe3");
var div_piede = new Array("piede", "piede1", "piede2", "piede3");


var utility_global1 = null;
var utility_global2 = null;
var utility_global3 = null;
var utility_global4 = null;
var utility_global5 = null;
var utility_global6 = null;
var utility_global7 = null;
var utility_global8 = null;

function popola_regioni() {
    var dati_post = "op=list_reg&rand="+Math.random();
    utility_global1 = CreateXmlHttpReq(result_popola_regioni);
    spedisci_richiesta(utility_global1, liv+'engineMYSQL/q_utility.php', dati_post);
}

function result_popola_regioni() {
    if (utility_global1.readyState == 4 && utility_global1.status == 200) {
        var result = new Array();
        if (utility_global1.responseText == "errore")
            visualizza_errore();
        else {
            result = converti(utility_global1.responseText);
            var e = document.getElementById("provincia").options;
            for (x = e.length-1; x > 0; x--)
                e[x] = null;
                
            e = document.getElementById("comune").options;
            for (x = e.length-1; x > 0; x--)
                e[x] = null;
    
            e = document.getElementById("regione").options;
            for (x = 0; x < result.length; x++) {
                document.getElementById("regione").options[x] = new Option(result[x][1], result[x][0]);
            }
        }
    }
}

function popola_provincie(id) {
    var dati_post = "op=list_prov&id="+id+"&rand="+Math.random();
    utility_global2 = CreateXmlHttpReq();
    spedisci_richiesta_aspetta(utility_global2, liv+'engineMYSQL/q_utility.php', dati_post);

    if (utility_global2.readyState == 4 && utility_global2.status == 200) {
        if (utility_global2.responseText == "errore")
            visualizza_errore();
        else {
            var e = document.getElementById("comune").options;
            for (x = e.length-1; x > 0; x--)
                e[x] = null;
        
            e = document.getElementById("provincia").options;
            
            var result = new Array();
            result = converti(utility_global2.responseText);
    
            for (x = e.length-1; x > 0; x--)
                e[x] = null;
            
            for (x = 0; x < result.length; x++)
                e[x] = new Option(result[x][1], result[x][0]);
            valida_comune();
        }
    }
}

function popola_comuni(id) {
    var dati_post = "op=list_com&id="+id+"&rand="+Math.random();
    utility_global3 = CreateXmlHttpReq();
    spedisci_richiesta_aspetta(utility_global3, liv+'engineMYSQL/q_utility.php', dati_post);

    if (utility_global3.readyState == 4 && utility_global3.status == 200) {
        if (utility_global3.responseText == "errore")
            visualizza_errore();
        else {
            e = document.getElementById("comune").options;
            
            var result = new Array();
            result = converti(utility_global3.responseText);
            
            for (x = e.length-1; x >= 0; x--)
                e[x] = null;
            for (x = 0; x < result.length; x++)
                e[x] = new Option(result[x][1], result[x][0]);
            valida_comune();
        }
    }
}

function popola_provincie_cerca(id) {
    var dati_post = "op=list_prov&id="+id+"&rand="+Math.random();
    utility_global2 = CreateXmlHttpReq();
    spedisci_richiesta_aspetta(utility_global2, liv+'engineMYSQL/q_utility.php', dati_post);

    if (utility_global2.readyState == 4 && utility_global2.status == 200) {
        if (utility_global2.responseText == "errore")
            visualizza_errore();
        else {
            var e = document.getElementById("comune").options;
            for (x = e.length-1; x > 0; x--)
                e[x] = null;
        
            e = document.getElementById("provincia").options;
            
            var result = new Array();
            result = converti(utility_global2.responseText);
    
            for (x = e.length-1; x > 0; x--)
                e[x] = null;
            
            for (x = 0; x < result.length; x++)
                e[x] = new Option(result[x][1], result[x][0]);
                
            document.getElementById("regione").selectedIndex = id;
            
            document.getElementById("italia").src = liv+"regioni/"+id+".gif";
            document.getElementById("cart_prov").style.visibility = "visible";
            document.getElementById("cart_prov").src = liv+"regioni/"+id+"-0.gif";
            //valida_comune();
        }
    }
}

function popola_comuni_cerca(id) {
    var dati_post = "op=list_com&id="+id+"&rand="+Math.random();
    utility_global3 = CreateXmlHttpReq();
    spedisci_richiesta_aspetta(utility_global3, liv+'engineMYSQL/q_utility.php', dati_post);

    if (utility_global3.readyState == 4 && utility_global3.status == 200) {
        if (utility_global3.responseText == "errore")
            visualizza_errore();
        else {
            e = document.getElementById("comune").options;
            
            var result = new Array();
            result = converti(utility_global3.responseText);
            
            for (x = e.length-1; x >= 0; x--)
                e[x] = null;
            for (x = 0; x < result.length; x++)
                e[x] = new Option(result[x][1], result[x][0]);
            //valida_comune();
        }
    }
}

function popola_editrici() {
    var dati_post = "op=list_editrici&rand="+Math.random();
    utility_global5 = CreateXmlHttpReq(result_popola_editrici);
    spedisci_richiesta(utility_global5, liv+'engineMYSQL/q_utility.php', dati_post);
}

function result_popola_editrici() {
    if (utility_global5.readyState == 4 && utility_global5.status == 200) {
        if (utility_global5.responseText == "errore")
            visualizza_errore();
        else {
            var e = document.getElementById("editrice").options;
            var result = new Array();
            result = converti(utility_global5.responseText);
    
            for (x = 0; x < result.length; x++)
                e[x] = new Option(result[x][1], result[x][0]);
            }      
    }
}

function popola_materie() {
    var dati_post = "op=list_materie&rand="+Math.random();
    utility_global6 = CreateXmlHttpReq(result_popola_materie);
    spedisci_richiesta(utility_global6, liv+'engineMYSQL/q_utility.php', dati_post);
}

function result_popola_materie() {
    if (utility_global6.readyState == 4 && utility_global6.status == 200) {
        if (utility_global6.responseText == "errore")
            visualizza_errore();
        else {
            var e = document.getElementById("materia").options;
            var result = new Array();
            result = converti(utility_global6.responseText);
    
            for (x = 0; x < result.length; x++)
                e[x] = new Option(result[x][1], result[x][0]);
            }      
    }
}

function popola_dizionari() {
    var dati_post = "op=list_dizionari&rand="+Math.random();
    utility_global7 = CreateXmlHttpReq(result_popola_dizionari);
    spedisci_richiesta(utility_global7, liv+'engineMYSQL/q_utility.php', dati_post);
}

function result_popola_dizionari() {
    if (utility_global7.readyState == 4 && utility_global7.status == 200) {
        if (utility_global7.responseText == "errore")
            visualizza_errore();
        else {
            var e = document.getElementById("dizionario").options;
            var result = new Array();
            result = converti(utility_global7.responseText);
    
            for (x = 0; x < result.length; x++)
                e[x] = new Option(result[x][1], result[x][0]);
            }      
    }
}

function popola_generi() {
    var dati_post = "op=list_generi&rand="+Math.random();
    utility_global8 = CreateXmlHttpReq(result_popola_generi);
    spedisci_richiesta(utility_global8, liv+'engineMYSQL/q_utility.php', dati_post);
}

function result_popola_generi() {
    if (utility_global8.readyState == 4 && utility_global8.status == 200) {
        if (utility_global8.responseText == "errore")
            visualizza_errore();
        else {
            var e = document.getElementById("genere").options;
            var result = new Array();
            result = converti(utility_global8.responseText);
    
            for (x = 0; x < result.length; x++)
                e[x] = new Option(result[x][1], result[x][0]);
            }      
    }
}
/*
function CreateXmlHttpReq(handler) {
    var xmlhttp = null;
    try {
        xmlhttp = new XMLHttpRequest();
    } catch(e) {
        try {
            xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
        } catch(e) {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    
    if (handler != null)
        xmlhttp.onreadystatechange = handler;
        
    return xmlhttp;
}*/



function CreateXmlHttpReq(handler) {
    var xmlhttp = null;
    
    if (window.XMLHttpRequest){
        xmlhttp = new XMLHttpRequest();
    }
    if (window.ActiveXObject) {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }

    if (handler != null)
        xmlhttp.onreadystatechange = handler;
        
    return xmlhttp;
}






function spedisci_richiesta(oggetto, dove, dati_post) {
    oggetto.open('POST', dove, true);
    oggetto.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    oggetto.setRequestHeader("Content-length", dati_post.length);
    oggetto.setRequestHeader("Connection", "close");
    oggetto.send(dati_post);
}

function spedisci_richiesta_aspetta(oggetto, dove, dati_post) {
    oggetto.open('POST', dove, false);
    oggetto.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    oggetto.setRequestHeader("Content-length", dati_post.length);
    oggetto.setRequestHeader("Connection", "close");
    oggetto.send(dati_post);
}

function converti(dati) {
    var conta = dati.length;
    dati = dati.substring(2, conta-4);

    var result = new Array();
    result = dati.split('"], ["');

    // scorro tutte le righe
    for (var i = 0; i < result.length; i++) {
            result[i] = result[i].split('", "');
    }
    return result;
}

function IsNumeric(sText) {
    var ValidChars = "0123456789.";
    var IsNumber=true;
    var Char;

    for (i = 0; i < sText.length && IsNumber == true; i++) { 
        Char = sText.charAt(i); 
        if (ValidChars.indexOf(Char) == -1)
            IsNumber = false;
    }
    return IsNumber;
}

function IsTesto(sText) {
    if (sText != null && sText != "" && sText.length > 10)
        return true;
    else
        return false;
}

function visu_simbolo(dove, cosa) {
    var d = document.getElementById(dove);
    d.src = "../img/val_"+cosa+".gif";
    
    if (cosa == "si")
        d.title = "Campo valido";
    if (cosa == "no")
        d.title = "Campo NON valido";
    if (cosa == "ob")
        d.title = "Campo Obbligatorio";
}

function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

function redirect(dove) {
    location.href = dove;
}

function visualizza_errore() {
    showDialog('Errore','Siamo spiacenti, per un malfunzionamento temporaneo il servizio richiesto non č disponibile.','error',2);
}

function visualizza_conferma(testo, azione) {
    var bottoni = "<input type=button onclick='"+azione+"' value='SI'>";
    bottoni += "<input type=button onclick='hideDialog()' value='NO'>";
    showDialog('Richiesta di conferma',testo+"<br>"+bottoni,'prompt');
}

function visualizza_warning(testo) {
    showDialog('Attenzione', testo,'warning');
}

function visualizza_warning_1(testo) {
    showDialog('Attenzione', testo,'warning', 10);
}

function visualizza_successo(testo) {
    showDialog('Operazione Eseguita', testo,'success', 2);
}

function valida_isbn() {
    var d = document.getElementById("isbn").value;
        		  	 
    if (d == null || d == '')
        visualizza_warning('Inserire il codice ISBN');
    else
        if (d.indexOf('-') >= 0)
            visualizza_warning('ll codice ISBN non puņ contenere trattini');
        else
            if (d.indexOf(' ') >= 0)
                visualizza_warning('ll codice ISBN non puņ contenere spazi');
            else
                if (d.length != 10 && d.length != 13)
                    visualizza_warning('ll codice ISBN deve essere composta da 10 o 13 caratteri');
                else
                    return true;
    return false;
}

function valida_testo(id) {
    var d = document.getElementById("div_"+id);
    var e = document.getElementById(id).value;
    if (e.length == 0) {
        visu_simbolo("div_"+id, "ob");
        return false;
    }
    else {
        if (e != "" && e != null && e.length > 3 && !IsNumeric(e)) {
            visu_simbolo("div_"+id, "si");
            return true;
        }
        else {
            visu_simbolo("div_"+id, "no");
            return false;
        }
    }
}

function valida_numero(id) {
    var d = document.getElementById("div_"+id);
    var e = document.getElementById(id).value;
    if (e.length == 0) {
        visu_simbolo("div_"+id, "ob");
        return false;
    }
    else {
        if (IsNumeric(e)) {
            visu_simbolo("div_"+id, "si");
            return true;
        }
        else {
            visu_simbolo("div_"+id, "no");
            return false;
        }
    }
}
