
function validation(url){
	xrow = new Number($('#row').val());
	chk_qty = 0;
						
	for(i=0;i<xrow;i++){
		qty = new Number($('#qty'+i).val());
		chk_qty  = chk_qty + qty;
	}
		
		if(chk_qty<=0){
			
			$.alert({
	        	title: 'Warning!',
    	    	content: 'Qty Produk Tidak Boleh Kosong!',
				action:	$('#pm0').focus()
			    });
				return false;
			}
			
		else if($('#ven_autoid').val()==""){
			$.alert({
	        	title: 'Warning!',
    	    	content: 'Harap Pilih Vendor!',
				action:	$('#ven_autoid').focus()
			    });
				return false;
			}
		else{
			dosubmit(url);
			}
				
}
	
function calc_cb(){
					xrow = new Number($('#row').val());
					amount = 0;
					for(i=0;i<xrow;i++){
						price = new Number( $('#pm_bprice'+i).val() );
						qty = new Number($('#qty'+i).val());
						disc = new Number($('#pm_disc'+i).val());
						pm_disctype = $('#pm_disctype'+i).val();
						
						if(qty=='NaN'){qty = 0;}
						
							if(pm_disctype=="P"){
								nett_price = price - (price * disc/100);
								}
							else{
								nett_price = price - disc;
								}
							subtotal = nett_price * qty;
							
							$('#nett_price'+i).val(nett_price);
							$('#subtotal'+i).val(subtotal);
							amount = amount + subtotal;
						}
						$('#subtotal').val(amount);
		calc_gtotal();
	}
	
function get_barcode(i){
	prev = i-1;
	
	query = {pm:$('#pm_barcode'+i).val(),row:i}
	
	$.post('getbarcode.php',query,function(e){
		$('#chk').html(e);
		pm_autoid = $('#_pm_autoid').val();
		pm_name = $('#_pm_name').val()
		hrg_beli = $('#_hrg_beli').val()
				if($('#pm_barcode'+i).val()==$('#pm_barcode'+prev).val()){
				$("#pm_autoid"+i).val('');
				$("#pm"+i).val('');
				$("#pm_bprice"+i).val('');			
				$('#qty'+i).val('');
				qty = 	new Number($('#qty'+prev).val());
				$('#qty'+prev).val(qty+1);
				calc_cb();
				$('#pm_barcode'+i).val('');
				$('#pm_barcode'+i).focus();
				}
			else{
			$("#pm_autoid"+i).val(pm_autoid);
			$("#pm"+i).val(pm_name);
			$("#pm_bprice"+i).val(hrg_beli);			
			$('#qty'+i).val(1);
			if(pm_name){
				get_ums(i);
			}
			calc_cb();
				add_detailrow();				
				}	
		

	})
}
				
function pm_auto_complete(i){
	

		$("#pm"+i).autocomplete('getproduct.php', {
		width: 450,
		matchContains: true,
		selectFirst: false
		});
		
			$("#pm"+i).result(function(event, data, formatted) {
				$("#pm_autoid"+i).val(data[1]);
				$("#pm_bprice"+i).val(data[3]);
			});
}

function get_ums(i){
	query = {
			pm_autoid:$('#pm_autoid'+i).val(),
			row:i
			}
			
		$.post('get_ums',query,function(e){
				$("#pm_ums"+i).html(e);
			})
	}

function add_detailrow(){

			xrow = new Number($('#row').val());
			
			query = {id:1,row:xrow}			
			
			nextrow = xrow + 1;
			
			$.post('getnewrow',query,function(data){
					$("#jt_table").last().append(data);
					$('#row').val(nextrow);
					$('#pm_barcode'+xrow).focus();
				})
}		
			
function chk_field(i){
			
				pm_autoid 	= new Number($('#pm_autoid'+i).val());
				qty 		= new Number($('#qty'+i).val());
				
				row = $('#row').val();
				curr  = new Number(i);
				
				if(curr+1==row){
					if(pm_autoid + qty > 0){
						add_detailrow();
					}
				}
	}


// MATH GRAND TOTAL TRANSACTION //
function calc_gtotal(){
	subtotal = new Number($('#subtotal').val());
	cdtype = $('#mst_disctype').val();
	cashdisc = new Number($('#cashdisc').val());
	if(cdtype=="R"){
		gtotal = subtotal - cashdisc;
		}
	else{
		cashdisc_rp = subtotal * cashdisc/100;
		gtotal = subtotal - cashdisc_rp;
		}
	$('#gtotal').val(gtotal);
	}


$().ready(function(e) {
	$('#transdate').datepicker({
    	format: 'dd/mm/yyyy',
	   	startDate: '-3d'
	});	
});

function getdate(id){
		$('#'+id).datepicker({
    			format: 'dd/mm/yyyy',
	    		startDate: '-3d'
			});			
	}
