// JavaScript Document
var currobj = null
var ricerca_pro;
var ajaxReq;
var veccopen = new Array();

function prendiElementoDaId(id_elemento)
{
	var elemento;
	if(document.getElementById)
		elemento = document.getElementById(id_elemento);
	else
		elemento = document.all[id_elemento];
		
	return elemento;
}

function getExternalData(pForcedLOAD, pForm, pObj, pPag)
{
	// settol'oggeto di ritorno
	currobj = prendiElementoDaId(pObj);
	
	var tmpMethod = "get";
	if(pForm!=null)
		tmpMethod = "post";
	
	if(pForcedLOAD || currobj.innerHTML.toLowerCase() == "<br>" || currobj.innerHTML.toLowerCase() == "")
	{
		if(!pForcedLOAD)
			currobj.innerHTML = "<br><div align='center'>.. loading data ..<br><br></div>";

		// per l'oggetto nativo XMLHttpRequest
		if (window.XMLHttpRequest)
		{
			ricerca_pro = new XMLHttpRequest();
			ricerca_pro.onreadystatechange = ricevi;
			ricerca_pro.open(tmpMethod, pPag, true);
			if(tmpMethod == "get")
			{
				ricerca_pro.send(null);
			}
			else
			{
				var tmpQS = getElementFRForm(prendiElementoDaId(pForm));
				ricerca_pro.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
				ricerca_pro.setRequestHeader("Content-length", tmpQS.length);
				ricerca_pro.setRequestHeader("Connection", "close");
				ricerca_pro.send(tmpQS);
			}
			// per IE 
		}
		else if (window.ActiveXObject)
		{
			ricerca_pro = new ActiveXObject("Microsoft.XMLHTTP");
			if (ricerca_pro)
			{
				ricerca_pro.onreadystatechange = ricevi;
				ricerca_pro.open(tmpMethod, pPag, true);
				if(tmpMethod == "get")
				{
					ricerca_pro.send();
				}
				else
				{
					var tmpQS = getElementFRForm(prendiElementoDaId(pForm));
					ricerca_pro.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
					ricerca_pro.setRequestHeader("Content-length", tmpQS.length);
					ricerca_pro.setRequestHeader("Connection", "close");
					ricerca_pro.send(tmpQS);
				}
			}
		}
	}
	else
	{
		currobj.innerHTML = "<br>";
		return;
	}
}

function getElementFRForm(pForm)
{
	getElementFRForm(pForm, true);
}

function getElementFRForm(pForm, excludeLast)
{
	var tmpStr = "";
	var plen = pForm.elements.length-1;
	if (excludeLast) {
		plen--;
	}
	for(x = 0; x <= plen; x++) 
	{
		tmpStr = tmpStr + pForm.elements[x].name + "=" + escape(pForm.elements[x].value);
		if(x<plen)
			tmpStr = tmpStr + "&";
	}
	return tmpStr;
}

function ricevi()
{ 
	var strRes; 
	var arrValori; 
	if (ricerca_pro.readyState == 4)
	{
		strRes=ricerca_pro.responseText;
		currobj.innerHTML = strRes;
	}
}

function ajaxLoadUrl(method, url, form, returnFunction) {
		// per l'oggetto nativo XMLHttpRequest
		if (window.XMLHttpRequest)
		{
			ajaxReq = new XMLHttpRequest();
			ajaxReq.onreadystatechange = returnFunction;
			ajaxReq.open(method, url, true);
			if(method == "get")
			{
				ajaxReq.send(null);
			}
			else
			{
				var tmpQS = getElementFRForm(form);
				ajaxReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
				ajaxReq.setRequestHeader("Content-length", tmpQS.length);
				ajaxReq.setRequestHeader("Connection", "close");
				ajaxReq.send(tmpQS);
			}
			// per IE 
		}
		else if (window.ActiveXObject)
		{
			ajaxReq = new ActiveXObject("Microsoft.XMLHTTP");
			if (ajaxReq)
			{
				ajaxReq.onreadystatechange = returnFunction;
				ajaxReq.open(method, url, true);
				if(method == "get")
				{
					ajaxReq.send();
				}
				else
				{
					var tmpQS = getElementFRForm(form, false);
					ajaxReq.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
					ajaxReq.setRequestHeader("Content-length", tmpQS.length);
					ajaxReq.setRequestHeader("Connection", "close");
					ajaxReq.send(tmpQS);
				}
			}
		}
}
