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

$page		= isset($_REQUEST['page'])	?	trim($_REQUEST['page'])	:	"1";
$id			= isset($_REQUEST['id'])	?	trim($_REQUEST['id'])	:	"0";
$message	= "";

if (!$objDB) 
{
	$message	= "Unable to Locate Database Server.";
}//end if
else 
{
	//$ts_id = $_GET['ts_id'];
        $ts_id = $_POST['id'];
	
	if (intval($ts_id) != 0)
	{
		$sqlEdit = " SELECT * FROM ".TABLEPREFIX."state WHERE ts_id='$ts_id'";
		$rsEdit = mysql_query($sqlEdit);
		if($arrEdit= mysql_fetch_array($rsEdit))
		{
 			@extract($arrEdit);
			$txtTitle=$arrEdit['ts_name'];
			$txtStitle=$arrEdit['ts_code'];
			$chkActive=$arrEdit['ts_status'];
			$ddlCountry=$arrEdit['tc_id'];
		}//end if
	}//end first if

	//if category is blank
	if($ddlCountry=='')
	{
		$ddlCountry=$_GET['ddlCountry'];
	}//end if

	//add or update category
	if(isset($_POST['Call']) && $_POST['Call']=='save')
	{
			@extract($_POST);
			
                        $ts_id = $_POST['ts_id'] ;
                        $ddlCountry = $_POST["ddlCountry"];
                        $txtTitle = $_POST["txtTitle"];
                        $txtStitle = $_POST["txtStitle"];
                        $chkActive =  $_POST["chkActive"];
			
	
			if($ts_id!='')
			{	
				$sqlUpdate = " UPDATE ".TABLEPREFIX."state SET ts_name='".addslashes($txtTitle)."',ts_status='".addslashes($chkActive)."',
								 tc_id='".addslashes($ddlCountry)."',ts_code='".addslashes($txtStitle)."'
								 WHERE ts_id='$ts_id' ";
								 
				mysql_query($sqlUpdate) or die(mysql_error());
				$message = "<font color='green'>State updated successfully</font>.";
			}
			else
			{
				$sqlIns = " INSERT INTO ".TABLEPREFIX."state (tc_id,ts_name,ts_code,ts_status) 
							VALUES ('".addslashes($ddlCountry)."','".addslashes($txtTitle)."',
							'".addslashes($txtStitle)."','".addslashes($chkActive)."') ";
							
				mysql_query($sqlIns) or die(mysql_error());
				$message = "<font color='green'>State added successfully</font>.";
			}
                        header('location:settings.php');

		//header('location:state.php?ddlCountry='.$ddlCountry);
		//exit();
	}//end if
	//$message
}//end else

//include_once('title.inc.php');
?>
<script language="javascript" src="../js/validation.js" type="text/javascript"></script>
<script language="javascript">
function category_chk() {

    var frm	= document.formState;  

    if (frm.txtTitle.value == "") {
        alert("Please enter state name.");
        frm.txtTitle.focus();
        return false;
    }
    
    return true;
			
}
function showListState(id){
    if(id==''){
        $('#div_state').show();
        $('#div_add_state').hide();
    }
    else{
      window.location.href='settings.php' ;
    }
}
</script>
<table width="100%"  border="0" cellspacing="0" cellpadding="0" class="tblestyle_admin2">
  <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="41%" > <strong class="sb-hd"><?php if($ts_id!=''){?>
              Edit
                <?php }else{?>
                Add New
                <?php }?>
              State </strong></td>
              
              </tr>
            </table></td>
          </tr>
        </table>

		<form name="formState" method="POST" action="<?php echo $PHP_SELF?>" onSubmit="return category_chk()" enctype="multipart/form-data">
  <input type=hidden name='Call' value="save">
  <input name='ts_id' type=hidden id="ts_id" value="<?php echo $ts_id?>">
  <input name='ddlCountry' type=hidden id="ddlCountry" value="<?php echo $ddlCountry;?>">
          <table width="100%"  border="0" cellspacing="0" cellpadding="5">
            <tr>
              <td><table width="100%"  border="0" cellspacing="0" cellpadding="0">
				<?php if ($objDB) 
					  { 
				?>
					<?php 	if(isset($message) && $message!='')
							{
					?>
                <tr align="left">
                  <td align="center" class="maintext2">&nbsp;</td>
                  <td colspan="3" class="maintext2 warning"><?php echo $message;?></td>
                  </tr>
					<?php  	}//end if?>
  				<tr align="left">
                 
                  <td width="27%" valign="top" class="maintext2">Category <span class="warning">*</span></td>
                  <td width="70%" colspan="2" valign="top" class="maintext2"><select name="ddlCountry" id="ddlCountry" class="textbox txt-bx">
		<?php
			//fectch category from State category table
			$category=mysql_query("select * from ".TABLEPREFIX."country where tc_name!='' order by tc_name") or die(mysql_error());
			if(mysql_num_rows($category)>0){
				while($arr=mysql_fetch_array($category))
				{?>
				<option value="<?php echo $arr['tc_id'];?>" <?php if($ddlCountry==$arr['tc_id']){echo 'selected';}?>><?php echo $arr['tc_name'];?></option>
				<?php
					}//end while
				}//end if
				else{
					echo '<option value="Nil">NIL</option>';
				}//end if?>
				</select></td>
  				</tr>
                 <tr align="left">
                  
                  <td valign="top" class="maintext2">Name <span class="warning">*</span></td>
                  <td colspan="2" valign="top" class="maintext2"><input name="txtTitle" class="textbox3 txt-bx" id="txtTitle" value="<?php echo $txtTitle?>" size="35" maxlength="100"></td>
                 </tr>
                 <tr align="left">
                  
                   <td valign="top" class="maintext2">Short Name </td>
                   <td colspan="2" valign="top" class="maintext2"><input name="txtStitle" class="textbox3 txt-bx" id="txtStitle" value="<?php echo $txtStitle?>" size="35" maxlength="100"></td>
                 </tr>
                <tr align="left">
                 
                  <td valign="top" class="maintext2">Active</td>
                  <td colspan="2" valign="top" class="maintext2"><input name="chkActive" type="radio" value="A" <?php if($chkActive=='A'){echo 'checked';}if($chkActive==''){echo 'checked';}?>>
						      Yes <input type="radio" name="chkActive" value="N" <?php if($chkActive=='N'){echo 'checked';}?>>No</td>
                </tr>
				<?php 			
						}//end first if
				?>
              </table>
                <table width="100%"  border="0" cellspacing="0" cellpadding="0">
                  <tr>
                    <td align="right"><table width="100%"  border="0" cellspacing="0" cellpadding="0">
                      
                    </table>
                      <?php if ($objDB) 
					  { 
					?>
                      <table width="100%"  border="0" cellspacing="0" cellpadding="5">
                      <tr><td width="27%"></td>
                        <td align="left"><table width="11%"  border="0" cellspacing="0" cellpadding="0">
                            <tr>
							<td><input name="BackNow" style="cursor: pointer;" type="button"  onclick="showListState(<?php echo $ts_id?>)"class="back-btn" value="Back"></td>
                              <td><input class="loginbutton" type="submit" value="<?php if($ts_id!=''){?>Edit<?php }else{?>Add<?php }?> State" name="submit2"></td>
                              <?php //if ($ts_id > 0) { ?>
                              
								<?php //}//end if?>
                              </tr>
                        </table></td>
                      </tr>
                    </table>
					<?php 		
						}//end first if
				?></td>
                  </tr>
                </table></td>
            </tr>
          </table></form></td>
        </tr>
    </table>      
    </td>
  </tr>
</table>
