﻿function ValidaForm(form_id,errorMessage) {
	var retorno = true;
	$("#" + form_id + " input").each(
		function() {	
			if	( 
					($(this).attr("valida") == "1" && $.trim($(this).attr("value")) == "") ||
					($(this).attr("valida") == "1" && $(this).attr('name').toLowerCase().indexOf('email') > -1 && !checkMail($(this).attr("value")))
				) {
				
				retorno = false;

				if ($(this).attr('class').indexOf('error') == -1) {
					var className = $(this).attr("class");
					$(this).focus(
						function() {
							$(this).removeClass(className + "-error");
						}
					);
					$(this).addClass(className + "-error");
				}
			} 
		}
	);
	$("#" + form_id + " textarea").each(
		function() {	
			if	( 
					($(this).attr("valida") == "1" && $.trim($(this).attr("value")) == "")
				) {
				
				retorno = false;

				if ($(this).attr('class').indexOf('error') == -1) {
					var className = $(this).attr("class");
					$(this).focus(
						function() {
							$(this).removeClass(className + "-error");
						}
					);
					$(this).addClass(className + "-error");
				}
			} 
		}
	);
	if (!retorno)
	{
		alert(errorMessage);
	}

	return retorno;
}

function checkMail(mail){
        var er = new RegExp(/^[A-Za-z0-9_\-\.]+@[A-Za-z0-9_\-\.]{2,}\.[A-Za-z0-9]{2,}(\.[A-Za-z0-9])?/);
        if(typeof(mail) == "string"){
                if(er.test(mail)){ return true; }
        }else if(typeof(mail) == "object"){
                if(er.test(mail.value)){ 
                                        return true; 
                                }
        }else{
                return false;
                }
}
