﻿$(document).ready(function(){

function verifica(campo){
  var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
  emailx=campo.replace(';',',')
  for(x=0;x<emailx.split(",").length;x++){	
	if (!emailReg.test(emailx.split(',')[x])){
		ok=false;
		//alert(emailx.split(',')[x])
	}	else {ok=true}
  }
}

$('#submit').click(function () {
	$(".input-error").hide();
	$('#form-ok').hide();
	$('#form-erro').hide();
	var hasError = false;
	var namec = $("#namei").val();
	var emailc = $("#emaili").val();
	var fonec = $("#emaildesi").val();
	var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
	
	if(namec == '') {
	$("#namei").addClass('hightlight');
	$('#namei').attr('title','O seu nome é obrigatório');
	hasError = true;
	} else {$("#namei").removeClass('hightlight');
	$('#namei').removeAttr('title');}
	
	
	
if(fonec==''){
	$("#emaildesi").addClass('hightlight');
	$('#emaildesi').attr('title','Coloque corretamente o(s) e-mail(s)');
	hasError = true;
	
} else {
	verifica(fonec);
	if(ok==true){
	$("#emaildesi").removeClass('hightlight');
	$('#namei').removeAttr('title');
	} else {
		$("#emaildesi").addClass('hightlight');
  		$('#emaildesi').attr('title','Coloque corretamente o(s) e-mail(s)');
		hasError = true;
		}

}
	
		
	if(!emailReg.test(emailc) || emailc == '') {
	$("#emaili").addClass('hightlight');
	$('#emaili').attr('title','O endereço digitado é inválido');
	hasError = true;
	} else {
		$("#emaili").removeClass('hightlight');
		$('#emaili').removeAttr('title');
		}
						   
	if(hasError == false) {
	var data = $('#formi').serialize();
	$('.text,#submit').attr('disabled','true');	
	$('.loading').show();	
	$.ajax({
		url: "process.asp",			
		type: "POST",
		data: data,		
		cache: false,		
		success: function (html) {				
			if (html=="Mensagem Enviada") {					
				$('#box-form').slideUp('slow');
				
				setTimeout(function(){$('#form-ok').fadeIn('slow');},1000);
				$('#form-erro').hide();
			} else {
			$('.text,#submit').removeAttr("disabled")
			$('.loading').hide();
			$('#box-form').slideUp('slow');
			setTimeout(function(){$('#form-erro').fadeIn('slow');},1000);
			}
		}		
	});}	
	return false;
});

$('#form-again').click(function () {
	$('#form-erro').slideUp('slow');
	setTimeout(function(){$('#box-form').fadeIn('slow');},1000);
	return false;
});
//------
});
