var xhttp;
var _elementName;
var _imageName1;
var _imageName2;
var _imageName3;
var _imageName4;
var _imageName5;
var _XH_ACTIVE_X_IDENTS=["MSXML2.XMLHTTP.5.0","MSXML2.XMLHTTP.4.0","MSXML2.XMLHTTP.3.0","MSXML2.XMLHTTP","MICROSOFT.XMLHTTP.1.0","MICROSOFT.XMLHTTP.1","MICROSOFT.XMLHTTP"];
var _xh_ieProgId=undefined;

function _XH_XmlHttpInit()
{
	if(typeof XMLHttpRequest=="undefined" && typeof ActiveXObject!="undefined")
	{
		for(var a=0;a<_XH_ACTIVE_X_IDENTS.length;a++)
		{
			var b=_XH_ACTIVE_X_IDENTS[a];
			try
			{
				new ActiveXObject(b);
				_xh_ieProgId=b;
				break;
			}
			catch(c)
			{
			}
		}
		
		if(undefined===_xh_ieProgId)
		{
			throw"Could not create ActiveXObject. ActiveX might be disabled, or msxml might not be installed";
		}
	}
}

function Fa()
{
	if(_xh_ieProgId!==undefined)
	{
		return new ActiveXObject(_xh_ieProgId)
	}
	else
	{
		return new XMLHttpRequest
	}
}

function RatePageHandlerOnReadyStateChange()
{
	var url = window.location.href;

	if (xhttp.readyState == 4)
	{
		var rating = xhttp.responseText.substring(0,1);
		var message = xhttp.responseText.substring(2);
		
		var oDiv = document.getElementById(_elementName);
		var oRating1 = document.getElementById(_imageName1);
		var oRating2 = document.getElementById(_imageName2); 
		var oRating3 = document.getElementById(_imageName3); 
		var oRating4 = document.getElementById(_imageName4); 
		var oRating5 = document.getElementById(_imageName5);
		
		oDiv.innerHTML = message;
		
		if (rating != null && rating != '' && typeof rating != "undefined")
		{
			if (rating >= 1)
			{
				oRating1.src = "/images/article_star.gif";
			}
			else
			{
				oRating1.src = "/images/article_star_disabled.gif";
			}
			
			if (rating >= 2)
			{
				oRating2.src = "/images/article_star.gif";
			}
			else
			{
				oRating2.src = "/images/article_star_disabled.gif";
			}
			
			if (rating >= 3)
			{
				oRating3.src = "/images/article_star.gif";
			}
			else
			{
				oRating3.src = "/images/article_star_disabled.gif";
			}
			
			if (rating >= 4)
			{
				oRating4.src = "/images/article_star.gif";
			}
			else
			{
				oRating4.src = "/images/article_star_disabled.gif";
			}
			
			if (rating >= 5)
			{
				oRating5.src = "/images/article_star.gif";
			}
			else
			{
				oRating5.src = "/images/article_star_disabled.gif";
			}
		}
	}
}

function ratePage(rating, elementName, contentID, imageName1, imageName2, imageName3, imageName4, imageName5)
{
		_XH_XmlHttpInit();
		xhttp = Fa();
		
		// Set the name of the element to write the XML response to
		_elementName = elementName;
		
		// Set the client ids of the images
		_imageName1 = imageName1;
		_imageName2 = imageName2;
		_imageName3 = imageName3;
		_imageName4 = imageName4;
		_imageName5 = imageName5;
		
        //hook the event handler
        xhttp.onreadystatechange = RatePageHandlerOnReadyStateChange;
        
        //prepare the call, http method=GET, false=asynchronous call
        xhttp.open("POST", "/scripts/rate_page.ashx", true);
        xhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        
        // finally send the call with 2 parameters since we cannot get a reference to the sitecore
        // context from with the ajax call
        var paramaters = "rating=" + rating + "&id=" + contentID;
        
        xhttp.send(paramaters);
}
