<?php
include ('logue.php'); 
include ('entete2.php');
include ('ressource.php');
include ('entete.php');
echo"
<STYLE TYPE=text/css>
.Style1 {color: #0000FF}
.Style2 {color: #FF0000}
input {
border:1px solid #7E7E7E;
background-color:#000000;
font-family:'trebuchet ms',sans-serif;
color:white;
}
textarea {
border:1px solid #7E7E7E;
background-color:#000000;
font-family:'trebuchet ms',sans-serif;
color:white;
}
select, option {
background-color:#000000;
color:white;
}
A.menulink2 {
width: 150px;
text-decoration: none;
font-family:Verdana;
font-size:11px;
color: #FF3300;
BORDER: none;
border: solid 1px #000000;
}

A.menulink2:hover {
border: solid 1px #000000;
background-color:#000000;
}
</STYLE>";
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'); 

$sql = "SELECT * FROM matgraph_ally WHERE id='$row2[ally]'";
$res = db_mysql_Query($sql)or die('erreur sql<br>'.$sql.'<br>'.mysql_error());
$row = mysql_fetch_array($res);

if($row[idfondateur] !== $idlogin) {
	echo '<script language="javascript" type="text/javascript">
	<!--
	window.location.replace("ally_index.php");
	-->
	</script>';
	exit;
}

$idceder = htmlentities ($_POST["ceder"]);
if($idceder > 0) {
	$sql9 = "SELECT id,login,ally FROM matgraph_users WHERE id = '$idceder' AND id = idjoueur ORDER BY id ASC limit 1";
	$res9 = db_mysql_Query($sql9) or die('Erreur SQL!<br>'.$sql9.'<br>'.mysql_error());
	$row9 = mysql_fetch_array($res9);
	
	if($row9[ally] == $row2[ally]) {
		//mis a jours du nouveau fondateur
		$requete = "UPDATE matgraph_users SET statut = '-1' WHERE id = $row9[id]";
		$resultat = db_mysql_Query($requete)or die('erreur sql<br>'.$requete.'<br>'.mysql_error());
		// maj de l'ally
		$requete = "UPDATE matgraph_ally SET idfondateur = '$row9[id]' WHERE id = $row9[ally]";
		$resultat = db_mysql_Query($requete)or die('erreur sql<br>'.$requete.'<br>'.mysql_error());
		//maj de l'ex fondateur
		$requete = "UPDATE matgraph_users SET ally = 'no' , statut = '0' WHERE id = $row2[id]";
		$resultat = db_mysql_Query($requete)or die('erreur sql<br>'.$requete.'<br>'.mysql_error());
		
		echo '<script language=javascript> alert ("L\'alliance a bien été cedé.");</script>';
		echo '<script language="javascript" type="text/javascript">
		<!--
		window.location.replace("ally_index.php");
		-->
		</script>';
		exit;
	}
}
else { 
	echo"<div align=\"left\"><a href=\"ally_index.php\" target=\"_parent\"> Retour</a></div><br>";
	echo"<div align=\center\"><form action=\"ally_ceder.php\" method=\"post\" name=\"form1\" target=\"_parent\">
	<select name=\"ceder\" id=\"ceder\">";
	$req = "SELECT id,login FROM matgraph_users WHERE ally='$row2[ally]' AND id = idjoueur AND id!=$idlogin ORDER BY id ASC";
	$res = db_mysql_Query($req);
	$num = mysql_num_rows($res);
	
	if ($num > 0) {
		While ( $row = mysql_fetch_array($res) ) {
			echo"<option value=\"".$row['id']."\">".$row['login']."</option>";
		}
	}
	echo"</select>
	<input type=\"submit\" name=\"Submit\" value=\"Ceder\"></form>";
}
?>

