function updateX() {
	ff.ok.value="nouveau";
	ff.aa.value="termine";
}


function calc(nom_debut, nom_fin, x) {
	element=document.getElementById('metal_'+x);

	alert('oops'+nom_debut.value+'/'+nom_fin.value+'/'+element.value);
}


function totaux() {
	metal=0;
	cristal=0;
	deuterium=0;

	for (i=0; i<fin; i++) {
		metal+=document.getElementById('metal_'+i).value*1;
		cristal+=document.getElementById('cristal_'+i).value*1;
		deuterium+=document.getElementById('deuterium_'+i).value*1;
	}
	
	document.getElementById('total_metal').value=Math.floor(metal);
	document.getElementById('total_cristal').value=Math.floor(cristal);	
	document.getElementById('total_deuterium').value=Math.floor(deuterium);
	
	production_metal();
	production_cristal();
	production_deuterium();
}


function element(nom) {
	return (document.getElementById(nom)).value;
}

function majRessources(ligne, metal, cristal, deuterium) {
	document.getElementById('metal_'+ligne).value=Math.floor(metal);
	document.getElementById('cristal_'+ligne).value=Math.floor(cristal);	
	document.getElementById('deuterium_'+ligne).value=Math.floor(deuterium);
}


function ressources(ligne) {
	ressources_debut(ligne);
	totaux();
}

function ressources_debut(ligne) {
	metal=element('metal_0'+ligne);
	cristal=element('cristal_0'+ligne);
	deuterium=element('deuterium_0'+ligne);
	niveau_actuel=element('niveau_actuel_'+ligne);
	niveau_futur=element('niveau_futur_'+ligne);
	coeff=element('coeff_'+ligne);

	power=0;
	i=0;

	if (niveau_futur != 0) {
		delta=niveau_futur - niveau_actuel;
		if (eval(delta) == 1)
			power=Math.pow(coeff, niveau_futur - 1);
		else {
			for (i=eval(niveau_actuel); i<eval(niveau_futur); i++)
				power+=Math.pow(coeff, i);
		}
	}
	
	majRessources(ligne, metal*power, cristal*power, deuterium*power);
}


function ressources_vaisseaux(ligne) {
	ressources_vaisseaux_debut(ligne);
	totaux();
}


function ressources_vaisseaux_debut(ligne) {
	metal=element('metal_0'+ligne);
	cristal=element('cristal_0'+ligne);
	deuterium=element('deuterium_0'+ligne);
	quantite=element('quantite_'+ligne);

	majRessources(ligne, metal*quantite, cristal*quantite, deuterium*quantite);
}




function calcule_duree(prod, total, stock) {
	if ((prod == 0) || (prod == null))
		return 'xx : xx';
	
	if (stock == null)
		stock=0;
	
	if (stock > total)
		return '0 : 00';
	
	ratio=(total-stock)/prod;
	heures=Math.floor(ratio);
	minutes=Math.floor((ratio-heures)*60);
	
	if (minutes < 10)
		return heures+' : 0'+minutes;
	else
		return heures+' : '+minutes;
}


function production_metal() {
	production=element('prod_metal');
	stock=element('stock_metal');
	total=element('total_metal');

	document.getElementById('temps_metal').value=calcule_duree(eval(production), eval(total), eval(stock));
}

function production_cristal() {
	production=element('prod_cristal');
	stock=element('stock_cristal');
	total=element('total_cristal');

	document.getElementById('temps_cristal').value=calcule_duree(eval(production), eval(total), eval(stock));
}

function production_deuterium() {
	production=element('prod_deuterium');
	stock=element('stock_deuterium');
	total=element('total_deuterium');

	document.getElementById('temps_deuterium').value=calcule_duree(eval(production), eval(total), eval(stock));
}


function affiche_detail(ligne, id) {
	window.open('calcul.php?bt='+ligne+'&id='+id, '', 'width=350, height=200, statusbars=no, resizable=yes, scrollbars=yes');
}

function element_parent(nom) {
	return (opener.document.getElementById(nom)).value;
}

function ecrit_ligne(metal, cristal, deuterium, niveau) {
	document.write("<tr>\n");
	document.write("<td class=td0 align=center>"+niveau+" vers "+(niveau+1)+"</td>\n");
	document.write("<td class=td0 align=center>"+metal+"</td>\n");
	document.write("<td class=td0 align=center>"+cristal+"</td>\n");
	document.write("<td class=td0 align=center>"+deuterium+"</td>\n");
	document.write("</tr>\n");
}


function calcul_detail(ligne) {
	metal=element_parent('metal_0'+ligne);
	cristal=element_parent('cristal_0'+ligne);
	deuterium=element_parent('deuterium_0'+ligne);
	niveau_actuel=element_parent('niveau_actuel_'+ligne);
	niveau_futur=element_parent('niveau_futur_'+ligne);
	coeff=element_parent('coeff_'+ligne);

	power=0;
	i=0;

	if (niveau_futur != 0) {
		delta=niveau_futur - niveau_actuel;
		if (eval(delta) == 1) {
			power=Math.pow(coeff, niveau_futur - 1);
			ecrit_ligne(metal*power, cristal*power, deuterium*power, niveau_futur-1);
		}
		else {
			for (i=eval(niveau_actuel); i<eval(niveau_futur); i++) {
				power=Math.pow(coeff, i);
				ecrit_ligne(Math.floor(metal*power), Math.floor(cristal*power), Math.floor(deuterium*power), i);
			}
		}
	}
}


function affiche_aide() {
	window.open('aide.php?', '', 'width=600, height=400, statusbars=no, resizable=yes, scrollbars=yes');
}

function affiche_a_propos() {
	window.open('apropos.php?', '', 'width=420, height=480, statusbars=no, resizable=yes, scrollbars=no');
}

function affiche_avis() {
	window.open('avis.php?', '', 'width=600, height=400, statusbars=no, resizable=yes, scrollbars=yes');
}
