var reEmail    =  /^[\w!#$%&'*+\/=?^`{|}~-]+(\.[\w!#$%&'*+\/=?^`{|}~-]+)*@(([\w-]+\.)+[A-Za-z]{2,6}|\[\d{1,3}(\.\d{1,3}){3}\])$/;

function readCookie(name){
 var cookieValue = "";
 var search = name + "=";
 if(document.cookie.length > 0){
  offset = document.cookie.indexOf(search);
  if (offset != -1){
   offset += search.length;
   end = document.cookie.indexOf(";", offset);
   if (end == -1) end = document.cookie.length;
   cookieValue = unescape(document.cookie.substring(offset, end))
  }
 }
 return cookieValue;
}

function writeCookie(name, value, time_exp){
 if(time_exp != "" ){
  var exp = new Date();
  var expira = exp.getTime() + (time_exp * 3600000);
  exp.setTime(expira);
  document.cookie = "" + name + "=" + value + "; expires=" + exp.toGMTString();
 }
}

function valida_cep(cep)
{
 return ((reDigits.test(cep.value.substring(6,9))) &&
         (reDigits.test(cep.value.substring(0,5))) &&
         (cep.value.length == 9))

};

function valida_telefone(numero)
{
 retorno = true;
 
 for (i = 0; i<numero.value.length;i++)
  {
   s_Byte = numero.value.substring(i,i+1);
   
   if ((!reDigits.test(s_Byte)) && (s_Byte != '-') && (s_Byte != '/')) 
    {
     retorno = false;
    };
  };
  
 return retorno;
};

function valida_cep(cep)
{
 return ((reDigits.test(cep.value.substring(6,9))) &&
         (reDigits.test(cep.value.substring(0,5))) &&
         (cep.value.length == 9))

};

function valida_ddd(ddd)
{
 return (reDigits.test(ddd.value));
};

function valida_email(email)
{
 return (reEmail.test(email.value));
}

function email_minusculo(email)
{
 email.value = email.value.toLowerCase();
}

function replicate(s_val,i_qtd)
{
 s_cpoaux = s_val;
 
 alert(s_val);

// for (i=1,i<i_qtd,i++)
// {
//  s_cpoaux = s_cpoaux+s_val;
// };

 return s_cpoaux;
 
}

function checkCNPJ(campo)
{
   if (campo.value == '11.111.111/1111-80')
    {
     return false;
    };
    
   vCNPJ = campo.value.substring(0,2)+
           campo.value.substring(3,6)+
           campo.value.substring(7,10)+
           campo.value.substring(11,15);
   
   var mControle = "";
   var aTabCNPJ = new Array(5,4,3,2,9,8,7,6,5,4,3,2);
   for (i = 1 ; i <= 2 ; i++)
    {
      mSoma = 0;
      for (j = 0 ; j < vCNPJ.length ; j++)
         mSoma = mSoma + (vCNPJ.substring(j,j+1) * aTabCNPJ[j]);
      if (i == 2 ) mSoma = mSoma + ( 2 * mDigito );
      mDigito = ( mSoma * 10 ) % 11;
      if (mDigito == 10 ) mDigito = 0;
      mControle1 = mControle ;
      mControle = mDigito;
      aTabCNPJ = new Array(6,5,4,3,2,9,8,7,6,5,4,3);
    };
  
   return (((mControle1 * 10) + mControle ) == campo.value.substring(16,18));
};

function checkCPF(campo)
{
   vCPF = campo.value.substring(0,3)+
          campo.value.substring(4,7)+
          campo.value.substring(8,11)+
          campo.value.substring(12,14);
          
   var mControle = "";
   var mContIni = 2, mContFim = 10, mDigito = 0;

   for (j = 1 ; j <= 2 ; j++)
    {
      mSoma = 0;
      for (i = mContIni ; i <= mContFim ; i++)
         mSoma = mSoma + (vCPF.substring((i-j-1),(i-j)) * (mContFim + 1 + j - i));
      if (j == 2 ) mSoma = mSoma + ( 2 * mDigito );
      mDigito = ( mSoma * 10 ) % 11;
      if (mDigito == 10) mDigito = 0;
      mControle1 = mControle;
      mControle = mDigito;
      mContIni = 3;
      mContFim = 11;
    };
   return (((mControle1 * 10) + mControle) == campo.value.substring(12,14));
};

function list_dados( valor )
{
  document.forms[0].cidade.options.length = 1;
  document.forms[0].cidade.options[0] = new Option("Aguarde... carregando cidades","0");
  http.open("GET", "cidades.php?estado=" + valor, true);
  http.onreadystatechange = handleHttpResponse;
  http.send(null);
}

function valida_chave(numchave,cnpj,pessoa,sistema)
{
  http_chave.open("GET", "../regsis/confchv.php?chave=" +numchave+"&cnpj="+cnpj+"&pessoa="+pessoa+"&sistema="+sistema , true);
  http_chave.onreadystatechange = handleHttpResponsechave;
  http_chave.send(null);
}

function handleHttpResponsechave()
{
  if (http_chave.readyState == 4) 
  {
    results = http_chave.responseText;
	//alert(results);
	chave_valida = (results == '1');
  }
}

function handleHttpResponse()
{
  campo_select = document.forms[0].cidade;
  if (http.readyState == 4) {
    campo_select.options.length = 0;
    results = http.responseText.split(",");
    for( i = 0; i < results.length; i++ )
    { 
      string = results[i].split( "|" );
      campo_select.options[i] = new Option( string[0], string[1] );
    }
  }
}

function getHTTPObject() {
var req;
 
try {
 if (window.XMLHttpRequest) {
  req = new XMLHttpRequest();
 
  if (req.readyState == null) {
   req.readyState = 1;
   req.addEventListener("load", function () {
   req.readyState = 4;
 
   if (typeof req.onReadyStateChange == "function")
    req.onReadyStateChange();
   }, false);
  }
 
  return req;
 }
 
 if (window.ActiveXObject) {
  var prefixes = ["MSXML2", "Microsoft", "MSXML", "MSXML3"];
 
  for (var i = 0; i < prefixes.length; i++) {
   try {
    req = new ActiveXObject(prefixes[i] + ".XmlHttp");
    return req;
   } catch (ex) {};
 
  }
 }
} catch (ex) {}
 
alert("XmlHttp Objects not supported by client browser");
}

var http = getHTTPObject();
var http_chave = getHTTPObject();
// Logo após fazer a verificação, é chamada a função e passada 
// o valor à variável global http.

function alimenta_ddd_fax(ddd)
{
registro.ddd_fax.value = ddd.value;
};

