﻿// JScript File
var popup = null;


function Abrir(nomePagina)
{
    LeftPosition = (screen.width) ? (screen.width-800)/2 : 0;
    TopPosition = (screen.height) ? (screen.height-550)/2 : 0;    
    popup = window.open(nomePagina,'janela','top='+TopPosition+',left='+LeftPosition+'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,menubar=no,width=800,height=550');
    popup.focus();	    
    return false;
}
function VerificarSelecao(item, radio)
{
    if (document.getElementById(radio).valorSelecionado == item.value)
    {
        item.checked = false;
        document.getElementById(radio).valorSelecionado = '';                
    }
    else
    {
        item.checked = true;
        document.getElementById(radio).valorSelecionado = item.value;
    }
}
function ShowModal(msg)
{
    alert(msg);
    return true;
}

function apenasNumeros(e) {
	var navegador = /msie/i.test(navigator.userAgent);

	if (navegador) {
		var tecla = event.keyCode;
	} else {
		var tecla = e.which;
	}
	
	if (tecla > 47 && tecla < 58) { // numeros de 0 a 9
		return true;
	} else {

		if (tecla != 8) { // backspace
			return false;
		} else {
			return true;
		}
	}
}
function FormataNumero(fld, milSep, decSep, casasDecimais, e) {

		var sep = 0;
		var key = '';
		var i = j = 0;
		var len = len2 = 0;
		var strCheck = '0123456789';
		var aux = aux2 = '';

        if(typeof(e)=='undefined') 
            e = window.event;
        if (e.keyCode) 
            whichCode = e.keyCode;
        else if (e.which) 
            whichCode = e.which;		
		
		//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;
		if (len >= fld.maxLength) return false;
		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 <= casasDecimais) {
		    zeros = '';
		    while( (zeros.length+len) < casasDecimais) {
		        zeros += '0';
		    }
		    fld.value = '0'+ decSep + zeros + aux;
        }
		//if (len == 1) fld.value = '0'+ decSep + '0' + aux;
		//if (len == 2) fld.value = '0'+ decSep + aux;
		if (len > casasDecimais) {
			aux2 = '';
 			for (j = 0, i = len - (casasDecimais + 1); i >= 0; i--) {
				if (j == 3) {
				    aux2 = aux.charAt(i) + milSep + aux2;					
					j = 0;
				} else {
    				aux2 = aux.charAt(i) + aux2;
				}
 				j++;
			}
			fld.value = '';
			//len2 = aux2.length;
			//for (i = len2 - 1; i >= 0; i--) 
			fld.value = aux2;//.charAt(i);
			
			if (casasDecimais > 0) {
			fld.value += decSep + aux.substr(len - casasDecimais, len);
			}
		}
		return false;
}

function SolicitarConfirmarSalvar(hdnSalvarPagina)
{
    document.getElementById(hdnSalvarPagina).value  = 'S';
}

//hdnConfirmaSalvar - indica se deverá ser realizada a pergunta para salvar
//hdn - indicar para o server se deverá ser chamado o método salvar
function ConfirmaSalvar(hdn, hdnSalvarPagina)
{
      //alert(document.getElementById(hdnSalvarPagina).value);
    if (document.getElementById(hdnSalvarPagina).value == 'S')
    {
        var answer = confirm ("Deseja salvar as alterações?")
        if (answer)
        {
        document.getElementById(hdn).value = "S";
            //return true;
        }
        //return true;
    }
        return true;
}
function ConfirmaSair(hdn, hdnSalvarPagina)
{
        var answer = confirm ("Deseja sair do Ficad ?")
        //var answer = confirm ("Deseja Salvar as Informações antes de Sair?")
        if (answer)
        {
            //return true;
            this.ConfirmaSalvar(hdn, hdnSalvarPagina);
        }
        else
        {
            return false;
        }
}

function AdicionaEventoConfirmaSalvar(hdn, valor)
{
    document.getElementById(hdn).value = valor;
    //alert(document.getElementById(hdn).value);
    //return true;
}


function validarValorMonetario(campo) {
	var tamanho = campo.value.length;
	var numeros = "1234567890,";
	
	for (i = 0; i < tamanho; i++) {
	
		if (numeros.indexOf(campo.value.substring(i, i + 1)) == -1) {
			campo.value = campo.value.substring(0, i);
			return false;
		}
	}

	return true;	     
}  
function formataTelefone(e, txtTelefone)
{
      var code;
      if (!e) var e = window.event;
      if (e.keyCode) code = e.keyCode;
      else if (e.which) code = e.which;
      var character = String.fromCharCode(code);
      if(code==8)
      {}    
      else
      {           
        if(code > 47 && code < 58)
        { 
          var conteudo;
          conteudo = document.getElementById(txtTelefone).value;
          if(conteudo.length==4)
          {
           conteudo = conteudo + '-';
           document.getElementById(txtTelefone).value = conteudo;
          }
       }
       else
       {
            event.keyCode = 0;
       }            
     }
 }
 
 function formataCPF(e, txtCPF) 
 {
      var code = 0;
      if(typeof(e)=='undefined') 
        e = window.event;
      if (e.keyCode) code = e.keyCode;
      else if (e.which) code = e.which;
      var character = String.fromCharCode(code);
      
      if(code==8)
      {
      }    
      else
      {           
        if(code > 47 && code < 58)
          { 
      //-------------
          var conteudo;
          conteudo = document.getElementById(txtCPF).value;
          if(conteudo.length==3)
          {
           conteudo = conteudo + '.';
           document.getElementById(txtCPF).value = conteudo;
          }
          else if(conteudo.length==7)
          {
           conteudo = conteudo + '.';
           document.getElementById(txtCPF).value = conteudo;  
          }
          else if(conteudo.length==11)
          {
           conteudo = conteudo + '-';
           document.getElementById(txtCPF).value = conteudo;  
          } 
       }
     else
     {
        event.keyCode = 0;
     }            
       }
} 

function mascara_data(e, idCampo)
{
    var code = 0;
    if(typeof(e)=='undefined') 
        e = window.event;
    if (e.keyCode) code = e.keyCode;
    else if (e.which) code = e.which;
    var character = String.fromCharCode(code);
    var campo = document.getElementById(idCampo)
    if(code==8)
    {
    }    
    else
    {
        if(code > 47 && code < 58)
        {
            if(campo.value.length == 2)
            {
                campo.value += "/";             
            }
            if(campo.value.length == 5)
            {
                campo.value += "/";             
            }
            document.getElementById(idCampo).value = campo.value;
        }
        else 
        {
            event.keyCode = 0;
        }        
    }

}

function formataPercentual(e, txt)
{
      var code;
      if (!e) var e = window.event;
      if (e.keyCode) code = e.keyCode;
      else if (e.which) code = e.which;
      var character = String.fromCharCode(code);
      //alert(code);
      if(code==8)
      {}    
      else
      {           
        if((code > 47 && code < 58) || (code == 44) || (code == 46))
        { 
          var conteudo;
          conteudo = document.getElementById(txt).value;
          if(conteudo.length==3)
          {
           //conteudo = conteudo + ',';
           document.getElementById(txt).value = conteudo;
          }
       }
       else
       {
            event.keyCode = 0;
       }            
     }
 }
