<?php
session_start();
if (!isset($_SESSION['reusername'])) { echo 'Sorry You Dont Have permissions to View this Page!'; exit();}
require_once 'includes/database.php';
require_once 'includes/funcs.php';
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>QIA | Endorsement </title>
<script type="text/javascript" src="js/jquery-1.10.2.js"></script>
<script type="text/javascript" src="js/jquery-ui-1.10.4.custom.js"></script>
<script type="text/javascript" src="ckeditor/ckeditor.js"></script>
<link rel="stylesheet" href="css/jquery-ui-1.10.4.custom.css" type="text/css" />
<link rel="stylesheet" type="text/css" href="css/allcss.css">
<style>
html, body {margin:0; padding:0; height:100%; width:100%; min-width:100%; min-height:100%; overflow:scroll;z-index: 100;}  
#insf {
text-align: left;
width:600px;
background: #AAFFD4;
margin: 5px; padding-left:5px; padding-right:5px;
font-family:"Tahoma", Arial, Helvetica, sans-serif;
font-size:14px;
text-wrap: none;
}
</style>

</head>
<body>



<p align="center" style="font-size:24px;font-weight:bold;padding:10px; background: #DEDEB4; box-shadow: 0px 0px 5px rgba(0,0,0,0.5);"> ENDORSEMENT DETAILS &nbsp;&nbsp; Booking No. <?php if(isset($_POST['btnaddins'])) { $insno = mysql_real_escape_string($_REQUEST['insno']); } else { $insno = $_GET['insno']; } echo $insno; ?> </p>
<form id="insform" method="post" onkeypress="return event.keyCode!=13" enctype="multipart/form-data" action="insendo.php" >
<?php 
if(isset($_POST['btnaddins'])) {
	$insno = $_REQUEST['insno'];
	$endomemo = $_REQUEST['endomemo'];	$endono = $_REQUEST['endono'];		$endoline = $_REQUEST['endoline'];  	$lineterms = $_REQUEST['lineterms'];
	$idate = substr($_REQUEST['idate'],6,4)."-".substr($_REQUEST['idate'],3,2)."-".substr($_REQUEST['idate'] ,0,2);
	$db=new MySQLDatabase();
	$db->open_connection(); 
	$endosav = 0;
	$user = $_SESSION['reusername'];	$ts = date("Y-m-d H:i:s");
	$sql = mysqli_query($db->connection, "SELECT * FROM endorses WHERE insno = '$insno';") or die (mysqli_error($db->connection) );
	if ($row = mysqli_fetch_array($sql, MYSQLI_ASSOC)){ $endosav = 1;  }
	if($endosav){ $qry = "UPDATE endorses SET endomemo='$endomemo', lineterms='$lineterms', endoline = '$endoline', endono='$endono', idate='$idate', us='$user', ts = '$ts' WHERE insno='$insno';";  }
	else { $qry = "INSERT INTO endorses (insno, idate, endomemo, endoline, lineterms, endono, us, ts) VALUES ('$insno','$idate','$endomemo', '$endoline','$lineterms', '$endono', '$user', '$ts');"; }
	$sql = mysqli_query($db->connection, $qry) or die (mysqli_error($db->connection) );
	echo '<script type="text/javascript"> window.close();</script>';
}
elseif(isset($_GET['insno'])) {
	$user = $_SESSION['reusername'];
	$db=new MySQLDatabase();
	$db->open_connection();
	$agp = '';
	$sql = mysqli_query($db->connection, "SELECT * FROM insrecs WHERE insno = '$insno';") or die (mysqli_error($db->connection) );
	if ($row = mysqli_fetch_array($sql, MYSQLI_ASSOC)){ 
		if($row['agpno'] !==''){ $agp = $row['agpno']; }
			else if($row['agpno3'] !==''){ $agp = $row['agpno3']; }
			else if($row['agpno2'] !==''){ $agp = $row['agpno2']; }
			else if($row['agpno1'] !==''){ $agp = $row['agpno1']; }
	}
	$sql = mysqli_query($db->connection, "SELECT * FROM endorses WHERE insno = '$insno';") or die (mysqli_error($db->connection) );
	if ($row = mysqli_fetch_array($sql, MYSQLI_ASSOC)){ if($row['endono'] !== '') { $endono = $row['endono']; 
									$endomemo = $row['endomemo'];
									$endoline = $row['endoline']; 
									$lineterms = $row['lineterms'];
									$idate = substr($row['idate'],8,2)."-".substr($row['idate'],5,2)."-".substr($row['idate'] ,0,4); 
									 }					 
					    }
	else {	$endomemo = '';	 $endono = $insno.'-1'; 	$idate=''; 	
			$endoline = 'With effect from '.$idate.' the following Memorandum forms the part of the Policy No. '.$agp;	
		  }										 

	echo '<table id="insf">';
	echo '<tr style="font-weight:bold;"><td>Endorsement No.</td> <td><input type="text" id="endono" name="endono" value="'.$endono.'"/></td></tr>';
	echo '<tr style="font-weight:bold;"><td>Endorsement Date</td> <td><input type="text" id="idate" name="idate" value="'.$idate.'"/></td></tr>';
	echo '<tr><td colspan="2"><b>Line 1 </b><textarea id="endoline" col="80" row="2" name="endoline">'.$endoline.'</textarea></td></tr>';
	echo '<tr style="font-weight:bold;"><td colspan="2">Memorandum </td></tr> 
	<tr><td colspan="2"><textarea id="endomemo" col="80" row="10" name="endomemo">'.$endomemo.'</textarea> <input type="hidden" id="insno" name="insno" value="'.$insno.'"/></td> </tr>';
	echo '<tr><td colspan="2"><b>Terms Line  </b><textarea id="lineterms" col="80" row="2" name="lineterms">'.$lineterms.'</textarea></td></tr>';
	echo '</table>';

	echo '<p align="center"><input type="submit" class="text ui-widget-content ui-corner-all" style="width:180px;font-weight:bold;"  id="btnaddins" name="btnaddins" value="Save Endorsement"/></p>';
}
?> 

</form>

</body></html>

<script type="text/javascript">       
$('#idate').datepicker({ changeMonth: true, changeYear: true, dateFormat: "dd-mm-yy"}); 
CKEDITOR.replace('endomemo');
CKEDITOR.replace('endoline');
CKEDITOR.replace('lineterms');
</script>
