<?php 
session_start();
require_once 'includes/database.php';
include 'includes/funcs.php';
if (!isset($_SESSION['reusername'])) { echo 'Sorry You Dont Have permissions to View this Page!'; exit();}
else {$user = $_SESSION['reusername'];}
?>

<!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>
        <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>
        <link rel="stylesheet" href="css/allcss.css" type="text/css" />
        <link rel="stylesheet" href="css/jquery-ui-1.10.4.custom.css" type="text/css" />
<style>
html, body {margin:0; padding:0; height:100%; width:100%; min-width:100%; min-height:100%; overflow:scroll;z-index: 100;}  
#insform {
text-align: left;
width:90%;
margin: 5px; padding-left:5px; padding-right:5px;
border: 1px solid #666; 
background: #DDB388;
box-shadow: 0px 0px 5px rgba(0,0,0,0.5); 
font-family:"Trebuchet MS", Arial, Helvetica, sans-serif;
font-weight:bold;
font-size:14px;
color:#2C2C57; 
z-index: 100;
text-wrap: none;
}
</style>

<script type="text/javascript">       
$(document).ready(function() {  

$("#btnaddrepo").click(function(e) {
e.preventDefault();

if( $('#cname').val() === ''  ){ document.getElementById('valerr').value="Customer Name Cannot be Blank!";	} 
else if( $('#auct').val() === ''  ){ document.getElementById('valerr').value="Auctioneer Name Cannot be Blank!";	}
else if( $('#odate').val() === ''  ){ document.getElementById('valerr').value="Date Cannot be Blank!";	}
else if( ($('#cname').val() !== '') && ($('#odate').val() !== '') && ($('#regno').val() !== '') && ($('#auct').val() !== '')) {
		var savjcdet = $.post("savrepord.php?e=0&cname="+$('#cname').val()+"&odate="+$('#odate').val()+"&vehmod="+$('#vehmod').val()+
		"&regno="+$('#regno').val()+"&vcolor="+$('#vcolor').val()+"&chasis="+$('#chasis').val()+"&auct="+$('#auct').val()+"&rmks="+$('#rmks').val()+"&floc="+$('#floc').val(), function(data){
                                alert(data);  
								window.close();  });
	 	}						
});

$("#btnrepoinv").click(function(e) {
e.preventDefault();
if( $('#idate').val() === ''  ){ document.getElementById('valerr').value="Invoice date Cannot be Blank!";	} 
else if( $('#iam').val() === ''  ){ document.getElementById('valerr').value="Invoice Amount Cannot be Blank!";	}
else {
	var savjcdet = $.post("savrepoinv.php?cname="+$('#cname').val()+"&idate="+$('#idate').val()+"&am="+$('#iam').val()+"&repono="+$('#repono').val()+"&nar="+$('#regno').val()+'-'+$('#vehmod').val()+"&i="+$('#invno').val(), function(data){
                                if(data.trim()==="OK") { window.location.replace("invprint.php?r="+$('#repono').val()+"&sec=r","INVOICE","width=900px,height=600px,scrollbar=1"); } 
		});
	} 
});

$("#btnrepodelv").click(function(e) {
 window.location.replace("delvprint.php?r="+$('#repono').val(),"width=900px,height=600px,scrollbar=1");
	});
});
</script>
<?php 
if($_REQUEST['e'] === "0"){
	$cname = '';
	$odate = '';
	$vehmod = '';
	$regno = '';
	$vcolor = '';
	$auct = '';
	$rmks = '';
	$chasis = '';
	$floc = '';
	$repono='';
	$invno='';
}
else {
	$repono = $_REQUEST['repono'];
	require_once 'includes/database.php';
	$db=new MySQLDatabase();
	$db->open_connection();
	$query = "SELECT * FROM reporders WHERE repono = '$repono';";
	$result = mysqli_query($db->connection, $query) or die(mysqli_error($db->connection) );
	if ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
		$cname = $row['cname'];
		$odate = substr($row['odate'],8,2)."-".substr($row['odate'],5,2)."-".substr($row['odate'] ,0,4);
		$vehmod = $row['vehmod'];
		$regno = $row['regno'];
		$chasis = $row['chasis'];
		$vcolor =$row['vcolor'];
		$auct = $row['auct'];
		$rmks = $row['rmks'];
		$floc = $row['floc'];
		$ddate = substr($row['ddate'],8,2)."-".substr($row['ddate'],5,2)."-".substr($row['ddate'] ,0,4);
		$qry = "SELECT * FROM repoinvoices WHERE repono = '$repono';";
		$res = mysqli_query($db->connection, $qry) or die(mysqli_error($db->connection) );
		if ($rowa = mysqli_fetch_array($res, MYSQLI_ASSOC)){
			$idate = substr($rowa['idate'],8,2)."-".substr($rowa['idate'],5,2)."-".substr($rowa['idate'] ,0,4);
			$am = $rowa['amount'];
			$invno = $rowa['invno'];
		}	
		else { $idate = '';
			   $am = '';
		}
	}
	else { $db->close_connection();
			echo '<script type="text/javascript"> window.close();</script>';}
	$db->close_connection();		
}
?>   
</head><body>
<p class="titletr"> <?php echo ($_REQUEST['e'] === "0") ? 'Reposession Order' : 'Edit Reposession Order' ; echo str_repeat('&nbsp;', 8); ?> <input type="text" id="valerr" name="valerr" value="" size="30"> <?php echo str_repeat('&nbsp;', 10); ?> <a href="#"><img src="images/close.png" onclick="window.close();"> </a>  </p>       
<form id="insform" method="post" onkeypress="return event.keyCode!=13" enctype="multipart/form-data" action="#" >
                    <center>
                    <table cellspacing="5px" id="regaddbox">
					<tr><td align="left"><label for="odate"> Order Date &nbsp;</label></td>
                    <td><input type="text" id="odate" name="odate" value="<?php echo $odate; ?>" size="20"/> </td>
                    </tr>
                    <tr><td align="left"><label for="cname">Dealer Name &nbsp; </label></td>
                    <td><select id="cname" name="cname" selected><?php echo '<option value="'.$cname.'">'.$cname.'</option>'; echo selcus();?> </select> <input type="hidden" id="repono" name="repono" value= "<?php echo $repono; ?>"> </td>
                    </tr>
					<tr><td align="left"><label for="auct"> Auctioneer &nbsp;</label></td>
                    <td><input type="text" id="auct" name="auct" value="<?php echo $auct; ?>" size="40"/> </td>
                    </tr>
					<tr><td align="left"><label for="floc"> Location &nbsp;</label></td>
                    <td><input type="text" id="floc" name="floc" value="<?php echo $floc; ?>" size="20"/> </td>
                    </tr>
                    <tr><td align="left"><label for="vehmod"> Vehicle Model &nbsp;</label></td>
                    <td><input type="text" id="vehmod" name="vehmod" value="<?php echo $vehmod; ?>" size="40"/> </td>
                    </tr>
                    <tr><td align="left"><label for="regno"> Reg. No. &nbsp;</label></td>
                    <td><input type="text" id="regno" name="regno" value="<?php echo $regno; ?>" size="10"/> </td>
                    </tr>
					<tr><td align="left"><label for="vcolor"> Vehicle Colour &nbsp;</label></td>
                    <td><input type="text" id="vcolor" name="vcolor" value="<?php echo $vcolor; ?>" size="20"/> </td>
                    </tr>				
					<tr><td align="left"><label for="rmks1"> Chasis No. &nbsp;</label></td>
                    <td><input type="text" id="chasis" name="chasis" value="<?php echo $chasis; ?>" size="20"> </td>
                    </tr>
					
					<?php  if($_REQUEST['e'] === "1") { echo '<tr><td align="left"><label for="ddate"> Delivery Date &nbsp;</label></td>
                    <td><input type="text" id="ddate" name="ddate" value="'.$ddate.'" size="20"/> </td> </tr>'; } ?>
					
					<tr><td align="left"><label for="rmks2"> Remarks &nbsp;</label></td>
                    <td><input type="text" id="rmks" name="rmks" value="<?php echo $rmks; ?>" size="60"> </td>
                    </tr>
					<?php  if($_REQUEST['e'] === "1") { echo '<tr><td align="left"><label for="idate"> Invoice Date &nbsp;</label></td>
                    <td><input type="text" id="idate" name="idate" value="'.$idate.'" size="8"/>&nbsp;&nbsp;Inv. No.&nbsp; <input type="text" id="invno" name="invno" disabled="disabled" value="'.$invno.'" size="8"/>&nbsp;&nbsp; Inv. Amount <input type="text" id="iam" name="iam" value="'.$am.'" size="8"/></td> </tr>'; } ?>
                    <tr><td colspan=2> </td></tr><br />
                    <tr><td colspan=2 align="center">
					 <?php echo '&nbsp;&nbsp;<input type="button" class="text ui-widget-content ui-corner-all" style="width:160px;font-weight:bold;"'; echo ($_REQUEST['e'] === "0") ? ' id="btnaddrepo" name="btnaddrepo" value=" Save Order "' : ' id="btnmodrepo" name="btnmodrepo" onclick="btnmodrepof();" value=" Save Details "'; echo '/>'; 
					 echo ($_REQUEST['e'] === "1") ? '<input type="button" class="text ui-widget-content ui-corner-all" style="width:120px;font-weight:bold;" id="btnrepoinv" name="btnrepoinv" value=" Make Invoice "/> <input type="button" class="text ui-widget-content ui-corner-all" style="width:120px;font-weight:bold;" id="btnrepodelv" name="btnrepodelv" value=" Delivery Note "/>':'';  ?>
                    </td></tr>
                    </table></center>
        </form>
</body>
</html>

<script type="text/javascript">   
$('#cname').autocomplete({ source: <?php echo allcustomers(); ?>, minLength: 2  });                                                             
$('#odate').datepicker({ changeMonth: true, changeYear: true, dateFormat: "dd-mm-yy"}); 
$('#idate').datepicker({ changeMonth: true, changeYear: true, dateFormat: "dd-mm-yy"});
$('#ddate').datepicker({ changeMonth: true, changeYear: true, dateFormat: "dd-mm-yy"});
$('#auct').autocomplete({ source: <?php echo allauctioneers(); ?>, minLength: 2  }); 

function btnmodrepof(){
if( $('#cname').val() === ''  ){ document.getElementById('valerr').value="Customer Name Cannot be Blank!";	} 
else if( $('#auct').val() === ''  ){ document.getElementById('valerr').value="auct Name Cannot be Blank!";	}
else if( $('#odate').val() === ''  ){ document.getElementById('valerr').value="Date Cannot be Blank!";	}
else if( ($('#cname').val() !== '') && ($('#regno').val() !== '') && ($('#auct').val() !== '')) {
var savjcdet = $.get("savrepord.php?e=1&repono="+$('#repono').val()+"&cname="+$('#cname').val()+"&odate="+$('#odate').val()+"&regno="+$('#regno').val()+"&vehmod="+$('#vehmod').val()+"&vcolor="+$('#vcolor').val()+
		"&auct="+$('#auct').val()+"&chasis="+$('#chasis').val()+"&rmks="+$('#rmks').val()+"&floc="+$('#floc').val()+"&ddate="+$('#ddate').val(), function(data){
                                if(data.trim()==="OK") { window.close(); } 
							   });
}						
};
</script>



