function verificaCampos(){
	var senhaAtual = document.getElementById('senhaAtual').value;
	var novaSenha = document.getElementById('novaSenha').value;
	var confirmarSenha = document.getElementById('confirmarSenha').value;
	if(senhaAtual == "" || novaSenha == "" || confirmarSenha == ""){
		var msg = '';
		if(senhaAtual == ""){
			msg += "Senha atual!\n";
		}
		if(novaSenha == ""){
			msg += "Nova senha!\n";			
		}
		if(confirmarSenha == ""){
			msg += "Confirmar nova senha!\n";
		}		
		alert("Por favor preencher os campos:\n"+msg);		
		return false;	
	}
	if(novaSenha.toString() != confirmarSenha.toString()){
		alert("A nova senha não confere com a confirmação!");				
		return false;	
	}
	if(senhaAtual == novaSenha){		
		alert("A nova senha não pode ser igual há atual!");						
		return false;	
	}
	return true;
}

function confirmarCadastro(){
	if(document.getElementById('pessoa.senha').value != document.getElementById('senhaConfirmacao').value){				
		alert("A senha digitada na confirmação não confere. Por favor verifique novamente!");
		return false;
	}
	
	if(document.getElementById('pessoa.email').value != document.getElementById('emailConfirmacao').value){				
		alert("O email digitado na confirmação não confere. Por favor verifique novamente!");
		return false;
	}
	
	if($('#dataNascimento').val() != ''){
		if(!verificaData($('#dataNascimento').val())){
			alert("Data nascimento inválida!");
			$('#dataNascimento').focus();
			return false;
		}
		return true;
	}
	return true;
}

function pessoaCNPJ(){
	if($('#tipoPessoa').val() == 'true'){
		$('#CNPJ').html('CNPJ*');
		$('#spanPessoaCNPJ').html('<input type="text" class="campoNaoEditavel min city" name="pessoa.CNPJ" id="pessoa.CNPJ" value="'+$('#pessoaJuridica').val()+'" maxlength="18" onkeypress="mascara(this, \'##.###.###/####-##\')" onkeydown="somenteNumeros(event,this,false,false);" >');
	}else{
		$('#CNPJ').html('CPF*');
		$('#spanPessoaCNPJ').html('<input type="text" class="campoNaoEditavel min city" name="pessoa.CNPJ" id="pessoa.CNPJ" value="'+$('#pessoaFisica').val()+'" maxlength="14" onkeypress="mascara(this, \'###.###.###-##\')" onkeydown="somenteNumeros(event,this,false,false);" >');
	}	
	
	//desabilita os campos CPF/CNPJ e o tipo de cadastro.
	var campo = $('#campoNaoEditavel').val();
	if(campo == 'true'){
		$('.campoNaoEditavel').each(function(indice, elemento){
			$(this).attr('disabled','true').addClass('campoNaoEditavelCSS');			
		});		
	}
}

function mudouTipoPessoa(elemento){	
	if(elemento.id == 'fisica'){
		var fisica = "";
		if($('#pessoaFisica').val() != null){
			fisica = $('#pessoaFisica').val();
		}
		$('#spanPessoaCNPJ').html('<input type="text" class="min city" name="pessoa.CNPJ" id="pessoa.CNPJ" value="'+fisica+'" maxlength="14" onkeypress="mascara(this, \'###.###.###-##\')" onkeydown="somenteNumeros(event,this,false,false);" >');
		$('#CNPJ').html('CPF*');		
	}else{
		var juridica = "";
		if($('#pessoaJuridica').val() != null){
			juridica = $('#pessoaJuridica').val();
		}
		$('#spanPessoaCNPJ').html('<input type="text" class="min city" name="pessoa.CNPJ" id="pessoa.CNPJ" value="'+juridica+'" maxlength="18" onkeypress="mascara(this, \'##.###.###/####-##\')" onkeydown="somenteNumeros(event,this,false,false);" >');
		$('#CNPJ').html('CNPJ*');		
	}	
	return true;
}

function mascara(campo, mask){
	var i = campo.value.length;
	var saida = mask.substring(0,1);
	var texto = mask.substring(i);	
	if (texto.substring(0,1) != saida)
	  {
		campo.value += texto.substring(0,1);
	  }
}

function somenteNumeros(event, This, CasasDecimais, ValoresNegativos) {
	if (arguments.length == 1) {
		var s = This.value;
		// garante que o sinal de "-" seja o primeiro do índice
		var i = s.lastIndexOf("-");
		if (i == -1)
			return;
		if (i != 0)
			This.value = s.substring(0, i) + s.substring(i + 1);
		return;
	}
	switch (event.keyCode) {
	case 8: // backspace
	case 9: // tab
	case 37: // left arrow
	case 39: // right arrow
	case 46: // delete
		event.returnValue = true;
		return;
	}
	if (event.keyCode == 189) // sinal de número de negativo
	{
		if (ValoresNegativos == false) {
			CancelEventExecution(event);
			return;
		}
		// aguarda até que o controle tenha sido atualizado
		var Valores = "somentNumeros(document.getElementById('" + This.id + "'))";
		setTimeout(s, 250);
		return;
	}
	if (CasasDecimais && event.keyCode == 188) {
		if (This.value.indexOf(",") >= 0) {
			// restringe a digitação de apenas uma vírgula
			CancelEventExecution(event);
			return;
		}
		event.returnValue = true;
		return;
	}
	// permite caracteres entre 0 e 9
	if (event.keyCode >= 48 && event.keyCode <= 57 || event.keyCode >= 96 && event.keyCode <= 105) {
		event.returnValue = true;
		return;
	}
	CancelEventExecution(event);
}

/*
* Cancela a execução de uma function mapeada por um evento
*/
function CancelEventExecution(event) {
	if (navigator.appName == "Netscape") {
		event.preventDefault();
	} else {
		event.returnValue = false;
	}
}

/**get url quando clica diretamento no login/não é você e alimenta a session (SESSAO_URL_ANTES_LOGIN)
 * 
 * @param requestUrl
 * @param queryString
 * @param path
 * @return
 */
function getUrl(requestUrl, queryString, path){
	var parametros = "";	
	if(queryString != ''){
		parametros = new Array();
		parametros = queryString.split('&');
	}				
	
	$.ajax({
		url: path+"/identificacao/url.jsp",
		dataType: "json",
		data: "url="+requestUrl+"?"+parametros,
		type: "POST",
		cache: false,
		success: function(json){
			window.location = path+'/identificacao/login.jsp';
		}
	});	
}

function excluirEndereco(codEndereco,path){
	if(confirm("Deseja excluir este endereço?")){
		$.getJSON(path+"/identificacao/excluirEndereco.jsp?endereco.codEndereco="+codEndereco, function(json){		
			if(json.actionErrors != null){
				alert(json.actionErrors.toString().replace(',', '\n\n'));		
			}else{
				$('#divEndereco').load(path+"/identificacao/includes/listaEndereco.jsp");	
			}		
		});
		return false;
	}
	$('#divEndereco').load(path+"/identificacao/includes/listaEndereco.jsp");	
	return false;
}

function alterarEndereco(codEndereco,logradouro,complemento,bairro, cidade, uf, cep, referencia, observacao,path)
{
	$("#codEndereco").val(codEndereco);
	$("#logradouro").val(logradouro);
	$("#complemento").val(complemento);
	$("#bairro").val(bairro);
	$("#cidade").val(cidade);
	$("#uf").val(uf);
	$("#cep").val(cep);
	$("#referencia").val(referencia);
	$("#obsEnd").val(observacao);
	
	$('#divEndereco').hide();
	$('#editaEndereco').show('slow');
	$('#divEndereco').load(path+"/identificacao/includes/listaEndereco.jsp");
}

function reinicia(path)
{
	$('#divEndereco').load(path+"/identificacao/includes/listaEndereco.jsp");
	$('#editaEndereco').hide();	
	$('#divEndereco').show('slow');	
}
