/**
 * @author Fabiano Chiaretto Fenandes
 * @contact chiaretto@gmail.com
 */

function clear_field(field){
    if (field.value == field.defaultValue) {
        field.value = ''
    }
}

function check_field(field){
    if (field.value == '' ||
    field.value == ' ') {
        field.value = field.defaultValue
    }
}

function cadastrarNews(varNome,varEmail){
	$.post("../pommo/user/process.php", {formSubmitted: "1", "d[1]": "John", email: "2pm" } );	
	// alert("Nome: "+varNome+"\r\nEmail: "+varEmail);
}

$('document').ready(function(){
        // $('#loading').hide();
		$("#perfil_salario_atual").maskMoney({symbol:"R$",decimal:",",thousands:"."})
		$("#perfil_salario_pretendida").maskMoney({symbol:"R$",decimal:",",thousands:"."})
		$("#cpf").mask("999.999.999-99");
		$("#nascimento").mask("99/99/9999");
		$("#cep").mask("99999-999");
		$("#tel_residencial").mask("(99) 9999-9999");
		$("#tel_comercial").mask("(99) 9999-9999");
		$("#tel_celular").mask("(99) 9999-9999");
		$("#tel_outro").mask("(99) 9999-9999");
		$("#conclusao1").mask("99/99/9999");
		$("#conclusao2").mask("99/99/9999");
		$("#perfil_data_inicio_1").mask("99/99/9999");
		$("#perfil_data_termino_1").mask("99/99/9999");
		$("#perfil_data_inicio_2").mask("99/99/9999");
		$("#perfil_data_termino_2").mask("99/99/9999");
		$("#perfil_data_inicio_3").mask("99/99/9999");
		$("#perfil_data_termino_3").mask("99/99/9999");

		
		// Cadastramento de email no pommo - Sistema de Newsletter
        $('#cadastrar').click(function(){
                
				$('#msgRetorno').remove();

				$('.newsletter ul').append("<li id='msgRetorno'></li>");

				$('#msgRetorno').append("<div class='loadNews'>Carregando</div>");
				
				$('#cadastrar').val("Aguarde.");
				// $('#cadastrar').attr("disabled", true);
				$('#cadastrar').attr("enabled", false); 
				
				$.post("../pommo/user/process.php", {formSubmitted: "1", "d[1]":  $('#nome').val(), Email:  $('#email').val() },
				function(data){
					// alert("Data Loaded: " + data);
					
					$('.newsletter ul').append("<li id='areaRetorno' style='display:none;visibility: hidden;'>"+data+"</li>");
					
					$('#cadastrar').attr("disabled", false); 
					$('#cadastrar').val("Cadastrar");
					
					// alert($('#alertmsg ul li').html());
					// alert($('#alertmsg').attr("class"));
					
					switch($('#alertmsg').attr("class")){
						
						case "error":{
								$('#msgRetorno div').remove();
								$('#msgRetorno').append("<div class='erroNews'>"+ $('#alertmsg ul li').html() +"</div>");
						}break;
						
						case "alert":{
								$('#msgRetorno div').remove();
								$('#msgRetorno').append("<div class='erroNews'>"+ $('#alertmsg ul li').html() +"</div>");
						}break;
						
						case "warn":{
								$('#nome').val("Informe seu nome |");
								$('#email').val("Informe seu e-mail |");
								
								$('#msgRetorno div').remove();
								$('#msgRetorno').append("<div class='okNews' >"+ $('#alertmsg ul li').text() +"</div>");
						}break;
						
						case "error3":{
								$('#nome').val("Informe seu nome |");
								$('#email').val("Informe seu e-mail |");
								
								$('#msgRetorno div').remove();
								$('#msgRetorno').append("<div >"+ $('#alertmsg ul li').html() +"</div>");
						}break;
						
						default:{
							
						}
					}
					
					$('#areaRetorno').remove();
				
				});
				
        });
});


// Função trabalhe conosco para seleção de cursos
function selecionaEscolaridade(valor){
	if (valor >= 6) {
		document.getElementById('instit1').style.display = '';
	} else {
		document.getElementById('instit1').style.display = 'none';
	}
	
	if (valor >= 12) {
		document.getElementById('instit2').style.display = '';
	} else {
		document.getElementById('instit2').style.display = 'none';
	}
	
}

function formataCampo(e, src, mask){
    if (window.event) {
        _TXT = e.keyCode;
    }
    else 
        if (e.which) {
            _TXT = e.which;
        }
    if (_TXT > 47 &&
    _TXT <
    58) {
        var i = src.value.length;
        var saida = mask.substring(0, 1);
        var texto = mask.substring(i);
        if (texto.substring(0, 1) != saida) {
            src.value += texto.substring(0, 1);
        }
        return true;
    }
    else {
        if (_TXT != 8) {
            return false;
        }
        else {
            return true;
        }
    }
}

function validarFormulario(array){

	return false;
	
    validado = false;
    campos = array.split("/");
    for (i = 0; i < campos.length; i++) {
        dados = campos[i].split("|");
        if (document.getElementById(dados[0]).value == "") {
            alert('Favor preencher o campo : ' + dados[2] + ' .');
            document.getElementById(dados[1]).focus();
            validado = false;
            break;
        }
        else {
            validado = true;
        }
    }
    if (validado) {
        alert("Formulário enviado");
        // document.curriculum.submit();
    }
}

/**
 * *****************************FUNCOES
 * GERAIS*****************************************
 */
function byId(element){
    return document.getElementById(element)
}

function Trim(campo){
    return campo.replace(/^\s+|\s+$/g, "");
}

function validateEmail(valor) {
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(valor)){
		return (true)
	} else {
		return (false);
	}
}

//Função que valida o CPF
function validaCPF(cpf) {

	for (var i=0; i<=5; i++) {
		 cpf = cpf.replace(".","");
		 cpf = cpf.replace("-","");
	} 
    
	//alert(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") 
        return false;
    add = 0;
    for (i = 0; i < 9; i++) 
        add += parseInt(cpf.charAt(i)) * (10 - i);
    rev = 11 - (add % 11);
    if (rev == 10 || rev == 11) 
        rev = 0;
    if (rev != parseInt(cpf.charAt(9))) 
        return false;
    add = 0;
    for (i = 0; i < 10; i++) 
        add += parseInt(cpf.charAt(i)) * (11 - i);
    rev = 11 - (add % 11);
    if (rev == 10 || rev == 11) 
        rev = 0;
    if (rev != parseInt(cpf.charAt(10))) 
        return false;
    //alert('O CPF INFORMADO É VÁLIDO.');
    return true;
}

function validateForm(formId){

    flag = 0;
    mensagem = "Verifique todos os campos (*) destacados.\r\n";
    
    for (i = 0; i < byId(formId).elements.length; i++) {
        if (byId(formId).elements[i].className.search(/required/) >= 0) {
            if (!Trim(byId(formId).elements[i].value)) {
                byId(byId(formId).elements[i].id).style.border = 'dashed 1px #FF0000';
                flag++;
            }
        }
        if (byId(formId).elements[i].className.search(/required/) >= 0) {
            if (Trim(byId(formId).elements[i].value)) {
                byId(byId(formId).elements[i].id).style.border = '';
            }
        }
        
        if (byId(formId).elements[i].className.search(/requiredemail/) >= 0) {
            if (!validateEmail(byId(formId).elements[i].value)) {
                byId(byId(formId).elements[i].id).style.border = 'dashed 1px #FF0000';
                flag++;
                mensagem = mensagem+"Email incorreto.\n\r";
            }
            
        }
       
        if (byId(formId).elements[i].className.search(/requiredcpf/) >= 0) {
        	if (!validaCPF(byId(formId).elements[i].value)) {
        		byId(byId(formId).elements[i].id).style.border = 'dashed 1px #FF0000';
        		flag++;
        		mensagem = mensagem+"CPF incorreto.\n\r";
        	}
        }
        
    }
    
    if (flag) {
        alert(mensagem);
        return false;
    } else {
        return true;
    }
    
    
}

