function CommentController()
{
	this.reqWidgetQueue= new Array();//queue with the widgets that made a request through this controller object
	var self=this;
	this.localWidgetReff = null;
	this.ref=null;
	this.type='banc';
	this.ref_err=null;
	this.ref_err_ok=null;
	this.ref_nr=null;
	this.applycomment=function(banc_id,comment)
	{
		comment=comment.split("\n").join("<br>");
		this.ref=banc_id;
		this.ref_err=document.getElementById('mesaj_err_comm');
		this.ref_err_ok=document.getElementById('mesaj_err_comm_ok');
		var rqid=RequestManager.getInstance().createRequest(CommentController.getInstance(),URL+"index.php?page=banc&action=post_comment&banc_id="+banc_id+'&comment='+comment);
		RequestManager.getInstance().startRequest(rqid);
		this.ref_img=document.getElementById('adauga_place');
		this.ref_img.innerHTML='<img src="'+URL+'images/ajax-loader.gif">';
		
	}
	this.applycommentpoza=function(poza_id,comment)
	{
		this.type='poza';
		comment=comment.split("\n").join("<br>");
		this.ref=poza_id;
		this.ref_err=document.getElementById('mesaj_err_comm');
		this.ref_err_ok=document.getElementById('mesaj_err_comm_ok');
		var rqid=RequestManager.getInstance().createRequest(CommentController.getInstance(),URL+"index.php?page=poza&action=post_comment&poza_id="+poza_id+'&comment='+comment);
		RequestManager.getInstance().startRequest(rqid);
		this.ref_img=document.getElementById('adauga_place');
		this.ref_img.innerHTML='<img src="'+URL+'images/ajax-loader.gif">';
		
	}
	this.applycommentfilm=function(film_id,comment)
	{
		this.type='film';
		comment=comment.split("\n").join("<br>");
		this.ref=film_id;
		this.ref_err=document.getElementById('mesaj_err_comm');
		this.ref_err_ok=document.getElementById('mesaj_err_comm_ok');
		var rqid=RequestManager.getInstance().createRequest(CommentController.getInstance(),URL+"index.php?page=film&action=post_comment&film_id="+film_id+'&comment='+comment);
		RequestManager.getInstance().startRequest(rqid);
		this.ref_img=document.getElementById('adauga_place');
		this.ref_img.innerHTML='<img src="'+URL+'images/ajax-loader.gif">';
		
	}
	this.applycommentdesen=function(desen_id,comment)
	{
		this.type='desen';
		comment=comment.split("\n").join("<br>");
		this.ref=desen_id;
		this.ref_err=document.getElementById('mesaj_err_comm');
		var rqid=RequestManager.getInstance().createRequest(CommentController.getInstance(),URL+"index.php?page=desen&action=post_comment&desen_id="+desen_id+'&comment='+comment);
		RequestManager.getInstance().startRequest(rqid);
		this.ref_img=document.getElementById('adauga_place');
		this.ref_img.innerHTML='<img src="'+URL+'images/ajax-loader.gif">';
		
	}
	this.applycommentjoc=function(joc_id,comment)
	{
		this.type='joc';
		comment=comment.split("\n").join("<br>");
		this.ref=joc_id;
		this.ref_err=document.getElementById('mesaj_err_comm');
		this.ref_err_ok=document.getElementById('mesaj_err_comm_ok');
		var rqid=RequestManager.getInstance().createRequest(CommentController.getInstance(),URL+"index.php?page=joc&action=post_comment&joc_id="+joc_id+'&comment='+comment);
		RequestManager.getInstance().startRequest(rqid);
		this.ref_img=document.getElementById('adauga_place');
		this.ref_img.innerHTML='<img src="'+URL+'images/ajax-loader.gif">';
		
	}

	this.applyraspunscomment=function(intrebare_id,comment)
	{
		this.type='intrebare';
		comment=comment.split("\n").join("<br>");
		this.ref=intrebare_id;
		this.ref_err=document.getElementById('mesaj_err_comm'+intrebare_id);
		var rqid=RequestManager.getInstance().createRequest(CommentController.getInstance(),URL+"index.php?page=intrebare&action=post_raspuns&intrebare_id="+intrebare_id+'&comment='+comment);
		RequestManager.getInstance().startRequest(rqid);
	}
	this.applyraspunscommentall=function(intrebare_id,comment)
	{
		this.type='intrebare_all';
		comment=comment.split("\n").join("<br>");
		this.ref=intrebare_id;
		this.ref_err=document.getElementById('mesaj_err_comm'+intrebare_id);
		var rqid=RequestManager.getInstance().createRequest(CommentController.getInstance(),URL+"index.php?page=intrebare&action=post_raspuns&intrebare_id="+intrebare_id+'&comment='+comment);
		RequestManager.getInstance().startRequest(rqid);
		this.ref_img=document.getElementById('adauga_place');
		this.ref_img.innerHTML='<img src="'+URL+'images/ajax-loader.gif">';
		
	}
	
	this.removecomment=function(banc_id,comment_id)
	{
		this.ref=banc_id;
		this.ref_err=document.getElementById('mesaj_err_comm');
		this.ref_err_ok=document.getElementById('mesaj_err_comm_ok');
		var rqid=RequestManager.getInstance().createRequest(CommentController.getInstance(),URL+"index.php?page=banc&action=remove_comment&banc_id="+banc_id+'&user_comment_id='+comment_id);
		RequestManager.getInstance().startRequest(rqid);
	}
	this.removecommentpoza=function(poza_id,comment_id)
	{
		this.type='poza';
		this.ref=poza_id;
		this.ref_err=document.getElementById('mesaj_err_comm');
		this.ref_err_ok=document.getElementById('mesaj_err_comm_ok');
		
		var rqid=RequestManager.getInstance().createRequest(CommentController.getInstance(),URL+"index.php?page=poza&action=remove_comment&poza_id="+poza_id+'&user_comment_id='+comment_id);
		RequestManager.getInstance().startRequest(rqid);
	}
	this.removecommentfilm=function(film_id,comment_id)
	{
		this.type='film';
		this.ref=film_id;
		this.ref_err=document.getElementById('mesaj_err_comm');
		this.ref_err_ok=document.getElementById('mesaj_err_comm_ok');
		var rqid=RequestManager.getInstance().createRequest(CommentController.getInstance(),URL+"index.php?page=film&action=remove_comment&film_id="+film_id+'&user_comment_id='+comment_id);
		RequestManager.getInstance().startRequest(rqid);
	}
	this.removecommentdesen=function(desen_id,comment_id)
	{
		this.type='desen';
		this.ref=desen_id;
		this.ref_err=document.getElementById('mesaj_err_comm1');
		var rqid=RequestManager.getInstance().createRequest(CommentController.getInstance(),URL+"index.php?page=desen&action=remove_comment&desen_id="+desen_id+'&user_comment_id='+comment_id);
		RequestManager.getInstance().startRequest(rqid);
	}
	this.removecommentjoc=function(joc_id,comment_id)
	{
		this.type='joc';
		this.ref=joc_id;
		this.ref_err=document.getElementById('mesaj_err_comm');
		this.ref_err_ok=document.getElementById('mesaj_err_comm_ok');
		var rqid=RequestManager.getInstance().createRequest(CommentController.getInstance(),URL+"index.php?page=joc&action=remove_comment&joc_id="+joc_id+'&user_comment_id='+comment_id);
		RequestManager.getInstance().startRequest(rqid);
	}
	
	/**
	 * @param The manager index of the request 
	 * @desc This method is a handler for the completition of one request
	 */
	this.handleResponse=function (managerIndex)
	{	
		this.ref_err.innerHTML="";
		this.ref_err_ok.innerHTML="";
		var respContent=RequestManager.getInstance().getRequestObj(managerIndex).getResponseContent();
		result=eval(respContent);
		this.ref_err.innerHTML=result[0]['value'];
		this.ref_err_ok.innerHTML=result[0]['value'];

		if(this.type!='intrebare')
		{
			this.ref_img=document.getElementById('adauga_place');
			this.ref_img.innerHTML='&nbsp;';				
		}
		if(result[0]['text']=='ok')
		{
			this.ref_err_ok.innerHTML=result[0]['value'];
			this.ref_err.innerHTML='';
			if(this.type=='intrebare' || this.type=='intrebare_all')
			{
				document.getElementById('comm_nr'+this.ref).innerHTML=result[1]['value'];
			}
			else
			{
				document.getElementById('comm_nr').innerHTML=result[1]['value'];
			}
			//call to get list of tags and to refresh existing tag list
			if(this.type=='banc')
			{
				RequestManager.getInstance().createRequest(CommentListController.getInstance().getCommentsList(this.ref));
				changeShowHideState('comments','add_comment');
			}
			else if(this.type=='intrebare')
			{
				RequestManager.getInstance().createRequest(CommentListController.getInstance().getResponsesList(this.ref));
			}			
			else if(this.type=='intrebare_all')
			{
				RequestManager.getInstance().createRequest(CommentListController.getInstance().getResponsesListAll(this.ref));
			}			

			else if(this.type=='poza')
			{
				
				RequestManager.getInstance().createRequest(CommentListController.getInstance().getPozeCommentsList(this.ref));
				changeShowHideState('comments','add_comment');
			}
			else if(this.type=='film')
			{
				RequestManager.getInstance().createRequest(CommentListController.getInstance().getFilmeCommentsList(this.ref));
				changeShowHideState('comments','add_comment');
			}
			else if(this.type=='joc')
			{
				RequestManager.getInstance().createRequest(CommentListController.getInstance().getJocuriCommentsList(this.ref));
				changeShowHideState('comments','add_comment');
			}
			else
			{
				RequestManager.getInstance().createRequest(CommentListController.getInstance().getDeseneCommentsList(this.ref));
			}
		}
		if(result[0]['text']=='vote_ok')
		{
			if(result[1]['value']>=0)
			{
				if(result[1]['value']>0)
				{
					nr_show="+"+result[1]['value'];
				}
				else
				{
					nr_show=result[1]['value'];
				}
				document.getElementById('ref_nr_'+this.ref_nr).innerHTML=nr_show;
			}
			else
			{
				nr_show=result[1]['value'];
				document.getElementById('ref_nr_'+this.ref_nr).innerHTML=nr_show;
			}

		}
		
		return true;					
	}
}

CommentController.__instance__=null;

CommentController.getInstance=function()
{
	if(CommentController.__instance__==null)
	{
		CommentController.__instance__=new CommentController();
	}
	return CommentController.__instance__;
}
