window.FuncoesGerais = {

	//abre um popup
	abrir_popup : function (url, altura, largura){
		window.open(url, "LevelUp", "width="+largura+" height="+altura+" menubar=no resizable=no scrollbars=yes status=yes toolbar=no top=200 left=200"); 
	},
	
	//troca imagens do menu principal
	SwapMenu : function (name,image){
		//verifica imagem clicada e substitui
		if (document.getElementById('lbl_img')){
			img = document.getElementById('lbl_img').value;
			if (image!=img){
				if (document.images){ 
					document.images[name].src = image;
				}
			}
		}else{
			document.images[name].src = image;
		}
	},
	
	//esconde a div de imagem
	SomeImagem : function(objeto){
	    objeto.style.display='none';
	},
	
	//busca parametro na url
	BuscaParametro : function (parametro){
		valor = '';
		parametros = window.location.search.substr(1);
		array_par = parametros.split('&');
		//loop passa pelo array de parametros para checar se existe o parametro solicitado
		for(c=0;c<array_par.length;c++){
			array_valor = array_par[c].split('=');
			if(array_valor[0]==parametro){
				valor = array_valor[1];
			}
		}
		return valor;
	},
	
	//mostra o nome da pagina atual
	RetornaNomePagina : function (){
		url = window.location.href;
		parametros = window.location.search;
		url = url.replace(parametros,'');
		url_array = url.split('/');
		url_tamanho = url_array.length;
		url_array2 = url_array[url_tamanho-1].split('#');
		return url_array2[0];
	},
	
	//mostra a url atual
	RetornaURL : function (){
		url = window.location.href;
		url_array = url.split('/');
		url_tamanho = url_array.length;
		return url_array[url_tamanho-1];
	},

	//tab automatico entre os campos
	tab_campo : function(nome_campo_ta,nome_campo_vai,tamanho){
		if (document.getElementById(nome_campo_ta).value.length==tamanho)
			document.getElementById(nome_campo_vai).focus();
	},
	
	//valida preenchimento de radio button
	ValidaRadio : function(campo){
	
		tamanho = campo.length;
		valor = 0;
		for(c=0;c<tamanho;c++){
			if(campo[c].checked)
				valor = 1;
		}
		
		return valor;
	
	},
	
	//retorna radio button selecionado
	RetornaValorRadio : function(campo){
	
		tamanho = campo.length;
		valor = '';
		for(c=0;c<tamanho;c++){
			if(campo[c].checked)
				valor = campo[c].value;
		}
		
		return valor;
	
	},
	
	SelecionaValorRadio : function(campo,valor){
	
		tamanho = campo.length;
		for(c=0;c<tamanho;c++){
			if(campo[c].value ==valor ){
				campo[c].checked = true;
				}
				
		}
		

	},
	
	//retorna check box selecionado
	RetornaValorCheckUnico : function(campo){
		valor = 0;
		if(campo.checked)
			valor = 1;
				
		return valor;
	
	},
	
	//retorna check box selecionado
	RetornaValorCheck : function(campo){
		valor = '';
		if(campo.checked)
			valor = campo.value;
				
		return valor;
	
	},
	
	//substitui valor nulo por zero
	TrocaVazio : function(valor){
	
		if (valor=='')
			return 0;
		else if(isNaN(valor))
			return 0;
		else
			return valor;
	},
	
	//valida preenchimento de e-mail
	ValidaEmail : function(email){
	
		if ((email.indexOf("@") == -1) || (email.indexOf(".") == -1))
			return 0;
		else
			return 1;
	
	},
	
	//valida preenchimento de data
	ValidaDataSeparada : function(dia,mes,ano){
	
		retorno=1;
		if(dia=='' || mes=='' || ano==''){
			retorno=0;
		}else if(isNaN(dia) || isNaN(mes) || isNaN(ano)){
			retorno=0;
		}else if(dia<1 || dia>31){
			retorno=0;
		}else if(mes<1 || mes>12){
			retorno=0;
		}else if(ano<1900 || ano>3000){
			retorno=0;
		}
	
		return retorno;
	},
	
	//formata data para o padrão sql
	FormataData : function(dia,mes,ano){
		if (dia=='' || mes=='' || ano=='')
			data = '1900-1-1';
		else
			data  = ano + '-' + mes + '-' + dia;
		return data;
	},
	
	//carrega combo de estados	
	CarregaEstado : function(){
		Class_cidades.carregaestados(this.CarregaEstado_CallBack);
	},
	
	//carrega combo de estados
	CarregaEstado_CallBack : function(response){
		if(response.error == null){
			nomecampoestado = document.getElementById("NomeCampoEstado").value;
			var retorno = response.value;
			var ds = retorno;
			if(ds!=null && typeof(ds) == "object" && ds.Tables!=null){
				//define a quantidade de itens do dropdown(1 a mais da quantidade de itens do dataset)
				document.getElementById(nomecampoestado).length =ds.Tables[0].Rows.length+1;
				
				//adiciona um item no dropdown
				document.getElementById(nomecampoestado).options[0].text= 'Selecione';
				document.getElementById(nomecampoestado).options[0].value= '';
				
				//adiciona os itens do dataset no dropdown
				for(var i=0; i<ds.Tables[0].Rows.length; i++){
					var row = ds.Tables[0].Rows[i];
					document.getElementById(nomecampoestado).options[i+1].text= row.uf;
					document.getElementById(nomecampoestado).options[i+1].value= row.uf;
				}
			} 
		}	
	},
	
	//carrega combo de estados
	CarregaEstadoNSelecione : function(){
		Class_cidades.carregaestados(this.CarregaEstado_CallBackNSelecione);
		this.CarregaCidade('AC');
	},
	
	//carrega combo de estados
	CarregaEstado_CallBackNSelecione : function(response){
		if(response.error == null){
			nomecampoestado = document.getElementById("NomeCampoEstado").value;
			var retorno = response.value;
			var ds = retorno;
			if(ds!=null && typeof(ds) == "object" && ds.Tables!=null){
				//define a quantidade de itens do dropdown(1 a mais da quantidade de itens do dataset)
				document.getElementById(nomecampoestado).length =ds.Tables[0].Rows.length;
				
				//adiciona os itens do dataset no dropdown
				for(var i=0; i<ds.Tables[0].Rows.length; i++){
					var row = ds.Tables[0].Rows[i];
					document.getElementById(nomecampoestado).options[i].text= row.uf;
					document.getElementById(nomecampoestado).options[i].value= row.uf;
				}
			} 
		}	
	},
	
	//carrega combo de cidades
	CarregaCidade : function(estado){
		if(estado.length==2)
		Class_cidades.Seleciona(estado,0,this.CarregaCidade_CallBack);
	},
	
	valida_dados_digitados : function(ponto,event){
		if(navigator.appName.indexOf("Netscape")!= -1) 
			tecla= event.which; 
		else 
			tecla= event.keyCode; 
  
		var pKey = String.fromCharCode(tecla);
	
		if(navigator.appName.indexOf("Netscape")!= -1 && tecla==8)
		{
			return(true);
		}
		
		if (ponto==' '){
			var blnRet = ("0123456789".indexOf(pKey)>=0);
		}else if (ponto=='.'){
			var blnRet = ("\/:*?<>. '".indexOf(pKey)<0);
		}else if (ponto=='-'){
			var blnRet = ("0123456789-".indexOf(pKey)>=0);
		}else if (ponto=='/'){
			var blnRet = ("0123456789/".indexOf(pKey)>=0);
		}
		return(blnRet);
	},
	
	//carrega combo de cidades
	CarregaCidade_CallBack : function(response){
		if(response.error == null){ 
			nomecampocidade = document.getElementById("NomeCampoCidade").value;
			var retorno = response.value;
			var ds = retorno;
			
			if(ds!=null && typeof(ds) == "object" && ds.Tables!=null){
				//define a quantidade de itens do dropdown(1 a mais da quantidade de itens do dataset)
				document.getElementById(nomecampocidade).length =ds.Tables[0].Rows.length;
				
				//adiciona os itens do dataset no dropdown
				for(var i=0; i<ds.Tables[0].Rows.length; i++){
					var row = ds.Tables[0].Rows[i];
					document.getElementById(nomecampocidade).options[i].text= row.cidade;
					document.getElementById(nomecampocidade).options[i].value= row.cidade;
				}
			} 
		} 
	},
	
	msgExclui : function(msg, classe, id_item){
		if (confirm(msg)){
			categoria='';
			resposta = classe.exclui(id_item).value;
			alert(resposta);
			if (document.getElementById('DropCategoria')){
				categoria = '&categoria=' + document.getElementById('DropCategoria').value;
			}
			window.location.href = window.location.href + categoria;
		}
	},
	
	msgExcluiGaleria : function(msg, classe, id_item, flg){
		if (confirm(msg)){
			categoria='';
			resposta = classe.exclui(id_item, flg).value;
			alert(resposta);
			if (document.getElementById('DropCategoria')){
				categoria = '&categoria=' + document.getElementById('DropCategoria').value;
			}
			window.location.href = window.location.href + categoria;
		}
	},
	
	AlteraDisplay : function(elemento,status){
		document.getElementById(elemento).style.display = status;
	},
	
	AlteraTexto : function(elemento,texto){
		document.getElementById(elemento).innerHTML = texto;
	},
	
	//Função para validação de CPF
	checaCPF : function(field){
	var cpf = '';
	var digito = '';
	var temp = '';
	var i = 0;
	var j = 0;
	var soma = 0;
	var mt = 0;
	var dg = 0;

	if (field == '')  
	{
		return false; 
	}   
	else
	{
		cpf = field;
	} 
  
  
	for(j = 0; j < cpf.length; j++) 
	{
		if (cpf.charAt(j)>= '0' && cpf.charAt(j) <= '9') temp += cpf.charAt(j);
	}
	cpf = temp;
	if (cpf.length != 11) 
	{
		return false;
	}

	// Testa se o CPF não tem todos os dígitos repetidos.
	if((cpf%11111111111) == 0)
	return false;

	cpf = temp.substring(0,9);
	digito = temp.substring(9,11);
	for (j = 1; j <= 2; j++) 
	{
		soma = 0;
		mt = 2;
		for (i = 8 + j; i >= 1; i--) 
		{
			soma += parseInt(cpf.charAt(i-1),10) * mt;
			mt++;
		}
		dg = 11 - (soma % 11);
		if (dg > 9) {dg = 0};
		cpf += dg;
	}
	
	if (digito != cpf.substring(9,11)){
		return false;}
	else{
		return true;}
	}
	//Fim da Função para validação de CPF

}
