// Valida Email, param = id do email e retorna boolean
$(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', 'padding-left':'6px', 'padding-top':'6px', 'color':'#999999'} );
	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.3'); } );
	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);

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 mEnviaFaleConosco() {
	if (mValidaForm()) {
		if (mValidaEmail("email")) {
			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;
}
/*
* Função para enviar orçamento
*/
function mEnviaTrabalheConosco() {
	if (mValidaTrabalheConosco()){
		if (mValidaForm()) {
			if (mValidaEmail("email")) {
				document.getElementById("formTrabalheConosco").submit();
			}
		}
	}
	return false;
}

function mValidaTrabalheConosco(){
	// var cpf = document.cadastro.cpf.value;
	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.");
	 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.");
		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.");
		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.");
		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;
}

// Máscara de Moeda, onkeypress="return(mMascaraMoeda(this, '.', ',', event));"
function mMascaraMoeda(objTextBox, SeparadorMilesimo, SeparadorDecimal, e){
	var sep = 0;
	var key = "";
	var i = j = 0;
	var len = len2 = 0;
	var strCheck = "0123456789";
	var aux = aux2 = "";
	var whichCode = (window.Event) ? e.which : e.keyCode;

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

	key = String.fromCharCode(whichCode);
	if (strCheck.indexOf(key) == -1) return false;
	len = objTextBox.value.length;

	for(i = 0; i < len; i++)
		if ((objTextBox.value.charAt(i) != "0") && (objTextBox.value.charAt(i) != SeparadorDecimal)) break;
	aux = "";
	for(; i < len; i++)
		if (strCheck.indexOf(objTextBox.value.charAt(i))!=-1) aux += objTextBox.value.charAt(i);
	aux += key;
	len = aux.length;
	if (len == 0) objTextBox.value = "";
	if (len == 1) objTextBox.value = "0"+ SeparadorDecimal + "0" + aux;
	if (len == 2) objTextBox.value = "0"+ SeparadorDecimal + aux;

	if (len > 2) {
		aux2 = "";
		for (j = 0, i = len - 3; i >= 0; i--) {
			if (j == 3) {
				aux2 += SeparadorMilesimo;
				j = 0;
			}
			aux2 += aux.charAt(i);
			j++;
		}
		objTextBox.value = "";
		len2 = aux2.length;

		for (i = len2 - 1; i >= 0; i--)
		objTextBox.value += aux2.charAt(i);
		objTextBox.value += SeparadorDecimal + aux.substr(len - 2, len);
	}
	return false;
}

// 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;
}

