/**
 * @author francesco
 */
var destination_div = "content";

function Contributo(mod)
{
	this.menu;
	this.istanza;
	this.mod = mod;
	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', null, null);\">Inserisci</a>";
			html += "<br />- <a href=\"javascript:void(0);\" onclick=\"" + this.istanza + ".loadform('edt', 1, null);\">Modifica</a>";
			//html += "<br /><div id=\"catbox_" + this.mod + "\" name=\"catbox_" + this.mod + "\">- <a href=\"javascript:void(0);\" onclick=\"ListaCategorie[" + this.mod + "].load_menu();\">Categorie</a></div>";

			this.loadform("edt", 1, null);
		}
		this.menu.innerHTML = html;
	}

	this.loadform = function(tipo, comando, operazione)
	{
		var parametri = "istanza=" + this.istanza + "&mod=" + this.mod + "&";
		switch (tipo)
		{
			case "ins" :
				parametri += "nome_form=inserisci";
				loadingGIF(document.getElementById("content"));
				makeRequest("include_amministrazione/php/forms.php", this.handleContentResponse, "POST", parametri);
				break;
			case "edt" :
				var items = 0;
				try
				{
					items = parseInt(document.getElementById("items_pag").value);
					if (items < 5) items = 5;
				} catch (e) { items = 5; }

				parametri += "nome_form=modifica&pag=" + comando + "&items_pag=" + items;
				var s_opz = "tutti";
				try
				{
					switch (document.getElementById("scad_opz").value)
					{
						case "online" :
						case "scaduti" :
							s_opz = document.getElementById("scad_opz").value;
					}
				} catch (e) { s_opz = "tutti"; }
				parametri += "&scad_opz=" + s_opz;
				loadingGIF(document.getElementById("content"));
				makeRequest("include_amministrazione/php/forms.php", this.handleContentResponse, "POST", parametri);
				break;
			case "upl" :
				destination_div = "add_div";
				parametri += "nome_form=upload&comando=" + comando + "&operazione=" + operazione;
				loadingGIF(document.getElementById("add_div"));
				makeRequest("include_amministrazione/php/forms.php", this.handlePhpResponse, "POST", parametri);
				break;
			case "lnk" :
				destination_div = "add_div";
				parametri += "nome_form=addlink";
				loadingGIF(document.getElementById("add_div"));
				makeRequest("include_amministrazione/php/forms.php", this.handlePhpResponse, "POST", parametri);
				break;
		}
	}

	this.readform = function()
	{
		var parametri = new String();
		var tmp = new String(document.getElementById("titolo").value);

		if (tmp.length > 2)
		{
			document.getElementById("titolo").style.borderColor = "gray";
			parametri += "titolo=" + specialChars(getVal("titolo"));
		}
		else
		{
			document.getElementById("titolo").style.borderColor = "#FF0000";
			document.getElementById("titolo").focus();
			throw new Object({id:"titolo",message:"Sono richiesti almeno 2 caratteri."});
		}

		tmp = new String(document.getElementById("abstract").value);
		if (tmp.length == 0 || tmp.length > 2)
		{
			document.getElementById("abstract").style.borderColor = "gray";
			parametri += "&abstract=" + specialChars(getVal("abstract"));
		}
		else
		{
			document.getElementById("abstract").style.borderColor = "#FF0000";
			document.getElementById("abstract").focus();
			throw new Object({id:"abstract",message:"Formato non valido."});
		}

		tmp = new String(tinyMCE.get("contenuto").getContent());
		if (tmp.length > 2) parametri += "&contenuto=" + specialChars(tmp);
		else throw new Object({id:"contenuto",message:"Sono richiesti almeno 2 caratteri."});

		if (check("data_cont_g", /^[0-9]{1,2}$/))
			parametri += "&data_cont_g=" + specialChars(getVal("data_cont_g")); else throw new Object({id:"data_cont_g",message:"Formato non valido."});
		if (check("data_cont_m", /^[0-9]{1,2}$/))
			parametri += "&data_cont_m=" + specialChars(getVal("data_cont_m")); else throw new Object({id:"data_cont_m",message:"Formato non valido."});
		if (check("data_cont_a", /^[0-9]{4}$/))
			parametri += "&data_cont_a=" + specialChars(getVal("data_cont_a")); else throw new Object({id:"data_cont_a",message:"Formato non valido."});
		if (check("data_scad_g", /^[0-9]{1,2}$/))
			parametri += "&data_scad_g=" + specialChars(getVal("data_scad_g")); else throw new Object({id:"data_scad_g",message:"Formato non valido."});
		if (check("data_scad_m", /^[0-9]{1,2}$/))
			parametri += "&data_scad_m=" + specialChars(getVal("data_scad_m")); else throw new Object({id:"data_scad_m",message:"Formato non valido."});
		if (check("data_scad_a", /^[0-9]{4}$/))
			parametri += "&data_scad_a=" + specialChars(getVal("data_scad_a")); else throw new Object({id:"data_scad_a",message:"Formato non valido."});
		parametri += "&approvato=" + document.getElementById("approvato").checked;

		return parametri;
	}

	this.inserisci = function()
	{
		try
		{
			var parametri = "operazione=inserisci&" + this.readform();
			parametri += "&istanza=" + this.istanza + "&mod=" + this.mod;

			destination_div = "content";
			loadingGIF(document.getElementById("content"));
			makeRequest("include_amministrazione/php/contributo.php", this.handlePhpResponse, "POST", parametri);
		} catch (err) { lightpopup(err.message, err.id); }
	}

	this.carica = function(id)
	{
		var parametri = "operazione=carica";
		parametri += "&id_cont=" + id;
		parametri += "&istanza=" + this.istanza + "&mod=" + this.mod;

		loadingGIF(document.getElementById("content"));
		makeRequest("include_amministrazione/php/contributo.php", this.handleContentResponse, "POST", parametri);
	}

	this.cancella = function(id)
	{
		if (confirm("Vuoi davvero eliminare questo elemento ?"))
			this.conf_cancella(id);
	}

	this.conf_cancella = function(id)
	{
		var parametri = "operazione=cancella";
		parametri += "&id_cont=" + id;
		parametri += "&istanza=" + this.istanza;

		destination_div = "content";
		loadingGIF(document.getElementById("content"));
		makeRequest("include_amministrazione/php/contributo.php", this.handlePhpResponse, "POST", parametri);
	}

	this.modifica = function(id)
	{
		try
		{
			var parametri = "operazione=modifica&id_cont=" + id + "&" + this.readform();
			parametri += "&istanza=" + this.istanza;

			destination_div = "content";
			loadingGIF(document.getElementById("content"));
			makeRequest("include_amministrazione/php/contributo.php", this.handlePhpResponse, "POST", parametri);
		} catch (err) { lightpopup(err.message, err.id); }
	}

	this.inviaFile = function()
	{
		document.getElementById("invia_btn").value = "Invio in corso ...";
		document.getElementById("form_file").submit();
	}

	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;
			}
		}
	}

	this.handleContentResponse = function()
	{
		if (xmlhttp.readyState == 4)
		{
			if (xmlhttp.status == 200)
			{
				document.getElementById("content").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;
			}
		}
	}
}
