//verifica o navegador
var IE = (navigator.userAgent.indexOf('MSIE') > -1);

//marcarMenuUrl(): marca um item do menu do topo de acordo com a pasta indicada na url
// - indiceURL: nº da pasta da url que contem o mesmo nome da Id a marcar
var itemMarcado;
function marcarMenuUrl(indiceURL){
	itemMarcado = location.href.split('/')[indiceURL];
	//itemMarcado = location.href.substring(location.href.lastIndexOf('/')+1, location.href.lastIndexOf('.'));
	if(itemMarcado == '' || itemMarcado == 'index.php')
		itemMarcado = 'home';
	var m = document.getElementById(itemMarcado);
	if(m)
		m.className = 'menuMarcado';
}

//marcarMenuUrl(): marca um item do menu lateral de acordo com a pasta indicada na url ou id do curso
// - indiceURL: nº da pasta da url que contem o mesmo nome da Id a marcar
var itemMarcadoLateral;
function marcarMenuUrlLateral(){
	//itemMarcadoLateral = location.href.split('/')[indiceURL];
	itemMarcadoLateral = location.href.substring(location.href.lastIndexOf('/')+1, location.href.lastIndexOf('.'));
	if(itemMarcadoLateral == '' || itemMarcadoLateral == 'index.php')
		itemMarcadoLateral = 'home';
	var m = document.getElementById('lateral_'+itemMarcadoLateral);
	if(m)
		m.className = 'menuMarcadoLateral';
}

//marcarMenuUrl(): marca um item do menu lateral de acordo com a pasta indicada na url ou id do curso
// - indiceURL: nº da pasta da url que contem o mesmo nome da Id a marcar
var itemMarcadoNoticias;
function marcarMenuUrlNoticias(){
	itemMarcadoNoticias = location.href.substring(location.href.lastIndexOf('curso=')+6, location.href.length);
	if(itemMarcadoNoticias == '' || itemMarcadoNoticias == 'index.php')
		itemMarcadoNoticias = 'home';
	var m = document.getElementById('lateral_'+itemMarcadoNoticias);
	if(m)
		m.className = 'menuMarcadoLateral';
}

//mostrarConteudo(): torna um conteudo visível
//	- id: id do conteudo a monstrar
function mostrarConteudo(id){			
	$("#"+id).slideDown(600);
}

//esconderConteudo(): esconde um conteudo
//	- id: id do conteudo a esconder
function esconderConteudo(id){
	$("#"+id).slideUp(600);
}


//trocarConteudo(): troca um conteudo visível por outro
//	-id: id do conteudo a trocar
var visivel = '';
function trocarConteudo(id){
	if(id != visivel){
		if(visivel.length > 0) 
			esconderConteudo(visivel);
	}
	mostrarConteudo(id);
	visivel = id;
}

//trocarConteudo2(): troca um conteudo visível por outro (função igual a anterior, utilizada para trocar conteudos um dentro do outro)
//	-id: id do conteudo a trocar
var visivel2 = '';
function trocarConteudo2(id){
	if(id != visivel2){
		if(visivel2.length > 0) 
			esconderConteudo(visivel2);
	}
	mostrarConteudo(id);
	visivel2 = id;
}

//trocarConteudoClique(): troca um conteudo visível por outro. caso clique no conteudo que ja esteje visivel, esconde ele
//	-id: id do conteudo a trocar
function trocarConteudoClique(id){	
	if(visivel.length > 0) 		
		esconderConteudo(visivel);
	if(visivel != id){
		mostrarConteudo(id);
		visivel = id;
	}else{
		visivel = '';
	}
}

//linkCombo(): linka um combo
//	- idCombo: id do combo a linkar
function linkCombo(idCombo){
	location.href = document.getElementById(idCombo).value;
}

//marcarCombo(): marca um combo, caso algum dos seus options contenham a url no seu value
//	- idCombo: id do combo a marcar
function marcarCombo(idCombo){	
	var opts = document.form[idCombo].options;	
	for(var i = 0; i < opts.length; i ++){		
		if(location.href.indexOf(opts[i].value) > -1){
			opts[i].selected = true;
			break;
		}
	}
}


//abrirPopup(): abre uma popup
//	- url: caminho da janela a abrir
//	- width: width da popup
//	- height: height da popup
//  - scroll(opcional): se a pop-up terá scroll
function abrirPopup(url, width, height,scroll){
	var parametro = 'width=' + width + ', height=' + height;
	if(scroll)
		parametro += ', scrollbars =' + scroll;	
	parametro += ' , resizable=no';
	window.open(url, '', parametro);
}

function enviarAmigo(){
	/*var f = document.getElementById('envieAmigo');
	if (document.title.indexOf('#') > - 1)
		f.linkNome.value = document.title.substring(document.title.lastIndexOf("- ") + 1, document.title.indexOf('#'));
	else
		f.linkNome.value = document.title.substring(document.title.lastIndexOf("- ") + 1);
	
	f.url.value = location.href.substr(location.href.indexOf('.br') + 3);
	f.submit();*/
	if (document.title.indexOf('#') > - 1)
		var linkNome = document.title.substring(document.title.lastIndexOf("- ") + 1, document.title.indexOf('#'));
	else
		var linkNome = document.title.substring(document.title.lastIndexOf("- ") + 1);
	
	var url = location.href.substr(location.href.indexOf('.br') + 3);
		
	
	abrirPopup('/envie_amigo/envie_amigo.php?linkNome='+linkNome+'&url='+url+'',440,600, 'yes', 'no');
}

//função para limpar o form
function resetarForm(){
	if (!confirm("Deseja realmente limpar todos os dados do formulário?")) return;
	var campos = document.getElementById('form').elements;	
	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].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
		}
	}
}

/* Retira a borda dos flashes
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, wmode, menu, scale){
	var ret = GetArguments(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>';
	document.write(str);
}

//GetArguments(): função auxiliar para retirar a borda dos flashes
//	- retorna os argumentos da tag object e os parametros
function GetArguments(src, w, h, id, flashVars, 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;
	
	ret.params['wmode'] = (wmode)? wmode : 'transparent';
	ret.params['scale'] = (scale)? scale : 'exactfit';
	ret.params['menu'] = (menu)? menu : 'false';
	
	return ret;
}

//Funções utilizadas em formulários

//bloqueia a entrada de caracteres
//utilização no form onkeypress="return(dt(this, event))";
function dt(fld, e){
	var strCheck = '0123456789';// lista os caracteres que serao aceitos
	var whichCode = (window.Event) ? e.which : e.keyCode;
	if ((whichCode == 13) || (whichCode == 8) || (whichCode == 0))return true;  // libera Enter e backspace
	key = String.fromCharCode(whichCode);  // Get key value from key code
	if (strCheck.indexOf(key) == -1) return false;  // Not a valid key
}

//formataData(): foramata um campo input no formato 00/00/0000
// - campo: id do campo a formatar
function formataData(campo){
	if (document.getElementById(campo).value.length == 2 || document.getElementById(campo).value.length == 5){
		valorData = document.getElementById(campo).value+ "/";
		document.getElementById(campo).value= valorData;
	}
}

//formataCep(): foramata um campo input no formato 00000-000
// - campo: id do campo a formatar
function formataCep(campo){
	if (document.getElementById(campo).value.length == 5){
		valorCep = document.getElementById(campo).value+ "-";
		document.getElementById(campo).value= valorCep;
	}
}

//formataTel(): foramata um campo input no formato (00)0000-0000
// - campo: id do campo a formatar
function formataTel(campo){
	if(document.getElementById(campo).value == ''){
		valorTel = "(" + document.getElementById(campo).value;
		document.getElementById(campo).value = valorTel;
	}
	if(document.getElementById(campo).value.length == 3){
		valorTel = document.getElementById(campo).value+ ")";
		document.getElementById(campo).value = valorTel;
	}
	if(document.getElementById(campo).value.length == 8){
		valorTel = document.getElementById(campo).value+ "-";
		document.getElementById(campo).value = valorTel;
	}
	if(document.getElementById(campo).value.length == 1 && document.getElementById(campo).value.indexOf('(') < 0){
		valorTel = "(" + document.getElementById(campo).value;
		document.getElementById(campo).value = valorTel;
	}
}