
//Our XmlHttpRequest object to get the auto suggest
var httpReq = getXmlHttpRequestObject();

var entityID = '';
var commentsAvailable = 0;
var typeID = '';

var minR = 0;
var maxR = 8;

var perPage = maxR;

function initComments(entity,type,cAvailable)
{
	entityID 		= entity;
	typeID			= type;
	commentsAvailable = cAvailable;
	
	commentChange(0);
}
// Eventually this will work
function addComment(form)
{
	var objID = form.objID;
	var tid = form.typeID;
	var msg = form.WallCommentArea;
	
	if (httpReq.readyState == 4 || httpReq.readyState == 0) 
	{
		var params = "add=new&objID="+objID+"&tid="+tid+"&msg="+msg;
		var url = '/ajax/comments.php';
		
		commentsAvailable++;
		
		httpReq.open("POST", url, false);
		
		//Send the proper header information along with the request
		httpReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		httpReq.setRequestHeader("Content-length", params.length);
		httpReq.setRequestHeader("Connection", "close");
		
		httpReq.onreadystatechange = function() {//Call a function when the state changes.
			if(httpReq.readyState == 4 && httpReq.status == 200) {
				alert(httpReq.responseText);
			}
		}
		httpReq.send(params);
		
		commentChange(0);
	}
}
function deleteComment(cid,tid)
{
	if (httpReq.readyState == 4 || httpReq.readyState == 0) 
	{
		var params = "rm="+cid;
		var url = '/ajax/comments.php';
		
		commentsAvailable -= 1;
		
		httpReq.open("POST", url, false);
		
		httpReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		httpReq.setRequestHeader("Content-length", params.length);
		httpReq.setRequestHeader("Connection", "close");
		
		httpReq.onreadystatechange = function() {//Call a function when the state changes.
			if(httpReq.readyState == 4 && httpReq.status == 200) {
				//alert(httpReq.responseText);
				startGrowl("Comment Removed","Your comment has been deleted.");
			}
		}
		httpReq.send(params);
		
		commentChange(0);
	}
}
//Called from link on vehicleProfile
//Starts the AJAX request.
function commentChange(dir) 
{
	var ic = document.getElementById('commentWrapper');
	
	$("#commentWrapper").html("<img src=\"/images/icons/ajax-loader.gif\" alt=\"loading\" />");
	
	try { gradientshadow.position(); }
	catch(err) { }
	
	if (httpReq.readyState == 4 || httpReq.readyState == 0) 
	{
		if(dir != 0)
		{
			$("#commentWrapper").css("min-height",300);;
		}
		if(commentsAvailable > maxR && dir == 0)
		{
			document.getElementById('NextCommentLink').style.display = "block";
		}
		else if(maxR < commentsAvailable && dir == 1)
		{
			minR += perPage;
			maxR += perPage;
			
			document.getElementById('PrevCommentLink').style.display = "block";
			
			if(maxR >= commentsAvailable)
				document.getElementById('NextCommentLink').style.display = "none";
		}
		else if(dir == -1)
		{
			if(minR > 0)
			{
				minR -= perPage;
				maxR -= perPage;
				
				if(minR < 0)
					minR = 0;
				
				if(minR  == 0)
					document.getElementById('PrevCommentLink').style.display = "none";
				
				document.getElementById('NextCommentLink').style.display = "block";
			}
		}
		else
		{
			minR = 0;
			maxR = perPage;
			
			document.getElementById('NextCommentLink').style.display = "none";
			document.getElementById('PrevCommentLink').style.display = "none";
		}	
		
		var url = '/ajax/comments.php?min='+minR+'&max='+maxR+'&entityID='+entityID+'&tid='+typeID;
		
		httpReq.open("GET", url, true);
		httpReq.onreadystatechange = handleCommentChange; 
		httpReq.send(null);
	}
}

//Called when the AJAX response is returned.
function handleCommentChange() 
{
	if (httpReq.readyState == 4) 
	{
		var ic = document.getElementById('commentWrapper');
		
		var str = httpReq.responseText;
		
		ic.innerHTML = str;
		
		$("#commentWrapper").css("height","auto");
		
		try { gradientshadow.position(); }
		catch(err) { }

	}
}