var IFrameObj; // our IFrame object

//alert(navigator.userAgent);

//-------------------------------------------------------------------------
function callRPC() 
{
	if (!document.createElement) {return true};
	var IFrameDoc;
	// var URL = 'prrpc.html';
	if (!IFrameObj && document.createElement) {
		// create the IFrame and assign a reference to the
		// object to our global variable IFrameObj.
		// this will only happen the first time 
		// callToServer() is called
		try {
			var tempIFrame=document.createElement('iframe');
			tempIFrame.setAttribute('id','RSIFrame');
			tempIFrame.style.border='0px';
			tempIFrame.style.width='0px';
			tempIFrame.style.height='0px';
			IFrameObj = document.body.appendChild(tempIFrame);
			
			if (document.frames) {
				// this is for IE5 Mac, because it will only
				// allow access to the document object
				// of the IFrame if we access it through
				// the document.frames array
				IFrameObj = document.frames['RSIFrame'];
			}
		} catch(exception) {
			// This is for IE5 PC, which does not allow dynamic creation
			// and manipulation of an iframe object. Instead, we'll fake
			// it up by creating our own objects.
			iframeHTML='<iframe id="RSIFrame" style="';
			iframeHTML+='border:0px;';
			iframeHTML+='width:0px;';
			iframeHTML+='height:0px;';
			iframeHTML+='"><\/iframe>';
			document.body.innerHTML+=iframeHTML;
			IFrameObj = new Object();
			IFrameObj.document = new Object();
			IFrameObj.document.location = new Object();
			IFrameObj.document.location.iframe = document.getElementById('RSIFrame');
			IFrameObj.document.location.replace = function(location) {
				this.iframe.src = location;
			}
		}
	}
	
	if (navigator.userAgent.indexOf('Gecko') !=-1 && !IFrameObj.contentDocument) {
		// we have to give NS6 a fraction of a second
		// to recognize the new IFrame
		setTimeout("callRPC(arguments)",10);
		return false;
	}
	
	if (IFrameObj.contentDocument) {
		// For NS6
		IFrameDoc = IFrameObj.contentDocument; 
	} else if (IFrameObj.contentWindow) {
		// For IE5.5 and IE6
		IFrameDoc = IFrameObj.contentWindow.document;
	} else if (IFrameObj.document) {
		// For IE5
		IFrameDoc = IFrameObj.document;
	} else {
		return true;
	}
	if (document.location.href.indexOf('?') > -1)
		qoe = '&';
	else
		qoe = '?';
	var qstr = document.location.href + qoe + 'rpccall=1' + '&funct=' + escape(arguments[0]);
	
	for (var i = 1; i < arguments.length; i++)
		{
		if (arguments[i] == '')
			qstr += '&prms[' + (i - 1) + ']=__rpc_empty_value__';
		else
			qstr += '&prms[' + (i - 1) + ']=' + escape(arguments[i]);
		}
	try 
		{
		IFrameDoc.location.replace(qstr);
		}
	catch (exception)
		{
		alert("ATTENZIONE: per qualche motivo non riesco" +
				" a chiamare una funzione di controllo." + 
				" Alcune funzionalita' non sono quindi disponibili.\n\n" +
				exception);
		}
	
	return false;
}
	
//-------------------------------------------------------------------------
function handleResponse(str) {
	alert(str);
}
//-------------------------------------------------------------------------
function showRPCErr(msg) {
	alert(msg);
}


//-------------------------------------------------------------------------
//-------------------------------------------------------------------------
//-------------------------------------------------------------------------
var waform_rpc_error = 'waform_rpc_error';
var http_request = false;

//-------------------------------------------------------------------------
function getHttpRequestObj()
	{
	var hr = false;
	if (window.XMLHttpRequest) 
		{ 
		// Mozilla, Safari,...
		hr = new XMLHttpRequest();
		if (hr.overrideMimeType) 
			hr.overrideMimeType('text/xml');
		}
	else if (window.ActiveXObject) 
		{ 
		// IE
		try 
			{
			hr = new ActiveXObject("Msxml2.XMLHTTP");
			}
		catch (e) 
			{
			try 
				{
				hr = new ActiveXObject("Microsoft.XMLHTTP");
				}
			catch (e) 
				{}
			}			
		}
	
	return hr;
	}

//-------------------------------------------------------------------------
function waform_showRPCError(msg)
	{
	alert(msg);
	return waform_rpc_error;
	}
//-------------------------------------------------------------------------
function waform_callRPCX()
	{
	http_request = getHttpRequestObj();
	
	if (!http_request) 
		return alert("Impossibile creare l'istanza RPC");

	if (document.location.href.indexOf('?') > -1)
		qoe = '&';
	else
		qoe = '?';
	var qstr = document.location.href + qoe + 'rpccallx=1' + '&funct=' + escape(arguments[0]);
	for (var i = 1; i < arguments.length; i++)
		{
		if (arguments[i] == '')
			qstr += '&prms[' + (i - 1) + ']=__rpc_empty_value__';
		else
			qstr += '&prms[' + (i - 1) + ']=' + escape(arguments[i]);
		}

	try
		{
		http_request.open('GET', qstr, false);
		http_request.send(null);
		
		// verifica eventuali errori di sistema
		if (http_request.status != 200) 
			return waform_showRPCError("Errore durante chiamata RPC: " + http_request.status);
		if (http_request.responseText == '__rpc_not_found__')
			return waform_showRPCError("Funzione RPC non trovata: " + arguments[0]);
		if (http_request.responseText == '__rpc_db_error__')
			return waform_showRPCError("Errore DB durante chiamata RPC");
			
		// estrazione dell'esito
		if (http_request.responseText == '__rpc_false__')
			return false;
		if (http_request.responseText == '__rpc_true__')
			return true;
		if (http_request.responseText.indexOf('|||'))
			{
			var toret = http_request.responseText.split("|||");
			for (var i = 0; i < toret.length; i++)
				{
				if (toret[i] == '__rpc_false__')
					toret[i] = false;
				else if (toret[i] == '__rpc_true__')
					toret[i] = true;
				}
			return toret;
			}
	
		return http_request.responseText;
		}
	catch(exception)
		{
		return waform_showRPCError("Errore durante chiamata RPC: " + exception);
		}
	}


