            function opacity(id, opacStart, opacEnd, millisec, delay) {
                //speed for each frame
                var speed = Math.round(millisec / 100);
                var timer = 0;

                //determine the direction for the blending, if start and end are the same nothing happens
                if(opacStart > opacEnd) {
                    for(i = opacStart; i >= opacEnd; i--) {
                        setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed)+delay);
                        timer++;
                    }
                } else if(opacStart < opacEnd) {
                    for(i = opacStart; i <= opacEnd; i++)
                        {
                        setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed)+delay);
                        timer++;
                    }
                }
            }

            //change the opacity for different browsers
            function changeOpac(opacity, id) {
                var object = document.getElementById(id).style;
                object.opacity = (opacity / 100);
                object.MozOpacity = (opacity / 100);
                object.KhtmlOpacity = (opacity / 100);
                object.filter = "alpha(opacity=" + opacity + ")";
            }

            function abaControl(aba) {
              if (aba == last)
                return;

              tempo = 250;

              opacity("pag"+last, 100, 0, tempo, 0);
              setTimeout('document.getElementById("a'+last+'").style.backgroundPosition = "top"', tempo);
              setTimeout('document.getElementById("pag'+last+'").style.display = "none"', tempo);

              setTimeout('document.getElementById("a'+aba+'").style.backgroundPosition = "bottom"', tempo);
              setTimeout('document.getElementById("pag'+aba+'").style.display = "block"', tempo);
              opacity("pag"+aba, 0, 100, tempo, tempo);

              last = aba;
            }

            function abreResumo(id) {
              changeOpac(0,"produto"+id);
              document.getElementById("produto"+id).style.display = "block"
              opacity("produto"+id, 0, 100, 250, 0);
            }

            function fechaResumo(id) {
              opacity("produto"+id, 100, 0, 250, 0);
              setTimeout('document.getElementById("produto'+id+'").style.display = "none"', 250);
            }









    // Valida CNPJ
    function validaCNPJ(cnpj)
    {
      if (isNaN(cnpj)) { return false; }
      var i;
      var c = cnpj.substr(0, 12);
      var dv = cnpj.substr(12, 2);
      var d1 = 0;
      for (i = 0; i < 12; i++)
      {
        d1 += c.charAt(11 - i) * (2 + (i % 8));
      }
      if (d1 == 0) { return false; }
      d1 = 11 - (d1 % 11);
      if (d1 > 9) { d1 = 0; }
      if (dv.charAt(0) != d1) { return false; }
      d1 *= 2;
      for (i = 0; i < 12; i++)
      {
        d1 += c.charAt(11 - i) * (2 + ((i + 1) % 8));
      }
      d1 = 11 - (d1 % 11);
      if (d1 > 9) { d1 = 0; }
      if (dv.charAt(1) != d1) { return false; }

      return true;
    }

    // Valida CPF
    function validaCPF(CPF)
    {
      var posicao, i, soma, dv, dv_informado;
      var digito = new Array(10); //cria uma array de 11 posições para armazenar o CPF
      dv_informado = CPF.substr(9, 2); //armazena os dois últimos dígito do CPF
      for (i=0; i<=8; i++) { //desmembra o número do CPF na array digito
        digito[i] = CPF.substr( i, 1);
      }

      //calcula o valor do 10° dígito da verificação
      posicao = 10;
      soma = 0;
      for (i=0; i<=8; i++) {
        soma = soma + digito[i] * posicao;
        posicao = posicao - 1;
      }
      digito[9] = soma % 11;
      if (digito[9] < 2)
      {
        digito[9] = 0;
      } else {
        digito[9] = 11 - digito[9];
      }

      //calcula o valor do 11° dígito da verificação
      posicao = 11;
      soma = 0;
      for (i=0; i<=9; i++) {
        soma = soma + digito[i] * posicao;
        posicao = posicao - 1;
      }
      digito[10] = soma % 11;
      if (digito[10] < 2) {
        digito[10] = 0;
      } else {
        digito[10] = 11 - digito[10];
      }

      //verifica se os dígitos verificadores conferem
      dv = digito[9] * 10 + digito[10];
      if (dv != dv_informado ||
        CPF == 00000000000 ||
        CPF == 11111111111 ||
        CPF == 22222222222 ||
        CPF == 33333333333 ||
        CPF == 44444444444 ||
        CPF == 55555555555 ||
        CPF == 66666666666 ||
        CPF == 77777777777 ||
        CPF == 88888888888 ||
        CPF == 99999999999) {
        return false;
      }

      return true;
    }





function numinput(evento) {
  var strCheck = '0123456789';
  var whichCode = (evento.which) ? evento.which : evento.keyCode;
  key = String.fromCharCode(whichCode);
  if (!isNaN(key))
    return true;
  if ((whichCode > 32 && whichCode < 41) || whichCode == 8 || whichCode == 13 || whichCode == 46 || whichCode == 0)
    return true;

  key = String.fromCharCode(whichCode);
  if (strCheck.indexOf(key) == -1) return false;
}

function numseq(campo) {
  return campo.value.replace(/\D/g,'');
}

function formatar(mascara, valor){
  retorno = "";
  x = qtd = 0;
  str_len = valor.length;
  mask_len = mascara.length - 1;
  dados = valor;
  for (i=0; i < mask_len; i++) {
    str = mascara.substr(i,1);
    if (str != "#")
      dados = dados.replace(str, "");
    else
      qtd++;
  }
  qtd++;
  if (dados.length > qtd)
    dados = dados.substr(0,qtd);

  for (i = 0; i < str_len; i++) {
    while (true) {
      if (mascara.substr(x,1) != "#" && x < mascara.length) {
        retorno += mascara.substr(x,1);
        x++;
        continue;
      } else {
        retorno += dados.substr(i,1);
				x++;
				break;
			}
		}
	}

	return retorno;
}


function formatnum (campo) {
  seqnum = numseq(campo);
  retorno = '';
  milhar = '';

  if (seqnum.substr(0,2) == '00')
    seqnum = seqnum.substr(2);

  if(seqnum.substr(0,1) == '0')
    seqnum = seqnum.substr(1);

  len = seqnum.length;
    if (len == 0) campo.value = '';
    if (len == 1) campo.value = '0,0' + seqnum;
    if (len == 2) campo.value = '0,' + seqnum;
    if (len > 2) {
        decimal = ',' + seqnum.substr(len-2);

        for (i=3; i <= len; i++) {
            if ((i % 3) == 0)
                milhar += '.';
            milhar += seqnum.charAt(len-i);
        }

        len = milhar.length;
        for (i=0; i < len; i++)
            retorno += milhar.charAt(len-i);

        campo.value = retorno+decimal;
    }
}


function addFields(campos, local) {
    var newFields = document.getElementById(campos).cloneNode(true);
    newFields.id = '';
    newFields.value = '';
    newFields.style.display = 'block';

    //zera inputs
    var fields = newFields.getElementsByTagName("input");
    for (var i=0;i<fields.length;i++) {
        fields[i].value = '';
    }
    
    //zera selects
    var fields = newFields.getElementsByTagName("select");
    for (var i=0;i<fields.length;i++) {
        fields[i].selectedIndex = 0;
    }
    
    var insertHere = document.getElementById(local);
    insertHere.parentNode.insertBefore(newFields,insertHere);

}


function validaData(ini) {
  if (ini.length != 10)
    return false;

  barras = ini.split("/");

  data = new Date();
  data.setFullYear(barras[2], barras[1]-1, barras[0]);

  if (data.getMonth()+1 != barras[1] || data.getDate() != barras[0])
    return false;

  return data;
}

function validaDataHora(ini) {
  if (ini.length != 19)
    return false;

  barras = ini.split("/");

  hora = barras[2].substring(7,9);
  min = barras[2].substring(11,13);


  barras[2] = barras[2].substring(0,4);

  data = new Date();
  data.setFullYear(barras[2], barras[1]-1, barras[0]);

  if (data.getMonth()+1 != barras[1] || data.getDate() != barras[0]
     || hora < 0 || hora > 23 || min < 0 || min > 59 )
    return false;

  return data;
}

function textarealimit(field,MaxLength) {
  if (MaxLength !=0) {
     if (field.value.length > MaxLength)  {
      field.value = field.value.substring(0, MaxLength);
      alert("Número de caracteres excedidos! O limite máximo é de "+MaxLength+" caracteres.");
    }
  }
  document.getElementById("contador").value = field.value.length+'/'+MaxLength;
}

var incoming = false;
function ajax(url, funcao, theForm, divStatus) {
  //testa se ja existe uma requisicao ajax em tramite
  if (incoming)
    return;

    var aux = document.getElementById(divStatus).innerHTML;
    document.getElementById(divStatus).innerHTML = "<img src='tela/ajax.gif' align='absmiddle'> &nbsp; carregando...";
    req = null;
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
    // Procura por uma versão ActiveX (IE)
    } else if (window.ActiveXObject) {
        if(!(req = new ActiveXObject("Microsoft.XMLHTTP")))
          req = new ActiveXObject("Msxm12.XMLHTTP");
    }

    if (req) {
      req.onreadystatechange = function() {
        if (req.readyState == 4 || req.readyState == "complete") {
            incoming = false;
            if (req.status == 200) {
              //volta o conteudo anterior do div da msg
              //document.getElementById(divStatus).innerHTML = aux;
              eval(funcao + "('" + req.responseText + "');");
            } else {
                alert("Houve um problema ao obter os dados");
            }
        }
      };

      if (!theForm) {
        req.open('GET',url,true);
        req.send(null);
      } else {
        var dados = '';
        if (theForm.elements) {
          for (e=0;e<theForm.elements.length;e++) {
              if (theForm.elements[e].name!= '') {
            	  if (theForm.elements[e].type == "radio" && !theForm.elements[e].checked)
                      continue;
            	  
                  var name = theForm.elements[e].name;
                  dados += (dados=='')?'':'&'
                  dados += name+'='+escape(theForm.elements[e].value);
              }
          }
        } else {
          dados = formulario;
        }

        req.open('POST',url,true);
        req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

        req.send(dados);
      }
      incoming = true;

    } else {
      alert("Seu navegador não suporta AJAX");
    }

}
