
var commentSubmitSuccess = function(transport, json) {
	alert(transport.responseText);
	if (substring(transport.responseText, 0, 1) == '#') {
		resetCommentForm();
	}
};

var commentSubmitFailure = function() {
	alert('Sikertelen beküldés');
}

function resetCommentForm() {
	document.commentform.name.value = '';
	document.commentform.email.value = '';
	document.commentform.captcha.value = '';
	document.commentform.opinion.value = '';
}

function submitComment(form) {
	try {
		var xmlhttp;
		if (window.XMLHttpRequest) {
			xmlhttp = new XMLHttpRequest();
		}
		else if (window.ActiveXObject) {
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		
		xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState == 4) {
				alert(xmlhttp.responseText);
				if (substring(xmlhttp.responseText, 0, 1) == '#') {
					resetCommentForm();
				}
			}
		}
		
		var postParams = '';
		postParams += 'product_id=' + encodeURI(form.product_id.value);
		postParams += '&name=' + encodeURI(form.name.value);
		postParams += '&email=' + encodeURI(form.email.value);
		postParams += '&captcha=' + encodeURI(form.captcha.value);
		postParams += '&opinion=' + encodeURI(form.opinion.value);
		
		xmlhttp.open('POST', '/alcor_comment_submit.php', true);
		xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		xmlhttp.setRequestHeader("Content-Length", postParams.length);
    xmlhttp.setRequestHeader("Connection", "close");
		xmlhttp.send(postParams);
		
		/*
		new Ajax.Request('/alcor_comment_submit.php', {
			method: 'post',
			parameters: $('commentform').serialize(true),
			onSuccess: 'commentSubmitSuccess',
		});
		*/
	}
	catch (ex) {
		alert('Hiba történt: ' + ex);
	}
	return false;
}


function addEvent(obj, evType, fn) {  
  if (obj.addEventListener) {  
    obj.addEventListener(evType, fn, true);  
    return true;  
  } else if (obj.attachEvent) {  
    var r = obj.attachEvent("on"+evType, fn);  
    return r;  
  } else {  
    return false;  
  }  
}

function refreshCaptchaImage() {
	var date = new Date();
	document.product_comment_captcha.src = '/alcor_product_comment_captcha.php?x=' + date.getTime();
}

function registerEventHandlers() {
	var element;
	element = document.getElementById('product_comment_captcha');
	addEvent(element, 'click', refreshCaptchaImage);
}


addEvent(window, 'load', registerEventHandlers); 

