<script type="text/javascript">
$(document).ready(function() {
	
	$("#radio_admin").attr("checked","checked");
	$("#radio_n").attr("checked","checked");
	$("#txt_user").focus();
		
	$("#tambah").click(function(){
		$(".input").val('');
		$(".input").val('');
		$(".radio_level").attr("checked","");
		$("#txt_user").focus();
	});
	
	$("#txt_user").keyup(function() {
		var user 	= $("#txt_user").val()
		
		$.ajax({
			type	: "POST",
			url		: "spv/users/cari.php",
			data	: "user="+user,
			dataType: "json",
			success	: function(data){
				$("#txt_pwd1").val(data.pwd1);
				$("#txt_pwd2").val(data.pwd2);
				$("#txt_nama").val(data.nama);
				if(data.admin=='admin') {
					$("#radio_admin").attr("checked","checked");
				}								
			}
		});
	});
	
	$("#simpan").click(function(){
		simpan_data();
	});

	function simpan_data(){
		var user	= $("#txt_user").val();
		var pwd1	= $("#txt_pwd1").val();
		var pwd2	= $("#txt_pwd2").val();
		var nama	= $("#txt_nama").val();
		var level	= $(".radio_level:checked").val();
		var jml_level = $(".radio_level:checked");
		
		if(user.length == 0){
           alert("Maaf, User Name tidak boleh kosong");
		   $("#txt_user").focus();
		   return (false);
        }
		if(pwd1.length == 0){
           alert("Maaf, Password tidak boleh kosong");
		   $("#txt_pwd1").focus();
		   return (false);
        }	 
		if(pwd2.length == 0){
           alert("Maaf, Ulangi Password tidak boleh kosong");
		   $("#txt_pwd2").focus();
		   return (false);
        }	 
		if(pwd1 != pwd2){
           alert("Maaf, Ulangi Password tidak sama");
		   $("#txt_pwd2").focus();
		   return (false);
        }
		if(nama.length == 0){
           alert("Maaf, Nama Lengkap tidak boleh kosong");
		   $("#txt_nama").focus();
		   return (false);
        }	 
		if(jml_level.length == 0){
           alert("Maaf, Anda belum memilih Level");
		   return (false);
        }
		
		$.ajax({
			type	: "POST",
			url		: "spv/users/simpan.php",
			data	: "user="+user+
					"&pwd1="+pwd1+
					"&nama="+nama+
					"&level="+level,
			timeout	: 3000,
			beforeSend	: function(){		
				$("#info").html("<img src='loading.gif' />");			
			},				  
			success	: function(data){
				$("#info").html(data);
				$("#tampil_data").load('spv/users/tampil_data.php');
			}
		});
	}
	
	$("#keluar").click(function(){
		document.location='?module=home';
	});
});
</script>
<?php
echo "<input type='hidden' id='akses'>";
echo "<table>
	<tr>
		<td>User Name</td>
		<td>:</td>
		<td><input type='text' name='txt_user' id='txt_user'  size='40' lenght='50' class='input'></td>
	</tr>
	<tr>
		<td>Password</td>
		<td>:</td>
		<td><input type='password' name='txt_pwd1' id='txt_pwd1'  size='40' lenght='50' class='input'></td>
	</tr>
	<tr>
		<td>Ulangi Password</td>
		<td>:</td>
		<td><input type='password' name='txt_pwd2' id='txt_pwd2'  size='40' lenght='50' class='input'></td>
	</tr>	
	<tr>
		<td>Nama Lengkap</td>
		<td>:</td>
		<td><input type='text' name='txt_nama' id='txt_nama'  size='50' lenght='50' class='input'></td>
	</tr>
	</table>
	
	<table>
	<tr>
		<td width='105'>Level</td>
		<td>:</td>
		<td><input type='radio' name='txt_level' id='radio_admin' value='Administrator' class='radio_level'>Administrator</td>
		<td><input type='radio' name='txt_level' id='radio_admin' value='Kasir_Retail' class='radio_level'>Kasir_Retail</td>
		<td><input type='radio' name='txt_level' id='radio_admin' value='Kasir_Grosir' class='radio_level'>Kasir_Grosir</td>
		<td><input type='radio' name='txt_level' id='radio_admin' value='Gudang' class='radio_level'>Gudang</td>
		<td><input type='radio' name='txt_level' id='radio_admin' value='MB' class='radio_level'>MB</td>
	</tr>
	</table>
	<table width='100%'
	<tr>
		<td colspan='3' align='center'>
		<button name='tambah' id='tambah'>Tambah</button>
		<button name='simpan' id='simpan'>Simpan</button>
		<button name='keluar' id='keluar'>Keluar</button>
		</td>
	</tr>
	</table>";
?>