// funções de Apresentação
function sel(obj){
	obj.style.backgroundColor=( (obj.style.backgroundColor=="") ? "#CCCCCC" : "" );
}
// Fim Funções de Apresentação

//Funções para tratamento de erro
function Erro(){
	this.msg = '';
	this.numErro = 0;
	this.isNull = isNull;
	this.isInt = isInt;
	this.comparar = comparar;
	this.validaData = validaData;
	this.validaEmail = validaEmail;
	this.calcDvCnpj = calcDvCnpj;
	this.validaCnpj = validaCnpj;
	this.calcDvCpf = calcDvCpf;
	this.validaCpf = validaCpf;
	this.AdicionaErro = AdicionaErro;
	this.numErros = numErros;
	this.ExibirErros = ExibirErros;
	
	function isNull(obj,nome){
		if(obj.value==''){
			this.msg += 'O campo '+ nome +' não pode ser branco!!! \n';
			this.numErro++;
			return false;
		}else{
			return true;
		}
	}
	
	function isInt(pVal,nome){
		var reTipo = /^\d+$/;
		if(reTipo.test(pVal)){
			return true;	
		}else{
			this.msg += 'O campo '+ nome +' não é um valor numérico válido!!! \n';
			this.numErro++;
			return false;
		}
	}
	
	function comparar(obj1,obj2,nome){
		if(obj1.value == obj2.value){
			return true;
		}else{
			this.msg += "O valor do campo " + nome + " não confere !!! \n";
			this.numErro++;
			return false;
		}
	}
	
	function validaData(pVal){
		var reTipo = /^((0[1-9]|[12]\d)\/(0[1-9]|1[0-2])|30\/(0[13-9]|1[0-2])|31\/(0[13578]|1[02]))\/\d{4}$/;
		if(reTipo.test(pVal)){
			return true;			
		}else{
			this.msg += 'Formato de data inválida!!! \n';
			this.numErro++;
			return false;			
		}
	}
	
	function validaEmail(pVal){
		var reTipo = /^[_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.)+[a-z]{2,3}$/;
		if(reTipo.test(pVal)){
			return true;	
		}else{
			this.msg += 'Formato de e-mail inválido!!! \n';
			this.numErro++;
			return false;
		}
	}
	
	function unformatNumber(pNum){
		return String(pNum).replace(/\D/g, "");
	}

	function calcDvCnpj(cnpj){
		peso = 2;
		soma = 0;
		for(i= cnpj.length -1 ; i >= 0; i--){			
			soma += cnpj.charAt(i) * peso;
			peso = peso < 9 ? peso + 1: 2;
		}
		dv = (11 - (soma % 11)) > 9 ? 0 : 11-(soma % 11);
		if( cnpj.length < 13 ){
			return calcDvCnpj(cnpj + dv);
		}else{
			return cnpj.substring(12,13) + dv;
		}
	}
	
	function validaCnpj(cnpj){
		vcnpj = unformatNumber(cnpj);
		if( (vcnpj.substring(12,14)==calcDvCnpj(vcnpj.substring(0,12))) && (vcnpj.length == 14) ){
			return true;
			window.alert("bla");
		}else{
			this.msg += 'CNPJ inválido!!! \n';
			this.numErro++;
			return false;
		}
	}
	
	function calcDvCpf(cpf){
		peso = cpf.length + 1;
		soma = 0;
		for(i= 0 ; i < cpf.length ; i++ ){			
			soma += cpf.charAt(i) * peso;
			peso--;
		}
		dv = (11 - (soma % 11)) > 9 ? 0 : 11-(soma % 11);
		if( cpf.length < 10 ){
			return calcDvCpf(cpf + dv);
		}else{
			return cpf.substring(9,10) + dv;
		}
	}
	
	function validaCpf(cpf){
		vcpf = unformatNumber(cpf);		
		if( (vcpf.substring(9,11)==calcDvCpf(vcpf.substring(0,9))) && (vcpf.length == 11) ){
			return true;
		}else{
			this.msg += 'CPF inválido!!! \n';
			this.numErro++;
			return false;
		}
	}
	
	function AdicionaErro(msg){
		this.numErro++;
		this.msg += msg;
	}
	
	function numErros(){
		return this.numErro;	
	}
	
	function ExibirErros(){
		window.alert(this.msg);
	}
}
//Fim das Funções para tratamento de erro

// funções para abrir Dialogs
function ProdDet(cod){
	if((cod != '')&&(cod != null)){		
		window.open("produtos_det.php?id=" + cod, "Prod_Det", "scrollbars=1, resizable=0, height=350, width=550,top=200,left=200");		
	}
}

function Noticias(cod){
	if((cod != '')&&(cod != null)){		
		window.open("noticias_det.php?id=" + cod, "Not_Det", "scrollbars=1, resizable=0, height=550, width=700,top=50,left=200");		
	}
}
// Fim Funções para abrir Dialogs

