<?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";
//selected language for filtering
$ddlCountry=($_POST['ddlCountry']!='')?$_POST['ddlCountry']:$_GET['ddlCountry'];
//for paging
$_SESSION['ddlCountry']=$ddlCountry;
$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 State category
	if(isset($_POST['ids']) && $_POST['ids']!='')
	{
		foreach ($_POST['ids'] as $val)
		{
			$sqlDel = mysql_query("DELETE FROM ".TABLEPREFIX."state WHERE ts_id='".$val."'") or die(mysql_error());
			$message = "<font color='green'>State deleted successfully.</font>";
		}//end foreach
	}//end if

	//add this link to ordering 
	if(isset($ddlCountry) && $ddlCountry!='')
	{
		$order_category='&ddlCountry='.$ddlCountry;
	}//end if
					
	if(isset($ddlCountry))
	{
		if($ddlCountry!='All')
		{
			//requested language category listing
			$query = "SELECT s.*,c.tc_name FROM ".TABLEPREFIX."state s left join ".TABLEPREFIX."country c on s.tc_id=c.tc_id 
						where s.tc_id='".$ddlCountry."' ORDER BY s.ts_name";
		}//end if
		else
		{
			//all category listing
			$query = "SELECT s.*,c.tc_name FROM ".TABLEPREFIX."state s left join ".TABLEPREFIX."country c on s.tc_id=c.tc_id 
						ORDER BY s.ts_name";
		}//end else
	}//end if
	else
	{
		//all category listing
		$query = "SELECT s.*,c.tc_name FROM ".TABLEPREFIX."state s left join ".TABLEPREFIX."country c on s.tc_id=c.tc_id ";
                 
                if($title!=''){
                    $query.=" ORDER BY ".$title." ".$order;
                }
                else{
                   $query.=" ORDER BY s.ts_name";
                }
					
	}//end else

	$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 State  for permenantly??");
		 if (where_to== true)
		 {
		   return true;
		 }
		 else
		 {
		  return false;
		  }
	}//end if
}
function showAddState(){
    $('#div_state').hide();
    $('#div_add_state').show();
}
function displayEditState(id,page){
        var data 	= 'id='  + id+'&page='+page;
        $.ajax({
                url: "add_state.php",
                type: "POST",
                data: data,
                cache: false,
                success: function (html) {
                    $('#tcontent11').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><table width="100%"  border="0" cellspacing="0" cellpadding="5">
              <tr>
                <td width="28%"><table width="25%" border="0" cellpadding="0" cellspacing="0" class="maintext" align="right">
                        <form action="<?php echo $_SERVER['PHP_SELF']?>" method="post" name="frmSearch">
						<tr>
                          <th align="right" scope="col">Filter by Country</th>
                          <th scope="col" align="right"><select name="ddlCountry" class="textbox" onChange="document.frmSearch.submit();">
								<option value="All">All</option>
								<?php
			//fectch language from music category table
			$language=mysql_query("select * from ".TABLEPREFIX."country order by tc_name") or die(mysql_error());
			if(mysql_num_rows($language)>0){
				while($arr=mysql_fetch_array($language))
				{?>
				<option value="<?php echo $arr['tc_id'];?>" <?php if($_POST['ddlCountry']==$arr['tc_id']){echo 'selected';}else if($_GET['ddlCountry']==$arr['tc_id']){echo 'selected';}?>><?php echo ucfirst($arr['tc_name']);?></option>
				<?php
					}//end while
				}//end if
				else{
					echo '<option value="Nil">NIL</option>';
				}//end if?>
				</select></th>
						</tr>
						</form>
                      </table></td>
                <td width="3%" align="right" height="40"><a href="#" onclick="showAddState()" class="add-btn">Add New State</a></td>
              </tr>
              
            </table></td>
          </tr>
        </table>
		<form name="form1" method="post" action="" onSubmit="return confirm_check();">
          <table width="100%"  border="0" cellspacing="0" cellpadding="5">
            <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="6" class="maintext2 warning"><?php echo $message;?></td>
                  </tr>
				<?php }//end if
					  if ($objDB) 
					  { 
                 			if (!empty($data)) 
							{ 
				?>
                <tr align="left">
				 <th class="maintext2">&nbsp;</th>
                  <th align="center" class="maintext2">#</th>
                  <th class="maintext2">State<a href="settings.php?form=state&title=ts_name&order=<?php echo $order == 'ASC' ? 'DESC' : 'ASC'?>"><?php echo $image ?></a></th>
                  <th class="maintext2">Short Name<a href="settings.php?form=state&title=ts_code&order=<?php echo $order == 'ASC' ? 'DESC' : 'ASC'?>"><?php echo $image ?></a></th>
                  <th class="maintext2">Country<a href="settings.php?form=state&title=tc_name&order=<?php echo $order == 'ASC' ? 'DESC' : 'ASC'?>"><?php echo $image ?></a></th>
                  <th class="maintext2" align="center">Active<a href="settings.php?form=state&title=ts_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['ts_id']?>"></td>
                  <td width="5%" align="center" class="maintext2"><?php echo $key+1;?></td>
                  <td width="26%" class="maintext2"><?php echo htmlspecialchars(stripslashes($value['ts_name']))?></td>
                  <td width="15%" class="maintext2"><?php echo $value['ts_code']?></td>
                  <td width="27%" align="left" class="maintext2"><?php echo $value['tc_name']?></td>
                  <td width="15%" align="center" class="maintext2"><?php if($value['ts_status']=='A'){echo 'Yes';}else{echo 'No';}?></td>
                  <td width="12%" align="center" class="maintext2">
                      <!--<a href="add_state.php?ts_id=<?php echo $value['ts_id']?>&page=<?php echo $objP->getCurrentPageID()?><?php echo $order_category;?>" class="signupbuttonlink">Edit</a>-->
                      <a  onClick="displayEditState(<?php echo $value['ts_id'];?>,<?php echo $objP->getCurrentPageID();?>);" class="signupbuttonlink">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>
