
var passo_atual = 0;
var total_paginas = 0;
var mostrarTela = true;

function salvar(){
		$form = $('#formCurriculo');	
		$values = $form.serialize();
		
		if(mostrarTela){
			$d = alert("Salvando alterações...");
		}else{
			$d = null;
		}
		
			$.ajax({
				type: "POST",
				url: "index.php?acao=editarCurriculo&a=s",
				data: $values,
				success: function(msg){	
					//alert(msg);	
					if($d != null){
						$d.dialog("close");
					}
					if(msg == "ok"){
						
					}else{
						alert("Ocorreu um erro ao salvar, por favor tente novamente!");
					}
				}
				});
				
		mostrarTela = true;
	
}

function next(){
	
	mostrarTela = false;
	passo_atual++;
	$tabs.tabs('select', passo_atual);	
	//checkPassos();
	
}

function previous(){
	
	mostrarTela = false;	
	passo_atual--;
	$tabs.tabs('select', passo_atual);
	//checkPassos();	
}

function gotoPasso(p){
	passo_atual = parseInt(p);
	checkPassos();	
}

function goToError(p){
	
	mostrarTela = false;		
	$tabs.tabs("select",parseInt(p));
	checkPassos();	
}

function checkPassos(){
	if(passo_atual > 0){	
	$('#botao_anterior').css("display","");
	}	
	if(passo_atual == total_paginas){	
	$('#botao_proximo').css("display","none");
	$('#botao_cadastrar').css("display","");	
	}	
	if(passo_atual < total_paginas){	
	$('#botao_proximo').css("display","");
	$('#botao_cadastrar').css("display","none");		
	}	
	if(passo_atual == 0){	
	$('#botao_anterior').css("display","none");
	}	
	
	if(typeof($editar) != "undefined"){
		salvar();			
	}	

	
}

$(document).ready(function(){
	
	total_paginas = $('div[id^="tabs-"]').length-1;
	
});

var filhos_num = 0;
var cursos_num = 0;
var idiomas_num = 0;
var areas_num = 0;
var empregos_num = 0;
var html = "";
var bufferInsert = false;
var isStart = true;

function add_filho(){
	
	if(bufferInsert == true && isStart == false){return;}
    bufferInsert = true;
 $.ajax({
   type: "POST",
   url: $BASE+"ajax_curriculo.php?acao=add_filho",
   data: "id="+filhos_num,
   success: function(msg){
 
    html = msg;
	 
	$('<div></div>').attr('id','filho_div'+filhos_num)
				.css('padding','5px 0 5px 0')
				.html(html)
				.appendTo('#filhos');
	
	filhos_num++;
	bufferInsert = false;
	
	runAfter();
		 
   }
 });

}

function remove_filho(id){
    $('#filho_div'+id).remove();
}   

function add_curso(){
	
	if(bufferInsert == true && isStart == false){return;}	
    bufferInsert = true;
 $.ajax({
   type: "POST",
   url: $BASE+"ajax_curriculo.php?acao=add_curso",
   data: "id="+cursos_num,
   success: function(msg){
 
    html = msg;
	 
	$('<div></div>').attr('id','curso_div'+cursos_num)
				.css('padding','5px 0 5px 0')
				.html(html)
				.appendTo('#cursos');
	
	cursos_num++;
	bufferInsert = false;	
	
	runAfter();	
   }
 });

}

function remove_curso(id){
    $('#curso_div'+id).remove();
} 


function add_idioma(){

	if(bufferInsert == true && isStart == false){return;}
    bufferInsert = true;
 $.ajax({
   type: "POST",
   url: $BASE+"ajax_curriculo.php?acao=add_idioma",
   data: "id="+idiomas_num,
   success: function(msg){
 
    html = msg;
	 
	$('<div></div>').attr('id','idioma_div'+idiomas_num)
				.css('padding','5px 0 5px 0')
				.html(html)
				.appendTo('#idiomas');
	
	idiomas_num++;
	bufferInsert = false;	
	
	runAfter();	
   }
 });

}

function remove_idioma(id){
    $('#idioma_div'+id).remove();
} 
function add_area(){

	if(bufferInsert == true && isStart == false){return;}
    bufferInsert = true;
 $.ajax({
   type: "POST",
   url: $BASE+"ajax_curriculo.php?acao=add_area",
   data: "id="+areas_num,
   success: function(msg){
 
    html = msg;
	 
	$('<div></div>').attr('id','area_div'+areas_num)
				.css('padding','5px 0 5px 0')
				.html(html)
				.appendTo('#areas');
	
	areas_num++;
	bufferInsert = false;	
	
	runAfter();	
   }
 });

}

function remove_area(id){
    $('#area_div'+id).remove();
} 


function add_emprego(){
	
	if(bufferInsert == true && isStart == false){return;}	
    bufferInsert = true;
 $.ajax({
   type: "POST",
   url: $BASE+"ajax_curriculo.php?acao=add_emprego",
   data: "id="+empregos_num,
   success: function(msg){
 
    html = msg;
	 
	$('<div></div>').attr('id','emprego_div'+empregos_num)
				.css('padding','5px 0 5px 0')
				.html(html)
				.appendTo('#empregos');
	
	empregos_num++;
	bufferInsert = false;
	
	runAfter();	
   }
 });

}

function remove_emprego(id){
    $('#emprego_div'+id).remove();
} 

function checkForm(){

	var campos_id = new Array("cadastro_nome","cadastro_data_nascimento","cadastro_cpf","cadastro_rg","cadastro_orgao_rg","cadastro_nacionalidade","cadastro_estado_nasceu","cadastro_cidade_nasceu","cadastro_estado_civil","cadastro_endereco_residencial","cadastro_numero","cadastro_bairro","cadastro_cidade","cadastro_estado","cadastro_cep","cadastro_nome_mae","cadastro_ficou_sabendo");
	var campos_nome = new Array("Nome","Data de Nascimento","CPF","RG","Orgão Expeditor RG","Nacionalidade","Estado onde nasceu","Cidade onde nasceu","Estado Civil","Endereço Residencial","Número","Bairro","Cidade","Estado","CEP","Nome da Mãe","Como ficou sabendo do trabalhe conosco?");
	var table_passo = new Array(1,1,1,1,1,1,1,1,1,2,2,2,2,2,3,4);
	
	for(var i = 0;i < campos_id.length;i++){
		if($('#'+campos_id[i]).val() == ""){
			alert("Por favor, preencha o campo '"+campos_nome[i]+"'!");
			goToError(table_passo[i]-1);
			$('#'+campos_id[i]).focus();
			return false;
		}
	}
	return true;
}
	
	
function getCargo(codArea,idArea){
	
	if(codArea == "" || codArea == undefined){return;}

 $.ajax({
   type: "POST",
   url: $BASE+"ajax_curriculo.php?acao=getCargo",
   data: "cod_area="+codArea+"&id="+idArea,
   success: function(msg){
	   		//alert(msg);
 			eval(msg);
   }
 });

}	


function runAfter(){
	$('.data').teclaPressionada({teclas:{numeros:true},formato:"99/99/9999"});
	$('.data2').teclaPressionada({teclas:{numeros:true},formato:"99/9999"});	
	$('.numeros').teclaPressionada({teclas:{numeros:true}});
}

function FormataReais(fld, milSep, decSep, 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(navigator.appName == "Microsoft Internet Explorer"){whichCode = e.keyCode;}
	if (whichCode == 8 || whichCode == 0) return true;
	if (whichCode == 13) return true;
	key = String.fromCharCode(whichCode);// Valor para o código da Chave
	if (strCheck.indexOf(key) == -1) return false; // Chave inválida
	len = fld.value.length;
	for(i = 0; i < len; i++)
		if ((fld.value.charAt(i) != '0') && (fld.value.charAt(i) != decSep)) break;
		aux = '';
		for(; i < len; i++)
			if (strCheck.indexOf(fld.value.charAt(i))!=-1) aux += fld.value.charAt(i);
				aux += key;
				len = aux.length;
				if (len == 0) fld.value = '';
				if (len == 1) fld.value = '0'+ decSep + '0' + aux;
				if (len == 2) fld.value = '0'+ decSep + aux;
				if (len > 2) {
					aux2 = '';
					for (j = 0, i = len - 3; i >= 0; i--) {
						if (j == 3) {
						aux2 += milSep;
						j = 0;
					}
					aux2 += aux.charAt(i);
					j++;
				}
				fld.value = '';
				len2 = aux2.length;
				for (i = len2 - 1; i >= 0; i--)
					fld.value += aux2.charAt(i);
					fld.value += decSep + aux.substr(len - 2, len);
				}

	return false;
}

jQuery.fn.teclaPressionada = function(options) {
	
	var o = jQuery.extend({
		formato : "",
		teclas : {}
	}, options);	
	
	var isCtrl = false;
	var isDelete = false;	


	this.keydown(function(event){
		
		switch(getTecla(event)){
			case 8: 
			case 46: isDelete = true; break;
			case 17: isCtrl = true;	break;	
		}
				
	});
	
	this.keyup(function(event){
						   
		switch(getTecla(event)){
			case 8: 
			case 46: isDelete = false; return; break;
			case 17: isCtrl = false; break;
		}
		
		if(o.formato){
			
			var i, nCount, sValue, fldLen, mskLen,bolMask, sCod, nTecla;
	 
			val = this.value;
			val = val.replace(/[\[\\\^\$\.\|\?\*\+\(\)\-\s\/\]]/g,"");
			fldLen = val.length;
			mskLen = o.formato.length;
			
			i = 0;
			nCount = 0;
			sCod = "";
			mskLen = fldLen;			
			
			while (i <= mskLen) {
				bolMask = ((o.formato.charAt(i) == "-") || (o.formato.charAt(i) == ".") || (o.formato.charAt(i) == "/"))
				bolMask = bolMask || ((o.formato.charAt(i) == "(") || (o.formato.charAt(i) == ")") || (o.formato.charAt(i) == " ") || (o.formato.charAt(i) == ":"))

				if (bolMask) {
					sCod += o.formato.charAt(i);
					mskLen++; }
				else {
					sCod += val.charAt(nCount);
					nCount++;
				}

				i++;
			}
			
			if(sCod.length > o.formato.length){
				sCod = sCod.substr(0,o.formato.length);
			}

			this.value = sCod;		
		}
				
	});	
	
	this.keypress(function(event){
		
		if(isCtrl == true){return;}
		
		tecla = getTecla(event);
		if(o.teclas.caracteres){
			if ((tecla != 8) && (tecla != 0) && (tecla <= 43 || tecla >= 58)){
				event.preventDefault();	
			}
		}else if(o.teclas.numeros){
			if ((tecla != 8) && (tecla != 0) && (tecla <= 46 || tecla >= 58)){
				event.preventDefault();	
			}
		}
		
	});
	
	function getTecla(e){
		if($.browser.mozilla){return e.which;}else{return e.keyCode;}
	}
	
	return this;
	
};
