


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");

    }



}


function utf8_encode(argString) {
    // Encodes an ISO-8859-1 string to UTF-8
    //
    // version: 1103.1210
    // discuss at: http://phpjs.org/functions/utf8_encode    // +   original by: Webtoolkit.info (http://www.webtoolkit.info/)
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: sowberry
    // +    tweaked by: Jack
    // +   bugfixed by: Onno Marsman    // +   improved by: Yves Sucaet
    // +   bugfixed by: Onno Marsman
    // +   bugfixed by: Ulrich
    // *     example 1: utf8_encode('Kevin van Zonneveld');
    // *     returns 1: 'Kevin van Zonneveld'    var string = (argString + ''); // .replace(/\r\n/g, "\n").replace(/\r/g, "\n");
    var utftext = "",
        start, end, stringl = 0;

    start = end = 0;    stringl = string.length;
    for (var n = 0; n < stringl; n++) {
        var c1 = string.charCodeAt(n);
        var enc = null;
         if (c1 < 128) {
            end++;
        } else if (c1 > 127 && c1 < 2048) {
            enc = String.fromCharCode((c1 >> 6) | 192) + String.fromCharCode((c1 & 63) | 128);
        } else {            enc = String.fromCharCode((c1 >> 12) | 224) + String.fromCharCode(((c1 >> 6) & 63) | 128) + String.fromCharCode((c1 & 63) | 128);
        }
        if (enc !== null) {
            if (end > start) {
                utftext += string.slice(start, end);            }
            utftext += enc;
            start = end = n + 1;
        }
    }
    if (end > start) {
        utftext += string.slice(start, stringl);
    }
     return utftext;
}



    // 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 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;

}


