// JavaScript Document

function com_comment_submit(setID){
	var comment = document.getElementById("commentTextField_"+setID);
	var parentID = document.getElementById("commentParentField_"+setID).value;
	var name = document.getElementById("commentNameField_"+setID).value;
	var email = document.getElementById("commentEmailField_"+setID);
	var location = document.getElementById("commentLocation_"+setID).value;
	var recieveUpdates = document.getElementById("commentNotifyField_"+setID).checked == true ? '1' : '0';
	
	var error = 0;
	
	//check comments value
	if(comment.value == ''){
		if(comment.className.match('_error') == null){
			comment.className = comment.className+'_error';
		}
		comment.focus();
		error ++;
	}else{
		comment.className = comment.className.replace(/_error/g,'');
	}
	
	//check email if they want updates
	if(recieveUpdates == '1'){
		var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
		if(filter.test(email.value) == false){
			if(email.className.match('_error') == null){
				email.className = email.className+'_error';
			}
			email.focus();
			error ++;
		}else{
			email.className = email.className.replace(/_error/g,'');
		}
	}else{
		email.className = email.className.replace(/_error/g,'');
	}
	
	//set name to anonymous if there is nothin
	if(name == ''){
		name = "Anonymous";	
	}
	
	if(error > 0){
		return false;	
	}
	
	comment = comment.value.replace(/\n/g,"<br/>");
	
	var ajax = "components/com_comments/ajax.php?action=addComment&setID="+setID;
	ajax +=	   "&comment="+comment;
	ajax +=	   "&parentID="+parentID;
	ajax +=	   "&name="+name;
	ajax +=	   "&email="+email.value;
	ajax +=	   "&location="+location;
	ajax +=	   "&recieveUpdates="+recieveUpdates;
	
	executeAjax(ajax,"postComments_"+setID);
	document.getElementById("commentTextField_"+setID).value = "";
}



function com_comment_reply(commentID, name, setID){
	document.getElementById("commentParentField_"+setID).value = commentID;
	document.getElementById("commentType_"+setID).innerHTML = name == 'new' ? "Your Comment" : "Your Reply to "+name+" - <a href='javascript:;' onclick=\"com_comment_reply('0','new','"+setID+"')\">Cancel Reply</a>";
	document.getElementById("commentTextField_"+setID).focus();
}