// funzione per il recupero dell'oggetto XMLHttpRequest o equivalente
function getXmlHttpObject()
{
    var xmlHttp = null;
    
    try
    {
        // Firefox, Opera 8.0+, Safari (anche Internet Explorer 7)
        xmlHttp = new XMLHttpRequest();
    }
    catch (e)
    {
        // Internet Explorer
        try
        {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e)
        {
            try
            {
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e)
            {
                alert("Your browser does not support AJAX!");
            }
        }
    }
    
    return xmlHttp;
}

// funzione per l'interazione asincrona tra portlet
// parametri:
// action l'url da richiamare
// start l'id dell'elemento div che include il contenuto da aggiornare
// stop l'id dell'elemento div che segna la fine del contenuto di cui sopra
// start1, stop1, start2, stop2 id di altri elementi div per contenuti multipli
function actionURLAjax(action, start, stop, start1, stop1, start2, stop2)
{
    var xmlHttp = getXmlHttpObject();
    if (xmlHttp == null)
    {
        return;
    }

    try
    {
        document.body.style.cursor = "wait";
        
        xmlHttp.onreadystatechange = function()
        {
            if(xmlHttp.readyState == 4)
            {
                var res = xmlHttp.responseText;
                var startString = '<div id="' + start + '">';
                var startIndex = res.indexOf(startString);
                var stopString = '<div id="' + stop + '"/>';
                var stopIndex = res.indexOf(stopString);
                
                if (startIndex != -1 && stopIndex != -1 && document.getElementById(start) != null)
                {
                    document.getElementById(start).innerHTML = res.substring(startIndex + startString.length, stopIndex + stopString.length);
                }
                
                if (start1 != null && stop1 != null)
                {
                    startString = '<div id="' + start1 + '">';
                    startIndex = res.indexOf(startString);
                    stopString = '<div id="' + stop1 + '"/>';
                    stopIndex = res.indexOf(stopString);
                    
                    if (startIndex != -1 && stopIndex != -1 && document.getElementById(start1) != null)
                    {
                        document.getElementById(start1).innerHTML = res.substring(startIndex + startString.length, stopIndex + stopString.length);
                    }
                }
                
                if (start2 != null && stop2 != null)
                {
                    startString = '<div id="' + start2 + '">';
                    startIndex = res.indexOf(startString);
                    stopString = '<div id="' + stop2 + '"/>';
                    stopIndex = res.indexOf(stopString);
                    
                    if (startIndex != -1 && stopIndex != -1 && document.getElementById(start2) != null)
                    {
                        document.getElementById(start2).innerHTML = res.substring(startIndex + startString.length, stopIndex + stopString.length);
                    }
                }
                
                document.body.style.cursor = "auto";
            }
        }
        
        xmlHttp.open("POST", action, true);
        xmlHttp.send("");
    }
    catch(e)
    {
        document.body.style.cursor = "auto";
    }
}

// funzione per l'interazione asincrona tra portlet
// (versione servlet)
// parametri:
// action l'url da richiamare
// id l'id dell'elemento che include il contenuto da aggiornare
function actionURLAjaxServlet(action, id)
{
    var xmlHttp = getXmlHttpObject();
    if (xmlHttp == null)
    {
        return;
    }

    try
    {
        document.body.style.cursor = "wait";
        
        xmlHttp.onreadystatechange = function()
        {
            if(xmlHttp.readyState == 4)
            {
            
            // alert ( "CHIAMATA AJAX = " + xmlHttp.responseText);
            
                if (document.getElementById(id) != null)
                {
                
                    document.getElementById(id).innerHTML = xmlHttp.responseText;
                }
                
                document.body.style.cursor = "auto";
            }
        }
        
        xmlHttp.open("POST", action, true);
        xmlHttp.send("");
    }
    catch(e)
    {
        document.body.style.cursor = "auto";
    }
}

function actionURLAjaxServletPersonalize(action,id,id_campo,indexPosition)
{
    var xmlHttp = getXmlHttpObject();
    if (xmlHttp == null)
    {
        return;
    }

    try
    {
        document.body.style.cursor = "wait";
        
        xmlHttp.onreadystatechange = function()
        {
            if(xmlHttp.readyState == 4)
            {
            
             // alert ( "CHIAMATA AJAX = " + xmlHttp.responseText);
            
                if (document.getElementById(id) != null)
                {
                
                    document.getElementById(id).innerHTML = xmlHttp.responseText;
                    
                    var p=document.getElementById(id_campo);
                    p.options[indexPosition].selected="true";

                }
                
                document.body.style.cursor = "auto";
            }
        }
        
        xmlHttp.open("POST", action, true);
        xmlHttp.send("");
    }
    catch(e)
    {
        document.body.style.cursor = "auto";
    }
}


