/* comment functions */

var msg = new Object();
msg["missing_nickname"] = "Your nickname is empty!";
msg["exceeded"] = "Characters over the limit:";
msg["remaining"] = "Remaining characters:"; 
msg["empty"] = "Your comment is empty!";
msg["toolong"] = "Your comment is too long!";
msg["show_comments"] = "Show Comments";
msg["hide_comments"] = "Hide Comments";
	
function updateCharCountScript(charCount_id,label_id,textArea,maxChars,messages){
	if (textArea.value.length > maxChars){
		if ( document.getElementById(label_id).innerHTML != messages["exceeded"]){
			document.getElementById(label_id).innerHTML = messages["exceeded"];
		}
		document.getElementById(charCount_id).value = textArea.value.length - maxChars;
	} else {
		if (document.getElementById(label_id).innerHTML != messages["remaining"]){
			document.getElementById(label_id).innerHTML = messages["remaining"];
		}
		document.getElementById(charCount_id).value = maxChars - textArea.value.length;
	}
}

function updateCharCount(charCount_id, label_id, textArea) {
	updateCharCountScript(charCount_id, label_id, textArea, 255, msg)
}

function postComment(form_id,messages,feedback_id){
	var nickname = document.getElementById(form_id).nickname;
	var comment = document.getElementById(form_id).comment;
	var comment_button = document.getElementById(form_id).comment_button;
	if (comment.value.length == 0 || comment.value == null) {
		alert(messages["empty"]);
		comment.disabled=false;
		comment.focus();
		return false; 
	}
	if (comment.value.length > 255) {
		alert(messages["toolong"]);
		comment.disabled=false;
		comment.focus();
		return false;
	}
	if (nickname.value.length == 0 || nickname.value == null) {
		alert(messages["missing_nickname"]);
		nickname.focus();
		return false; 
	}
	// sendForm();
	
	document.getElementById(feedback_id).innerHTML = 'Sending data ... <img align="middle" src="images/layout/loading.gif" alt="Loading..." border="0" />';
	
	return true;
}

function commentFunctions(id, modus) {
	// alert( "commentFunctions(" + id + "," + modus + ")" );
	
	// bei comment_id spam++;
	var oOptions = {
		method: "GET",
		parameters: 'id=' + id + '&modus=' + modus,
		asynchronous: true,
		onFailure: function (oXHR) {
			$('feedback' + id).update(oXHR.statusText);
		},
		onLoading: function (oXHR) {
			$('feedback' + id).update('Sending data ... <img align="middle" src="images/layout/loading.gif" title="Loading..." alt="Loading..." border="0" />');
		},							
		onSuccess: function(oXHR) {
			$('feedback' + id).update(oXHR.responseText);
		}				
	};		
	var oRequest = new Ajax.Updater({success: oOptions.onSuccess.bindAsEventListener(oOptions)}, "comment/comment_functions.php", oOptions);
	
	// beitrag + spam!-button unsichtbar schalten
	document.getElementById("comment_body" + id).style.display = "none";
	document.getElementById("comment_functions_spam" + id).style.display = "none";
}

	
function toggleComments(element, url){
    new Effect.toggle(element,'Slide', {duration:1, afterFinish:function() { 
		window.location.href = url;
		var link_title = document.getElementById('comments_link_title');
		// alert("toggleComments: link_title -> " + link_title.innerHTML);
		if (link_title.innerHTML.indexOf(msg["show_comments"]) == -1) {
			link_title.innerHTML = msg["show_comments"];
			setClass("comment_links", "comment_links_off");
		} else {
			link_title.innerHTML = msg["hide_comments"];
			setClass("comment_links", "comment_links_on");
		}
	}  });
}
	
function sendForm() {
	// alert("sendForm");
	var oOptions = {
		method: "POST",
		parameters: Form.serialize("comment_form"),
		asynchronous: true,
		onFailure: function (oXHR) {
			$('feedback').update(oXHR.statusText);
		},
		onLoading: function (oXHR) {
			$('feedback').update('Sending data ... <img align="middle" src="images/layout/loading.gif" title="Loading..." alt="Loading..." border="0" />');
		},							
		onSuccess: function(oXHR) {
			$('feedback').update(oXHR.responseText);
		}				
	};		
	var oRequest = new Ajax.Updater({success: oOptions.onSuccess.bindAsEventListener(oOptions)}, "comment/comment_process.php", oOptions);	
}

var RecaptchaOptions = { theme:'white', tabindex:5 };

function getFeedback(feedback_iframe_id,feedback_id) {	
	var i = document.getElementById(feedback_iframe_id);
	if (i.contentDocument) {
		var d = i.contentDocument;
	} else if (i.contentWindow) {
		var d = i.contentWindow.document;
	} else {
		var d = window.frames[id].document;
	}
	if (d.location.href == "about:blank") {
		return;
	} else {
		document.getElementById(feedback_id).innerHTML = d.body.innerHTML;
		if (d.body.innerHTML.indexOf("Error") == -1) {
			BUEffect('comment_form_div');
		}
	}
}
