<?php
require_once ("../config.inc.php");
require_once ("../includes/adminlogincheck.inc.php");
require_once ("../includes/mysql.inc.php");
require_once ("../includes/Pager/Pager.php");

$message	= "";
$pages		= "";
$data		= array();
$array		= array();
$page		= isset($_REQUEST['page'])	?	trim($_REQUEST['page'])	:	"1";
$image='<img src="../images/bgar.gif"></img>';
if($order=='DESC') {

    $image='<img src="../images/descar.gif"></img>';
}

else if($order=='ASC') {

    $image='<img src="../images/ascrar.gif" ></img>';

}

if (!$objDB) 
{
	$message	= "Unable to Locate Database Server.";
}//end if
else 
{
	//delete Country
	if(isset($_POST['ids']) && $_POST['ids']!='')
	{
		foreach ($_POST['ids'] as $val)
		{
			//check if it in use
			$check_category=mysql_query("SELECT * FROM ".TABLEPREFIX."state WHERE tc_id='".$val."'") or die (mysql_error());
			if(mysql_num_rows($check_category)>0)
			{
				$message = "This Country cannot be deleted since it is in use!";
			}//end if
			else
			{
				$sqlDel = mysql_query("DELETE FROM ".TABLEPREFIX."country WHERE tc_id='".$val."'") or die(mysql_error());
				$message = "<font color='green'>Country deleted successfully.</font>";
			}//end else
		}//end foreach
	}//end if

	//all country listing
	$query = "SELECT * FROM ".TABLEPREFIX."country" ;
         if($title!=''){
             $query.=" ORDER BY ".$title." ".$order;
         }
         else{
            $query.=" ORDER BY tc_name";
          }

	$res=mysql_query($query) or die(mysql_error());

	if (@mysql_num_rows($res) > 0) 
	{
		while($row = @mysql_fetch_array($res)) 
		{
			$array[]	= $row;
		}//end while loop
		@mysql_free_result($res);
	}//end if
	
	$params 	= array(
		'mode'      => 'Sliding',
		'perPage'   => PAGESIZE,
		'urlVar'    => 'page',
		'extraVars' => array(),
		'itemData'  => $array
	);
	$objP 		= & Pager::factory($params);
	$data  		= $objP->getPageData();
	if (empty($data))
		$message	.= "No record(s) exists.<br>";
}//end else

include_once('title.inc.php');
?>
<script language="javascript" type="text/javascript">
function confirm_check()
{
	var chkVar;
	for (var i = 0; i < document.form1.elements.length; i++) 
	{
		var e = document.form1.elements[i];
		if (e.name == "ids[]") 
		{
			if(e.checked==true)
			{
				chkVar=true;
			}//end if
		}//end if
	}//end for loop

	 if(chkVar==true)
	{
		 var where_to= confirm("Do you really want to delete these Country for permenantly??");
		 if (where_to== true)
		 {
		   return true;
		 }
		 else
		 {
		  return false;
		  }
	}//end if
}
function showAddcounty(){
    $('#div_country').hide();
    $('#div_add_country').show();
}

    function displayEditCountry(id,page){
        var data 	= 'id='  + id+'&page='+page;
        $.ajax({
                url: "add_country.php",
                type: "POST",
                data: data,
                cache: false,
                success: function (html) {
                    $('#tcontent10').html(html);
                }
        });
    }

</script>

<table width="100%"  border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td valign="top" class="white"><table width="100%"  border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td width="72%" valign="top"><?php include_once('tophtml.php');?>
			<table width="100%"  border="0" cellspacing="0" cellpadding="5">
              <tr>
                <!--td width="32%">Country</td-->
                <td width="68%" align="right"><a href="#" onclick="showAddcounty()" class="add-btn">Add New Country</a></td>
              </tr>
			  <tr><td></td></tr>
            </table>
		<form name="form1" method="post" action="" onSubmit="return confirm_check();">
          <table width="100%"  border="0" cellspacing="0" cellpadding="0">
            <tr>
              <td><table width="100%"  border="0" cellspacing="1" cellpadding="0" class="listing_tbl_admin">
					<?php 	if(isset($message) && $message!='')
							{
					?>
                <tr align="left">
                  <td align="center" class="maintext2">&nbsp;</td>
                  <td colspan="5" class="maintext2 warning"><?php echo $message;?></td>
                  </tr>
					<?php  	}//end if
				      if ($objDB) 
					  { 
                 			if (!empty($data)) 
							{ 
				?>
                <tr align="left">
				 <th class="maintext2"  align="center">&nbsp;</th>
                  <th class="maintext2" align="center">#</th>
                  <th class="maintext2"  align="center">Country<a href="settings.php?form=country&title=tc_name&order=<?php echo $order == 'ASC' ? 'DESC' : 'ASC'?>"><?php echo $image ?></a></th>
                  <th class="maintext2"  align="center">Short Name<a href="settings.php?form=country&title=tc_code&order=<?php echo $order == 'ASC' ? 'DESC' : 'ASC'?>"><?php echo $image ?></a></th>
                  <th class="maintext2"  align="center">Active<a href="settings.php?form=country&title=tc_status&order=<?php echo $order == 'ASC' ? 'DESC' : 'ASC'?>"><?php echo $image ?></a></th>
                  <th class="maintext2" align="center">Action</th>
                 
                  </tr>
					<?php  	foreach ($data as $key => $value) 
							{ 
				?>
                <tr align="left">
				<td width="5%" align="center" class="maintext2"><input name="ids[]" type="checkbox" value="<?php echo $value['tc_id']?>"></td>
                  <td width="5%" align="center" class="maintext2"><?php echo $key+1;?></td>
                  <td width="26%" class="maintext2"><?php echo htmlspecialchars(stripslashes($value['tc_name']))?></td>
                  <td width="24%" class="maintext2"><?php echo $value['tc_code']?></td>
                  <td width="27%" align="center" class="maintext2"><?php if($value['tc_status']=='A'){echo 'Yes';}else{echo 'No';}?></td>
                  <td width="12%" align="center" class="maintext2">
                      <!--<a href="add_country.php?tc_id=<?php echo $value['tc_id']?>&page=<?php echo $objP->getCurrentPageID()?>" class="signupbuttonlink">Edit</a>-->
                      <a onClick="displayEditCountry(<?php echo $value['tc_id'];?>,<?php echo $objP->getCurrentPageID();?>);" class="signupbuttonlink" style="cursor: pointer;">Edit</a>
                  </td>
                  
                  </tr>
				<?php 			
						}//end foreach
				?>
			   
			   <?php
					 		}//end if
						}//end first if
				?>
              </table>
			  <table width="100%" cellspacing="1" cellpadding="1" border="0" style="margin-top:10px;">
			  <tr align="left">
                 
                  <td colspan="0" height="40" align="left">
        <input type="submit" name="DelNow" value="Delete" class="loginbutton"></td>
		<td width="18%"><?php if(!empty($data))
					  {
				?>
                <table width="100%"  border="0" cellspacing="0" cellpadding="0">
                  <tr>
                    <td width="82%">&nbsp;</td>
                    <td width="18%"><table width="100%"  border="0" cellspacing="2" cellpadding="0" class="pagination_style">
                        <tr align="center" class="textbox">
                          <td><?php echo $objP->links;?></td>
                          </tr>
                    </table></td>
                  </tr>
                </table><?php }//end if?></td>
                </tr>
				
			  </table>
				
                <table width="100%"  border="0" cellspacing="0" cellpadding="0">
                  <tr>
                    <td align="right"><table width="100%"  border="0" cellspacing="0" cellpadding="0">
                      <tr>
                        <td>&nbsp;</td>
                      </tr>
                    </table>
                     </td>
                  </tr>
                </table></td>
            </tr>
          </table></form></td>
        </tr>
    </table>      
    </td>
  </tr>
</table>


