
function getXmlHttpRequest()
{
	var httpRequest = null;
	try
	{
		httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch (e)
	{
		try
		{
			httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch (e)
		{
			httpRequest = null;
		}
	}
	if (!httpRequest && typeof XMLHttpRequest != "undefined")
	{
		httpRequest = new XMLHttpRequest();
	}
	return httpRequest;
}

function postUrl(url, data, async, stateChangeCallback)
{ 
	var xmlHttpReq = getXmlHttpRequest(); 

	if (!xmlHttpReq)
		return;

	xmlHttpReq.open("POST", url, async);
	xmlHttpReq.onreadystatechange = function()
		{
			stateChangeCallback(xmlHttpReq);
		};
	xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlHttpReq.send(data);
	//alert ('url: ' + url + '\ndata: ' + data);
}


function postFormByForm(form, async, successCallback) {
	var formVars = new Array();
	for (var i = 0; i < form.elements.length; i++)
	{
		var formElement = form.elements[i];
		
		// Special handling for checkboxes (we need an array of selected checkboxes..)!
		if(formElement.type=='checkbox' && !formElement.checked) {
			continue;
		} 
		var v=new Object;
		v.name=formElement.name;
		v.value=formElement.value;
		formVars.push(v);		
	} 
	postUrl(form.action, urlEncodeDict(formVars), async, execOnSuccess(successCallback));
}

function postForm(formName, async, successCallback)
{
	// postFormByName
	var form = document.forms[formName];
	return postFormByForm(form, async, successCallback);
}

function urlEncodeDict(dict)
{ 
	var result = "";
	for (var i=0; i<dict.length; i++) {
		result += "&" + encodeURIComponent(dict[i].name) + "=" + encodeURIComponent(dict[i].value);
	}
	return result;
}

function execOnSuccess(stateChangeCallback)
{
	return function(xmlHttpReq)
		{
			if (xmlHttpReq.readyState == 4 &&
					xmlHttpReq.status == 200)
				stateChangeCallback(xmlHttpReq);
			//alert(xmlHttpReq + " " + xmlHttpReq.readyState + " " + xmlHttpReq.status);
		};
}


function replaceDivContents(xmlHttpRequest, dstDivId)
{
	var dstDiv = document.getElementById(dstDivId);
	dstDiv.innerHTML = xmlHttpRequest.responseText;
}

//var UT_RATING_IMG = '/images/icn_star_full_19x20.gif';
//var UT_RATING_IMG_HALF = '/images/icn_star_half_19x20.gif';
//var UT_RATING_IMG_BG = '/images/icn_star_empty_19x20.gif';

var UT_RATING_IMG = '/images/cherry_full.gif';
var UT_RATING_IMG_HALF = '/images/cherry_half_full.gif';
var UT_RATING_IMG_BG = '/images/cherry_empty.gif';

function UTRating(ratingElementId, maxStars, objectName, formName, ratingMessageId, componentSuffix, size, messages, starCount, callback)
{
	this.ratingElementId = ratingElementId;
	this.maxStars = maxStars;
	this.objectName = objectName;
	this.formName = formName;
	this.ratingMessageId = ratingMessageId
	this.componentSuffix = componentSuffix
	this.messages = messages;
	this.callback = callback;

	this.starTimer = null;
	this.starCount = 0;
	if (starCount) {
		this.starCount = starCount; 
		that = this;
		onLoadFunctionList.push(function() { that.drawStars(that.starCount, true); });
	}

	if(size=='S') {
		UT_RATING_IMG      = '/images/icn_star_full_11x11.gif'
		UT_RATING_IMG_HALF = '/images/icn_star_half_11x11.gif'
		UT_RATING_IMG_BG   = '/images/icn_star_empty_11x11.gif'
	}
	
	// pre-fetch image
	(new Image()).src = UT_RATING_IMG;
	(new Image()).src = UT_RATING_IMG_HALF;

	function showStars(starNum, skipMessageUpdate) {
		this.clearStarTimer();

		this.greyStars();
		this.colorStars(starNum);
		if(!skipMessageUpdate)
			this.setMessage(starNum, messages);
	}

	function setMessage(starNum) {
		if (starNum>0) {
			if (!this.savedMessage) {
				this.savedMessage = document.getElementById(this.ratingMessageId).innerHTML;
			}
			document.getElementById(this.ratingMessageId).innerHTML = this.messages[starNum-1];
		} else 
		{
		    if (this.savedMessage) {
			    document.getElementById(this.ratingMessageId).innerHTML = this.savedMessage; 
		    }
		}
		    
	}

	function colorStars(starNum) {
	    //alert( starNum );
	    
		for (var i=0; i < starNum; i++) {
			document.getElementById('star_'  + this.componentSuffix + "_" + (i+1)).src = UT_RATING_IMG;
		}
		
		if ( i - starNum > 0 )
		    document.getElementById('star_'  + this.componentSuffix + "_" + (i)).src = UT_RATING_IMG_HALF;
	}

	function greyStars() {
		for (var i=0; i < this.maxStars; i++)
			if (i <= this.starCount) {
				document.getElementById('star_' + this.componentSuffix + "_"  + (i+1)).src = UT_RATING_IMG_BG;
			}
			else
			{
				document.getElementById('star_' + this.componentSuffix + "_"  + (i+1)).src = UT_RATING_IMG_BG;
			}
	}

	function setStars(starNum) {
		this.starCount = starNum;
		this.drawStars(starNum);
		document.forms[this.formName]['rating'].value = this.starCount;
		var ratingElementId = this.ratingElementId;
		that = this;
		postForm(this.formName, true, function (req) { 
				replaceDivContents(req, ratingElementId); 
				if (that.callback) {
					that.callback();
				}
		});
	}


	function drawStars(starNum, skipMessageUpdate) {
		this.starCount=starNum;
		this.showStars(starNum, skipMessageUpdate);
	}

	function clearStars() {
		this.starTimer = setTimeout(this.objectName + ".resetStars()", 300);
	}

	function resetStars() {
		this.clearStarTimer();

		if (this.starCount)
			this.drawStars(this.starCount);
		else
			this.greyStars();
			this.setMessage(0);
	}

	function clearStarTimer() {
		if (this.starTimer) {
			clearTimeout(this.starTimer);
			this.starTimer = null;
		}
	}


	this.clearStars = clearStars;
	this.clearStarTimer = clearStarTimer;
	this.greyStars = greyStars;
	this.colorStars = colorStars;
	this.resetStars = resetStars;
	this.setStars = setStars;
	this.drawStars = drawStars;
	this.showStars = showStars;
	this.setMessage = setMessage;
}

// Additional helper for hover handling which is use whether or not the UTRating exists (ie. static widget) 
ratingHoverTimers = []
function ratingHoverOver(componentSuffix) {
	if (componentSuffix == "") { 
		componentSuffix == "reserved"
	}
	
	_clearHoverTimer(componentSuffix);
	hideDiv('defaultRatingMessage' + componentSuffix); 
	showDiv('hoverMessage' + componentSuffix);
}
function ratingHoverOut(componentSuffix) {
	if (componentSuffix == "") { 
		componentSuffix == "reserved"
	}
	ratingHoverTimers[componentSuffix] = setTimeout(function(){_ratingHoverClear(componentSuffix);}, 300);
}
function _ratingHoverClear(componentSuffix) {
	if (componentSuffix == "") { 
		componentSuffix == "reserved"
	}
	_clearHoverTimer();
	hideDiv('hoverMessage'); 
	showDiv('defaultRatingMessage');
}
function _clearHoverTimer(componentSuffix) {
	if (componentSuffix == "") { 
		componentSuffix == "reserved"
		}
	
	if (ratingHoverTimers[componentSuffix]) {
		clearTimeout(ratingHoverTimers[componentSuffix]);
		ratingHoverTimers[componentSuffix] = null;
	}
}
