/**
 * @author wiltondeoliveiragarcia
 */
//commentform
API.attachDocumentReadyListener(function()
{
	var el = API.getEBI('commentform');

	if(el)
	{
		el.onsubmit = function(e){
			if (!isEmpty(this) || !isValid(this)) 
			{
				API.cancelDefault(e);
			}
		};
		
		isValid = (function(el){
			var email = el.elements.email;
			var emailRegExp = new RegExp("^[^@]+@[\\w\\d.-]+\\.[\\w\\d]{2,6}$");
			if(!emailRegExp.test(email.value))
			{
				global.alert("Preencha o campo ''Email'' com um email válido!");
				email.thisClassName = "error";
				email.focus();
				return false;
			}
			else
			{
				return true;
			}
		});
		
		trim = (function(str){return str.replace(/^\s+|\s+$/g,"");});
		
		isEmpty = (function(el){
			var author = el.elements.author;
			var email = el.elements.email;
			var comment = el.elements.comment;
			if(trim(author.value)=="")
			{
				global.alert("Preencha o campo ''Nome''");
				author.thisClassName = "error";
				author.focus();
			} 
			else if(trim(email.value)=="")
			{
				global.alert("Preencha o campo ''Email''");
				email.thisClassName = "error";
				email.focus();
			}
			else if(trim(comment.value)=="")
			{
				global.alert("Preencha o campo ''Comentário''");
				comment.thisClassName = "error";
				comment.focus();
			}
			return (trim(author.value)!=""&&trim(email.value)!=""&&trim(comment.value)!="");
		});	
	}

});
