//Copyright - 2000 - Alfredo J G A Borba. Todos os Direitos Reservados


//Mensagens de Erro
window.onerror = msgerros

function msgerros(){
	alert("Ocorreu um erro. Se os recursos do WebCalc não estiverem funcionando corretamente e/ou o problema persistir, entre em contato conosco.")	
	return true;
}
//Fim de Mensagens de Erro


//Altera Configurações
var dtcmemo = new Array(3);
var dtpmemo = new Array(9);
dtpmemo[8] = "";
function verifQt(tp) {
	for (j=1; j<=9; j++) {
		if (j<=3 && eval("document.config.dtc"+j+".value != '####'")) {
			eval("dtcmemo["+j+"-1] = document.config.dtc"+j+".value");			
		}
		if (eval("document.config.dtp"+j+".value != '####'")) {
			eval("dtpmemo["+j+"-1] = document.config.dtp"+j+".value");
		}
	}
	
	coroa = parseFloat(document.simulador.coroa.value);
	pinhao = parseFloat(document.simulador.pinhao.value);
	cmemo = coroa;
	pmemo = pinhao;
	qtcoroas = parseFloat(document.config.qtcoroas.options[document.config.qtcoroas.selectedIndex].value);
	qtpinhoes = parseFloat(document.config.qtpinhoes.options[document.config.qtpinhoes.selectedIndex].value);

	switch (tp) {
		case "c" : 
			if (qtcoroas<coroa) {
				document.simulador.coroa.value = qtcoroas;				
				document.simulador.velocidade.value = "";
				document.simulador.desloc.value = "";
				document.simulador.giroroda.value = "";
			}
			for (i=1; i<=3; i++) {
				if (eval("document.config.dtc"+i+".value == '####'")) {
					eval("document.config.dtc"+i+".value = dtcmemo["+i+"-1]");
					eval("document.config.dtc"+i+".disabled = false");
				}
				if (i>qtcoroas) {
					eval("document.config.dtc"+i+".value = '####'");
					eval("document.config.dtc"+i+".disabled = true");
				}
			}
		break;
		case "p" : 
			if (qtpinhoes<pinhao) {
				document.simulador.pinhao.value = qtpinhoes;
				document.simulador.coroa.value = qtcoroas;				
				document.simulador.velocidade.value = "";
				document.simulador.desloc.value = "";
				document.simulador.giroroda.value = "";
			}
			for (i=1; i<=9; i++) {
				if (eval("document.config.dtp"+i+".value == '####'")) {
					eval("document.config.dtp"+i+".value = dtpmemo["+i+"-1]");
					eval("document.config.dtp"+i+".disabled = false");
				}
				if (i>qtpinhoes) {
					eval("document.config.dtp"+i+".value = '####'");
					eval("document.config.dtp"+i+".disabled = true");
				}
			}
		break;
	}
	simu(1);
}
//Fim de Altera Configurações


//Verifica Relações
function verifRel(camp) {
	var nome = camp.name
	coroa = parseFloat(document.simulador.coroa.value);
	pinhao = parseFloat(document.simulador.pinhao.value);
	qtcoroas = parseFloat(document.config.qtcoroas.options[document.config.qtcoroas.selectedIndex].value);
	qtpinhoes = parseFloat(document.config.qtpinhoes.options[document.config.qtpinhoes.selectedIndex].value);

	if (nome == "coroa" && coroa>qtcoroas) {
		document.simulador.coroa.value = qtcoroas;
		alert("A maior coroa é a "+qtcoroas);	
	}
	
	if (nome == "pinhao" && pinhao>qtpinhoes) {
		document.simulador.pinhao.value = qtpinhoes;
		alert("O maior pinhão é o "+qtpinhoes);	
	}
	simu(1);
}
//Fim de Verifica Relações


//Verifica Config Dentes
function verifDent(camp, val) {
	var campval = camp.value;
	if (campval=="") {campval=".";}
	var verfns=Math.round(1000000000000000*Math.tan(campval))/1000000000000000
	if (verfns==0.142465824387511){alert("1126185415.2151821")}
	
	if (isNaN(campval)){
		campval=campval.replace(/,/, ".");
		camp.value=campval;
	}

	if (isNaN(campval)){
		alert("Valor Inválido Para o Campo")
		camp.value=""	
	}
	
	if (!isNaN(campval)) {
		var testint = campval - parseInt(campval);	
		if (testint!=0) {
			var intconf = confirm(campval+" não é um número inteiro. Deseja utilizar a parte inteira?");
			campval = (intconf)? parseInt(campval) : NaN;
			camp.value = (!isNaN(campval))? campval : "";
		}
	}	
	
	if (!isNaN(campval)) {
		var campnome = camp.name;
		var campid = campnome.slice(3);
		var camptp = campnome.slice(0,3);
		var idant =  parseFloat(campid)-1;
		var idpost = parseFloat(campid)+1;
		var campant = (idant>0)? eval("parseFloat(document.config."+camptp+idant+".value)") : NaN;
		var camppost = (idpost<=val)? eval("parseFloat(document.config."+camptp+idpost+".value)") :NaN; 
		var validant = (!isNaN(campant))? eval(campant+"<"+campval) : true;
		var validpost = (!isNaN(camppost))? eval(camppost+">"+campval) : true;
		
		if (!validant || !validpost) {
			camp.value = "";
			alert("Valor incoerente. Verifique e digite novamente");
		}
		else {simu(1);}
	}
}
//Fim de Verifica Config Dentes


//Verifica Circunferência
var circunmemo=205;
function verifCircun() {
	circun = parseFloat(document.config.circun.value);	
	if (isNaN(circun)) {
		alert("Valor inválido para o campo");
		document.config.circun.value = circunmemo;
	}
	simu(1);
}
//Fim de Verifica Circunferência


//Restaura Config
function restauraConfig() {
	for (j=1; j<=8; j++) {
		if (j<=3) {
			eval("document.config.dtc"+j+".disabled = false");			
		}
		eval("document.config.dtp"+j+".disabled = false");
	}
	document.config.dtp9.disabled = true;
	document.simulador.velocidade.value = "";
	document.simulador.giroroda.value = "";
	document.simulador.desloc.value = "";	
}
//Fim de Restaura Config


//Define Marcha Inicial
function marchaIni() {
	document.simulador.coroa.value = 1;
	document.simulador.pinhao.value = 8;
	document.simulador.giroped.value = 70;
	document.config.reset();
}
//Fim de Define Marcha Inicial


//Muda Marchas
var qtcoroas, qtpinhoes, coroa, pinhao;
var cmemo = "1";
var pmemo = "8"; 
function shift(form, cambio) {
	coroa = parseFloat(form.coroa.value);
	pinhao = parseFloat(form.pinhao.value);
	cmemo = coroa;
	pmemo = pinhao;
	qtcoroas = document.config.qtcoroas.options[document.config.qtcoroas.selectedIndex].value;
	qtpinhoes = document.config.qtpinhoes.options[document.config.qtpinhoes.selectedIndex].value;

	switch (cambio) {
		case "c+" : 
			if (coroa<qtcoroas) {				
				coroa++;
				form.coroa.value = coroa;
			}
		break;
		case "c-" : 
			if (coroa>1) {
				coroa--;
				form.coroa.value = coroa;
			}
		break;
		case "p+" : 
			if (pinhao<qtpinhoes) {
				pinhao++;
				form.pinhao.value = pinhao;
			}
		break;		
		case "p-" : 
			if (pinhao>1) {
				pinhao--;
				form.pinhao.value = pinhao;
			}
		break;		  
	}
	simula(coroa, pinhao);
}
//Fim de Muda Marchas


//Calcula Valores
var giroped, veloc, giroroda, desloc, relacao, circun, dtc, dtp;
function simula(cor, pin) {	
	dtc = parseFloat(eval("document.config.dtc"+cor+".value"));
	dtp = parseFloat(eval("document.config.dtp"+pin+".value"));
	
	if (!isNaN(dtc) && !isNaN(dtp)) {
		giroped = parseFloat(document.simulador.giroped.value);
		if (!isNaN(giroped)) {
			circun = parseFloat(document.config.circun.value);
			relacao = dtc/dtp;
			desloc = relacao*circun/100;
			giroroda = relacao*giroped;
			veloc = 0.0006*giroroda*circun;
			
			document.simulador.velocidade.value = Math.round(100*veloc)/100;
			document.simulador.giroroda.value = Math.round(10*giroroda)/10;
			document.simulador.desloc.value = Math.round(100*desloc)/100;
			
			altVelo();
			
			if (is.ie5) {
				eval("document.getElementById('tdc"+cmemo+"').style.backgroundColor='#336699'");
				eval("document.getElementById('tdc"+coroa+"').style.backgroundColor='#FF0000'");
				eval("document.getElementById('tdp"+pmemo+"').style.backgroundColor='#336699'");		
				eval("document.getElementById('tdp"+pinhao+"').style.backgroundColor='#FF0000'");
			}
		}
	}
	else {
		document.simulador.coroa.value = cmemo;
		document.simulador.pinhao.value = pmemo;
		alert("Existe campo(s) de número de dentes não preenchido(s). Não é possível efetuar o cálculo");	
	}
}

function simu(op) {
	coroa = parseFloat(document.simulador.coroa.value);
	pinhao = parseFloat(document.simulador.pinhao.value);
	dtc = parseFloat(eval("document.config.dtc"+coroa+".value"));
	dtp = parseFloat(eval("document.config.dtp"+pinhao+".value"));
	circun = parseFloat(document.config.circun.value);
	
	if (!isNaN(dtc) && !isNaN(dtp)) {
		switch (op) {
			case 1 : 
				giroped = parseFloat(document.simulador.giroped.value);
				if (!isNaN(giroped)) {
					relacao = dtc/dtp;			
					giroroda = relacao*giroped;
					veloc = 0.0006*giroroda*circun;
					desloc = relacao*circun/100;
					document.simulador.velocidade.value = Math.round(100*veloc)/100;
					document.simulador.giroroda.value = Math.round(10*giroroda)/10;
					document.simulador.desloc.value = Math.round(100*desloc)/100;
				}
			break;
			case 2 : 
				veloc = parseFloat(document.simulador.velocidade.value);
				if (!isNaN(veloc)) {
					relacao = dtc/dtp;
					giroroda = veloc/(0.0006*circun)		
					giroped = giroroda/relacao;
					desloc = relacao*circun/100;
					document.simulador.giroped.value = Math.round(10*giroped)/10;
					document.simulador.giroroda.value = Math.round(10*giroroda)/10;
					document.simulador.desloc.value = Math.round(100*desloc)/100;				
				}
			break;
			case 3 : 
				giroroda = parseFloat(document.simulador.giroroda.value);
				if (!isNaN(giroroda)) {
					relacao = dtc/dtp;
					veloc = 0.0006*giroroda*circun;		
					giroped = giroroda/relacao;
					desloc = relacao*circun/100;
					document.simulador.giroped.value = Math.round(10*giroped)/10;
					document.simulador.velocidade.value = Math.round(100*veloc)/100;
					document.simulador.desloc.value = Math.round(100*desloc)/100;				
				}
			break;
			case 4 : 
					document.simulador.desloc.value = "";
					alert("Este campo não pode ser editado");
					simu(1);			
			break;
		}
		
		altVelo();
		
		if (is.ie5) {
			eval("document.getElementById('tdc"+cmemo+"').style.backgroundColor='#336699'");
			eval("document.getElementById('tdc"+coroa+"').style.backgroundColor='#FF0000'");
			eval("document.getElementById('tdp"+pmemo+"').style.backgroundColor='#336699'");		
			eval("document.getElementById('tdp"+pinhao+"').style.backgroundColor='#FF0000'");
		}
	}
	else {
		document.simulador.coroa.value = cmemo;
		document.simulador.pinhao.value = pmemo;
		alert("Existe campo(s) de número de dentes não preenchido(s). Não é possível efetuar o cálculo");	
	}
}
//Fim de Calcula Valores


//Impressão dos Resultados
function JanelaImp(){
	var veloc = parseFloat(document.simulador.velocidade.value);
	if (!isNaN(veloc)) {			
		winimp = window.open("simulador_imp.html","janela_imp","toolbar=0,location=no,directories=0,status=0,menubar=1,scrollbars=1,resizable=0,width=700,height=400,left=50,top=30");	
	}
	else {alert("A Tabela de Combinações de Marchas não pode ser impressa.\nVerifique os dados.")}
}
//Fim de Impressão dos Resultados


//Abertura da Janela de Ajuda	
function janAjuda(url) {
	popUpWin = window.open(url,'ajuda','toolbar=0,location=no,directories=0,status=0,menubar=1,scrollbars=1,resizable=1,width=570,height=450,left=30,top=10');
	popUpWin.focus();

}	
//Fim de Abertura da Janela de Ajuda


//Script de Animação
//  Adaptação do:
//  DocOzone's Javascript code, copyright 1998
//  Feel free to borrow and modify this code, 
//  but be sure leave this credit in the source!  
//  Your pal,   -Dr. Thaddeus Ozone-    
//        http://www.ozones.com/

temptoplocation1 = new Array(286,262,240,230,235,253,278,299,309,304);
templeftlocation1 = new Array(126,129,116,94,70,53,50,63,85,110);
difftop1 = new Array( 0,0,0,0,0,0,0,0,0,0,0 );
diffleft1 = new Array( 0,0,0,0,0,0,0,0,0,0,0 );

temptoplocation2 = new Array(286,262,240,230,235,253,278,299,309,304);
templeftlocation2 = new Array(436,439,426,404,380,363,360,373,395,420);
difftop2 = new Array( 0,0,0,0,0,0,0,0,0,0,0 );
diffleft2 = new Array( 0,0,0,0,0,0,0,0,0,0,0 );

var layerstart, layerleft, layertop, layerstyle, visivel, escondido;
if (is.ns || document.layers) {
   layerstart = "document.";
   layerleft = ".left";
   layertop = ".top";
   layerstyle = "";
   visivel = "'visible'";
   escondido = "'hidden'";
}
else if (is.ie || document.all) {
   layerstart = "document.all.";
   layerleft = ".left";
   layertop = ".top";
   layerstyle = ".style";
   visivel = "'visible'";
   escondido = "'hidden'";
}

var speed0, speed1, OrbitSize1, Orbitfator1;
var speed2, OrbitSize2, Orbitfator2;
function setup() {
Xorigin1 = 90;
Yorigin1 = 270;
speed0 = -0.06*1.8
speed1 = speed0;
OrbitSize1 = 120;
Orbitfator1 = OrbitSize1/speed1;

Xorigin2 = 400;
Yorigin2 = 270;
speed2 = speed1*1.5;
OrbitSize2 = 120;
Orbitfator2 = OrbitSize2/speed2;

//run();
//setTimeout('visib()', 1000)
altVelo();
visib();
}

function altVelo() {
	var fator = 0.8*relacao;
	if (fator>2.8) {fator=2.8;}
	if (fator<0.7) {fator=0.7;}
	if (relacao==1) {fator=1;}
	speed2 = fator*speed1;		
	OrbitSize2 = Orbitfator2*speed2;
}

Count1 = new Array ( 5.67, 5.04, 4.41, 3.78, 3.15, 2.52, 1.89, 1.26, .63, 0 );
Xpoint1 = new Array ( 5.67, 5.04, 4.41, 3.78, 3.15, 2.52, 1.89, 1.26, .63, 0 );
Ypoint1 = new Array ( 5.67, 5.04, 4.41, 3.78, 3.15, 2.52, 1.89, 1.26, .63, 0 );

Count2 = new Array ( 5.67, 5.04, 4.41, 3.78, 3.15, 2.52, 1.89, 1.26, .63, 0 );
Xpoint2 = new Array ( 5.67, 5.04, 4.41, 3.78, 3.15, 2.52, 1.89, 1.26, .63, 0 );
Ypoint2 = new Array ( 5.67, 5.04, 4.41, 3.78, 3.15, 2.52, 1.89, 1.26, .63, 0 );

var timerun;
function run() {
  for ( j = 0 ; j <= 9 ; j++ ) {  
Count1[j] = Count1[j] + (speed1);
Xpoint1[j] = Xorigin1 + (OrbitSize1*Math.cos(Count1[j]));
Ypoint1[j] = Yorigin1 + (OrbitSize1*Math.sin(Count1[j]));  
difftop1[j]  = Ypoint1[j] -  temptoplocation1[j];
diffleft1[j] = Xpoint1[j] - templeftlocation1[j];
temptoplocation1[j] = temptoplocation1[j] + difftop1[j]/30;
templeftlocation1[j] = templeftlocation1[j] + diffleft1[j]/30;
  eval(layerstart+"a"+j+layerstyle+layerleft+" = templeftlocation1["+j+"]");
  eval(layerstart+"a"+j+layerstyle+layertop+" = temptoplocation1["+j+"]");  
  
Count2[j] = Count2[j] + (speed2);
Xpoint2[j] = Xorigin2 + (OrbitSize2*Math.cos(Count2[j]));	
Ypoint2[j] = Yorigin2 + (OrbitSize2*Math.sin(Count2[j]));  
difftop2[j]  = Ypoint2[j] -  temptoplocation2[j];
diffleft2[j] = Xpoint2[j] - templeftlocation2[j];
temptoplocation2[j] = temptoplocation2[j] + difftop2[j]/30;
templeftlocation2[j] = templeftlocation2[j] + diffleft2[j]/30;
  eval(layerstart+"b"+j+layerstyle+layerleft+" = templeftlocation2["+j+"]");
  eval(layerstart+"b"+j+layerstyle+layertop+" = temptoplocation2["+j+"]");  
  }
timerun = setTimeout('run()', 12) 
}

var vis = true;
function visib() {
	var visestilo;
	vis = (vis)? false : true;
	if (vis) {
		visestilo = visivel;
		run();
		document.simulador.visibilid.value = "Off";
	}
	else {
		visestilo = escondido;
		clearTimeout(timerun);
		document.simulador.visibilid.value = "On";
	}
	  
	for ( i = 0 ; i <= 9 ; i++ ) {		
		eval(layerstart+"a"+i+layerstyle+".visibility = "+visestilo);
		eval(layerstart+"b"+i+layerstyle+".visibility = "+visestilo);
	}
}
//Fim do Script de Animação