<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(){
	var akses = $("#akses").val();								
	if (akses==1){
		var pilih = confirm("Data sudah ada, apakah Akan mengubahnya ?");
		if (pilih==true) {
			simpan_data();
			return false;
		}
		else{
			$("#info").html("Anda membatalkan proses simpan");
			return false;
		}
	}
	else{
		simpan_data();
		return false;
	}
	});

	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");
		
		var error = false;

		if(user.length == 0){
           var error = true;
           alert("Maaf, User Name tidak boleh kosong");
		   $("#txt_user").focus();
		   return (false);
         }
		if(pwd1.length == 0){
           var error = true;
           alert("Maaf, Password tidak boleh kosong");
		   $("#txt_pwd1").focus();
		   return (false);
         }	 
		if(pwd2.length == 0){
           var error = true;
           alert("Maaf, Ulangi Password tidak boleh kosong");
		   $("#txt_pwd2").focus();
		   return (false);
         }	 
		if(pwd1 != pwd2){
           var error = true;
           alert("Maaf, Ulangi Password tidak sama");
		   $("#txt_pwd2").focus();
		   return (false);
         }	 
		 
		if(nama.length == 0){
           var error = true;
           alert("Maaf, Nama Lengkap tidak boleh kosong");
		   $("#txt_nama").focus();
		   return (false);
         }	 
		 if(jml_level.length == 0){
           var error = true;
           alert("Maaf, Anda belum memilih Level");
		   //$("#txt_user").focus();
		   return (false);
         }

		 
		if(error == 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');
				$("#akses").val(1);
			}
		});
		}
		return false; 		
		
	}
	
	$("#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='supervisor' class='radio_level'>Supervisor</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='cashier' class='radio_level'>Cashier</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>";
?>