// JavaScript Document
// Funcoes JavaScript mais comuns

// Mostra uma mensagem para o usuario e seta o focus parar o componente utilizado
// 		showMessage(component, message)

// Auxilia no preenchimento de uma data numa caixa de texto
// 		dateAdjust(component)

// Auxilia no preenchimento de um numero de telefone
//		phoneAdjust(component)

// Verifica se um caracter é um digito
//		isDigit(character)

// Verifica se um componente esta vazio
// 		isEmptyComponent(component)

// Verifica se uma string esta fazia ou nula
//		isEmpty(string)

// Somente permite o usuario entrar com numeros num campo
//	numberAdjust(component)

// Verifica se uma string é um inteiro
//		isInteger(string)

// Mostra uma janela popup
// 		popup(url, width, height) 

// Verifica se eh um endereco de email valido
//		function isEmail(email)

function currencyAdjust(fld, milSep, decSep, e)
{
	var sep = 0;
	var key = '';
	var i = j = 0;
	var len = len2 = 0;
	var strCheck = '0123456789';
	var aux = aux2 = '';
	var whichCode = (window.Event) ? e.which : e.keyCode;
	if (whichCode == 13) return true;  // Enter
	key = String.fromCharCode(whichCode);  // Get key value from key code
	if (strCheck.indexOf(key) == -1) return false;  // Not a valid key
	len = fld.value.length;
	for(i = 0; i < len; i++)
	if ((fld.value.charAt(i) != '0') && (fld.value.charAt(i) != decSep)) break;
	aux = '';
	for(; i < len; i++)
	if (strCheck.indexOf(fld.value.charAt(i))!=-1) aux += fld.value.charAt(i);
	aux += key;
	len = aux.length;
	if (len == 0) fld.value = '';
	if (len == 1) fld.value = '0'+ decSep + '0' + aux;
	if (len == 2) fld.value = '0'+ decSep + aux;
	if (len > 2) {
	aux2 = '';
	for (j = 0, i = len - 3; i >= 0; i--) {
	if (j == 3) {
	aux2 += milSep;
	j = 0;
	}
	aux2 += aux.charAt(i);
	j++;
	}
	fld.value = '';
	len2 = aux2.length;

	for (i = len2 - 1; i >= 0; i--)
	fld.value += aux2.charAt(i);
	fld.value += decSep + aux.substr(len - 2, len);
	}
	return false;
}

function showMessage(component, message) {
	alert(message);
	component.focus();
	component.select();
    return false;
}

function numberAdjust(component) {
	if ( (event.keyCode<48) || (event.keyCode>57) )
		event.returnValue = false; 
}

function dateAdjust(component) {
	if ( (event.keyCode<48) || (event.keyCode>57) ) {
		event.returnValue = false; 
	} else { 
		if ((component.value.length==2)||(component.value.length==5))
			component.value=component.value + "/" ;
	}
}

function phoneAdjust(component) {
	if( (event.keyCode<48) || (event.keyCode>57) ) {
		event.returnValue = false;
	} else { 
		if(component.value.length==0) {
			component.value = component.value + "(";
		} else if(component.value.length==3) {
			component.value = component.value + ") ";
		}
	}
}

function isDigit(character) {
	return ((character >= "0") && (character <= "9"));
}

function isEmptyComponent(component) {
	return isEmpty(component.value);
}

function isEmpty(string){
    return ((string == null) || (string.length == 0));
}

function isInteger(string) {
	var i;
	if (isEmpty(string))
		if (isInteger.arguments.length == 1)
			return false;     
    for (i = 0; i < string.length; i++) {
		var c = string.charAt(i);
		if (!isDigit(c))
			return false;
    }
    return true;
}

function popup(url, width, height) {
	newWindow = window.open(url, "popup", "width=" + width + ", height=" + height + ", scrollbars");
	newWindow.focus();
}

function isEmail(email) {
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email))
    	return true;
	else
    return false;
}


// Mostra e esconde um layer
function MM_findObj(n, d) { //v4.0
  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=MM_findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function MM_showHideLayers() { //v3.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v='hide')?'hidden':v; }
    obj.visibility=v; }
}