//----------PERSONALIZA (necessita JQuery)
var colorBgAlert = "#f3f3f3";
var colorBgAlertError = "#EDCA4C";

var arMsg = {
		pt:{
			inicial: "* Campos necessários",
			erroVazios: "* Preencha os campos destacados",
			msgErroEmails: "@ campo de email inválido"
			},
		en:{
			inicial: "* fields required",
			erroVazios: "* os campos em destaque são necessários",
			msgErroEmails: "@ campo de email inválido"
			},
		es:{
			inicial: "* campos necessários",
			erroVazios: "* os campos em destaque são necessários",
			msgErroEmails: "@ campo de email inválido"
			}
		};
//----------

if (!language) language = "pt";

$(function() {
	//destaca campos obrigatórios
	$('.val_noEmpty').each(function(i) {
		$(this).css('font-weight', 'bold');
		$(this).text($(this).text() + ' *');
	});
	//mensagem inicial
	$('#formAlerts').text(arMsg[language].inicial).css("backgroundColor", colorBgAlert);
});

function valicadao() {
	var firstFieldError;
	//VALIDA CAMPOS QUE NÃO PODEM ESTAR VAZIO
	var foundEmpty = false;
	$('.val_noEmpty').each(function(i) {
		var fieldError = false;
		var f = $(this).attr("for");
		var type = $("input[@name='"+f+"']").attr("type");
		if (type == "radio") {
			//trata radio diferente
			fieldError = (($("input[@name='"+f+"'][checked]").length == 0));
		} else {
			//outros inputs
			var v = $.trim($("#"+f).val());
			fieldError = (v == "");
		}
		//destaca ERROR
		if (fieldError) {
			foundEmpty = true;
			$(this).addClass("error");
			$("#"+f).addClass("error");
			if (!firstFieldError) firstFieldError = $("#"+f);
		} else {
			$(this).removeClass("error");
			$("#"+f).removeClass("error");
		}
	});
	//VALIDA EMAIL
	var foundEmailError = false;
	var filterEmail = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	$('.val_email').each(function(i) {
		var fieldError = false;
		var f = $(this).attr("for");
		var v = $.trim($("#"+f).val());
		fieldError = (!filterEmail.test(v));
		//destaca ERROR
		if (fieldError) {
			foundEmpty = true;
			$(this).addClass("error");
			$("#"+f).addClass("error");
			if (!firstFieldError) firstFieldError = $("#"+f);
		} else {
			$(this).removeClass("error");
			$("#"+f).removeClass("error");
		}
	});
	//mensagem de erro
	var msg = "";
	if (foundEmpty) msg += arMsg[language].erroVazios+"<br />";
	if (foundEmailError) msg += arMsg[language].msgErroEmails+"<br />";
	if (foundEmpty || foundEmailError)
		$('#formAlerts').html(msg)
						.css("backgroundColor", colorBgAlertError)
						.animate({backgroundColor:colorBgAlert}, "slow");
	if (firstFieldError) firstFieldError.focus();
	return (!foundEmpty && !foundEmailError);
}

/* Scripts para validação de formularios de CONTATO, PRE-RESERVA, INDICAÇÃO */

	function Tecla(e){
		var tecla;
		if(window.event) {
			// for IE, e.keyCode or window.event.keyCode can be used
			tecla = e.keyCode; 
		}
		else if(e.which) {
			// netscape or firefox
			tecla = e.which; 
		}
		else {
			// no event, so pass through
			return true;
		}
	
		if(tecla > 47 && tecla < 58) // numeros de 0 a 9
			return true;
		else
		{
			if (tecla != 8) // backspace
				return false;
			else
				return true;
		}
	
	}

	function Formatar_Telefone(objeto,tammax,teclapres)
	{
		var tecla;// = teclapres.keyCode;
		if(window.event) {
			// for IE, e.keyCode or window.event.keyCode can be used
			tecla = teclapres.keyCode; //e.keyCode; 
		}
		else if(e.which) {
			// netscape or firefox
			tecla = teclapres.which;//e.which; 
		}

		vr = objeto.value;
		vr = vr.replace( "(", "" );
		vr = vr.replace( ")", "" );
		vr = vr.replace( " ", "" );
		vr = vr.replace( "-", "" );
		tam = vr.length;
		if (tam < tammax && tecla != 8) {
			tam = vr.length + 1 ;
		}
		if (tecla == 8 ) {
			tam = tam - 1 ;
		}
		if ( tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 ) {
			if ( tam <= 4 ) { 
				objeto.value = vr ;
			}
			if ( (tam > 4) && (tam <= 8) ) {
				objeto.value = vr.substr(0,tam-4) + '-' + vr.substr( tam - 4, tam ) ;
			}
			if ( (tam >= 9) && (tam <= 10) ) {
				objeto.value = '(' + vr.substr(0,2) + ') ' + vr.substr(2,tam-6) + '-' + vr.substr(tam-4,tam) ;			
			}
		}		
	}

	function validaEmail(email)
		{
			if(email.length < 6) {
				return false;
			}
			var x = 0;
			for (var c=0;c<email.length;c++) {
				if (email.substring(c,c+1) == '@') {
					x = c;
				}
			}
			var y = 0;
			if (x > 0) {
				for (c=x;c<email.length;c++) {
					if (email.substring(c,c+1)=='.') {
						y = c;
						var valida = 1;
					}
				}
				if (y > 0) {
					var dominio = '';
					for (c=x;c<y;c++) {
						dominio = dominio + email.substring(1,c);
					}
				}
			}
			else {
				return false;
			}					
			if (y <= x+2){
				return false;
			}				
			if (valida == 1){
				return true;
			}
		}
	
	
	
		
	
		function Valida_Contato(){
			if(document.getElementById('fcn_nome').value == ''){
				alert('Informe o Nome!');
				document.getElementById('fcn_nome').focus();
				return false;
			}
			if(document.getElementById("fcn_email").value == ""){
				alert("Informe o seu E-mail!");
				document.getElementById("fcn_email").focus();
				return false;
			}
			else{
				if (!validaEmail(document.getElementById('fcn_email').value)) {
					alert("Informe um E-mail válido!");
					document.getElementById('fcn_email').focus();
					return false;
				}
			}
			if(document.getElementById('fcn_cidade').value == ''){
				alert('Informe a Cidade!');
				document.getElementById('fcn_cidade').focus();
				return false;
			}
			if(document.getElementById('fcn_uf').value == ''){
				alert('Informe o Estado!');
				document.getElementById('fcn_uf').focus();
				return false;
			}
			if(document.getElementById('fcn_fone').value == ''){
				alert('Informe seu Telefone!');
				document.getElementById('fcn_fone').focus();
				return false;
			}
			if(document.getElementById('fcn_msg').value == ''){
				alert('Informe a sua Mensagem!');
				document.getElementById('fcn_msg').focus();
				return false;
			}
			return true;
	}