/**
 * @author francesco
 */
var destination_usr = "content";

function Utente()
{
	this.istanza;
	this.open = false;

	this.loadmenu = function(istanza)
	{
		var html = "";
		this.istanza = istanza;

		if (this.open)
		{
			this.open = false;
			html = "<a href=\"javascript:void(0);\" onclick=\"load_menu('" + this.istanza + "');\">" + this.istanza + "</a>";
		}
		else
		{
			this.open = true;
			html = "<a href=\"javascript:void(0);\" onclick=\"load_menu('" + this.istanza + "');\">" + this.istanza + "</a>";
			html += "<br />- <a href=\"javascript:void(0);\" onclick=\"" + this.istanza + ".loadform('ins');\">Inserisci</a>";
			html += "<br />- <a href=\"javascript:void(0);\" onclick=\"" + this.istanza + ".loadform('edt');\">Modifica</a>";

			this.loadform("edt");
		}
		this.menu.innerHTML = html;
	}

	this.loadform = function(tipo)
	{
		switch (tipo)
		{
			case "ins" :

				destination_usr = "content";
				var parametri = "nome_form=ins_utente";
				loadingGIF(document.getElementById(destination_usr));
				makeRequest("include_amministrazione/php/forms.php", this.handlePhpResponse, "POST", parametri);

				break;

			case "edt" :

				destination_usr = "content";
				var parametri = "nome_form=edt_utente";
				loadingGIF(document.getElementById(destination_usr));
				makeRequest("include_amministrazione/php/forms.php", this.handlePhpResponse, "POST", parametri);

				break;
		}
	}

	this.readform = function(tipo)
	{
		var campi = new Array();
		campi.push(new Object({id:"nome",pattern:/^.{2,}$/}));
		campi.push(new Object({id:"cognome",pattern:/^.{2,}$/}));
		campi.push(new Object({id:"utente",pattern:/^.{2,}$/}));
		if (tipo == "ins")
			campi.push(new Object({id:"pass",pattern:/^.{2,}$/}));
		else if (tipo == "edt")
			campi.push(new Object({id:"pass",pattern:/^.*$/}));
		campi.push(new Object({id:"email",pattern:/^([_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})){1}$/}));

		var parametri = "tipo=" + document.getElementById("tipo_utente").value;

		for (var i = 0; i < campi.length; i++)
		{
			if (!check(campi[i].id, campi[i].pattern))
				throw new Object({id:campi[i].id,message:"Formato non valido."});
			else
				parametri += "&" + campi[i].id + "=" + document.getElementById(campi[i].id).value;
		}

		return parametri;
	}

	this.inserisci = function()
	{
		try
		{
			var parametri = this.readform("ins");
			parametri += "&operazione=inserisci";
	
			destination_usr = "content";
			loadingGIF(document.getElementById(destination_usr));
			makeRequest("include_amministrazione/php/utente.php", this.handlePhpResponse, "POST", parametri);
		}
		catch(e) { lightpopup(e.message, e.id); }
	}

	this.modifica = function(id)
	{
		try
		{
			var parametri = this.readform("edt");
			parametri += "&operazione=modifica&id_utente_edit=" + id;
	
			destination_usr = "content";
			loadingGIF(document.getElementById(destination_usr));
			makeRequest("include_amministrazione/php/utente.php", this.handlePhpResponse, "POST", parametri);
		}
		catch(e) { lightpopup(e.message, e.id); }
	}

	this.carica = function(id)
	{
		var parametri = "id_utente_edit=" + id + "&operazione=carica";

		destination_usr = "content";
		loadingGIF(document.getElementById(destination_usr));
		makeRequest("include_amministrazione/php/utente.php", this.handlePhpResponse, "POST", parametri);
	}

	this.cancella = function(id)
	{
		if (confirm("Vuoi davvero eliminare questo utente ?"))
			this.conf_cancella(id);
	}

	this.conf_cancella = function(id)
	{
		var parametri = "operazione=cancella&id_utente_edit=" + id;

		destination_usr = "content";
		loadingGIF(document.getElementById(destination_usr));
		makeRequest("include_amministrazione/php/utente.php", this.handlePhpResponse, "POST", parametri);
	}

	this.handlePhpResponse = function()
	{
		if (xmlhttp.readyState == 4)
		{
			if (xmlhttp.status == 200) document.getElementById(destination_div).innerHTML = xmlhttp.responseText;
			else
			{
				var contentHTML = "<b>Errore durante il trasferimento dati.</b>";
				contentHTML += "<br />Stato : " + xmlhttp.status;
				document.getElementById("content").innerHTML = contentHTML;
			}
		}
	}
}

