/////////////////////////////////////////////////////////////////
//Função Máscaras Gerais
/////////////////////////////////////////////////////////////////
function formatar(src, mask)
  {
  var objRange = document.selection.createRange();

  document.cadastro.nome.value.charAt = src.value.charAt;

  var i = src.value.length;
  var saida = mask.substring(0,1);
  var texto = mask.substring(i)

  if (texto.substring(0,1) != saida)
    {
	  src.value += texto.substring(0,1);
    }
  }

function FormatMask(Campo, sMask, evtKeyPress)
  {

  var i, nCount, sValue, fldLen, mskLen,bolMask, sCod, nTecla;

  if(document.all)
    {
    nTecla = evtKeyPress.keyCode; // Internet Explorer
    }
    else if(document.layers)
           {
           nTecla = evtKeyPress.which; // Nestcape
           }

  if(nTecla == 08) return true;
  if(nTecla < 48 || nTecla > 57)
    {
    if(document.all)
      {
      evtKeyPress.keyCode=null;
      }
      else if(document.layers)
           {
           evtKeyPress.which = null;
           }
    return false;
    }

  sValue = Campo.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) == "/") || (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++;
    }

  Campo.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;
    }
  }
//Fim da Função Máscaras Gerais
/////////////////////////////////////////////////////////////////

/////////////////////////////////////////////////////////////////
//funcoes pra manipular SELECT
/////////////////////////////////////////////////////////////////
function transferOption(objectB,objectA,objecthidden)
    {
    var index = objectB.selectedIndex;
    if (index > -1)
       {
       var newoption = new Option(objectB.options[index].text, objectB.options[index].value, true, true);
       objectA.options[objectA.length] = newoption;
       if (!document.getElementById) history.go(0);
       //object.dropdownlistB.options[index] = null;
       objectB.selectedIndex = -1;
       }
    CopiarItens(objectA,objecthidden);
    }

function ClearSelect(object,objecthidden)
    {
    object.options.length = 0;
    CopiarItens(object,objecthidden);
    }

function DelOption(object,objecthidden)
    {
    var index = object.selectedIndex;
    if (index > -1)
      {
      object.options[index] = null;
      object.selectedIndex = 0;
      CopiarItens(object,objecthidden);
      }
    }

function Unselect(object)
    {
    object.selectedIndex = -1;
    }

function UpOption(object,objecthidden)
    {
    var index = object.selectedIndex;
    if (index > 0)
      {
      var optionvalue = object.options[index].value;
      var optiontext = object.options[index].text;

      object.options[index].value = object.options[index-1].value;
      object.options[index].text = object.options[index-1].text;

      object.options[index-1].value = optionvalue;
      object.options[index-1].text = optiontext;

      object.selectedIndex = index-1;
      }
   CopiarItens(object,objecthidden);
   }

function DownOption(object,objecthidden)
    {
    var index = object.selectedIndex;
    var N = object.options.length -1;
    if (index < N)
      {
      var optionvalue = object.options[index].value;
      var optiontext = object.options[index].text;

      object.options[index].value = object.options[index+1].value;
      object.options[index].text = object.options[index+1].text;

      object.options[index+1].value = optionvalue;
      object.options[index+1].text = optiontext;

      object.selectedIndex = index+1;
      }
    CopiarItens(object,objecthidden);
    }

function CopiarItens(object,objecthidden)
    {
    var N = object.options.length;
    var aux = "";
    if (N > 0)
      {
      aux = object.options[0].value;
      for (var i=1;i<N;i++)
         {
         aux += "@" + object.options[i].value;
         }
      }
    objecthidden.value=aux;
    }

//fim das funcoes para manipular SELECT
/////////////////////////////////////////////////////////////////

function trocaDocumento(elemento,campolabel,campotext)
  {
  if (elemento[0].checked)
    {
    campolabel.value='CPF';
    campotext.onkeypress = FormatMask(this,'999.999.999-99',event);
    }
  if (elemento[1].checked)
    {
    campolabel.value='Identidade';
    campotext.onkeypress = FormatMask(this,'',event);
    }
  }

//Prevenir enter submit
var nav = window.Event ? true : false;
if (nav) {
   window.captureEvents(Event.KEYDOWN);
   window.onkeydown = NetscapeEventHandler_KeyDown;
} else {
   document.onkeydown = MicrosoftEventHandler_KeyDown;
}

function NetscapeEventHandler_KeyDown(e) {
  if (e.which == 13 && e.target.type != 'textarea' && e.target.type != 'submit') { return false; }
  return true;
}

function MicrosoftEventHandler_KeyDown() {
  if (event.keyCode == 13 && event.srcElement.type != 'textarea' && event.srcElement.type != 'submit')
    return false;
  return true;
}

///////////////////////////////////////////////////////////////////////////////
function MostraEscondeDiv(nomediv,opcao)
  {
  document.getElementById(nomediv).style.display = opcao;
  }

function MostraEscondeDivAuto(nomediv)
  {
  aa = document.getElementById(nomediv).style.display;
  if (aa == 'none')
    {
    document.getElementById(nomediv).style.display = '';
    }
    else
    {
    document.getElementById(nomediv).style.display = 'none';
    }
  }

function AddEnviarOption(objectB,objectA,objectResposta,objecthidden,objectLabel,nomediv)
    {
    var novotexto = objectB.value;
    if (objectLabel.value == 'Criar Texto:')
      {
      var diferencial = Math.round(99999*Math.random())
      var novovalor = "*"+diferencial;
      var newoption = new Option(novotexto, novovalor, true, true);
      objectA.options[objectA.length] = newoption;
      }
    if (objectLabel.value == 'Editar Texto:')
      {
      var index = objectA.selectedIndex;
      objectA.options[index].text = novotexto;
      }
    objectB.value = '';
    if (nomediv != '') MostraEscondeDiv(nomediv,'none');
    AddCopiarItens(objectA,objectResposta,objecthidden);
    }

function AddFechaOption(objectB,nomediv)
    {
    objectB.value = '';
    if (nomediv != '') MostraEscondeDiv(nomediv,'none');
    }

function AddCriarOption(objectB,objectLabel,nomediv)
    {
    objectB.value = '';
    if (nomediv != '') MostraEscondeDiv(nomediv,'');
    objectB.focus();
    objectLabel.value = 'Criar Texto:';
    }

function AddEditarOption(objectB,objectA,objectLabel,nomediv)
    {
    var index = objectA.selectedIndex;
    //var optionvalue = object.options[index].value;
    if (index != -1)
      {
      objectB.value = objectA.options[index].text;
      if (nomediv != '') MostraEscondeDiv(nomediv,'');
      objectB.focus();
      }
    objectLabel.value = 'Editar Texto:';
    }

function AddUpOption(object,objectResposta,objecthidden)
    {
    var index = object.selectedIndex;
    if (index > 0)
      {
      var optionvalue = object.options[index].value;
      var optiontext = object.options[index].text;

      object.options[index].value = object.options[index-1].value;
      object.options[index].text = object.options[index-1].text;

      object.options[index-1].value = optionvalue;
      object.options[index-1].text = optiontext;

      object.selectedIndex = index-1;
      }
   AddCopiarItens(object,objectResposta,objecthidden);
   }

function AddDownOption(object,objectResposta,objecthidden)
    {
    var index = object.selectedIndex;
    var N = object.options.length -1;
    if (index < N)
      {
      var optionvalue = object.options[index].value;
      var optiontext = object.options[index].text;

      object.options[index].value = object.options[index+1].value;
      object.options[index].text = object.options[index+1].text;

      object.options[index+1].value = optionvalue;
      object.options[index+1].text = optiontext;

      object.selectedIndex = index+1;
      }
    AddCopiarItens(object,objectResposta,objecthidden);
    }

function AddCopiarItens(object,objectResposta,objecthidden)
    {
    var index = object.selectedIndex;
    var optiontext = object.options[index].text;
    var optionindex = object.options[index].value;
    objectResposta.value = optiontext;

    var N = object.options.length;
    var aux = "-1%&%" + optionindex;
    if (N > 0)
      {
      for (var i=0;i<N;i++)
         {
         aux += "%@%" + object.options[i].value + "%&%" + object.options[i].text;
         }
      }
    objecthidden.value=aux;
    }

function AddDelOption(object,objectResposta,objecthidden)
    {
    var index = object.selectedIndex;
    if (index > -1)
      {
      object.options[index] = null;
      object.selectedIndex = 0;
      AddCopiarItens(object,objectResposta,objecthidden);
      }
    }

///////////////////////////////////////////////////////////////////////////////
function WindowAddCopiarItens(object,objecthidden)
    {
    var index = object.selectedIndex;
    var optiontext = object.options[index].text;
    var optionindex = object.options[index].value;

    var N = object.options.length;
    var aux = "-1%&%" + optionindex;
    if (N > 0)
      {
      for (var i=0;i<N;i++)
         {
         aux += "%@%" + object.options[i].value + "%&%" + object.options[i].text;
         }
      }
    objecthidden.value=aux;
    }

function WindowAddDelOption(object,objecthidden)
    {
    var index = object.selectedIndex;
    var N = object.options.length;
    if ((index > -1) & (N > 0))
      {
      object.options[index] = null;
      object.selectedIndex = 0;
      WindowAddCopiarItens(object,objecthidden);
      }
    }

function WindowAddUpOption(object,objecthidden)
    {
    var index = object.selectedIndex;
    var N = object.options.length;
    if ((index > 0) & (N > 0))
      {
      var optionvalue = object.options[index].value;
      var optiontext = object.options[index].text;

      object.options[index].value = object.options[index-1].value;
      object.options[index].text = object.options[index-1].text;

      object.options[index-1].value = optionvalue;
      object.options[index-1].text = optiontext;

      object.selectedIndex = index-1;

      WindowAddCopiarItens(object,objecthidden);
      }
   }

function WindowAddDownOption(object,objecthidden)
    {
    var index = object.selectedIndex;
    var N = object.options.length -1;
    if ((index < N) & (N > 0))
      {
      var optionvalue = object.options[index].value;
      var optiontext = object.options[index].text;

      object.options[index].value = object.options[index+1].value;
      object.options[index].text = object.options[index+1].text;

      object.options[index+1].value = optionvalue;
      object.options[index+1].text = optiontext;

      object.selectedIndex = index+1;

      WindowAddCopiarItens(object,objecthidden);
      }
    }

function WindowAddFechaOption()
    {
    openedWindow.close();
    }

function WindowAddCriarOption(object,Link,MaxItens,Nome)
    {
    var N = object.options.length;
    if (N < MaxItens)
      {      openedWindow = window.open(Link,'','resizable=yes,toolbar=no,scrollbars=yes,status=no,directories=no,menubar=no,left=150,top=100,width=640,location=no');
      //openedWindow.opener.document.title = 'Romir Soares';
      }
      else
      {      var mensagem='Alcançado o número máximo de ' + MaxItens.toString() + ' ' + Nome + '.\nPara poder incluir mais ' + Nome + ' deve-se retirar um dos existentes...';
      alert(mensagem);
      }
    }

function WindowAddIncluirOption(object,novotexto,novovalor,objecthidden)
    {
    var newoption = new Option(novotexto, novovalor, true, true);
    object.options[object.length] = newoption;
    WindowAddCopiarItens(object,objecthidden);
    }


