<?php
include('header.php');
echo $h;


$action = $_GET['action'];
if(!isset($action)) $action = "add";

IF ($action=="add"){
?>
<form name="vbform" method="post" action="country_cp.php?action=insert" enctype="multipart/form-data">
<table class='xtable' width="100%" id="AutoNumber1">
     <tr>
      <td width="100%" colspan="5" class='xtx' align="center"><?php echo $LANGAUGE_CP[212]; ?></td>
    </tr>
    <tr>
      <td width="24%" class='xtr' align="center"><?php echo $LANGAUGE_CP[213]; ?></td>
      <td width="22%" class='xtr' align="center"><?php echo $LANGAUGE_CP[214]; ?></td>
      <td width="22%" class='xtr' align="center"><?php echo $LANGAUGE_CP[215]; ?></td>
      <td width="22%" class='xtr' align="center"><?php echo $LANGAUGE_CP[216]; ?></td>
      <td width="10%" class='xtr' align="center"><?php echo $LANGAUGE_CP[217]; ?></td>
    </tr>
    <tr>
      <td width="24%" class='xtd' align="center"><input size="20" name="name_ar" /></td>
      <td width="22%" class='xtd' align="center"><input size="20" name="name_en" /></td>
      <td width="22%" class='xtd' align="center"><input size="20" name="short" /></td>
      <td width="22%" class='xtd' align="center"><input size="20" name="flags" /></td>
      <td width="10%" class='xtd' align="center"><input size="10" name="orders" /></td>
    </tr>

     <tr>
      <td width="100%" colspan="5" class='xtr' align="center">
      <input type="submit" value=" إضافة " name="cmdAdd" /></td>
    </tr>
  </table>
</form>

<?php
$page = (int) (!isset($_GET["page"]) ? 1 : $_GET["page"]);
$page = ($page == 0 ? 1 : $page);
$perpage = 50;
$startpoint = ($page * $perpage) - $perpage;

$queryx = mysql_query("SELECT * FROM contries order by orders ASC LIMIT $startpoint,$perpage");
$xx = mysql_num_rows($queryx);
if($xx == 0){
echo "";
}else{

echo "<form name='xxx1' action='country_cp.php?action=order' method='post'>
<table class='xtable' width='100%' id='Aut4oNumber15'>
  <tr>
<td width='100%' colspan='7' class='xtt' align='center'>".$LANGAUGE_CP[97]."</td>
</tr>
  <tr>
      <td width='30%' align='center' class='xtr'>".$LANGAUGE_CP[218]."</td>
      <td width='30%' align='center' class='xtr'>".$LANGAUGE_CP[219]."</td>
      <td width='10%' align='center' class='xtr'>".$LANGAUGE_CP[220]."</td>
      <td width='9%' align='center' class='xtr'>".$LANGAUGE_CP[221]."</td>
      <td width='7%' align='center' class='xtr'>".$LANGAUGE_CP[28]."</td>
      <td width='7%' align='center' class='xtr'>".$LANGAUGE_CP[26]."</td>
      <td width='7%' align='center' class='xtr'>".$LANGAUGE_CP[27]."</td>
    </tr>";
while($p = mysql_fetch_array($queryx))
{
$p['name_ar'] = stripslashes($p['name_ar']);
$p['name_ar'] = htmlspecialchars($p['name_ar']);

$p['name_en'] = stripslashes($p['name_en']);
$p['name_en'] = htmlspecialchars($p['name_en']);

$p['short'] = stripslashes($p['short']);
$p['short'] = htmlspecialchars($p['short']);

if($p['active'] == 1){
$im = "images/on.gif";
$li = "country_cp.php?action=actx&id=$p[id]&act=0";
$te = $LANGAUGE_CP[72];
}else{
$im = "images/off.gif";
$li = "country_cp.php?action=actx&id=$p[id]&act=1";
$te = $LANGAUGE_CP[73];
}

echo "<input type='hidden' name='idx[]' value='".$p['id']."' />
<tr>
      <td width='30%' class='xtd' align='center'>".$p['name_ar']."</td>
      <td width='30%' class='xtd' align='center'>".$p['name_en']."</td>
      <td width='10%' class='xtd' align='center'>".$p['short']."</td>
      <td width='9%' align='center' class='xtd'><input style='text-align:center;' size='2' name='order[]' value='".$p['orders']."' /></td>
      <td width='7%' align='center' class='xtd'><a href='$li'><img border='0' src='$im' alt='$te ".$p['name']."' /></a></td>
      <td width='7%' align='center' class='xtd'><a href='country_cp.php?action=edit&id=$p[id]'><img border='0' src='images/edit.gif' alt='".$p['name']."' /></a></td>
      <td width='7%' align='center' class='xtd'><a href='country_cp.php?action=delete&id=$p[id]'><img border='0' src='images/delete.gif' alt='".$p['name']."' /></a></td>
</tr>";
}
echo "<tr>
<td colspan='7' align='center' class='xtr'><input name='activ' type='submit' id='activ' value=' ".$LANGAUGE_CP[221]." ' />
</td>
</tr>";

$pagesnum = @ceil(mysql_num_rows(mysql_query("SELECT * FROM contries")) / $perpage);
if($pagesnum > 1){
echo "<tr>
<td colspan='6' align='center' class='xtr'>";
for ($i=1; $i<=$pagesnum; $i++) {
if ($i != $page) {
$z = "[<a href='country_cp.php?page=".$i."'>$i</a>] ";
} else {
$z = "[<u>$i</u>]";
}
echo "$z";
}
echo "</td>
</tr>";
}

echo "</table>
</form>";

}

}ELSEIF ($action=="insert"){

$name_ar = addslashes($_POST['name_ar']);
$name_ar = trim($name_ar);

$name_en = addslashes($_POST['name_en']);
$name_en = trim($name_en);

$flags = addslashes($_POST['flags']);
$flags = trim($flags);

$short = addslashes($_POST['short']);
$short = trim($short);

$orders = $_POST['orders'];

$ns = mysql_num_rows(mysql_query("SELECT * FROM contries where short='$short'"));

if (empty($name_ar)){
echo $LANGAUGE_CP[222];
}elseif (empty($name_en)){
echo $LANGAUGE_CP[223];
}elseif (empty($short)){
echo $LANGAUGE_CP[224];
}elseif($ns > 0){
echo $LANGAUGE_CP[225];
}else{
$query =  mysql_query ("INSERT INTO contries (name_ar,name_en,flags,short,orders,active) VALUES ('$name_ar','$name_en','$flags','$short','$orders','1')") or die ("ERROR");
print $LANGAUGE_CP[21];
echo "<META HTTP-EQUIV='refresh' CONTENT='1; URL=country_cp.php?action=add' />";
}

}ELSEIF ($action=="order"){
if(isset($_POST['activ'])){

$number = count($_POST['idx']);

for($i=0;$i<$number;$i++){
$id = $_POST['idx'][$i];
$orders = $_POST['order'][$i];
$sqld = mysql_query("UPDATE contries SET orders='".$orders."' WHERE id=$id");
}
echo $LANGAUGE_CP[34];
echo "<META HTTP-EQUIV='refresh' CONTENT='1; URL=country_cp.php' />";
}else{
echo $LANGAUGE_CP[63];
}

}ELSEIF ($action=="actx"){
$id = intval($_GET['id']);
$act = intval($_GET['act']);
if ($act == 0){
$query =  mysql_query("UPDATE contries SET active='$act' where id='$id'");
echo $LANGAUGE_CP[61];
echo "<META HTTP-EQUIV='Refresh' CONTENT='1;URL=country_cp.php?action=add' />";
}elseif($act == 1){
$query =  mysql_query("UPDATE contries SET active='$act' where id='$id'");
echo $LANGAUGE_CP[62];
echo "<META HTTP-EQUIV='Refresh' CONTENT='1;URL=country_cp.php?action=add' />";
}else{
echo $LANGAUGE_CP[63];
echo "<META HTTP-EQUIV='Refresh' CONTENT='1;URL=country_cp.php?action=add' />";
}


}ELSEIF ($action=="edit"){
$id = intval($_GET['id']);
$query = mysql_query("SELECT * FROM contries where id='$id'");
$p = mysql_fetch_array($query);

$p['name_ar'] = stripslashes($p['name_ar']);
$p['name_ar'] = htmlspecialchars($p['name_ar']);

$p['name_en'] = stripslashes($p['name_en']);
$p['name_en'] = htmlspecialchars($p['name_en']);

$p['flags'] = stripslashes($p['flags']);
$p['flags'] = htmlspecialchars($p['flags']);

$p['short'] = stripslashes($p['short']);
$p['short'] = htmlspecialchars($p['short']);

echo "<form name='vbform' method='POST' action='country_cp.php?action=editok'>
<input type='hidden' name='id' value='$p[id]' />

<table class='xtable' width='100%' id='AutoNumber15'>

<tr>
<td width='100%' colspan='5' class='xtx' align='center'>".$LANGAUGE_CP[32]."</td>
</tr>

<tr>
<td width='20%' align='center' class='xtr'>".$LANGAUGE_CP[218]."</td>
<td width='20%' align='center' class='xtr'>".$LANGAUGE_CP[219]."</td>
<td width='20%' align='center' class='xtr'>".$LANGAUGE_CP[220]."</td>
<td width='20%' align='center' class='xtr'>".$LANGAUGE_CP[226]."</td>
<td width='20%' align='center' class='xtr'>".$LANGAUGE_CP[221]."</td>
</tr>

<tr>
<td width='20%' align='center' class='xtd'><input name='name_ar' size='20' value='".$p['name_ar']."' /></td>
<td width='20%' align='center' class='xtd'><input name='name_en' size='20' value='".$p['name_en']."' /></td>
<td width='20%' align='center' class='xtd'><input name='short' size='20' value='".$p['short']."' /></td>
<td width='20%' align='center' class='xtd'><input name='flags' size='20' value='".$p['flags']."' /></td>
<td width='20%' align='center' class='xtd'><input name='orders' size='20' value='".$p['orders']."' /></td>
</tr>

<tr>
<td width='100%' colspan='5' align='center' class='xtr'><input type='submit' value=' ".$LANGAUGE_CP[33]." ' name='B1' /></td>
</tr>

</table>
</form>";

}ELSEIF ($action=="editok"){

$id = intval($_POST['id']);

$name_ar = addslashes($_POST['name_ar']);
$name_ar = trim($name_ar);

$name_en = addslashes($_POST['name_en']);
$name_en = trim($name_en);

$flags = addslashes($_POST['flags']);
$flags = trim($flags);

$short = addslashes($_POST['short']);
$short = trim($short);

$orders = $_POST['orders'];

if (empty($name_ar)){
echo $LANGAUGE_CP[222];
}elseif (empty($name_en)){
echo $LANGAUGE_CP[223];
}elseif (empty($short)){
echo $LANGAUGE_CP[224];
}else{
$query =  mysql_query("UPDATE contries SET name_ar='$name_ar',name_en='$name_en',flags='$flags',short='$short',orders='$orders' where id='$id'");
echo $LANGAUGE_CP[34];
echo "<META HTTP-EQUIV='Refresh' CONTENT='1;URL=country_cp.php?action=add' />";
}

}ELSEIF ($action=="delete"){
$id = intval($_GET['id']);
$query =  mysql_query("DELETE FROM contries WHERE id='$id'");
echo $LANGAUGE_CP[35];
echo "<META HTTP-EQUIV='Refresh' CONTENT='1;URL=country_cp.php?action=add' />";
}
echo $f;
?>