// JavaScript Document
/* Empresa : O Sul - Rede Pampa;
   Autor: João Fabian;
   Data Criação: 18/02/2008;
*/
//Método para formatar valores para decimal;
function formatarDecimal(valor){
	var valor = valor.toString().replace(/\$|\,/g,"");
	if(isNaN(valor)){
		valor = 0;
	}
	cents = Math.floor((valor*100+0.5)%100);
	valor = Math.floor((valor*100+0.5)/100).toString();
	if(cents < 10){
		cents = "0"+cents;
	}
	for(var x=0; x<Math.floor((valor.length-(1+x))/3); x++){
		valor = valor.substring(0,valor.length-(4*x+3))+"."+valor.substr(valor.length-(4*x+3));
	}
	return valor+","+cents;
}
//Método para somar valores passados por array;
function somarValores(array){
	var resultado = 0;
	for(var x=0; x < sizeof(array); x++){
		resultado = resultado + array(x);
	}
	resultado = formatarDecimal(resultado);
	return resultado;
}
//Método para validação de dados
function validarDados(dado,tipo){
	//dado = dado a ser validado
	//tipo = tipo de dado a ser validado
	// 0 - Alfanumérico;
	// 1 - Numérico em Branco;
	// 2 - Numérico, inclusive zero;
	// 3 - Data;
	if(tipo == 0){
		if(dado != ''){
			return true;
		}
	}else if(tipo == 1){
		if(dado.length > 0){
			if(isNan(dado)){
				return true;
			}
		}
	}else if(tipo == 2){
		if(isNaN(dado)){
			return true;
		}
	}else if(tipo == 3){
		var vld = /^((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(vld.test(dado)){
			return true;
		}
	}else{
		return false;
	}
}
function abreJanela(nome_janela,caminho,params){
	nome_janela = window.open(caminho,nome_janela,params);
}
function display(obj,controle){
	if(controle == 'mostra'){
		document.getElementById(obj).style.display = '';
	}else if(controle == 'esconde'){
		document.getElementById(obj).style.display = 'none';
	}
}
function muda_div(obj1,obj2){
	if(document.getElementById(obj1).style.display == 'none'){
		document.getElementById(obj1).style.display = '';
		document.getElementById(obj2).style.display = 'none';
	}else if(document.getElementById(obj1).style.display == ''){
		document.getElementById(obj1).style.display = 'none';
		document.getElementById(obj2).style.display = '';
	}
}
function manda_div(obj1,obj2){
	document.getElementById(obj1).innerHTML = document.getElementById(obj2).innerHTML;
}
function mascaraData(evt,campo){
	var valor = campo.value;
	var tam = valor.length;
	if((evt.which >= 48 && evt.which <= 57) || (evt.which >= 96 && evt.which <= 105) && tam < 11){
		if(tam == 2 || tam == 5){
			valor = valor+'/';
		}
	}else{
		valor = valor.substr(0,tam-1);	
	}
	campo.value = valor;
}
function mascara(evt, campo, tipo){
	var valor = campo.value;
	var tam = valor.length;
	if(tipo == 'data'){
		if(((evt.which >= 48 && evt.which <= 57) || (evt.which >= 96 && evt.which <= 105)) && tam < 11){
			if(tam == 2 || tam == 5){
				valor = valor+'/';
			}
		}else{
			valor = valor.substr(0,tam-1);	
		}
	}else if(tipo == 'telefone'){
		if(((evt.which >= 48 && evt.which <= 57) || (evt.which >= 96 && evt.which <= 105)) && tam < 12){
			if(tam == 2){
				valor = valor+'-';
			}
		}else{
			valor = valor.substr(0,tam-1);	
		}
	}else if(tipo == 'cep'){
		if(((evt.which >= 48 && evt.which <= 57) || (evt.which >= 96 && evt.which <= 105)) && tam < 10){
			if(tam == 5){
				valor = valor+'-';
			}
		}else{
			valor = valor.substr(0,tam-1);	
		}
	}else if(tipo == 'cpf'){
		if(((evt.which >= 48 && evt.which <= 57) || (evt.which >= 96 && evt.which <= 105)) && tam < 13){
			if(tam == 9){
				valor = valor+'-';
			}
		}else{
			valor = valor.substr(0,tam-1);	
		}
	}else if(tipo == 'cnpj'){
		if(((evt.which >= 48 && evt.which <= 57) || (evt.which >= 96 && evt.which <= 105)) && tam < 19){
			if(tam == 2 || tam == 6){
				valor = valor+'.';
			}
			if(tam == 10){
				valor = valor+'/';
			}
			if(tam == 15){
				valor = valor+'-';
			}
		}else{
			valor = valor.substr(0,tam-1);	
		}
	}
	campo.value = valor;
}
function hover(id,controle,img,img_hover){
	var campo = document.getElementById(id);
	if(controle == 'mostra'){
		campo.src = img_hover;	
	}else{
		campo.src = img;
	}
}
function muda_modulo(caminho){
	document.getElementById("frame_principal").src = '../php/'+caminho+'.php';
}
function expande(obj,id){
	if(document.getElementById(obj).style.display == 'none'){
		document.getElementById(obj).style.display = '';
		document.getElementById('expande_'+id).src = '../imagens/site_bt_menos.jpg';
		document.getElementById('pasta_'+id).src = '../imagens/site_bt_pasta_aberta.jpg';
	}else if(document.getElementById(obj).style.display == ''){
		document.getElementById(obj).style.display = 'none';
		document.getElementById('expande_'+id).src = '../imagens/site_bt_mais.jpg';
		document.getElementById('pasta_'+id).src = '../imagens/site_bt_pasta_fechada.jpg';
	}
}
function trim(str){
	return str.replace(/^\s+|\s+$/g,"");
}
function fundoTabela(controle,nro_tabela,nro_tr){
	var tabela = document.getElementsByTagName("table");
	var tr = tabela[nro_tabela].getElementsByTagName("tr");
	var tds = tr[nro_tr].getElementsByTagName("td");
	if(controle == 'mostrar'){
		for(var i=0; i < tds.length; i++){
			tds[i].className = 'fundo_td';	
		}
	}else if(controle == 'esconder'){
		for(var i=0; i < tds.length; i++){
			tds[i].className = '';	
		}
	}else if(controle == 'clique'){
		for(var i=0; i < tds.length; i++){
			tds[i].style.background = '#FFFFAE';
		}
	}else if(controle == 'duplo_clique'){
		for(var i=0; i < tds.length; i++){
			tds[i].style.background = '';
		}
	}else if(controle == 'titulo'){
		for(var i=1; i < tr.length; i++){
			tds = tr[i].getElementsByTagName("td");
			tds[nro_tr].style.background = '#cadfff';
		}
	}else if(controle == 'titulo2'){
		for(var i=1; i < tr.length; i++){
			tds = tr[i].getElementsByTagName("td");
			tds[nro_tr].style.background = '';
		}
	}
	
}
//verifica se a imagem existe
function imagemExiste(url){
        var tmp=new Image;
        tmp.src=url;
        if(tmp.height>0){
            return true;
        }else{
            return false;
        }
}
function localizacao(caminho){
	if(caminho != ''){
		window.location.href = caminho;
	}else{
		window.location.href = '../php/index_site.php';
	}
}
function localizacao_frame(caminho){
	if(caminho != ''){
		phpTroca.location.href = caminho;
	}else{
		phpTroca.location.href = '../php/index_site.php';
	}
}
function botao_direito(){ 
    alert ("Esta função está desabilitada"); 
    return false;
}
function display2(obj){
	if(document.getElementById(obj).style.display == 'none'){
		document.getElementById(obj).style.display = '';
	}else if(document.getElementById(obj).style.display == ''){
		document.getElementById(obj).style.display = 'none';
	}
}
function up_foto(valor,form){
	document.forms[form].action = '../php/up_foto.php';
	document.forms[form].submit();
	if(form == 'form_depoimentos'){
		document.forms[form].action = '../php/recebe_depoimentos.php';
	}
	if(form == 'form_universo'){
		document.forms[form].action = '../php/recebe_universo.php';
	}
}

function Geral(){
	//document.oncontextmenu = botao_direito;
	this.formatarDecimal = formatarDecimal;
	this.somarValores = somarValores;
	this.validarDados = validarDados;
	this.abreJanela = abreJanela;
	this.display = display;
	this.muda_div = muda_div;
	this.mascara = mascara;
	this.hover = hover;
	this.muda_modulo = muda_modulo;
	this.expande = expande;
	this.trim = trim;
	this.fundoTabela = fundoTabela;
	this.imagemExiste = imagemExiste;
	this.localizacao = localizacao;
	this.localizacaoFrame = localizacao_frame;
	this.botaoDireito = botao_direito;
	this.mandaDiv = manda_div;
	this.display2 = display2;
	this.upFoto = up_foto;
}