var xmlHttp;

/* Please always make the start index a even number, ie 0! */
function adjustRating(szIdHeader, iStartIndex, iEndIndex, iCurrentIndex, iType)
{
	
	var iX; 
	if(iCurrentIndex == -1)
	{
		for(iX = iStartIndex; iX <= iEndIndex; iX++)
		{
			if((iX % 2)==0)
			{
				document.getElementById(szIdHeader + iX).className = 'ratingUBottomOff';
			} else {
				document.getElementById(szIdHeader + iX).className = 'ratingUTopOff';
			}
		}
	} else {
		for(iX = iStartIndex; iX <= iEndIndex; iX++)
	{
		if(iX <= iCurrentIndex)
		{
			//Switch the star on
			if((iX % 2)==0)
			{
				if(iType == 0)
				{
					document.getElementById(szIdHeader + iX).className = 'ratingBottomOn';
				} else {
					document.getElementById(szIdHeader + iX).className = 'ratingABottomOn';
				}
			} else {
				if(iType == 0)
				{
					document.getElementById(szIdHeader + iX).className = 'ratingTopOn';
				} else {
					document.getElementById(szIdHeader + iX).className = 'ratingATopOn';
				}
			}
			
		} else {
			//Switch the star off
			if((iX % 2)==0)
			{
				if(iType == 0)
				{
					document.getElementById(szIdHeader + iX).className = 'ratingBottomOff';
				} else {
					document.getElementById(szIdHeader + iX).className = 'ratingABottomOff';
				}
			} else {
				if(iType == 0)
				{
					document.getElementById(szIdHeader + iX).className = 'ratingTopOff';
				} else {
					document.getElementById(szIdHeader + iX).className = 'ratingATopOff';
				}
			}
		}
	}
	}
	
}

function createXMLHttpRequest() {
	//Create object dependent on browser
	if (window.ActiveXObject) {
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	} 
	else if (window.XMLHttpRequest) {
		xmlHttp = new XMLHttpRequest();
	}
}

function startRequest(iPic,iRating,iDays,iUsersRating) {
	var szUrl="/rate_matchPic.asp?ajax=true&iUsers=" + iUsersRating + "&iPic=" + iPic + "&iRating=" + iRating + "&iDays=" + iDays;
	createXMLHttpRequest();
	xmlHttp.onreadystatechange = handleStateChange;
	xmlHttp.open("GET", szUrl, true);
	xmlHttp.send(null);
	document.getElementById(iPic + "status").className = 'ratingStatusSaving';
}

function handleStateChange() {
	if(xmlHttp.readyState == 4) {
		if(xmlHttp.status == 200) {
			var root = xmlHttp.responseXML.getElementsByTagName('root')[0];
			var iPic = root.getElementsByTagName('ratingResults')[0].getAttribute('pic_id');
			var iRating = root.getElementsByTagName('ratingResults')[0].getAttribute('rating');
			var iNewVoteTotal = root.getElementsByTagName('ratingResults')[0].getElementsByTagName('voteTotal')[0].firstChild.nodeValue;
			var iNewVoteCount = root.getElementsByTagName('ratingResults')[0].getElementsByTagName('voteCount')[0].firstChild.nodeValue;
			var iNewVoteAverage = root.getElementsByTagName('ratingResults')[0].getElementsByTagName('newRating')[0].firstChild.nodeValue;
			iNewVoteAverage=iNewVoteAverage/10;
			
			document.getElementById(iPic + "status").className = 'ratingStatusSaved';
			document.getElementById(iPic + "rating").innerHTML = iNewVoteAverage;
			document.getElementById(iPic + "count").innerHTML = iNewVoteCount;
			
			adjustRating(iPic, 0, 9, iRating-1, 0)
			starCount[iPic] = iRating-1;
		} else {
		}
	}
}

function showStatus(picid)
{
	nameOfClass = document.getElementById(picid + "status").className;
	
	if(nameOfClass == "ratingStatusBlank")
	{
		document.getElementById(picid + "status").className = 'ratingStatusRate';
	}
}

function hideStatus(picid)
{
		nameOfClass = document.getElementById(picid + "status").className;
	
		if(nameOfClass == "ratingStatusRate")
		{
			document.getElementById(picid + "status").className = 'ratingStatusBlank';
		}
}

