function controla() {
	//alert(navigator.appName);
	var elemento= document.getElementById("radioon");
	
	if (!elemento) alert("Player não reconhecido pelo seu navegador!");
	//alert("2: "+document.getElementById("radioon").GetPlayState());
	try {
		var estado;
		//soh o Opera lê isso
		if (navigator.appName.indexOf('Opera') != -1)
			estado=elemento.GetPlayState();
		//firefox dá erro nesse comando
		else if (navigator.appName.indexOf('Netscape') != -1)
			estado=elemento.GetPlayState();
		else
			estado=elemento.PlayState;
			
		switch(estado) {
			//parado
			case 0:
			//pausado
			case 1:
				elemento.Play();
				break;
			//tocando
			case 2:
				elemento.Pause();
				break;
			//carregando
			case 3:
				elemento.Stop();
				break;
		}
		
		/*document.getElementById("radioon").Pause();
		if (navigator.appName.indexOf('Netscape') != -1)
			document.getElementById("radioon").SetCurrentPosition(0);
		else
			document.getElementById("radioon").CurrentPosition = 0;*/
	} catch (eee) { alert("Comandos não suportados por seu navegador!\n\nPara controlar o player, clique com o botão inverso do mouse sobre\no dispositivo ao lado e escolha a opção desejada."); }
	
	return false;
}

function aumentaVolume() {
	var elemento= document.getElementById("radioon");
	try {
		if (elemento.Volume <= -300) 
			elemento.Volume = elemento.Volume + 300;
	} catch (eee) { alert("Comandos não suportados por seu navegador!"); }
	return false;
}

function abaixaVolume() {
	var elemento= document.getElementById("radioon");
	try {
		if (elemento.Volume >= -8000) 
			elemento.Volume = elemento.Volume - 300;
	} catch (eee) { alert("Comandos não suportados por seu navegador!"); }
	return false;
}

function trocaFundo() {
	var elemento= document.getElementById("player_comandos");
	if (elemento.className=="madeira")
		elemento.className="metal";
	else
		elemento.className="madeira";
	
	return false;
}

function destacaPlayer() {
	window.open('/opt/player', '', 'width=740,height=180,top=100,left=100,scrollbars=no,resizable=no');
}

function fechaPlayer() {
	window.open('/', '', 'width=820,height=480,top=100,left=100,toolbar=yes,location=yes,directories=yes,status=yes,menubar=yes,scrollbars=yes,resizable=yes');
	setTimeout(window.close(), 1500);
}

function abreDiv(div) {
	var div_mesmo= document.getElementById(div);
	div_mesmo.style.display="block";
}

function fechaDiv(div) {
	var div_mesmo= document.getElementById(div);
	div_mesmo.style.display="none";
}

function emoticon(text) {
	var txtarea= document.getElementById("mensagem");;
	text= ' ' + text + ' ';
	
	if (txtarea.createTextRange && txtarea.caretPos) {
		var caretPos = txtarea.caretPos;
		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text + ' ' : text;
		txtarea.focus();
	}
	else {
		txtarea.value += text;
		txtarea.focus();
	}
}

function carregaObjeto(arquivo, largura, altura, nome, comecar) {
	//if (navigator.appName.indexOf('Netscape') != -1)
	//	altura= 50;

	document.write('<object id="'+nome+'" width="'+largura+'" height="'+altura+'" type="application/x-oleobject" standby="Carregando..." codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,0,02,902" classid="CLSID:22D6F312-B0F6-11D0-94AB-0080C74C7E95">');
	document.write('<param value="'+arquivo+'" name="FileName"/>');
	document.write('<param value="true" name="transparentatStart"/>');
	document.write('<param value="1" name="autoStart"/>');
	document.write('<param name="ShowControls" value="0" />');
	document.write('<param value="1" name="ShowStatusBar"/>');
	document.write('<embed width="'+largura+'" height="'+altura+'" autostart="1" showcontrols="0" showstatusbar="1" src="'+arquivo+'" pluginspage="http://www.microsoft.com/Windows/MediaPlayer/" type="application/x-mplayer2"/>');	
	document.write('</object>');

	/*document.write('<object id="'+nome+'" width="'+largura+'" height="'+altura+'" data="'+arquivo+'" type="application/x-mplayer2">');
	document.write('<param name="FileName" value="'+arquivo+'" />');
	document.write('<param name="AutoStart" value="'+comecar+'" />');
	document.write('<param name="TransparentAtStart" value="true" />');
	if (navigator.appName.indexOf('Netscape') != -1)
		document.write('<param name="ShowControls" value="1" />');
	else
		document.write('<param name="ShowControls" value="0" />');
	document.write('<param name="ShowDisplay" value="0" />');
	document.write('<param name="ShowStatusBar" value="1" />');
	document.write('<param name="AutoSize" value="0" />');
	document.write('</object>');*/
}

function carregaFlash(arquivo, width, height, name) {
	document.write('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="'+width+'" height="'+height+'" id="flash'+name+'" name="'+name+'" align="middle">');
	document.write('<param name="allowScriptAccess" value="sameDomain" /><param name=wmode value=transparent /><param name="scale" value="noscale" />');
	document.write('<param name="movie" value="'+arquivo+'" /><param name="quality" value="high" /><param name="bgcolor" value="#ffffff" /><embed src="'+arquivo+'" quality="high" wmode="transparent" bgcolor="#ffffff" width="'+width+'" height="'+height+'" name="'+name+'" id="flash'+name+'" align="middle" scale="noscale"  allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />');
	document.write('</object>');
}

function carregaPlayerGravado(arquivo, parametro, width, height, name) {
	arquivo = arquivo + parametro;
	document.write('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="'+width+'" height="'+height+'" id="'+name+'" name="'+name+'" align="middle">');
	document.write('<param name="allowScriptAccess" value="sameDomain" /><PARAM NAME=wmode VALUE=transparent /><param name="scale" value="noscale" />');
	document.write('<param name="movie" value="'+arquivo+'" /><param name="quality" value="high" /><param name="bgcolor" value="#ffffff" /><embed src="'+arquivo+'" quality="high" wmode="transparent" bgcolor="#ffffff" width="'+width+'" height="'+height+'" name="'+name+'" id="flash'+name+'" align="middle" scale="noscale"  allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />');
	document.write('</object>');
}


function atribuiValor(id_elemento, valor) {
	var elemento= document.getElementById(id_elemento);
	elemento.value= valor;
}

function atribuiClasse(id_elemento, classe) {
	var elemento= document.getElementById(id_elemento);
	elemento.className= classe;
}

function setaClasse(campo, classe) {
	if(classe != undefined) {
		if (campo.lastClass == undefined){
			campo.lastClass = null;
		}
		campo.lastClass = campo.className || "";
		campo.className = classe;
	}
	else
		campo.className = campo.lastClass;
}

function alertaCampo(campo) {
	campo.className= "campo_erro";
}

function validaEmail(email) {
	var retorno= true;
	
	if (email=="")
		retorno= false;
	if (email.indexOf("@") < 2)
		retorno= false;
	if (email.indexOf(".") < 1)
		retorno= false;
	
	return(retorno);
}

function daFoco(id_elemento) {
	var elemento = document.getElementById(id_elemento);
	elemento.focus();
}

function validaData(data_nasc, periodo) {
	var erros = "";
	if (data_nasc=="")
		erros = "Entre com a data!\n";
	else {
		var dia= data_nasc.substring(0, 2);
		var mes= data_nasc.substring(3, 5);
		var ano= data_nasc.substring(6, 10);
		
		var barra1= data_nasc.substring(2, 3);
		var barra2= data_nasc.substring(5, 6);
		
		if ((barra1=="/") && (barra2=="/")) {
			var nonNumbers = /\D/;
						
			if ( (dia<=0) || (dia>31)  || (nonNumbers.test(dia)) )
				if (erros=="") erros = "Dia inválido\n";
			else {
				if ( ((mes=="02") || (mes=="04") || (mes=="06") || (mes=="09") || (mes=="11")) && (dia=="31") )
					if (erros=="") erros = "O mes informado não possui o dia 31!\n";
			}
			if ( (mes<=0) || (mes>12)  || (nonNumbers.test(mes)) )
				if (erros=="") erros = "Mês invalido\n";
			
			if (periodo=="n") {
				var dataAtual= new Date();
				var anoAtual= dataAtual.getFullYear();
				
				if ( (ano<=0) || (ano>anoAtual) || (nonNumbers.test(ano)) )
					if (erros=="") erros = "Ano inválido!\n";
			}
			//ano bissexto
			if ((ano%4!=0) && (mes==2) && (dia>28))
				if (erros=="") erros = "Dia inválido! \n";
		}
		else
			if (erros=="") erros = "Preencha a data no formato dd/mm/AAAA\n";
	}
	return(erros);
}

function validaForm(id_form) {
	
	var area_erro= "div_erro";
	var erro_alerta= "";
	var avisar=true;
	var botao= document.getElementById("enviar");
	
	switch (id_form) {
		case 'formContato':
			var nome= document.getElementById("nome");
			var email= document.getElementById("email");
			var telefone= document.getElementById("telefone");
			var mensagem= document.getElementById("mensagem");
			var erro= null;
			var foco= null;
			
			if (nome.value=="") {
				if (foco==null) foco= nome;
				if (erro_alerta=="") erro_alerta= "Campo nome não pode ser vazio!";
				setaClasse(nome, "campo_erro");
				erro= 1;
			}
			if (!validaEmail(email.value))  {
				if (foco==null) foco= email;
				if (erro_alerta=="") erro_alerta= "E-mail inválido!";
				setaClasse(email, "campo_erro");
				erro= 1;
			}
			if (telefone.value=="") {
				if (foco==null) foco= telefone;
				if (erro_alerta=="") erro_alerta= "Campo telefone não pode ser vazio!";
				setaClasse(telefone, "campo_erro");
				erro= 1;
			}
			if (mensagem.value=="") {
				if (foco==null) foco= mensagem;
				if (erro_alerta=="") erro_alerta= "Campo mensagem não pode ser vazio!";
				setaClasse(mensagem, "campo_erro");
				erro= 1;
			}
		break;
	}
	
	if (erro) {
		if (avisar) {
			//var form_erro= document.getElementById(area_erro);
			//form_erro.innerHTML= erro_alerta;
			alert(erro_alerta);
			foco.focus();
		}
		
		return false;
	}
	else {
		botao.disabled= true;
		botao.innerHTML= "...";
		//alert("merda");
		return true;
	}
}

function Limpar(valor, validos) {
	var result = "";
	var aux;
	for (var i=0; i < valor.length; i++) {
		aux = validos.indexOf(valor.substring(i, i+1));
		if (aux>=0) {
			result += aux;
		}
	}
	return result;
}

function formataValor(campo,teclapres) {
	var tammax = 200;
	var decimal = 2;
	var tecla = teclapres.keyCode;
	vr = Limpar(campo.value,"0123456789");
	tam = vr.length;
	dec=decimal
	
	if (tam < tammax && tecla != 8){ tam = vr.length + 1 ; }
	
	if (tecla == 8 )
	{ tam = tam - 1 ; }
	
	if ( tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 )
	{
	
	if ( tam <= dec )
	{ campo.value = vr ; }
	
	if ( (tam > dec) && (tam <= 5) ){
	campo.value = vr.substr( 0, tam - 2 ) + "," + vr.substr( tam - dec, tam ) ; }
	if ( (tam >= 6) && (tam <= 8) ){
	campo.value = vr.substr( 0, tam - 5 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - dec, tam ) ;
	}
	if ( (tam >= 9) && (tam <= 11) ){
	campo.value = vr.substr( 0, tam - 8 ) + "." + vr.substr( tam - 8, 3 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - dec, tam ) ; }
	if ( (tam >= 12) && (tam <= 14) ){
	campo.value = vr.substr( 0, tam - 11 ) + "." + vr.substr( tam - 11, 3 ) + "." + vr.substr( tam - 8, 3 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - dec, tam ) ; }
	if ( (tam >= 15) && (tam <= 17) ){
	campo.value = vr.substr( 0, tam - 14 ) + "." + vr.substr( tam - 14, 3 ) + "." + vr.substr( tam - 11, 3 ) + "." + vr.substr( tam - 8, 3 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - 2, tam ) ;}
	}
} 

function formataData(val) {
	// onkeyup="formataData(this);"
   	var pass = val.value;
	var expr = /[0123456789]/;
		
	for(i=0; i<pass.length; i++){
		var lchar = val.value.charAt(i);
		var nchar = val.value.charAt(i+1);
	
		if(i==0) {
		   if ((lchar.search(expr) != 0) || (lchar>3)){
			  val.value = "";
		   }
		   
		}
		else if(i==1){
			   if(lchar.search(expr) != 0){
				  var tst1 = val.value.substring(0,(i));
				  val.value = tst1;				
 				  continue;			
			   }
			   
			   if ((nchar != '/') && (nchar != '')){
				 	var tst1 = val.value.substring(0, (i)+1);
				
					if(nchar.search(expr) != 0) 
						var tst2 = val.value.substring(i+2, pass.length);
					else
						var tst2 = val.value.substring(i+1, pass.length);
	
					val.value = tst1 + '/' + tst2;
			   }

		 }else if(i==4){
			
				if(lchar.search(expr) != 0){
					var tst1 = val.value.substring(0, (i));
					val.value = tst1;
					continue;			
				}
		
				if	((nchar != '/') && (nchar != '')){
					var tst1 = val.value.substring(0, (i)+1);

					if(nchar.search(expr) != 0) 
						var tst2 = val.value.substring(i+2, pass.length);
					else
						var tst2 = val.value.substring(i+1, pass.length);
	
					val.value = tst1 + '/' + tst2;
				}
   		  }
		
		  if(i>=6){
			  if(lchar.search(expr) != 0) {
					var tst1 = val.value.substring(0, (i));
					val.value = tst1;			
			  }
		  }
	 }
	
     if (pass.length>10)
		val.value = val.value.substring(0, 10);
	 	return true;
}
