/*****************************************************************************************************/
/*                                                                                                   */
/*                                   'COMMENTS UTILITY PANEL' CLASS                                  */          
/*                                                                                                   */
/*****************************************************************************************************/

function UTILITY_GINFO(parent){
	var JSObject = this;
	this.type = "Comment Utility"; 
	
	var container = "comment_utility_";
	this.comments = [];
	this.no_comments = 0;
	
	
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                                         FUNCTION INIT                                             */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	this.init = function(no_comments){
		
		this.no_comments = no_comments;
		
		for (var i=0; i<this.no_comments; i++){
			var comment = new Object();
			comment.id = document.getElementById("commentId_"+i).value;
			comment.index = i;
			comment.container = document.getElementById(container+i);
			comment.table_1 = comment.container.getElementsByTagName("table")[0];
			comment.table_2 = comment.container.getElementsByTagName("table")[1];
			
			if (comment.table_2.getElementsByTagName("a").length > 0){
				comment.link_yes = comment.table_2.getElementsByTagName("a")[0];
				comment.link_yes.index = comment.index;
				comment.link_yes._id = comment.id;
				
				comment.link_no = comment.table_2.getElementsByTagName("a")[1];
				comment.link_no.index = comment.index;
				comment.link_no._id = comment.id;
				
				comment.link_abuse = comment.table_2.getElementsByTagName("a")[2];
				comment.link_abuse.index = comment.index;
				comment.link_abuse._id = comment.id;
				
				//click YES
				comment.link_yes.onclick = function(){
					//alert(LOCALPATH+"comment_setutility.php?"+'comment_id='+this._id+'&'+'answer='+'yes');
					var index = this.index;
					www.post(LOCALPATH+"comment_setutility.php",
						 'comment_id='+this._id+'&'+
						 'answer='+'yes', 
						 function(response) {
							//alert(response)
							var no_answer = String(response).split(",")[0];
							var totals = String(response).split(",")[1];
							JSObject.changeStatus(index, no_answer, totals);
						 }
						 );	
				}
					
				//click NO
				comment.link_no.onclick = function(){
					//alert(LOCALPATH+"comment_setutility.php?"+'comment_id='+this._id+'&'+'answer='+'no');
					var index = this.index;
					www.post(LOCALPATH+"comment_setutility.php",
						 'comment_id='+this._id+'&'+
						 'answer='+'no', 
						 function(response) {
							//alert(response)
							var no_answers = String(response).split(",")[0];
							var no_totals = String(response).split(",")[1];
							JSObject.changeStatus(index, no_answers, no_totals);
						 }
						 );		
				}
				
				
				//click ABUSE
				comment.link_abuse.onclick = function(){
					var conf = confirm("Sunteti sigur ca doriti sa raportati acest comentariu ca fiind injurios?");
					
					if (!(conf)) return false;
					
					//alert(LOCALPATH+"comment_alert.php?"+'comment_id='+this._id);
					var index = this.index;
					www.post(LOCALPATH+"comment_alert.php",
						 'comment_id='+this._id,
						 function(response) {
							//alert(response)
							JSObject.changeAbuse(index);
						 }
						 );		
				}
			}
			else{
				comment.link_yes = null;
				comment.link_no = null;
				comment.link_abuse = null;
			}
			
			this.comments.push(comment);
		}
	}
	
	
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                                         FUNCTION CHANGE STATUS                                    */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	//schimbam statusul de utilitate al unui comentariu
	this.changeStatus = function(index, no_answers, no_totals){
		var comment = this.comments[index];
		
		//sterg toate randurile mai putin primul
		while (comment.table_2.rows.length > 1){
			comment.table_2.deleteRow(comment.table_2.rows.length-1);
		}
		
		comment.table_2.rows[0].cells[0].innerHTML = '<span class="guaranty_grey_text">Va multumim pentru parerea dumneavoastra!</span>';	
		comment.table_1.rows[0].cells[0].innerHTML = '<span class="comment_text"><b>'+no_answers+'</b> din <b>'+no_totals+'</b> utilizatori au gasit acest comentariu util.</span>'
		
	}
	
	
	
	/*****************************************************************************************************/
	/*                                                                                                   */
	/*                                         FUNCTION CHANGE ABUSE                                     */          
	/*                                                                                                   */
	/*****************************************************************************************************/
	//schimbam statusul de abuz al unui comentariu
	this.changeAbuse = function(index){
		var comment = this.comments[index];
		
		//sterg toate randurile mai putin primul
		while (comment.table_2.rows.length > 1){
			comment.table_2.deleteRow(comment.table_2.rows.length-1);
		}
		
		comment.table_2.rows[0].cells[0].innerHTML = '<span class="guaranty_grey_text">Va multumim pentru semnalarea dumneavoastra!</span>';	
		
	}
	
}