Event.observe(window, 'load', init, false);

function init(){
}

function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp('(^|\\s)'+searchClass+'(\\s|$)');
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

function textCounter(field, countfield, maxlimit) {
	if (field.value.length > maxlimit) {
		field.value = field.value.substring(0, maxlimit);
	}
	else {
		countfield.value = maxlimit - field.value.length;
	}
	return true;
}

/**
 *	Add comment if user is already logged in
 */
function addComment( entry, player_id, comment ) {
	var url = '/addcomment.php';
	var pars = 'entry=' + entry + '&player_id=' + player_id + '&comment=' + comment.value;
	var myAjax = new Ajax.Request(url, {method: 'post', postBody: pars, onComplete: ajax_response_addcomment});
}

function ajax_response_addcomment(originalRequest) {
	if ( originalRequest.responseText ) {
		Effect.Fade('addComment', {queue: 'front'});
		$('newCommentText').innerHTML = originalRequest.responseText;
		Effect.Appear('newComment', {queue: 'end'});
		$('comment').value = '';
	}
	else {
		$('submitError').innerHTML = "Error: Unable to add comment";
	}
}

/**
 *	Add comment if user is not logged in
 */
function addComment2( entry, name, comment, recaptcha_challenge_field, recaptcha_response_field ) {

	if ( name.value == "" ) {
		alert('You must enter a name');
		return false;
	}
	else if ( recaptcha_response_field.value == "" ) {
		alert('You must enter the captcha text');
		return false;
	}

	var url = '/addcomment2.php';
	var pars = 'entry=' + entry + '&name=' + name.value + '&comment=' + comment.value + '&recaptcha_challenge_field=' + recaptcha_challenge_field.value + '&recaptcha_response_field=' + recaptcha_response_field.value;
	var myAjax = new Ajax.Request(url, {method: 'post', postBody: pars, onComplete: ajax_response_addcomment2});
}

function ajax_response_addcomment2(originalRequest) {
	if ( originalRequest.responseText ) {
		Effect.Fade('addComment', {queue: 'front'});
		$('newCommentText').innerHTML = originalRequest.responseText;
		Effect.Appear('newComment', {queue: 'end'});
		$('comment').value = '';
	}
	else {
		$('submitError').innerHTML = "Error: Unable to add comment, try entering the captcha text again.";
		new Effect.Highlight('submitError');
	}
}
