function getXMLHttpRequest()
{
    var XHR = null;
    var browserUtente = navigator.userAgent.toUpperCase();
    if(typeof(XMLHttpRequest) == "function" || typeof(XMLHttpRequest) == "object")
    {
        XHR = new XMLHttpRequest();
    }
    else if(window.ActiveXObject && browserUtente.indexOf("MSIE 4") < 0 ) 
    {
        if(browserUtente.indexOf("MSIE 5") < 0)
            XHR = new ActiveXObject("Msxml2.XMLHTTP");
        else
            XHR = new ActiveXObject("Microsoft.XMLHTTP");
    }
    return XHR;
}

function ajaxSubmit(xml)
{
    var ajax = getXMLHttpRequest();

    if(ajax)
    {
        ajax.open("post", "validator.aspx", true);
        ajax.setRequestHeader("content-type", "application/x-www-form-urlencoded");
        ajax.onreadystatechange = function() {
            if (ajax.readyState == 4) {
                if (ajax.status == 200) {
                    if (ajax.responseText == "") {
                        // document.forms[0].salva.disabled = true;
                        document.forms[0].submit();
                    }
                    else {
                        document.location = '#';
                        if (document.getElementById('salva')) {
                            document.getElementById('salva').disabled = false;
                        }
                        if (document.getElementById('pleasewaitScreen')) {
                            document.getElementById('pleasewaitScreen').style.visibility = "hidden";
                        }
                        
                        document.getElementById('messaggi').innerHTML = ajax.responseText;
                    }
                }
                else
                    alert("Ajax: Operazione fallita, errore numero " + ajax.status);
            }
        }
    }
    var str = "xml=" + xml + "&";
    for (i=0; i<document.forms[0].elements.length; i++)
    {
        str += document.forms[0].elements[i].name + "=" + document.forms[0].elements[i].value + "&";
    }
    str = str.substring(0,str.length-1);
    ajax.send(str);
}

function ajaxScriptExecutor()
{
    var ajax = getXMLHttpRequest();

    if(ajax)
    {
        ajax.open("post", "scriptexecutor.aspx", true);
        ajax.setRequestHeader("content-type", "application/x-www-form-urlencoded");
        ajax.onreadystatechange = function()
        {
            if(ajax.readyState == 4)
            {
                if(ajax.status == 200)
                {
                    if (ajax.responseText=="")
                        document.forms[0].submit();
                    else
                    {
                        document.location = '#';
                        document.getElementById('messaggi').innerHTML = ajax.responseText;
                    }
                }
                else
                    alert("Ajax: Operazione fallita, errore numero " + ajax.status);
            }
        }
    }
    var str = "";
    for (i=0; i<document.forms[0].elements.length; i++)
    {
        str += document.forms[0].elements[i].name + "=" + document.forms[0].elements[i].value + "&";
    }
    str = str.substring(0,str.length-1);
    ajax.send(str);
}


function ajaxArticoloAddValue(idTipo, valore,valore_EN,idUnivoco) {
    var ajax = getXMLHttpRequest();

    if (ajax) {
        ajax.open("post", "articoloAdminAjax.aspx?action=newValue&idTipo=" + idTipo + "&value=" + valore.value + "&value_EN=" + valore_EN.value, true);
        ajax.setRequestHeader("content-type", "application/x-www-form-urlencoded");
        ajax.onreadystatechange = function() {
            if (ajax.readyState == 4) {
                if (ajax.status == 200) {
                    if (ajax.responseText == "")
                        document.location = '#';

                    else {
                        //document.location = '#';
                        //document.getElementById('tst').innerHTML = ajax.responseText;

                        var _table = document.getElementById('tabella' + idTipo);
                        var _row = _table.insertRow(1);
                        _row.id = 'row' + ajax.responseText;

                        var cell0 = _row.insertCell(0);
                        var cell1 = _row.insertCell(1);
                        var cell2 = _row.insertCell(2);
                        cell0.innerHTML = valore.value;
                        cell1.innerHTML = valore_EN.value;
                        cell2.innerHTML = ' <a href="javascript:deleteValue(\'' + idTipo + '\',\'row' + ajax.responseText + '\')"><img src="public/images/delete.gif"/></a>';

                        valore.value = '';
                        valore_EN.value = '';
                    }
                }
                else
                    alert("Ajax: Operazione fallita, errore numero " + ajax.status);
            }
        }
    }
    var str = "";
    for (i = 0; i < document.forms[0].elements.length; i++) {
        str += document.forms[0].elements[i].name + "=" + document.forms[0].elements[i].value + "&";
    }
    str = str.substring(0, str.length - 1);
    ajax.send(str);
}


function ajaxArticoloDeleteValue(idTipo, idValore) {
    var ajax = getXMLHttpRequest();

    if (ajax) {
        ajax.open("post", "articoloAdminAjax.aspx?action=deleteValue&idTipo=" + idTipo + "&idValore=" + idValore, true);
        ajax.setRequestHeader("content-type", "application/x-www-form-urlencoded");
        ajax.onreadystatechange = function() {
            if (ajax.readyState == 4) {
                if (ajax.status == 200) {
                    if (ajax.responseText == "")
                        document.location = '#';
                        
                    else {
                        //document.location = '#';
                        if (ajax.responseText == 'OK') {
                            var _table = document.getElementById('tabella' + idTipo).deleteRow(document.getElementById(idValore).rowIndex);
                        }
                    }
                }
                else
                    alert("Ajax: Operazione fallita, errore numero " + ajax.status);
            }
        }
    }
    var str = "";
    for (i = 0; i < document.forms[0].elements.length; i++) {
        str += document.forms[0].elements[i].name + "=" + document.forms[0].elements[i].value + "&";
    }
    str = str.substring(0, str.length - 1);
    ajax.send(str);
}


function ajaxArticoloAddSpec(idArticolo, spec, spec_EN) {
    var ajax = getXMLHttpRequest();

    if (ajax) { 
        ajax.open("post", "articoloAdminAjax.aspx?action=newSpec&idArticolo=" + idArticolo + "&spec=" + spec.value + "&spec_EN=" + spec_EN.value, true);
        ajax.setRequestHeader("content-type", "application/x-www-form-urlencoded");
        ajax.onreadystatechange = function() {
            if (ajax.readyState == 4) {
                if (ajax.status == 200) {
                    if (ajax.responseText == "")
                        document.location = '#';

                    else {
                        //document.location = '#';
                        //document.getElementById('tst').innerHTML = ajax.responseText;
                        var _table = document.getElementById('tabella' + idArticolo);
                        var _row = _table.insertRow(2);
                        _row.id = ajax.responseText;

                        var cell0 = _row.insertCell(0);
                        var cell1 = _row.insertCell(1);
                        var cell2 = _row.insertCell(2);

                        cell0.innerHTML = '<span class="labelProdotto">' + spec.value + '</span>';
                        cell1.innerHTML = '<span class="labelProdotto">' + spec_EN.value + '</span>';
                        cell2.innerHTML = ' <a href="javascript:deleteCategoria(\'' + idArticolo + '\',\'' + ajax.responseText + '\')"><img src="public/images/delete.gif"/></a>';

                        var _row1 = _table.insertRow(3);
                        _row1.id = ajax.responseText + '1';
                        var cell10 = _row1.insertCell(0);
                        var cell11 = _row1.insertCell(1);
                        //cell10.colSpan = 2;
                        //cell10.rowSpan = 2;
                        var cell12 = _row1.insertCell(2);


                        // Creazione Tabella con i valori
                        var innerTable = document.createElement("table");
                        innerTable.id = 'tabella' + ajax.responseText;
                        innerTable.border = 0;
                        //innerTable.width = '100%';

                        // Riga con intestazione tabella
                        var rowDescrizioneValore = innerTable.insertRow(0);
                        var thDescrizioneValore0 = document.createElement('th');
                        var thDescrizioneValore1 = document.createElement('th');
                        var thDescrizioneValore2 = document.createElement('th');
                        thDescrizioneValore0 = rowDescrizioneValore.appendChild(thDescrizioneValore0);
                        thDescrizioneValore1 = rowDescrizioneValore.appendChild(thDescrizioneValore1);
                        thDescrizioneValore2 = rowDescrizioneValore.appendChild(thDescrizioneValore2);
                        thDescrizioneValore0.innerHTML = 'Italiano'
                        thDescrizioneValore1.innerHTML = 'Inglese'
                        thDescrizioneValore2.innerHTML = ' '

                        // Riga con valori tabella
                        var rowValore = innerTable.insertRow(1);
                        var tdDescrizioneValore0 = document.createElement('td');
                        var tdDescrizioneValore1 = document.createElement('td');
                        var tdDescrizioneValore2 = document.createElement('td');
                        tdDescrizioneValore0 = rowValore.appendChild(tdDescrizioneValore0);
                        tdDescrizioneValore1 = rowValore.appendChild(tdDescrizioneValore1);
                        tdDescrizioneValore2 = rowValore.appendChild(tdDescrizioneValore2);

                        tdDescrizioneValore0.innerHTML = '<input type="text" size="12" name="newValore' + ajax.responseText.substring(0, 7) + '"/>';
                        tdDescrizioneValore1.innerHTML = '<input type="text" size="12" name="newValore' + ajax.responseText.substring(0, 7) + '_EN"/>';
                        tdDescrizioneValore2.innerHTML = '<a href="javascript:addNewValue(\'' + ajax.responseText + '\',document.articolo.newValore' + ajax.responseText.substring(0, 7) + ',document.articolo.newValore' + ajax.responseText.substring(0, 7) + '_EN)"><img src="public/images/vero.ico"/></a>'

                        cell12.appendChild(innerTable);

                        spec.value = '';
                        spec_EN.value = '';
                    }
                }
                else
                    alert("Ajax: Operazione fallita, errore numero " + ajax.status);
            }
        }
    }
    var str = "";
    for (i = 0; i < document.forms[0].elements.length; i++) {
        str += document.forms[0].elements[i].name + "=" + document.forms[0].elements[i].value + "&";
    }
    str = str.substring(0, str.length - 1);
    ajax.send(str);
}


function ajaxArticoloDeleteCategoria(idArticolo, idCategoria) {
    var ajax = getXMLHttpRequest();

    if (ajax) {
        ajax.open("post", "articoloAdminAjax.aspx?action=deleteCat&idArticolo=" + idArticolo + "&idCategoria=" + idCategoria, true);
        ajax.setRequestHeader("content-type", "application/x-www-form-urlencoded");
        ajax.onreadystatechange = function() {
            if (ajax.readyState == 4) {
                if (ajax.status == 200) {
                    if (ajax.responseText == "")
                        document.location = '#';

                    else {
                        //document.location = '#';
                        //if (ajax.responseText == 'OK') {
                        var _table = document.getElementById('tabella' + idArticolo).deleteRow(document.getElementById(idCategoria).rowIndex);
                        var _table = document.getElementById('tabella' + idArticolo).deleteRow(document.getElementById(idCategoria + '1').rowIndex);
                        //}
                    }
                }
                else
                    alert("Ajax: Operazione fallita, errore numero " + ajax.status);
            }
        }
    }
    var str = "";
    for (i = 0; i < document.forms[0].elements.length; i++) {
        str += document.forms[0].elements[i].name + "=" + document.forms[0].elements[i].value + "&";
    }
    str = str.substring(0, str.length - 1);
    ajax.send(str);
}
