/**
 *	Desabilita tecla Enter
 */
disableEnterKey = false;
function kH(e) {
	var pK = e ? e.which : window.event.keyCode;
	return pK != 13;
}
if (disableEnterKey) {
	document.onkeypress = kH;
	if (document.layers) document.captureEvents(Event.KEYPRESS);
}

/**
 *	Desabilita menu / botao direito
 */
disableContextMenu = false;
function click(e) {
	if (!e) { e = event; }
	if (e.button==2||e.button==3) {
		oncontextmenu='return false';
	}
}
if (disableContextMenu) {
	document.onmousedown=click;
	document.oncontextmenu = new Function("return false;");
}

/*
 *	Strip whitespace from the beginning and end of a string
 *	Input : a string
 */
function trim(str)
{
	return str.replace(/^\s+|\s+$/g,'');
}

function isInStr(pattern, string) {
	var _m = pattern.toLowerCase(); // pattern to match.
	var _s = string.toLowerCase();
	var _c = 0; // count
	for (var i=0;i<_s.length;i++) {
		if (_m == _s.substr(i,_m.length))
		 _c++;
	}
	if (_c > 0) {
		return true;
	} else {
		return false;
	}
}

/*
 *	Make sure that textBox only contain number (accepts dots)
 */
function checkNumber(textBox)
{
	while (textBox.value.length > 0 && isNaN(textBox.value)) {
		textBox.value = textBox.value.substring(0, textBox.value.length - 1)
	}
	
	textBox.value = trim(textBox.value);
}

function checkNumberOnly(textBox)
{
	var i;
	for (i = 0; i < textBox.value.length; i++) {
		if (textBox.value.charAt(i) == '.') {
			textBox.value = textBox.value.substring(0, i);
		}
	}
	while (textBox.value.length > 0 && isNaN(textBox.value)) {
		textBox.value = textBox.value.substring(0, textBox.value.length - 1)
	}
	
	textBox.value = trim(textBox.value);
}

function setMask(strField, sMask, evtKeyPress) {
	var i, nCount, sValue, fldLen, mskLen,bolMask, sCod, nTecla;
	
	if(document.all) { // Internet Explorer
		nTecla = evtKeyPress.keyCode; }
	else if(document.layers) { // Nestcape
		nTecla = evtKeyPress.which;
	}
	
	sValue = strField.value;
	
	// Limpa todos os caracteres de formatação que
	// já estiverem no campo.
	sValue = sValue.toString().replace( "-", "" );
	sValue = sValue.toString().replace( "-", "" );
	sValue = sValue.toString().replace( ".", "" );
	sValue = sValue.toString().replace( ".", "" );
	sValue = sValue.toString().replace( "/", "" );
	sValue = sValue.toString().replace( "/", "" );
	sValue = sValue.toString().replace( "(", "" );
	sValue = sValue.toString().replace( "(", "" );
	sValue = sValue.toString().replace( ")", "" );
	sValue = sValue.toString().replace( ")", "" );
	sValue = sValue.toString().replace( " ", "" );
	sValue = sValue.toString().replace( " ", "" );
	fldLen = sValue.length;
	mskLen = sMask.length;
	
	i = 0;
	nCount = 0;
	sCod = "";
	mskLen = fldLen;
	
	while (i <= mskLen) {
		bolMask = ((sMask.charAt(i) == "-") || (sMask.charAt(i) == ".") || (sMask.charAt(i) == "/"))
		bolMask = bolMask || ((sMask.charAt(i) == "(") || (sMask.charAt(i) == ")") || (sMask.charAt(i) == " "))
		
		if (bolMask) {
			sCod += sMask.charAt(i);
			mskLen++; }
		else {
			sCod += sValue.charAt(nCount);
			nCount++;
		}
		
		i++;
	}
	
	strField.value = sCod;
	
	if (nTecla != 8) { // backspace
		if (sMask.charAt(i-1) == "9") { // apenas números...
			return ((nTecla > 47) && (nTecla < 58)); } // números de 0 a 9
		else { // qualquer caracter...
			return true;
		}
	} else {
		return true;
	}
}

function popUp(arquivo, nome, attr) {
//	attr = "toolbars=no,status=no,resize=no,scrollbars=no,width=633,height=540,top=50,left=100";
	
	if (!arquivo) { arquivo = ''; }
	
	if (!nome) { nome = ''; }
	
	if (!attr) { attr = ''; }
	
	return window.open(arquivo,nome,attr);
}

/**
 *	Deixa so' os digitos no numero
 */
function limpa_string(S){
	var Digitos = "0123456789";
	var temp = "";
	var digito = "";
	for (var i=0; i<S.length; i++) {
	  digito = S.charAt(i);
	  if (Digitos.indexOf(digito)>=0){temp=temp+digito}
	}
	return temp
}

function checkFrmLogin(form)
{
	fields = new Array( new Array('txtUserName', 		'user', 	'Login'),
						new Array('txtPassword',		'pass', 	'Senha')
						);
	
	result = validateForm(fields, form);
	
	if (result == '') {
		return true;
	} else {
		alert(result);
		return false;
	}
}

function appendScript(url) {
	var head = document.getElementsByTagName('head').item(0);
	var old  = document.getElementById('lastLoadedCmds');
	if (old) head.removeChild(old);

	script = document.createElement('script');
	script.src = url;
	script.type = 'text/javascript';
	script.defer = true;
	script.id = 'lastLoadedCmds';
	void(head.appendChild(script));
}