/*
*******************************************************************************************************************************************
*                                                                                                                                         *
*                                                          INIZIALIZZAZIONE AJAX                                                          *
*                                                                                                                                         *
*******************************************************************************************************************************************
*/
// NOTA AGLI SCRIPT AJAX : Il post non funziona con mozilla

//VAR GLOBALI PER SCRIPTS AJAX
var chiamaPagina = null;

// Creo un oggetto XMLHttpRequest che fa richieste http in background
function creaXMLHttpRequest(eventoAjax) { 
	var xmlhttp = null;

	if(window.ActiveXObject) { // IE < v.7
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else if(window.XMLHttpRequest) { // Mozilla, Firefox, Safari, Netscape, IE7
		xmlhttp = new XMLHttpRequest();
	}

	if(xmlhttp) {
		xmlhttp.onreadystatechange = eventoAjax;
		return xmlhttp;
	}		
}


/*
*******************************************************************************************************************************************
*                                                                                                                                         *
*                                                    BOX MODIFICA DESCRIZIONE ALLEGATI                                                    *
*                                                                                                                                         *
*******************************************************************************************************************************************
*/
var globalDivDescr = "";

function statoPopUp(elemento) {
	var popup = document.getElementById(["dv_" + elemento]);
	popup.style.display == 'none' ? popup.style.display='' : popup.style.display='none';
}

function modDescr() {
	if(chiamaPagina.readyState == 4 && chiamaPagina.status == 200) {
		var descr = chiamaPagina.responseText;
		descr.length > 30 ? descr = descr.substr(0,27)+ "...<br>" : descr = descr + "<br>";
		document.getElementById([globalDivDescr]).innerHTML = descr;	
	}
}

function salvaDescrizione(elemento,id) {

	var url = "../controls/comandi.php";
	var idall = document.getElementById(["idall_" + id]).value;
	var descr = document.getElementById(["txtdescr_" + id]).value;
	globalDivDescr = "divdescr_" + id;

	chiamaPagina = creaXMLHttpRequest(modDescr);
	chiamaPagina.open("GET", url + "?cmd=all_descr&_idall=" + idall + "&dsc=" + descr + "&rand=" + escape(Math.random()));	
	chiamaPagina.send(null);
	
	statoPopUp(elemento);
}


/*
*******************************************************************************************************************************************
*                                                                                                                                         *
*                                                            CONTATTA REFERENTI                                                           *
*                                                                                                                                         *
*******************************************************************************************************************************************
*/
var gloabalDivID = "";

function statoBusta(indice, codUtente) {
	var icona = "";
	var busta = document.getElementById(["busta_" + indice + "_" + codUtente]).src;
	var boxMex = document.getElementById(["mail_" + indice + "_" + codUtente]);
	
	busta.substr(busta.lastIndexOf("/")+1, busta.length) == "mail_closed.jpg" ? icona = "mail_open.jpg" : icona = "mail_closed.jpg";
	boxMex.style.display == 'none' ? boxMex.style.display='' : boxMex.style.display='none';
	document.getElementById(["busta_" + indice + "_" + codUtente]).src = "../layout/icons/" + icona;							
}

function esitoContatto() {
	if(chiamaPagina.readyState == 4 && chiamaPagina.status == 200) {
		document.getElementById(["mail_" + gloabalDivID]).style.border='1px solid #ccc';
		document.getElementById(["mail_" + gloabalDivID]).style.padding='0.5em';		
		document.getElementById(["mail_" + gloabalDivID]).style.color='#f00';		
		document.getElementById(["txt_" + gloabalDivID]).display='none';		
		document.getElementById(["mail_" + gloabalDivID]).innerHTML=chiamaPagina.responseText;
	}
}

function contattaReferente(indice, codUtente) {

	var url = "../controls/comandi.php";
	var descr = document.getElementById(["txt_" + indice + "_" + codUtente]).value;
	var mex = document.getElementById(["txt_" + indice + "_" + codUtente]).value;
		
	gloabalDivID = indice + "_" + codUtente;
	
	if(mex == "") {
		alert("Non hai scritto nulla!");
		return false;
	}	
	else {	
		chiamaPagina = creaXMLHttpRequest(esitoContatto);
		chiamaPagina.open("GET", url + "?cmd=mexpriv&_userid=" + codUtente + "&_mex=" + descr + "&rand=" + escape(Math.random()));
		chiamaPagina.send(null);
	}
}


/*
*******************************************************************************************************************************************
*                                                                                                                                         *
*                                                             SPOSTA DOCUMENTI                                                            *
*                                                                                                                                         *
*******************************************************************************************************************************************
*/
function doMove() {
	if(chiamaPagina.readyState == 4 && chiamaPagina.status == 200) {
		window.location.reload();
	}
}

function spostaDocumenti(idel) {

	var url = "../controls/comandi.php";
	var idSezione = document.getElementById(["sezione" + idel]).value;
	var arrayDocumenti = document.getElementById(["arrdocs" + idel]).value;	
	
	for(var i=0; i<arrDocumenti.length+1; i++) {
		alert(arrDocumenti[i]);
	}
	alert(arrayDocumenti.length);
	
	
	chiamaPagina = creaXMLHttpRequest(doMove);
	//window.open(url + "?_idel=" + idel + "&_idsez=" + idSezione + "&_eledocs=" + arrayDocumenti + "&cmd=docs_move&rand=" + escape(Math.random()), "get");
	chiamaPagina.open("GET", url + "?_idel=" + idel + "&_idsez=" + idSezione + "&_eledocs=" + arrayDocumenti + "&cmd=docs_move&rand=" + escape(Math.random()));
	chiamaPagina.send(null);
}


/*
*******************************************************************************************************************************************
*                                                                                                                                         *
*                                                               FOTO ACCOUNT                                                              *
*                                                                                                                                         *
*******************************************************************************************************************************************
*/
var gloabalDiv = "";

function esitoCambioFoto() {
	//alert('../public/users_foto/' + chiamaPagina.responseText);
	if(chiamaPagina.readyState == 4 && chiamaPagina.status == 200) {
		if (gloabalDiv == "fotoelement") { // Immagine elemento
			document.getElementById(["img_" + gloabalDiv]).src='../public/element_images/' + chiamaPagina.responseText;
		}
		else if (gloabalDiv == "fotoaccount") { // Foto account
			document.getElementById(["img_" + gloabalDiv]).src='../public/users_foto/' + chiamaPagina.responseText;		
		}
		document.getElementById(["dv_" + gloabalDiv]).style.display = "none";
		// Manca aggiornamento icone div "dv_" + gloabalFotoAccount;
	}
}

/* NON USASTO  */
function caricaFoto(elemento) {

	var url = "../controls/comandi.php";
	gloabalDiv = elemento;
	var foto = document.getElementById([elemento]).value;

	if(foto == "") {
		alert("Non è stato scelto alcun file da caricare");
		return false;
	}
	else {
		if (elemento == "fotoelement") { // Immagine elemento
			chiamaPagina = creaXMLHttpRequest(esitoCambioFoto);
			window.open(url + "?_foto=" + foto + "&cmd=el_foto&rand=" + escape(Math.random()), "get");
			//chiamaPagina.open("GET", url + "?_foto=" + foto + "&cmd=account_foto&rand=" + escape(Math.random()));
		}
		else if (elemento == "fotoaccount") { // Foto account
			chiamaPagina = creaXMLHttpRequest(esitoCambioFoto);
			window.open(url + "?_foto=" + foto + "&cmd=account_foto&rand=" + escape(Math.random()), "get");
			//chiamaPagina.open("GET", url + "?_foto=" + foto + "&cmd=account_foto&rand=" + escape(Math.random()));
		
		}
		chiamaPagina.send(null);
	}
}


function selFoto(elemento,id) {

	var url = "../controls/comandi.php";
	gloabalDiv = elemento;

	if(id == "") {
		alert("Non è stato selezionato alun file");
		return false;
	}
	else {
		chiamaPagina = creaXMLHttpRequest(esitoCambioFoto);
		//window.open(url + "?_foto=" + foto + "&cmd=account_selfoto&rand=" + escape(Math.random()), "get");
		chiamaPagina.open("GET", url + "?_foto=" + id + "&cmd=account_selfoto&rand=" + escape(Math.random()));
		chiamaPagina.send(null);
	}
}


/*
*******************************************************************************************************************************************
*                                                                                                                                         *
*                                                        MODIFICA PASSWORD ACCOUNT                                                        *
*                                                                                                                                         *
*******************************************************************************************************************************************
*/
function boxPwd(elemento) {
	var popup = document.getElementById([elemento]);
	popup.style.display == 'none' ? popup.style.display='' : popup.style.display='none';
}

function esitoCambioPwd() {
	if(chiamaPagina.readyState == 4 && chiamaPagina.status == 200) {
		document.getElementById(["dv_pwd"]).style.border='1px solid #ccc';
		document.getElementById(["dv_pwd"]).style.padding='0.5em';		
		document.getElementById(["dv_pwd"]).style.color='#f00';		
		document.getElementById(["dv_pwd"]).innerHTML=chiamaPagina.responseText;
	}
}

function cambiaPwd(pwd1,pwd2) {

	var url = "../controls/comandi.php";
	var pass1 = document.getElementById([pwd1]).value;
	var pass2 = document.getElementById([pwd2]).value;

	if(pass1 == "" || pass2 == "") {
		alert("Non è stata inserita alcuna password");
		return false;
	}
	else if(pass1 != pass2) {
		alert("Attenzione, le due password non coincidono");
		return false;
	}

	else {
		chiamaPagina = creaXMLHttpRequest(esitoCambioPwd);
		chiamaPagina.open("GET", url + "?_pwd=" + pass2 + "&cmd=account_pwd&rand=" + escape(Math.random()));
		chiamaPagina.send(null);
	}
}


/*
*******************************************************************************************************************************************
*                                                                                                                                         *
*                                                                PAGINAZIONE                                                              *
*                                                                                                                                         *
*******************************************************************************************************************************************
*/
function scriviPaginazione() {
	if(chiamaPagina.readyState == 4 && chiamaPagina.status == 200) {
		document.getElementById(["cls_paginazione"]).innerHTML = "";
//		document.getElementById(["cls_paginazione"]).style.height = "0px";
//		document.getElementById(["cls_paginazione"]).style.width = "0px";				
		document.getElementById(["cls_paginazione"]).display='none';
		document.getElementById(["allegati"]).innerHTML = "";
		document.getElementById(["allegati"]).innerHTML = chiamaPagina.responseText;
	}
}

function paginazioneAjax(idelemento,idcontenuto,begin,recperpag,href) {
	
	var url = "../controls/comandi.php";
	var cmd = "pagajax";

	chiamaPagina = creaXMLHttpRequest(scriviPaginazione);
	//window.open(url + "?cmd=" + cmd + "&_idel=" + idelemento + "&_idcont=" + idcontenuto + "&_begin=" + begin + "&_recperpag=" + recperpag + "&_uri=" + href + "&rand=" + escape(Math.random()), "get");
	chiamaPagina.open("GET", url + "?cmd=" + cmd + "&_idel=" + idelemento + "&_idcont=" + idcontenuto + "&_begin=" + begin + "&_quanti=" + recperpag + "&_uri=" + href + "&rand=" + escape(Math.random()), "GET");
	chiamaPagina.send(null);
}	















function gest_gruppiutenti(IdGruppo, IdAzione, azione, sizeSelect) {

	var url = "../controls/comandi.php";
	var listautenti_ins = "";
	var listautenti_del = "";
	
	if(azione == "ins") {
		arrayvalori = document.getElementById(["utenti_presenti[]"]);
			
		for (var i=0; i<arrayvalori.length; i++) {
			if(arrayvalori[i].selected == true) {
				listautenti_ins = listautenti_ins != "" ? listautenti_ins + "," + arrayvalori[i].value : listautenti_ins + arrayvalori[i].value;
				 // alert(arrayvalori[i].value);
			}
		}
		// alert(listautenti_ins);
	}
	else { //Tolgo
		arrayvalori = document.getElementById(["utenti_inseriti[]"]);
		
		for (var i=0; i<arrayvalori.length; i++) {
			if(arrayvalori[i].selected == true) {
				listautenti_del = listautenti_del != "" ? listautenti_del + "," + arrayvalori[i].value : listautenti_del + arrayvalori[i].value;
				 // alert(arrayvalori[i].value);
			}
		}
	}
	
	chiamaPagina = creaXMLHttpRequest(aggiornaBoxUtenti);
	//window.open(url + "?cmd=group_insutenti&_idaz=" + azione + "&_idgroup=" + idgruppo + "&_lista=" + listautenti + "&rand=" + escape(Math.random()), "get");
	chiamaPagina.open("GET", url + "?cmd=group_insutenti&_akt=" + azione + "&_idaz=" + IdAzione + "&_idgroup=" + IdGruppo + "&_lins=" + listautenti_ins + "&_ldel=" + listautenti_del + "&_ss=" + sizeSelect + "&rand=" + escape(Math.random()));
	chiamaPagina.send(null);

}

function aggiornaBoxUtenti() {
	if(chiamaPagina.readyState == 4 && chiamaPagina.status == 200) {
		//alert(chiamaPagina.responseText);
		document.getElementById(["responseAjax"]).innerHTML=chiamaPagina.responseText;
		//document.getElementById(["users"]).innerHTML=chiamaPagina.responseText;
	}
}

