//////////////////////////////////////////////////////////
/////////////////funcao p/ jump menu//////////////////////
//////////////////////////////////////////////////////////
function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}

////////////////////////////////////////////////////////////
/////// funcao p/ abrir popup no centro do navegador ///////
////////////////////////////////////////////////////////////
function centerPopup(mypage, myname, w, h, scroll) {
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;
	winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll;
	win = window.open(mypage, myname, winprops)
	if (parseInt(navigator.appVersion) >= 4){
		win.window.focus();
	}
}

///////////////////////////////////////////////////////
/////////////// função para as divs ///////////////////
///////////////////////////////////////////////////////

function MM_reloadPage(init){
	if (init==true) with (navigator) {
		if ((appName=="Netscape")&&(parseInt(appVersion)==4)){
			document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage;
		}
	} else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH){
		location.reload();
	}
}
MM_reloadPage(true);

////////////////////////////////////////////////////
/////// funcao p/ abrir o site em tela cheia ///////
////////////////////////////////////////////////////
function OpenMeFull(pagina){
	remote = window.open(pagina, 'janela_openFull', 'toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=no,top=0,left=0');
	remote.resizeTo(window.screen.availWidth, window.screen.availHeight);
}


//////////////////////////////////////////////
//////// função para pular de campo //////////
//////////////////////////////////////////////
function saltaCampo(obj){
	if(obj.value.length>=obj.maxLength){        
		proximo(obj);
		return false;
	}
    return true;
}
function proximo(obj){
    var elementos = document.getElementsByTagName('INPUT');
    for(var i=0;i<elementos.length;i++){
        if(elementos[i].name==obj.name){
            while(elementos[i+1]!=null){
                if(!elementos[i+1].disabled && elementos[i+1].tabIndex>=0){
                    elementos[i+1].focus();
                    return;
                }
                i++;
            }
        }
    }
}

//////////////////////////////////////////////
//// função para digitar apenas números //////
//////////////////////////////////////////////
function bloqueia_alfa(e){
navegador = /msie/i.test(navigator.userAgent);
if (navegador)
	var tecla = event.keyCode;
else
	var tecla = e.which;
	if(tecla > 47 && tecla < 58) // numeros de 0 a 9
		return true;
	else{
	if (tecla != 8) // backspace
		return false;
	else
		return true;    
	}
}


//////////////////////////////////////////////
//////// função para formatar campos /////////
//////////////////////////////////////////////
function formataCampos(e,src, mask) {
	
	evt = /msie/i.test(navigator.userAgent);
	bloqueia_alfa(evt);
		
		var i = src.value.length;
		var saida = mask.substring(0,1);
		var texto = mask.substring(i)
		
		if (texto.substring(0,1) != saida){
			src.value += texto.substring(0,1);
		}
		{
		if (evt < 45 || evt > 57) evt.returnValue = false;
		}
}


///////////////////////////////////////////////////////
/////// funções para formatar campos de valor /////////
///////////////////////////////////////////////////////
function Limpar(valor, validos){
	// retira caracteres invalidos da string
	var result = "";
	var aux;
	for (var i=0; i < valor.length; i++){
		aux = validos.indexOf(valor.substring(i, i+1));
		if (aux>=0){
			result += aux;
		}
	}
	return result;
}

function Formata(campo,tammax,teclapres,decimal){
	var tecla = teclapres.keyCode;
	vr = Limpar(campo.value,"0123456789");
	tam = vr.length;
	dec=decimal

	if (tam < tammax && tecla != 8){
		tam = vr.length + 1;
	}

	if (tecla == 8){
		tam = tam - 1;
	}

	if ( tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 ){

		if ( tam <= dec ){
			campo.value = vr;
		}

		if ( (tam > dec) && (tam <= 5) ){
			campo.value = vr.substr( 0, tam - 2 ) + "," + vr.substr( tam - dec, tam );
		}
		
		if ( (tam >= 6) && (tam <= 8) ){
			campo.value = vr.substr( 0, tam - 5 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - dec, tam ); 
		}

		if ( (tam >= 9) && (tam <= 11) ){
			campo.value = vr.substr( 0, tam - 8 ) + "." + vr.substr( tam - 8, 3 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - dec, tam );
		}

		if ( (tam >= 12) && (tam <= 14) ){
			campo.value = vr.substr( 0, tam - 11 ) + "." + vr.substr( tam - 11, 3 ) + "." + vr.substr( tam - 8, 3 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - dec, tam );
		}
		
		if ( (tam >= 15) && (tam <= 17) ){
			campo.value = vr.substr( 0, tam - 14 ) + "." + vr.substr( tam - 14, 3 ) + "." + vr.substr( tam - 11, 3 ) + "." + vr.substr( tam - 8, 3 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - 2, tam );
		}
	}
}


//////////////////////////////////////////////////////////
////////////////função para as divs///////////////////////
//////////////////////////////////////////////////////////
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_showHideLayers() { //v6.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
    obj.visibility=v; }
}
//-->

/////////////////////////////////////////////////////
//////// função para validar o campo de CPF /////////
/////////////////////////////////////////////////////
function Verifica_campo_CPF(campo) {
	var cpf = campo.value; // Recebe o valor digitado no campo
	cpf = cpf. replace (".","");
	cpf = cpf. replace (".","");
	cpf = cpf. replace ("-","");

	// Aqui começa a checagem do CPF
	var POSICAO, I, SOMA, DV, DV_INFORMADO;
	var DIGITO = new Array(10);
	DV_INFORMADO = cpf.substr(9, 2); // Retira os dois últimos dígitos do número informado

	// Desemembra o número do CPF na array DIGITO
	for (I=0; I<=8; I++) {
		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 valores dos dígitos verificadores conferem
	DV = DIGITO[9] * 10 + DIGITO[10];
	if (DV != DV_INFORMADO) {
		alert('O CPF informado é inválido!');
		campo.value = '';
		return false;
	}
	if (campo.value == '00000000000') {
		alert('O CPF informado é inválido!');
		campo.value = '';
		return false;
	}
}



//////////////////////////////////////////////////////
//////// função para validar o campo de CNPJ /////////
//////////////////////////////////////////////////////
function validaCNPJ() {
	CNPJ = document.getElementById("cnpj").value;
	erro = new String;

/*
	if (CNPJ.length < 18) 
		erro += "É necessario preencher corretamente o número do CNPJ! \n\n";

	if ((CNPJ.charAt(2) != ".") || (CNPJ.charAt(6) != ".") || (CNPJ.charAt(10) != "/") || (CNPJ.charAt(15) != "-")){
		if (erro.length == 0)
			erro += "É necessário preencher corretamente o número do CNPJ! \n\n";
	}
*/

	//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/;
	if (nonNumbers.test(CNPJ)) erro += "A verificacao de CNPJ suporta apenas numeros! \n\n"; 
	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])){
		erro +="CNPJ invalido!";
	}
	if (erro.length > 0){
		alert(erro);
		document.getElementById("cnpj").value = "";
		return false;
	}

	if (document.getElementById("cnpj").value == "00.000.000/0000-00"){
		alert("CNPJ invalido!");
		document.getElementById("cnpj").value = "";
		return false; 
	}

	return true;
}

