// JavaScript Document

function isEmail(string) {
	if (string.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1) {
		return true;
	}
	else return false;
}

function isDate( string ) {
	if (string.search("(0[1-9]{1}|[12]{1}[0-9]{1}|3[01]{1})/(0[1-9]{1}|1[012]{1})/([12]{1}[0-9]{3})") != -1) {
		return true;
	}
	else return false;
}

function isDatePartial( string ) {
	if (string.search("(0[1-9]{1}|1[012]{1})/([12]{1}[0-9]{3})") != -1) {
		return true;
	}
	else return false;
}

var submeter = true;

$(document).ready(function() {

	var experiencias = 1;
	
	/* Mascara de Validação */
	$("input[@name=nascimento]").mask("99/99/9999");
	$("input[@name=cep]").mask("99999-999");
	$(".telefone").mask("(99) 9999 9999");
	$(".data_mes").mask("99/9999");

	$("input[@name=salario_atual]").maskMoney( { symbol: "", decimal: ",", thousands: "" } );
	$("input[@name=salario_pretendido]").maskMoney( { symbol: "", decimal: ",", thousands:"" } );

	$('.mais_experiencia').click(function(){
		if (experiencias < 3) {
			$('.exp_profissional:last').clone().insertAfter('.exp_profissional:last');
			$('.exp_profissional:last').find('input').each(function(){
				$(this).removeAttr('value');
			});
			$('.exp_profissional:last').find('textarea').each(function(){
				$(this).removeAttr('value');
				$(this).text('');
			});
			$('.exp_profissional:last').find('.data_mes').each(function(){
				$(this).mask("99/9999");
			});
			experiencias++;
		}
		if (experiencias == 3) {
			$('.mais_experiencia').hide();
		}
		return false;
	});

	$('.habilitacao').click(function() {
		if ($(this).val() == 'S') {
			$('#tem_carteira').show();
		}
		else {
			$('#tem_carteira').hide();
		}
	});
	
	$('.tem_parentes').click(function() {
		if ($(this).val() == 'Sim') {
			$('#tem_parentes_grau_hi').show();
		}
		else {
			$('#tem_parentes_grau_hi').hide();
		}
	});
	
	$('.tem_parentes_grau').click(function() {
		if ($('.tem_parentes_grau:checked').val() == 'Outro') {
			$('#parentes_outros').show();
		}
		else {
			$('#parentes_outros').hide();
		}
	});
	
	$('.trabalhou_conosco').click(function() {
		if ($(this).val() == 'Sim') {
			$('#se_ja_trabalhou').show();
		}
		else {
			$('#se_ja_trabalhou').hide();
		}
	});
	
	// Validação da etapa 1
	$('#check_etapa1').click(function() {
		var valido = true;
		var errors = '';
		
		var tmp = $('#nome').val();
		if (tmp.length < 1) {
			valido = false;
			errors += '- Nome é obrigatorio!\n';
		}
		
		var tmp = $('#nascimento').val();
		if (tmp.length < 1) {
			valido = false;
			errors += '- Nascimento é obrigatorio!\n';
		}
		else if (!isDate(tmp)) {
			valido = false;
			errors += '- Data de Nascimento invalida!\n';
		}
		
		if ($('.sexo:checked').size() != 1) {
			valido = false;
			errors += '- Sexo é obrigatorio!\n';
		}
		
		var tmp = $('#naturalidade').val();
		if (tmp.length < 1) {
			valido = false;
			errors += '- Naturalidade é obrigatorio!\n';
		}
		
		var tmp = $('#rg').val();
		if (tmp.length < 1) {
			valido = false;
			errors += '- Identidade é obrigatorio!\n';
		}
		
		var tmp = $('#rg_orgao').val();
		if (tmp.length < 1) {
			valido = false;
			errors += '- Orgão Emissor é obrigatorio!\n';
		}
		
		if ($('.habilitacao:checked').size() == 1) {
			var tmp = $('#tipocarteira').val();
			if ( $('.habilitacao:checked').val() == 'S' && tmp.length < 1) {
				valido = false;
				errors += '- Você deve informar o tipo de habilitação!\n';
			}
		}
		else {
			valido = false;
			errors += '- Habilitação é obrigatorio!\n';
		}
		
		if ($('.deficiencia:checked').size() != 1) {
			valido = false;
			errors += '- Você deve informar se possui ou não alguma deficiência!\n';
		}
		
		var tmp = $('.estado_civil option:selected').val();
		if (tmp.length < 1) {
			valido = false;
			errors += '- Você deve informar o estado civil!\n';
		}
		
		if (!valido) {
			alert( 'Verifique os seguintes itens:\n' + errors );
			submeter = false;
		}
		else {
			$('#etapa1').fadeOut();
			$('#etapa2').fadeIn();
			submeter = true;
		}
		
	});
	
	$('#etapa1_v').click(function(){
		$('#etapa2').fadeOut();
		$('#etapa1').fadeIn();
	});
	
	$('#check_etapa2').click(function(){
		var valido = true;
		var errors = '';
		
		var tmp = $('.endereco').val();
		if (tmp.length < 1) {
			valido = false;
			errors += '- Você deve informar seu endereço!\n';
		}
		
		var tmp = $('.bairro').val();
		if (tmp.length < 1) {
			valido = false;
			errors += '- Você deve informar seu bairro!\n';
		}
		
		var tmp = $('.cidade').val();
		if (tmp.length < 1) {
			valido = false;
			errors += '- Você deve informar sua cidade!\n';
		}
		
		var tmp = $('.uf option:selected').val();
		if (tmp.length < 1) {
			valido = false;
			errors += '- Você deve informar o seu estado!\n';
		}
		
		var tmp = $('.cep').val();
		if (tmp.length < 1) {
			valido = false;
			errors += '- Você deve informar o seu CEP!\n';
		}
		
		var tmp = $('.tel_res').val();
		if (tmp.length < 1) {
			valido = false;
			errors += '- Você deve informar o seu telefone residencial!\n';
		}
		
		var tmp = $('.email').val();
		if (tmp.length < 1) {
			valido = false;
			errors += '- Você deve informar o seu email!\n';
		}
		else if (!isEmail(tmp)) {
			valido = false;
			errors += '- Email invalido!\n';
		}
		
		if (!valido) {
			alert('Verifique os seguintes itens:\n' + errors);
			submeter = false;
		}
		else {
			$('#etapa2').fadeOut();
			$('#etapa3').fadeIn();
			submeter = true;
		}
		
	});
	
	$('#etapa2_v').click(function() {
		$('#etapa3').fadeOut();
		$('#etapa2').fadeIn();
	});
	
	$('#check_etapa3').click(function() {
		var valido = true;
		var errors = '';
		
		var tmp = $('.escolaridade option:selected').val();
		if (tmp.length < 1) {
			valido = false;
			errors += '- Você deve informar sua escolaridade!\n';
		}
		
		var tmp = $('#data_inicio_estagio').val();
		if (tmp.length > 0 && !isDatePartial(tmp)) {
			valido = false;
			errors += '- O campo data inicio deve ser preenchido por uma data correta!\n';
		}
				
		var tmp = $('#data_fim_estagio').val();
		if (tmp.length > 0 && !isDatePartial(tmp)) {
			valido = false;
			errors += '- O campo data fim deve ser preenchido por uma data correta!\n';
		}
		
		if ($('.informatica:checked').size() != 1) {
			valido = false;
			errors += '- Você deve informar se possui ou não conhecimentos em informatica!\n';
		}
		
		if (!valido) {
			alert('Verifique os itens abaixo: \n' + errors);
			submeter = false;
		}
		else {
			$('#etapa3').fadeOut();
			$('#etapa4').fadeIn();
			submeter = true;
		}
	});
	
	$('#etapa3_v').click(function(){
		$('#etapa4').fadeOut();
		$('#etapa3').fadeIn();
	});
	
	$('#check_etapa4').click(function() {
		var valido = true;
		var errors = '';
		
		var i = 0;
		$('.data_inicio_exp').each(function(){
			var tmp = $(this).val();
			if (tmp.length > 1 && !isDatePartial(tmp)) {
				valido = false;
				errors += '- O ' + (i+1) + ' campo de data inicio é uma data invalida!\n';
			}
			i++;
		});
		
		var i = 0;
		$('.data_fim_exp').each(function(){
			var tmp = $(this).val();
			if (tmp.length > 1 && !isDatePartial(tmp)) {
				valido = false;
				errors += '- O ' + (i+1) + ' campo de data fim é uma data invalida!\n';
			}
			i++;
		});
		
		if (!valido) {
			alert('Verifique os seguintes itens:\n' + errors);
			submeter = false;
		}
		else {
			$('#etapa4').fadeOut();
			$('#etapa5').fadeIn();
			submeter = true;
		}

	});
	
	$('#etapa4_v').click(function() {
		$('#etapa5').fadeOut();
		$('#etapa4').fadeIn();
	});
	
	
	$('#curriculo').submit(function()
	{
		var etapa1 = true;
		var etapa2 = true;
		var etapa3 = true;
		var etapa4 = true;
		
			
		$('#check_etapa1').trigger( 'click' );
		if( !submeter ) 
			return false;
			
			
		$('#check_etapa2').trigger( 'click' );
		if( !submeter )
			return false;
		
		
		$('#check_etapa3').trigger( 'click' );
		if( !submeter )
			return false;
		
		
		$('#check_etapa4').trigger( 'click' );
		if( !submeter )
			return false;
			
	});
	
});
