
function getScrollY () 
{
	var scrollY = 0;    
	if (typeof window.pageYOffset == "number") this.scrollY = window.pageYOffset;
	else if (document.documentElement && document.documentElement.scrollTop)
		scrollY = document.documentElement.scrollTop;
	else if (document.body && document.body.scrollTop) 
		scrollY = document.body.scrollTop; 
	else if (window.scrollY) scrollY = window.scrollY;
	return scrollY;
}

function FormataValor (valorCampo) {
	var valorFim = new Number(valorCampo);
	if (valorFim >=0 )
		return FormataReaisString(valorFim.toFixed(2),'.',',');
	else
		return "-" + FormataReaisString(valorFim.toFixed(2),'.',',');
}

function PegaPosY() 
{
	var objPosY = MM_findObj("posY");
	if (objPosY != null){
		objPosY.value = getScrollY();
	}
}

function ValorCampo (nomeCampo) {
	var obj = MM_findObj(nomeCampo);
	var retorno = 0;
	if (obj != null){
		var valorCampo = obj.value.replace(/\./g,'');
		var valor = new Number(valorCampo.replace(",","."));
		if (isNaN(valor)){
			obj.value = 0;
		}
	    if (obj.value != ""){
	        retorno = new Number(valorCampo.replace(",","."));
	    }
	}
	
	return retorno;
}

function confirma () {
  if (confirm('Confirma a exclusão?')) {
    return true;
  }else{
    return false;
  }
}

function subMenu(id, nivel){
  if (document.getElementById(id).style.display == 'none') {
	document.getElementById(id).style.display = 'inline';
	document.getElementById("img"+id).src = nivel + '../_imagens/estruturaPrincipal/icoMenuMenos.gif';
  } else {
	document.getElementById(id).style.display = 'none';
	document.getElementById("img"+id).src = nivel + '../_imagens/estruturaPrincipal/icoMenuMais.gif';
  }
}

function over(id){
  document.getElementById(id).className = 'mouseOver';
}

function out(id){
  document.getElementById(id).className = 'mouseOut';
}


function filtroLayout(combo){
  document.location.href=combo[combo.selectedIndex].value;
}


function abrir() {

	var windowFeatures = "", nomeArquivo = "", nomeJanela = "", erro = null
	Argumentos = abrir.arguments; noArgumentos = Argumentos.length; nomeArquivo = Argumentos[0]
	for (i = 1; i < noArgumentos; i++) {
		valor = Argumentos[i].substring(2,Argumentos[i].length)
		switch(Argumentos[i].substring(0,2)) {
			case "nj" : nomeJanela = valor; break
			case "to" : windowFeatures += "top=" + valor + ", "; break
			case "le" : windowFeatures += "left=" + valor + ", "; break
			case "he" : windowFeatures += "height=" + valor + ", "; break
			case "wi" : windowFeatures += "width=" + valor + ", "; break
			case "lb" : windowFeatures += "location=" + valor + ", "; break
			case "mb" : windowFeatures += "menubar=" + valor + ", "; break
			case "sc" : windowFeatures += "scrollbars=" + valor + ", "; break
			case "st" : windowFeatures += "status=" + valor + ", "; break
			case "tb" : windowFeatures += "toolbar=" + valor + ", "; break
			case "tt" : windowFeatures += "titlebar=" + valor + ", "; break
			case "re" : windowFeatures += "resizable=" + valor + ", "; break
			default : erro = '"Código de atributo não informado no '+(i+1)+' º argumento (' +Argumentos[i]+ ')"'
		}
	}
	windowFeatures = windowFeatures.substring(0,windowFeatures.lastIndexOf(","))
	if (erro) { alert(erro) } else { novaJanela = window.open(nomeArquivo, nomeJanela, windowFeatures) }
}

function MM_findObj(n, d) { //v4.01
  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 && d.getElementById) x=d.getElementById(n); return x;
}

function MM_setTextOfLayer(objName,x,newText) { //v4.01
  if ((obj=MM_findObj(objName))!=null) with (obj)
    if (document.layers) {document.write(unescape(newText)); document.close();}
    else innerHTML = unescape(newText);
}

function setData(Objeto,teclapres) {
  var tecla = teclapres.keyCode;
  var vr = Objeto.value;  
  vr = vr.replace( "/", "" );  
  vr = vr.replace( "/", "" );
  vr = vr.replace( "/", "" );
  var tam = vr.length;

  var str = '';

  if( tecla != 9 && tecla != 8 && tecla != 37 && tecla != 39 && tecla != 46 ) {
    if( tam == 1 ) {
      str = vr.substr( 0, 1 );
    }
    if( tam == 2 ) { 
      str = vr.substr( 0, 2 ) + '/';
    }
    if( tam == 3 ) { 
      str = vr.substr( 0, 2 ) + '/' + vr.substr( 2, 1 );
    }
    if( tam == 4 ) {      
      str = vr.substr( 0, 2 ) + '/' + vr.substr( 2, 2 ) + '/';
    } 
    if( tam > 4 ) {                
      str = vr.substr( 0, 2 ) + '/' + vr.substr( 2, 2 ) + '/' + vr.substr( 4, tam - 4 );
    }
    Objeto.value = str.substr( 0, 10 );
  }
}



function FormataReais(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; 
	
	key = String.fromCharCode(whichCode);// Valor para o código da Chave 
	
	if (strCheck.indexOf(key) == -1) return false; // Chave inválida 
	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 FormataReaisString(valor, milSep, decSep) 
{ 
	valor = String(valor);
	var sep = 0; 
	var i = j = 0; 
	var len = len2 = 0; 
	var strCheck = '0123456789'; 
	var aux = aux2 = ''; 

	len = valor.length; 
	for(i = 0; i < len; i++) 
		if ((valor.charAt(i) != '0') && (valor.charAt(i) != decSep)) break; 
	
	aux = ''; 
	for(; i < len; i++) 
		if (strCheck.indexOf(valor.charAt(i))!=-1) aux += valor.charAt(i); 
	
	len = aux.length; 
	if (len == 0) valor = ''; 
	if (len == 1) valor = '0'+ decSep + '0' + aux; 
	if (len == 2) valor = '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++; 
		} 
		valor = ''; 
		len2 = aux2.length; 
		for (i = len2 - 1; i >= 0; i--) 
			valor += aux2.charAt(i); 
		valor += decSep + aux.substr(len - 2, len); 
	} 
	return valor; 
} 


var divName = 'lAtualizando';
var offX = -8;
var offY = -8;

function mouseX(evt) {if (!evt) evt = window.event; if (evt.pageX) return evt.pageX; else if (evt.clientX)return evt.clientX + (document.documentElement.scrollLeft ?  document.documentElement.scrollLeft : document.body.scrollLeft); else return 0;}
function mouseY(evt) {if (!evt) evt = window.event; if (evt.pageY) return evt.pageY; else if (evt.clientY)return evt.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop); else return 0;}

function ExecutaAnimacao(evt) {
	$get('lAtualizando').visibility = 'visible';
	$get('lAtualizando').style.left = (parseInt(mouseX(evt))+offX) + 'px';
	$get('lAtualizando').style.top = (parseInt(mouseY(evt))+offY) + 'px';
	$get('lAtualizando').style.display='block';
}
	
function AtivaAnimacao() {
	document.onmousemove = ExecutaAnimacao;
}

function DesativaAnimacao() {
	document.onmousemove = null;
	$get('lAtualizando').style.display='none';
}

var arObjCalendarios = null;
function itemCalendario (IdCampo, IdBotao)
{
	this.ppIdCampo = IdCampo;
	this.ppIdBotao = IdBotao;
}



//Specify affected tags. Add or remove from list:
var tgs = new Array( 'div','td','tr','a');

//Specify spectrum of different font sizes:
var szs = new Array( 'xx-small','x-small','small','medium','large','x-large','xx-large' );
var startSz = 2;

function ts( trgt,inc ) {
	if (!document.getElementById) return
	var d = document,cEl = null,sz = startSz,i,j,cTags;
	
	sz += inc;
	if ( sz < 0 ) sz = 0;
	if ( sz > 6 ) sz = 6;
	startSz = sz;
		
	if ( !( cEl = d.getElementById( trgt ) ) ) cEl = d.getElementsByTagName( trgt )[ 0 ];

	cEl.style.fontSize = szs[ sz ];

	for ( i = 0 ; i < tgs.length ; i++ ) {
		cTags = cEl.getElementsByTagName( tgs[ i ] );
		for ( j = 0 ; j < cTags.length ; j++ ) cTags[ j ].style.fontSize = szs[ sz ];
	}
}

function simulaClick(elId) { 
	var evt; 
	var el = document.getElementById(elId); 
	if (document.createEvent){ 
		evt = document.createEvent("MouseEvents"); 
		evt.initMouseEvent("click", true, true, window, 
		0, 0, 0, 0, 0, false, false, false, false, 0, null); 
	} 
	(evt)? el.dispatchEvent(evt):(el.click && el.click()); 
} 


var msgSelecao = "";
function SetaMensagem(elm)
{
	var elmref;
	if (msgSelecao != "")
	{
		elmref = eval("document.getElementById('" + msgSelecao + "_h1')");
		if (elmref) elmref.style.display = 'none';
		elmref = eval("document.getElementById('" + msgSelecao + "_h0')");
		if (elmref) elmref.bgColor = '#edfbfc';
	}
	if (msgSelecao != elm.name) 
	{
		msgSelecao = elm.name;
		elmref = eval("document.getElementById('" + msgSelecao + "_h1')");
		if (elmref) 
		{
			if (elmref.style.display=='none') elmref.style.display='';
			else elmref.style.display = 'none';
		}
		elmref = eval("document.getElementById('" + msgSelecao + "_h0')");
		if (elmref) 
			elmref.bgColor = '#C1F0F4';
	}
	else
		msgSelecao="";
		
	//window.location = "#xx" + msgSelecao + "xx";
	return false;
}


function ConsultaSEDEX(sObjeto){
  var sURL;
  sURL='http://websro.correios.com.br/sro_bin/txect01$.QueryList?P_LINGUA=001&P_TIPO=001&P_COD_UNI='+sObjeto;
  var popup = window.open(sURL,'_blank','top=20,left=20,height=400,width=540,resizable=yes,status=no,scrollbars=yes');
  popup.focus();
}


// Valida se o valor passado é numérico
function isNumber ( pStrExp ){
	var bolReturn = false;
	if ( trim( pStrExp ) != '' ){ // se foi passado algum valor
		var oRegNumber = /\D/;
		bolReturn = !oRegNumber.test( pStrExp );
	}
	return bolReturn;
}

// Remove os espaços no começo e fim da string passada
// AMartins : 09/01/2006
// Returns String
function trim( pStrExp )
{
   return pStrExp.replace(/^\s*|\s*$/g, '');
}

// Valida se o email passado está correto
// AMartins : 09/01/2006
// Returns Boolean
function isEmail ( pStrExp ){
	var bolReturn = false;
	if ( trim( pStrExp ) != '' ){ // se foi passado algum valor
		var oRegEmail = /^[a-z0-9\._\-]+\@[a-z0-9\._\-]+\.[a-z]{2,3}$/i;
		bolReturn = oRegEmail.test( pStrExp );
	}
	return bolReturn;
}


//Permite a digitação dos caracteres solicitados
function AllowedKeys(strChars){

	var lngKey;			// Código ASC da tecla digitada
	var strValor;
	
	lngKey = window.event.keyCode;
	strValor = String.fromCharCode(lngKey);
		
	if ( (strChars.indexOf(strValor) < 0 )&& (lngKey != 9)){
		window.event.keyCode = 0;
		return false;
	}
	
	return true;	
}

//Permite a digitação de números
function NumbersOnly(myfield, e, dec)
{
	var key;
	var keychar;

	if (window.event)
		key = window.event.keyCode;
	else if (e)
		key = e.which;
	else
		return true;
	
	keychar = String.fromCharCode(key);

	// control keys
	if ((key==null) || (key==0) || (key==8) || (key==9) || (key==27) )
		return true;

	// numbers
	else if ((("0123456789").indexOf(keychar) > -1))
		return true;

	// decimal point jump
	else if (dec == true && (keychar == ","))
	{
		return true;
	}
	else
		return false;
}



//Gera um click no objeto passado em no caso de "Enter" Pressionado
//Esta funçõa deve ser chamada no OnKeyDown do objeto
function ClickOn(element, e, objName) 
{
	var key;
	
	if (window.event)
		key = window.event.keyCode;
	else if (e)
		key = e.which;
	else
		key = 0;

	// Se for "Enter"
	if (key==13)
	{
		var Image = document.getElementById( objName );
		Image.click();
		return false;
	}
	else
	{
		return true;
	}
}

//Não gera click para os objetos passados
//Esta funçõa deve ser chamada no OnKeyDown do objeto
function ClickOff(element, e) 
{
	var key;
	
	if (window.event)
		key = window.event.keyCode;
	else if (e)
		key = e.which;
	else
		key = 0;

	// Se for "Enter"
	if (key==13)
	{
		return false;
	}
	else
	{
		return true;
	}
}
