// JavaScript Document
// Luís Fernando Viégas

function openAjax() {
var ajax;
try{
    ajax = new XMLHttpRequest();
	}catch(ee){
		try{
			ajax = new ActiveXObject("Msxml2.XMLHTTP");
		}catch(e){
			try{
				ajax = new ActiveXObject("Microsoft.XMLHTTP");
			}catch(E){
				ajax = false;
			}
		}
	}
return ajax;
}

// Funcao POPUP

PositionX = 100;
PositionY = 100;

defaultWidth  = 50;
defaultHeight = 50;

var AutoClose = true;

if (parseInt(navigator.appVersion.charAt(0))>=4){
var isNN=(navigator.appName=="Netscape")?1:0;
var isIE=(navigator.appName.indexOf("Microsoft")!=-1)?1:0;}
var optNN='scrollbars=no,width='+defaultWidth+20+',height='+defaultHeight+',left='+PositionX+',top='+PositionY;
var optIE='scrollbars=no,width=150,height=100,left='+PositionX+',top='+PositionY;
function amplia(imageURL,legenda){
if (isNN){imgWin=window.open('about:blank','',optNN);}
if (isIE){imgWin=window.open('about:blank','',optIE);}
with (imgWin.document){
writeln('<html><head><title>'+legenda+'</title><style>body{margin:0px;}</style>');writeln('<sc'+'ript>');
writeln('var isNN,isIE;');writeln('if (parseInt(navigator.appVersion.charAt(0))>=4){');
writeln('isNN=(navigator.appName=="Netscape")?1:0;');writeln('isIE=(navigator.appName.indexOf("Microsoft")!=-1)?1:0;}');
writeln('function reSizeToImage(){');writeln('if (isIE){');writeln('window.resizeTo(100,100);');
writeln('width=112-(document.body.clientWidth-document.images[0].width);');
writeln('height=100-(document.body.clientHeight-document.images[0].height);');
writeln('window.resizeTo(width,height);}');writeln('if (isNN){');       
writeln('window.innerWidth=document.images["George"].width;');writeln('window.innerHeight=document.images["George"].height;}}');
writeln('function doTitle(){document.title=legenda;}');writeln('</sc'+'ript>');
if (!AutoClose) writeln('</head><body bgcolor=FFFFFF scroll="no" onload="reSizeToImage();doTitle();self.focus()">')
else writeln('</head><body bgcolor=FFFFFF scroll="no" onload="reSizeToImage();doTitle();self.focus()" onblur="self.close()">');
writeln('<img name="George" src='+imageURL+' style="display:block"></body></html>');
close();		
}}

function login(act){
	if(act=='out' || (document.getElementById('f_login').value!="" && document.getElementById('senha').value!="")){
		
		if(act=='out'){
			var url="ajax/logout.php";
		}else{
			var url='ajax/login.php?l='+document.getElementById('f_login').value+'&s='+document.getElementById('senha').value;
		}
		var ajax= openAjax();
		ajax.open("GET", url, true);
		ajax.send(null);
		ajax.onreadystatechange = function() {
											if (ajax.readyState == 4) {
												if (ajax.status == 200) {
														if(ajax.responseText=="0"){
															alert('Usuário ou Senha inválidos.');
														}else{
															document.getElementById('boxLogin').innerHTML=ajax.responseText;
															if(act=='out'){
																document.getElementById('mensagem').style.display='none';
															}else{
																document.getElementById('mensagem').style.display='inline';
																include('mensagem','mensagem','');
															}
														}
												}else{
													alert("Erro:\n" + ajax.statusText);
												}
											 }
											}
	}else{
		alert('Preencha o Login e a Senha');
	}
}
// Apenas Inclui arquivos
function include(arquivo,destino,id){
	var url='ajax/'+arquivo+'.php?id='+id;
	var ajax= openAjax();
		ajax.open("GET", url, true);
		ajax.send(null);
		ajax.onreadystatechange = function() {
											if (ajax.readyState == 4) {
												if (ajax.status == 200) {
													document.getElementById(destino).innerHTML=ajax.responseText;
												}else{
													alert("Erro:\n" + ajax.statusText);
												}
											 }
											}
}
// Se a div destino estiver preenchida torna-a "Abre/Fecha"
function include2(arquivo,destino,id){
	if(document.getElementById(destino).style.display=='none'){
	var url='ajax/'+arquivo+'.php?id='+id;
	var ajax= openAjax();
		ajax.open("GET", url, true);
		ajax.send(null);
		ajax.onreadystatechange = function() {
											if (ajax.readyState == 4) {
												if (ajax.status == 200) {
													document.getElementById(destino).style.display='inline';
													document.getElementById(destino).innerHTML=ajax.responseText;
												}else{
													alert("Erro:\n" + ajax.statusText);
												}
											 }else{
												 document.getElementById(destino).innerHTML='<p><img src=\"imagens/load.gif\" alt=\"Carregando...\"></p>';
											 }
											}
	}else{
		document.getElementById(destino).style.display='none';
	}
}

// Força o FIREFOX a submeter quando teclar ENTERT
function envia(event){
	if(event.keyCode==13){
		login();
	}
}

function confirma(tipo,add)
{
	if(add==undefined){
		add="";
	}else{
		add="\n"+add;	
	}
	if(confirm('Remover '+tipo+'?'+add))
	{
		return true;
	}
	return false;
}

function detalhes(id,destino,nome){
	
	if(document.getElementById(nome+'_'+id).innerHTML==''){
	var url='ajax/'+destino+'.php?id='+id;
	var ajax= openAjax();
		ajax.open("GET", url, true);
		ajax.send(null);
		ajax.onreadystatechange = function() {
											if (ajax.readyState == 4) {
												if (ajax.status == 200) {
													document.getElementById(nome+'_'+id).innerHTML=ajax.responseText;
												}else{
													alert("Erro:\n" + ajax.statusText);
												}
											 }
											}
	}else{
		document.getElementById(nome+'_'+id).innerHTML='';	
	}
}

function num(e)
{
	if (document.all) // Internet Explorer
		var tecla = event.keyCode;
	else if(document.layers) // Nestcape
		var tecla = e.which;
		if (tecla > 45 && tecla < 58) // numeros de 0 a 9
			return true;
		else
			{
				if(tecla == 44){
					event.keyCode = 46;
					return true;
				}
				else{
					if (tecla != 8) // backspace
						event.keyCode = 0;
						//return false;
					else
						return true;
				}
			}
}