<?
require_once('flotte_antideadlock.php'); 
if( $test != "ok" ) {
	echo '<script language="javascript" type="text/javascript">
	<!--
	window.location.replace("flotte_baser.php");
	-->
	</script>';
}
include( 'config.php' );

if( !isset($_POST['etape'] ) ) {

echo"<div align=\"center\"><form name=\"colo\" method=\"post\" action=\"colonie.php\" target=\"_parent\">
<table align=\"center\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">
   <tr>
     <td width=\"100\" rowspan=\"2\" valign=\"middle\">
       <div align=\"center\">
         <input name=\"page\" value=\"" . $PHP_SELF . "\" type=\"hidden\" id=\"page\">
         <select name=\"idcolo\" onchange=submit()>";
include ('colo2.php');

	// Affichage du form

	$req = "SELECT * FROM matgraph_nbrevaisseaux WHERE planete = '$planete' AND nbre > 0" ;
	$res = db_mysql_Query($req) ;
	$num = mysql_num_rows($res) ;

	$i = 0 ;
	$militaire = 0;
	$equipage = array();

	if ( $num > 0 )
	{
		echo '<FORM name="baser" method="post" action="' . $PHP_SELF . '" target="_parent">
<input type="hidden" name="etape" value="1">
<table width="618" border="0" cellspacing="0" cellpadding="4" style="font-family:trebuchet ms,sans-serif; font-size: 9pt ;">
<tr>
<td colspan="6" align="center" style="font-size: 10pt ;"><B>';

		echo 'BASEMENT DE FLOTTE
</B><br>&nbsp;</td>
</tr>
<tr>
<td colspan="6" align="center">
Saisir le nombre de vaisseaux à envoyer.<br>
Si le nombre saisi est supérieur à ce qui est disponible pour ce type de vaisseau,<br>
le maximum disponible sera mobilisé automatiquement.<br>&nbsp;
</td>
</tr>
<tr style ="background-color:#222222;">
	<td align="left"><b>Vaisseau</b></td>
	<td align="center"><b>Disponible</b></td>
	<td align="center"><b>Equipage</b></td>
	<td align="center"><b>A envoyer</b></td>
</tr>';

		While ( $row = mysql_fetch_array($res) )
		{
			$sql2 = "SELECT * FROM matgraph_vaisseaux WHERE id='$row[vaisseau]'" ;
			$res2 = db_mysql_Query($sql2) ;
			$row2 = mysql_fetch_array($res2);

			// gère le quadrillage du tableau
			if (($i/2) == ceil($i/2))
			{
				echo '<tr style ="background-color:#111111;">';
			} else {
				echo '<tr>';
			}

	    	echo '<td onMouseOver="bullepop(\'bulle' . $i . '\',event)" onMouseOut="couic()">' . $row2[nom] . '</td>' ;

			$liste_infra_par_lvl = unserialize($LISTE_INFRA_LVL);

			if( $liste_infra_par_lvl[$row2['chantier_lvl']] == '' ) {
				// L infra n existe pas

				echo '<script language=javascript> alert ("Votre vaisseau ('.$row2['nom'].') est incorrect");</script>';
				echo '<meta http-equiv="refresh" content="0; url=flotte.php" />';
				exit;
			} else {

				global ${'INFRA_' . $liste_infra_par_lvl[$row2['chantier_lvl']] . '_EQUIPAGE'};
				$besoin = ${'INFRA_' . $liste_infra_par_lvl[$row2['chantier_lvl']] . '_EQUIPAGE'};

				$militaire += ($equipage[] = $besoin) * $row6['total'];

				echo '<td align="center">' . ( number_format($row['nbre'], 0, ',', ' ') ) . '</td>' ;
			}

	    	echo '<td align="center">' . number_format($equipage[$i], 0, ',', ' ') . '</td>' ;



			echo'
	<input type="hidden" name="max'. $i .'" value="' . $row['nbre'] . '"/>
	<input type="hidden" name="id'. $i .'" value="' . $row['vaisseau'] . '"/>
<td align="center">
	<input name="Transit_case_'. $i . '" type="text" size="6" maxlength="6" value=""><a href="javascript:maxvaisseau('. $i .');" > max</a>
</td>' ;

			echo '</tr>' ;
			++$i ;
		}
	}
	else
	{
		echo'
		<FORM name="baser" method="post" action="' . $PHP_SELF . '" target="_parent">
		<table width="618"  border="0" cellspacing="0" cellpadding="4"  style="font-family:trebuchet ms,sans-serif; font-size: 9pt ;">
		<tr>
			<td colspan="6" align="center" style="font-size: 10pt ;"><B>BASEMENT DE FLOTTE</B><br>&nbsp;</td>
		</tr>
		<tr>
			<td colspan="6" align="center">Aucun vaisseau construit à envoyer.</td>
		</tr>';
	}

	echo '
	<tr>
		<td colspan="6" align="center"><hr></td>
	</tr>';


	$sql2 = "SELECT mili_nbre FROM matgraph_users WHERE id='$planete'" ;
	$res2 = db_mysql_Query($sql2) ;
	$row2 = mysql_fetch_array($res2) ;

	$mili_nbre = floor($row2['mili_nbre']) ;


	if (($i/2) != ceil($i/2))
	{
		echo '<tr style ="background-color:#111111;">' ;
	} else {
		echo '<tr>';
	}
	echo '
		<td align="left">Militaires :</td>
		<td>&nbsp;</td>
		<td align="center">' . number_format($mili_nbre, 0, ',', ' ') . '</td>
		<td align="center">automatique</td>
	</tr>';


	// Coordonnée

	echo '
	<tr>
		<td colspan="6" align="center"><hr></td>
	</tr>';

	echo '
	<tr>
		<td align="left"><b>Coordonnées :</b></td>
		<td align="center"><b>Galaxie</b></td>
		<td align="center"><b>Systeme</b></td>
		<td align="center"><b>Position</b></td>
	</tr>
	<tr>
		<td>&nbsp;</td>
		<td align="center">
			<input type="text" name="galaxie" size="4" maxlength="3" value="">
		</td>
		<td align="center">
			<input type="text" name="systeme" size="4" maxlength="3" value="">
		</td>
		<td align="center">
			<input type="text" name="position" size="4" maxlength="3" value="">
		</td>
	</tr>';

	echo '
	<tr>
		<td colspan="6" align="center"><hr></td>
	</tr>';

	echo '
	<tr>
		<td align="left"><b>Transport :</b></td>
		<td align="center"><b>Fer</b></td>
		<td align="center"><b>Or</b></td>
		<td align="center"><b>Cristal</b></td>
	</tr>
	<tr>
		<td>&nbsp;</td>
		<td align="center">
			<input type="text" name="fer" size="7" maxlength="10" value="">
		</td>
		<td align="center">
			<input type="text" name="or" size="7" maxlength="10" value="">
		</td>
		<td align="center">
			<input type="text" name="cristal" size="7" maxlength="10" value="">
		</td>
	</tr>
	<tr>
		<td align="left"></td>
		<td align="center"><b>Naquadah</b></td>
		<td align="center"><b>Militaire</b></td>
		<td align="center"></td>
	</tr>
	<tr>
		<td>&nbsp;</td>
		<td align="center">
			<input type="text" name="naquadah" size="7" maxlength="10" value="">
		</td>
		<td align="center">
			<input type="text" name="militaire" size="7" maxlength="10" value="">
		</td>
		<td align="center"></td>
	</tr>';


	echo '
	<tr>
		<td colspan="6" align="center"><br>' ;

		if ($i != 0) {
			echo '<input type="Submit" name="Submit" value="Envoyer" style="cursor:cross;" class="button">' ;
		} else {
			echo '<input type="Submit" name="Submit" value="Envoyer" disabled="disabled" style="cursor:default;" class="button">' ;
		}

		echo '&nbsp;&nbsp;&nbsp;&nbsp;' ;

		if ($Stock_i != 0) {
			echo '<input type="reset" name="Submit" value="Reinitialiser" style="cursor:cross;" class="button">' ;
		} else {
			echo '<input type="reset" name="Submit" value="Reinitialiser" disabled="disabled" style="cursor:default;" class="button">' ;
		}

	echo '
		</td>
	</tr>
	</table>
	</FORM>';

	echo '<SCRIPT LANGUAGE="JavaScript" type="text/javascript" SRC="Js_module/Infobulle.js"></SCRIPT>' ;
	echo "<script type='text/javascript'>
	function maxvaisseau(id) {
	    document.getElementsByName(\"Transit_case_\" + id)[0].value = document.getElementsByName(\"max\" + id)[0].value;
	}
	</script>";

	$_SESSION['page'] = "baser";

} else {


	if( !isset($_POST['galaxie']) ||
		!isset($_POST['systeme']) ||
		!isset($_POST['position']) ||
		!isset($_POST['id0']) ||
		!isset($_POST['fer']) ||
		!isset($_POST['or']) ||
		!isset($_POST['cristal']) ||
		!isset($_POST['naquadah']) ||
		!isset($_POST['militaire']) ||
		!isset($_SESSION['page'])
		) {

		// Erreur

		header('location:flotte.php');
		echo '<meta http-equiv="refresh" content="0; url=flotte.php" />';
		exit;
	}

	$id_vaisseau = array();
	$nb_vaisseau = array();
	$ok_vaisseau = array();

	for( $i = 0 ; isset($_POST['id'.$i]) && isset($_POST['Transit_case_'.$i]) ; ++$i ) {
		if( preg_match('!^[0-9]*$!', $_POST['Transit_case_'.$i]) == 0 ) {
			// Erreur

			header('location:flotte.php');
			echo '<meta http-equiv="refresh" content="0; url=flotte.php" />';
			exit;
		}
		if( $_POST['Transit_case_'.$i] != "" && $_POST['Transit_case_'.$i] != "0" ) {
			$id_vaisseau[] = $_POST['id'.$i];
			$nb_vaisseau[] = ($_POST['Transit_case_'.$i] == "") ? "0" : $_POST['Transit_case_'.$i];
			$ok_vaisseau[] = false;
		}
	}
	$nb_total = 0;

	for( $i = 0 ; $i < count($id_vaisseau) ; ++$i ) {
		$temp = $id_vaisseau[$i];
		settype($temp, "integer");
		$id_vaisseau[$i] = $temp;

		$temp = $nb_vaisseau[$i];
		settype($temp, "integer");
		$nb_total += ($nb_vaisseau[$i] = $temp);

		if( $nb_vaisseau[$i] < 0 ) {
			// Erreur

			header('location:flotte.php');
			echo '<meta http-equiv="refresh" content="0; url=flotte.php" />';
			exit;
		}
	}
	if( $nb_total <= 0 ) {
		// Erreur

		header('location:flotte.php');
		echo '<meta http-equiv="refresh" content="0; url=flotte.php" />';
		exit;
	}


	if( $_POST['etape'] == 1 ) {

	echo '<FORM name="aller" method="post" action="' . $PHP_SELF . '" target="_parent">';
	echo'
	<table width="618" border="0" cellspacing="0" cellpadding="4" style="font-family:trebuchet ms,sans-serif; font-size: 9pt ;">
	<tr>
		<td colspan="5" align="center" style="font-size: 10pt ;"><B>RAPPORT INTERMEDIAIRE</B><br>&nbsp;</td>
	</tr>
	<tr style ="background-color:#222222;">
		<td align="left"><b>Vaisseau</b></td>
		<td align="center"><b>Attaque</b></td>
		<td align="center"><b>Bouclier</b></td>
		<td align="center"><b>Coque</b></td>
		<td align="center"><b>Demandé</b></td>
		<td align="center"></td>
	</tr>';
	}



	$sql_a = "SELECT id, idjoueur FROM matgraph_users WHERE id='$planete' ";
	$res_a = db_mysql_Query($sql_a) or die('erreur sql<br>'.$sql_a.'<br>'.mysql_error()) ;
	$row_a = mysql_fetch_array($res_a) ;

	$Id_joueur = $row_a['idjoueur'] ;

	// Calcul total du nombre de militaires nécessaire
	$req_b = "SELECT * FROM matgraph_nbrevaisseaux WHERE planete ='$planete' AND nbre > 0" ;
	$res_b = db_mysql_Query($req_b) or die('erreur sql<br>'.$req_b.'<br>'.mysql_error()) ;


	$nbr_vaisseaux_total = 0 ;
	While ( $row_b = mysql_fetch_array($res_b) )
	{

		for( $i = 0 ; $i < count($id_vaisseau) ; ++$i ) {

			if( $row_b['vaisseau'] == $id_vaisseau[$i] ) {
				// Found
				if( $nb_vaisseau[$i] > $row_b['nbre'] ) {
					$nb_vaisseau[$i] = $row_b['nbre'];
					$ok_vaisseau[$i] = true;
				} else {
					$ok_vaisseau[$i] = true;
				}
				break;
			}
		}

	}

	for( $i = 0 ; $i < count($ok_vaisseau) ; ++$i ) {
		if( $ok_vaisseau[$i] == false ) {
			// Erreur

			header('location:flotte.php');
			echo '<meta http-equiv="refresh" content="0; url=flotte.php" />';
			exit;
		}
	}

	// Calcul equipage + vitesse + fret

	$equipage = array();
	$equipage_total = 0;
	$nom_vaisseau = array();
	$att_vaisseau = array();
	$bou_vaisseau = array();
	$coq_vaisseau = array();
	$fret_total = 0;

	$vitesse = false;

	for( $i = 0 ; $i < count($id_vaisseau) ; ++$i ) {

		$sql2 = "SELECT * FROM matgraph_vaisseaux WHERE id='".$id_vaisseau[$i]."' LIMIT 1";
		$res2 = db_mysql_Query($sql2) or die('erreur sql<br>'. $sql2 .'<br>'.mysql_error());
		$row2 = mysql_fetch_array($res2);

		$liste_infra_par_lvl = unserialize($LISTE_INFRA_LVL);

		if( $liste_infra_par_lvl[$row2['chantier_lvl']] == '' ) {
			// L infra n existe pas

			echo '<script language=javascript> alert ("Votre vaisseau ('.$row2['nom'].') est incorrect");</script>';
			echo '<meta http-equiv="refresh" content="0; url=flotte.php" />';
			exit;

		} else {

			global ${'INFRA_' . $liste_infra_par_lvl[$row2['chantier_lvl']] . '_EQUIPAGE'};
			$besoin = ${'INFRA_' . $liste_infra_par_lvl[$row2['chantier_lvl']] . '_EQUIPAGE'};

			$equipage_total += ($equipage[] = $besoin) * $nb_vaisseau[$i];

			$nom_vaisseau[] = $row2['nom'];
			$att_vaisseau[] = $row2['arme'];
			$bou_vaisseau[] = $row2['bouclier'];
			$coq_vaisseau[] = $row2['coque'];

			$fret_total += $row2['place']*$nb_vaisseau[$i];

			if( $vitesse === false || $vitesse > $row2['vitesse'] ) {
				$vitesse = $row2['vitesse'];
			}

		}

	}
	if($vitesse < 0.05) {
		$vitesse = 0.05;
	}
	if( $vitesse === false ) {

		if( $_POST['etape'] == 1 ) {

			echo '
</table>
<b>Vitesse des vaisseaux incorrects</b>';

exit;

		} else {
			// Erreur

			header('location:flotte.php');
			echo '<meta http-equiv="refresh" content="0; url=flotte.php" />';
			exit;
		}

	}

	if( $_POST['etape'] == 1 ) {

		$att_total = 0;
		$bou_total = 0;
		$coq_total = 0;
		$nb_total = 0;

		for( $i = 0 ; $i < count($id_vaisseau) ; ++$i ) {

			echo '
<tr>
	<td align="left">'.$nom_vaisseau[$i].'</td>
	<td align="center">'.number_format($att_vaisseau[$i], 0, ',', ' ').'</td>
	<td align="center">'.number_format($bou_vaisseau[$i], 0, ',', ' ').'</td>
	<td align="center">'.number_format($coq_vaisseau[$i], 0, ',', ' ').'</td>
	<td align="center">'.number_format($nb_vaisseau[$i], 0, ',', ' ').'</td>
</tr>';

			$att_total += $att_vaisseau[$i]*$nb_vaisseau[$i];
			$bou_total += $bou_vaisseau[$i]*$nb_vaisseau[$i];
			$coq_total += $coq_vaisseau[$i]*$nb_vaisseau[$i];
			$nb_total += $nb_vaisseau[$i];

		}

	echo '
<tr>
	<td colspan="6" align="center"><hr></td>
</tr>';

	echo '
<tr>
	<td align="left"><b>Total</b></td>
	<td align="center"><b>'.number_format($att_total, 0, ',', ' ').'</b></td>
	<td align="center"><b>'.number_format($bou_total, 0, ',', ' ').'</b></td>
	<td align="center"><b>'.number_format($coq_total, 0, ',', ' ').'</b></td>
	<td align="center"><b>'.number_format($nb_total, 0, ',', ' ').'</b></td>
</tr>';


	} else {

		$att_total = 0;
		$bou_total = 0;
		$coq_total = 0;
		$nb_total = 0;

		for( $i = 0 ; $i < count($id_vaisseau) ; ++$i ) {

			$att_total += $att_vaisseau[$i]*$nb_vaisseau[$i];
			$bou_total += $bou_vaisseau[$i]*$nb_vaisseau[$i];
			$coq_total += $coq_vaisseau[$i]*$nb_vaisseau[$i];
			$nb_total += $nb_vaisseau[$i];

		}

	}

	// Position

	if( !ereg("^[0-9]+$", $_POST['galaxie']) ||
		!ereg("^[0-9]+$", $_POST['systeme']) ||
		!ereg("^[0-9]+$", $_POST['position'])
		) {

		if( $_POST['etape'] == 1 ) {

			echo '
		<tr>
			<td colspan="6" align="center"><hr></td>
		</tr></table></form>';

			echo '
<br>Coordonnées invalides<br>';

			exit;

		} else {
			// Erreur

			header('location:flotte.php');
			echo '<meta http-equiv="refresh" content="0; url=flotte.php" />';
			exit;
		}

	}

	// Coordonnées ok, verification joueur

	$sql_a = "SELECT a.id AS id, a.idjoueur AS idjoueur, b.vacance AS vacance, b.pts AS pts FROM matgraph_users AS a, matgraph_users AS b WHERE a.galaxi=".$_POST['galaxie']." AND a.system=".$_POST['systeme']." AND a.position=".$_POST['position']." AND a.idjoueur = b.id";
	$res_a = db_mysql_Query($sql_a) or die('erreur sql<br>'.$sql_a.'<br>'.mysql_error()) ;

	if( !$row_a = mysql_fetch_array($res_a) ) {

		if( $_POST['etape'] == 1 ) {

			echo '
		<tr>
			<td colspan="6" align="center"><hr></td>
		</tr></table></form>';

			echo '
<br>Aucune planète habitée à ces coordonnées<br>';

			exit;

		} else {

			// Erreur

			header('location:flotte.php');
			echo '<meta http-equiv="refresh" content="0; url=flotte.php" />';
			exit;

		}

	}

	// On verifie qu elle appartienne au joueur

	if( $row_a['idjoueur'] != $idlogin ) {

		if( $_POST['etape'] == 1 ) {

			echo '
		<tr>
			<td colspan="6" align="center"><hr></td>
		</tr></table></form>';

			echo '
<br>Cette planete n\'est pas a vous<br>';

			exit;

		} else {

			// Erreur

			header('location:flotte.php');
			echo '<meta http-equiv="refresh" content="0; url=flotte.php" />';
			exit;

		}

	}

	if( $row_a['vacance'] != '0000-00-00 00:00:00' ) {

		if( $_POST['etape'] == 1 ) {

			echo '
		<tr>
			<td colspan="6" align="center"><hr></td>
		</tr></table></form>';

			echo '
<br>Vous êtes en mode vacances<br>';

			exit;

		} else {

			// Erreur

			header('location:flotte.php');
			echo '<meta http-equiv="refresh" content="0; url=flotte.php" />';
			exit;

		}

	}



	// Calcul distance + verif equipage

	$sqlcc = "SELECT galaxi,system,position, fer_ressource, or_ressource, ada_ressource, ura_ressource, mili_nbre FROM matgraph_users WHERE id='$planete' ";
	$rescc = db_mysql_Query($sqlcc)or die('erreur sql<br>'.$sqlcc.'<br>'.mysql_error());
	$rowcc = mysql_fetch_array($rescc);

	if( !$rowcc ) {

		if( $_POST['etape'] == 1 ) {

			echo '
		<tr>
			<td colspan="6" align="center"><hr></td>
		</tr></table></form>';

			echo '
<br>Problème avec votre compte<br>';

			exit;

		} else {

			// Erreur

			header('location:flotte.php');
			echo '<meta http-equiv="refresh" content="0; url=flotte.php" />';
			exit;

		}

	}

	if( $rowcc['mili_nbre'] < $equipage_total + $_POST["militaire"] ) {

		if( $_POST['etape'] == 1 ) {

			echo '
		<tr>
			<td colspan="6" align="center"><hr></td>
		</tr></table></form>';

			echo '
<br>Vous n\'avez pas assez de militaires<br>';

			exit;

		} else {

			// Erreur

			header('location:flotte.php');
			echo '<meta http-equiv="refresh" content="0; url=flotte.php" />';
			exit;

		}

	}

	$Diff_position = $_POST['position'] - $rowcc[position] ;
	if ($Diff_position < 0) { $Dif_position = -$Diff_position ; }
	else { $Dif_position = $Diff_position ; }

	$Diff_system = $_POST['systeme'] - $rowcc[system] ;
	if ($Diff_system < 0) { $Dif_system = -$Diff_system ; }
	else { $Dif_system = $Diff_system ; }

	$Diff_galaxi = $_POST['galaxie'] - $rowcc[galaxi] ;
	if ($Diff_galaxi < 0) { $Dif_galaxi = -$Diff_galaxi ; }
	else { $Dif_galaxi = $Diff_galaxi ; }

	if(($Dif_system + $Dif_galaxi) == 0)
	{
		$temps_position = $Dif_position*0.5 ;
		$temps = (600 * ( 5 + $temps_position) / $vitesse)/10 ;
	}elseif($Dif_galaxi == 0)
	{
		$temps_position = $Dif_position*0.5 ;
		$temps_system = $Dif_system*1 ;
		$temps = (600 * ( 20 + $temps_position + $temps_system) / $vitesse)/13.5 ;
	}else
	{
		$temps_position = $Dif_position*0.03 ;
		$temps_system = $Dif_system*0.3 ;
		$temps_galaxi = $Dif_galaxi*30 ;
		$temps = (600 * ( 40 + $temps_position + $temps_system + $temps_galaxi) / $vitesse)/10 ;
	}

	$carbu = (($temps / 3600) * $vitesse) * 300 * 10 ;
	$carbu_total = $carbu * $nb_total ;

	if( $_POST['etape'] == 1 ) {


		echo '
	<tr>
		<td colspan="6" align="center"><hr></td>
	</tr>';

		echo '
	<tr>
		<td align="left"><b>Equipage</b></td>
		<td>&nbsp;</td>
		<td>&nbsp;</td>
		<td>&nbsp;</td>
		<td colspan="1" align="center">'.number_format($equipage_total, 0, ',', ' ').'</td>
	</tr>';


		echo '
	<tr>
		<td colspan="6" align="center"><hr></td>
	</tr>';

		echo '
	<tr>
		<td align="left"><b>Consommation</b></td>
		<td>&nbsp;</td>
		<td>&nbsp;</td>
		<td>&nbsp;</td>
		<td colspan="1" align="center">'.number_format($carbu_total, 0, ',', ' ').'</td>
	</tr>';

	}

	// Carburant ok ?

	if( $rowcc['ura_ressource'] < $carbu_total ) {

			if( $_POST['etape'] == 1 ) {

				echo '
			<tr>
				<td colspan="6" align="center"><hr></td>
			</tr></table></form>';

				echo '
	<br>Vous n\'avez pas assez de Naquadah<br>';

				exit;

			} else {

				// Erreur

				header('location:flotte.php');
				echo '<meta http-equiv="refresh" content="0; url=flotte.php" />';
				exit;

			}

	} else {

		$rowcc['ura_ressource'] -= $carbu_total;

	}

	// Calcul Temps

	$temps_tot = $temps;
	$temps_min = $temps_tot / 60;

	if( floor($temps_min) != $temps_min ) {
		$temps_sec = ($temps_min - floor($temps_min)) * 60;
		$temps_sec = floor($temps_sec);

		$temps_min = floor($temps_min);
	}

	if( $temps_min > 60 ) {

		$temps_heure = floor($temps_min / 60);
		$temps_min -= $temps_heure * 60;

		if( $temps_heure > 24 ) {
			$temps_jour = floor($temps_heure / 24);
			$temps_heure -= $temps_jour * 24;
		}

	}




	if( $_POST['etape'] == 1 ) {

		echo '
	<tr>
		<td colspan="6" align="center"><hr></td>
	</tr>';

		echo '
	<tr>
		<td><b>Temps de trajet</b></td>
		<td>&nbsp;</td>
		<td>&nbsp;</td>
		<td>&nbsp;</td>
		<td colspan="1" align="center">'.
			( isset($temps_jour) ? $temps_jour . " jour(s) " : "" ) .
			( isset($temps_heure) ? $temps_heure . " heure(s) " : "" ) .
			( isset($temps_min) ? $temps_min . " minute(s) " : "" ) .
			( isset($temps_sec) ? $temps_sec . " seconde(s) " : "" ) .
		'</td>
	</tr>';

	}


	// Transport
	$_POST['fer'] = floor($_POST['fer']);
	$_POST['or'] = floor($_POST['or']);
	$_POST['cristal'] = floor($_POST['cristal']);
	$_POST['naquadah'] = floor($_POST['naquadah']);
	$_POST['militaire'] = floor($_POST['militaire']);
	
	if( !ereg("^[0-9]*$", $_POST['fer']) ||
		!ereg("^[0-9]*$", $_POST['or']) ||
		!ereg("^[0-9]*$", $_POST['cristal']) ||
		!ereg("^[0-9]*$", $_POST['naquadah']) ||
		!ereg("^[0-9]*$", $_POST['militaire'])
		) {


			if( $_POST['etape'] == 1 ) {

				echo '
			<tr>
				<td colspan="6" align="center"><hr></td>
			</tr></table></form>';

				echo '
	<br>Fret entré incorrect<br>';

				exit;

			} else {

				// Erreur

				header('location:flotte.php');
				echo '<meta http-equiv="refresh" content="0; url=flotte.php" />';
				exit;

			}

	}

	$_POST['fer'] = ($_POST['fer'] == "") ? 0 : $_POST['fer'];
	$_POST['or'] = ($_POST['or'] == "") ? 0 : $_POST['or'];
	$_POST['cristal'] = ($_POST['cristal'] == "") ? 0 : $_POST['cristal'];
	$_POST['naquadah'] = ($_POST['naquadah'] == "") ? 0 : $_POST['naquadah'];
	$_POST['militaire'] = ($_POST['militaire'] == "") ? 0 : $_POST['militaire'];



	$fret_fer = ($_POST['fer'] > $rowcc['fer_ressource']) ? $rowcc['fer_ressource'] : $_POST['fer'];
	$fret_or = ($_POST['or'] > $rowcc['or_ressource']) ? $rowcc['or_ressource'] : $_POST['or'];
	$fret_cristal = ($_POST['cristal'] > $rowcc['ada_ressource']) ? $rowcc['ada_ressource'] : $_POST['cristal'];
	$fret_naquadah = ($_POST['naquadah'] > $rowcc['ura_ressource']) ? $rowcc['ura_ressource'] : $_POST['naquadah'];
	$fret_militaire = ($_POST['militaire'] > $rowcc['mili_nbre']) ? $rowcc['mili_nbre'] : $_POST['militaire'];

	if( $fret_total > $fret_fer ) {

		$fret_total -= $fret_fer;

	} else {

		$fret_fer = $fret_total;

		$fret_total = 0;

	}

	if( $fret_total > $fret_or ) {

		$fret_total -= $fret_or;

	} else {

		$fret_or = $fret_total;

		$fret_total = 0;

	}

	if( $fret_total > $fret_cristal ) {

		$fret_total -= $fret_cristal;

	} else {

		$fret_cristal = $fret_total;

		$fret_total = 0;

	}

	if( $fret_total > $fret_naquadah ) {

		$fret_total -= $fret_naquadah;

	} else {

		$fret_naquadah = $fret_total;

		$fret_total = 0;

	}

	if( $fret_total > $fret_militaire ) {

		$fret_total -= $fret_militaire;

	} else {

		$fret_militaire = $fret_total;

		$fret_total = 0;

	}

	if( $_POST['etape'] == 1 ) {

		echo '
	<tr>
		<td colspan="6" align="center"><hr></td>
	</tr>';

		echo '
	<tr>
		<td align="left"><b>Fret</b></td>
		<td align="center">Fer</td>
		<td align="center">Or</td>
		<td align="center">Cristal</td>
		<td align="center">Naquadah</td>
		<td align="center">Militaire</td>
	</tr>';

		echo '
	<tr>
		<td>&nbsp;</td>
		<td align="center">'.number_format($fret_fer, 0, ',', ' ').'</td>
		<td align="center">'.number_format($fret_or, 0, ',', ' ').'</td>
		<td align="center">'.number_format($fret_cristal, 0, ',', ' ').'</td>
		<td align="center">'.number_format($fret_naquadah, 0, ',', ' ').'</td>
		<td align="center">'.number_format($fret_militaire, 0, ',', ' ').'</td>
	</tr>';

	}



	// Tout est fini ?

	//Remplissage formulaire etape1

	if( $_POST['etape'] == 1 ) {
		echo '
	<input type="hidden" name="galaxie" value="'.$_POST['galaxie'].'">
	<input type="hidden" name="systeme" value="'.$_POST['systeme'].'">
	<input type="hidden" name="position" value="'.$_POST['position'].'">';

		for( $i = 0 ; $i < count($id_vaisseau) ; ++$i ) {

			echo '
	<input type="hidden" name="id'.$i.'" value="'.$id_vaisseau[$i].'">
	<input type="hidden" name="Transit_case_'.$i.'" value="'.$nb_vaisseau[$i].'">';
		}

		echo '
	<input type="hidden" name="fer" value="'.$fret_fer.'">
	<input type="hidden" name="or" value="'.$fret_or.'">
	<input type="hidden" name="cristal" value="'.$fret_cristal.'">
	<input type="hidden" name="naquadah" value="'.$fret_naquadah.'">
	<input type="hidden" name="militaire" value="'.$fret_militaire.'">';

		echo '
	<input type="hidden" name="etape" value="2">';

		echo '
	<br>
	<tr>
		<td colspan="5" align="center"><br>
		<input type="submit" name="Submit" value="Valider" style="cursor:cross;" class="button">
		<input type="button" value="Annuler" onClick="parent.location=\'flotte_baser.php\'"  style="cursor:cross;" class="button">
		</td>
	</tr>';

	} else {

		//echo "<br>GG Spaz<br>";

		//unset ($_SESSION['page']);

		// On enleve le carbu
		$totalMilitaire = $equipage_total + $fret_militaire ;
		$sql = "
		SELECT mili_nbre 
		FROM matgraph_users
		WHERE id='".$planete."'";
		$res = db_mysql_Query($sql)or die('erreur sql<br>'.$sql.'<br>'.mysql_error());
		$row = mysql_fetch_array($res);
		if($row['mili_nbre'] < $totalMilitaire) { exit; }
		
		$sql = "UPDATE matgraph_users
SET fer_ressource = fer_ressource - ".$fret_fer.",
or_ressource = or_ressource - ".$fret_or.",
ada_ressource = ada_ressource - ".$fret_cristal.",
ura_ressource = ura_ressource - ".$carbu_total." - ".$fret_naquadah.",
mili_nbre = mili_nbre - ".$totalMilitaire."
WHERE id='$planete'";
		$res = db_mysql_Query($sql)or die('erreur sql<br>'.$sql.'<br>'.mysql_error());

		$temps_voyage = $temps_tot;
		$temps_arrivee = strftime("%Y-%m-%d %H:%M:%S", time() + $temps_tot);

		for( $i = 0 ; $i < count($id_vaisseau) ; ++$i ) {

			// Suppressions nbrevaisseaux
			$sql = "UPDATE matgraph_nbrevaisseaux
SET nbre = nbre - ".$nb_vaisseau[$i]."
WHERE vaisseau = ".$id_vaisseau[$i]." AND planete = ".$planete;
			$res = db_mysql_Query($sql)or die('erreur sql<br>'.$sql.'<br>'.mysql_error());

			// Insertions mobil
			$sql = "INSERT INTO matgraph_mobil
VALUES ('', '".$temps_voyage."', '".$temps_arrivee."', '".$planete."', '".$row_a['id']."', '".$id_vaisseau[$i]."', '".$nb_vaisseau[$i]."', 'fort', 'baser', 0, 0, 0, 0, 0)";

			$res = db_mysql_Query($sql)or die('erreur sql<br>'.$sql.'<br>'.mysql_error());

			if( $fret_fer != 0 || $fret_or != 0 || $fret_cristal != 0 || $fret_naquadah != 0  || $fret_militaire != 0 ) {

				$sql = "UPDATE matgraph_mobil
SET fret_fer = " . $fret_fer . ",
fret_or = " . $fret_or . ",
fret_ada = " . $fret_cristal . ",
fret_ura = " . $fret_naquadah . ",
fret_militaire = " . $fret_militaire . " 
WHERE id = " . mysql_insert_id();

				$res = db_mysql_Query($sql)or die('erreur sql<br>'.$sql.'<br>'.mysql_error());

				$fret_fer = $fret_or = $fret_cristal = $fret_naquadah = $fret_militaire = 0;

			}

		}

	echo '<script language="javascript" type="text/javascript">
	<!--
	window.location.replace("flotte.php");
	-->
	</script>';
	}


	if( $_POST['etape'] == 1 ) {
	echo'
	</table>
	</FORM>' ;

	}


}

?>