$(function() {
	$('#login input:visible:first').focus();
	$('input:text').setMask();
});

var AcessoRestrito = {
	salvarParte1Curriculo : function(form) {
		var form = $('.dados_pessoais');
		
		$.alert({type:'loading', html:'Processando, aguarde...', width:200, hide:false});
		
		$.ajax({
			url:'/curriculo/AcessoRestrito.acoes.php?candidato', type:'post', data:$(form).serialize(),
			success:function(response) { //alert(response);
				if ( response.indexOf('Duplicate entry') != -1 && response.indexOf('prs_candidatos') != -1 ) {
					$.alert({type:'error', html:'O CPF digitado já consta em nossa base de candidatos!', width:450});
				} else if(response.indexOf('code:') != -1) {
					if($('.dados_pessoais input[name=act]').val() != 'upd') {
						alert('ATENÇÃO!\nContinue preenchendo seu currículo assim que a página recarregar.');
					}
					
					location.href='?oportunidades-em-aberto&cpt=6&menu=5&form&act=upd&parte2';
				} else {
					alert(response);
					$.alert('hide');
				}		
			},
			error:function(obj, status) {
				$.alert({ type:'error', html:'Falha ao salvar registro! Tente novamente em instantes.', width:380});
			}
		});
		
		return false;
	},
	loginCandidato:function(form) {
		if(form.cpf.value == '' || form.nascimento.value == '') {
			alert('Todos os dados são de preenchimento obrigatório!');
			$('#login input:visible:first').focus();
		} else {
			$.ajax({
				url:'/curriculo/AcessoRestrito.acoes.php?candidato&login', type:'post', data:$(form).serialize(),
				success:function(response) { //alert(response);
					if(response == 'no-user') {
						$.alert({ type:'error', html:'CPF e/ou data de nascimento incorretos!', width:280 });
						form.cpf.value='';
						form.nascimento.value='';
						$('#login input:visible:first').focus();
					} else if(response == 'yes-user') {
						location.href='?vagas';
					} else {
						alert(response);
					}
				}
			});
		}
		
		return false;
	},
	logoutCandidato: function(form){
		$.ajax({
			url:'/curriculo/AcessoRestrito.acoes.php?candidato&logout',
			success:function(response) { //alert(response);
				location.href='?';
			}
		});
	},
	/* Incluir candidato no processo seletivo */
	incluirCandidatoPS:function(vaga, candidato) {
		$('#candidatar_'+vaga).css({color:'#fff'}).html('Processando, aguarde...');
		
		$.ajax({url:'/curriculo/AcessoRestrito.acoes.php?incluirCandidatoPS&vaga='+vaga+'&candidato='+candidato,
			success:function(response) { //alert(response);
				location.reload();
			} 
		});
		
		return false;
	}
}
