// JavaScript Document

function abre(id)
{
	el = document.getElementById(id);
	if (el.style.display == "none")
		el.style.display = "block";
	else
		el.style.display = "none";
}

eventosConstantes = function() 
{
	// Colore Menu Lateral
	var itemMenu;
	var menuLateral = document.getElementById("menuLateral");
	for (i = 0; i < menuLateral.childNodes.length; i++) 
	{
		itemMenu = menuLateral.childNodes[i];
		if (itemMenu.nodeName=="LI") 
		{
			if (itemMenu.className != "selecionado")
			{
				itemMenu.onmouseover = function() 
				{
					this.className = "sobre";
				}
				itemMenu.onmouseout = function() 
				{
					this.className = "";
				}
			}
		}
	}
	
	// Colore Icones Ferramentas
	var ferramenta;
	var imagemFerramenta;
	var icone;
	var ferramentas = document.getElementById("ferramentas").childNodes[0];
	for (i = 0; i < ferramentas.childNodes.length; i++) 
	{
		ferramenta = ferramentas.childNodes[i];
		if (ferramenta.nodeName=="IMG") 
		{
			ferramenta.onmouseover = function() 
			{
				icone = this.src;
				icone = icone.split(".gif");
				this.src = icone[0] + "_sobre.gif";
			}
			ferramenta.onmouseout = function() 
			{
				icone = this.src;
				icone = icone.split(".gif");
				this.src = icone[0].substr(0, icone[0].length-6) + ".gif";
			}
		}
		if (ferramenta.nodeName=="A") 
		{
			imagemFerramenta = ferramenta.childNodes[0];
			imagemFerramenta.onmouseover = function() 
			{
				icone = this.src;
				icone = icone.split(".gif");
				this.src = icone[0] + "_sobre.gif";
			}
			imagemFerramenta.onmouseout = function() 
			{
				icone = this.src;
				icone = icone.split(".gif");
				this.src = icone[0].substr(0, icone[0].length-6) + ".gif";
			}
		}
	}
}

function login()
{
	mostraDivCimaModal('cimaLogin');
	document.getElementById("usuario").focus();
}

function logout(sid)
{
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null) {
		alert ("Browser does not support HTTP Request");
		return;
	}
	var url="interacaoBD/logout.php";
	var campos = "sid="+sid;

	xmlHttp.onreadystatechange = logando;
	xmlHttp.open("POST",url,true);
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=iso-8859-1");
	xmlHttp.setRequestHeader("Content-length", campos.length);         
	xmlHttp.send(campos);
}

function fazerLogin()
{
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null) {
		alert ("Browser does not support HTTP Request");
		return;
	}
	var url="interacaoBD/login.php";
	var campos = buscaCamposForm(document.getElementById('formLogin'));
	xmlHttp.onreadystatechange = logando;
	xmlHttp.open("POST",url,true);
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=iso-8859-1");
	xmlHttp.setRequestHeader("Content-length", campos.length);         
	xmlHttp.send(campos);
}

function logando()
{
	if(xmlHttp.readyState==1){
		document.getElementById("mensagem").innerHTML = "<img src='template/imagens/carregando.gif' />";
	}
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") 
	{
		var resposta = xmlHttp.responseText;
		
		if (resposta == 'erro')
			document.getElementById("mensagem").innerHTML = "Foi especificado um usuário e/ou senha inválido(s).";
		else if (resposta == 'errologout')
			document.getElementById("mensagem").innerHTML = "Não foi possível fazer logout.";
		else if (resposta == 'ok')
		{
			fechaDivCimaModal();
			carregaDados('interacaoBD/buscaFerramentas.php', carregaFerramentas);
		}
	}
}

function carregaFerramentas()
{
	if(xmlHttp.readyState==1){
		document.getElementById("ferramentas").innerHTML = '<span class="fundo"><img src="template/imagens/carregando.gif" /></span>';
	}
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") 
	{
		document.getElementById("ferramentas").innerHTML = '<span class="fundo">'+xmlHttp.responseText+'</span>';
	}
}

// Funções para a janela "modal"
function desabilitaFundo()
{
	var img = document.getElementById('imgBloqueioFundo');	
	img.style.display = "inline";
	if (navigator.appName == "Microsoft Internet Explorer")
	{
		img.style.width = "200%";
		img.style.height = "500%";
	}
	else
	{
		img.style.width = "100%";
		img.style.height = "100%";
	}
}

function habilitaFundo()
{
	document.getElementById('imgBloqueioFundo').style.display = "none";
}

function mostraDivCimaModal(id)
{
	var div = document.getElementById(id);
	
	desabilitaFundo();
	
	document.getElementById("mensagem").innerHTML = "";
	div.style.display = "inline";
	if (navigator.appName == "Microsoft Internet Explorer")
		desabilitaBarraRolagem();	
}

function desabilitaBarraRolagem()
{
	document.body.style.overflow = "hidden";
	document.documentElement.style.overflow = "hidden";
	document.body.scroll = "no";
	parent.scroll(0, 0);
}

function fechaDivCimaModal()
{
	document.getElementById("mensagem").innerHTML = "";
	document.getElementById('cimaLogin').style.display = "none";
	
	habilitaFundo();
	
	if (navigator.appName == "Microsoft Internet Explorer")
	{
		document.body.style.overflow = "auto";
		document.documentElement.style.overflow = "auto";
		document.body.scroll = "auto";
	}
}