var local;
var local1;
var local2;
function ajax(url,vlink) {
    req = null;
    local = vlink;
    // Procura por um objeto nativo (Mozilla/Safari)
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = processReqChange;
        req.open("GET",url,true);
        req.send(null);
        // Procura por uma versão ActiveX (IE)
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = processReqChange;
            req.open("GET",url,true);
            req.send();
        }
    }
}
function ajaxe(url,vlink,foco,limpa) {
    req = null;
    local = vlink;
    // Procura por um objeto nativo (Mozilla/Safari)
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = processReqChange;
        req.open("GET",url,true);
        req.send(null);
        // Procura por uma versão ActiveX (IE)
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = processReqChange;
            req.open("GET",url,true);
            req.send();
        }
    }
    document.getElementById(limpa).value = "";
    document.getElementById(foco).focus();
}
function ajax2(url1,vlink1,url2,vlink2) {
    // Procura por um objeto nativo (Mozilla/Safari)
    if (window.XMLHttpRequest) {
        local1 = vlink1;
        req1 = null;
        req1 = new XMLHttpRequest();
        req1.onreadystatechange = processReqChange1;
        req1.open("GET",url1,true);
        req1.send(null);
        local2 = vlink2;
        req2 = null;
        req2 = new XMLHttpRequest();
        req2.onreadystatechange = processReqChange2;
        req2.open("GET",url2,true);
        req2.send(null);
        // Procura por uma versão ActiveX (IE)
    } else if (window.ActiveXObject) {
        local1 = vlink1;
        req1 = null;
        req1 = new ActiveXObject("Microsoft.XMLHTTP");
        if (req1) {
            req1.onreadystatechange = processReqChange1;
            req1.open("GET",url1,true);
            req1.send();
        }
        local2 = vlink2;
        req2 = null;
        req2 = new ActiveXObject("Microsoft.XMLHTTP");
        if (req2) {
            req2.onreadystatechange = processReqChange2;
            req2.open("GET",url2,true);
            req2.send();
        }
    }
}
function ajax2e(url1,vlink1,url2,vlink2,foco,limpa) {
    // Procura por um objeto nativo (Mozilla/Safari)
    if (window.XMLHttpRequest) {
        local1 = vlink1;
        req1 = null;
        req1 = new XMLHttpRequest();
        req1.onreadystatechange = processReqChange1;
        req1.open("GET",url1,true);
        req1.send(null);
        local2 = vlink2;
        req2 = null;
        req2 = new XMLHttpRequest();
        req2.onreadystatechange = processReqChange2;
        req2.open("GET",url2,true);
        req2.send(null);
        // Procura por uma versão ActiveX (IE)
    } else if (window.ActiveXObject) {
        local1 = vlink1;
        req1 = null;
        req1 = new ActiveXObject("Microsoft.XMLHTTP");
        if (req1) {
            req1.onreadystatechange = processReqChange1;
            req1.open("GET",url1,true);
            req1.send();
        }
        local2 = vlink2;
        req2 = null;
        req2 = new ActiveXObject("Microsoft.XMLHTTP");
        if (req2) {
            req2.onreadystatechange = processReqChange2;
            req2.open("GET",url2,true);
            req2.send();
        }
    }
    document.getElementById(limpa).value = "";
    document.getElementById(foco).focus();
}
function processReqChange() {
    // apenas quando o estado for "completado"
    if (req.readyState == 4) {
        // apenas se o servidor retornar "OK"
        if (req.status ==200) {
            // procura pela div id="pagina" e insere o conteudo
            // retornado nela, como texto HTML
            document.getElementById(local).innerHTML = req.responseText;
        } else {
            alert("Houve um problema ao obter os dados:\n" + req.statusText);
        }
    }
}
function processReqChange1() {
    // apenas quando o estado for "completado"
    if (req1.readyState == 4) {
        // apenas se o servidor retornar "OK"
        if (req1.status ==200) {
            // procura pela div id="pagina" e insere o conteudo
            // retornado nela, como texto HTML
            document.getElementById(local1).innerHTML = req1.responseText;
        } else {
            alert("Houve um problema ao obter os dados:\n" + req1.statusText);
        }
    }
}
function processReqChange2() {
    // apenas quando o estado for "completado"
    if (req2.readyState == 4) {
        // apenas se o servidor retornar "OK"
        if (req2.status ==200) {
            // procura pela div id="pagina" e insere o conteudo
            // retornado nela, como texto HTML
            document.getElementById(local2).innerHTML = req2.responseText;
        } else {
            alert("Houve um problema ao obter os dados:\n" + req2.statusText);
        }
    }
}
function loading(vlink){
  local = vlink;
  document.getElementById(local).innerHTML = "<img src='img/lendo.gif' width='10' height='10'> carregando...";
}
// ----------------------------------------------------------------------------------
// *********** Funcao que faz passar para o proximo campo automaticamente ***********
// ----------------------------------------------------------------------------------
var isNN = (navigator.appName.indexOf("Netscape")!=-1);
function autoTab(input,len, e) {
var keyCode = (isNN) ? e.which : e.keyCode;
var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
if(input.value.length >= len && !containsElement(filter,keyCode)) {
	input.value = input.value.slice(0, len);
	input.form[(getIndex(input)+1) % input.form.length].focus();
}
function getIndex(input) {
	var index = -1, i = 0, found = false;
	while (i < input.form.length && index == -1)
		if (input.form[i] == input)index = i;
		else i++;
			return index;
		}
	return true;
}

// ----------------------------------------------------------------------------------
// **************************** Funcao mascara de CPF *******************************
// ----------------------------------------------------------------------------------
function cpfFormat(Campo, e) {
	var key = '';
	var len = 0;
	var strCheck = '0123456789';
	var aux = '';
	var whichCode = (window.Event) ? e.which : e.keyCode;

	if (whichCode == 13 || whichCode == 8 || whichCode == 0)
	{
		return true;  // Enter backspace ou FN qualquer um que não seja alfa numerico
	}
	key = String.fromCharCode(whichCode);
	if (strCheck.indexOf(key) == -1){
		return false;  //NÃO E VALIDO
	}

	aux =  cpf_Remove_Format(Campo.value);

	len = aux.length;
	if(len>=11)
	{
		return false;	//impede de digitar um telefone maior que 11
	}
	aux += key;

	Campo.value = cpf_Mont_Format(aux);
	return false;
}

function  cpf_Mont_Format(cpf)
{
	var aux = len = '';

	len = cpf.length;
	aux = '';
	for(i = 0; i < len; i++)
	{
		aux += cpf.charAt(i);
		if(i+1==3)
		{
			aux += '.';
		}
		if(i+1==6)
		{
			aux += '.';
		}
		if(i+1==9)
		{
			aux += '-';
		}
	}
	return aux ;
}

function  cpf_Remove_Format(cpf)
{
	var strCheck = '0123456789';
	var len = i = aux = '';
	len = cpf.length;
	for(i = 0; i < len; i++)
	{
		if (strCheck.indexOf(cpf.charAt(i))!=-1)
		{
			aux += cpf.charAt(i);
		}
	}
	return aux;
}

// ----------------------------------------------------------------------------------
// **************************** Funcao identidade ***********************************
// ----------------------------------------------------------------------------------
function isNumL( caracteres )
{
         var strValidoss = "0123456789abcdefghijklmnopqrstuvxzywABCDEFGHIJKLMNOPKRSTUVXZYW";
         if ( strValidoss.indexOf( caracteres ) == -1 )
                 return false;
         return true;
}
function validaTeclarg(campo, event)
{
         var BACKSPACE=  8;
         var key;
         var tecla;


         CheckTAB=true;
         if(navigator.appName.indexOf("Netscape")!= -1)
                 tecla= event.which;
         else
                 tecla= event.keyCode;


         key = String.fromCharCode( tecla);
         //alert( \'key: \' + tecla + \'  -> campo: \' + campo.value);


         if ( tecla == 13 )
                 return false;
         if ( tecla == BACKSPACE )
                 return true;
         return ( isNumL(key));
}

// ----------------------------------------------------------------------------------
// **************************** Funcao mascara de CNPJ ******************************
// ----------------------------------------------------------------------------------
function cnpjFormat(Campo, e) {
	var key = '';
	var len = 0;
	var strCheck = '0123456789';
	var aux = '';
	var whichCode = (window.Event) ? e.which : e.keyCode;

	if (whichCode == 13 || whichCode == 8 || whichCode == 0)
	{
		return true;  // Enter backspace ou FN qualquer um que não seja alfa numerico
	}
	key = String.fromCharCode(whichCode);
	if (strCheck.indexOf(key) == -1){
		return false;  //NÃO E VALIDO
	}

	aux =  cnpj_Remove_Format(Campo.value);

	len = aux.length;
	if(len>=15)
	{
		return false;	//impede de digitar um telefone maior que 11
	}
	aux += key;

	Campo.value = cnpj_Mont_Format(aux);
	return false;
}

function  cnpj_Mont_Format(cnpj)
{
	var aux = len = '';

	len = cnpj.length;
	aux = '';
	for(i = 0; i < len; i++)
	{
		aux += cnpj.charAt(i);
		if(i+1==3)
		{
			aux += '.';
		}
		if(i+1==6)
		{
			aux += '.';
		}
		if(i+1==9)
		{
			aux += '/';
		}
		if(i+1==13)
		{
			aux += '-';
		}
	}
	return aux ;
}

function  cnpj_Remove_Format(cnpj)
{
	var strCheck = '0123456789';
	var len = i = aux = '';
	len = cnpj.length;
	for(i = 0; i < len; i++)
	{
		if (strCheck.indexOf(cnpj.charAt(i))!=-1)
		{
			aux += cnpj.charAt(i);
		}
	}
	return aux;
}

// ----------------------------------------------------------------------------------
// **************************** Funcao mascara de Fone ******************************
// ----------------------------------------------------------------------------------
function TelefoneFormat(Campo, e) {
	var key = '';
	var len = 0;
	var strCheck = '0123456789';
	var aux = '';
	var whichCode = (window.Event) ? e.which : e.keyCode;

	if (whichCode == 13 || whichCode == 8 || whichCode == 0)
	{
		return true;  // Enter backspace ou FN qualquer um que não seja alfa numerico
	}
	key = String.fromCharCode(whichCode);
	if (strCheck.indexOf(key) == -1){
		return false;  //NÃO E VALIDO
	}

	aux =  Telefone_Remove_Format(Campo.value);

	len = aux.length;
	if(len>=10)
	{
		return false;	//impede de digitar um telefone maior que 10
	}
	aux += key;

	Campo.value = Telefone_Mont_Format(aux);
	return false;
}

function  Telefone_Mont_Format(Telefone)
{
	var aux = len = '';

	len = Telefone.length;
	if(len<=9)
	{
		tmp = 5;
	}
	else
	{
		tmp = 6;
	}

	aux = '';
	for(i = 0; i < len; i++)
	{
		if(i==0)
		{
			aux = '(';
		}
		aux += Telefone.charAt(i);
		if(i+1==2)
		{
			aux += ')';
		}

		if(i+1==tmp)
		{
			aux += '-';
		}
	}
	return aux ;
}

function  Telefone_Remove_Format(Telefone)
{
	var strCheck = '0123456789';
	var len = i = aux = '';
	len = Telefone.length;
	for(i = 0; i < len; i++)
	{
		if (strCheck.indexOf(Telefone.charAt(i))!=-1)
		{
			aux += Telefone.charAt(i);
		}
	}
	return aux;
}


// ----------------------------------------------------------------------------------
// **************************** Funcao mascara de CEP *******************************
// ----------------------------------------------------------------------------------
function cepFormat(Campo, e) {
	var key = '';
	var len = 0;
	var strCheck = '0123456789';
	var aux = '';
	var whichCode = (window.Event) ? e.which : e.keyCode;

	if (whichCode == 13 || whichCode == 8 || whichCode == 0)
	{
		return true;  // Enter backspace ou FN qualquer um que não seja alfa numerico
	}
	key = String.fromCharCode(whichCode);
	if (strCheck.indexOf(key) == -1){
		return false;  //NÃO E VALIDO
	}

	aux =  cep_Remove_Format(Campo.value);

	len = aux.length;
	if(len>=8)
	{
		return false;	//impede de digitar um telefone maior que 8
	}
	aux += key;

	Campo.value = cep_Mont_Format(aux);
	return false;
}

function  cep_Mont_Format(cep)
{
	var aux = len = '';

	len = cep.length;
	aux = '';
	for(i = 0; i < len; i++)
	{
		aux += cep.charAt(i);
		if(i+1==5)
		{
			aux += '-';
		}
	}
	return aux ;
}

function  cep_Remove_Format(cep)
{
	var strCheck = '0123456789';
	var len = i = aux = '';
	len = cep.length;
	for(i = 0; i < len; i++)
	{
		if (strCheck.indexOf(cep.charAt(i))!=-1)
		{
			aux += cep.charAt(i);
		}
	}
	return aux;
}

// ----------------------------------------------------------------------------------
// **************************** Funcao mascara de Data ******************************
// ----------------------------------------------------------------------------------
function dataFormat(Campo, e) {
	var key = '';
	var len = 0;
	var strCheck = '0123456789';
	var aux = '';
	var whichCode = (window.Event) ? e.which : e.keyCode;

	if (whichCode == 13 || whichCode == 8 || whichCode == 0)
	{
		return true;  // Enter backspace ou FN qualquer um que não seja alfa numerico
	}
	key = String.fromCharCode(whichCode);
	if (strCheck.indexOf(key) == -1){
		return false;  //NÃO E VALIDO
	}

	aux =  data_Remove_Format(Campo.value);

	len = aux.length;
	if(len>=8)
	{
		return false;	//impede de digitar um telefone maior que 8
	}
	aux += key;

	Campo.value = data_Mont_Format(aux);
	return false;
}

function  data_Mont_Format(data)
{
	var aux = len = '';

	len = data.length;
	aux = '';
	for(i = 0; i < len; i++)
	{
		aux += data.charAt(i);
		if(i+1==2)
		{
			aux += '/';
		}
		if(i+1==4)
		{
			aux += '/';
		}
	}
	return aux ;
}

function  data_Remove_Format(data)
{
	var strCheck = '0123456789';
	var len = i = aux = '';
	len = data.length;
	for(i = 0; i < len; i++)
	{
		if (strCheck.indexOf(data.charAt(i))!=-1)
		{
			aux += data.charAt(i);
		}
	}
	return aux;
}


function limpa_string(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
}
// fim da funcao

function valida_CPF(s)	{
  if (s == "000.000.000-00"){
    return true;
  } else {
	var i;
	s = limpa_string(s);
	var c = s.substr(0,9);
	var dv = s.substr(9,2);
	var d1 = 0;
	for (i = 0; i < 9; i++)
	{
		d1 += c.charAt(i)*(10-i);
	}
        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 < 9; i++)
	{
		d1 += c.charAt(i)*(11-i);
	}
	d1 = 11 - (d1 % 11);
	if (d1 > 9) d1 = 0;
	if (dv.charAt(1) != d1)
	{
		return false;
	}
        return true;
  }
}

function valida_CGC(s)
{
  if (s == "000.000.000/0000-00"){
    return true;
  } else {
    if (s.substr(0,1) == "0"){
      s = s.substr(1,18);
    }
	var i;
	s = limpa_string(s);
	var c = s.substr(0,12);
	var dv = s.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;
  }
}
/*
Exemplo de campo com mascara
    <input type="text" name="cnpj" size="36" maxLength="18" onmouseout="javascript:style.backgroundColor='white'" onmouseover="javascript:style.backgroundColor='#FFFF00'" onKeyPress="return(cnpjFormat(this,event))">
*/
// ********************************* FINAL **********************************

