// Handset page reviews JavaScript

function requestReviews(hsId, pageNo)
{
	var xmlHttp = getXMLHttp();

	xmlHttp.onreadystatechange = function()
	{
		if(xmlHttp.readyState == 4)
		{
			handleResponse(xmlHttp.responseText);
		}
		else
		{
			$("#handsetReviewsContainer").html('<div id="loadingreviews"><p>Loading reviews...</p></div>');
		}
	}

	xmlHttp.open("GET", "/handset-specific-pages-ajax.php?reviewrequest=true&hsid=" + hsId + "&pageno=" + pageNo, true);
	xmlHttp.send(null);
}

function handleResponse(response)
{
	$("#handsetReviewsContainer").fadeOut("fast", function()
	{
		$("#handsetReviewsContainer").html(response);
		$("#handsetReviewsContainer").fadeIn("fast");
	});
}

function requestNewPage(e)
{
	var handsetId   = $("#hsid").val();
	var urlPageNo   = $("#reviewsPage").val();

	// Call AJAX.
	requestReviews(handsetId, urlPageNo);

}

function goSelectPage(e)
{
	$("#reviewsPage").val($("#revSelectBox").val());
	requestNewPage(e);
	checkLinkHighlight();
}

function goPrevPage(e)
{
	if(document.getElementById("reviewsPage").value != 0)
	{
		var newCurrentPageNo = parseInt($("#reviewsPage").val()) - 1;
		$("#reviewsPage").val(newCurrentPageNo);
		$("#revSelectBox").val(newCurrentPageNo);
		requestNewPage(e);
	}

	checkLinkHighlight();
	return false;
}

function goNextPage(e)
{
	if((parseInt($("#reviewsPage").val()) + 1) != parseInt($("#totalRevPages").val()))
	{
		var newCurrentPageNo = parseInt($("#reviewsPage").val()) + 1;
		$("#reviewsPage").val(newCurrentPageNo);
		$("#revSelectBox").val(newCurrentPageNo);
		requestNewPage(e);
	}

	checkLinkHighlight();
	return false;
}

function checkLinkHighlight()
{
	if($("#reviewsPage").val() == 0)
	{
		$("#prevpagelink").css({ color: "#999999", cursor: "default" });
	}
	else
	{
		$("#prevpagelink").css({ color: "#003399", cursor: "pointer" });
	}
	
	if((parseInt($("#reviewsPage").val()) + 1) == parseInt($("#totalRevPages").val()) || $("#totalRevPages").val() <= 1)
	{
		$("#nextpagelink").css({ color: "#999999", cursor: "default" });
	}
	else
	{
		$("#nextpagelink").css({ color: "#003399", cursor: "pointer" });
	}
}

function setReviewsPagenationLinks()
{
	if($("#revSelectBox").length)
	{
		$("#revSelectBox").change(function()
		{
			goSelectPage();
		});
	}
	
	if($("#prevpagelink").length)
	{
		$("#prevpagelink").click(function()
		{
			goPrevPage();
		});
	}
	
	if($("#nextpagelink").length)
	{
		$("#nextpagelink").click(function()
		{
			goNextPage();
		});
	}
	
	$("#reviewsPage").val(0);
}