<?php 
include("db.php");
include("ms_meta.php");
//print_r($_GET);
if($_POST){
	$total=$_GET["amount"]-$_GET["given"];
	if($total<$_POST["fine"] && $_GET["type"]=="other"){
		echo "Please enter valid amount.Amount must shorter then or equal to $total";
	}else{
		$ins=installment_fine($_GET["idno"],$_GET["no"]."-".$_GET["type"],$_POST["fine"],$_POST["msg"],$_REQUEST["udate"]);
		if($db->getErMsg()!=""){
			echo $db->getErMsg();
		}else{
			$reff_receipt=array("amount"=>$_REQUEST["fine"],
							"type"=>$_REQUEST["msg"],
							"date"=>$_REQUEST["udate"],
							"idno"=>$_REQUEST["idno"],
							"session"=>$_SESSION["admin"]["session"]
							);
			$reff_receipt["inst_id"]=$ins;
			$reff_receipt["school_id"]=$_SESSION["admin"]["school_id"];
			$reff_receipt["added_by"]=$_SESSION["admin"]["uid"];
			
			$db->insertAry("reff_receipt",$reff_receipt);//echo $db->getErMsg();exit;
			if($db->getErMsg()!=""){
				echo $db->getErMsg();
			}else{
				?>
                <br /><br /><br /><h4>Fine insert successfully. Please wait this page auto redirect to pay fee page thank you... :)</h4>
				<script>window.parent.location = "./index.php?src=Student&page_id=pay_fee&action=add&id=<?php echo $_GET["idno"] ?>";</script>
				<?php
				exit;
			}
		}
	}
}{
?>
<script>
function checkForm(){
	var fine=document.getElementById('fine').value;
	if(fine=="" || fine==0){
		alert("fine cannot empty or must greater then 0")
		return false;
	}
	else{
		return true;
	}
}
</script>

        <form method="post" action="" onsubmit="return checkForm();">
			<!-- Forms: Box -->
			<div class="span12">

				<!-- Forms: Top Bar -->
				
				<!-- / Forms: Top Bar -->

				<!-- Forms: Content -->
				<div class="well no-padding">

						<div class="control-group">
						
						  <label class="control-label" for="dateofadmission">Total Amount</label>
                            <div class="controls">
                                   <input required class="input-block-leve" type="text" id="fine" name="fine" class="obtain" value="" />                           </div>
                        </div>
                        <div class="control-group">
						
						  <label class="control-label" for="udate">Date</label>
                            <div class="controls">
                                   <input required type="text" id="udate" name="udate" class="obtain datepicker" value="<?php echo date("Y-m-d H:i:g"); ?>" placeholder="Date">
                            </div>
                        </div>
                        <div class="control-group">
						Fee Detail
							<div class="controls">
								<textarea id="msg" name="msg" class="input-block-level" rows="1" <?php  if(isset($_GET['name'])) echo "readonly"; ?>><?php echo $_GET['name'] ?></textarea>
							</div>
						</div>
						<div class="form-actions">
							<button type="submit" class="btn btn-primary"> Save</button>
							<button type="reset" class="btn"> Cancel</button>
						</div>
                        
				</div>
				<!-- / Forms: Content -->

			</div>
			<!-- / Forms: Box -->

        </form>
	
<?php } ?>
<script>
$(function() {
	var d = new Date(); // for now
	datetext=d.getHours()+": "+d.getMinutes()+": "+d.getSeconds();
	$( '.datepicker' ).datepicker({
		changeMonth: true,
		changeYear: true,
		showOn: "button",
		buttonImage: "js/images/calendar.gif",
		buttonImageOnly: true,
		buttonText: "Select date",
		dateFormat: 'yy-mm-dd ',
		onSelect: function(datetext){
			var d = new Date(); // for now
			datetext=datetext+" "+d.getHours()+":"+d.getMinutes()+":"+d.getSeconds();
			$('.datepicker').val(datetext);
		}
	});
});
</script>