<?php 
include("includes/FCKeditor/fckeditor.php");
$file_name		= "index?page=view_country";
$form_action	= "insert";
$button_value 	= "Add";
$table_name 	= "country";
$page_name      = "Add Country";
$id	 = "";
$vat = "";
$query_string = "";
################################################## START INSERT ##################################################
if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'insert'){	
	utility::addslashesOnAllInput(array("query_string"));
	
    $country 	= $_REQUEST['country']; 
	
    $sql= "INSERT INTO $table_name (country) VALUES('$country')";
    mysql_query($sql);
	
	$tmp = ucfirst(str_replace("_"," ", $table_name));
	$_SESSION['info_message'] = "$tmp added sucessfully";
    header("Location: $file_name");
    exit();
}
################################################# END INSERT #################################################

################################################## START EDIT ##################################################
if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'edit'){	
	$id = $_REQUEST['id'];

	$form_action 	= "update";
	$button_value 	= "Update";
	$page_name      = "Edit Country";
	$query_string 	=  (isset($_POST['query_string'])) ? $_POST['query_string'] : $_SERVER['QUERY_STRING'];
	
	
    $sql= "SELECT * FROM $table_name WHERE id = $id";
    $result_edit = mysql_query($sql);
    $row_edit = mysql_fetch_array($result_edit);

	$country 	= $row_edit['country']; 
}
################################################# END EDIT ################################################# 


################################################## START UPDATE ################################################## 
if(isset($_REQUEST['action']) && $_REQUEST['action'] == 'update'){    
	utility::addslashesOnAllInput(array("query_string"));
	 
	$id					= $_REQUEST['id'];
	$country 			= $_REQUEST['country'];
	

	$sql= "UPDATE $table_name SET country = '$country' WHERE id = $id";
    mysql_query($sql);
	
	$query_string = (isset($_REQUEST['query_string']) && !empty($_REQUEST['query_string'])) ? $_REQUEST['query_string'] : "";
  	$query_string = str_ireplace("page=add_country", "page=view_country", $query_string);
	$query_string = "&".$query_string;
	$tmp = ucfirst(str_replace("_"," ", $table_name));
	$_SESSION['info_message'] = "$tmp updated sucessfully";
    header("Location: $file_name$query_string");
    exit();
}
################################################# END UPDATE ################################################# 
?>
<section class="content-header">
  <h1>
	<?=$page_name;?>
  </h1>
</section>
<section class="content">
	<div class="row">
		<div class="col-xs-12">
			<!-- general form elements -->
              <div class="box box-success">
                <form class="form-horizontal" method="post" name="form1" id="form1" action="index?page=add_country&action=<? echo $form_action; ?>" onsubmit="return validateForm(this)" enctype="multipart/form-data">
                  <div class="box-body">
                    <div class="form-group">
                      <label for="Country" class="col-sm-2 control-label">Country</label>
                      <div class="col-sm-10">
                        <input type="text" id="country" name="country" value="<?php echo $country;?>" class="form-control" placeholder="Country">
                      </div>
                    </div>
                  </div><!-- /.box-body -->
                  <div class="box-footer">
					<input type="hidden" id="id" name="id" value="<?php echo $id;?>">
					<input type="hidden" id="query_string" name="query_string" value="<?php echo $query_string;?>">
                    <button type="submit" id="submit" name="submit" value="<?php echo $button_value;?>" class="btn btn-success">Submit</button>
                  </div>
                </form>
              </div><!-- /.box -->
		</div>
	</div>
</section>