// Marcelo Roberto PrEsSi - ®Editora Glasberg

function checkBrowser(){
	this.ver=navigator.appVersion
	this.dom=document.getElementById?1:0
	this.ie6=(this.ver.indexOf("MSIE 6")>-1 && this.dom)?1:0;
	this.ie5=(this.ver.indexOf("MSIE 5")>-1 && this.dom)?1:0;
	this.ie4=(document.all && !this.dom)?1:0;
	this.ns5=(this.dom && parseInt(this.ver) >= 5) ?1:0;
	this.ns4=(document.layers && !this.dom)?1:0;
	this.bw=(this.ie6 || this.ie5 || this.ie4 || this.ns4 || this.ns5)
	this.ie=(this.ie6 || this.ie5 || this.ie4)
	return this
}
bw = new checkBrowser()

var preencha = "Preencha o campo ";
var emailerrado = "Voce digitou um endereço de e-mail inválido";
var numeroerrado = "Apenas caracters numéricos são aceitos";
var dataerrado = "Data inválida";
var anterior = "tabLayer0";
var proximo;
var tab_;
var tab_prev = "tab0";
var win_categoria = null;
var win_usuario = null;
var undefined;

function popup(endereco,largura,altura,nomejanela,comscrolling,jan,comresize) {
	if (comscrolling)
		scrolling = "yes";
	else
		scrolling = "no";

	if (comresize)
		resizi = "yes";
	else
		resizi = "no";

    var attributes = "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=" + scrolling + ",resizable=" + resizi;
//    var attributes = "screenX=0,screenY=0,left=0,top=0,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no";
    attributes += ",width=" + largura + ",height=" + altura;
    attributes += ", screenX=" + ((screen.width-largura)/2) + ",screenY=" + ((screen.height-altura)/2);
    attributes += ", left=" + ((screen.width-largura)/2) + ",top=" + ((screen.height-altura)/2);
	if(jan == 1) {
		if (win_usuario == null || win_usuario.closed)
	    	win_usuario = window.open(endereco, nomejanela, attributes);
	    else
	    	win_usuario.location.href = endereco;
	    win_usuario.focus();
		return win_usuario;
	} else if(jan == 2) {
		if (win_categoria == null || win_categoria.closed)
	    	win_categoria = window.open(endereco, nomejanela, attributes);
	    else
	    	win_categoria.location.href = endereco;
	    win_categoria.focus();
		return win_categoria;
	} else {
		var Janela = window.open(endereco, nomejanela, attributes);
		Janela.location.href = endereco;
		Janela.focus();
	}
}

function redimenciona(Flag) {
	var Tamanho_Altura = document.body.offsetHeight - (Flag?32:10);
	var TopBotoes = Tamanho_Altura + 2;
	var Tamanho_Largura = document.body.offsetWidth - 2;

	var Mostrar = "visible";
	if (Tamanho_Altura <= 0) {
		Tamanho_Altura = 0;
		Mostrar = "hidden";
	}
	if (Tamanho_Largura <= 0) {
		Tamanho_Largura = 0;
		Mostrar = "hidden";
	}

	document.all.divPrincipal.style.visibility = "hidden";
	document.all.divPrincipal.style.clip = "rect(0px " + Tamanho_Largura + "px " + Tamanho_Altura + "px 0px)";
	document.all.divPrincipal.style.height = Tamanho_Altura;
	document.all.divPrincipal.style.width = Tamanho_Largura;
	document.all.divPrincipal.style.visibility = Mostrar;

	if (Flag) {
		document.all.divBotoes.style.visibility = "hidden";
		document.all.divBotoes.style.pixelTop = TopBotoes;
		document.all.divBotoes.style.clip = "rect(0px " + Tamanho_Largura + "px 30px 0px)";
		document.all.divBotoes.style.width = Tamanho_Largura;
		document.all.divBotoes.style.height = 30;
		document.all.divBotoes.style.visibility = Mostrar;
	}
}

function Testa_Texto(campo, msg) {
	if (campo.value.match(/\S+/) == null) {
		alert(preencha + msg);
		campo.select();
		campo.focus();
		return false;
	}
	else
		return true;
}

function Testa_Numeros(campo, msg, obrigatorio) {
	if (campo.value.match(/\S+/) == null) {
		if (obrigatorio) {
			alert(preencha + msg);
			campo.select();
			campo.focus();
			return false;
		}
	}
	else {
		if (campo.value.match(/^[\d-.\/]+$/) == null) {
			alert(numeroerrado);
			campo.select();
			campo.focus();
			return false;
		}
	}

	return true;
}

function Testa_email(campo, obrigatorio) {
	if (campo.value.match(/\S+/) == null) {
		if (obrigatorio) {
			alert(preencha + "e-mail");
			campo.select();
			campo.focus();
			return false;
		}
	}
	else {
		if (campo.value.match(/^[\w-._]+@[\w-._]+$/) == null) {
			alert(emailerrado);
			campo.select();
			campo.focus();
			return false;
		}
	}
	return true;
}

function algumescolhido(campo) {
	for(var a=0; a<campo.length; a++) {
		if (campo[a].checked)
			return true;
	}
	return false;
}

function Testa_Combos(combo, msg) {
	if (combo.selectedIndex == 0) {
		alert(preencha + msg);
		combo.focus();
		return false;
	} else
		return true;
}


function Testa_Datas(campo, msg)
{
	var errada = false;

	if (campo.value.match(/\S+/) == null) {
		alert(preencha + msg);
		campo.select();
		campo.focus();
		return false;
	}

	if (campo.value.match(/\d{2}\/\d{2}\/\d{4}/) == null) {
		alert(dataerrado);
		campo.select();
		campo.focus();
		return false;
	}

	var Data = campo.value.split("/");
	var Dia = parseInt(Data[0], 10);
	var Mes = parseInt(Data[1], 10);
	var Ano = parseInt(Data[2], 10);

	if ((Dia > 0 && Dia < 32) && (Mes > 0 && Mes < 13) && (Ano > 1990)) {
		var Bissexto = ((Ano % 4) == 0);
		var FevDia = 28;

		switch( Mes ) {
			case 4:
			case 6:
			case 9:
			case 11:
				if ( Dia > 30 )
					errada = true;
				break;
			case 2:
				if (Bissexto)
					FevDia = 29;
				if ( Dia > FevDia )
					errada = true;
				break;
			default:
		}
	}
	else
		errada = true;

	if (errada) {
		alert(dataerrado);
		campo.select();
		campo.focus();
		return false;
	}

	return true;
}

function dataatual(campo) {
	Hoje = new Date();

	campo.value = dois_digitos(Hoje.getDate()) + "/" + dois_digitos((Hoje.getMonth() + 1)) + "/" + Hoje.getFullYear();
}

function dois_digitos(valor) {
	var texto = valor.toString();

	if (texto.length == 1)
		return "0" + texto;
	else
		return texto;
}

function Devolve_Objeto_Data(campo) {
	var novadata = new Date();
	var temp = campo.value.split("/");

	novadata.setDate(temp[0]);
	novadata.setMonth(temp[1]);
	novadata.setFullYear(temp[2]);

	return novadata;
}

function Ajuda(msg) {
	help.innerText = msg;
}

//função para validar CNPJ-CGC ou CPF
function Testa_CPF_CNPJ(campo, msg) {
	if (!checa(campo.value, msg + ' inválido')) {
		campo.select();
		campo.focus();
		return false;
	}
	return true;
}

function checa(msCPF_CGC, msMSG){
	if ((msCPF_CGC.length != 14) && (msCPF_CGC.length != 11)) {
		alert(msMSG);
		return false;
	}
	for (var a=0;a<=9;a++) {
		if (msCPF_CGC.match(a + "{11}")) {
			alert(msMSG);
			return false;
		}
	}
	if ((!(modulo(msCPF_CGC.substring(0,msCPF_CGC.length - 2)).toString()+modulo(msCPF_CGC.substring(0,msCPF_CGC.length - 1)).toString() == msCPF_CGC.substring(msCPF_CGC.length - 2,msCPF_CGC.length))) && (modulo_cic(msCPF_CGC.substring(0,msCPF_CGC.length - 2)) + '' + modulo_cic(msCPF_CGC.substring(0,msCPF_CGC.length - 1)) != msCPF_CGC.substring(msCPF_CGC.length - 2,msCPF_CGC.length))){
		alert(msMSG);
		return false;
	}
	return true;
}

function modulo(msCPF_CGC){
	soma=0;
	ind=2;

	for(pos=msCPF_CGC.length-1;pos>-1;pos=pos-1){
		soma = soma + (parseInt(msCPF_CGC.charAt(pos)) * ind);
		ind++;

		if(msCPF_CGC.length>11){
			if(ind>9) ind=2;
		}
	}

	resto = soma - (Math.floor(soma / 11) * 11);

	if(resto < 2){
		return 0;
	}
	else{
		return (11 - resto);
	}
}

function modulo_cic(msCPF_CGC){
	soma=0;
	ind=2;

	for(pos=msCPF_CGC.length-1;pos>-1;pos=pos-1){
		 soma = soma + (parseInt(msCPF_CGC.charAt(pos)) * ind);
		 ind++;

		 if(msCPF_CGC.length>11){
			if(ind>9) ind=2;
		 }
	}

	resto = soma - (Math.floor(soma / 11) * 11);

	if(resto < 2){
		return 0;
	}
	else{
		return 11 - resto;
	}
}

function Fixa_Duas_Casas(valor, Idioma) {

	var temp = new String(valor);
	var resultado = temp.match(/^(\d+)((\.)(\d{1,2}))?/);
	var Decimais = resultado[4];
	//var Pontuacao = resultado[3];

	if(Decimais.length == 1)
		Decimais = Decimais + "0";

	if(Idioma == "P")
		Pontuacao = ",";
	else
		Pontuacao = ".";

	if(Decimais == '')
		Decimais = "00";

	return resultado[1] + Pontuacao + Decimais;
}

function Numeros_Int(valor) {
	if (valor.match(/^\d+$/) != null) {
		var novo = parseInt(valor, 10);
		if(novo != 0)
			return novo;
	}

	return '';
}

function Numeros_Float(valor) {
	var novo_valor = valor.replace(/\,/g,'.');

	if (novo_valor.match(/^\d+(\.\d+)?$/) != null) {
		var novo = parseFloat(novo_valor);
		if(novo != 0)
			return novo;
	}

	return '';
}
