﻿ function validaCNPJ(CNPJ) {
   erro = new String;
   if (CNPJ.length < 18)
   {
     return false;
   }

   if ((CNPJ.charAt(2) != ".") || (CNPJ.charAt(6) != ".") || (CNPJ.charAt(10) != "/") || (CNPJ.charAt(15) != "-"))
   {
    return false;
   }
   //substituir os caracteres que não são números
   if(document.layers && parseInt(navigator.appVersion) == 4){
       x = CNPJ.substring(0,2);
       x += CNPJ. substring (3,6);
       x += CNPJ. substring (7,10);
       x += CNPJ. substring (11,15);
       x += CNPJ. substring (16,18);
       CNPJ = x;
   } else {
       CNPJ = CNPJ. replace (".","");
       CNPJ = CNPJ. replace (".","");
       CNPJ = CNPJ. replace ("-","");
       CNPJ = CNPJ. replace ("/","");
   }
   var nonNumbers = /\D/;
   var a = [];
   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] = CNPJ.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 ((CNPJ.charAt(12) != a[12]) || (CNPJ.charAt(13) != a[13])){
        return false;
   }

   return true;
 }

function validaCPF(CPF)
{
    if (CPF.length < 14)
    {
        return false;
    }

    if ((CPF.charAt(3) != ".") || (CPF.charAt(7) != ".") || (CPF.charAt(11) != "-"))
    {
        return false;
    }

    if(document.layers && parseInt(navigator.appVersion) == 4){
       x = CPF.substring(0,3);
       x += CPF. substring (4,7);
       x += CPF. substring (8,11);
       x += CPF. substring (12,14);
       CPF = x;
   } else {
       CPF = CPF. replace (".","");
       CPF = CPF. replace (".","");
       CPF = CPF. replace ("-","");
   }

    if (CPF.length != 11)
    {
      return false;
    }

    digito1 = parseInt(CPF.substring(9,10));
    digito2 = parseInt(CPF.substring(10,11));

    ver = 0;
    for (i = 0; i < 9; i++)
    {
        ver += parseInt(CPF.substring(i, i + 1)) * (10 - i);
    }

    if (ver == 0)
    {
        return false;
    }

    ver = (ver % 11);

   if (ver < 2) {
      ver = 0;
   }
   else {
      ver = 11 - ver;
   }

   if (digito1 != ver) {
      return false;
   }

   ver *= 2;

   for (i = 0; i < 9; i++) {
      ver += parseInt(CPF.substring(i, i + 1)) * (11 - i);
   }

   ver = 11 - (ver % 11);

   if (ver > 9) ver = 0;

   if (digito2 != ver) {
      return false;
   }

   return true;
}

function FormataLogin(Campo, teclapres)
{
   if(window.event) // for IE
   {
    	tecla = window.event.keyCode;
   }
   else
   {
       tecla = teclapres.which;
   }

   if (tecla == 8 || tecla == 13 || tecla == 0 || tecla == 9)// || tecla == 118 || tecla == 99)
   {
      return true;
   }

    tecla = String.fromCharCode(tecla);
    valor = Campo.value.concat(tecla)

    if (valor.length < 15)
    {
        retorno = AjustaAoFormato(valor, "XXX.XXX.XXX-XX", "0123456789");
    }
    else
    {
        retorno = AjustaAoFormato(valor, "XX.XXX.XXX/XXXX-XX", "0123456789");
    }
    Campo.value = retorno;
    return false;

}

function FormataCNPJ(Campo, teclapres)
{

   if(window.event) // for IE
   {
    	tecla = window.event.keyCode;
   }
   else
   {
       tecla = teclapres.which;
   }

   if (tecla == 8 || tecla == 13 || tecla == 0 || tecla == 9)// || tecla == 118 || tecla == 99)
   {
      return true;
   }

   tecla = String.fromCharCode(tecla);

   retorno = AjustaAoFormato(Campo.value.concat(tecla), "XX.XXX.XXX/XXXX-XX", "0123456789");
   Campo.value = retorno;
   return false;
}

function FormataData(Campo, teclapres)
{
   if(window.event) // for IE
   {
    	tecla = window.event.keyCode;
   }
   else
   {
       tecla = teclapres.which;
   }


   if (tecla == 8 || tecla == 13 || tecla == 0 || tecla == 9)// || tecla == 118 || tecla == 99)
   {
      return true;
   }

     var ini;
     //IE @#$%@$%&*
     if (document.selection) {

       // Set focus on the element
       Campo.focus ();

       // To get cursor position, get empty selection range
       var oSel = document.selection.createRange ();

       // Move selection start to 0 position
       oSel.moveStart ('character', -Campo.value.length);

       ini = oSel.text.length;
     }
     else
     {
          ini = Campo.selectionEnd;
     }


   var setaCaret = false;


   if (ini != Campo.value.length)
   {
      setaCaret = true;
   }

   tecla = String.fromCharCode(tecla).toUpperCase();


   valor = Campo.value.substring(0,ini) + tecla + Campo.value.substr(ini);

//   retorno = AjustaAoFormato(Campo.value.concat(tecla), "XX/XX/XXXX", "0123456789");
   retorno = AjustaAoFormato(valor, "XX/XX/XXXX", "0123456789");

   Campo.value = retorno;

   if (setaCaret)
   {
          if (document.selection)
          {
               // Set focus on the element
               Campo.focus ();

               // Create empty selection range
               var oSel = document.selection.createRange ();

               // Move selection start and end to 0 position
               oSel.moveStart ('character', -Campo.value.length);
               oSel.moveEnd ('character', -Campo.value.length);
//               oSel.moveEnd ('character', 0);

               // Move selection start and end to desired position
               oSel.moveStart ('character', ini + 1);
//               oSel.moveEnd ('character', ini - 15);
               oSel.select ();
          }
          else
          {
             Campo.selectionEnd = ini + 1;
          }
   }

   return false;

}

function FormataPLACA(Campo, teclapres)
{

   if(window.event) // for IE
   {
    	tecla = window.event.keyCode;
   }
   else
   {
       tecla = teclapres.which;
   }

//      alert(tecla);
   if (tecla == 8 || tecla == 13 || tecla == 0 || tecla == 9)// || tecla == 118 || tecla == 99)
   {

      return true;
   }

   tecla = String.fromCharCode(tecla).toUpperCase();


   if (Campo.value.length < 3)
   {
        retorno = AjustaAoFormato(Campo.value.concat(tecla), "XXX", "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
   }
   else
   {
        formato = Campo.value.substring(0, 3) + "XXXX";
        retorno = AjustaAoFormato(Campo.value.concat(tecla), formato, "0123456789");
   }

   Campo.value = retorno;

   return false;
}

function FormataCPF(Campo, teclapres)
{

   if(window.event) // for IE
   {
    	tecla = window.event.keyCode;
   }
   else
   {
       tecla = teclapres.which;
   }

   if (tecla == 8 || tecla == 13 || tecla == 0 || tecla == 9)// || tecla == 118 || tecla == 99)
   {
      return true;
   }

   tecla = String.fromCharCode(tecla);

   retorno = AjustaAoFormato(Campo.value.concat(tecla), "XXX.XXX.XXX-XX", "0123456789");
   Campo.value = retorno;
   return false;
}


function AjustaAoFormato(valor, formato, validos)
{

     count = 0;
     resultado = "";
	for (i = 0; i < valor.length; i++)
	{
          if (i == formato.length)
		{
			break;
		}
		if (validos.indexOf(valor.substr(i,1)) != -1)
		{
			if (formato.substr(count, 1) != "X")
			{
				resultado = resultado.concat(formato.substr(count, 1));
				count++;
			}
			resultado = resultado.concat(valor.substr(i,1));
               count++;
		}
		else
		{
			if (formato.substr(count, 1) == valor.substr(i,1))
			{
				resultado = resultado.concat(formato.substr(count,1));
				count++
			}
		}
	}
	return resultado;
}

function newXMLHttpRequest() {

  var xmlreq = false;
  // Create XMLHttpRequest object in non-Microsoft browsers
  if (window.XMLHttpRequest) {
    xmlreq = new XMLHttpRequest();

  } else if (window.ActiveXObject) {

    try {
      // Try to create XMLHttpRequest in later versions
      // of Internet Explorer

      xmlreq = new ActiveXObject("Msxml2.XMLHTTP");
      
    } catch (e1) {

      // Failed to create required ActiveXObject
      
      try {
        // Try version supported by older versions
        // of Internet Explorer
      
        xmlreq = new ActiveXObject("Microsoft.XMLHTTP");

      } catch (e2) {

        // Unable to create an XMLHttpRequest by any means
        xmlreq = false;
      }
    }
  }

return xmlreq;
}


