var xmlhttp = new Array(); // ARRAY OF XML-HTTP REQUESTS
var xi = new Array(0); // ARRAY OF XML-HTTP REQUEST INDEXES
var g_lbl_ErrName;
xi[0] = 1; // FIRST INDEX SET TO 1 MAKING IT AVAILABLE
var AjaxInterval;

function CheckAJAXSupportTimer(id_Div, URLPage, sec) {
    CheckAJAXSupport(id_Div,URLPage);
    AjaxInterval = setInterval("CheckAJAXSupport('" + id_Div + "', '" + URLPage + "')", sec * 1000);
}


function StopRepetition()
{
	clearInterval(AjaxInterval); 
}

function StartRepetition(id_Div, URLPage, sec)
{
	AjaxInterval = setInterval("CheckAJAXSupport('" + id_Div + "', '" + URLPage + "')", sec * 1000);
}

function CheckAJAXSupport(id_Div, URLPage) {
    var index = GetXmlHttpObject();
    if (xmlhttp[index] == null) {
        alert("Il tuo browser non sopporta XMLHTTP!");
        return;
    }

var URLString = new String(URLPage)
var old_sidvalue =0;
if (URLString.indexOf("&sid=") > 0){
	old_sidvalue = getParameter ( URLPage, "sid");
	URLPage = URLString.replace(old_sidvalue,Math.random())
}
else
    URLPage = URLPage + "&sid=" + Math.random(); 
    xmlhttp[index].onreadystatechange = function() { stateChanged(id_Div, index) };
    xmlhttp[index].open("GET", URLPage, true);
    xmlhttp[index].send(null);
}


function getParameter ( queryString, parameterName ) {
// Add "=" to the parameter name (i.e. parameterName=value)
var parameterName = parameterName + "=";
if ( queryString.length > 0 ) {
// Find the beginning of the string
begin = queryString.indexOf ( parameterName );
// If the parameter name is not found, skip it, otherwise return the value
if ( begin != -1 ) {
// Add the length (integer) to the beginning
begin += parameterName.length;
// Multiple parameters are separated by the "&" sign
end = queryString.indexOf ( "&" , begin );
if ( end == -1 ) {
end = queryString.length
}
// Return the string
return unescape ( queryString.substring ( begin, end ) );
}
// Return "null" if no parameter has been found
return "null";
}
} 




function CheckAJAXSupportNoQuerry(id_Div, URLPage) {
    var index = GetXmlHttpObject();
    if (xmlhttp[index] == null) {
        alert("Il tuo browser non sopporta XMLHTTP!");
        return;
    }
    URLPage = URLPage + "?sid=" + Math.random(); // javascript cache buster
    xmlhttp[index].onreadystatechange = function() { stateChanged(id_Div, index) };
    xmlhttp[index].open("GET", URLPage, true);
    xmlhttp[index].send(null);
}

//file che richiamano questa funzione :
function CheckAJAXSupportLabel(ValoreCampo, NomeCampo, lbl_ErrName, URLPage, b_AllowEmpty) {

    g_lbl_ErrName = lbl_ErrName
    if (!b_AllowEmpty && ValoreCampo.length == 0) {
        alert("Il campo " + NomeCampo + " non puņ essere vuoto!");
        return;
    }
    var index = GetXmlHttpObject();
    if (xmlhttp[index] == null) {
        alert("Il tuo browser non sopporta XMLHTTP!");
        return;
    }
    // URLPage = URLPage + "sid=" + Math.random(); // javascript cache buster
    xmlhttp[index].onreadystatechange = function() { stateChangedLabel(index)};
    xmlhttp[index].open("GET", URLPage, true);
    xmlhttp[index].send(null);
}


//file che richiamano questa funzione :
//Modifica_Display.asp
function CheckAJAXSupportAlert(ValoreCampo, NomeCampo, URLPage, b_AllowEmpty) {

    if (!b_AllowEmpty && ValoreCampo.length == 0) {
        alert("Il campo " + NomeCampo + " non puņ essere vuoto!");
        return;
    }
    var index = GetXmlHttpObject();
    if (xmlhttp[index] == null) {
        alert("Il tuo browser non sopporta XMLHTTP!");
        return;
    }
    URLPage = URLPage + "&sid=" + Math.random(); // javascript cache buster
    xmlhttp[index].onreadystatechange = function() { stateChangedAlert(index) };
    xmlhttp[index].open("GET", URLPage, true);
    xmlhttp[index].send(null);
}


function GetXmlHttpObject() {

    // i_length is the xi position that gets passed back
    var i_length = xi.length;

    // go through available xi values
    for (var i = 0; i < xi.length; i++) {
        // if it's 1 (available), allocate it for use and break
        if (xi[i] == 1) {
            xi[i] = 0;
            i_length = i;
            break;
        }
    }

    xi[i_length] = 0;       // set to 0 since it's now allocated for use
    // set up the request
    if (window.ActiveXObject) {
        try {
            xmlhttp[i_length] = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                xmlhttp[i_length] = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) { }
        }
    } else if (window.XMLHttpRequest) {
        xmlhttp[i_length] = new XMLHttpRequest();
    }
    return (i_length);
}



function stateChanged(id_Div, index) {
    if (xmlhttp[index].readyState == 4) {
        document.getElementById(id_Div).innerHTML = xmlhttp[index].responseText;
        xi[index] = 1;
        xmlhttp[index] = null;
    }
}


function stateChangedLabel(index) {
    if (xmlhttp[index].readyState == 4) {
        document.getElementById(g_lbl_ErrName).innerHTML = xmlhttp[index].responseText;
        xi[index] = 1;
        xmlhttp[index] = null;
        
        // se la pagina non ha errori fai l'effeto aggiornamento pagina
        if (document.getElementById(g_lbl_ErrName).innerHTML.length == 0)
            window.location.href = unescape(window.location.pathname);
    }
}

function stateChangedAlert(index) {
    if (xmlhttp[index].readyState == 4) {
        var s_Error = xmlhttp[index].responseText;
        xi[index] = 1;
        xmlhttp[index] = null;
        
        // se la pagina non ha errori fai l'effeto aggiornamento pagina
        if (s_Error.length == 0)
            window.location.href = unescape(window.location.pathname);
       /* else
            alert(s_Error);*/
    }
}




function CursorWait(obj) {
    obj.style.cursor = 'wait';
    document.body.style.cursor = 'wait';
    //document.body.style.cursor ="url(wait_m.cur)";
}

function CursorWaitExit() {
    document.body.style.cursor = 'default';
}

