window.FuncoesGerais = {

	recuperaMenorValor : function(arrayNumerico){
		var valorInicial = arrayNumerico[0];
		for(i=1;i<arrayNumerico.length;i++){
			if(valorInicial>arrayNumerico[i])
				valorInicial=arrayNumerico[i];
		}
		return valorInicial;
	},
	recuperaMaiorValor : function(arrayNumerico){
		var valorInicial = arrayNumerico[0];
		for(i=1;i<arrayNumerico.length;i++){
			if(valorInicial<arrayNumerico[i])
				valorInicial=arrayNumerico[i];
		}
		return valorInicial;
	},

	//abre um popup
	abrir_popup : function (url, altura, largura){
		window.open(url, "CidadeDoChopp", "width="+largura+" height="+altura+" menubar=no resizable=no scrollbars=no status=yes toolbar=no top=200 left=200"); 
	},
	
	replaceMaster : function(valor,caracter,substituir){
		for(;valor.indexOf(caracter)!=-1;)
				valor = valor.replace(caracter,substituir);
		return valor;
	},
	
	//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){
	    document.getElementById(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 (nome_campo_ta.value.length==tamanho)
			nome_campo_vai.focus();
	},
	
	Max_caracteres : function(txarea,limite){
		tam = txarea.value.length; 
		str=""; 
		str=str+tam; 
		
		if (tam > limite){ 
			aux = txarea.value; 
			txarea.value = aux.substring(0,limite); 
		} 
	},
	
	teste : 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 = ("0123456789.,".indexOf(pKey)>=0);
		}else if (ponto=='-'){
			var blnRet = ("0123456789-".indexOf(pKey)>=0);
		}else if (ponto=='/'){
			var blnRet = ("0123456789/".indexOf(pKey)>=0);
		}
		return(blnRet);
	},
	
	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=='x'){
			var blnRet = ("0123456789".indexOf(pKey)<0);
		}else if (ponto=='.'){
			var blnRet = ("0123456789.,".indexOf(pKey)>=0);
		}else if (ponto=='-'){
			var blnRet = ("0123456789-".indexOf(pKey)>=0);
		}else if (ponto=='/'){
			var blnRet = ("0123456789/".indexOf(pKey)>=0);
		}
		return(blnRet);
	},
	
	//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);
	},
	
	
	//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;
				}
			} 
		} 
	
	},
	
	//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
	
	
	AlteraDisplay : function(elemento,status){
		document.getElementById(elemento).style.display = status;
	},
	
	AlteraVisibility: function(elemento,status){
		document.getElementById(elemento).style.visibility = status;
	},
	
	AlteraTexto : function(elemento,texto){
		document.getElementById(elemento).innerHTML = texto;
	},
	
	SomaTexto : function(elemento,texto){
		document.getElementById(elemento).innerHTML += texto;
	},
	
	AlteraCorFundo : function(elemento,cor){
		document.getElementById(elemento).style.backgroundColor = cor;
	},
	
	AlteraClasse : function(elemento,classe){
		document.getElementById(elemento).className = classe;
	},
	
	AlteraSrc : function(elemento,src){
		document.getElementById(elemento).src = src;
	},
	
	AssinalaChecks : function(elemento,valor){
		Checks = document.getElementById(elemento).getElementsByTagName('input');
		quantidade = Checks.length;
		for(a=0;a<quantidade;a++){
			if(Checks[a].value==valor)
				Checks[a].checked = true;
		}
	},
	
	ValidaChecks : function(elemento){
		Checks = document.getElementById(elemento).getElementsByTagName('input');
		quantidade = Checks.length;
		for(a=0;a<quantidade;a++){
			if(Checks[a].checked==true)
				return 1;
		}
		return 0;
	},
	
	ConcatenaChecks : function(elemento){
		Checks = document.getElementById(elemento).getElementsByTagName('input');
		quantidade = Checks.length;
		ValorFinal = '';
		for(a=0;a<quantidade;a++){
			if(Checks[a].checked==true){
				if(ValorFinal=='')
					ValorFinal = Checks[a].value;
				else
					ValorFinal += ',' + Checks[a].value;
			}
		}
		return ValorFinal;
	},
	
	CarregaDropCidade : function(estado,objeto){
		if(estado!=''){
			response = Class_cidades.Seleciona(estado,0);
			if(response.error == null){
				objeto.length = 0;
				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)
					objeto.length =ds.Tables[0].Rows.length+1;
					
					//adiciona um item no dropdown
					objeto.options[0].text= 'Selecione';
					objeto.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];
						objeto.options[i+1].text= row.cidade;
						objeto.options[i+1].value= row.cidade;
					}
				} 
			}
		}
	},
	
	CarregaFlash : function(caminho,largura,altura){
		document.write('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="'+largura+'" height="'+altura+'">');
		document.write('<param name="movie" value="'+caminho+'">');
		document.write('<param name="quality" value="high">');
		document.write('<param name="wmode" value="transparent">');
		document.write('<param name="menu" value="false">');
		document.write('<embed wmode="transparent" src="'+caminho+'" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="'+largura+'" height="'+altura+'"></embed>');
		document.write('</object>');
	},
	
	CarregaFlash2 : function(caminho,largura,altura,elemento){
		var conteudo = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"  codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="'+largura+'" height="'+altura+'" name="flashtop">';
		conteudo += '<param name="movie" value="'+caminho+'">';
		conteudo += '<param name="quality" value="high">';
		conteudo += '<param name="wmode" value="transparent">';
		conteudo += '<param name="menu" value="false">';
		conteudo += '<param name="scale" value="noscale">';
		conteudo += '<param name="salign" value="t" />';
		conteudo += '<param name="allowScriptAccess" value="always" />';
		conteudo += '<embed name="flashtop" wmode="transparent" src="'+caminho+'" scale="noscale" salign="t" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" allowScriptAccess="always" width="'+largura+'" height="'+altura+'"></embed>';
		conteudo += '</object>';
		FuncoesGerais.AlteraTexto(elemento,conteudo);
	},
	
	CarregaFlash3 : function(caminho,largura,altura,elemento){
		var conteudo = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"  codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="'+largura+'" height="'+altura+'" name="flashtop">';
		conteudo += '<param name="movie" value="'+caminho+'">';
		conteudo += '<param name="quality" value="high">';
		conteudo += '<param name="wmode" value="transparent">';
		conteudo += '<param name="menu" value="false">';
		conteudo += '<param name="scale" value="noscale">';
		conteudo += '<param name="salign" value="t" />';
		conteudo += '<param name="allowScriptAccess" value="always" />';
		conteudo += '<embed wmode="transparent" name="flashtop" src="'+caminho+'" scale="noscale" salign="t" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" allowScriptAccess="always" width="'+largura+'" height="'+altura+'"></embed>';
		conteudo += '</object>';
		FuncoesGerais.AlteraTexto(elemento,conteudo);
	}

}

