function showProductCommentForm() {
	document.getElementById('productCommentForm').style.display='';
}

function hideProductCommentForm() {
	document.getElementById('productCommentForm').style.display='none';
}

function formatScore(score, allowFloat, addOnclick) {
	var scoreString='';
	for (var i=1; i<=score && i<=5; i++) {
		scoreString+='<img src="/images/star.png" alt=""';
		if (addOnclick) {
			scoreString+=' onclick="changeScore('+i+');"';
		}
		scoreString+=' />';
	}
	if (i-1<score && allowFloat) {
		scoreString+='<img src="/images/star_half.png" alt=""';
		if (addOnclick) {
			scoreString+=' onclick="changeScore('+i+');"';
		}
		scoreString+=' />';
		i++;
	}
	while (i<=5) {
		scoreString+='<img src="/images/star_empty.png" alt=""';
		if (addOnclick) {
			scoreString+=' onclick="changeScore('+i+');"';
		}
		scoreString+=' />';
		i++;
	}
	return scoreString;
}

function changeScore(score) {
	var scoreString=formatScore(score,false,true);
	document.getElementById('commentScore').innerHTML=scoreString;
	document.getElementById('commentScoreValue').value=score;
}

function removeCommentErrors() {
	document.getElementById('hozzaszolastema').style.backgroundColor='';
	document.getElementById('hozzaszolas').style.backgroundColor='';
}

function verifyCommentInputs() {
	var titleElement=document.getElementById('hozzaszolastema');
	var bodyElement=document.getElementById('hozzaszolas');
	var errorMessage='';
	if (titleElement.value=='') {
		titleElement.style.backgroundColor='#f99';
		errorMessage+='Kérem adja meg a hozzászólás témáját<br />';
	}
	if (bodyElement.value=='') {
		bodyElement.style.backgroundColor='#f99';
		errorMessage+='Kérem töltse ki a hozzászólást<br />';
	}
	if (errorMessage!='') {
		document.getElementById('commentError').innerHTML=errorMessage;
		setTimeout('removeCommentErrors();', 2000);
		return false;
	}
	return {
		title:titleElement.value,
		text:bodyElement.value,
		score:document.getElementById('commentScoreValue').value
	};
}

function showAllProductComments() {
	allProductCommentsShown=true;
	$('commentList').innerHTML='<p style="text-align: center; margin: 20px;"><img src="/images/ajaxupdating.gif" alt="" /></p>';
	$('showAllCommentsDiv').style.display='none';
	var params;
	if (encodeURI) {
		params='product='+encodeURI(window.location.pathname);
	} else {
		params='product='+escape(window.location.pathname);
	}
	sendProductCommentAjaxRequest(params);
	return false;
}

function sendProductCommentAjaxRequest(params) {
	if (allProductCommentsShown) {
		params+='&allComments=1';
	}
	new Ajax.Request(
		productCommentProcessor,
		{
			method:'post',
			parameters:params,
			onComplete:processCommentResponse,
			onError:processCommentResponse
		}
	)
}

function sendComment(doc_id) {
	sendingComment=true;
	var commentsDiv=document.getElementById('commentList');
	var commentData=verifyCommentInputs();
	if (commentData==false) {
		return false;
	}
	var params;
	if (encodeURI) {
		params='product='+encodeURI(window.location.pathname)+'&commentTitle='+encodeURI(commentData.title)+'&commentText='+encodeURI(commentData.text)+'&commentScore='+encodeURI(commentData.score);
	} else {
		params='product='+escape(window.location.pathname)+'&commentTitle='+escape(commentData.title)+'&commentText='+escape(commentData.text)+'&commentScore='+escape(commentData.score);
	}
	document.getElementById('productCommentForm').innerHTML='<p style="text-align: center; margin: 20px;"><img src="/images/ajaxupdating.gif" alt="" /></p>';
	sendProductCommentAjaxRequest(params);
	if (typeof(doc_id) != 'undefined') {
		var params = {
			eventType: "RATING",
			itemId: doc_id,
			rateValue: commentData.score
		}
		Gravity.addEvent(params);
		Gravity.commit();
	}
	return false;
}

function processCommentResponse(originalRequest) {
	
	function escapeTags(str) {
		str=str.replace(/</, '&lt;');
		return str.replace(/>/, '&gt;');
	}
	
	function responseError() {
		document.getElementById('commentError').innerHTML='Hiba a kommunikáció során. Kérem próbálja újra.';
	}
	
	function getCommentTemplate(xmlElement) {
		var currentElement=xmlElement.firstChild;
		var i;
		while(currentElement!=null) {
			for(i=0; i<currentElement.attributes.length; i++) {
				if (currentElement.attributes[i].name=='id') {
					if(currentElement.attributes[i].value=='comment') {
						return new Template(currentElement.firstChild.nodeValue);
					}
				}
			} 
			currentElement=currentElement.nextSibling;
		}
		return null;
	}
	
	function getCommentData(commentElement) {
		var commentData={
			firstName: '',
			lastName: '',
			userName: '',
			commentTitle: '',
			commentText: '',
			score: ''
		};
		for (var i=0; i<commentElement.attributes.length; i++) {
			switch (commentElement.attributes[i].name) {
				case 'firstName':
					commentData.firstName=escapeTags(commentElement.attributes[i].value);
					break;
				case 'lastName':
					commentData.lastName=escapeTags(commentElement.attributes[i].value);
					break;
				case 'userName':
					commentData.userName=escapeTags(commentElement.attributes[i].value);
					break;
				case 'commentTitle':
					commentData.commentTitle=escapeTags(commentElement.attributes[i].value)	;
					break;
				case 'commentScore':
					if (commentElement.attributes[i].value>0) {
						commentData.score=formatScore(commentElement.attributes[i].value);
					}
			}
		}
		commentData.commentText=escapeTags(commentElement.firstChild.nodeValue);
		return commentData;
	}
		
	var commentsDiv=$('commentList');
	if (originalRequest.responseXML==null) {
		responseError();
		return;
	}
	var xmlTagArray=originalRequest.responseXML.getElementsByTagName('xml')
	if (xmlTagArray.length==0 || xmlTagArray[0].firstChild==null) {
		responseError();
		return;
	}
	var xmlRoot=originalRequest.responseXML.getElementsByTagName('xml')[0];
	if (xmlRoot.getElementsByTagName('error')[0].firstChild.nodeValue=='0') {
		if (sendingComment) {
			document.getElementById('productCommentForm').innerHTML='';
			hideProductCommentForm();
		}
		var commentTemplate=getCommentTemplate(xmlRoot.getElementsByTagName('templates')[0]);
		if (commentTemplate==null) {
			responseError();
			return;
		}
		var commentHTML='';
		var tmpComment;
		var currentComment=xmlRoot.getElementsByTagName('comments')[0].firstChild;
		while (currentComment!=null) {
			if (currentComment.tagName=='comment') {
				commentHTML+=commentTemplate.evaluate(getCommentData(currentComment));
			}
			currentComment=currentComment.nextSibling;
		}
		commentsDiv.innerHTML=commentHTML;
		var score=formatScore(xmlRoot.getElementsByTagName('productscore')[0].firstChild.nodeValue,true);
		document.getElementById('productAverageScore').innerHTML=score;
		$('commentCounter').innerHTML=xmlRoot.getElementsByTagName('commentcount')[0].firstChild.nodeValue,true;
	}
}

preloadArray.push('/images/star.png');
preloadArray.push('/images/star_half.png');
preloadArray.push('/images/star_empty.png');
loginHandlers.push(showProductCommentForm);
logoutHandlers.push(hideProductCommentForm);

var allProductCommentsShown=false;
var sendingComment=false;

