$(document).ready(function(){
	$( '#formFaleConoscoCadastro' ).parseFormNoLabels();
});


(function ($) {
// PARSE FORM NO LABELS
$.fn.parseFormNoLabels = function() {
	return this.each(function(i){
	var f = $(this);
	f.addClass( 'parsed' );
	f.find( 'label' ).css( {'position':'absolute', 'cursor':'text'});
	f.find( 'input[type=text], select' ).each( function(){ this.label=$('label[for='+this.id+']'); } );
	f.find( 'input[type=text], select' ).focus( function() { $(this.label).css('opacity','0.5'); } );
	f.find( 'input[type=text], select' ).blur( function() { $(this.label).css('opacity','1'); } );
	f.find( 'input[type=text], select' ).bind( 'keyup change', function() {
		if( $(this).val() ) $(this.label).css('display','none');
		else $(this.label).css('display','inline');
	} );
	});
};
})(jQuery);

// Valida Email, param = id do email e retorna boolean
function mValidaEmail(id){
	var email = document.getElementById(id).value;
	var erro = 0;

	if (typeof(email) != "string") erro++;
    else if (!email.match(/^[A-Za-z0-9]+([_.-][A-Za-z0-9]+)*@[A-Za-z0-9]+([_.-][A-Za-z0-9]+)*\.[A-Za-z0-9]{2,4}$/)) erro++;

	if(erro != 0){
		if (document.getElementById(id).value != "") {
			alert ("E-mail tem que ser válido!");
			document.getElementById(id).focus();
		}
		return false;
	}else return true;
}

/*
* Função para enviar fale conosco
*/
function mEnviaProcuracao() {

	if (mValidaForm()) {
		if($('#cnpj').is('input')){
			if (mValidaCNPJ()) {
				if(mValidaCPF()){
				//	window.open('documentacao/_impressao_juridica');
					return true;
				}
			}
		}else{
			if(mValidaCPF()){
			//	window.open('documentacao/_impressao_fisica');
				return true;
			}
		}
	}
	return false;
}
/*
* Função para enviar fale conosco
*/
function mEnviaFaleConosco() {
	if (mValidaForm()) {
		if (mValidaEmail("email")) {
			ajaxForm("ajaxFaleConosco","formFaleConosco");
		}
	}
	return false;
}
/*
* Função para enviar fale conosco que não possui e-mail
*/
function mEnviaFaleConoscoSemEmail() {
	if (mValidaForm()) {
		ajaxForm("ajaxFaleConosco","formFaleConosco");
	}
	return false;
}
/*
* Função para enviar fale conosco com alert
*/
function mEnviaFaleConoscoCadastro() {
	if (mValidaFormCadastro()) {
		if (mValidaEmail("email_cadastro")) {
			//ajaxForm("ajaxFaleConosco","formFaleConosco");
			alert('Cadastro efetuado com sucesso!');
		}
	}else{
		return false;
	}
	//return false;
}
//envento onkeyup
function mValidaCPF() {
	var cpf = document.getElementById('cpf').value;

	var filtro = /^\d{3}.\d{3}.\d{3}-\d{2}$/i;
	if(!filtro.test(cpf)){
	 window.alert("CPF incorreto.");
	 eval("document.getElementById('cpf').focus");
	 eval("document.getElementById('cpf').value = ''");
	 return false;
	}

	cpf = remove(cpf, ".");
	cpf = remove(cpf, "-");

	if(cpf.length != 11 || cpf == "00000000000" || cpf == "11111111111" ||
		cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" ||
		cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" ||
		cpf == "88888888888" || cpf == "99999999999"){
		window.alert("CPF incorreto.");
		eval("document.getElementById('cpf').focus");
		eval("document.getElementById('cpf').value = ''");
		return false;
	}

	soma = 0;
	for(i = 0; i < 9; i++)
		soma += parseInt(cpf.charAt(i)) * (10 - i);
	resto = 11 - (soma % 11);
	if(resto == 10 || resto == 11)
		resto = 0;
	if(resto != parseInt(cpf.charAt(9))){
		window.alert("CPF incorreto.");
		eval("document.getElementById('cpf').focus");
		eval("document.getElementById('cpf').value = ''");
		return false;
	}
	soma = 0;
	for(i = 0; i < 10; i ++)
		soma += parseInt(cpf.charAt(i)) * (11 - i);
	resto = 11 - (soma % 11);
	if(resto == 10 || resto == 11)
		resto = 0;
	if(resto != parseInt(cpf.charAt(10))){
		window.alert("CPF incorreto.");
		eval("document.getElementById('cpf').focus");
		eval("document.getElementById('cpf').value = ''");
		return false;
	}
	return true;


}

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 mMascaraCPF(cpf, e) {
    var whichCode = (window.Event) ? e.which : e.keyCode;

    if (whichCode == 13 || whichCode == 8 || whichCode == 0) return true;

	if (cpf.value.length == 3) { cpf.value = cpf.value + '.'; }
	if (cpf.value.length == 7) { cpf.value = cpf.value + '.'; }
	if (cpf.value.length == 11) { cpf.value = cpf.value + '-'; }
}

function campo_numerico (){

		if (window.Event.keyCode < 45 || window.Event.keyCode > 57) window.Event.returnValue = false;

}

function mascara_cnpj (campo,documento,f){
	var mydata = '';
	mydata = mydata + documento;

	if (mydata.length == 2){
		mydata   = mydata + '.';

		ct_campo = eval("document.getElementById('"+campo+"').value = mydata");
		ct_campo;
	}

	if (mydata.length == 6){
				mydata   = mydata + '.';

				ct_campo = eval("document.getElementById('"+campo+"').value = mydata");
				ct_campo;
	}

	if (mydata.length == 10){
		mydata 	  = mydata + '/';

		ct_campo1 = eval("document.getElementById('"+campo+"').value = mydata");
		ct_campo1;
	}

	if (mydata.length == 15){
				mydata 	  = mydata + '-';

				ct_campo1 = eval("document.getElementById('"+campo+"').value = mydata");
				ct_campo1;
	}
}

function mValidaCNPJ(f,campo){

		pri = eval("document.getElementById('cnpj').value.substring(0,2)");
		seg = eval("document.getElementById('cnpj').value.substring(3,6)");
		ter = eval("document.getElementById('cnpj').value.substring(7,10)");
		qua = eval("document.getElementById('cnpj').value.substring(11,15)");
		qui = eval("document.getElementById('cnpj').value.substring(16,18)");

		var i;
		var numero;
		var situacao = '';

		numero = (pri+seg+ter+qua+qui);

		s = numero;

		c = s.substr(0,12);
		var dv = s.substr(12,2);
		var d1 = 0;

		for (i = 0; i < 12; i++){
			d1 += c.charAt(11-i)*(2+(i % 8));
		}

		if (d1 == 0){
			var result = "falso";
		}
			d1 = 11 - (d1 % 11);

		if (d1 > 9) d1 = 0;

			if (dv.charAt(0) != d1){
				var result = "falso";
			}

		d1 *= 2;
		for (i = 0; i < 12; i++){
			d1 += c.charAt(11-i)*(2+((i+1) % 8));
		}

		d1 = 11 - (d1 % 11);
		if (d1 > 9) d1 = 0;

			if (dv.charAt(1) != d1){
				var result = "falso";
			}


		if (result == "falso") {
			alert("CNPJ inválido!");
			aux1 = eval("document.getElementById('cnpj').focus");
			aux2 = eval("document.getElementById('cnpj').value = ''");

		}else{
			return true;
		}
}


// Máscara de telefone, onkeypress="return mMascaraTelefone(this, event); FORMATO 1234-5678"
function mMascaraTelefone(Campo, e) {
	var key = "";
    var len = 0;
    var strCheck = "0123456789";
    var aux = "";
    var whichCode = (window.Event) ? e.which : e.keyCode;

    if (whichCode == 13 || whichCode == 8 || whichCode == 0) return true;
    key = String.fromCharCode(whichCode);
    if (strCheck.indexOf(key) == -1) return false;

    aux =  Telefone_Remove_Format(Campo.value);

    len = aux.length;
    if(len >= 8) return false;
    aux += key;

    Campo.value = Telefone_Mont_Format(aux);
    return false;
}
// Utilizada pela mascara telefone
function Telefone_Mont_Format(Telefone){
    var aux = len = "";
    len = Telefone.length;

    if(len <= 9) tmp = 4;
    else tmp = 6;

    aux = "";
    for(i = 0; i < len; i++){
     //   if(i == 0)aux = "(";
        aux += Telefone.charAt(i);
     //   if(i+1 == 2) aux += ") ";

        if(i+1==tmp) aux += "-";
    }
    return aux ;
}
// Utilizado pela mascara telefone
function Telefone_Remove_Format(Telefone){
    var strCheck = "0123456789";
    var len = i = aux = "";
    len = Telefone.length;
    for(i = 0; i < len; i++){
        if (strCheck.indexOf(Telefone.charAt(i))!= -1) aux += Telefone.charAt(i);
    }
    return aux;
}

