
function set_reply_to(cid)
{
	document.getElementById('reply_to_field').value = cid;
	var titleObj = document.getElementById('title_' + cid);
	if(titleObj != null)
	{
		var title = titleObj.innerHTML;
		
		if(title.substring(0,4) != 'RE: ')
		{ title = 'RE: ' + title; }
		
		document.getElementById('comment_title').value = my_unescape(title);
	}
	
	document.getElementById('comment_title').focus();	
}


function my_unescape(val)
{
	val = unescape(val);
	val = val.replace(/&amp;/g,"&");
	val = val.replace(/&quot;/g,"\"");
	val = val.replace(/&#039;/g,"'");
	val = val.replace(/&lt;/g,"<");
	val = val.replace(/&gt;/g,">");
	return val;
}


function quote(cid)
{
	set_reply_to(cid);
	var obj = getCommentObj(cid);
	obj.quote();
}

function getHTTPObject() {

  var xmlhttp;

  /*@cc_on

  @if (@_jscript_version >= 5)

    try {

      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");

    } catch (e) {

      try {

        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");

      } catch (E) {

        xmlhttp = false;

      }

    }

  @else

  xmlhttp = false;

  @end @*/

  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {

    try {

      xmlhttp = new XMLHttpRequest();

    } catch (e) {

      xmlhttp = false;

    }

  }

  return xmlhttp;

}

function rateComment(cid, val)
{
	var obj = getCommentObj(cid)
	obj.rate(val);
}

httpObjs = new Array();
httpObjIndex = 0;
function getNewHttpObj()
{
	var obj = getHTTPObject();
	httpObjs[httpObjIndex] = obj;
	httpObjIndex++;
	return obj;
}

function handleResponse()
{
	for(var i = 0; i < httpObjs.length; i++)
	{
		
		if(httpObjs[i].value == "") 
		{ continue; }
		
		if(httpObjs[i].readyState != 4)
		{ continue; }
			
		var results = httpObjs[i].responseText.split("\n");
		var subresults;
		for(var j = 0; j < results.length; j++)
		{
			subresults = results[j].split("=");
			if(subresults[0] == "cid")
			{
				comment = getCommentObj(subresults[1]);
				comment.handleResponse(httpObjs[i]);
				httpObjs[i] = "";
			}	
		}
	}
}


function handleQuote()
{
	for(var i = 0; i < httpObjs.length; i++)
	{
		if(httpObjs[i].value == "") 
		{ continue; }
		
		if(httpObjs[i].readyState != 4)
		{ continue; }
		
		var results = httpObjs[i].responseText.split("\n");
		var subresults;
		for(var j = 0; j < results.length; j++)
		{
			subresults = results[j].split("=");
			if(subresults[0] == "cid")
			{
				comment = getCommentObj(subresults[1]);
				comment.handleQuote(httpObjs[i]);
				httpObjs[i] = "";
			}	
		}
	}	
}


function getCommentObj(cid)
{
	for(var i=0; i < comments.length; i++)
	{
		if(comments[i].cid == cid)
		{
			return comments[i];
		}	
	}
	
	comments[i] = new Comment(cid);
	return comments[i];
}

function Comment(cid)
{
	this.cid = cid;
	this.displayed = true;
	
	this.display = function() 
	{ 
		return true; 
	};
	
	this.rate = function(vote)
	{
		var obj = getNewHttpObj();
		obj.open("GET", "/rate_comment.php?cid=" + this.cid + "&rating=" + vote, true);
		obj.onreadystatechange = handleResponse;
		obj.send(null);
	};
	
	this.quote = function()
	{
		var obj = getNewHttpObj();
		obj.open("GET", "/quote_comment.php?cid=" + this.cid, true);
		obj.onreadystatechange = handleQuote;
		obj.send(null);
	};
	
	this.handleQuote = function(obj)
	{
		/*alert(obj.responseText);*/
		var results = obj.responseText.split("\n");
		var subresults;
		for(var j = 0; j < results.length; j++)
		{
			subresults = results[j].split("=");
			
			switch(subresults[0])
			{
				case 'quote':
					this.updateQuoteBox(subresults[1]);
				break;
			}
		}	
	}
	
	this.updateQuoteBox = function(str)
	{
		str =  my_unescape(str);
		str = str.replace(/\+/g," ");
		document.getElementById('comment_textarea').value = "[quote]" + str + "[/quote]";
	};
	
	this.handleResponse = function(obj)
	{
		
		var results = obj.responseText.split("\n");
		var subresults;
		for(var j = 0; j < results.length; j++)
		{
			subresults = results[j].split("=");
			
			switch(subresults[0])
			{
				case 'rating':
					this.updateRating(subresults[1]);
				break;
				case 'msg':
					alert(subresults[1]);
				break;
				case 'result':
					this.handleResult(subresults[1]);
				break;	
			}
		}
		//alert('Back: ' + obj.responseText);
	};
	
	this.updateRating = function(rating)
	{
		var ratingId = document.getElementById('rating_' + this.cid);
		ratingId.innerHTML = rating;
	};
	
	this.handleResult = function(result)
	{
		if(result != 'true')
		{ return; }	
		
		var rateCtrls = document.getElementById('ratectrls_' + this.cid);
		rateCtrls.style.display = 'none';
	};
}

function toggleCommentDisplay(cid)
{
	var el = document.getElementById('comment_text_' + cid);
	if(!el)
	{
		return;
	}
	if(el.style.display == 'none')
	{
		el.style.display = '';
	}
	else
	{
		el.style.display = 'none';
	}
}
