/**  Variabile che fungera' da store per oggetti XMLHttpRequest **/
var xmlhttp;
var caphttp;
/** Variabile che servira' ad indirizzare il container di destinazione di molti contenuti **/
var container="container";
function doubleQuotesPurifier(str){
	return str.replace(/D_QUOTE/gi,"\"");
}
/** Metodo eseguito quando lo stato dell'oggetto condiviso xmlhttp (di tipo XMLHttpObject) cambia **/
function stateChanged() {
	if (xmlhttp.readyState==4) {
		document.getElementById(container).innerHTML=xmlhttp.responseText;
	}
	var rightnow = new Date();
	document.getElementById("captcha").src="include/captcha.php?s="+rightnow.getTime();
}
function commercialAndReplace(str){
	return str.replace(/&/gi,"C_AND");
}
/**	Get the right HTTPObject depending on the executing browser
*	@return An object representing an HTTPObject
*	@type	HTTPObject
**/
function GetXmlHttpObject() {
	if (window.XMLHttpRequest) {
		// code for IE7+, Firefox, Chrome, Opera, Safari
		return new XMLHttpRequest();
	}
	if (window.ActiveXObject) {
		// code for IE6, IE5
		return new ActiveXObject("Microsoft.XMLHTTP");
	}
}
/** Metodo che permette di ottenere tutti gli elementi facenti parte di una stessa classe css
 * @param searchClass Il nome della classe da cercare
 * @param node Eventuale nodo di partenza da cui iniziare a cercare
 * @param tag Eventuale tag in cui cercare
 * @return un array di nodi facenti parte della classe css scelta
 */
function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node === null ){
		node = document;
	}
	if ( tag === null ){
		tag = '*';
	}
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	var i;
	var j;
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}
/** Metodo che permette di trovare l'indice di un elemento in una select list; ovviamente gli elementi debbono essere non ripetuti
 * @param id Id della lista
 * @param value Valore di cui prelevare l'indice
 * @return L'indice del valore nella lista
 */
function findIndexInSelectList(id, value){
	var list = document.getElementById(id);
	for(var i=0;i<list.length;i++){
		if(list.options[i].value==value){
			return i;
		}
	}
}
/** Metodo che permette di selezionare una sezione e di visualizzarne la lista degli argomenti
 * @param str Identificativo della stringa
 */
function selectSection(str) {
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp===null) {
		alert ("Browser does not support HTTP Request");
		return;
	}
	var url="include/ajax_section_handler.php";
	url=url+"?sez="+str;
	container = "container";
	xmlhttp.onreadystatechange = stateChanged; 
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
}
/** Metodo che permette di nascondere un nodo e tutti i suoi nodi discendenti
 * @param node Nodo da far sparire
 */
function hide(node){
	node.style.display='none';
	var childs = getElementsByClass(node.id, null,null);
	if(childs.length>0){
		for(var i=0;i<childs.length;i++){
			if((typeof(childs[i]))!="undefined"){
				childs[i].style.display='none';
			}
		}
		for(var y=0;y<childs.length;y++){
			if((typeof(childs[y]))!="undefined"){
				hide(childs[y]);
			}
		}
	}
}
/** Metodo che permette di far apparire e scomparire un elemento
 * @param elemName L'elemento da far apparire o scomparire
 */
function toggle(elemName){
	selectSection(elemName.replace(/sezione_/, ""));
	var elem = getElementsByClass(elemName,null,null);
	for(var i=0;i<elem.length;i++){
		var elemState=elem[i].style.display;
		if(elemState=='block'){
			if(elemName!="sezione_1"){
				hide(elem[i]);
			}
		}
		else {
			elem[i].style.display='block';
		}
	}
}
/** Metodo che permette di inserire una nuova sezione o di modificarne una gia' esistente */
function insertSection(){
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp===null) {
		alert ("Browser does not support HTTP Request");
		return;
	}
	var url="include/ajax_section_insert_handler.php";
	if(document.getElementById("new-sez-id").value===""){
		url=url+"?intention=insert"+"&sez="+commercialAndReplace(document.getElementById("new-sez-name").value)+"&sez_assoc="+commercialAndReplace(document.getElementById("new-sez-assoc-select").options[document.getElementById("new-sez-assoc-select").selectedIndex].value);
	}
	else{
		url=url+"?intention=modify"+"&id="+document.getElementById("new-sez-id").value+"&sez="+commercialAndReplace(document.getElementById("new-sez-name").value)+"&sez_assoc="+commercialAndReplace(document.getElementById("new-sez-assoc-select").options[document.getElementById("new-sez-assoc-select").selectedIndex].value);	
	}
	container = "menu";
	xmlhttp.onreadystatechange = stateChanged;
	document.getElementById("new-sez-name").value="";
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);	
}
/** Metodo che permette di inserire un nuovo articolo o di modificarne uno gia' esistenze */
function insertArgument(){
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp===null) {
		alert ("Browser does not support HTTP Request");
		return;
	}
	var url="include/ajax_article_insert_handler.php";
	var data;
	if(document.getElementById("new-id-arg").value===""){
		data="intention=insert"+"&arg_auth="+commercialAndReplace(document.getElementById("new-autore-argomento").value)+"&arg_titolo="+commercialAndReplace(document.getElementById("new-titolo-argomento").value)+"&sez_assoc="+commercialAndReplace(document.getElementById("new-sez-assoc-argomento").options[document.getElementById("new-sez-assoc-argomento").selectedIndex].value)+"&arg_desc="+commercialAndReplace(document.getElementById("new-descrizione-argomento").value)+"&arg_testo="+commercialAndReplace(document.getElementById("new-testo-argomento").value);
	}
	else{
		data="intention=modify"+"&id_arg="+document.getElementById("new-id-arg").value+"&arg_auth="+commercialAndReplace(document.getElementById("new-autore-argomento").value)+"&arg_titolo="+commercialAndReplace(document.getElementById("new-titolo-argomento").value)+"&sez_assoc="+commercialAndReplace(document.getElementById("new-sez-assoc-argomento").options[document.getElementById("new-sez-assoc-argomento").selectedIndex].value)+"&arg_desc="+commercialAndReplace(document.getElementById("new-descrizione-argomento").value)+"&arg_testo="+commercialAndReplace(document.getElementById("new-testo-argomento").value);
	}
	container = "container";
	xmlhttp.onreadystatechange = stateChanged;
	document.getElementById("new-id-arg").value="";
	xmlhttp.open("POST",url,true);
	xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xmlhttp.send(data);
}
/** Metodo che permette di selezionare un Articolo e di visualizzarlo
 * @param str Identificativo dell'articolo
 */
function selectArticle(str) {
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp===null) {
		alert ("Browser does not support HTTP Request");
		return;
	}
	var url="include/ajax_article_handler.php";
	url=url+"?art="+str;
	container = "container";
	xmlhttp.onreadystatechange = stateChanged; 
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
}
/** Metodo che permette di inserire un nuovo commento */
function insertComment(){
	xmlhttp=GetXmlHttpObject();	
	if (xmlhttp===null) {
		alert ("Browser does not support HTTP Request");
		return;
	}
	var url="include/ajax_comment_handler.php";
	var data="auth="+commercialAndReplace(document.getElementById("autore-commento").value)+"&text="+commercialAndReplace(document.getElementById("testo-commento").value)+"&id_arg="+document.getElementById("id-arg").value+"&code="+document.getElementById("code").value;
	container = "container";
	xmlhttp.onreadystatechange = stateChanged; 
	xmlhttp.open("POST",url,true);
	xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	xmlhttp.send(data);	
}
/** Metodo che permette di eliminare un commento
 * @param idComm Id del commento da cancellare
 */
function deleteComment(idComm){
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp===null) {
		alert ("Browser does not support HTTP Request");
		return;
	}
	var url="include/ajax_comment_delete_handler.php";
	url=url+"?id="+idComm;
	container = "container";
	xmlhttp.onreadystatechange = stateChanged; 
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);	
}
/** Metodo che permette di adattare il form di edit della sezione alla funzione di modifica (se ne riempiono i campi)
 * @param id Id della sezione che si desidera modificare
 * @param nome Nome della sezione che si desidera modificare
 * @param sez_assoc Sezione Associata della sezione che si desidera modificare
 */
function modifySection(id,nome,sez_assoc){
	document.getElementById("form-sez-title").innerHTML="Modifica Sezione --> <a href=\"#\" onclick=\"insSection()\">Inserisci</a>";
	document.getElementById("new-sez-name").value=nome;
	document.getElementById("new-sez-assoc-select").selectedIndex=findIndexInSelectList("new-sez-assoc-select", sez_assoc);
	document.getElementById("new-sez-id").value=id;
}
/** Metodo che permette di adattare il form di edit dell'articolo alla funzione di modifica (se ne riempion i campi)
 * @param id Id dell'articolo da modificare
 * @param autore Autore dell'articolo da modificare
 * @param testo Testo dell'articolo da modificare
 * @param titolo Titolo dell'articolo da modificare
 * @param descrizione Descrizione dell'articolo da modificare
 * @param data Data di scrittura dell'articolo da modificare
 * @param id_sezione Id della sezione a cui e' associato l'articolo
 * @param switcher Booleano che permette di decidere se e' necessario cambiare la pagina per utilizzare il form di modifica
 */
function modifyArgument(id,autore,testo,titolo,descrizione,data,id_sezione,switcher){
	if(switcher){
		xmlhttp=GetXmlHttpObject();
		if (xmlhttp===null) {
			alert ("Browser does not support HTTP Request");
			return;
		}
		var url="include/ajax_section_handler.php";
		url=url+"?sez="+id_sezione+"&switch=yes"+"&id_selected="+id;
		container = "container";
		xmlhttp.onreadystatechange = stateChanged; 
		xmlhttp.open("GET",url,true);
		xmlhttp.send(null);
	}
	else{
		document.getElementById("form-article-edit").innerHTML="Modifica Argomento --> <a href=\"#\" onclick=\"insArgument("+id_sezione+")\">Inserisci</a>";
		document.getElementById("new-autore-argomento").value=doubleQuotesPurifier(autore);
		document.getElementById("new-testo-argomento").value=doubleQuotesPurifier(testo);
		document.getElementById("new-descrizione-argomento").value=doubleQuotesPurifier(descrizione);
		document.getElementById("new-titolo-argomento").value=doubleQuotesPurifier(titolo);
		document.getElementById("new-sez-assoc-argomento").selectedIndex=findIndexInSelectList("new-sez-assoc-argomento", id_sezione);
		document.getElementById("new-id-arg").value=id;
	}
}
/** Metodo che permette di adattare il form di edit di argomenti per la funzione di inserimento (se ne svuotano i campi)
 * @param id_sezione Id della sezione a cui e' associato l'articolo
 */
function insArgument(id_sezione){
	document.getElementById("form-article-edit").innerHTML="Inserisci Argomento";
	document.getElementById("new-autore-argomento").value="";
	document.getElementById("new-testo-argomento").value="";
	document.getElementById("new-descrizione-argomento").value="";
	document.getElementById("new-titolo-argomento").value="";
	document.getElementById("new-sez-assoc-argomento").selectedIndex=findIndexInSelectList("new-sez-assoc-argomento", id_sezione);
	document.getElementById("new-id-arg").value="";	
}
/** Metodo che permette di adattare il form di edit delle sezioni per la funzione di inserimento (se ne svuotano i campi)
 */
function insSection(){
	document.getElementById("form-sez-title").innerHTML="Nuova Sezione";
	document.getElementById("new-sez-name").value="";
	document.getElementById("new-sez-assoc-select").selectedIndex=findIndexInSelectList("new-sez-assoc-select", 1);
	document.getElementById("new-sez-id").value="";
}
/** Metodo che permette di cancellare una sezione
 * @param idSez Id della sezione da cancellare
 */
function deleteSection(idSez){
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp===null) {
		alert ("Browser does not support HTTP Request");
		return;
	}
	var url="include/ajax_section_delete_handler.php";
	url=url+"?id="+idSez;
	container = "menu";
	xmlhttp.onreadystatechange = stateChanged; 
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);	
}
/** Metodo che permette di cancellare un articolo
 * @param idArg Id dell'articolo da cancellare
 */
function deleteArgument(idArg){
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp===null) {
		alert ("Browser does not support HTTP Request");
		return;
	}
	var url="include/ajax_article_delete_handler.php";
	url=url+"?id="+idArg;
	container = "container";
	xmlhttp.onreadystatechange = stateChanged; 
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
}

