/// CLI
///
/// Implementa funcionalidades básicas e esbate diferenças entre "browsers".
///
var W = window, D = document, proto, NULLF = function(){}, UNDEF, isGecko, isIE, x, _mround_ = Math.round, _dfrMods_ = [], SSRC = "", SROOT = SSRC + "/SAPIENS/", SCROOT = SROOT + "comps/";
/*@cc_on @*//*@if(1)isIE=-1;@end @*/
if (!W.dbg) W.dbg = {	assert : NULLF	};
refer (SROOT + "compat/" + (isIE ? "ie" : "w3c") + ".js");
D.write ('<style type="text/css"><!-- .js{display:block}.jsi{display:inline;display:list-item}.nojs{display:none} --></style>');


// Retorna o elemento ID.
function $(id) {	return D.getElementById (id)	}

// Elimina EL.
function rm$(el) {	return el.parentNode.removeChild (el)	}

// Retorna OBJ se for do tipo TAG-NAME, ou procura na sua hierarquia um elemento desse tipo.
function up2 (tagName, obj, optN) {
	var n = optN || 1;
	do {	if (obj.tagName == tagName && !--n) return obj;	} while (obj = obj.parentNode);
}

// Tenta focar C e 'false' se não conseguir.
function setFocus (c) {	if (c) try {	c.focus (); c.select && c.select (); return 1	} catch (e) {}	}

// Regista os "scripts" em uri-LIST (de forma assíncrona).
function use (urilist) {
	var i, args  = arguments, l = args.length, uri;
	// Processar ficheiro-a-ficheiro.
	for (i = 0; i < l;) {	uri = args [i++]; refer (mod2uri (uri) || uri)	}
}
function refer (uri, optDefer) {
	var head, b, f;
	if (b = D.body) {
		head = b.previousSibling;
		W.refer = f = function (uri) {
			f = D.createElement ("SCRIPT");
			f.type = "text/javascript";
			f.src = uri;
			head.appendChild (f)
		};

		f (uri)
	}
	else D.write ('<script type="text/javascript" src="' + uri + '' + (optDefer ? '" defer="defer' : "") + '"></script>')
}

// Executa o método F de O quando todos os MOD-NM estiverem carregados.
function defer (optO, f, modnm	/*, ..., param1, ... */) {
	var args = arguments, l = args.length, i, a, ai = 2, lf = f, lo = optO, uri, u = 0, o, params = [];
	if (lo.call || lo.slice)  {	lf = lo; lo = W; ai = 1	}
	// Carregar os módulos necessários.
	for (i = ai; i < l; ++i) {
		a = args [i];
		// Detectar e extrair parâmetros.
		if (!a.slice) {
			o = l;
			for (l = i; i < o;) params.push (args [i++]);
			break
		}
		// Determinar o URI.
		uri = mod2uri (a);
		if (!uri) {
			// Módulo com caminho próprio: este parâmetro é o nome e o próximo é que o caminho.
			a = a.slice (1);
			args [i] = 0;
			uri = args [++i]
		}
		// Referenciar módulos.
		o = _dfrMods_ [a];
		if (o != 1) {	_dfrMods_ [a] = {	F : F, NO : o	}; ++u	}
		else uri = 0;
		args [i] = uri
	}
	// Carregar módulos.
	if (u) {
		for (i = ai; i < l;) (uri = args [i++]) && refer (uri);
		args = 0
	}
	else {	u = 1; F ()	}
	return false;

	function F () {
		if (!--u) {
			if (lf.slice) {
				lf = eval (lf);
				if (!lf.call) return
			}
			lf.apply (lo, params)
		}
	}
}

// Anuncia que o módulo MODNM foi carregado.
// É possível declarar as dependências e o último parâmetro pode indicar que há código só para o IE.
function Im (modnm	/*	dep1, ..., optIEmod	*/) {
	var args = arguments, u = args.length - 1, a, o, IEmod = 0, i;
	// Verificar se há módulo IE.
	if (u && !isNaN (o = args [u] - 0)) {	if (isIE) IEmod = o; --u	}
	// Tratar dependências.
	if (u) {
		a = new Array (u + 1);
		a [0] = function () {	Im (modnm, IEmod)	};
		for (i = 1; i <= u; ++i) a [i] = args [i];
		defer.apply (W, a);
		return args = a = 0
	}
	// Possibilitar o tratamento específico do IE.
	if (!IEmod || IEfix (modnm, IEmod)) {
		// Despachar código em espera da disponibilidade deste módulo.
		a = _dfrMods_;
		o = a [modnm];
		a [modnm] = 1;
		if (o != 1) while (o) {	o.F (); o = o.NO	}
	}
}

// Retorna o URI para MOD-NM.
function mod2uri (modnm) {
	var ch = modnm.charAt (0), uri, s = ".js";
	if (ch == ch.toUpperCase ()) {
		if (ch != ch.toLowerCase ()) uri = SCROOT + modnm + "/cli.js";
		else {
			switch (ch) {
				case "*": return;
				case "/": return SSRC + modnm + s;
				case ":": uri = SCROOT; break;
				case ".": uri = SROOT
			}
			if (uri) uri += modnm.slice (1) + s
		}
	}
	return uri || (SROOT + modnm + s)
}

// Retorna o campo posterior a EL.
function getNextFld (el) {
	var dh = "hidden", e, ch, st, db = D.body;
	return down_(el);


	// Procura para cima e para o lado.
	function up_(el) {
		for (;;)
			if (e = el.nextSibling) {
				if (isAcc_(el = e)) return sel_(e)
			} else {
				el = el.parentNode;
				if (el == db || !el) break
			}
	}

	// Procura para baixo e para o lado.
	// NOTA: ignora as opções dos <SELECT>s.
	function down_(el) {
		e = el.firstChild;
		return (e && !el.options && isAcc_(e)) ? sel_(e) : up_(e || el)
	}

	// Selecciona EL ou um dos seus dependentes.
	function sel_(el) {
		ch = el.tagName.charAt (0);
		return el.form != UNDEF
				&& (ch != "I" || el.type != dh)
				&& "IBTS".indexOf (ch) >= 0
				&& !el.disabled
			? el : down_(el)
 	}

	// Indica se EL ou um dos seus dependentes podem ser seleccionados.
	function isAcc_(el) {	return el.nodeType == 1 && (st = W.getComputedStyle (el, null)).display != "none" && st.visibility != dh	}
}
///
///
/// STRING
///
proto = String.prototype;
// Retorna N repetições.
proto.rep = function (n) {	var res = ""; while (n-- > 0) res += this; return res	};
// 'Trim' e companhia. O Firefox suporta 'trim' nativamente.
if (!proto.trim) {
	proto.trimLeft = function () {	return this.replace (/^[\s\xA0]+/, "")	};
	proto.trimRight = function () {	return this.replace (/[\s\xA0]+$/, "")	};
	proto.trim = function () {	return this.trimLeft ().trimRight ()	}
}
///
///
/// MATH
///
// Arredonda segundo a precisão solicitada.
Math.round = function (n, optPrecis) {
	if (!optPrecis) return _mround_ (n);
	x = Math.pow (10, optPrecis);
	return _mround_ (n * x) / x
}
