<?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;}
            .titletr {
            text-align: left;
            width:100%;
            padding-left:10px; padding-right:5px; 
            background: #DEDEB4;
            box-shadow: 0px 0px 5px rgba(0,0,0,0.5); 
            font-family:Calibri, Arial, Helvetica, sans-serif;
            font-weight:bold;
            font-size:14px;
            color:#7A2525;
            }
        </style>
<script type="text/javascript">       
$(document).ready(function() { 
$('#cname').autocomplete({ source: <?php echo allcustomers(); ?>, minLength: 2  }); 

$("#btnaddcus").click(function(e) {
e.preventDefault();
if( $('#cname').val() === ''  ){ document.getElementById('valerr').value="Customer Name Cannot be Blank!";	}
else if( $('#phone').val() === ''  ){ document.getElementById('valerr').value="Phone Cannot be Blank!";	}
else if( $('#cname').val() !== '' && $('#phone').val() !== '' ) {
		var savregdet = $.post("savecus.php?e=0&cname="+$('#cname').val()+"&cadd="+$('#cadd').val()+"&phone="+$('#phone').val()+"&city="+$('#city').val()+"&email="+$('#email').val(), function(data){
                                if(data.trim()==="OK") { window.close(); } });
	 	}						
});

$("#btnmodcus").click(function(e) {
e.preventDefault();
if( $('#cname').val() === ''  ){ document.getElementById('valerr').value="Customer Name Cannot be Blank!";	}
else if( $('#phone').val() === ''  ){ document.getElementById('valerr').value="Phone Cannot be Blank!";	}
else if( $('#cname').val() !== '' && $('#phone').val() !== '' ) {
		var savregdet = $.post("savecus.php?e=1&cname="+$('#cname').val()+"&ocname="+$('#ocname').val()+"&cadd="+$('#cadd').val()+"&phone="+$('#phone').val()+"&city="+$('#city').val()+"&email="+$('#email').val(), function(data){
                                if(data.trim()==="OK") { window.close(); } });
	 	}						
});

});
</script>
<?php 
if($_REQUEST['e'] === "0"){
	$cname = '';
	$cadd = '';
	$phone = '';
	$city = '';
	$email = '';
}
else {
	$cname = $_REQUEST['cname'];
	require_once 'includes/database.php';

	$db=new MySQLDatabase();
	$db->open_connection();
	$query = "SELECT cname, address, phone, city, email FROM customers WHERE cname = '$cname';";
	$result = mysqli_query($db->connection, $query) or die(mysqli_error($db->connection) );
	if ($row = mysqli_fetch_array($result, MYSQLI_ASSOC )){
		$cadd = $row['address'];
		$phone = $row['phone'];
		$city = $row['city'];
		$email = $row['email'];
	}
	else { echo '<script type="text/javascript"> window.close();</script>';}
}
?>   
</head><body>
<p class="titletr"> <?php echo ($_REQUEST['e'] === "0") ? 'New Customer' : 'Edit Customer' ; 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="regform" method="post" onkeypress="return event.keyCode!=13" enctype="multipart/form-data" action="#" >
                    <center>
                    <table cellspacing="5px" id="regaddbox">
                    <tr><td align="left"><label for="cname">Customer Name &nbsp; </label></td>
                    <td><input type="text" id="cname" name="cname" value= "<?php echo $cname; ?>" size="40"> <input type="hidden" id="ocname" name="ocname" value= "<?php echo $cname; ?>"> </td>
                    </tr>
                    <tr><td align="left"><label for="cadd"> Address &nbsp;</label></td>
                    <td><input type="text" id="cadd" name="cadd" value="<?php echo $cadd; ?>" size="40"> </td>
                    </tr>
                    <tr><td align="left"><label for="phone"> Phone &nbsp;</label></td>
                    <td><input type="text" id="phone" name="phone" value="<?php echo $phone; ?>" size="20"> </td>
                    </tr>
                    <tr><td align="left"><label for="city"> City &nbsp;</label></td>
                    <td><input type="text" id="city" name="city" value="<?php echo $city; ?>" size="20"> </td>
                    </tr>
					<tr><td align="left"><label for="email"> Email &nbsp;</label></td>
                    <td><input type="text" id="email" name="email" value="<?php echo $email; ?>" size="20"> </td>
                    </tr>
                    <tr><td colspan=2> </td></tr><br />
                    <tr><td colspan=2 align="center">
					 <?php echo '<input type="button" class="text ui-widget-content ui-corner-all" style="width:160px;font-weight:bold;"'; echo ($_REQUEST['e'] === "0") ? ' id="btnaddcus" name="btnaddcus" value=" Add Customer "' : ' id="btnmodcus" name="btnmodcus" value=" Save Details "'; echo '/>'; ?>
                    </td></tr>
                    </table></center>
        </form>
</body>
</html>

<script type="text/javascript">   
$('#cname').autocomplete({ source: <?php echo allcustomers(); ?>, minLength: 2  });                                                                

</script>



