// JavaScript Document
$(document).ready(function(){

	var mail_com = false;
	var error_msg_com = {
			'author'	:	' * Preencha o campo Nome.',
			'email'		:	' * Preencha o campo E-mail.',
			'comment'	:	' * Preencha o campo Comentário.'
		};

	function validaMail(campo){
		//alert(campo.attr('id'));
		var email = campo.val();
		//alert(email);
		var reg = /^[\w!#$%&'*+\/=?^`{|}~-]+(\.[\w!#$%&'*+\/=?^`{|}~-]+)*@(([\w-]+\.)+[A-Za-z]{2,6}|\[\d{1,3}(\.\d{1,3}){3}\])$/;
		//alert(email);
		if (!( reg.test(email))){
			//alert('fail');
			mail = false;//alert(mail);
			$('#commentform #except').text(' * Formato de e-mail incorreto.');
			$('#commentform #ini_normal').hide();
			$('#commentform #except').show();
			
			return false;
		}else{
			$('#commentform #except').text('');
			$('#commentform #except').hide();
			$('#commentform #ini_normal').show();
			//alert('foi');
			
			mail = true;//alert(mail);
			return true;		
		}
		alertando = !(mail);
	}
	
	function valida(){
		validaMail($('#commentform :text[id="email"]'));
		var camp_vazio = $('#commentform .obrig').get().reverse();
		//alert(camp_vazio.length);
		var vazio = 0;
		var temp = "";
		var msg = "";
		for(var i=0; i < camp_vazio.length; i++){
			temp = camp_vazio[i].value;
			if(temp == "" || temp == null){
				msg = camp_vazio[i].name;
				vazio++;
			}
		}
		//alert(vazio);
		//alert(msg);

		if(vazio > 0 || !mail){ 
		//	if(!alertando){
				$('#commentform #except').text(error_msg_com[msg]);
				$('#commentform #ini_normal').hide();
				$('#commentform #except').show();
		//	}
			//alert(alertando);
			return false;
		}else{
			//$('form').submit(); 
			return true;
		}
	}

   $("#commentform").submit(function() {
   		//$('#btn_enviar').attr('disabled','disabled');
		var vai = valida();
		//alert(vai);
		if(vai){
			//$("#frm_contato").submit();
			return true;
		}else{
		//	$('#btn_enviar').removeAttr('disabled');
			return false;
		}
	//return false;
  });

});