function ObjetoAjax(){
	var req;

	try{
		req = new XMLHttpRequest();
	}catch(e){
		try{
			req = new ActiveXObject("Msxml2.XMLHTTP");
		}catch(e2){
			try{
				req= new ActiveXObject("Microsoft.XMLHTTP");
			}catch(e3){
				alert("Esser browser não tem recursos para rodar AJAX!");
				req = null;
			}
		}
	}
	return req;
}

function logar()
{
	mess = valida2();
	if(mess != "")
	{
		alert(mess);
		escondeCarregando();
		return;
	}

	var dados = "servico=ajaxLogar";

	dados = dados + "&nomeUsuario=" + document.getElementById('nomeUsuario').value;
	dados = dados + "&SenhaUsuario=" + document.getElementById('SenhaUsuario').value;

	ajax = ObjetoAjax();
	ajax.open("POST","index.php");
	ajax.onreadystatechange=function()
	{
		if (ajax.readyState==4)
		{
			x = ajax.responseText;

			if(trim(x) == "845651"){
				limpaCampos();
				tipo = document.getElementById('erro');
				tipo.innerHTML = '<font color="red">Senha ou login invalidos.</font>';
				escondeCarregando();
			}
			else
			{
				x = x.split("\n");
				limpaCampos();

				document.getElementById("nome").readOnly       = true;
							
				document.getElementById("nome").value          = trim(x[1]);
				document.getElementById("usuario").value       = trim(x[3]);
				document.getElementById("senha").value         = trim(x[5]);
				document.getElementById("email").value         = trim(x[7]);
				document.getElementById("email2").value        = trim(x[7]);
				document.getElementById("frase").innerHTML = "<b>Deixar a conta ativa?</b>";
				marcaCheck(trim(x[9]));
				trocaDisplay("help2","help1");
				document.getElementById("cadastrar").value = "ALTERAR" ;
				document.getElementById("validacao").style.display = "none";
				document.getElementById("btnHistorico").style.display = "";
				document.getElementById("cancelar").style.display = "";
				escondeCarregando();
			}
		}
	}
	ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	ajax.send(dados);

}

function valida2(){
	mostraCarregando();
	usuario  =  document.getElementById("nomeUsuario").value;
	senha    =  document.getElementById("SenhaUsuario").value;

	var mess = '';

	if(usuario == '') mess  = "Preencha o campo usuário.\n";
	if(senha   == '') mess += "Preencha o campo senha.\n";

	if(mess!=''){
	
		return mess;

	}
	else
	{
		return "";
	}

}

function limpaCampos()
{
	mostraCarregando();
	recarregaCaptcha();
	document.getElementById("cadastroL").style.display = "";
    document.getElementById("historico").style.display = "none";
	document.getElementById("validacao").style.display = "";
	document.getElementById("cancelar").style.display = "none";
	document.getElementById("btnHistorico").style.display = "none";
	trocaDisplay("help1","help2");
	tipo = document.getElementById('erro');
	tipo.innerHTML                                = '';
	document.getElementById("nomeUsuario").value  = "";
	document.getElementById("SenhaUsuario").value = "";
	document.getElementById("nome").value         = "";
	document.getElementById("usuario").value      = "";
	document.getElementById("senha").value        = "";
	document.getElementById("email").value        = "";
	document.getElementById("email2").value       = "";
	document.getElementById("cadastrar").value    = "CADASTRAR";
	document.getElementById("nao").checked        = false;
	document.getElementById("sim").checked        = false;
	document.getElementById("frase").innerHTML    = '<b>Concordo que todos meus <br> scraps serão apagados.</b>';
	document.getElementById("nome").readOnly      = false;
	escondeCarregando();
}

function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function marcaCheck(paramentro){
	if(paramentro == 'n') document.getElementById("nao").checked = true;
	if(paramentro == 's') document.getElementById("sim").checked = true;
}
function trocaDisplay(display1,display2){
	document.getElementById(display1).style.display = "block";
	document.getElementById(display2).style.display = "none";
}

function incluiAltera(){
	botao    =  document.getElementById("cadastrar").value;
	if(botao == "CADASTRAR")
	{
		cadastrar();
		return
	}
	if(botao == "ALTERAR")
	{
		
		alterar();
		return
	}
	return
}

function alterar()
{

	var dados = "servico=ajaxAlterar";
	
	dados = dados + "&nome="    + escape(document.getElementById('nome').value);
	dados = dados + "&usuario=" + escape(document.getElementById('usuario').value);
	dados = dados + "&senha="   + escape(document.getElementById('senha').value);
	dados = dados + "&email="   + escape(document.getElementById('email').value);
	dados = dados + "&status="  + escape(radioValue());


	


	ajax = ObjetoAjax();
	ajax.open("POST","index.php");
	ajax.onreadystatechange=function()
	{
		if (ajax.readyState==4)
		{
			x = ajax.responseText;
			alert(x);
			escondeCarregando();
		}
	}
	ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	ajax.send(dados);

}

function radioValue()
{
	if(document.getElementById("nao").checked == true) return "n";
	if(document.getElementById("sim").checked == true) return "s";

}

function cadastrar()
{

	var dados = "servico=ajaxCadastrar";

	dados = dados + "&nome="        + escape(document.getElementById('nome').value);
	dados = dados + "&usuario="     + escape(document.getElementById('usuario').value);
	dados = dados + "&senha="       + escape(document.getElementById('senha').value);
	dados = dados + "&email="       + escape(document.getElementById('email').value);
	dados = dados + "&private_key=" + escape(document.cadastro.private_key.value);
	dados = dados + "&public_key="  + escape(document.cadastro.public_key.value);
	dados = dados + "&hncaptcha="   + escape(document.cadastro.hncaptcha.value);

	ajax = ObjetoAjax();
	ajax.open("POST","index.php");
	ajax.onreadystatechange=function()
	{
		if (ajax.readyState==4)
		{
			x = ajax.responseText;
			if(trim(x.substring(0,7))=="reload")
			{
				document.getElementById("captcha").innerHTML = x.substr(7);
                escondeCarregando();
			}
			else
			{
				alert(x);
				limpaCampos();
			}


		}
	}
	ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	ajax.send(dados);

}

function recarregaCaptcha(){
	
    var dados = "servico=ajaxRecarregaCaptcha";

	ajax = ObjetoAjax();
	ajax.open("POST","index.php");
	ajax.onreadystatechange=function()
	{
		if (ajax.readyState==4)
		{
			x = ajax.responseText;
			document.getElementById("captcha").innerHTML = x;
			
		}
	}
	ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	ajax.send(dados);
}

function carregaHistorico(){
	
    var dados = "servico=ajaxHistorico";

	ajax = ObjetoAjax();
	ajax.open("POST","index.php");
	ajax.onreadystatechange=function()
	{
		if (ajax.readyState==4)
		{
			x = ajax.responseText;
			document.getElementById("conteudoHistorico").innerHTML = x;
			
		}
	}
	ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	ajax.send(dados);
}


function mostraCarregando()
{
	document.getElementById("carregando").style.display = "";
}
function escondeCarregando()
{
	document.getElementById("carregando").style.display = "none";
}


