//Verifica o navegador
var IE = (navigator.userAgent.indexOf("MSIE") > -1);

window.status = 'Clubinho Sabesp';

//exibirConteudo(): exibe um conteúdo
//	- id: id do conteudo a exibir
function exibirConteudo(id){			
	document.getElementById(id).style.display = "block";
}

//ocultarConteudo(): oculta um conteúdo
//	- id: id do conteudo a exibir		
function ocultarConteudo(id){
	document.getElementById(id).style.display = "none";
}

var conteudoVisivel = '';

//trocarConteudo(): troca o conteúdo visivel por outro
//	-id: id do conteúdo a exibir
function trocarConteudo(id){
	if(conteudoVisivel.length > 0) ocultarConteudo(conteudoVisivel);
	
	exibirConteudo(id);
	conteudoVisivel = id;
}

//trocarConteudoClique(): troca o conteúdo visivel por outro. Caso o conteudo em questão ja esteja visivel, oculta
//	-id: id do conteúdo a exibir
function trocarConteudoClique(id){
	if(conteudoVisivel.length > 0) ocultarConteudo(conteudoVisivel);
	if(id != conteudoVisivel){
		exibirConteudo(id);
		conteudoVisivel = id;
	}else conteudoVisivel = '';
}

if (!IE)
	window.captureEvents(Event.MOUSEMOVE);

//Função para arrastar a div
calcular = true;
function arrastar(idDiv){
	var d = document.getElementById(idDiv);
	document.onmousemove = function(e){			
		var pX = (IE)?event.clientX + document.body.scrollLeft : e.pageX;
		var pY = (IE)?event.clientY + document.body.scrollTop : e.pageY;
		if(calcular){
			difX = pX - d.offsetLeft;
			difY = pY - d.offsetTop;
			(IE)? d.style.filter = 'alpha(opacity=80)' : d.style.opacity = '0.8';
				
			calcular = false;				
		}
		d.style.left = pX - difX + 'px';
		d.style.top = pY - difY + 'px';
	}	

	document.onselectstart = null;

	document.onmouseup = function(){
		(IE)? d.style.filter = 'alpha(opacity=100)' : d.style.opacity = '1';
		document.onmousemove = document.onselectstart = null;
		calcular = true;
	}	
}

//logoff(): faz o logoff do site
function logoff(){
	if( window.confirm('Tem certeza que deseja sair?') ){
		location.href = '/clubinho_sabesp/cadastro/logoff.asp';
	}
}

//enviarAmigo(): preenche e submete o formulário de envie a um amigo
function enviarAmigo(){
	var formulario = document.getElementById('envieAmigo');
	
	if (document.title.indexOf('#') > - 1)
		formulario.titulo.value = document.title.substring(document.title.lastIndexOf("- ") + 1, document.title.indexOf('#'));
	else
		formulario.titulo.value = document.title.substring(document.title.lastIndexOf("- ") + 1);
	
	formulario.link.value = location.href;
	formulario.submit();
}

//validaNum(): bloqueia a digitação de letras
//	- input: input a bloquear
//	- e: evento
function validaNum(input, e){
	if (!e) var e = window.event; 
	//captura o código da tecla digitada
	var code = (e.keyCode) ? e.keyCode : e.which;
	code = parseInt(code);
	
	//caracteres permitidos
	if(((code >= 48) && (code <= 57)) || ((code >= 96) && (code <= 105)))
		return true;
	//excecões permitidas (enter, backspace, delete, setas, etc..)
	else if(code != 8 && code != 46 && code != 37 && code != 9 && code != 39 && code != 36 && code != 35)
		return false;
}


//pularCampo(): troca o foco do input para o próximo do form, quando ele atingir o tamanho limite
//	- objeto: input a verificar
function pularCampo(objeto){
	if(objeto.value.length == objeto.maxLength){
		var campos = document.getElementsByTagName('input');
		for(var i = 0; i < campos.length; i++){
			if(objeto.name == campos[i].name){
				campos[i + 1].focus();
				break;
			}
		}
	}
}

//validarNumeroCaracteres(): bloqueia a digitação em um campo de texto quando ele atinge o tamanho maximo, e mostra os restantes em um span
function validarNumeroCaracteres(campo, maximo, spanId) {
	if (campo.value.length <= maximo)
		document.getElementById(spanId).innerHTML = maximo - campo.value.length;
	else
		campo.value = campo.value.substring(0, maximo);
}

//ResetarForm(): limpa todos os campos de um formulário
function ResetarForm(form){
	var campos = form.elements;	
	if(confirm("Tem certeza que deseja limpar o formulário?")){
		for (var i = 0; i < campos.length; i++) {
			if((campos[i].type.toUpperCase() == "TEXT") || (campos[i].type.toUpperCase() == "TEXTAREA"))
				campos[i].value = "";
			if(campos[i].type.toUpperCase() == "CHECKBOX" || campos[i].type.toUpperCase() == "RADIO")
				campos[i].checked = false;
			if(campos[i].type.toUpperCase() == "SELECT-ONE")
				campos[i].value = "";
			if(campos[i].type.toUpperCase() == "PASSWORD")
				campos[i].value = "";
			if(campos[i].type.toUpperCase() == "FILE"){
				campos[i].value = "";
				if (IE)
					campos[i].outerHTML = campos[i].outerHTML;
			}
		}
	}
}

//CarregarImagem(): Carrega uma foto em um elemento html. Exibe o 'carregando' enquanto a imagem não for carregada
//	- src: caminho da imagem
//	- idAlvo: id do elemento onde a imagem deve ser inserida
//	- [w e h]: dimensões máximas que a imagem vai ter ao carregar
function CarregarImagem(src, idAlvo, w, h){
	
	var alvo = document.getElementById(idAlvo);
	
	alvo.parentNode.style.width = '200px';
	alvo.parentNode.style.height = '150px';
	
	CentralizarConteudo(alvo.parentNode);
	
	//Cria o objeto da imagem
	var foto = new Image();	
	
	//atribui o caminho		
	foto.src = src;
	
	//Caso a foto ja esteja carregada, insere na id
	if(foto.complete){
		InserirFoto();
	}else{
		//Caso a foto nao estiver carregada, exibe o 'carregando' e insere a foto no onload
		document.getElementById(idAlvo).innerHTML = '<br style="line-height:5px;" />&nbsp;carregando...<br /><br style="line-height:5px;" />';
		foto.onload = function(){
			InserirFoto();
		}
	}
	//sub-função que insere a foto no elemento html
	function InserirFoto(){
		
		//armazena as dimensões originais da foto
		var _width = foto.width;
		var _height = foto.height
		
		//caso exista um tamanho a redimensionar, e uma das dimensões da foto ultrapasse o tamanho máximo, altera as dimensões da foto
		if(w && h && (foto.width > w || foto.height > h) ){
			if (foto.width > foto.height && foto.width > w)
				var porcentagem = foto.width / w * 100;
			else if(foto.height > h)
				var porcentagem = foto.height / h * 100;
			
			_width = foto.width / (porcentagem / 100);
			_height = foto.height / (porcentagem / 100);
		}
		//redimensiona o elemento alvo para ficar do tamanho da foto
		alvo.parentNode.style.width = (_width + 6)+ 'px';
		alvo.parentNode.style.height = (_height + 33) + 'px';
		
		CentralizarConteudo(alvo.parentNode, _width);
		
		//insere a foto
		alvo.innerHTML = '<img src="'+ foto.src +'" width="' + _width + '" height="' + _height + '" alt="" onmousedown="return false" onmousemove="return false" />';
		
		//limpa o objeto
		foto = '';
		delete foto;
	}
}

//CentralizarConteudo(): centraliza um conteudo no centro do site
//	- conteudo: conteudo a centralizar
//	- [w]: width do conteudo
function CentralizarConteudo(conteudo, w){
	var _width = (w)? w : conteudo.offsetWidth;
	conteudo.style.left = ( (document.getElementById('conteudo').offsetWidth / 2) - (_width / 2) ) + 'px';
}

var m = 0; //variavel que armazena a margem do conteudo
var intervalo; //variavel que armazena o interval
var velocidade = 4; //define a velocidade

//subirConteudo(): faz um conteudo subir
//	- id: id do conteúdo
function subirConteudo(id){
	var c = document.getElementById(id);
	if((m*-1) < c.offsetHeight - c.parentNode.offsetHeight){
		c.style.marginTop = m + 'px';
		m -= velocidade;
		intervalo = setTimeout("subirConteudo('" + id + "')", 1);
		document.body.onmouseup = parar;
	}
	
	document.body.onmouseup = parar;
}

//subirConteudo(): faz um conteudo descer
//	- id: id do conteúdo
function descerConteudo(id, e){
	var c = document.getElementById(id);
	if(m <= 0){
		c.style.marginTop = m + 'px';
		m += velocidade;
		intervalo = setTimeout("descerConteudo('" + id + "')", 1);
		document.body.onmouseup = parar;
	}
	
	document.body.onmouseup = parar;
}

//parar(): para o movimento
function parar(){
	clearTimeout(intervalo);
	document.body.onmouseup = null;
}


/* TIRA A BORDA DOS FLASHS
Copyright 2006 Adobe Systems, Inc. All rights reserved.
Versão compacta, alterada por Renato Herculano
	- src, width e height são obrigatórios
	- os demais parametros são opcionais
*/
function flash(src, width, height, id, flashVars, title, idAlvo, wmode, menu, scale){
	
	var ret = GetArgumentos(src, width, height, id, flashVars, wmode, menu, scale);
	
	var str = '<object ';
	
	for (var i in ret.objAttrs)
		str += i + '="' + ret.objAttrs[i] + '" ';
	
	str += '>';
	
	for (var i in ret.params)
		str += '<param name="' + i + '" value="' + ret.params[i] + '" /> ';
	
	str += '</object>';
	
	if(idAlvo){
		document.getElementById(idAlvo).innerHTML = str;
	}else
		document.write(str);
}


function GetArgumentos(src, w, h, id, flashVars, title, wmode, menu, scale){
	var ret = new Object();
	ret.params = new Object();
	ret.objAttrs = new Object();

	ret.objAttrs['data'] = ret.params["movie"] = src;
	ret.objAttrs['width'] = w;
	ret.objAttrs['height'] = h;
	ret.params['quality'] = 'high';
	ret.objAttrs['type'] = 'application/x-shockwave-flash';
	ret.params['menu'] = 'false';
	
	if(id) ret.objAttrs['id'] = id;
	
	if(flashVars) ret.params['flashVars'] = flashVars;
	
	if(title) ret.objAttrs['title'] = title;
	
	ret.params['wmode'] = (wmode)? wmode : 'transparent';
	ret.params['scale'] = (scale)? scale : 'exactfit';
	ret.params['menu'] = (menu)? menu : 'false';
	
	return ret;
}