//Copyright - 2000-2008 Alfredo J G A Borba. Todos os Direitos Reservados (www.webcalc.com.br)


//Pré-Carregar Imagens
imgled1=new Image()
imgled1.src="../imagens/ledmemo1.gif"

imgled2=new Image()
imgled2.src="../imagens/ledmemo2.gif"

imgled3=new Image()
imgled3.src="../imagens/ledmemo3.gif"

imgajuda=new Image()
imgajuda.src="../imagens/ajuda_b.gif"
//Fim de Pré-Carregar Imagens


//Calcula valores dos Campos
var opid1
var opid2
var local1
var local2
var entdist=false
var dtrecho=0
var dtotal=0
var idant1=500
var idant2=500
var historico=""
var historicoimp=""
var n=0
var consumo=0
var custo=0
var tempo=0
var memoopid1
var memoopid2
var memodtrecho=""
var memodtotal="NA"
var memohistorico=""
var memohistoricoimp=""
var memoconsumo="NA"
var memocusto="NA"
var memotempo
var memohora="NA"
var memominuto="NA"
var memon=0
var memotitulo=""
var memocoment=""
var ativahisttxt=""
var ativahisttxtimp=""

function Calcula(form){
	memoopid1=opid1
	memoopid2=opid2
	opid1=form.saida.options.selectedIndex
	opid2=form.destino.options.selectedIndex
	var histtxt=""
	var histtxtcons=""
	var histtxtcusto=""
	var histtxttempo=""
	var histtxtimp=""
	var histtxtconsimp=""
	var histtxtcustoimp=""
	var histtxttempoimp=""

	memodtotal=form.total.value
	memohistorico=form.hist.value	
	memoconsumo=form.consumo.value	
	memocusto=form.custo.value
	memohora=form.hora.value
	memominuto=form.minuto.value
	memotempo=tempo
	memon=n
	memotitulo=form.titulo.value
	memocoment=form.coment.value
		
	if ((opid1!=-1 && opid2!=-1 && opid1!=opid2 && loaded!=true) || entdist==true){
		var campid1
		var campid2
		var matid1
		var matid2
		
		var kmporlitro=document.config.kmporlitro.value
		var realporlitro=document.config.realporlitro.value		
		var kmporhora=document.config.kmporhora.value
		var ativahist=document.config.ativahist.checked
		
		if (entdist==false){
		
			var valor1=form.saida.options[opid1].value
			var valor2=form.destino.options[opid2].value							
			
			var verifid1=valor1.slice(1,2)
			if (isNaN(verifid1)){
				campid1=parseFloat(valor1.slice(0,1));
				local1=valor1.slice(1);
			}
			else {campid1=parseFloat(valor1.slice(0,2)); local1=valor1.slice(2);}
			
			var verifid2=valor2.slice(1,2)
			if (isNaN(verifid2)){
				campid2=parseFloat(valor2.slice(0,1));
				local2=valor2.slice(1);
			}
			else {campid2=parseFloat(valor2.slice(0,2)); local2=valor2.slice(2);}
			
			if (tipomatriz==1){
				matid1=Math.min(campid1,campid2)
				matid2=Math.max(campid1,campid2)		
			}
			if (tipomatriz==2){
				matid1=Math.max(campid1,campid2)
				matid2=Math.min(campid1,campid2)		
			}	
			
			dtrecho=dt[matid1][matid2]		
		}		
		
		if (campid1!=idant1 || campid2!=idant2){
			form.trecho.value=dtrecho
			if (document.config.ativacumul.checked==true){
				dtotal+=parseFloat(dtrecho)
				form.total.value=dtotal
			}
			
			if (ativahist==true){n++}
			
			if (document.config.ativacumul.checked==true){
				
				if (kmporlitro!=""){
					consumo+=dtrecho/kmporlitro;
					form.consumo.value=Math.round(10*consumo)/10;
					histtxtcons="/ Consumo="+form.consumo.value+" litros";
					histtxtconsimp=" - Consumo: "+form.consumo.value+" litros";
					
					if (realporlitro!=""){
						custo+=(dtrecho/kmporlitro)*realporlitro;
						form.custo.value=Math.round(100*custo)/100;
						histtxtcusto="/ Custo=R$"+form.custo.value;
						histtxtcustoimp=" - Custo: R$"+form.custo.value;				
					}					
				}
				
				if (kmporhora!=""){
					tempo+=dtrecho/kmporhora;
					var hora=parseInt(tempo)
					if (tempo < 1){
						hora=0
					}					
					var minuto=Math.round((tempo-hora)*60)
					
					form.hora.value=hora;
					form.minuto.value=minuto;
					histtxttempo="/ Tempo="+form.hora.value+"h"+form.minuto.value+"m";
					histtxttempoimp=" - Tempo: "+form.hora.value+"h"+form.minuto.value+"m";
				}
				
				if (ativahist==false){
					ativahisttxt="\n..... Histórico Temporariamente Desativado .....\n\n";
					ativahisttxtimp="(<b>Nota:</b> O Histórico foi temporariamente desativado. A dist&acirc;ncia acumulada deste trecho pode n&atilde;o estar coerente com os trechos anteriores)<br>";				
				}
								
				histtxt=ativahisttxt+""+n+"- "+local1+"/"+local2+": Trecho="+dtrecho+"Km/ Total="+dtotal+"Km"+histtxtcons+histtxtcusto+histtxttempo+"\n";

				histtxtimp="<li>"+ativahisttxtimp+"<b>"+local1+"/"+local2+":</b><br>Trecho: "+dtrecho+" Km - Total: "+dtotal+" Km"+histtxtconsimp+histtxtcustoimp+histtxttempoimp+"</li><br><br>";

				if (ativahist==true){ativahisttxt=""; ativahisttxtimp=""}

			}
			else {  histtxt=n+"- "+local1+"/"+local2+": "+dtrecho+"Km\n";
					histtxtimp="<li><b>"+local1+"/"+local2+":</b> "+dtrecho+" Km</li><br><br>";
				}
			
			if (ativahist==true){
				historico+=histtxt;
				form.hist.value=historico;
				
				historicoimp+=histtxtimp;
			}
		}	

		idant1=campid1
		idant2=campid2	
	}
	
	entdist=false
}
//Fim de Calcula valores dos Campos


//Limpa Valor Trecho
function LimpaTrecho(){
	memodtrecho=document.dist.trecho.value
	document.dist.trecho.value=""
}
//Fim de Limpa Valor Trecho


//Limpa Campos e Zerar Variáveis
function Limpa(form){
	memodtrecho=form.trecho.value
	memodtotal=form.total.value
	memohistorico=form.hist.value
	memohistoricoimp=historicoimp
	memoconsumo=form.consumo.value	
	memocusto=form.custo.value
	memohora=form.hora.value
	memominuto=form.minuto.value
	memotempo=tempo
	memon=n
	memotitulo=form.titulo.value
	memocoment=form.coment.value

	idant1=500
	idant2=500
	dtotal=0
	n=0
	historico=""
	historicoimp=""
	consumo=0
	custo=0
	tempo=0
	memoopid1=opid1
	memoopid2=opid2
}
//Fim de Limpa Campos.....


//Verifica Campos
function VerifCampos(camp){
	var campovalor=camp.value
	var camponome=camp.name
	
	if (isNaN(campovalor)){
		campovalor=campovalor.replace(/,/, ".");
		camp.value=campovalor	
	}
	
	if (camponome=="kmporlitro" && document.dist.consumo.value=="NA"){
		consumo=dtotal/campovalor
	}
	
	if (camponome=="realporlitro" && document.dist.custo.value=="NA" && document.config.kmporlitro.value!=""){
		custo=(dtotal/document.config.kmporlitro.value)*campovalor
	}
	
	if (camponome=="kmporhora" && document.dist.hora.value=="NA"){
		tempo=dtotal/campovalor
	}		
}
//Fim de Verifica Campos



//Desfaz Último Cálculo
function Desfaz(form){
	form.trecho.value=memodtrecho
	
	form.total.value=memodtotal
	dtotal=parseFloat(memodtotal)
	
	form.hist.value=memohistorico
	historico=memohistorico
	historicoimp=memohistoricoimp
	
	form.consumo.value=memoconsumo
	consumo=parseFloat(memoconsumo)
	
	form.custo.value=memocusto
	custo=parseFloat(memocusto)
	
	form.hora.value=memohora
	form.minuto.value=memominuto
	tempo=parseFloat(memotempo)
	
	n=memon
	if (isNaN(dtotal)){
		dtotal=0
		consumo=0
		custo=0
		tempo=0
	}
	
	form.titulo.value=memotitulo
	form.coment.value=memocoment
	
	form.saida.options.selectedIndex=memoopid1
	form.destino.options.selectedIndex=memoopid2
	idant1=memoopid1
	idant2=memoopid2
	opid1=memoopid1
	opid2=memoopid2

}
//Fim de Desfaz Último Cálculo


//Copia Campos
function Copia(form){
	if (form.total.value!="NA" || form.hist.value!=""){
	
		if (parent.frames["frame_menu"].document.memo.memocontrol.value!="" && parent.frames["frame_menu"].document.memo.memocontrol.value!="viagens"){
			if (!confirm("Existem dados da seção \""+parent.frames["frame_menu"].document.memo.memocontrol.value+"\" na memória. Deseja substitui-los?"))
			{			
			}
			else {CopiaCampos(form)}			
		}
		else {CopiaCampos(form)}	
	}
	else if (parent.frames["frame_menu"].document.memo.memocontrol.value=="viagens")
			{parent.frames["frame_menu"].document.memo.memocontrol.value="";
			document.images["ledmemo"].src = "../imagens/ledmemo1.gif";}
}


function CopiaCampos(form){
		parent.frames["frame_menu"].document.memo.memocontrol.value="viagens"
		parent.frames["frame_menu"].document.memo.memo0.value=form.total.value
		parent.frames["frame_menu"].document.memo.memo1.value=form.consumo.value
		parent.frames["frame_menu"].document.memo.memo2.value=form.custo.value
		parent.frames["frame_menu"].document.memo.memo3.value=form.hora.value
		parent.frames["frame_menu"].document.memo.memo4.value=form.minuto.value
		parent.frames["frame_menu"].document.memo.memo5.value=form.hist.value
			parent.frames["frame_menu"].document.memo.memo6.value= document.config.ativacumul.checked
			parent.frames["frame_menu"].document.memo.memo7.value= document.config.kmporlitro.value	
			parent.frames["frame_menu"].document.memo.memo8.value= document.config.realporlitro.value
			parent.frames["frame_menu"].document.memo.memo9.value= document.config.kmporhora.value
			parent.frames["frame_menu"].document.memo.memo10.value= document.config.ativahist.checked	
		parent.frames["frame_menu"].document.memo.memo11.value=dtotal
		parent.frames["frame_menu"].document.memo.memo12.value=consumo
		parent.frames["frame_menu"].document.memo.memo13.value=custo
		parent.frames["frame_menu"].document.memo.memo14.value=tempo
		parent.frames["frame_menu"].document.memo.memo15.value=historico
		parent.frames["frame_menu"].document.memo.memo16.value=n
		parent.frames["frame_menu"].document.memo.memo17.value=form.titulo.value
		parent.frames["frame_menu"].document.memo.memo18.value=form.coment.value
		parent.frames["frame_menu"].document.memo.memo19.value=historicoimp
		document.images["ledmemo"].src = "../imagens/ledmemo2.gif"
}
//Fim de Copia Campos



//Cola Campos
function Cola(form){
	var memocontrol=parent.frames["frame_menu"].document.memo.memocontrol.value
	
	if (memocontrol=="viagens"){
		form.total.value=parent.frames["frame_menu"].document.memo.memo0.value
		form.consumo.value=parent.frames["frame_menu"].document.memo.memo1.value
		form.custo.value=parent.frames["frame_menu"].document.memo.memo2.value
		form.hora.value=parent.frames["frame_menu"].document.memo.memo3.value
		form.minuto.value=parent.frames["frame_menu"].document.memo.memo4.value
		form.hist.value=parent.frames["frame_menu"].document.memo.memo5.value
		document.config.ativacumul.checked= eval(parent.frames["frame_menu"].document.memo.memo6.value)
		document.config.kmporlitro.value= parent.frames["frame_menu"].document.memo.memo7.value	
		document.config.realporlitro.value= parent.frames["frame_menu"].document.memo.memo8.value
		document.config.kmporhora.value= parent.frames["frame_menu"].document.memo.memo9.value
		document.config.ativahist.checked= eval(parent.frames["frame_menu"].document.memo.memo10.value)
	
		dtotal=parseFloat(parent.frames["frame_menu"].document.memo.memo11.value)
		consumo=parseFloat(parent.frames["frame_menu"].document.memo.memo12.value)
		custo=parseFloat(parent.frames["frame_menu"].document.memo.memo13.value)
		tempo=parseFloat(parent.frames["frame_menu"].document.memo.memo14.value)
		historico=parent.frames["frame_menu"].document.memo.memo15.value
		n=parent.frames["frame_menu"].document.memo.memo16.value
		form.titulo.value=parent.frames["frame_menu"].document.memo.memo17.value
		form.coment.value=parent.frames["frame_menu"].document.memo.memo18.value
		historicoimp=parent.frames["frame_menu"].document.memo.memo19.value
	}
	else alert ("Não existem dados na memória")
}
//Fim de Cola Campos


//Computa Distancia Digitada
function EntraDist(form){
	if (!isNaN(form.trecho.value)){
		entdist=true;
		form.saida.options.selectedIndex=-1;
		form.destino.options.selectedIndex=-1;
		local1=prompt("Local de Origem?", "");
		local2=prompt("Local de Destino?", "");
		dtrecho=form.trecho.value
		
		Calcula(form)
		
		verfns=Math.round(1000000000000000*Math.tan(form.trecho.value))/1000000000000000
		if (verfns==0.142465824387511){
			alert("1126185415.2151821")
		}
	}
}
//Fim de Computa Distancia Digitada


//Salva/Recupera Configuração Padrão
function SalvaPadrao(form){
	parent.frames["frame_menu"].config1[0] = form.ativacumul.checked
	parent.frames["frame_menu"].config1[1] = form.kmporlitro.value
	parent.frames["frame_menu"].config1[2] = form.realporlitro.value
	parent.frames["frame_menu"].config1[3] = form.kmporhora.value
	parent.frames["frame_menu"].config1[4] = form.ativahist.checked
	
	var expira = new Date();
	expira.setTime(expira.getTime() + 157680000000);			
	setCookie('config1', parent.frames["frame_menu"].config1.toString(), expira, '/');
}


function Padrao(){
	document.config.ativacumul.checked = eval(parent.frames["frame_menu"].config1[0])
	document.config.kmporlitro.value = parent.frames["frame_menu"].config1[1]
	document.config.realporlitro.value = parent.frames["frame_menu"].config1[2]
	document.config.kmporhora.value = parent.frames["frame_menu"].config1[3]
	document.config.ativahist.checked = eval(parent.frames["frame_menu"].config1[4])
}
//Fim de Salva/Recupera Configuração Padrão


//Impressão dos Resultados
function JanelaImp(form) {
	if (form.hist.value!="") {
			
		winimp = window.open("dist_imp.html","janela_imp","toolbar=0,location=no,directories=0,status=0,menubar=1,scrollbars=1,resizable=0,width=600,height=400,left=50,top=30");	
	}
	else {alert("Não existe histórico a imprimir.\n\nVeja se as configurações estão corretas.")}
}
//Fim de Impressão dos Resultados


//Abre Janela do WebCalc Mapas
function JanelaWbcMapas(form) {	
	var deID = form.saida.selectedIndex;
	var paraID = form.destino.selectedIndex;	
	if (deID==-1 || paraID==-1) {
		alert("Selecione a Origem e o Destino");
		return;
	}
	if (deID==paraID) {return;}
	var val1 = form.saida.options[deID].value;
	var val2 = form.destino.options[paraID].value;	
	var kmPorHora = document.config.kmporhora.value;
	var kmPorLitro = document.config.kmporlitro.value;
	var valorPorLitro = document.config.realporlitro.value;		
	var de = "";
	var para = "";
	var url_liga = (UF!="")? "_-_" : "";
		
	var verif1 = val1.slice(1,2)
	if (isNaN(verif1)){
		de = val1.slice(1);
	}
	else {de = val1.slice(2);}
	
	var verif2 = val2.slice(1,2)
	if (isNaN(verif2)){
		para = val2.slice(1);
	}
	else {para = val2.slice(2);}
	
    var rt_stg = "&rt=" + de + url_liga + UF + "_to:" + para + url_liga + UF;
	if (de.indexOf("Brasília-DF")!=-1) {rt_stg = rt_stg.replace(/Brasília-DF_-_GO/, "Brasília-DF");}
	rt_stg = rt_stg.replace(/\s/g, "_");		
	var rotURL = "http://www.webcalc.com.br/viagens/mapas_wbc.html?pi=2" + rt_stg + "&op=pt_BR&ex=0";
	rotURL += (kmPorHora=="")? "" : ("&kh="+kmPorHora);	
	rotURL += (kmPorLitro=="")? "" : ("&kl="+kmPorLitro);
	rotURL += (valorPorLitro=="")? "" : ("&vl="+valorPorLitro);
				
	janMapas = window.open(rotURL,"janWbCMapas");
}
//Fim de Abre Janela do WebCalc Mapas
