/* Scommentare la riga corrispondente alla tecnologia server utilizzata */
var server_technology = "asp";
//var server_technology = "php";


/**
 * Funzione che istanzia un oggetto XMLHttpRequest usando un meccanismo cross browser.
 *
 * @return   restituisce un'istanza di XMLHttpRequest oppure il valore false in caso
 *           di errori.
 */

function getXMLHttpRequestInstance() {

    var xmlhttp;

    // Prova il metodo Microsoft usando la versione più recente:
    try {
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
        try {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (E) {
            xmlhttp = false;
        }
    }

    // Se non è stato possibile istanziare l'oggetto forse siamo
    // su Mozilla/FireFox o su un altro browser compatibile:
    if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
		//alert("mozilla!");
        try {
            xmlhttp = new XMLHttpRequest();
        } catch (e) {
            xmlhttp = false;
        }
    }

    // Restituisce infine l'oggetto:
    return xmlhttp;
}

/**
 * Funzione che sostituisce il contenuto HTML di un nodo della pagina.
 *
 * @param    nodeId ID del nodo
 * @param    html   codice HTML da sostituire a quello del nodo
 */
function updateContent(nodeId, html) {
   
    var node = document.getElementById(nodeId);
    if(null == node) {
		//alert("[ERRORE] L'elemento " + nodeId + " non esiste");
		return;
    }
    node.innerHTML = html;
    //node.style.visibility = "visible";
}



function LoadForm(nodeId, url, sync) {

	/*if (window.XMLHttpRequest) { // Mozilla, Safari, ...
		var xmlhttp = new XMLHttpRequest();
	} else if (window.ActiveXObject) { // IE
		var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}*/
	
	var xmlhttp = getXMLHttpRequestInstance();

    if(!xmlhttp) {
        alert("Il browser non supporta l'oggetto XMLHttpRequest");
        return false;
    }

    
	
    updateContent(nodeId,"");     


    if (sync == "undefined") {
	xmlhttp.open("GET", url,true);
    } else {
	xmlhttp.open("GET", url,false);
	//alert (sync);
    }
    
    
    xmlhttp.send("");
			//alert (xmlhttp.responseText);
            if (xmlhttp.status==200) {
                updateContent(nodeId, xmlhttp.responseText);
                //alert (xmlhttp.responseText);
            } else if (xmlhttp.status==404) {
                //alert("[ERRORE] l'URL "+url+"non esiste!");
                MsgBox("[ERRORE] l'URL "+url+"non esiste!");
            } else {
                //alert("[ERRORE] errore non gestito (" + xmlhttp.status + ")");
                MsgBox("[ERRORE] errore non gestito (" + xmlhttp.status + ")");
            }    

}








/*
* Creazione ComboBOX da query
* @param    ID			ID da assegnare all'oggetto creato
* @param    Query			Stringa contenente la query da inviare al server (primo valore=Value della combo, secondo valore=Testo)
* @param    elemento       ID dell'elemento della pagina che conterrà la combo
*/

function SQL2Combo(ID, query, elemento, default_id)
{

	query = escape(query);
	var xmlDoc = getXMLHttpRequestInstance();
	xmlDoc.open("GET" , "componenti/SQL2XML." + server_technology + "?query=" + query, false);
	xmlDoc.send("");
	
	
	var x = xmlDoc.responseXML.getElementsByTagName("row");
	//alert (x.lenght);
	
	document.getElementById(elemento).innerHTML="";
	
	var newEl = document.createElement("select");

	newEl.setAttribute("id",ID);
	newEl.setAttribute("name",ID);

	//alert(xmlDoc.responseText);

	var oOption = document.createElement("OPTION");
	oOption.text="Seleziona...";
	oOption.value=-1;
	 try {
	 		newEl.add(oOption,null); // standards compliant; doesn't work in IE
  		}
  	catch(ex) {
    		newEl.add(oOption); // IE only
  		}	
     default_id2 = "-1";
	for (i=0;i<x.length;i++)
	{
			var oOption = document.createElement("OPTION");
			oOption.setAttribute("id",ID+i);
			oOption.text=x[i].childNodes[1].firstChild.nodeValue;
			
			//alert(x[i].childNodes[1].firstChild.nodeValue);
			
			oOption.value=x[i].childNodes[0].firstChild.nodeValue;			
			if (default_id != "undefined") {
				if (x[i].childNodes[0].firstChild.nodeValue == default_id) {
					default_id2 = i;					
					//alert(default_id2);
				}			
			}			
	 try {
	 		newEl.add(oOption,null); // standards compliant; doesn't work in IE
  		}
  	catch(ex) {
    		newEl.add(oOption); // IE only
  		}		


	}	
	if (document.getElementById(ID)==null) {
		document.getElementById(elemento).appendChild(newEl);
		
	}
	//alert(newEl.selectedIndex);
	//alert(default_id2);
	if (default_id2 != "-1") {		
		document.getElementById(ID+default_id2).selected=true;
	}
	
}	




function SQL2Combo2(ID, query, elemento, default_id)
{

	query = escape(query);
	var xmlDoc = getXMLHttpRequestInstance();
	xmlDoc.open("GET" , "componenti/SQL2XML2." + server_technology + "?query=" + query, false);
	xmlDoc.send("");
	
	
	var x = xmlDoc.responseXML.getElementsByTagName("row");
	//alert (x.lenght);
	
	document.getElementById(elemento).innerHTML="";
	
	var newEl = document.createElement("select");

	newEl.setAttribute("id",ID);
	newEl.setAttribute("name",ID);

	//alert(xmlDoc.responseText);

	var oOption = document.createElement("OPTION");
	oOption.text="Seleziona...";
	oOption.value=-1;
	 try {
	 		newEl.add(oOption,null); // standards compliant; doesn't work in IE
  		}
  	catch(ex) {
    		newEl.add(oOption); // IE only
  		}	
     default_id2 = "-1";
	for (i=0;i<x.length;i++)
	{
			var oOption = document.createElement("OPTION");
			oOption.setAttribute("id",ID+i);
			oOption.text=x[i].childNodes[1].firstChild.nodeValue;
			
			//alert(x[i].childNodes[1].firstChild.nodeValue);
			
			oOption.value=x[i].childNodes[0].firstChild.nodeValue;			
			if (default_id != "undefined") {
				if (x[i].childNodes[0].firstChild.nodeValue == default_id) {
					default_id2 = i;					
					//alert(default_id2);
				}			
			}			
	 try {
	 		newEl.add(oOption,null); // standards compliant; doesn't work in IE
  		}
  	catch(ex) {
    		newEl.add(oOption); // IE only
  		}		


	}	
	if (document.getElementById(ID)==null) {
		document.getElementById(elemento).appendChild(newEl);
		
	}
	//alert(newEl.selectedIndex);
	//alert(default_id2);
	if (default_id2 != "-1") {		
		document.getElementById(ID+default_id2).selected=true;
	}
	
}	































function showSlide(nodeId, url) {
    var xmlhttp = getXMLHttpRequestInstance();
    if(!xmlhttp) {
        alert("Il browser non supporta l'oggetto XMLHttpRequest");
        return false;
    }
    //toggle_visibility('waitDiv', 1);
    
    updateContent(nodeId,"");
    //updateContent(nodeId,"<br><center><strong>...Caricamento in corso...</strong></center><br>");
    updateContent(nodeId,"<center><strong>...Caricamento in corso...</strong></center>");
    //updateContent(nodeId,"<br><center><img src='/img/await.gif'></center><br>");
    

    xmlhttp.open("GET", url,true);
    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4) {
            if (xmlhttp.status==200) {
                updateContent(nodeId, xmlhttp.responseText);
                //alert (xmlhttp.responseText);
            } else if (xmlhttp.status==404) {
                alert("[ERRORE] l'URL "+url+"non esiste!");
            } else {
                alert("[ERRORE] errore non gestito (" + xmlhttp.status + ")");
            }
        }
    }
    
    xmlhttp.send(null);
}





function showSlideS(nodeId, url) {
    var xmlhttp = getXMLHttpRequestInstance();
    if(!xmlhttp) {
        alert("Il browser non supporta l'oggetto XMLHttpRequest");
        return false;
    }
    //toggle_visibility('waitDiv', 1);
    
    updateContent(nodeId,"");
    //updateContent(nodeId,"<br><center><strong>...Caricamento in corso...</strong></center><br>");
    updateContent(nodeId,"<center><strong>...Caricamento in corso...</strong></center>");
    //updateContent(nodeId,"<br><center><img src='/img/await.gif'></center><br>");
    

    xmlhttp.open("GET", url,false);
    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4) {
            if (xmlhttp.status==200) {
                updateContent(nodeId, xmlhttp.responseText);
                //alert (xmlhttp.responseText);
            } else if (xmlhttp.status==404) {
                alert("[ERRORE] l'URL "+url+"non esiste!");
            } else {
                alert("[ERRORE] errore non gestito (" + xmlhttp.status + ")");
            }
        }
    }
    
    xmlhttp.send(null);
}







