<?
include_once("../include/main.inc.php");
check_session('MyCpanel','index.php');

################ Paging Starts From Here ##################
@extract($_REQUEST);
$start = (intval($start)==0 or $start=="")?$start=0:$start;
$pagesize = intval($pagesize)==0?$pagesize=20:$pagesize;

$record_type="Country";
$script_name="manage_countries.php";
$table_name="country_tbl";
####################################################

if($id=="add_record" && $countryName!="") {
	$numCnt=getCount($table_name," where countryName='$countryName'");
	if($numCnt>0) {
		$_SESSION[session_message]=$record_type.' already exist';
		header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&pagesize=".$pagesize);
		exit;
	}
	if($_FILES[countryFlag][name]!="") {
		$file_extension=strtolower(getExtension($_FILES[countryFlag][name]));
		if($file_extension!="jpg" && $file_extension!="gif" && $file_extension!="jpeg" && $file_extension!="png") {
			$form_check_alert .="<li>Please upload small image(.jpg,.gif,.png Format Only)</li>";
		}
	}
	if(!strlen($form_check_alert)>0) {
		if($_FILES[countryFlag][name]!="") {
			$img_name="flag-".time(); $img1=upload_file($img_name,"countryFlag","uploaded_files/flags");
		}
		$sql=db_query("insert into $table_name (countryName, countryFlag)values('$countryName', '$img1')");
		$_SESSION[session_message]='New '.$record_type.' successfully added';
		header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&pagesize=".$pagesize);
		exit;
	}
	else {
		$msg=html_head();
		$msg.=$form_check_alert;
		$msg.=html_foot();
	}
}

if($id=="update_record") {
	$numCnt=getCount($table_name," where countryName='$countryName' and slno!='$recordID'");
	if($numCnt>0) {
		$_SESSION[session_message]=$record_type.' already exist';
		header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&pagesize=".$pagesize);
		exit;
	}
	if($_FILES[countryFlag][name]!="") {
		$file_extension=strtolower(getExtension($_FILES[countryFlag][name]));
		if($file_extension!="jpg" && $file_extension!="gif" && $file_extension!="jpeg" && $file_extension!="png") {
			$form_check_alert .="<li>Please upload small image(.jpg,.gif,.png Format Only)</li>";
		}		
	}
	if(!strlen($form_check_alert)>0) {
		if($_FILES[countryFlag][name]!="") {
			$res1=getResult($table_name," where slno='$recordID'");
			$filenm1=SITE_DIR_PATH."/uploaded_files/flags/".$res1[countryFlag];
			if (file_exists($filenm1)) {
				unlink($filenm1);
			}
			$img_name="flag-".time();
			$img1=upload_file($img_name,"countryFlag","uploaded_files/flags");
			updateTable($table_name," countryFlag='$img1' where slno='$recordID'");
		}		
		updateTable($table_name," countryName='$countryName' where slno='$recordID'");
		$_SESSION[session_message]=$record_type.' has been updated successfully';
		header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&pagesize=".$pagesize);
		exit;
	}
	else {
		$msg=html_head();
		$msg.=$form_check_alert;
		$msg.=html_foot();
	}	
}

if($id=="delete_record") {
	db_query("delete from $table_name where slno='$recordID'");
	$_SESSION[session_message]=$record_type.' is deleted successfully';
	header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&pagesize=".$pagesize);
	exit;
}

admin_header("");
?>
<table width="99%" border="0" align="center" cellpadding="0" cellspacing="0" valign="top">
 <tr>
	<td align="left" width="50%"><strong><b class="myTitle">Manage Countries</b></strong></td>
	<td align="right">
	 <form name="pgimg_form" action="<?=$PHP_SELF?>" method="post" style="margin:0px;padding:0px;">
	 <table width="98%" border="0" align="center" cellpadding="0" cellspacing="0" valign="top">
	  <tr>
	   <td align="right">Records Per Page:
	    <select name="pagesize" onchange="this.form.submit();">
	     <option value="0">Select</option>
	     <?php
	     foreach($_GLOBALS[pgsize_arr] as $val) {
		     if(trim($pagesize)==trim($val)) {
			     echo '<option value="'.$val.'" selected>'.$val.'</option>';
		     }
		     else{
			     echo '<option value="'.$val.'">'.$val.'</option>';
		     }
	     }
	     ?>
	    </select>	    
		 </td>
		</tr>
	 </table>
	 </form>
	</td>
 </tr>
</table>
<hr size="2" width="99%" color="#DA251C">

<?
$qry1= db_query("select * from $table_name");
$reccnt = NumRows($qry1);
$qry= db_query("select * from $table_name order by slno limit $start, $pagesize");
echo pageNavigation($reccnt,$pagesize,$start);
print_message();
?>

<table width="99%" border="0" cellpadding="2" cellspacing="2" class="lightBorder">
 <?
 if($reccnt>0) {
	 ?>
	 <tr height="25" bgcolor="#EAEAEA">
	  <td width="5%" class="PadFive" align="center"><strong>S.No.</strong></td>
	  <td class="PadFive" width="50%" align="left"><strong>Country Name</strong></td>
	  <td class="PadFive" width="30%" align="center"><strong>Country Flag</strong></td>
	  <td class="PadFive" width="15%" align="center"><strong>Options</strong></td>
	 </tr>
	 <?
	 $sl=$start+1;
	 while($arr=mysql_fetch_array($qry)) {
		 $arr=ms_htmlentities_decode($arr);
		 if($sl%2!=0) {
			 $color="#f9f9f9";
		 }
		 else{
			 $color="#f0f0f0";
		 }		 
		 ?>		 
		 <tr bgcolor="<?=$color;?>">
		  <td align="center"><?=$sl;?></td>
		  <td align="left"><?=$arr[countryName];?></td>
		  <td align="center">
		   <?
		   if($arr[countryFlag]!="") {
			   $filenm=SITE_URL_PATH."/uploaded_files/flags/".$arr[countryFlag];
			   ?>
			   <img name="flg" src="<?=$filenm;?>" alt="<?=$arr[countryName];?> Flag" title="<?=$arr[countryName];?> Flag" border="0">
			   <?			   
		   }
		   else {
			   ?>
			   <strong>Flag Not Uploaded</strong>
			   <?
		   }
		   ?>
			</td>
		  <td align="center">&nbsp;<a href="<?=$script_name;?>?id=edit_record&start=<?=$start;?>&pagesize=<?=$pagesize?>&recordID=<?=$arr[slno];?>#bot"><img src="<?=SITE_ADMIN_URL?>/images/edit.gif" alt="Edit This <?=$record_type;?>" title="Edit This <?=$record_type;?>" border="0"></a>&nbsp;<a href="<?=$script_name;?>?id=delete_record&start=<?=$start;?>&pagesize=<?=$pagesize?>&recordID=<?=$arr[slno];?>" onClick="return confirm('Are you sure to delete this record...');"><img src="<?=SITE_ADMIN_URL?>/images/cancel.gif" alt="Delete This <?=$record_type;?>" border="0"></td>
		 </tr>
		 <?
		 $sl++;
	 }
 }
 else{
	 ?>
	 <tr><td><b>No Record Found........</b></td></tr>
	 <?
 }
 ?>
</table><br/>
<table width="96%" align="center" cellpadding="0" cellspacing="0" border="0">
 <tr><td align="right"><input type="button" onclick="window.location.href='<?=SITE_ADMIN_URL;?>/<?=$script_name;?>?id=add_new#bot'" value="Add New <?=$record_type;?>" class="button"></td></tr>
</table>

<?php
if ($id == "add_new" || $id == "edit_record") {
	?>
	<a name="bot"></a>
	<br/><br/>
	<form name="imageFrm" method=post action="<?=$script_name;?>" enctype="multipart/form-data" onsubmit="return validate(this);">
	<table width="99%"  border="1" cellpadding="2" cellspacing="2" class="lightBorder">
	 <?
	 /*
	 if($recordID=="" || $id!="edit_record") {
		 ?>
		 <tr bgcolor="#EAEAEA"><td colspan="2" align="left" class="PadFive" height="20"><b><a name="bot">Add New <?=$record_type;?></a></b></td></tr>
		 <?
	 }
	 else {
		 */
		 ?>
		 <tr bgcolor="#EAEAEA">
		  <td align="left" class="PadFive" height="20"><a name="bot"></a><b>Edit <?=$record_type;?></b></td>
		  <td align="right" class="PadFive" height="20"> <!-- <a href="<?=$script_name;?>"><b>Add New <?=$record_type;?></b></a> --> </td>
		 </tr>
		 <?
	 //}
	 $res=getResult($table_name," where slno='$_GET[recordID]'");
	 ?>
	 <tr>
	  <td class="brdrbtm" valign="top" width="15%"><b>Flag Image *</b></td>
	  <td class="brdrbtm" valign="top">
	   <?
	   if($res[countryFlag]!="") {
		   ?>
		   <input name="countryFlag" type="file" size="40" class="button">
		   <?
		   $file1=SITE_DIR_PATH."/uploaded_files/flags/".$res[countryFlag];
		   if(file_exists($file1)) {
			   $dimens=GetImageSize($file1);
			   if($dimens[0]>130) {
				   $dimens[0]=$dimens[0]+50;
				   $dimens[1]=$dimens[1]+80;
			   }
			   else{
				   $dimens[0]=200;
				   $dimens[1]=200;
			   }
			   echo "&nbsp;<a href=\"javascript:void(0);\" onClick=\"openwin('".SITE_ADMIN_URL."/popup.php?img=flags/".$res[countryFlag]."', '$dimens[0]', '$dimens[1]')\" class=\"linkgray fontbold\"><b>View Flag</b></a>"; 
		   }
	   }
	   else {
		   ?>
		   <input name="countryFlag" type="file" size="40" class="button" id="NOBLANK~Please upload flag image.~DM~">
		   <?
	   }
	   ?>
	   <br/>
	   [Flag Size] : <strong>Width 20 Pixels</strong> Fixed, and <strong>Height 13 Pixels</strong> (+/-) Adjustable.   
	  </td>
	 </tr>
	 <tr>
	  <td class="brdrbtm"><b>Country Name *</b></td>
	  <td class="brdrbtm"><input type="text" name="countryName" size="60" value="<?=$res[countryName];?>" id="NOBLANK~Please enter country name.~DM~" /></td>
	 </tr>
	 
	 <tr>
	  <td class="brdrbtm"></td>
	  <td class="brdrbtm">
	   <?
	   if($recordID!="") {
		   ?>
		   <input type="submit" name="submit" value="Update <?=$record_type;?>" class="button">
		   <input type="hidden" name="recordID" value="<?=$res[slno];?>">
		   <input type="hidden" name="id" value="update_record">
		   <?	   
	   }
	   else{
		   ?>
		   <input type="submit" name="submit" value="Add New <?=$record_type;?>" class="button">
		   <input type="hidden" name="id" value="add_record">
		   <?
	   }
	   ?>   
	  </td>
	 </tr>
	</table>
	</form>
	<?php
}

admin_footer();
exit;
?>