///////////////////////////////////////////////////////////////////
//	Funções Ajax
//////////////////////////////////////////////////////////////////
var banner = 'rco_fiqueatento';

function atualizaCaptcha(caminho){
	document.getElementById("divCaptcha").innerHTML = ""
	document.getElementById("divCaptcha").innerHTML = "<img src='" + caminho + "/paginas/includes/captcha.jsp' />"
	
	document.getElementById("txtcaptcha").value = "";
	document.getElementById("txtcaptcha").focus();
}

function busca() {
	carregando();
	var valor = dwr.util.getValue("buscartexto");
	AjaxFacade.getBusca(valor, {
	callback:function(data) {
		dwr.util.setValue("colunacentro", data, { escapeHtml:false, evalScripts:true });
		
		try{
			window.setTimeout('desbloqueiaTela()', 600);
		}catch(e){}
		
	}, errorHandler:function(errorString, exception) {
		window.location = "http://www.checkcheck.com.br/institucional";
	}
	});
}

function acessa(pagina) {
	AjaxFacade.getPagina(pagina, null, {
		callback:function(data) {
		dwr.util.setValue("colunacentro", data, { escapeHtml:false, evalScripts:true });
		
		try {
			initConteudo();
		} catch(e) {}
		try {
			reInitMarquee();
		} catch(e) {}
		try {
			initMascara();
		} catch(e) {}
		try{
			desbloqueiaTela();
		}catch(e){}
		try{
			perguntasFrequentes();
		}catch(e){}
		try {
			centralizarDivCentro($('#mensagemCadastro'));
		}catch(e){}
		try {
			reiniciaDestaque(1);
		}catch(e){}
		try {
			ativaURL();
		}catch(e){}
		analytics();
		
	}, errorHandler:function(errorString, exception) {
		window.location = "http://www.checkcheck.com.br/institucional";
	}
	});
}

function consultaGratuita(parametros) {
	AjaxFacade.consultaGratuita(parametros, {
	callback:function(data) {
		dwr.util.setValue("bloqueiodetelaconteudo", data, { escapeHtml:false, evalScripts:true });
		try{
			exibeBloqueioConteudo();
		}catch(e){}
		try{
			mascaraTelebloqueio();
		}catch(e){}
		try{
			document.captcha.txtcaptcha.focus();
		}catch(e){}
		try {
			centralizarDivCentro($('#mensagemCadastro'));
		}catch(e){}
		try {
			ativaURL();
		}catch(e){}
		analytics();
		
	}, errorHandler:function(errorString, exception) {
		window.location = "http://www.checkcheck.com.br/institucional";
	}
	});
}

function abreDesbloqueioCheques(pagina) {
	AjaxFacade.redireciona(pagina, {
	callback:function(data) {
		dwr.util.setValue("bloqueiodetelaconteudo", data, { escapeHtml:false, evalScripts:true });
		try{
			exibeBloqueioConteudo();
		}catch(e){}
		try{
			document.desbloqueioTelebloqueio.senha.focus();
		}catch(e){}
		try {
			centralizarDivCentro($('#mensagemCadastro'));
		}catch(e){}
		try {
			ativaURL();
		}catch(e){}
		analytics();
		
	}, errorHandler:function(errorString, exception) {
		window.location = "http://www.checkcheck.com.br/institucional";
	}
	});
}

function validaCaptcha(form) {
	var validado;
	var resposta = document.getElementById("mensagemErro");
	if (form.txtcaptcha.value == "") {
		resposta.innerHTML = "<br/>O valor da imagem deve ser preenchido.";
		form.txtcaptcha.focus();
		return false;
	} else {
		AjaxFacade.validaCaptcha(form.txtcaptcha.value,{
			callback : function(data) {
				if (data.trim() == "true") {
					validado = true;
				} else {
					resposta.innerHTML = "<br/>Os caracteres da imagem não foram preenchidos corretamente. Por favor, preencha os dados novamente.";
					form.txtcaptcha.focus();
					validado = false;
				}
			}, async :false, errorHandler : function(errorString, exception) {
				dwr.util.setValue("colunacentro", "Erro geral: " + errorString
						+ exception.message);
			}
		});
	}
	
	if (!validado) {
		atualizaCaptcha('/institucional');
		return false;
	} else {
		bloqueiaBotoesConteudo();
		
		var pagina = form.pagina.value;
		var cpf = form.cpf.value;
		var tipoBloqueio = form.tipoBloqueio.value;
		var cpfcnpj = form.cpfcnpj.value;
		var tipoPessoa = form.tipoPessoa.value;
		var origem = form.origem.value;
		var captcha = form.txtcaptcha.value;
	
		pagina = "/validaCaptcha?pagina=" + pagina;
		pagina += "&cpf=" + cpf;
		pagina += "&tipoBloqueio=" + tipoBloqueio;
		pagina += "&cpfcnpj=" + cpfcnpj;
		pagina += "&tipoPessoa=" + tipoPessoa;
		pagina += "&origem=" + origem;
		pagina += "&txtcaptcha=" + captcha;
	
		AjaxFacade.redireciona(pagina, {
			callback : function(data) {
				dwr.util.setValue("colunacentro", data, {escapeHtml :false});
				try {
					document.captcha.txtcaptcha.focus();
				} catch (e) {}
				try {
					initMascara();
				} catch (e) {}
				try {
					desbloqueiaTela();
				} catch(e) {}
				try {
					recarregaCaptcha(document.getElementById('retornocaptcha'));
				} catch(e) {
					try {
						desbloqueiaTela();
					} catch(e) {}
				}
				try {
					centralizarDivCentro($('#mensagemCadastro'));
				}catch(e){}
				try {
					ativaURL();
				}catch(e){}
				analytics();
			},
			errorHandler : function(errorString, exception) {
				dwr.util.setValue("colunacentro", "Erro geral: "
								+ exception.message);
			}
		});
	}
}

function redireciona(pagina) {
	AjaxFacade.redireciona(pagina, {
		callback:function(data) {
			dwr.util.setValue("colunacentro", data, { escapeHtml:false, evalScripts:true });
			
			try {
				initMascara();
			} catch(e) {}
			try{
				desbloqueiaTela();
			}catch(e){}
			try {
				centralizarDivCentro($('#mensagemCadastro'));
			}catch(e){}
			try {
				ativaURL();
			}catch(e){}
			analytics();
			
		}, errorHandler:function(errorString, exception) {
			window.location = "http://www.checkcheck.com.br/institucional";
		}
	});
}

function redirecionaPrincipal(pagina) {
	AjaxFacade.redireciona(pagina, {
		callback:function(data) {
			dwr.util.setValue("colunacentro", data, { escapeHtml:false, evalScripts:true });
			
			try{
				desbloqueiaTela();
			}catch(e){}
			try {
				centralizarDivCentro($('#mensagemCadastro'));
			}catch(e){}
			try {
				ativaURL();
			}catch(e){}
			try {
				mensagemAlerta();
			}catch(e){}
			analytics();
			
		}, errorHandler:function(errorString, exception) {
			window.location = "http://www.checkcheck.com.br/institucional";
		}
	});
}

function recarregaCaptcha(form) {
	var parametros = "?pagina=" + form.pagina.value;
	parametros += "&cpf" + form.cpf.value;
	parametros += "&tipoBloqueio" + form.tipoBloqueio.value;
	parametros += "&cpfcnpj" + form.cpfcnpj.value;
	parametros += "&tipoPessoa" + form.tipoPessoa.value;
	parametros += "&origem" + form.origem.value;
	parametros += "&mensagem" + form.mensagem.value;

	consultaGratuita(parametros);
}

function carregaConteudoFAQ(div, pergunta, palavra) {
	carregando();
	var pagina = "/respostaFAQ?pergunta=" + pergunta;
	pagina += "&palavra=" + palavra;
	
	AjaxFacade.redireciona(pagina, {
	callback:function(data) {
		dwr.util.setValue(div, data, { escapeHtml:false, evalScripts:true });
		
		try{
			desbloqueiaTela();
		}catch(e){}
		try{
			document.getElementById(div).style.display = '';
		}catch(e){}
		try {
			ativaURL();
		}catch(e){}
		
	}, errorHandler:function(errorString, exception) {
		window.location = "http://www.checkcheck.com.br/institucional";
	}
	});
}

function carregaListaFranquias(uf) {
	try {
		$("#listafranquiaslinha").slideUp();
	} catch(e){}
	
	AjaxFacade.getFranquias(uf, {
	callback:function(data) {
		dwr.util.setValue("listafranquias", data, { escapeHtml:false, evalScripts:true });
		
		try {
			$("#listafranquiaslinha").slideDown();
		} catch(e){}
		
	}, errorHandler:function(errorString, exception) {
		dwr.util.setValue("listafranquias","Erro geral: " + exception.message);
	}
	});
}

function sincroniza(codigo, obj) {
	var pagina = "/paginas/includes/md5.jsp?empCod=" + codigo;
	AjaxFacade.redireciona(pagina,{
		callback : function(data) {
			obj.value = data.trim();
		}, async :false, errorHandler : function(errorString, exception) {
			dwr.util.setValue("colunacentro", "Erro geral: " + errorString
					+ exception.message);
		}
	});
}

function preencheData() {
	var pagina = "/paginas/includes/cabecalho/data.jsp";
	AjaxFacade.redireciona(pagina,{
		callback : function(data) {
			dwr.util.setValue("barra_menu2", data, { escapeHtml:false, evalScripts:true });
		}, async :false, errorHandler : function(errorString, exception) {
			dwr.util.setValue("barra_menu2", "Erro geral: " + errorString
					+ exception.message);
		}
	});
}

function preencheColunaCoringa() {
	if($("#containerCoringa").get(0)!=null) {
		var pagina = "/paginas/includes/corpo/colunacoringa.jsp";
		AjaxFacade.redireciona(pagina,{
			callback : function(data) {
				dwr.util.setValue("containerCoringa", data, { escapeHtml:false, evalScripts:true });
			}, async :false, errorHandler : function(errorString, exception) {
				dwr.util.setValue("containerCoringa", "Erro geral: " + errorString
						+ exception.message);
			}
		});
	}
}

function paginaPrincipal() {
	AjaxFacade.getPagina("conteudo", null, {
		callback:function(data) {
			dwr.util.setValue("conteudo", data, { escapeHtml:false, evalScripts:true });
			
			try {
				initConteudo();
			} catch(e) {}
			try{
				desbloqueiaTela();
			}catch(e){}
			try {
				ativaURL();
			}catch(e){}
			try {
				configuraConteudo();
			}catch(e){}
			try {
				reiniciaDestaque(1);
			}catch(e){}
			try {
				reInitMarquee();
			} catch(e) {}
			$("#utilidadepublicaconteudo_telebloqueio").fadeIn("500");
			analytics();
			
		}, errorHandler:function(errorString, exception) {
			window.location = "http://www.checkcheck.com.br/institucional";
		}
	});
}

//function log() {
//	var resolucao = screen.width + "x" + screen.height;
//	AjaxFacade.log(resolucao,{});
//}
