//----------------------------------------------------------------------------
// Funções para rollover
//----------------------------------------------------------------------------

function swapImgRestore() {
  var i,x,a=document.sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function preloadImages() {
  var d=document; if(d.images){ if(!d.p) d.p=new Array();
    var i,j=d.p.length,a=preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.p[j]=new Image; d.p[j++].src=a[i];}}
}

function findObj(n, d) {
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function swapImage() {
  var i,j=0,x,a=swapImage.arguments; document.sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=findObj(a[i]))!=null){document.sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

//----------------------------------------------------------------------------
// Funções para formulários
//----------------------------------------------------------------------------

// Mostra a janela pop-up de login

function login(logname)
{
	var popupWindow = null;

	if(!logname)
		return alert("Please type your username.");
	if(logname == "_empty_")
		logname = "";
	if(popupWindow != null)
		popupWindow.close();
	xPos = window.screenLeft + 280;
	yPos = window.screenTop + 80;
	// innerWidth vs. width = Netscape vs, Explorer = http://www.webreference.com/js/tips/000504.html:
	popupWindow = window.open("","newWin","scrollbars=0, width=230, innerWidth=230, height=220, innerHeight=220, left=" + xPos + ", screenX=" + xPos + ", top=" + yPos + ", screenY=" + yPos);
	popupWindow.document.clear;
	popupWindow.document.location = "login.php?logname=" + logname;
}

// Mostra a dica do campo fieldname em uma janela pop-up

function hint(fieldname)
{
	var popupWindow = null;

	if(popupWindow != null)
		popupWindow.close();
	xPos = max(0, window.event.x + window.screenLeft - 320);
	yPos = max(0, window.event.y + window.screenTop - 250);
	// innerWidth vs. width = Netscape vs, Explorer = http://www.webreference.com/js/tips/000504.html:
	popupWindow = window.open("","newWin","scrollbars=1, width=300, innerWidth=300, height=250, innerHeight=250, left=" + xPos + ", screenX=" + xPos + ", top=" + yPos + ", screenY=" + yPos);
	popupWindow.document.clear;
	popupWindow.document.location = "hints/" + fieldname + ".html";
}

// Verifica se os campos na string "fields" do formulário foram preenchidos

function checkRequired(which, fields)
{
	if(fields == "")
		return true;

	var checked = true;

	for(i = 0; i < which.length; i++) {
		var fieldobj = which.elements[i];
		if(((fieldobj.type == "text" || fieldobj.type == "password" || fieldobj.type == "textarea" || fieldobj.type == "file") && trim(fieldobj.value) == '') || fieldobj.selectedIndex == 0) {
			if(fields.indexOf(fieldobj.name) != -1) {
				checked = false;
				break;
			}
		}
	}

	if(!checked) {
		alert("O campo " + fieldobj.name + " deve ser preenchido.");
		return false;
	} else
		return true;
}

// Remove whitespace antes e depois da string, para evitar que o usuário digite só espaços
// http://www.p2p.wrox.com/archive/javascript/2001-02/123.asp
function trim(strn)
{
	return strn.replace(/^\s*(.*\S|.*)\s*$/, '$1');
}

// Verifica os campos de um login
function checkLogin(frm,win,clos)
{
	if(frm.pass.value != "" && frm.name.value != "") {
		frm.submit();
		if(clos)
			win.close();
		return true;
	} else {
		alert('Digite seu nome e senha.');
		return false;
	}
}

// Pede confirmação antes de limpar o formulário
function resetConfirm()
{
	return confirm("Isso irá restaurar todos os valores do formulário.\nTem certeza?");
}
// Pede confirmação antes de apagar registros
function deleteConfirm()
{
	return confirm("Deseja apagar os registros selecionados?");
}

// *** NÃO FUNCIONA AINDA
/*function markAll(bool mark)
{
	var valor = document.Formdel.valor_chk.value;
	if ((valor == "") || (valor == "false")) {
		document.Formdel.valor_chk.value = true;
	} else {
		document.Formdel.valor_chk.value = "";
	}
	valor = document.Formdel.valor_chk.value;
	for (var i = 0; i < document.Formdel.elements.length; i++) {
		var e = document.Formdel.elements[i];
		if ((e.name != "allbox") && (e.name != "acao") && (e.name != "novo_folder") && (e.name != "lixeira") && (e.name != "cria_folder"))
			e.checked = valor;
	}
}*/

//----------------------------------------------------------------------------
// Funções de uso geral
// Some adapted from free scripts available at http://javascript.internet.com
//----------------------------------------------------------------------------

function showProperties(obj, showValues)
{
	var result = "<html><body>";

	result += "<font size=4>Properties for object " + obj.name + ":<p></font>\n";
	result += "<table border='1'>\n\n";
	for(curProperty in obj) {
		result += "<tr>\n";
		result += "<td width='20%'>" + curProperty + "</td>";
		if(showValues)
			result += "<td width='80%'>" + eval("obj." + curProperty) + "</td>\n";
		result += "</tr>\n";
	}
	result += "\n\n</table></body></html>";
	prop = window.open("","","scrollbars=1, resizable, width=640, height=480");
	prop.document.write(result);
}

// Não testada
function showPopupWindow(msg, winWidth, winHeight)
{
	prop = window.open("","","scrollbars=1, resizable, width=" + winWidth + ", height=" + winHeight);
	prop.document.write(msg);
}

function max(a, b)
{
	return a > b ? a : b;
}

function min(a, b)
{
	return a < b ? a : b;
}

function h()
{
}

function hidden()
{
	document.body.style.overflow='hidden';
}

//----------------------------------------------------------------------------
// FIM
//----------------------------------------------------------------------------
