//////////////////////////////////////////////////////////////////////////////////
//                           Biblioteca de Funções
//
//  Versao: 1.0
//  Autor: Rodrigo Silva (rodrigosilva@rodrigosilva.com)
//
//	Última Modificacao: 
//
//////////////////////////////////////////////////////////////////////////////////

// Sobrescreve a funcao document.getElementsByTagName, que no IE 5 não suporta "*"
function ie_getElementsByTagName(str) {
	if (str=="*")
		return document.all
	else
		return document.all.tags(str)
}
if (document.all) document.getElementsByTagName = ie_getElementsByTagName
 

function initPage()
{
	var i, oItem = document.getElementsByTagName("*");

	var bSetaFoco = true;
	var bg_notags = "submit,reset,image,button,checkbox,radio";	
	
	var oAux;
	
	for (i=0; i < oItem.length; i++ )
	{
		switch (oItem[i].tagName)
		{
			case "INPUT":
			
				if ( bg_notags.indexOf(oItem[i].type) == -1 ) init_backgroundColor(oItem[i]);
				
				if ( ((oItem[i].type=="radio")||(oItem[i].type=="checkbox")) && (oItem[i].parentNode.tagName == "LABEL") )
				{
					oItem[i].id = (oItem[i].id) ? oItem[i].id : "id_" + oItem[i].name + "_" + oItem[i].value;
					oItem[i].parentNode.htmlFor = oItem[i].id;
					oItem[i].parentNode.onclick = (oItem[i].type=="radio") ? attach_highlightRadio : attach_highlightCheckbox ;
					oItem[i].parentNode.style.fontWeight = (oItem[i].checked) ? "bold" : "normal" ;
				}
				
				if (bSetaFoco) bSetaFoco = init_focus(oItem[i]);
				
			break;
			
			case "TEXTAREA":
				
				init_backgroundColor(oItem[i]);
				
				if ( oItem[i].getAttribute("maxlength") != null )
				{
					oItem[i].onkeyup   = attach_textLimit;
					oItem[i].onkeydown = attach_textLimit;
					oItem[i].onchange  = attach_textLimit;
					oItem[i].onkeyup();
					
					oAux = document.getElementById( oItem[i].name + "_maxlength" );
					if (oAux)
					{
						oAux.innerHTML = oItem[i].getAttribute("maxlength");
						oAux.value     = oItem[i].getAttribute("maxlength");
					}
				}
				
				if (bSetaFoco) bSetaFoco = init_focus(oItem[i]);
				
			break;
			
			case "SELECT":
			
				init_backgroundColor(oItem[i]);
				
				if (bSetaFoco) bSetaFoco = init_focus(oItem[i]);
				
			break;
			
			case "FIELDSET":
				if ( oItem[i].parentNode.tagName == "TD" )
				{
					oItem[i].style.height = oItem[i].parentNode.clientHeight-23 + "px";
				}
			break;

		}
	}
}
function init_focus(oItem)
{
	if ( oItem.getAttribute("foco")!=null )
	{
		try { oItem.onfocus() ; oItem.focus() ; oItem.select() ; return false; }
		catch (erro){ return true; }
	}
	else
		return true;
}
function init_backgroundColor(oItem)
{
	oItem.onfocus = function() { this.style.backgroundColor = "#FFFFE0"; };
	oItem.onblur  = function() { this.style.backgroundColor = "white"; };	
}


function attach_textLimit()
{
	var maxLength = parseInt( this.getAttribute("maxlength") );
	
	if ( this.value.length > maxLength )
		this.value = this.value.substring(0,maxLength);
	
	var oSpan;

	oSpan = document.getElementById( this.name + "_counter" );
	if (oSpan)
	{
		oSpan.innerHTML = this.value.length;
		oSpan.value     = this.value.length;
	}
	
	oSpan = document.getElementById( this.name + "_remaining" );
	if (oSpan)
	{
		oSpan.innerHTML = maxLength - this.value.length;
		oSpan.value     = maxLength - this.value.length;
	}
}
function attach_highlightRadio()
{
	var oRadio = document.getElementById(this.htmlFor);
	var radios = oRadio.form.elements[oRadio.name];
	for (var i=0 ; i < radios.length ; i++)
	{
		if ( radios[i].parentNode.tagName == "LABEL" )
			radios[i].parentNode.style.fontWeight = "normal";
	}
	this.style.fontWeight = "bold";
}

function attach_highlightCheckbox()
{
	this.style.fontWeight = (document.getElementById(this.htmlFor).checked) ? "bold" : "normal" ;
}


function alternaExibicao(id){
	var obj = document.getElementById(id);
	if (obj.style.display=="none")
		obj.style.display="";
	else
		obj.style.display="none";
}

function setaFoco()
{
	var i, item;
	item = document.getElementsByTagName("*");
	
	for (i=0; i < item.length; i++ )
	{
		if (item[i].getAttribute("foco")!=null)
		{
			try { item[i].onfocus() ; item[i].focus() ; item[i].select() ; break; }
			catch (erro){}
		}
	}
}