var myRequest = new Array();
var x = 0;

function newsletter(){
    var email = document.getElementById('newsEmail').value;
    var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
    if(reg.test(email)) {
        x++;
        var kgoCaptcha = document.getElementById('kgoCaptcha').value;
        var r = Math.random();
        myRequest[x] = CreateXmlHttpReq(function() {myVerificaHandler(x)});
        myRequest[x].open("GET","kgoLoader/checkVerifica.php?r=" + escape(r) + "&kgoCaptcha=" + escape(kgoCaptcha));
        myRequest[x].send(null);
    }else{
        startLoading();
        document.getElementById('myLayer').innerHTML = '<div class="titoloAlert">Attenzione</div><div class="testoAlert">Inserire un indirizzo email corretto.<p><input type="button" name="Ok" id="Ok" value="Ok" onclick="stopLoading();" /></p></div>';
        fissaCentro('myLayer', 40, 10, 120);
        return false;
    }
}

//ancora in bozza.. da completare quando trovo tempo----------------------------------
function loadObjX(div, m, d, h, y){
    //startLoading();
    //document.getElementById(div).innerHTML = '<table width="100%" bgcolor="#D0D0D7"><tr><td height="400" align="center" valign="middle"><img src="' + 'http://demo.kgo.it/' + 'kgoLoader/overcome.gif"></td></tr></table>';
    coverIt('bigImage', 'cover');
    //startLoading();
    setTimeout("loadObjX2('"+div+"', '"+m+"', '"+d+"', '"+h+"', '"+y+"')", 250);
}

function coverIt(master, slave){
    var myMaster = document.getElementById(master);
    var mySlave = document.getElementById(slave);
    var masterW = myMaster.width;
    var masterH = myMaster.height;
    var masterL = findPosX(myMaster);
    var masterT = findPosY(myMaster);
    mySlave.style.width = masterW + 'px';
    mySlave.style.height = masterH + 'px';
    document.getElementById('tbCover').style.height = masterH + 'px';
    mySlave.style.top = masterT + 2 + 'px';
    mySlave.style.left = masterL + 2 + 'px';
    Mostra(slave);
}

function findPosX(obj){
    var curleft = 0;
    if(obj.offsetParent){
        while(1){
            curleft += obj.offsetLeft;
            if(!obj.offsetParent){ break; }
            obj = obj.offsetParent;
        }
    }else if(obj.x){
        curleft += obj.x;
    }
    return curleft;
}

function findPosY(obj){
    var curtop = 0;
    if(obj.offsetParent){
        while(1){
            curtop += obj.offsetTop;
            if(!obj.offsetParent){ break; }
            obj = obj.offsetParent;
        }
    }else if(obj.y){
        curtop += obj.y;
    }
    return curtop;
}

function loadObjX2(div, m, d, h, y){
    x++;
    var r = Math.random();
    var myUrl = 'kgoLoader/kgoLoader.php?';
    myUrl += 'r=' + escape(r);
    myUrl += '&d=' + escape(d);
    myUrl += '&m=' + escape(m);
    myUrl += '&h=' + escape(h);
    myUrl += '&y=' + escape(y);
    myRequest[x] = CreateXmlHttpReq(function() {StandardMyHandlerKgo(div, x)});
    myRequest[x].open("GET", myUrl);
    myRequest[x].send(null);
}

function StandardMyHandlerKgo(div, x) {
    if (myRequest[x].readyState == 4 && myRequest[x].status == 200) {
        e = document.getElementById(div);
        e.innerHTML = myRequest[x].responseText;
        //setTimeout("stopLoading()", 150);
        setTimeout("Nascondi('cover')", 150);
    }
}

function checkDelivery(idDelivery){
    document.getElementById('delivery').value = idDelivery;
    if(document.getElementById('userId').value == idDelivery){
        loadCart('1');
    }else{
        loadCart(idDelivery);
    }
}

function alertConsegna(){
    startLoading();
    document.getElementById('myLayer').innerHTML = '<div class="titoloAlert">Modalità di consegna</div><div class="testoAlert">Attualmente consegniamo settimanalmente in punti di ritrovo:<ul><li><span>MARTEDI: </span>Albinea, Reggio E. zona EST (Pappagnocca) </li><li><span>MERCOLEDI:</span> Cavriago</li><li><span>GIOVEDI:</span> Correggio</li><li><span>VENERDI:</span> Novellara, Campagnola, Villa rotta</li></ul>La consegna a domicilio &egrave;:<ul><li><span>MARTEDI: </span>Rivalta, Albinea, Reggio E. </li><li><span>VENERDI: </span>Novellara, Campagnola, Bagnolo </li></ul>La consegna a domicilio verr&agrave; effettuata <span>una volta a settimana al costo aggiuntivo di 3 &euro;</span>, direttamente a casa vostra.<br />La consegna nei punti di ritrovo o ai gruppi di acquisto esclude il costo aggiuntivo della consegna a domicilio.<br />Nel caso voi non foste in casa al momento della consegna vi preghiamo di indicarci un posto preciso, raggiungibile dal nostro autista, al riparo da animali ed intemperie nel quale lasceremo la Bio-cassa e ritireremo, se possibile, la cassa vuota della settimana precedente.<p><input type="button" name="Ok" id="Ok" value="Ok" onclick="stopLoading();" /></p></div>';
    fissaCentro('myLayer', 50, 50, 120);
}

function controllaOrdine(){
    var delivery = document.getElementById('delivery').value;
    if(delivery == ''){
        startLoading();
        document.getElementById('myLayer').innerHTML = '<div class="titoloAlert">Attenzione</div><div class="testoAlert">Indicare il metodo di consegna desiderato.<p><input type="button" name="Ok" id="Ok" value="Ok" onclick="stopLoading();" /></p></div>';
        fissaCentro('myLayer', 40, 10, 120);
        return false;
    }else if(document.getElementById('dataConsegna').value == ''){
        startLoading();
        document.getElementById('myLayer').innerHTML = '<div class="titoloAlert">Attenzione</div><div class="testoAlert">Indicare la data di consegna desiderata.<p><input type="button" name="Ok" id="Ok" value="Ok" onclick="stopLoading();" /></p></div>';
        fissaCentro('myLayer', 40, 10, 120);
        return false;
    }else{
        x++;
        var kgoCaptcha = document.getElementById('kgoCaptcha').value;
        var r = Math.random();
        myRequest[x] = CreateXmlHttpReq(function() {myVerificaHandler(x)});
        myRequest[x].open("GET","kgoLoader/checkVerifica.php?r=" + escape(r) + "&kgoCaptcha=" + escape(kgoCaptcha));
        myRequest[x].send(null);
    }
}

function loadCart(domicilio){
    x++;
    startLoading();
    var r = Math.random();
    var theUrl = 'carrelloAjaxRiepilogo.php?r=' + escape(r);
    theUrl += '&domicilio=' + escape(domicilio);
    myRequest[x] = CreateXmlHttpReq(function() {stampaInnerHTML('contenutoCarrello', true, x , '', '')});
    myRequest[x].open("GET", theUrl);
    myRequest[x].send(null);
}

function checkRdata(){
    var azienda = document.getElementById('azienda').value;
    var fiscale = document.getElementById('fiscale').value;
    var nome = document.getElementById('nome').value;
    var cognome = document.getElementById('cognome').value;
    var email = document.getElementById('email').value;
    var emailC = document.getElementById('emailC').value;
    if(azienda == ''){
        alert('Il campo azienda è obbligatorio. Inserire "Privato" se non si desidera registrarsi come Azienda.');
        blockMe('nome');
        return false;
    }else if(fiscale == ''){
        alert('Il campo Codice Fiscale è obbligatorio');
        blockMe('fiscale');
        return false;
    }else if(nome == ''){
        alert('Il campo nome è obbligatorio');
        blockMe('nome');
        return false;
    }else if(cognome == ''){
        alert('Il campo cognome è obbligatorio');
        blockMe('cognome');
        return false;
    }else if(email == '' || (email.indexOf('@') < 1)){
        alert('Il campo email è obbligatorio');
        blockMe('email');
        return false;
    }else if(email != emailC){
        alert('L\'email inserita non corrisponde');
        blockMe('emailC');
    }else{
        startLoading();
        setTimeout("controlloRdati()", 200);
    }
}

function controlloRdati(){
    x++;
    var kgoCaptcha = document.getElementById('kgoCaptcha').value;
    var r = Math.random();
    myRequest[x] = CreateXmlHttpReq(function() {myRverificaHandler(x)});
    myRequest[x].open("GET","kgoLoader/checkVerifica.php?r=" + escape(r) + "&kgoCaptcha=" + escape(kgoCaptcha));
    myRequest[x].send(null);
}

function myRverificaHandler(x) {
    if (myRequest[x].readyState == 4 && myRequest[x].status == 200) {
        mySo = myRequest[x].responseText;
        if(mySo == 1){
            x++;
            startLoading();
            var email = document.getElementById('email').value;
            var userId = document.getElementById('userId').value;
            var userHash = document.getElementById('userHash').value;
            var requestHash = document.getElementById('requestHash').value;
            var r = Math.random();
            var theUrl = 'kgoLoader/kgoLoader.php?r=' + escape(r);
            theUrl += '&email=' + escape(email);
            theUrl += '&userId=' + escape(userId);
            theUrl += '&userHash=' + escape(userHash);
            theUrl += '&requestHash=' + escape(requestHash);
            theUrl += '&h=0be86ef85513d2252f6067c34c8c91b1';
            theUrl += '&d=cooplalucerna.it';
            theUrl += '&m=controlloEmail';
            myRequest[x] = CreateXmlHttpReq(function() {emailHandler(x)});
            myRequest[x].open("GET", theUrl);
            myRequest[x].send(null);
        }else{
            alert('Ricopiare il codice Captcha nel campo apposito.');
            document.getElementById('kgoCaptcha').focus();
            document.getElementById('kgoCaptcha').value = '';
            cambiaCodice();
        }
    }
}

function emailHandler(x) {
    if (myRequest[x].readyState == 4 && myRequest[x].status == 200) {
        mySo = myRequest[x].responseText;
        if(mySo == '0010'){
            document.aggiornamento.submit();
        }else{
            document.getElementById('myLayer').innerHTML = '<div class="titoloAlert">Attenzione</div><div class="testoAlert">L\'indirizzo email indicato è già presente.<p><input type="button" name="Annulla" id="Annulla" value="Annulla" onclick="stopLoading();" /></p></div>';
            fissaCentro('myLayer', 40, 10, 120);
            return false;
        }
    }
}

function checkData(){
    var azienda = document.getElementById('azienda').value;
    var fiscale = document.getElementById('fiscale').value;
    var nome = document.getElementById('nome').value;
    var cognome = document.getElementById('cognome').value;
    var email = document.getElementById('email').value;
    var emailC = document.getElementById('emailC').value;
    var password = document.getElementById('password').value;
    var passwordC = document.getElementById('passwordC').value;
    var privacy = document.getElementById('privacy');

    if(azienda == ''){
        alert('Il campo azienda è obbligatorio. Inserire "Privato" se non si desidera registrarsi come Azienda.');
        blockMe('nome');
        return false;
    }else if(fiscale == ''){
        alert('Il campo Codice Fiscale è obbligatorio');
        blockMe('fiscale');
        return false;
    }else if(nome == ''){
        alert('Il campo nome è obbligatorio');
        blockMe('nome');
        return false;
    }else if(cognome == ''){
        alert('Il campo cognome è obbligatorio');
        blockMe('cognome');
        return false;
    }else if(email == '' || (email.indexOf('@') < 1)){
        alert('Il campo email è obbligatorio');
        blockMe('email');
        return false;
    }else if(email != emailC){
        alert('L\'email inserita non corrisponde');
        blockMe('emailC');
    }else if(password == ''){
        alert('Il campo password è obbligatorio, le servirà per i suoi prossimi acquisti');
        blockMe('password');
        return false;
    }else if(password != passwordC){
        alert('Le password inserite non corrispondono');
        blockMe('passwordC');
    }else if(!privacy.checked){
        alert('Deve acconsentire al trattamento dei dati per la privacy');
    }else{
        startLoading();
        setTimeout("controlloDati()", 200);
    }
}

function blockMe(field){
    stopLoading();
    document.getElementById(field).focus();
}

function controlloDati(){
    x++;
    var kgoCaptcha = document.getElementById('kgoCaptcha').value;
    var r = Math.random();
    myRequest[x] = CreateXmlHttpReq(function() {myVerificaHandler(x)});
    myRequest[x].open("GET","kgoLoader/checkVerifica.php?r=" + escape(r) + "&kgoCaptcha=" + escape(kgoCaptcha));
    myRequest[x].send(null);
}

function myVerificaHandler(x) {
    if (myRequest[x].readyState == 4 && myRequest[x].status == 200) {
        mySo = myRequest[x].responseText;
        if(mySo==1){
            document.theForm.submit();
        }else{
            alert('Ricopiare il codice Captcha nel campo apposito.');
            document.getElementById('kgoCaptcha').focus();
            document.getElementById('kgoCaptcha').value = '';
            cambiaCodice();
        }
    }
}

function cambiaCodice(){
    startLoading();
    setTimeout("cambiaCodice2()", 135);
}

function cambiaCodice2(){
    var r = Math.random();
    document.getElementById('captchaDiv').innerHTML = '<img src="kgoLoader/captcha/immagine.php?r=' + escape(r) + '"><br>se il codice è illeggibile, <span class="captchaLinkSmall" onClick="cambiaCodice();">[clicca qui]</span>';
    stopLoading();
}

function loadChart(){
    x++;
    startLoading();
    var r = Math.random();
    var theUrl = 'carrelloAjax.php?r=' + escape(r);
    myRequest[x] = CreateXmlHttpReq(function() {stampaInnerHTML('elencoCarrello', true, x , '', '')});
    myRequest[x].open("GET", theUrl);
    myRequest[x].send(null);
}
function sendCart(){
    var I = document.getElementById('i').value;
    x++;
    startLoading();
    document.getElementById('myLayer').innerHTML = '<div class="titoloAlert">Elaborazione in corso...</div><div class="testoAlert">Attendere prego</div>';
    fissaCentro('myLayer', 30, 10, 120);
    var r = Math.random();
    var theUrl = 'kgoLoader/kgoCart.php?r=' + escape(r);
    theUrl += '&do=' + 'refresh';
    for(m = 0; m != I; m++){
        theUrl += '&cLID' + escape(m) + '=' + escape(document.getElementById("cLID" + m).value);
        theUrl += '&cLTP' + escape(m) + '=' + escape(document.getElementById("cLTP" + m).value);
        theUrl += '&cLQN' + escape(m) + '=' + escape(document.getElementById("cLQN" + m).value);
        if(document.getElementById("cLTP" + m).value == 'box'){
            theUrl += '&cLCP' + escape(m) + '=' + escape(document.getElementById("cLCP" + m).value);
        }
    }
    theUrl += '&i=' + escape(m);
    theUrl += '&objHash=' + escape( document.getElementById('objHash').value );
    myRequest[x] = CreateXmlHttpReq(function() {stampaInnerHTML('myLayer', true, x , loadChart, '')});
    myRequest[x].open("GET", theUrl);
    myRequest[x].send(null);
}
function eliminaOggetto(id){
    document.getElementById('cLQN' + id).value = 0;
    document.getElementById('cLQN' + id).readOnly = 'readOnly';
    sendCart();
}
function aggiungiCarrello(cLID, cLTP, objHash){
    x++;
    startLoading();
    document.getElementById('myLayer').innerHTML = '<div class="titoloAlert">Elaborazione in corso...</div><div class="testoAlert">Attendere prego</div>';
    fissaCentro('myLayer', 30, 10, 120);
    var r = Math.random();
    var theUrl = 'kgoLoader/kgoCart.php?r=' + escape(r);

    theUrl += '&do=' + 'add';
    theUrl += '&cLTP=' + escape(cLTP);
    theUrl += '&cLID=' + escape(cLID);
    theUrl += '&cLQN=1';
    theUrl += '&objHash=' + escape(objHash);
    myRequest[x] = CreateXmlHttpReq(function() {stampaInnerHTML('myLayer', false, x , '', '')});
    myRequest[x].open("GET", theUrl);
    myRequest[x].send(null);
}

function cerca(pagina){
    var testo = document.getElementById('cercaOggetto');
    var categoria = document.getElementById('cercaCategoria');
    //se le chiavi di ricerca sono impostate, ma minori di 3 caratteri blocco tutto
    if(testo.value != '' && testo.value < 3){
        startLoading();
        document.getElementById('myLayer').innerHTML = '<div class="titoloAlert">Attenzione</div><div class="testoAlert">Le parole chiavi devono essere alemno 3 caratteri<p><input type="button" name="Ok" id="Ok" value="Ok" onclick="stopLoading();" /></p></div>';
        fissaCentro('myLayer', 40, 10, 120);
        return false;
    }else{
        x++;
        startLoading();
        var r = Math.random();
        var theUrl = 'kgoLoader/kgoLoader.php?r=' + escape(r);

        theUrl += '&pag=' + escape(pagina);
        theUrl += '&idCap=' + escape(categoria.value);
        theUrl += '&txt=' + escape(testo.value);
        theUrl += '&h=3e01d873621ee9a67b6ed8b9b517b9ff';
        theUrl += '&d=cooplalucerna.it';
        theUrl += '&m=stampaProdotti';
        myRequest[x] = CreateXmlHttpReq(function() {stampaInnerHTML('ecommerce', true, x, salvaRicerca, pagina)});
        myRequest[x].open("GET", theUrl);
        myRequest[x].send(null);
    }
}

function salvaRicerca(pagina){
    x++;
    var sHash = document.getElementById('sHash').value;
    var testo = document.getElementById('cercaOggetto');
    var categoria = document.getElementById('cercaCategoria');

    var r = Math.random();
    var theUrl = 'kgoLoader/kgoSM.php?r=' + escape(r);

    theUrl += '&sHash=' + escape(sHash);
    theUrl += '&sn[1]=cercaTxt';
    theUrl += '&sv[1]=' + escape(testo.value);
    theUrl += '&sn[2]=cercaCategoria';
    theUrl += '&sv[2]=' + escape(categoria.value);
    theUrl += '&sn[3]=cercaPagina';
    theUrl += '&sv[3]=' + escape(pagina);
    myRequest[x] = CreateXmlHttpReq(function(){hiddenHandler(x)});
    myRequest[x].open("GET", theUrl);
    myRequest[x].send(null);
}


function hiddenHandler(x){
    if (myRequest[x].readyState == 4 && myRequest[x].status == 200) {
        var myResponse = myRequest[x].responseText;
        if(myResponse != 'ok'){
            alert(myResponse);
        }
    }
}

function login(userField, passField){
    startLoading();
    var theUser = document.getElementById(userField);
    var thePass = document.getElementById(passField);
    if(theUser.value == ''){
        document.getElementById('myLayer').innerHTML = '<div class="titoloAlert">Attenzione</div><div class="testoAlert">Il campo User non può essere vuoto.<p><input type="button" name="Ok" id="Ok" value="Ok" onclick="stopLoading();" /></p></div>';
        fissaCentro('myLayer', 30, 10, 120);
    }else if(thePass.value == ''){
        document.getElementById('myLayer').innerHTML = '<div class="titoloAlert">Attenzione</div><div class="testoAlert">Inserire la propria Password di accesso.<p><input type="button" name="Ok" id="Ok" value="Ok" onclick="stopLoading();" /></p></div>';
        fissaCentro('myLayer', 30, 10, 120);
    }else{
        document.getElementById('myLayer').innerHTML = '<div class="titoloAlert">Log-in</div><div class="testoAlert">Autenticazione in corso...</div>';
        fissaCentro('myLayer', 20, 5, 120);
        x++;
        var r = Math.random();
        var theUrl = '/kgoLoader/kgoLoader.php?r=' + escape(r);

        theUrl += '&user=' + escape(theUser.value);
        theUrl += '&pass=' + escape(thePass.value);
        theUrl += '&h=2efa52245ed49f1fcc385a94670fb965';
        theUrl += '&d=cooplalucerna.it';
        theUrl += '&m=login';
        myRequest[x] = CreateXmlHttpReq(function() {loginHandler(true, x)});
        myRequest[x].open("GET", theUrl);
        myRequest[x].send(null);
    }
}

function salvaBiocassa(){
    startLoading();
    x++;
    var idBiocassa = document.getElementById('idBiocassa').value;
    var objHash = document.getElementById('objHash').value;

    document.getElementById('myLayer').innerHTML = '<div class="titoloAlert">Elaborazione in corso...</div><div class="testoAlert">Attendere prego</div>';
    fissaCentro('myLayer', 30, 10, 120);
    var r = Math.random();
    var theUrl = 'kgoLoader/kgoCart.php?r=' + escape(r);

    theUrl += '&do=' + 'add';
    theUrl += '&cLTP=box';
    theUrl += '&cLID=' + escape(idBiocassa);
    theUrl += '&cLQN=1';
    theUrl += '&objHash=' + escape(objHash);
    theUrl += '&cLCP=';
    var i = document.getElementById('i').value;
    for(e = 0; e != i; e++){
        theUrl += escape(document.getElementById('composizione' + e).value + '.');
    }
    myRequest[x] = CreateXmlHttpReq(function() {stampaInnerHTML('myLayer', false, x , '', '')});
    myRequest[x].open("GET", theUrl);
    myRequest[x].send(null);
}

function elaboraVariazione(){
    x++;
    var idBiocassa = document.getElementById('idBiocassa').value;
    var idComposizione = document.getElementById('mySelectedC').value;
    var idVariazione = document.getElementById('mySelectedV').value;
    var r = Math.random();

    var theUrl = 'kgoLoader/kgoLoader.php?r=' + escape(r);
    theUrl += '&d=cooplalucerna.it';
    theUrl += '&m=elaborazioneVariazioni';
    theUrl += '&h=3c590687422ade5c0b318ffe4ce2958d';
    theUrl += '&idBiocassa=' + idBiocassa;
    theUrl += '&idComposizione=' + idComposizione;
    theUrl += '&idVariazione=' + idVariazione;

    myRequest[x] = CreateXmlHttpReq(function() {stampaInnerHTML('elaborazioneBiocassa', true, x, '', '')});
    myRequest[x].open("GET", theUrl);
    myRequest[x].send(null);
}

function seleziona(tipo, id){
    var mySelected = document.getElementById('mySelected' + tipo);

    if(mySelected.value == ''){
        mySelected.value = id;

        if(tipo == 'C'){
            startLoading();
            fissaCentro('menuVariazioni', 30, 60, 120);
            return 'fselected';
        }else if(tipo == 'V'){
            Nascondi('menuVariazioni');
            document.getElementById('myLayer').innerHTML = '<div class="titoloAlert">Conferma</div><div class="testoAlert">Sei sicuro di voler effettuare questa Variazione?<p><input type="button" name="No" id="No" value="No" onclick="askVariazione(0, \'mySelected' + tipo + '\', \''+ id +'\', \''+ tipo +'\');" /><input type="button" name="Si" id="Si" value="Si" onclick="askVariazione(1, \'mySelected' + tipo + '\', \''+ id +'\', \''+ tipo +'\');" /></p></div>';
            fissaCentro('myLayer', 30, 10, 120);
        }
    }else if(mySelected.value == id){
        mySelected.value = '';
    }else{
        if(tipo == 'V'){
            startLoading();
            document.getElementById('myLayer').innerHTML = '<div class="titoloAlert">Attenzione</div><div class="testoAlert">E\' già stata selezionata una variazione<p><input type="button" name="Ok" id="Ok" value="Ok" onclick="stopLoading();" /></p></div>';
            fissaCentro('myLayer', 40, 10, 120);
        }else{
            startLoading();
            document.getElementById('myLayer').innerHTML = '<div class="titoloAlert">Attenzione</div><div class="testoAlert">E\' già stato selezionato un prodotto<p><input type="button" name="Ok" id="Ok" value="Ok" onclick="stopLoading();" /></p></div>';
            fissaCentro('myLayer', 40, 10, 120);
        }
    }
}

function askVariazione(esito, mySelected, id, tipo){
    if(esito){
        elaboraVariazione();
    }else{
        document.getElementById('mySelectedC').value = '';
        document.getElementById('mySelectedV').value = '';
        Nascondi('myFog');
        Nascondi('myLayer');
    }
}
function controllaSelezione(tipo, id){
    var mySelected = document.getElementById('mySelected' + tipo);
    if(mySelected.value == id){ return 'fselected'; }else{ return 'elencoObj' + tipo; }
}
function startLoading(){
    var myTarget = document.getElementById('myFog');
    var screenLen = document.body.clientWidth;
    var screenHei= document.body.clientHeight;
    //myTarget.style.width = screenLen + 'px';
    //myTarget.style.height = screenHei + 'px';
    myTarget.style.width = screenLen + 'px';
    myTarget.style.height = screenHei + 'px';
    myTarget.style.top = 0;
    myTarget.style.left = 0;
    myTarget.style.visibility = 'visible';
    myTarget.style.display = 'block';
}
function fissaCentro(div, larghezza, altezza, dimensioneFont){
    myDiv = document.getElementById(div);
    myDiv.style.width = larghezza + '%';
    myDiv.style.height = altezza + '%';
    myDiv.style.fontSize = dimensioneFont + '%';
    myDiv.style.top = (50 - (altezza/2)) + '%';
    myDiv.style.left = (50 - (larghezza/2)) + '%';
    myDiv.style.visibility = 'visible';
    myDiv.style.display = 'block';
}
function Nascondi(quale){
    var r = document.getElementById(quale);
    r.style.visibility = 'hidden';
    r.style.display = 'none';
}
function Mostra(quale){
    var r = document.getElementById(quale);
    r.style.visibility = 'visible';
    r.style.display = 'block';
}
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");
        }
    }
    xmlhttp.onreadystatechange = handler;
    return xmlhttp;
}

function StandardMyHandler(div) {
    if (myRequest.readyState == 4 && myRequest.status == 200) {
        e = document.getElementById(div);
        e.innerHTML = myRequest.responseText;
        stopLoading();
    }
}
function stampaInnerHTML(div, sL, x ,funzione, parametro) {
    if (myRequest[x].readyState == 4 && myRequest[x].status == 200) {
        var myResponse = myRequest[x].responseText;
        document.getElementById(div).innerHTML = myResponse;
        if(sL){
            Nascondi('myFog');
            Nascondi('myLayer');
        }
        if(funzione){
            if(parametro){
                funzione(parametro);
            }else{
                funzione();
            }
        }
    }
}
function stopLoading(){
    Nascondi('myFog');
    Nascondi('myLayer');
}
function salvaSessione(sL, x) {
    if (myRequest[x].readyState == 4 && myRequest[x].status == 200) {
        var myResponse = myRequest[x].responseText;
        if(myResponse == 'ok'){
            window.location = 'compra_online_login.php';
        }else{
            document.getElementById('myLayer').innerHTML = 'Errore:<br>' + myResponse;
            fissaCentro('myLayer', 70, 70, 110);
        }
    }
}

function loginHandler(sL, x) {
    if (myRequest[x].readyState == 4 && myRequest[x].status == 200) {
        var myResponse = myRequest[x].responseText;
        var esito = myResponse.split('|');
        if(esito[0] == 'ok'){
            document.getElementById('myLayer').innerHTML = '<div class="titoloAlert">Attenzione</div><div class="testoAlert">' + esito[2] + '</div>';
            x++;
            myRequest[x] = CreateXmlHttpReq(function() {loginCreator(true, x)});
            myRequest[x].open("GET", esito[1]);
            myRequest[x].send(null);
        }else{
            document.getElementById('myLayer').innerHTML = '<div class="titoloAlert">Attenzione</div><div class="testoAlert">' + esito[0] + '</div>';
        }
    }
}

function loginCreator(sL, x, page) {
    if (myRequest[x].readyState == 4 && myRequest[x].status == 200) {
        var myResponse = myRequest[x].responseText;
        if(myResponse == 'ok'){
            //window.location = page;
            document.getElementById('myLayer').innerHTML = '<div class="titoloAlert">Attenzione</div><div class="testoAlert">Autenticazione avvenuta<br /><a href="#" onclick="self.location.reload(true);">Clicca qui</a> per continuare.</div>';
        }else{
            document.getElementById('myLayer').innerHTML = 'Errore:<br>' + myResponse;
            fissaCentro('myLayer', 70, 70, 110);
        }
    }
}