/*
Author: mg12
Update: 2008/05/05
Author URI: http://www.neoease.com/
*/
function checkCommentsFrom(obj , commentId){

/*var author = document.getElementById("author");
var email = document.getElementById("email");
var comment = document.getElementById("comment");*/
var author = obj.author;
var email = obj.email;
var comment = obj.comment.value;

var error_msg = document.getElementById("error_comment_msg_"+commentId);
error_msg.style.display="none";
if(author.value == "" && email.value=="" && comment.value==""){
error_msg.style.display="block";

error_msg.innerHTML="<em>* Please type a name, email and a comment</em>";
return false;
}else if(author.value=="" && !checkEmail(email.value)){
error_msg.style.display="block";
error_msg.innerHTML="<em>* Please type a name and an email</em>";
return false;

}else if(author.value=="" && comment.value==""){
error_msg.style.display="block";
error_msg.innerHTML="<em>* Please type a name and a comment </em>";
return false;

}else if(comment.value=="" && !checkEmail(email.value)){
error_msg.style.display="block";
error_msg.innerHTML="<em>* Please type an email and a comment </em>";
return false;

}
else if(author.value ==""){
error_msg.style.display="block";

error_msg.innerHTML="<em>* Please type a name</em>";
return false;


}else if(!checkEmail(email.value)){
error_msg.style.display="block";

error_msg.innerHTML="<em>* Please type an email</em>";
return false;


}else if(comment.value ==""){
error_msg.style.display="block";

error_msg.innerHTML="<em>* Please type a comment</em>";
return false;
}

return true;
}
function checkEmail(email)
{
    var emailRegEx = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/;

    return email.match(emailRegEx);
}
(function() {

function reply(authorId, commentId, commentBox) {
	var author = MGJS.$(authorId).innerHTML;
	var insertStr = '<a href="#' + commentId + '">@' + author.replace(/\t|\n|\r\n/g, "") + ' </a> \n';

	appendReply(insertStr, commentBox);
}

function quote(authorId, commentId, commentBodyId, commentBox) {
	var author = MGJS.$(authorId).innerHTML;
	var comment = MGJS.$(commentBodyId).innerHTML;

	var insertStr = '<blockquote cite="#' + commentBodyId + '">';
	insertStr += '\n<strong><a href="#' + commentId + '">' + author.replace(/\t|\n|\r\n/g, "") + '</a> :</strong>';
	insertStr += comment.replace(/\t/g, "");
	insertStr += '</blockquote>\n';

	insertQuote(insertStr, commentBox);
}

function appendReply(insertStr, commentBox) {
	if(MGJS.$(commentBox) && MGJS.$(commentBox).type == 'textarea') {
		field = MGJS.$(commentBox);

	} else {
		alert("The comment box does not exist!");
		return false;
	}

	if (field.value.indexOf(insertStr) > -1) {
		alert("You've already appended this reply!");
		return false;
	}

	if (field.value.replace(/\s|\t|\n/g, "") == '') {
		field.value = insertStr;
	} else {
		field.value = field.value.replace(/[\n]*$/g, "") + '\n\n' + insertStr;
	}
	field.focus();
}
function insertQuote(insertStr, commentBox) {
	if(MGJS.$(commentBox) && MGJS.$(commentBox).type == 'textarea') {
		field = MGJS.$(commentBox);

	} else {
		alert("The comment box does not exist!");
		return false;
	}

	if(document.selection) {
		field.focus();
		sel = document.selection.createRange();
		sel.text = insertStr;
		field.focus();

	} else if (field.selectionStart || field.selectionStart == '0') {
		var startPos = field.selectionStart;
		var endPos = field.selectionEnd;
		var cursorPos = startPos;
		field.value = field.value.substring(0, startPos)
					+ insertStr
					+ field.value.substring(endPos, field.value.length);
		cursorPos += insertStr.length;
		field.focus();
		field.selectionStart = cursorPos;
		field.selectionEnd = cursorPos;

	} else {
		field.value += insertStr;
		field.focus();
	}
}

window['MGJS_CMT'] = {};
window['MGJS_CMT']['reply'] = reply;
window['MGJS_CMT']['quote'] = quote;

})();
