//-------------------------------------------------------- 
// Gestione Ajax
//--------------------------------------------------------
// id del paragrafo per comunicazione Ajax
var SHOW_PAGES_PARAGRAPH = "showPages";

function getNode( nodeId ) 
{
  if( document.getElementById )
	return document.getElementById( nodeId );
  else if( document.all && document.all( nodeId ) )
	return document.all( nodeId );
  else if( document.layers && document.layers[ nodeId ] )
	return document.layers[ nodeId ];
  else
	return false;
}

function createXmlHttpRequest()
{	
   if ( window.ActiveXObject )
     xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
   else
     xmlHttp = new XMLHttpRequest();
      
   return( xmlHttp );
}

function callServer( getpost, urlToCall, resultHandler, queryString )
{
  var tmst;
  var today;
  var urlToCallNoCache;
  var queryStringNoCache;
  
  if ( createXmlHttpRequest() != null )
    {
      // accodo una parametro fittizio per evitare il caching
	  today = new Date();
	  tmst = "XYTMST=" + today.getTime();
	  
	  if ( getpost == "POST" )
	    {
		  urlToCallNoCache = urlToCall;
		  
		  if ( queryString == null )
		    queryStringNoCache = tmst;
		  else	
		    queryStringNoCache = queryString + "&" + tmst;
		}
	  else
	    {
		  if ( urlToCall.indexOf("?") > 0 )
            urlToCallNoCache = urlToCall + "&" + tmst;
          else	
		    urlToCallNoCache = urlToCall + "?" + tmst;
		  
		  queryStringNoCache = queryString;	
		}
	 // if ( getpost == "POST" )		
     //   alert( "Open POST NoCache " + urlToCallNoCache );
		
	  xmlHttp.open( getpost, urlToCallNoCache, true);
	   
	  if (resultHandler != null)
	    {
          //alert( "Set ResultHandler");
		  xmlHttp.onreadystatechange = resultHandler;
		} 
      else
	    { 
          //alert( "Set GENERIC ResultHandler");
		  xmlHttp.onreadystatechange = genericResultHandler;  
		}  
      
      if ( getpost == "POST" )
	    {
		 // alert("Set Content-Type");
          xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");

   	      //alert(queryString);
		  xmlHttp.send(queryStringNoCache);	
		}  
	  else
	    {	
	      //alert("Send NULL");
		  xmlHttp.send(null);
		}  
    }  
  else
     alert("Errore XmlHttpRequest");  
}

function genericResultHandler()
{
  var elP;
  
  if ( xmlHttp.readyState == 4 )
    {
      if ( xmlHttp.status == 200 )
        {
          // modifico innerHTML per id=showPages
          elP = getNode(SHOW_PAGES_PARAGRAPH);
          if ( elP )
		    {
              //alert(xmlHttp.responseText);
			  elP.innerHTML = xmlHttp.responseText;
			}  
          else  
            alert("KO !");  
        }
      else
        {
          alert("Errore nella comunicazione con il server (status=" + xmlHttp.status + ")" );
        } 
    }
}

function xmlResultHandler()
{
  var elP;
  var htmlData;
  var messageData;
  var mex;
  var j;
  
  if ( xmlHttp.readyState == 4 )
    {
      if ( xmlHttp.status == 200 )
        {
          // modifico innerHTML per id=showPages
          elP = getNode(SHOW_PAGES_PARAGRAPH);
          if ( elP )
		    {
			  // HTML da sostituire sul nodo SHOW_PAGES_PARAGRAPH
			  htmlData = extract_HTMLDATA_FromXML( xmlHttp );
			  elP.innerHTML = htmlData;
			  // eventuale testo di un messaggio da mostare
			  messageData = extract_MESSAGE_FromXML( xmlHttp );
			  if ( messageData != "" )
			     alert( messageData );
			  // eventuale messaggio di debug
			  debugData = extract_DEBUGINFO_FromXML( xmlHttp );
			  if ( debugData != "" )
  			    showDebugMex( debugData );				 
			}  
          else  
            alert("KO !");  
        }
      else
        {
          alert("Errore nella comunicazione con il server (status=" + xmlHttp.status + ")" );
        } 
    }
}

function openChildWindow( urlToCall, params )
{
	var childWnd;
	var showParms;
	
    if (params != null )
	  showParms = params;
	else
	  showParms = "width=800,height=600,scrollbars=yes,resizable=yes";
	  
	//alert( urlToCall );
	childWnd = window.open(urlToCall, "", showParms);
	
}

//----------------------------------------------------------
// Metodi per l'estrazione delle risposte XML via Ajax
//----------------------------------------------------------
function extract_HTMLDATA_FromXML( xmlHttp )
{
  var retStr = "";
  var  htmlData;
  
  htmlData = xmlHttp.responseXML.getElementsByTagName("htmldata");
  if ( getDetectedBrowser() == BROWSER_MSIE )
	{ 
	  if (htmlData[0].firstChild != null )
	    retStr = htmlData[0].firstChild.nodeValue;
	} 
  else
    {	
	  if ( htmlData[0].childNodes[1] != null )
	    retStr = htmlData[0].childNodes[1].nodeValue;
	}   
  	
  return( retStr );		
}

function extract_MESSAGE_FromXML( xmlHttp )
{
  var retStr = "";
  var  messageData;
  
  messageData = xmlHttp.responseXML.getElementsByTagName("message");
  if ( getDetectedBrowser() == BROWSER_MSIE )
    {
      if ( messageData[0].firstChild != null )
		 retStr = messageData[0].firstChild.nodeValue;
	}
  else
    {
      if ( messageData[0].childNodes[0] != null )
		 retStr = messageData[0].childNodes[0].nodeValue;
	}	 
  
  // Sembra che il primo carattere sia un \n anche se il messaggio è vuoto.
  // Aggiungo un controllo per sicurezza.
  if ( (retStr.length < 2) && retStr.charAt(0) == "\n" )
	 retStr = "";
	
  return( retStr );	
}

function extract_DEBUGINFO_FromXML( xmlHttp )
{
  var retStr = "";
  var  debugData;
  
  debugData = xmlHttp.responseXML.getElementsByTagName("debuginfo");

  if ( getDetectedBrowser() == BROWSER_MSIE )
    retStr = debugData[0].firstChild.nodeValue;
  else	
	retStr = debugData[0].childNodes[1].nodeValue;

  // dato che il tag <debuginfo> è definito come CDATA, trovo sempre
  // almeno uno o due \n (in dipendenza dal fatto che apertura e chiusura del CDATA
  // stiano sulla stessa riga o meno). 
  // Metto un controllo sulla lunghezza per evitare di far vedere finestre vuote
  //alert(">" + retStr + "<" + retStr.length);
  
  if ( retStr.length < 2 )	
    retStr = "";
	
  return( retStr );		
}

// funzioni di trim di una stringa
function trim(stringToTrim) 
{
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

function ltrim(stringToTrim) 
{
	return stringToTrim.replace(/^\s+/,"");
}

function rtrim(stringToTrim) 
{
	return stringToTrim.replace(/\s+$/,"");
}

