/*
** getResponse
** Usa l'oggetto XMLHTTP per inviare una query al server ed ottenere una
** risposta, che viene poi ritornata
*/
function getResponse(sQuery, corpo){
    if (sQuery == '') return;
/*
    if (sQuery.indexOf("?") != -1)
        sQuery += "&NHRN="+Math.random();
    else
        sQuery += "?NHRN="+Math.random();
*/
    oHTTP = getHttp();
    if (oHTTP == null)
        return ("!? XMLHttpRequest non istanziato, problemi...");

    if(arguments.length == 1){
        // invia la query con una GET
        oHTTP.open("GET",sQuery, false);
        oHTTP.send(null);
    }else{
         // invia la query con una POST, stile <FORM>
        try{
            oHTTP.open("POST",sQuery, false);
            oHTTP.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            oHTTP.setRequestHeader("Content-length", corpo.length);
            oHTTP.setRequestHeader("Connection", "close");
            /*
            oHTTP.onreadystatechange = function() {//Call a function when the state changes.
                if(oHTTP.readyState == 4 && oHTTP.status == 200) {
                    // chiamata dopo la POST. se non è presente nella pagina interessata, non fa nulla.
                    try{
                        ss = oHTTP.responseText;
                    }catch(e){};
                }
            }
            */
            //alert(corpo);
            oHTTP.send(corpo);
        }catch(e){
            alert(e)
        }
    }
    //oHTTP.responseText.length && 
    if (oHTTP.readyState == 4 && oHTTP.status == 200){
        //if (oHTTP.status == 200 ){
        return(oHTTP.responseText);
    }
    else{
        // alert('Si è verificato un errore HTTP: '+oHTTP.status+"\n\n\n"+sQuery+corpo);
        alert("An error was encountered on the page you were trying to view.\nI'm sorry for the inconvience - an email has already been sent to me that describes the problem you've had.\nThanks "+oHTTP.status);
        oHTTP.abort();
        return null;
    }
}
/*
**  istanza l'oggetto HTTP
*/
var oHTTP = null;
function getHttp(){
    if(oHTTP == null){
        try{
            oHTTP = new XMLHttpRequest();
        }catch(e){
            if(window.ActiveXObject){
                if (navigator.userAgent.toLowerCase().indexOf("msie 5") != -1)
                    oHTTP = new ActiveXObject("Microsoft.XMLHTTP");
                else
                    oHTTP = new ActiveXObject("Msxml2.XMLHTTP");
            }
        }
    }
    return oHTTP;
}



/*
**  I.Montafia 06-05-09
**  Funzione che serve a popolare con ajax dei campi input.
**  parametri:
**  -oForm:     nome del form contenente gli input
**  -sBuf:      stringa derivante da una select (separatori di riga e colonna richiesti: CTRL-A, CTRL-B)
**  -campiPop:  elenco dei campi da popolare. (deve essere uguale all'elenco dei campi estratti con select.
**  -ind:       eventuale indice se il campo fa parte di una collezione.
*/
function autoComplet(oForm, sBuf, campiPop, ind){
    var r1 = String.fromCharCode(2);
    var c1 = String.fromCharCode(1);

    // se l'ultimo carattere dei campi da popolare non è un ; lo aggiungo io per splittare correttamente.
    lung = campiPop.length-1;
    if (campiPop.substr(lung) != ';')
        campiPop += ';';

    campi   = campiPop.split(';');
    nCampi  = campi.length;

    // se non trovo nessun record, creo una riga vuota in modo da svuotare i campi
    if (sBuf == r1){
        sBuf = "";
        for (kk=0; kk<nCampi-2; kk++){
            sBuf += ''+c1;
        }
        sBuf += ''+r1;
    }

    // commentato perchè se tiro fuori solo un campo mi blocca.
    if (sBuf.indexOf(r1) < 0 || sBuf.indexOf(c1) < 0)
    {
//        my_alert("Formato non conforme: "+sBuf);
//        return;
    }
    righe   = sBuf.split(r1);
    nRighe  = righe.length - 1;
    // ciclo sulle righe
    for (ii=0; ii<nRighe; ii++){
        colonne     = righe[ii].split(c1);
        nColonne    = colonne.length;
        try 
        {
            for (jj=0; jj<nColonne; jj++){

                // se è previsto un indice di collezione, prendo l'oggetto corretto.
                if (arguments.length == 4 && parseInt(ind) >= 0)
                    indVero = ind;
                else
                    indVero = jj;

                if (nRighe > 1 || parseInt(ind) >= 0)
                    objBox = eval("document."+oForm.name+"."+campi[jj]+"["+indVero+"]");
                else
                    objBox = eval("document."+oForm.name+"."+campi[jj]);
                // se campo checkbox valorizzo opportunamente.
                if (objBox.type.toUpperCase() == 'CHECKBOX'){
                    if(objBox.value == colonne[jj])
                        objBox.checked = true;
                    else
                        objBox.checked = false;
                }
                else{
                    //objBox.value = htmlEntities(colonne[jj], 'ret', 'ajax');
                    objBox.value = colonne[jj];
                }
            }
        }
        catch(e)
        {
            alert("Campo: "+campi[jj]+"\nErrore: "+e.description);
        }
    }
}


function getBufForm(form){
    var getstr = "";
    for(var i = 0; i < form.elements.length; i++){
        getstr += ctrl_obj(form.elements[i]);
    }
    return(getstr);
}

function ctrl_obj(obj){
    var str_qui = "";
    if(String(obj.tagName).toUpperCase() == "INPUT"){
        if(String(obj.type).toUpperCase() == "HIDDEN" || String(obj.type).toUpperCase() == "TEXT"){
            str_qui += obj.name + "=" + escape(obj.value) + "&";
        }
        if(String(obj.type).toUpperCase() == "CHECKBOX"){
            if(obj.checked){
                str_qui += obj.name + "=" + escape(obj.value) + "&";
            }else{
                str_qui += obj.name + "=&";
            }
        }
        if(String(obj.type).toUpperCase() == "RADIO"){
            if (obj.checked){
                str_qui += obj.name + "=" + escape(obj.value) + "&";
            }
        }
    }
    if(String(obj.tagName).toUpperCase() == "SELECT"){
        var sel = obj;
        str_qui += sel.name + "=" + escape(sel.options[sel.selectedIndex].value) + "&";
    }
    if(String(obj.tagName).toUpperCase() == "TEXTAREA"){
        str_qui += obj.name + "=" + escape(obj.value) + "&";
    }
    return(str_qui);
}

function pulisci_campi(form){
    for(var i = 0; i < form.elements.length; i++){
        if(String(form.elements[i].type).toUpperCase() != "HIDDEN" && String(form.elements[i].readOnly).toUpperCase()!="TRUE")
            form.elements[i].value="";
    }
}
