var url;
var response;
var campi_ok = false;
var prefix   = "http://" + location.hostname + ":" + location.port + "/LinksProcuraWebProj/";
var prefix_1   = "http://" + location.hostname + ":" + location.port;
//var prefix   = "http://" + location.hostname + ":" + location.port;
//var prefix   = "";
//var prefix_r = "http://hqdemo:8080/";
//var prefix_r = "http://sv-procura-w1:8080/";
var prefix_r = "http://10.0.2.248:8080/";
var url_address = "";

/*
Questa funzione prende in input i nomi dei parametri e i valori da passare all'url_base;
Controlla che i campi siano valorizzati;
Restituisce l'url completo.
*/
function changecss(theClass, element, value){
    alert(theClass + " " + element + " " + value);
    var cssRules;
    if (document.all) {
        cssRules = 'rules';
    }
    else if (document.getElementById) {
        cssRules = 'cssRules';
    }
    for (var S = 0; S < document.styleSheets.length; S++) {
        for (var R = 0; R < document.styleSheets[S][cssRules].length; R++) {
            if (document.styleSheets[S][cssRules][R].selectorText == theClass) {
                document.styleSheets[S][cssRules][R].style[element] = value;
            }
        }        
    }
}

function creaUrl(nomi, valori, url_base) {
    url = url_base + "?";
    if (nomi.length == valori.length){
        for (var i=0; i<nomi.length; i++){
            //alert(document.getElementById(valori[i]).value);
            if (escape(document.getElementById(valori[i]).value) == ""){
                campi_ok=false;
		break;
            } else {
                campi_ok=true;
		url += nomi[i] + "=" + escape(document.getElementById(valori[i]).value);
		if (i != (nomi.length-1)) {
                    url += "&";
		}
            }
        }
    } else {
	alert("Campi non impostati");
    }
    return url, campi_ok;
}

function creaUrlByArray(nomi, valori, url_base) {
    url = url_base + "?";
    if (nomi.length == valori.length){
        for (var i=0; i<nomi.length; i++){
            if (valori[i] == ""){
                campi_ok=false;
		break;
            } else {
                campi_ok=true;
		url += nomi[i] + "=" + valori[i];
		if (i != (nomi.length-1)) {
                    url += "&";
		}
            }
        }
    } else {
	alert("Campi non impostati");
    }
    return url, campi_ok;
}

function creaUrlTable(nomi, id, valore, url_base) {
    url = url_base + "?" + nomi[0] + "=" + id + "&" + nomi[1] + "=" + valore;
    return url;
}

//Prende in input il nome del DIV, il valore da inserire e il colore del font(opzionale)
function valorizzaDIV(div, value, color) {
	var mdiv = document.getElementById(div);
	if (color == null || color == "") {
            mdiv.innerHTML = value;
	} else {
            mdiv.innerHTML = "<font color=\"" + color + "\">" + value + "</font>";
	}
}

function ajax(pageToCall, functionName, _nomi, _valori, output_vd, fontColor, prefix_page){
	if(prefix_page == null) prefix_page = prefix;
	var array_nomi   =   _nomi.split(',');
	var array_valori = _valori.split(',');
	url_base  = prefix_page + "pages/ajax_interaction/" + pageToCall + ".jsp";
	creaUrlByArray(array_nomi, array_valori, url_base);
	var msg_parsed;
    var urls_loaded = {} // request made cache
    if (!urls_loaded[url]) {
        var ai = new AJAXInteraction(url,function(req) {
            response = eval('(' + req.responseText + ')');
            var resultString = functionName();
            if (resultString != null) {
            	valorizzaDIV(output_vd, resultString, fontColor);
            }
            else functionName();
        })
    }
    ai.doGet();
    urls_loaded[url] = true;
}

function AJAXEngine(url) {
    var urls_loaded = {} // request made cache
    if (!urls_loaded[url]) {
        var ai = new AJAXInteraction(url,function(req) {
                                            alert(req.responseText);
                                                response = req.responseText;
                                         })
    }
    ai.doGet();
    urls_loaded[url] = true;
    return response;
}
//Core AJAX
function AJAXInteraction(url, callback) {
    var req = init();
    req.onreadystatechange = processRequest;
   
    function init() {
        if (window.XMLHttpRequest) {
            return new XMLHttpRequest();
        } else if (window.ActiveXObject) {
			var sVersions = [
				"MSXML2.XMLHttp.5.0", 
				"MSXML2.XMLHttp.4.0",
				"MSXML2.XMLHttp.3.0",
				"MSXML2.XMLHttp",
				"Microsoft.XMLHttp"
			];
			for(var i=0; i<sVersions.length; i++){
				try{
					var ret = new ActiveXObject(sVersions[i]);
					return ret;
				}
				catch(oException){}
			}
            //return new ActiveXObject("Microsoft.XMLHTTP");
        }
        alert("Il browser in uso č obsoleto. Aggiornarlo con uno pių recente");
    }
   
    function processRequest() {
        //alert('req.readyState ' + req.readyState + ' req.status ' + req.status);
        if (req.readyState == 4) {
            if (req.status == 200) {
            	if (callback) callback(req);
            }
        }
    }

    this.doGet = function() {
        req.open("GET", url, true);
        req.send(null);
    }
   
    this.doPost = function(body) {
        req.open("POST", url, true);
        req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        req.send(body);
    }
}



function coloraRow(id) {
	if (id != null && id.length > 0 && id[0].substring(1,1) == '{') {
		for (var i = 0; i < id.length; i++) {
			//alert("riga_"+id[i]);
			var mdiv = document.getElementById("riga_"+id[i]);
			mdiv.style.background = "red";
		}
	}     
}