// JavaScript Document
function xmlhttpPost(strURL,param,obj) {  
	var xmlHttpReq = false;   
	var self = this;    // Xhr per Mozilla/Safari/Ie7   
	if (window.XMLHttpRequest) {      
	     self.xmlHttpReq = new XMLHttpRequest();    
	}    // per tutte le altre versioni di IE   
	else if (window.ActiveXObject) {  
	      self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
	    }   
	self.xmlHttpReq.open('POST', strURL, true); 
	self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');  
	
	self.xmlHttpReq.onreadystatechange = function() {    
	if (self.xmlHttpReq.readyState == 4) {  
		 updatepage(obj,self.xmlHttpReq.responseText);   
	 }  
}    
self.xmlHttpReq.send(param);
return true;
}

function updatepage(obj,str){ 

	document.getElementById(obj).innerHTML = str;
		
}

/** 
*   INIZIALIZZAZIONE DI AJAX 
**/    
function ajaxInit() 
{ 
   // ======================================================================================== 
   // ============== Inizializzo la classe che permette di utilizzare l'AJAX =================
   // ======================================================================================== 
    
    
   if (window.XMLHttpRequest) { // Mozilla, Safari,... 
      httpReq = new XMLHttpRequest(); 
      if (httpReq.overrideMimeType) { 
         httpReq.overrideMimeType('text/xml'); 
      } 
   } else if (window.ActiveXObject) { // IE 
      try { 
         httpReq = new ActiveXObject("Msxml2.XMLHTTP"); 
      } catch (e) { 
         try { 
            httpReq = new ActiveXObject("Microsoft.XMLHTTP"); 
         } catch (e) {} 
      } 
   } 
   if (!httpReq) { 
      alert('Cannot create XMLHTTP instance'); 
      return false; 
   } 
   // ---------------------------------------------------------------------------------------- 
   // ============================ FINE INIZIALIZZAZIONE DI AJAX ============================= 
   // ---------------------------------------------------------------------------------------- 
} 

/** 
*   INVIO PARAMETRI VIA POST ATTRAVERSO AJAX 
**/ 
function SongOnAir(url,post) 
{ 
   // Inizializzo ajax 
   	ajaxInit();
   // ================================================= 
   // ========= Esecuzione della richiesta ============ 
   // ================================================= 
   
   httpReq.onreadystatechange = function() {
		if (httpReq.readyState == 4){ 
			if (httpReq.status == 200) { 
				var XmlObj = new XML.ObjTree();
				var Xml = XmlObj.parseXML(httpReq.responseText);
				var postResult = httpReq.responseText;
				head	= "";
				foto	= "";
				testo	= "";
				title	= "";
				sep		= "";
				if (Xml.canale.onair){
					if (Xml.canale.onair["-foto"]){
						foto	= Xml.canale.onair["-foto"];
					}
					if (Xml.canale.onair["-titolo"]){
						title	= Xml.canale.onair["-titolo"];
					}
					if (Xml.canale.onair["-testo"]){
						testo	= Xml.canale.onair["-testo"];
					}
					sep		= "-";
				}
				else{
					head	= "";
					foto	= "";
					title	= "";
					testo	= "";
					sep		= "";
				}
				
				myText	= "<table width='100%' border='0' cellspacing='0' cellpadding='0'><tr><td align='center'><img src='"+foto+"' style='width: 250px;' alt='"+title+"' /></td></tr><tr><td><div style='text-align: center'>"+ testo +"</div></td></tr></table>";
				
			
				updatepage("songonair",myText);
			}
		}
    }

   httpReq.open('POST', url, true); //envoi POST 
   httpReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 
   httpReq.setRequestHeader("Content-length", post.length); // taille du post envoyé 
   httpReq.setRequestHeader("Connection", "close"); 
   httpReq.send(post);
   
   
   the_timeout = setTimeout("SongOnAir('onairxml.asp','action=song')",60000);

   // ------------------------------------------------- 
   // ====== Fine Esecuzione della richiesta ========== 
   // ------------------------------------------------- 
    
   return false; // Impedimento del refresh della pagina
}


/** 
*   INVIO PARAMETRI VIA POST ATTRAVERSO AJAX 
**/ 
function inviaPost(url,post,destination) 
{
   // Inizializzo ajax 
   ajaxInit(); 
    
   // ================================================= 
   // ========= Esecuzione della richiesta ============ 
   // ================================================= 
    
   httpReq.onreadystatechange = function() {
		if (httpReq.readyState == 4){ 
			if (httpReq.status == 200) { 
				var postResult = httpReq.responseText;
				updatepage(destination,postResult);
			}
		}
		else{
			$(destination).innerHTML='<div align="center" style="margin-top:20px;"><img src="/img/ajax-loader.gif" /></div>';
		}
    }

   httpReq.open('POST', url, true); //envoi POST 
   httpReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 
   httpReq.setRequestHeader("Content-length", post.length); // taille du post envoyé 
   httpReq.setRequestHeader("Connection", "close"); 
   httpReq.send(post); 

   // ------------------------------------------------- 
   // ====== Fine Esecuzione della richiesta ========== 
   // ------------------------------------------------- 
    
   return false; // Impedimento del refresh della pagina
}

/** 
*   RISULTATO DI RITORNO DALLA RICHIESTA 
**/ 
function resultatPost() { 
   // ============================================================ 
   // ========== testo sull'esecuzione della richiesta =========== 
   // ============================================================ 

   if (httpReq.readyState == 4) { 
      if (httpReq.status == 200) { 
         var postResult = httpReq.responseText; 
		 document.getElementById(target).innerHTML = postResult; // scrittura del risultato 
      } else { 
         alert("E' sorto un problema durante la richiesta"); 
      } 
   } 

   // ------------------------------------------------------------ 
   // ======== Fine testo sull'esecuzione della richiesta ======== 
   // ------------------------------------------------------------ 
}

function handleErrFullPage(strIn) {

        var errorWin;

        // Create new window and display error
        try {
                errorWin = window.open('', 'errorWin');
                errorWin.document.body.innerHTML = strIn;
        }
        // If pop-up gets blocked, inform user
        catch(e) {
                alert('An error occurred, but the error message cannot be' +
                        ' displayed because of your browser\'s pop-up blocker.\n' +
                        'Please allow pop-ups from this Web site.');
        }
}