function criaMascara(_RefObjeto, _Modelo){

    var valorAtual = _RefObjeto.value;
    var valorNumerico = '';
    var nIndexModelo = 0;
    var nIndexString = 0;
    var valorFinal = '';
    var adicionarValor = true;


      // limpa a string valor atual para verificar
      // se todos os caracteres são números
      for (i=0;i<_Modelo.length;i++){
        if (_Modelo.substr(i,1) != '#'){
          valorAtual = valorAtual.replace(_Modelo.substr(i,1),'');
      }}

      // verifica se todos os caracteres são números
      for (i=0;i<valorAtual.length;i++){
        if (!isNaN(parseFloat(valorAtual.substr(i,1)))){
          valorNumerico = valorNumerico + valorAtual.substr(i,1);
      }}
      
      // aplica a máscara ao campo informado usando
      // o modelo de máscara informado no script
      for (i=0;i<_Modelo.length;i++){

        if (_Modelo.substr(i,1) == '#'){
          if (valorNumerico.substr(nIndexModelo,1) != ''){
            valorFinal = valorFinal + valorNumerico.substr(nIndexModelo,1);
            nIndexModelo++;nIndexString++;
          }
            else {
              adicionarValor = false;
        }}

          else {
            if (adicionarValor && valorNumerico.substr(nIndexModelo,1) != ''){
            valorFinal = valorFinal + _Modelo.substr(nIndexString,1)
            nIndexString++;
          }}
      }

      //alert(valorFinal)
      _RefObjeto.value = valorFinal
}

function limpaString(S)
{
	// Deixa so' os digitos no numero
	var Digitos = "0123456789";
	var temp = "";
	var digito = "";

	for (var i=0; i<S.length; i++)
	{
		digito = S.charAt(i);
		if (Digitos.indexOf(digito)>=0)
		{
			temp=temp+digito;
		}
	} //for

	return temp;
}

//Função de validação de CNPJ
function isCnpj(s)
{
	s = limpaString(s);

	var a = new Array();
	var b = new Number;

	var c = [6,5,4,3,2,9,8,7,6,5,4,3,2];
	
	for (i=0; i<12; i++)
	{
		a[i] = s.charAt(i);
		b += a[i] * c[i+1];
	}
	
	if ((x = b % 11) < 2)
	{
		a[12] = 0;
	} else {
		a[12] = 11-x;
	}
	
	b = 0;
	for (y=0; y<13; y++)
	{
		b += (a[y] * c[y]);
	}
	
	if ((x = b % 11) < 2)
	{
		a[13] = 0;
	} else {
		a[13] = 11-x;
	}
	
	if ((s.charAt(12) != a[12]) || (s.charAt(13) != a[13]))
	{
		return false;
	}
	
	return true;
}

function OpenPopUpFull(targetUrl, paramentros ,windowName)
  {
  if (document.all) 
    {
    var w = screen.availWidth-10;
    var h = screen.availHeight-200;
    }
  else if (document.layers||document.getElementById) 
    {
    if (top.window.outerHeight<screen.availHeight||top.window.outerWidth<screen.availWidth) 
      {
      var h = screen.availHeight-200;
      var w = screen.availWidth-10;
      }
    }
  var windowFeatures = "width=" + w + ", height=" + h + ", top=0, left=0, resizable=yes, scrollbars=yes, toolbar=no, location=no, menubar=no, status=no";
//  alert(targetUrl);
  windowFull = window.open('popup.php?t='+targetUrl+'&'+paramentros, windowName, windowFeatures);
  windowFull.focus();
  }

function OpenPopUp(targetUrl, targetParam, windowName, x, y, escala, scroll, tool, localiz, barramenu, barstatus)
  {
  var windowFeatures = "width=" + x + ", height=" + y + ", top=100, left=130, resizable=" + escala + ", scrollbars=" + scroll + ", toolbar=" + tool + ", location=" + localiz + ", menubar=" + barramenu + ", status=no";
  windowPesquisa = window.open('monta_popup.php?t='+targetUrl+'&'+targetParam, windowName, windowFeatures);
  windowPesquisa.focus();
  }

function janela(param,w,h,nome)
  {
  var nomearq=param;
  var windowvar = window.open(nomearq,nome,"scrollbars=yes,location=no,directories=no,status=no,menubar=no,resizable=no,toolbar=no,width="+ w + ",height="+h );
  }

function PopupBusca(tela, campo, param)
  {
  parametros = "campoAlvo="+campo+"&"+param;
  OpenPopUp(tela, parametros, 'Busca', 490, 400, 'no', 'yes', 'no', 'no', 'no', 'no');
  }


function APagaImg(php, param)
  {
  var link = "monta_popup.php?t="+php+"&"+param;
  parent.frames['auxFrame'].document.location = link;
  }

function PreencheForm(campo, cod, close, concatena)
  {
  try
    {
    eval('var campo = window.opener.document.'+campo+';');
    }
  catch(erro)
	{
    //nem trata o erro
	}

  if (typeof(campo) == 'object')
    {
    if(concatena)
  	  campo.value = campo.value+" "+cod;
	else
      campo.value = cod;
	
	if(campo.type != 'hidden') 
      campo.focus();
    }

  if (typeof(close)== false) 
    window.close();
  }

  function SendUrl()
  {
  var args = SendUrl.arguments;

  args[1] = (args[1] != null && args[1] != '') ? args[1] : "";
  args[2] = (args[2] != null && args[2] != '') ? "#"+args[2] : "";

  url = "montatela.php?t="+args[0]+"&"+args[1]+args[2];

  window.open(url, '_self');
  }

function AdmCombos (form, cmp, cmpDest, op)
  {
// form -> Nome do formulário
// cmp  -> Campo de origem dos dados
// cmpDest -> Campo destino dos dados
// op -> Ocção que se deseja executar
  var i;
  var j      = 0;
  var count  = 0;
  var c      = new Array();
  var aux = false;

  var Form = eval("document."+form);
  for (i = 0; i < Form.elements.length; i++)
    {
	if (Form.elements[i].name.indexOf(cmp) == 0)
	  var obj = Form.elements[i].options;

	if (Form.elements[i].name.indexOf(cmpDest) == 0)
      var objSel = Form.elements[i].options;
    }
    
  switch(op)
    {
    case "add":
      for(i=0; i < obj.length; i++) 
		{
        if(obj[i].selected)
          {
          c[count] = i;
          count ++;
          obj[i].selected = false;
          }
		}

	  if(c.length == 0) alert("Você deve selecionar pelo menos um item");

      j = objSel.length;
      objSel.length = objSel.length + c.length;  
      for(i=0; i < c.length; i++) 
        {
		for(a=0; a < objSel.length; a++)
          if(obj[c[i]].value == objSel[a].value)
            aux = true;

        if(!aux)
          {
          objSel[j].value  = obj[c[i]].value;
          objSel[j].text   = obj[c[i]].text;
          j++;
          }
        else
          {
          alert("Item já selecionado");
          objSel.length = objSel.length - 1;
          aux = false;
          }
        }
      break;

    case "del":
      for(i=0; i < obj.length; i++) 
        if(!obj[i].selected)
          {
		  c[j] = new Array();
          c[j]["text"]  = obj[i].text;
		  c[j]["value"] = obj[i].value;
		  j++;
          }
      
      if(c.length == obj.length) alert("Você deve selecionar pelo menos um item");

      obj.length = c.length;

      for(i=0; i < c.length; i++) 
        {
        obj[i].text  = c[i]["text"];
        obj[i].value = c[i]["value"];
        obj[i].selected = false;
        }
      break;

    case "del_all":
      obj.length = 0;
      break;

    case "add_all":
      j = objSel.length;
      objSel.length = j + obj.length;  
      for(i=0; i < obj.length; i++) 
        {
        for(a=0; a < objSel.length; a++)
          if(obj[i].value == objSel[a].value)
            aux = true;

        if(!aux)
          {
          objSel[j].value    = obj[i].value;
          objSel[j].text     = obj[i].text;
          objSel[j].selected = false;
          j++;
          }
        else
          {
          objSel.length = objSel.length - 1;
          aux = false;
          }
        }
      break;
    case "sel_all":
      for(i=0; i < obj.length; i++) 
        obj[i].selected = true;

      break;
    }
  }

<!--
function OutroFrame(php, param)
  {
  var link = "montatela.php?t="+php+param;
//    link += "frm="+nomefrm;
//    link += "&cmp="+mudar;
//    link += "&c="+tipo;
//    link += "&todas="+todas;
//    link += "&sel="+selecao;

  parent.frames['auxFrame'].document.location = link;
  }

function mostraDiv(nome){
	if(document.getElementById(nome).style.display != "block"){
		document.getElementById(nome).style.display = "block";
	} else {
		document.getElementById(nome).style.display = "none";
	}
}
-->
