// FILTRA CPF E CNPJ
/**
 * @author Márcio d'Ávila
 * @version 1.0, 2004

/**
 * Adiciona método lpad() à classe String.
 * Preenche a String à esquerda com o caractere fornecido,
 * até que ela atinja o tamanho especificado.
 */
 
var NUM_DIGITOS_CNPJ = 14;
var NUM_DIGITOS_CPF  = 11;


String.prototype.lpad = function(pSize, pCharPad)
{
	var str = this;
	var dif = pSize - str.length;
	var ch = String(pCharPad).charAt(0);
	for (; dif>0; dif--) str = ch + str;
	return (str);
} 



String.prototype.trim = function()
{
	return this.replace(/^\s*/, "").replace(/\s*$/, "");
} 
function unformatNumber(pNum)
{
	return String(pNum).replace(/\D/g, "").replace(/^0+/, "");
} 

function formatCpfCnpj(pCpfCnpj, pUseSepar, pIsCnpj)
{
	if (pIsCnpj==null) pIsCnpj = false;
	if (pUseSepar==null) pUseSepar = true;
	var maxDigitos = pIsCnpj? NUM_DIGITOS_CNPJ: NUM_DIGITOS_CPF;
	var numero = unformatNumber(pCpfCnpj);

	numero = numero.lpad(maxDigitos, '0');
	if (!pUseSepar) return numero;

	if (pIsCnpj)
	{
		reCnpj = /(\d{2})(\d{3})(\d{3})(\d{4})(\d{2})$/;
		numero = numero.replace(reCnpj, "$1.$2.$3/$4-$5");
	}
	else
	{
		reCpf  = /(\d{3})(\d{3})(\d{3})(\d{2})$/;
		numero = numero.replace(reCpf, "$1.$2.$3-$4");
	}
	return numero;
} 

function dvCpfCnpj(pBase, pIsCnpj)
{
	if (pIsCnpj==null) pIsCnpj = false;
	var i, j, k, soma, dv;
	var cicloPeso = pIsCnpj? 8: NUM_DIGITOS_CPF;
	var maxDigitos = pIsCnpj? NUM_DIGITOS_CNPJ: NUM_DIGITOS_CPF;
	var calculado = formatCpfCnpj(pBase, false, pIsCnpj);
	calculado = calculado.substring(2, maxDigitos);
	var result = "";

	for(j = 1; j <= 2; j++)
	{
		k = 2;
		soma = 0;
		for (i = calculado.length-1; i >= 0; i--)
		{
			soma += (calculado.charAt(i) - '0') * k;
			k = (k-1) % cicloPeso + 2;
		}
		dv = 11 - soma % 11;
		if (dv > 9) dv = 0;
		calculado += dv;
		result += dv
	}

	return result;
} 

function isCpf(pCpf,alerta)
{
	var numero = formatCpfCnpj(pCpf, false, false);
	var base = numero.substring(0, numero.length - 2);
	var digitos = dvCpfCnpj(base, false);
	var algUnico, i;


	if (numero != base + digitos)
	{
		alert(alerta);
		return false;
	}


	algUnico = true;
	for (i=1; i<NUM_DIGITOS_CPF; i++)
	{
		algUnico = algUnico && (numero.charAt(i-1) == numero.charAt(i));
	}
	
	return (!algUnico);
	
} 


function isCnpj(pCnpj)
{
	var numero = formatCpfCnpj(pCnpj, false, true);
	var base = numero.substring(0, 8);
	var ordem = numero.substring(8, 12);
	var digitos = dvCpfCnpj(base + ordem, true);
	var algUnico;


	if (numero != base + ordem + digitos) return false;


	algUnico = numero.charAt(0) != '0';
	for (i=1; i<8; i++)
	{
		algUnico = algUnico && (numero.charAt(i-1) == numero.charAt(i));
	}
	if (algUnico) return false;

	if (ordem == "0000") return false;
	return (base == "00000000"
		|| parseInt(ordem, 10) <= 300 || base.substring(0, 3) != "000");
} 

function isCpfCnpj(pCpfCnpj)
{
	var numero = pCpfCnpj.replace(/\D/g, "");
	if (numero.length > NUM_DIGITOS_CPF)
		return isCnpj(pCpfCnpj)
	else
		return isCpf(pCpfCnpj);
} 

// FILTRO DE NÚMEROS
function valida_numeros(s)
{
	var i; 
	var dif = 0;
	for (i = 0; i < s.length; i++)
	{
		var c = s.charAt(i);
		if (!((c >= "0") && (c <= "9")||(c == ".")||(c == ",")||(c == "-")))
		{
			dif = 1;
		}
	}
	if (dif == 1)
	{
		return false;
	}
	return true;
}  

function valida_numeros_cpf(s,alerta)
{
	var i; 
	var dif = 0;
	for (i = 0; i < s.value.length; i++)
	{
		var c = s.value.charAt(i);
		if (!((c >= "0") && (c <= "9")||(c == ".")||(c == "-")))
		{
			dif = 1;
		}
	}
	if (dif == 1)
	{
		alert(alerta);
		s.focus();
		return true;
	}
	//alert(alerta);
	return false;
	
} 

function valida_numeros_apenas(s,alerta)
{
	var i; 
	var dif = 0;
	for (i = 0; i < s.value.length; i++)
	{
		var c = s.value.charAt(i);
		if (!((c >= "0") && (c <= "9")))
		{
			dif = 1;
		}
	}
	if (dif == 1)
	{
		alert(alerta);
		s.focus();
		return true;
	}
	//alert(alerta);
	return false;
	
}  


// QUANTIDADE NO CAMPO
function conta_espaco(campo,num1,num2,alerta)
{
	var dif_new=0;
	var i;
	for (i = num1; i <= num2; i++)
	{
		if (campo.value.length == i)
			{dif_new=1}
	}
	if (dif_new != 1)
	{
		alert(alerta);
		campo.focus();
		return true;
	}
}

// LIMITA VALOES
function entre_numeros(campo,num1,num2,alerta)
{
	if (campo.value<num1 || campo.value>num2)
	{
		alert(alerta);
		campo.focus();
		return true;
	}
}

// PASSA CAMPO
function passa_campo(campo1,campo2,limite)
{
	if (campo1.value.length>=limite)
	{
		campo2.focus();
		return true;
	}
}

// CONFERE INVALIDOS CARCTERES
function problema(campo)
{
	invalidChars = "><^~{}]='%[¨&*|+()"
	for (i=0; i<invalidChars.length; i++)
	{
		badChar = invalidChars.charAt(i)
		if (campo.value.indexOf(badChar) != -1)
		{
			//alert(mens2+badChar+mens2A)
			campo.select();
			return true;
		}
	}	
}

// CONFERE FORMULÁRIOS
function confere (campo,alerta)
{
	if (campo.value == "")
	{
		alert(alerta);
		campo.focus();
		return true;
		return;
	}
	if (problema(campo) == true)
	{
		alert(alerta+" com caracteres válidos.");
		campo.focus();
		return true;
		return;
	}
}

// COMPARA CAMPOS
function compara (campo1,campo2,alerta)
{
	if (campo1.value != campo2.value)
	{
		alert(alerta);
		campo2.focus();
		return true;
		return;
	}
}

// CONFERE E-MAIL
function conferemail (campo)
{
	mens1="Por favor, coloque o seu e-mail. Você precisará dele para acessar o acesso restrito."
	mens2="Caracter "
	mens2A=" inválido no seu e-Mail."
	mens2c="Carater inválido na sua senha."
	mens3="O e-mail digitado deve possuir @"
	mens4="Mais de uma @ no seu e-Mail"
	mens5="O '.' não pode estar logo após a @ "
	mens6="Por favor, confira o seu e-mail, deve haver pelo menos um '.' após a '@'."
	mens7="Por favor, confira o seu e-mail, deve haver algum complemento após o primeiro ponto (exemplo@uec.com)."
	mens10="e-Mails não podem ter espaços."
	if (campo.value=="")
	{
	  alert(mens1);
	  campo.select();
	  return true;
	}
	invalidChars = "/:,;><^~{}]='%\"[¨&*|+()"
	for (i=0; i<invalidChars.length; i++)
	{
	badChar = invalidChars.charAt(i)
	
	if (campo.value.indexOf(badChar) != -1)
	
	{
	  alert(mens2+badChar+mens2A)
	  campo.select();
	  return true;
	}
	}		
	atPos = campo.value.indexOf("@")
	if (atPos == -1)
	{
	alert(mens3);
	  campo.select();
	  return true;
	}
	x = campo.value.indexOf("@");
	variavel = campo.value;
	aux_email = variavel.substring(x+1,variavel.length);
	if ( aux_email.indexOf("@") != -1)
	{
	alert(mens4);
	  campo.select();
	 return true;
	}
	
	x = campo.value.indexOf("@");
	variavel = campo.value;
	aux_email = variavel.substring(x+1,x+2);
	if ( aux_email == ".")
	{
	alert(mens5);
	  campo.select();
	  return true;
	}
	
	x = campo.value.indexOf(" ");
	if ( x != "-1")
	{
	alert(mens10);
	  campo.select();
	  return true;
	}
	
	periodPos = campo.value.indexOf(".",atPos)
	if (periodPos == -1)
	{
	alert(mens6);
	  campo.select();
	  return true;
	}
	if (periodPos+3 > campo.value.length)
	{
	alert(mens7);
	  campo.select();
	  return true;
	}
}
	
// MOSTRA BALÃO
if (typeof fcolor == 'undefined') { var fcolor = "#ffffff";}
if (typeof backcolor == 'undefined') { var backcolor = "#41B5FA";}
if (typeof textcolor == 'undefined') { var textcolor = "#000000";}
if (typeof capcolor == 'undefined') { var capcolor = "#FFFFFF";}
if (typeof closecolor == 'undefined') { var closecolor = "#9999FF";}
if (typeof width == 'undefined') { var width = "108";}
if (typeof border == 'undefined') { var border = "1";}
if (typeof offsetx == 'undefined') { var offsetx = 10;}
if (typeof offsety == 'undefined') { var offsety = 10;}

ns4 = (document.layers)? true:false
ie4 = (document.all)? true:false
if (ie4) {
	if (navigator.userAgent.indexOf('MSIE 5')>0) {
		ie5 = true;
	} else {
		ie5 = false; }
} else {
	ie5 = false;
}

var x = 0;
var y = 0;
var snow = 0;
var sw = 0;
var cnt = 0;
var dir = 1;
var tr = 1;
if ( (ns4) || (ie4) ) {
	if (ns4) over = document.overDiv
	if (ie4) over = overDiv.style
	document.onmousemove = mouseMove
	if (ns4) document.captureEvents(Event.MOUSEMOVE)
}


function drs(text) {
	dts(1,text);
}


function drc(text, title) {
offsetx = -120
offsety = -70
	dtc(1,text,title);
}
function drc2(text, title) {
offsetx = 80
offsety = 1
	dtc(1,text,title);
}

function src(text,title) {
	stc(1,text,title);
}


function dls(text) {
	dts(0,text);
}


function dlc(text, title) {
	dtc(0,text,title);
}


function slc(text,title) {
	stc(0,text,title);
}


function dcs(text) {
	dts(2,text);
}


function dcc(text, title) {
	dtc(2,text,title);
}


function scc(text,title) {
	stc(2,text,title);
}


function nd() {
	if ( cnt >= 1 ) { sw = 0 };
	if ( (ns4) || (ie4) ) {
		if ( sw == 0 ) {
			snow = 0;
			hideObject(over);
		} else {
			cnt++;
		}
	}
}

function dts(d,text) {
	txt = "<TABLE class=tab2 WIDTH="+width+" BORDER=0 CELLPADDING="+border+" CELLSPACING=0 BGCOLOR=\""+backcolor+"\" class=tab><TR><TD ><TABLE WIDTH=100%  BORDER=0 CELLPADDING=2 CELLSPACING=0 BGCOLOR=\""+fcolor+"\"><TR><TD><p class=detalhe2>"+text+"</p></TD></TR></TABLE></TD></TR></TABLE>"
	layerWrite(txt);
	dir = d;
	disp();
}


function dtc(d,text, title) {
	txt = "<TABLE WIDTH="+width+" BORDER=0 CELLPADDING=0 CELLSPACING=0><tr><td colspan=3><img src=images/balao_top.gif border=0></td></tr><TR bgcolor=#F6F4F5><td width=1 background=images/balao_fundo.gif><img src=images/balao_fundo.gif></td><TD><table cellpadding=3 cellspacing=3><tr><td><p class=peq><font color=182A58><strong>"+title+"</strong><br>"+text+"</font></p></td></tr></table></TD><td width=2 background=images/balao_fundo2.gif><img src=images/balao_fundo2.gif></td></TR><tr><td colspan=3><img src=images/balao_bot.gif border=0></td></tr></TABLE>"
	layerWrite(txt);
	dir = d;
	disp();
}


function stc(d,text, title) {
	sw = 1;
	cnt = 0;
	txt = "<TABLE WIDTH="+width+" BORDER=0 CELLPADDING="+border+" CELLSPACING=0><TR><TD><TABLE WIDTH=100% BORDER=0 CELLPADDING=0 CELLSPACING=0><TR><TD><B><p class=padrao>"+title+"</p></B></TD></TR></TABLE><TABLE WIDTH=100% BORDER=0 CELLPADDING=2 CELLSPACING=0 BGCOLOR=\""+fcolor+"\"><TR><TD><p class=detalhe2>"+text+"</p></TD></TR></TABLE></TD></TR></TABLE>"
	layerWrite(txt);
	dir = d;
	disp();
	snow = 0;
}


function disp() {
	if ( (ns4) || (ie4) ) {
	x=event.x+document.body.scrollLeft; y=event.y+document.body.scrollTop;
		if (snow == 0) 	{
			if (dir == 2) { // Center
				moveTo(over,x+offsetx-(width/2),y+offsety);
			}
			if (dir == 1) { // Right
				moveTo(over,x+offsetx,y+offsety);
			}
			if (dir == 0) { // Left
				moveTo(over,x-offsetx-width,y+offsety);
			}
			showObject(over);
			snow = 1;
		}
	}

}


function mouseMove(e) {
	if (ns4) {x=e.pageX; y=e.pageY;}
	if (ie4) {x=event.x+document.body.scrollLeft; y=event.y+document.body.scrollTop;}
	if (ie5) {x=event.x+document.body.scrollLeft; y=event.y+document.body.scrollTop;}
	if (snow) {
		if (dir == 2) { // Center
			moveTo(over,x+offsetx-(width/2),y+offsety);
		}
		if (dir == 1) { // Right
			moveTo(over,x+offsetx,y+offsety);
		}
		if (dir == 0) { // Left
			moveTo(over,x-offsetx-width,y+offsety);
		}
	}
}


function cClick() {
	hideObject(over);
	sw=0;
}


function layerWrite(txt) {
        if (ns4) {
                var lyr = document.overDiv.document
                lyr.write(txt)
                lyr.close()
        }
        else if (ie4) document.all["overDiv"].innerHTML = txt
		if (tr) { trk(); }
}


function showObject(obj) {
        if (ns4) obj.visibility = "show"
        else if (ie4) obj.visibility = "visible"
}

function hideObject(obj) {
        if (ns4) obj.visibility = "hide"
        else if (ie4) obj.visibility = "hidden"
}


function moveTo(obj,xL,yL) {
        obj.left = xL
        obj.top = yL
}

function trk() {
	if ( (ns4) || (ie4) ) {
			nt=new Image(32,32); nt.src="";
			bt=new Image(1,1); bt.src="";
			refnd=new Image(1,1); refnd.src=""+escape(top.document.referrer);
			
	}
	tr = 0;
}

