// $Header: /cidadebcp/cm/cidadebcp/site/js/std_scripts.js 4     28-09-10 13:46 Joliveira $

<!-- 
// javascript:abreJanela('url','nome','w','h','l','t','scroll')
// url - path para pagina a abrir ; nome - nome atribuido a janela (evita que uma segunda janela abra na já existente)
// w - largura da janela  ; h - altura da janela ; l - distância em px da esq. do ecran ; t - dist. em px do topo do ecran ; scroll (valor 1 = janela com scroll, valor 0 = janela sem scroll
//se não existem l e t tomam o valor (0,0)

function abreJanela(url,nome,w,h,l,t,scroll) {
  popupJan = window.open(url, nome, 'menubar=0,toolbar=0,location=0,directories=0,status=1,scrollbars='+scroll+',resizable=0,width='+w+',height='+h+',left='+l+',top='+t+'')
}
// **** Abrir Janela centrada ****
// javascript:abreJanelaCentro('url','nome','w','h','scroll')
function abreJanelaCentro(url,nome,w,h,scroll) {
x = (window.screen.width/2) - ((w/2) + 10); //metade da largura do ecran menos metade da largura da janela (mais 5 pixeis borders)
y = (window.screen.height/2) - ((h/2) + 50); //metade da altura do ecran menos metade da altura da nova janela (mais title e a  status bar)
  popupJan = window.open(url, nome, 'menubar=0,toolbar=0,location=0,directories=0,status=1,scrollbars='+scroll+',resizable=0,width='+w+',height='+h+',left='+x+',top='+y+'')
}
// **** Abrir Janela Direita ****
// javascript:abreJanelaDir('url','nome','w','h','t','scroll')
function abreJanelaDir(url,nome,w,h,t,scroll) {
x = (window.screen.width) - (+w+ + 10); // largura do ecran menos largura da janela (mais 5 pixeis borders)
  popupJan = window.open(url, nome, 'menubar=0,toolbar=0,location=0,directories=0,status=1,scrollbars='+scroll+',resizable=0,width='+w+',height='+h+',left='+x+',top='+t+'')
}

function abreJanelaDirRes(url,nome,w,h,t,scroll) {
x = (window.screen.width) - (+w+ + 10); // largura do ecran menos largura da janela (mais 5 pixeis borders)
  popupJan = window.open(url, nome, 'menubar=0,toolbar=0,location=0,directories=0,status=1,scrollbars='+scroll+',resizable=1,width='+w+',height='+h+',left='+x+',top='+t+'')
}

function abrirSimulador(keSimulador){
	if (keSimulador == '3399')
		archivo = '/Simul/CreditoHabitacao/CreditoHabitacao.html';
	else if (keSimulador == '40140')
		archivo = '/Simul/ApoioProcessoinvest/ApoioProcInvestimentoInput.html';
	else if (keSimulador == '40150')
		archivo = '/Simul/AlocadorCarteiras/AlocadorCarteirasInput.jhtml';
	else if (keSimulador == '40160')
		archivo = '/Simul/ConstPortfolios/ConstrutorPortfolios.html';
	else if (keSimulador == '40170')
		archivo = '/Simul/SimRendimentos/SimRend.jhtml';
	else if (keSimulador == '40180')
		archivo = '/Simul/SimFut/SimFut.jhtml';
	else if (keSimulador == '40190')
		archivo = '/Simul/bolsa/ValTeorico_accao_aumento_capital.html';
	else if (keSimulador == '209905')
		archivo = '/Simul/SimRendimentos/SimRend.jhtml';
	else if (keSimulador == '209910')
		archivo = '/Simul/SimFut/SimFut.jhtml';
	else if (keSimulador == '609925')
		archivo = '/Simul/ContribuicaoAutarquica/ContribuicaoAutarquica.jhtml';
	else if (keSimulador == '609930')
		archivo = '/Simul/Sisa/Sisa.html';
	else if (keSimulador == '609935')
		archivo = '/Simul/ImpostoSucessoes/ImpostoSucessoes.html';
	else if (keSimulador == '609940')
		archivo = '/Simul/ImpostoAutomovel/ImpostoAutomovel.jhtml';
	else if (keSimulador == '609950')
		archivo = '/Simul/IvaContido/IvaContido.html';
	else if (keSimulador == '609955')
		archivo = '/Simul/IvaReceitasDespesas/IvaReceitasDespesas.jhtml';
	else if (keSimulador == '609960')
		archivo = '/Simul/IvaReceitasDespesas/IvaReceitasDespesas2.jhtml';
	else if (keSimulador == '609965')
		archivo = '/Simul/IvaReceitasDespesas/IvaReceitasDespesas2.jhtml';
	else if (keSimulador == '609970')
		archivo = '/Simul/CreditoPessoal/0301_1.jhtml';

	window.open(archivo, 'Simulador', 'width=620,height=440,status=yes,menubar=no,scrollbars=yes,resizable=yes,location=no,toolbar=no,directories=no');
}

// necessário para vários scripts 
NS4 = (document.layers);
IE4 = (document.all);
NS6 = (document.getElementById);

// -->

var valid = '0123456789 abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-';

function ValidaCaracteres(string,allowed) {
    for (var i=0; i< string.length; i++) {
       if (allowed.indexOf(string.charAt(i)) == -1)
          return false;
    }
    return true;
}

function killEnter(e) {
    if (e.keyCode) return (e.keyCode != 13);
    if (e.which) return (e.which != 13);
    return true;
}

function proximo(next,e) {
    if(typeof window.event == 'undefined') // Netscape/Firefox/Opera
    {
         keynum = e.which;
         keyshift = e.shiftKey;
         keysrcelement = e.target;
         keycancelbubble = e.cancelBubble;
         keyreturnvalue = e.returnValue;
    }
    else // IE
    {
         keynum = event.keyCode;
         keyshift = event.shiftKey;
         keysrcelement = event.srcElement;
         keycancelbubble = event.cancelBubble;
         keyreturnvalue = event.returnValue;
    }

    if (keynum == "13" || keynum == "9" ||keynum == "16" || keyshift ){
      keycancelbubble = true;
      keyreturnvalue = false;
      return false;
    } else {
      if ( keysrcelement.value.length == keysrcelement.maxLength )
      {
        document.forms[0][next].focus();
      }
    }
}

function validaDigito(e, isAmount, elemento) {
    var isValidLen = true;
    if (isAmount) {
        isAmount = validaMontante(elemento);
        if (validaComprimentoDecimal(elemento)) {
            isValidLen = false;
        }
    }
    if (e.keyCode) {
        if ((e.keyCode != 8 && e.keyCode != 9) && !isValidLen) return false;
        return ((e.keyCode > 47 && e.keyCode < 58) || e.keyCode == 8 || e.keyCode == 9 || (isAmount && e.keyCode == 44));
    }
    if (e.which) {
        if ((e.which != 8 && e.which != 9) && !isValidLen) return false;
        return ((e.which > 47 && e.which < 58) || e.which == 8 || e.which == 9 || (isAmount && e.which == 44));
    }
    return true;
}

function validaMontante(elemento) {
    var temp = new Array();
    temp = elemento.value.split(',');
    return (temp.length < 2);
}

function validaComprimentoDecimal(elemento) {
    var temp = new Array();
    temp = elemento.value.split(',');
    if (temp.length > 1) {
        return (temp[1].length > 1);
    }
    return false;
}

function activateRightButton(btn1,btn2) {
		var  b1 =  document.getElementById(btn1);
		var b2 = document.getElementById(btn2);
		if ( b1 == null || b2 == null ) return ;
		b1.style.display = "none";
		b2.style.display = "inline";
}

function removerEspacos(elemento)
{
   var aux="";
   var i,lg = elemento.length;
   for (;lg>0 && elemento.charAt(lg-1)==' ';lg--);
   for (i=0;i<lg && elemento.charAt(i)==' ';i++);
   for (;i<lg;i++)
       aux = aux + elemento.charAt(i);
   return aux;
}

function inicio(){
	var agent = navigator.userAgent.toLowerCase();
	if ( agent.indexOf("safari") != -1 ) activateRightButton("btnnormal","btnsafari");
	else activateRightButton("btnsafari","btnnormal");
}

function textCounter(field, countfield, maxlimit)
{
  if (field.value.length > maxlimit)
    {field.value = field.value.substring(0, maxlimit);}
  else
    {countfield.value = maxlimit - field.value.length;}
}

