<?	
if(isset($_POST['equipe1']) && isset($_POST['equipe2']) && $_POST['equipe1']!='' && $_POST['equipe2']!='' && $_POST['debut']!=''){
	$alliance1 = $_POST["equipe1"];
	$alliance2 = $_POST["equipe2"];
	$debut = $_POST["debut"];
	
	$tableau_alliance1=split(';',$alliance1);
	$tableau_alliance2=split(';',$alliance2);
	array_pop($tableau_alliance1);
	array_pop($tableau_alliance2);
	$nom_alliance1='';
	foreach($tableau_alliance1 as $valeur_id){
		$query="SELECT nom FROM matgraph_ally WHERE id='".$valeur_id."'";
		$sql=mysql_query($query);
		list($nom)=mysql_fetch_array($sql);
		if($nom_alliance1==""){
			$nom_alliance1=$nom."(".$valeur_id.")";
		}else{
			$nom_alliance1.="-".$nom."(".$valeur_id.")";
		}
	}
	$nom_alliance2='';
	foreach($tableau_alliance2 as $valeur_id){
		$query="SELECT nom FROM matgraph_ally WHERE id='".$valeur_id."'";
		$sql=mysql_query($query);
		list($nom)=mysql_fetch_array($sql);
		if($nom_alliance2==""){
			$nom_alliance2=$nom."(".$valeur_id.")";
		}else{
			$nom_alliance2.="-".$nom."(".$valeur_id.")";
		}
	}

	$req = "SELECT id FROM matgraph_guerre WHERE (alliance1 = '".$alliance1."' AND alliance2 = '".$alliance2."') OR (alliance1 = '".$alliance2."' AND alliance2 = '".$alliance1."')";
	$res = mysql_query($req) or die('Erreur SQL! <br>'.$req.'<br>'.mysql_error());
	$num = mysql_num_rows($res);
	if ( $num == 0 ){
		$requete = "INSERT INTO matgraph_guerre VALUES ('', '".$alliance1."', '".$alliance2."', '".$debut."' )";
		$resultat = mysql_query($requete)or die('erreur sql<br>'.$requete.'<br>'.mysql_error());
		
		$sql4 = "INSERT INTO `matgraph_logadmin` ( `id` , `mdj` , `date` , `action` ) VALUES (
		'', '".$login."',
		 now(), 'Ajout guerre | ".addslashes($nom_alliance1)." Vs ".addslashes($nom_alliance2)."')";
		$res4 = mysql_query($sql4)or die('erreur sql<br>'.$sql4.'<br>'.mysql_error());
	}else{
		echo '<script type="text/javascript"> alert ("Guerre déjà en cours");</script>';
	}
	echo '<script type="text/javascript">
<!--
window.location.replace("index.php?action=guerre");
-->
</script>';
}elseif( isset($_REQUEST['recherche'])){
	echo "<form name=\"result_search-".$_REQUEST['form']."\"><input type=\"hidden\" id=\"result\" value=\"";
	if($_REQUEST['recherche']!=''){
		$query="SELECT id,nom,tag FROM matgraph_ally WHERE nom LIKE '".$_REQUEST['recherche']."%'";
	}else{
		$query="SELECT id,nom,tag FROM matgraph_ally";
	}
	$sql=mysql_query($query) or die(mysql_error());
	while($row=mysql_fetch_array($sql)){
		$result.=$row['nom']." [".$row['tag']."]~".$row['id'].";";
	}
	echo $result."\" /></form>";
}else{
echo '
<div align="center"><h3>Ajout d\'une Guerre</h3></div><br/><br/><br/>
<div id="resultat1" style="display:none;"></div>
<div align="center">Equipe Alliance 1</div>
<FORM NAME="alliance1">
  <div align="center">
  <input type="text" name="search" />
  <input type="button" value="Filtrer" onclick="search_ally(this.form.search,this.form.equipeposs,\'resultat1\')" /><br/><br/>
  <SELECT NAME="equipeposs" SIZE=8 style="width:250px;">';
	$query="SELECT id,nom,tag FROM matgraph_ally";
	$sql=mysql_query($query);
	while($row=mysql_fetch_array($sql)){
		echo "<option value=\"".$row['id']."\">".$row['nom']."[".$row['tag']."]</option>";
	}
	echo '
  </SELECT>
    <SELECT NAME="equipefin" size="8" style="width:250px;">
    </SELECT>
	<div style="width:80px;height:30px;">
   <input type="button" onclick="selectmenu(this.form.equipeposs,this.form.equipefin)" value="=&gt;" />
   <input type="button" onclick="deletemenu(this.form.equipefin)" value="&lt;=" />
   </div>
  </div>
</FORM>
<br/>
<div id="resultat2" style="display:none;"></div>
<div align="center">Equide Alliance 2</div>
<FORM NAME="alliance2">
  <div align="center" style="height:250px;">
  <input type="text" name="search" />
  <input type="button" value="Filtrer" onclick="search_ally(this.form.search,this.form.equipeposs,\'resultat2\')" /><br/><br/>
  <SELECT NAME="equipeposs" SIZE=8 style="width:250px;">';
	$query="SELECT id,nom,tag FROM matgraph_ally";
	$sql=mysql_query($query);
	while($row=mysql_fetch_array($sql)){
		echo "<option value=\"".$row['id']."\">".$row['nom']."[".$row['tag']."]</option>";
	}
echo '
  </SELECT>
    <SELECT NAME="equipefin" size="8" style="width:250px;">
    </SELECT>
	<div style="width:80px;height:30px;">
   <input type="button" onclick="selectmenu(this.form.equipeposs,this.form.equipefin)" value="=&gt;" />
   <input type="button" onclick="deletemenu(this.form.equipefin)" value="&lt;=" />
   </div>
  </div>
</FORM><div align="center">
<form action="" method="post" name="guerre">
	<input type="hidden" name="equipe1" />
	<input type="hidden" name="equipe2" />
	Date début : <input type="text" name="debut" value ="0000-00-00 00:00:00" /><br/><br/>
	<input type="button" value="Ajouter la Guerre" onclick="format_guerre(0)">
</form></div><br/>';
}  
?>