// JavaScript Document
Cufon.replace('h3',{fontFamily: 'Ebrima', hover:true });
$(document).ready(function(){
	$(".fone").setMask('phone');
	$(".cnpj").setMask('cnpj');
	
	function remove(str, sub) {
	   i = str.indexOf(sub);
	   r = "";
	   if (i == -1) return str;
	   r += str.substring(0,i) + remove(str.substring(i + sub.length), sub);
	   return r;
	 }

	function validaCNPJ(valor) {
	   var cnpj = valor;
	   cnpj = remove(cnpj, ".");
	   cnpj = remove(cnpj, "/");
	   cnpj = remove(cnpj, "-");
       var numeros, digitos, soma, i, resultado, pos, tamanho, digitos_iguais;
       digitos_iguais = 1;
       if (cnpj.length < 14 && cnpj.length < 15)
             return false;
       for (i = 0; i < cnpj.length - 1; i++)
             if (cnpj.charAt(i) != cnpj.charAt(i + 1))
                   {
                   digitos_iguais = 0;
                   break;
                   }
       if (!digitos_iguais)
            {
            tamanho = cnpj.length - 2
            numeros = cnpj.substring(0,tamanho);
            digitos = cnpj.substring(tamanho);
            soma = 0;
            pos = tamanho - 7;
            for (i = tamanho; i >= 1; i--)
                  {
                  soma += numeros.charAt(tamanho - i) * pos--;
                  if (pos < 2)
                        pos = 9;
                  }
            resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
            if (resultado != digitos.charAt(0))
                  return false;
            tamanho = tamanho + 1;
            numeros = cnpj.substring(0,tamanho);
            soma = 0;
            pos = tamanho - 7;
            for (i = tamanho; i >= 1; i--)
                  {
                  soma += numeros.charAt(tamanho - i) * pos--;
                  if (pos < 2)
                        pos = 9;
                  }
            resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
            if (resultado != digitos.charAt(1))
                  return false;
            return true;
            }
       else
            return false;

     }
	 
	$(".btn-novo-cartao").live('click',function(){
		$(".conteudo_cartao").fadeOut(
			function(){
				$(".formulario-cartao").fadeIn();
			}
		);
		
		$(".btn-enviar-cadastro").live('click',function(){
			var dados = $("#form_cadastro").serialize();
			var action = $("#form_cadastro").attr('action');
			var cnpjInvalido = 0;
			var emailInvalido = 0;
			var testaEmail = new RegExp(/^[A-Za-z0-9_\-\.]+@[A-Za-z0-9_\-\.]{2,}\.[A-Za-z0-9]{2,}(\.[A-Za-z0-9])?/);
			var erros = 0;
			$("#form_cadastro input, #form_cadastro textarea").each(function(){
				if (this.value==this.defaultValue || this.value=="")
					erros = 1;
				if ($(this).hasClass('cnpj')){
					if (!validaCNPJ($(this).val())){
						cnpjInvalido=1;
					}
				}
				if ($(this).attr('name')=='email'){
					if(!testaEmail.test($(this).val())){
						emailInvalido=1;
					}
				}
			})
			if (cnpjInvalido==1){
				alert("CNPJ inválido.");
				$('.cnpj').focus();
				$('.cnpj').val("");
				return false;
			}
			if (emailInvalido==1){
				alert("E-mail inválido.");
				$('.email').focus();
				$('.email').val("");
				return false;
			}
			if (erros==1) {
				alert ('Preencha todos os campos corretamente.');
				return false;
			} else {
				$.ajax({
					type: 'post',
					data: dados,
					url: action,
					success: function(retorno){
						$("#form_cadastro input, #form_cadastro textarea").each(
							function(){
								$(this).val("");
							}
						);	
						$(".mensagem-confirmacao").fadeIn();
					}
				})
			}				
		})
	})
})
