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 getUrlSync(url) {
	return getUrl(url, false, null);
}

function getUrlAsync(url, handleStateChange) {
	return getUrl(url, true, handleStateChange);
}

// call a url
function getUrl(url, async, handleStateChange) {
	var xmlHttpReq = getXmlHttpRequest();

	if (!xmlHttpReq) return;

	if (handleStateChange) {
		xmlHttpReq.onreadystatechange = function() {
			handleStateChange(xmlHttpReq);
		};
	}
	else {
		xmlHttpReq.onreadystatechange = function() {;}
	}

	xmlHttpReq.open("GET", url, async);
	xmlHttpReq.send(null);
}

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 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 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 replaceDivContents(xmlHttpRequest, dstDivId, actRating, elementId) {
	var dstDiv = document.getElementById(dstDivId);
	dstDiv.innerHTML = xmlHttpRequest.responseText;
	eval(actRating);
	document.getElementById(elementId).innerHTML = '<font color="#ff0000">Köszönjük!</font>';
}


function getUrlXMLResponseCallback(xmlHttpReq) {
	if(xmlHttpReq.responseXML == null) {
		alert("Error while processing your request.");
		return;
	}
	var root_node = getRootNode(xmlHttpReq);
	var return_code = getNodeValue(root_node, 'return_code');
	//alert("return code " + return_code);

	if(return_code == 0) {
		redirect_val = getNodeValue(root_node, 'redirect_on_success');
		if(redirect_val != null) {
			window.location=redirect_val;
		} 
		else {
			success_message = getNodeValue(root_node, 'success_message');
			if (success_message != null) {
				alert(success_message);
			}
			if(this.successCallback != null) {
				this.successCallback(xmlHttpReq);
			}
		}
	} 
	else {
		var error_msg = getNodeValue(root_node, 'error_message');
		if (error_msg == null || error_msg.length == 0) {
			error_msg = "An error occured while performing this operation.";
		}
		alert(error_msg)
	}
}

function getUrlXMLResponseCallbackFillDiv(xmlHttpReq) {
	getUrlXMLResponseCallback(xmlHttpReq);
	document.getElementById(this.div_id).innerHTML=getNodeValue(xmlHttpReq.responseXML, "html_content");
}

function getNodeValue(obj,tag) {
	node=obj.getElementsByTagName(tag);
	if(node!=null && node.length>0) {
		return node[0].firstChild.nodeValue;
	} 
	else {
		return null;
	}
}

function getRootNode(xmlHttpReq) {
	return xmlHttpReq.responseXML.getElementsByTagName('root')[0];
}

function getUrlXMLResponse(url, successCallback) {
	this.successCallback = successCallback;
	this.urlResponseCallback = getUrlXMLResponseCallback;
	getUrl(url, true, execOnSuccess(this.urlResponseCallback)) 
}

function getUrlXMLResponseAndFillDiv(url, div_id, successCallback) {
	this.successCallback = successCallback;
	this.urlResponseCallback = getUrlXMLResponseCallbackFillDiv;
	this.div_id = div_id;
	getUrl(url, true, execOnSuccess(this.urlResponseCallback)) 
}

function postUrlXMLResponse(url, data, successCallback) {
	this.successCallback = successCallback;
	this.urlResponseCallback = getUrlXMLResponseCallback;
	postUrl(url, data, true, execOnSuccess(this.urlResponseCallback))
}
// ANGUS - This appears to be unused...
function confirmAndPostUrlXMLResponse(url, confirmMessage, data, successCallback) {
	if (confirm(confirmMessage)) {
		postUrlXMLResponse(url, data, successCallback);
	}
}

function postFormXMLResponse(formName, successCallback) {
	this.successCallback = successCallback;
	postForm(formName, true, execOnSuccess(getUrlXMLResponseCallback))
}

if ((navigator.appVersion.indexOf('MSIE') > -1) && (typeof window.XMLHttpRequest == 'undefined')) imgExt = '.gif';
else imgExt = '.png';

var T_RATING_IMG = 'images/icn_star_full_19x20'+imgExt;
var T_RATING_IMG_HOVER = 'images/star_hover.gif';
var T_RATING_IMG_HALF = 'images/icn_star_half_19x20'+imgExt;
var T_RATING_IMG_BG = 'images/icn_star_empty_19x20'+imgExt;
var T_RATING_IMG_REMOVED = 'images/star_removed.gif';

function TRating(ratingElementId, maxStars, objectName, formName, ratingMessageId, componentSuffix, size, messages, hoverMessage) {
	this.ratingElementId = ratingElementId;
	this.maxStars = maxStars;
	this.objectName = objectName;
	this.formName = formName;
	this.ratingMessageId = ratingMessageId;
	this.componentSuffix = componentSuffix;
	this.messages = messages;
	this.hoverMessage = hoverMessage;

	this.starTimer = null;
	this.starCount = 0;

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

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

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

	function setMessage(starNum) {
		document.getElementById(this.ratingMessageId).innerHTML = this.messages[starNum];
	}

	function colorStars(starNum) {
		for (var i=0; i < starNum; i++) {
			document.getElementById('star_'  + this.componentSuffix + "_" + (i+1)).src = T_RATING_IMG;
		}
	}

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

	function setStars(starNum) {
		this.starCount = starNum;
		this.drawStars(starNum);
		document.forms[this.formName]['rating'].value = this.starCount;
		var ratingElementId = this.ratingElementId;
		var str = this.objectName+".showStars("+this.starCount+");";
		var divRatingMessageId = this.ratingMessageId;
		postForm(this.formName, true, function (req) { replaceDivContents(req, ratingElementId, str, divRatingMessageId); });
	}


	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();
		
		if (!this.hoverMessage) this.setMessage(0);
	}

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

	function showHoverMessage() {
		if (document.getElementById(this.ratingMessageId).style.visibility != "visible") {
			document.getElementById(this.ratingMessageId).style.visibility = "visible";
		}
	}

	function hideHoverMessage() {
		document.getElementById(this.ratingMessageId).style.visibility = "hidden";
	}

	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;
	this.showHoverMessage = showHoverMessage;
	this.hideHoverMessage = hideHoverMessage;
}
