
/*******************************************************************
 *
 * funzioni per inserire del codice HTML
*
 ********************************************************************/
function insertHTMLCode(targetID, newdiv_id, strcode)
{
	if (strcode) {
		// la stringa da inserire deve essere stata specificata
	
		var newDiv = document.getElementById(newdiv_id);
		
		if (!newDiv) {
			// le operazioni avvengono solo se se l'identificativo non e' gia' presente 
			var targetDiv = document.getElementById(targetID);
			if (targetDiv) {
				targetDiv.innerHTML += "<div id='" + newdiv_id + "'>";
				//targetDiv.innerHTML += strcode;
				targetDiv.innerHTML += "</div>\n";
			}
			var newDiv = document.getElementById(newdiv_id);
			if (newDiv)
			{
				newDiv.innerHTML = strcode;
			}
		}
	}
}

// rimuove un elemento inserito
function removeHTMLCode(targetID, olddiv_id)
{
	var oldDiv = document.getElementById(olddiv_id);
	if (oldDiv)
	{
		oldDiv.parentNode.removeChild(oldDiv);
	}
}




/*******************************************************************
 *
 * classe per inserire a runtime del codice HTML ricavato via AJAX
 *
 ********************************************************************/

function cIncludeHTMLCode() {
	this.targetID        = null;  // ID del <div /> in cui inserire il codice
	this.newdiv_id       = null;  // ID del <div /> specifico che conterra' il codice inserito
	this.callback        = null;  // funzione di callback da chiamare dopo che e' avvenuta l'inclusione del codice
	this.callback_param  = null;  // eventuali parametri da passare al callback
	
	
  	// chiama, se indicata, la funzione di callback
  	this.callingCallback = function()
  	{
		if (this.callback)
		{
			if (this.callback_param)
				this.callback(this.callback_param);
			else
				this.callback();
		}
  	}
	
	// XMLHttpRequest callback
	this.clbk_read = function(str)
	{
		document.body.style.cursor = 'default';
	
		if ((typeof(str) == "string") && (str.indexOf(preAJAXError) != 0))
		{
			insertHTMLCode(this.targetID, this.newdiv_id, str);
		}
		
		this.callingCallback();
	}
	
	// funzione che richiama callxhttp (XMLHttpRequest) per ottenere il 
	// contenuto del file
	this.read = function(filename) 
	{
		var param = '&filename=' + encodeURIComponent(filename);
		var url = getBaseURL() + "/../inner/" + filename;
		
		document.body.style.cursor = 'wait';
		callxhttp(url, this, this.clbk_read, param);
	}
  	
  	
  	/******************************************************************  
  	 * funzione per l'inclusione di un file
  	 *
  	 * La funzione si attende:
	 * targetID = ID del <div /> in cui effettuare l'inclusione
	 * newdiv_id: indicativo che avra' il <div /> all'interno del quale sara' posto il codice.                                          
	 * filename: filename da cui includere il codice,
	 * callback: funzione di callbak che sara' chiamata dopo l'inclusione del codice 
	 *
	 * Nel caso che esista gia un elemento con l'indicativo 'newdiv_id'
	 * non sara' effettuata la lettura del file e sara' chiamato il callback
	 *
     ******************************************************************/
	this.include = function(targetID, newdiv_id, filename, callback)
	{
		this.targetID  = targetID;
		this.newdiv_id = newdiv_id;
		this.callback  = callback;
		
		if (arguments.length > 4)
			this.callback_param = arguments[4];
		else
			this.callback_param = null;
			
		// se esiste un tag con l'identificativo indicato si chiama direttamente la funzione callback
		if (document.getElementById(newdiv_id)) 
		{
			this.callingCallback();
		}
		else 
		{
			this.read(filename);  // da un errore
		}
	}
}

