/**
 * @author Márcio d'Ávila
 * @version 1.0, 2004
 *
 * PROTÓTIPOS:
 * construtor CrossEvent(Event pEvent)
 * String inspectObject(Object pObj)
 * boolean doFocus(Object pInput, String pTexto)
 * boolean doBlur(Object pInput, String pTexto)
 */

c_HINT_TEXT = "Digite aqui";

/**
 * Construtor da classe CrossEvent.
 */
function CrossEvent(evt)
{
	evt = evt? evt: (window.event? window.event: null);
	if (evt)
	{
		this.originalEvent = evt;
		this.type = evt.type;
		this.screenX = evt.screenX;
		this.screenY = evt.screenY;

		// IE: srcElement
		this.target = evt.target? evt.target: evt.srcElement;

		// N4: modifiers
		if (evt.modifiers)
		{
			this.altKey   = evt.modifiers & Event.ALT_MASK;
			this.ctrlKey  = evt.modifiers & Event.CONTROL_MASK;
			this.shiftKey = evt.modifiers & Event.SHIFT_MASK;
			this.metaKey  = evt.modifiers & Event.META_MASK;
		}
		else
		{
			this.altKey   = evt.altKey;
			this.ctrlKey  = evt.ctrlKey;
			this.shiftKey = evt.shiftKey;
			this.metaKey  = evt.metaKey;
		}

		// N4: which // N6+: charCode
		this.charCode = !isNaN(evt.charCode)? evt.charCode: !isNaN(evt.keyCode)? evt.keyCode: evt.which;
		this.keyCode = !isNaN(evt.keyCode)? evt.keyCode: evt.which;
		this.button = !isNaN(evt.button)? evt.button: !isNaN(evt.which)? evt.which-1: null;
		this.debug = "c:" + evt.charCode + " k:" + evt.keyCode
			+ " b:" + evt.button + " w:" + evt.which;
	}
} // CrossEvent


function autoSkip(pObj, pKey, pLoop)
{
	// Processa apenas se o objeto fornecido é um INPUT TEXT
	if ( ! pObj.form || ! pObj.type || pObj.type != "text" )
	{
		return false;
	}

	if ( pObj.value.length == pObj.maxLength && (pKey > 47 || pKey == 32) )
	{
		var form = pObj.form;
		for(var i = 0; i < form.elements.length; ++i)
		{
			if (form.elements[i] == pObj)
				break;
		}
		var next = (i + 1) % form.elements.length;
		if (! pLoop && next == 0)
			return false;
		while (form.elements[next].disabled || form.elements[next].type == "hidden")
		{
			next = (next + 1) % form.elements.length;
			if (! pLoop && next == 0)
				return false;
		}
		form.elements[next].focus();
		return true;
	}
	return false;
} // autoSkip

/* Deve ser chamado no KeyUp */
function mudaCampo(pEvent, pThis, pLoop)
{
	var evt = new CrossEvent(pEvent);
	var obj = pThis;
	if(!obj)
		obj = evt.target;
	autoSkip(obj, evt.keyCode, pLoop);
	return true;
} // mudaCampo

function inspectObject(obj) {
	var result = "";
	var objName = obj.name? obj.name: "object";
	for (var i in obj) {
		result += objName + "." + i + " = " + obj[i] + "\n";
	}
	return result;
} // inspectObject

//----------------------------------------------------------------------//

function doFocus(pInput, pTexto)
{
	if (pInput.value == (pTexto != null? pTexto: c_HINT_TEXT))
		pInput.value = "";
	return true;
} // doSearchFocus


function doBlur(pInput, pTexto)
{
	if (pInput.value == "")
		pInput.value = (pTexto != null? pTexto: c_HINT_TEXT);
	return true;
} // doSearchBlur


/**
 * Monta um link de mail-to dinamicamente, para evitar spam.
 * Escreve o codigo HTML do link no documento.
 *
 * @return String
 *	Codigo HTML do link, que foi escrito no documento.
 * @param String pNome
 *	Nome da caixa postal de destino (username) do e-mail.
 * @param String pDomino
 *	Dominio da caixa postal de destino do e-mail.
 * @param String pText
 *	[Opcional] Texto de rotulo do link.
 *	Se nao for fornecido, é assumido o e-mail de destino.
 * @param String pAttr
 *	[Opcional] Atributos a serem adicionados a tag <A>.
 * @param String pQuery
 *	[Opcional] Query de pares parametro/valor a ser adicionada ao link,
 *	permitindo fornecer parametros como subject, cc, body.
 */
function linkMail(pNome, pDominio, pText, pAttr, pQuery)
{
	var destino = pNome + '@' + pDominio;
	var rotulo = (pText!=null && pText!="")? pText: destino;
	var link = '<a';
	if (pAttr!=null) link += ' ' + pAttr;
	link += ' href="m' + 'ailto:' +  destino
	if (pQuery!=null) link += '?' + pQuery;
	link += '">' + rotulo + '</a>';
	document.write(link);
	return link;
} // linkMail