var xmlHttp;

function ProcessaDestinatari(Numeri)
{ 
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
 {
 alert ("Browser does not support HTTP Request");
 return;
 }

var url="ajaxCommand.php";
url=url+"?ajaxCommand=ProcessaDestinatari";
url=url+"&Numeri="+escape(Numeri);
url=url+"&sid="+Math.random();

 oggettoAjax='NumeriDestinatari';
xmlHttp.onreadystatechange=stateChangedInner;

xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}



function CosaInviare(str)
{
if (str=='PDF') 
 document.getElementById('Cosa').innerHTML='<td align=left><div onclick="CosaInviare(\'TXT\');" align=right><img name=TipoCosa src=images/PDFTiff.jpg onMouseOver="javascript:document.TipoCosa.src=\'images/TXT.jpg\';" onMouseOut="javascript:document.TipoCosa.src=\'images/PDFTiff.jpg\';" border=0 title="Digita Testo"></div>Allega File:&nbsp; (max 1MB) Solo <b>PDF o Tiff</b><br><input name="pdf" type="file" size="25" /></td>';
else  document.getElementById('Cosa').innerHTML='<td align=left valign=top><div onclick="CosaInviare(\'PDF\');" align=right><img name=TipoCosa src=images/TXT.jpg onMouseOver="javascript:document.TipoCosa.src=\'images/PDFTiff.jpg\';" onMouseOut="javascript:document.TipoCosa.src=\'images/TXT.jpg\';" border=0 title="Allegato PDF o Tiff"></div>Testo del Fax:<textarea name="txt"  rows=15 cols=55 />Inserisci qui il testo da inviare via Fax</textarea></td>';
				
}






function stateChangedValue() 
{
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
 {
 document.getElementById(oggettoAjax).value=xmlHttp.responseText;
 } 
}

function stateChangedInner() 
{ //alert (xmlHttp.responseText);
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
 {if (xmlHttp.responseText.substr(0,1)>0)  {
 		 			document.emailform.Invia.disabled=false;
  }
 else   { document.emailform.Invia.disabled=true;
  }
//  document.getElementById(oggettoAjax).innerHTML=xmlHttp.responseText;
   document.getElementById(oggettoAjax).innerHTML=xmlHttp.responseText.substr(1);
 } 
}


function GetXmlHttpObject()
{
var xmlHttp=null;
try
 {
 // Firefox, Opera 8.0+, Safari
 xmlHttp=new XMLHttpRequest();
 }
catch (e)
 {
 //Internet Explorer
 try
  {
  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  }
 catch (e)
  {
  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
 }
return xmlHttp;
}


/**
 * Determines when a request is considered "timed out"
 */
var timeOutMS = 5000; //ms
 
/**
 * Stores a queue of AJAX events to process
 */
var ajaxList = new Array();

/**
 * Initiates a new AJAX command
 *
 * @param   the url to access
 * @param   the document ID to fill, or a function to call with response XML (optional)
 * @param	true to repeat this call indefinitely (optional)
 * @param   a URL encoded string to be submitted as POST data (optional)
 */
function newAJAXCommand(url, container, repeat, data)
{
	// Set up our object
	var newAjax = new Object();
	var theTimer = new Date();
	newAjax.url = url;
	newAjax.container = container;
	newAjax.repeat = repeat;
	newAjax.ajaxReq = null;
	
	// Create and send the request
	if(window.XMLHttpRequest) {
        newAjax.ajaxReq = new XMLHttpRequest();
        newAjax.ajaxReq.open((data==null)?"GET":"POST", newAjax.url, true);
        newAjax.ajaxReq.send(data);
    // If we're using IE6 style (maybe 5.5 compatible too)
    } else if(window.ActiveXObject) {
        newAjax.ajaxReq = new ActiveXObject("Microsoft.XMLHTTP");
        if(newAjax.ajaxReq) {
            newAjax.ajaxReq.open((data==null)?"GET":"POST", newAjax.url, true);
            newAjax.ajaxReq.send(data);
        }
    }
    
    newAjax.lastCalled = theTimer.getTime();
    
    // Store in our array
    ajaxList.push(newAjax);
}

/**
 * Loops over all pending AJAX events to determine
 * if any action is required
 */
function pollAJAX() {
	
	var curAjax = new Object();
	var theTimer = new Date();
	var elapsed;
	
	// Read off the ajaxList objects one by one
	for(i = ajaxList.length; i > 0; i--)
	{
		curAjax = ajaxList.shift();
		if(!curAjax)
			continue;
		elapsed = theTimer.getTime() - curAjax.lastCalled;
				
		// If we suceeded
		if(curAjax.ajaxReq.readyState == 4 && curAjax.ajaxReq.status == 200) {
			// If it has a container, write the result
			if(typeof(curAjax.container) == 'function'){
				curAjax.container(curAjax.ajaxReq.responseXML.documentElement);
			} else if(typeof(curAjax.container) == 'string') {
				document.getElementById(curAjax.container).innerHTML = curAjax.ajaxReq.responseText;
			} // (otherwise do nothing for null values)
			
	    	curAjax.ajaxReq.abort();
	    	curAjax.ajaxReq = null;

			// If it's a repeatable request, then do so
			if(curAjax.repeat)
				newAJAXCommand(curAjax.url, curAjax.container, curAjax.repeat);
			continue;
		}
		
		// If we've waited over 1 second, then we timed out
		if(elapsed > timeOutMS) {
			// Invoke the user function with null input
			if(typeof(curAjax.container) == 'function'){
				curAjax.container(null);
			} else {
				// Alert the user
				alert("Command failed.\nConnection to development board was lost.");
			}

	    	curAjax.ajaxReq.abort();
	    	curAjax.ajaxReq = null;
			
			// If it's a repeatable request, then do so
			if(curAjax.repeat)
				newAJAXCommand(curAjax.url, curAjax.container, curAjax.repeat);
			continue;
		}
		
		// Otherwise, just keep waiting
		ajaxList.push(curAjax);
	}
	
	// Call ourselves again in 10ms
	setTimeout("pollAJAX()",10);
	
}// End pollAjax
			
/**
 * Parses the xmlResponse returned by an XMLHTTPRequest object
 *
 * @param	the xmlData returned
 * @param	the field to search for
 */
function getXMLValue(xmlData, field) {
	try {
		if(xmlData.getElementsByTagName(field)[0].firstChild.nodeValue)
			return xmlData.getElementsByTagName(field)[0].firstChild.nodeValue;
		else
			return null;
	} catch(err) { return null; }
}

//kick off the AJAX Updater
//setTimeout("pollAJAX()",500);

