/**
 * @author francesco
 */
var destination_msg = "content";

function Messaggio()
{
	this.open = false;
	this.istanza;

	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('new', null, null);\">Nuovo</a>";
			html += "<br />- <a href=\"javascript:void(0);\" onclick=\"" + this.istanza + ".loadform('edit', null, 1);\">Modifica</a>";

			this.loadform("edit", null, 1);
		}
		this.menu.innerHTML = html;
	}

	this.loadform = function(tipo, t_msg, pag)
	{
		var parametri = "nome_form=";
		destination_msg = "content";
		
		if (tipo == "new") parametri += "new_msg";
		else if (tipo == "edit")
		{
			var items = 5;
			parametri += "edit_msg&pag=" + pag;
			try { items = parseInt(document.getElementById("items_pag").value); }
			catch (e) { items = 5; }
			parametri += "&items_pag=" + items;
		}
		else if (tipo == "sel_msg")
		{
			var tipo_msg = (t_msg != undefined && t_msg != null) ? t_msg : document.getElementById("tipo_msg").value;

			parametri += "sel_msg";
			parametri += "&tipo_msg=" + tipo_msg;
			destination_msg = "msg_target";

			if (tipo_msg == "topic")
			{
				try { loadingGIF(document.getElementById(destination_msg)); }
				catch (e)
				{
					destination_msg = "content";
					loadingGIF(document.getElementById(destination_msg));
				}
				makeRequest("include_amministrazione/php/forms.php", this.handleContentResponse, "POST", parametri);
				return;
			}
			else if (tipo_msg == "post")
			{
				var items = 5;
				parametri += "&pag=";
				parametri += (pag != undefined && pag != null) ? pag : 1;
				try { items = parseInt(document.getElementById("items_pag").value); }
				catch (e) { items = 5; }
				parametri += "&items_pag=" + items;
			}
		}
		else return;

		loadingGIF(document.getElementById(destination_msg));
		makeRequest("include_amministrazione/php/forms.php", this.handlePhpResponse, "POST", parametri);
	}

	this.nuovoTopic = function()
	{
		var parametri = "operazione=nuovoTopic";

		if (!check("titolo", /^.{2,}$/))
		{
			lightpopup("Formato non valido.", "titolo");
			return;
		}
		else parametri += "&titolo=" + specialChars(document.getElementById("titolo").value);
		
		var tmp = new String(tinyMCE.get("contenuto").getContent());
		if (tmp.length < 2)
		{
			lightpopup("Formato non valido.", "contenuto");
			return;
		}
		else parametri += "&contenuto=" + specialChars(tinyMCE.get("contenuto").getContent());

		destination_msg = "content";
		loadingGIF(document.getElementById(destination_msg));
		makeRequest("include_amministrazione/php/messaggio.php", this.handlePhpResponse, "POST", parametri);
	}

	this.nuovoPost = function(id_topic, titolo)
	{
		var parametri = "id_topic=" + id_topic + "&nome_form=replyTopic&titolo=" + specialChars(titolo);
		destination_msg = "content";
		loadingGIF(document.getElementById(destination_msg));
		makeRequest("include_amministrazione/php/forms.php", this.handleContentResponse, "POST", parametri);
	}

	this.replyTopic = function(id_topic)
	{
		var parametri = "id_topic=" + id_topic + "&operazione=nuovoPost";

		if (!check("titolo", /^.{2,}$/))
		{
			lightpopup("Formato non valido.", "titolo");
			return;
		}
		else parametri += "&titolo=" + specialChars(document.getElementById("titolo").value);
		
		var tmp = new String(tinyMCE.get("contenuto").getContent());
		if (tmp.length < 2)
		{
			lightpopup("Formato non valido.", "contenuto");
			return;
		}
		else parametri += "&contenuto=" + specialChars(tinyMCE.get("contenuto").getContent());

		destination_msg = "content";
		loadingGIF(document.getElementById(destination_msg));
		makeRequest("include_amministrazione/php/messaggio.php", this.handlePhpResponse, "POST", parametri);
	}

	this.modificaTopic = function(id_topic, titolo)
	{
		var parametri = "nome_form=modificaTopic&id_topic=" + id_topic + "&titolo=" + specialChars(titolo);
		destination_msg = "content";
		loadingGIF(document.getElementById(destination_msg));
		makeRequest("include_amministrazione/php/forms.php", this.handleContentResponse, "POST", parametri);
	}

	this.caricaPost = function(id_topic, titolo)
	{
		var parametri = "nome_form=caricaPost&id_topic=" + id_topic + "&titolo=" + specialChars(titolo);
		destination_msg = "content";
		loadingGIF(document.getElementById(destination_msg));
		makeRequest("include_amministrazione/php/forms.php", this.handlePhpResponse, "POST", parametri);
	}

	this.editTopic = function(id_topic)
	{
		var parametri = "operazione=modificaTopic&id_topic=" + id_topic;

		if (!check("titolo", /^.{2,}$/))
		{
			lightpopup("Formato non valido.", "titolo");
			return;
		}
		else parametri += "&titolo=" + specialChars(document.getElementById("titolo").value);
		
		var tmp = new String(tinyMCE.get("contenuto").getContent());
		if (tmp.length < 2)
		{
			lightpopup("Formato non valido.", "contenuto");
			return;
		}
		else parametri += "&contenuto=" + specialChars(tinyMCE.get("contenuto").getContent());

		destination_msg = "content";
		loadingGIF(document.getElementById(destination_msg));
		makeRequest("include_amministrazione/php/messaggio.php", this.handlePhpResponse, "POST", parametri);
	}

	this.cancellaTopic = function(id_topic, titolo)
	{
		if (confirm("Vuoi davvero eliminare il Topic \"" + titolo + "\" ?"))
			this.conf_cancellaTopic(id_topic, titolo);
	}

	this.conf_cancellaTopic = function(id_topic, titolo)
	{
		var parametri = "operazione=cancellaTopic&id_messaggio=" + id_topic + "&titolo=" + specialChars(titolo);
		destination_msg = "content";
		loadingGIF(document.getElementById(destination_msg));
		makeRequest("include_amministrazione/php/messaggio.php", this.handlePhpResponse, "POST", parametri);
	}

	this.modificaPost = function(id_messaggio)
	{
		var parametri = "nome_form=modificaPost&id_messaggio=" + id_messaggio;
		destination_msg = "content";
		loadingGIF(document.getElementById(destination_msg));
		makeRequest("include_amministrazione/php/forms.php", this.handleContentResponse, "POST", parametri);
	}

	this.editPost = function(id_messaggio)
	{
		var parametri = "operazione=modificaPost&id_messaggio=" + id_messaggio;

		if (!check("titolo", /^.{2,}$/))
		{
			lightpopup("Formato non valido.", "titolo");
			return;
		}
		else parametri += "&titolo=" + specialChars(document.getElementById("titolo").value);
		
		var tmp = new String(tinyMCE.get("contenuto").getContent());
		if (tmp.length < 2)
		{
			lightpopup("Formato non valido.", "contenuto");
			return;
		}
		else parametri += "&contenuto=" + specialChars(tinyMCE.get("contenuto").getContent());

		destination_msg = "content";
		loadingGIF(document.getElementById(destination_msg));
		makeRequest("include_amministrazione/php/messaggio.php", this.handlePhpResponse, "POST", parametri);
	}

	this.cancellaPost = function(id_messaggio)
	{
		if (confirm("Vuoi davvero eliminare questo Post ?"))
			this.conf_cancellaPost(id_messaggio);
	}

	this.conf_cancellaPost = function(id_messaggio)
	{
		var parametri = "operazione=cancellaPost&id_messaggio=" + id_messaggio;
		destination_msg = "content";
		loadingGIF(document.getElementById(destination_msg));
		makeRequest("include_amministrazione/php/messaggio.php", this.handlePhpResponse, "POST", parametri);
	}

	this.handlePhpResponse = function()
	{
		if (xmlhttp.readyState == 4)
		{
			if (xmlhttp.status == 200) document.getElementById(destination_msg).innerHTML = xmlhttp.responseText;
			else
			{
				var contentHTML = "<b>Errore durante il trasferimento dati.</b>";
				contentHTML += "<br />Stato : " + xmlhttp.status;
				document.getElementById("content").innerHTML = contentHTML;
			}
		}
	}

	this.handleContentResponse = function()
	{
		if (xmlhttp.readyState == 4)
		{
			if (xmlhttp.status == 200)
			{
				document.getElementById(destination_msg).innerHTML = xmlhttp.responseText;
				tinyMCE.init({mode:"exact", elements:"contenuto", theme:"advanced",
				plugins:"table,style,layer", theme_advanced_toolbar_location:"top",
				theme_advanced_layout_manager:"SimpleLayout",
				theme_advanced_buttons1:"removeformat,bold,italic,underline,strikethrough,separator,bullist,numlist,outdent,indent,separator,sup,sub,separator,forecolor,separator,link,unlink,separator,undo,redo,separator,code",
				theme_advanced_buttons2:"", language:"it"});
			}
			else
			{
				var contentHTML = "<b>Errore durante il trasferimento dati.</b>";
				contentHTML += "<br />Stato : " + xmlhttp.status;
				document.getElementById("content").innerHTML = contentHTML;
			}
		}
	}
}
