/**
 * @author cristiano
 */

function Commento()
{
	this.script_page = "/commento.php";

	this.checkFormCommenti = function()
	{
		nome = document.getElementById("nome");
		commento = document.getElementById("commento");
		
		if (nome.value.length <= 2)
		{
			alert("Devi inserire almeno 3 caratteri.");
			nome.style.borderColor = "red";
			nome.focus();
			return false;
		}
		
		if (commento.value.length <= 2)
		{
			alert("Devi inserire almeno 3 caratteri.");
			commento.style.borderColor = "red";
			commento.focus();
			return false;
		}
		
		nome.style.borderColor = "";
		commento.style.borderColor = "";
		
		return true;
	}
	
	this.sendComment = function()
	{
		if (this.checkFormCommenti())
		{
			this.setLoadingSrc("/immagini/vote_loader.gif");
			this.setLoadingTarget(document.getElementById("php_response"));
			this.setTarget(this.gestisciRisposta);
			this.params["operazione"] = "commenta";
			this.params["idc"] = document.getElementById("idc").value;
			this.params["nome"] = document.getElementById("nome").value;
			this.params["commento"] = document.getElementById("commento").value;
			this.makeRequest(this.script_page);
		}
	}
	
	this.listaCommenti = function(idc, pag)
	{
		this.setLoadingSrc("/immagini/vote_loader.gif");
		this.setLoadingTarget(document.getElementById("php_response"));
		this.setTarget(document.getElementById("php_response"));
		this.params["operazione"] = "lista_commenti";
		this.params["idc"] = parseInt(idc);
		this.params["pag"] = (pag == null || pag == undefined) ? 1 : parseInt(pag);
		this.makeRequest(this.script_page);
	}
	
	this.gestisciRisposta = function(response)
	{
		response = new String(response);
		response = response.split(":");
		
		if (response[0] == 'OK')
			document.getElementById('form_commento').reset();
		else if (response[0] == 'FLOOD')
			alert('Devi aspettare un minuto per inserire il prossimo commento.');
		else
			alert('Errore nell\'inserimento del commento!');
		
		eval("comm.listaCommenti(" + response[1] + ", 1);");
	}
}

Commento.prototype = new AjaxModule();
Commento.prototype.constructor = Commento;