//insert_comment.js

function insert_comment () {

	//imposto il wait sul cursore
	document.body.style.cursor="wait";

	//ottengo i valori dei campi necessari
	mode = $j("#mode").get(0).value;
	set = $j("#set").get(0).value;
	id_news = $j("#id_news").get(0).value;
   /*if ($j("#id_user").get(0).value=="") {
      id_user = $j("#id_user").get(0).value;   
      username = "";
   } else {
      id_user = "";
      username = $j("#username").get(0).value;      
   }*/
   id_user = $j("#id_user").get(0).value;
   //username = $j("#username").get(0).value;
	text = $j("#text").get(0).value;
	vote = $j("#vote").get(0).value;
		
	//controlli e set per andare avanti
		//testo
		vaiavanti = 1;
		if (text.length==0) {
			alert("Il campo testo non deve essere vuoto!");
			document.body.style.cursor="default";
			vaiavanti = 0;		
		} else {
			if (text.length>=1000) {
				alert("Il campo testo deve essere minore di 1000 caratteri");
				document.body.style.cursor="default";
				vaiavanti = 0;
			}
		}

	if (vaiavanti==1) {
		//compongo la stringa dei parametri	
		string_param = "mode=" + mode;
		string_param = string_param + "&set=" + set;
		string_param = string_param + "&nId=" + id_news;
		if (id_user!="") {
			string_param = string_param + "&id_user=" + id_user;
		} else {
			string_param = string_param + "&username=" + username;
		}
		string_param = string_param + "&text=" + text;
		string_param = string_param + "&vote=" + vote;
		//alert(string_param);
	
		//lancio il post	
		$j.ajax({ type: "GET", 
			 url: "/ajax.php", 
			 data: string_param, 
			 dataType: "xml", 
			 success: function(xmlData,id_user)
			 {
				xmlDataSet = xmlData;
				return_ok = browseXMLComment(xmlDataSet);
			 }
		});
	}

}

function browseXMLComment (inXML,InIdUser) {

	$j(inXML).find("comment").each(function(){

		//ottengo i dati dall'xml
	   var _post = $j(this).find("id_post").text();
		var _head = $j(this).find("head_post").text();	   
	   var _vote = $j(this).find("vote_post").text();	   
		var _text = $j(this).find("text_post").text();				

		//ottengo dove aggiungere i dati dell'xml
		da_agg = $j("#da_aggiungere").html();
		
		//creo la nuova riga commento	
		html_da_agg = "<div id=\"add_" + _post + "\" style=\"display: none;\"><table border=\"0\" width=\"90%\"><tr><td align=\"left\" valign=\"middle\" class=\"txt_comments\"><img src=\"/images/freccia_nera.gif\" border=\"0\" width=\"7\" height=\"7\" />&nbsp;&nbsp;" + _head + "</td><td align=\"right\" valign=\"middle\">" + _vote + "</td></tr><tr><td colspan=\"2\" class=\"txt_comments\"><p>" + _text + " <span class=\"testo_small\">(<a href=\"javascript: return true;\" onclick=\"delete_comment(" + _post + ");\">Cancella</a>)</span></p><br /></td></tr></table></div>" + da_agg;
		
		//rimpiazzo il vecchio html
		$j("#da_aggiungere").html(html_da_agg);
		
		//lo slido alla facebook mode
		$j("#add_"+_post).slideDown();

	});

	//reimposto il puntatore per il cursore
	document.body.style.cursor = "default";
	
	//resetto i text e i voti
	$j("#text").get(0).value = "";
	getlength();	//resetto il counter caratteri
	$j("#vote").get(0).value = "1";
	document.getElementById('ul_stelle').style.backgroundPosition = "0 -16px";
	
	//cancello l'at_msg
	if ($j("#at_msg").get(0)!="undefined") {
		$j("#at_msg").html("");
	}	

}

function delete_comment (inIdComm) {

		//compongo la stringa dei parametri
		string_param = "mode=delete_comment&id_comment=" + inIdComm;

		//lancio il post	
		$j.ajax({ type: "GET", 
			 url: "/ajax.php", 
			 data: string_param
		});
		
		//lo slido alla facebook mode
		$j("#add_"+inIdComm).slideUp();
		
}
