<!---- 
    Author 	: Ladur
    Tanggal : 12-08-2014	
    Revisi 	: Fachri
-->

<div class="easyui-layout" data-options="fit:true,border:false">
	<!-- Top view-->
    <div id="form-header" data-options="region:'north',border:false" >
        <div id="form-header-wrapper">
            <table cellpadding="0" cellspacing="0" width="100%" height="100%" border="0">
            <tr>
                <td align="left">
                    <a href="#" class="easyui-linkbutton" iconCls="icon-document" plain="true" onclick="location.reload()">New</a>
                    <!--                    
                    <input type="checkbox" name="chk_status" id="chk_status" disabled/>
                    <span style="color:gray">Suspend</span
                    >-->
                </td>
                <td height="25" align="right" valign="middle">
                    <span class="form-title" style="margin-left:7px;">
                        <?=$menu_label?>
                    </span>
                </td>
            </tr>
            </table>		     	
        </div>			
    </div>
    <!-- End of top view -->
    
    <div id="form-body" data-options="region:'center',border:false">
        <div id="form-wrapper" class="easyui-panel" style="width:1180%;height:526px;padding:5px;" fit="true">
        	<form id="fm_journal_voucher" name="fm_journal_voucher" method="post">
			    <input type="hidden" id="detail_jv" name="detail_jv" />
            	<input type="hidden" id="sub_type" name="sub_type" />
                <input type="hidden" id="cek_data" name="cek_data" />
                 <input type="hidden" id="txt_vendor_customer" name="txt_vendor_customer" />
                 <input type="hidden" id="txt_currency" name="txt_currency" value="0"/>
                 <input type="hidden" id="txt_hidden_debit" name="txt_hidden_debit" />
                 <input type="hidden" id="txt_hidden_rate" name="txt_hidden_rate" />
                 <input type="hidden" id="flag_status_vendor" name="flag_status_vendor" />
                 <input type="hidden" id="flag_status_customer" name="flag_status_customer" />
                
                <!-- Form Header -->
                <table width="100%" height="141" cellpadding="0" cellspacing="0">
                <tr>
                    <td width="50%" valign="top">
                    	<table width="100%" cellpadding="0" cellspacing="1" style="margin-top:4px;">
                        <tr height="20">
                        	<td width="120"><b>Voucher No.</b></td>
                            <td width="10"><b>:</b></td>
                            <td>
                            	<input name="txt_voucher_no" id="txt_voucher_no" value="<?=$journal_number?>" type="text" class="easyui-validatebox" style="width:178px;height:18px" required readonly />
                            </td>
                        </tr>
                        <tr height="20">
                        	<td><b>Voucher Date</b></td>
                            <td><b>:</b></td>
                            <td>
                            	<input name="txt_voucher_date" id="txt_voucher_date" type="text" class="easyui-datebox validBackward" style="width:180px;" required editable="false" value="<?php echo $date_now;?>><"/>
                            </td>
                        </tr>
                        <tr height="20">
                        	<td valign="top" style="padding-top:4px;"><b>Description</b></td>
                            <td valign="top" style="padding-top:4px;"><b>:</b></td>
                            <td>
                            	<textarea class="textbox" style="width:178px;resize:none;white-space:normal;" name="txa_description" id="txa_description" rows="4"></textarea>
                            </td>
                        </tr>
                        </table>
                    </td>
                    <td width="50%" valign="top">&nbsp;</td>
                </tr>
                </table>
                <!-- End of form header -->
                
                <!-- Datagrid -->
                <table id="dtg_journal_voucher" class="easyui-edatagrid" toolbar="#tb_dtg_journal_voucher"
					idField="id"
					rownumbers="true"  singleSelect="true" style="height:355px;">
					
				</table>
                <!-- Dialog Button -->
				<div id="tb_dtg_journal_voucher">
					<a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="addRow()">New</a>
					<a href="#" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="javascript:$('#dtg_journal_voucher').edatagrid('destroyRow')">Delete</a>
				</div>
                <!-- End of Dialog Button -->
                <!-- End of Datagrid -->
                
                
        	</form>
		</div>
	</div>
    <div id="form-footer" data-options="region:'south',border:false">
    	<table width="100%" cellpadding="0" cellspacing="0">
            <tr>
                <td>&nbsp;</td>
                <td align="right">
                     <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-help" onclick="" plain="true">Help</a>
                     <a href="javascript:void(0)" class="easyui-linkbutton form-button" iconCls="icon-save" onclick="saveData();">Save</a> 								
                     <a href="javascript:void(0)" class="easyui-linkbutton form-button" iconCls="icon-print" onclick="">Preview & Print</a>   
                     <a href="javascript:void(0)" class="easyui-linkbutton form-button" iconCls="icon-cancel" onclick="">Cancel</a>	
                     </td>
             </tr>
         </table>
		</div>
</div>

<div id="dialog-account" class="easyui-dialog" style="width:655px; left:150px; top:50px; background-color:#F8F8F8; height:400px; padding: 0 0px 0 0px" closed="true" data-options="modal:true, iconCls:'icon-save'" buttons="#dialog-buttons-account-payment">
    <form id="form_account" method="post" novalidate>
        <table width="100%" align="left" border="0">
        <tr>
            <td align="left" valign="middle">
                <input type="text" style="width:150px;height:20px" name="txt_account_number" id="txt_account_number" class="easyui-validatebox" placeholder="Account Number" onkeyup="search_account_popup()" />
            </td>
            <td colspan="2" align="left" valign="middle">
                <input type="text" style="width:476px;height:20px" name="txt_account_name" id="txt_account_name" class="easyui-validatebox" placeholder="Account Name" onkeyup="search_account_popup()" />
            </td>
		</tr>
        <tr>
            <td colspan="3">
                <table id="tt_choose_account_journal" class="easyui-datagrid" 
                    url="<?php echo base_url(); ?>activities/journal_voucher/get_akun" 
                    singleSelect="true" 
                    style="width:635px;height:290px"
                >
                    <thead>
                    <tr>
                        <th field="AccountNumber" width="150" sortable="true">Account Number</th>
                        <th field="AccountName" width="460" sortable="true">Name</th>
                        <th field="AccountSubTypeID" width="460" sortable="true" hidden="true">AccountSubType ID</th>
                        <th field="CurrencyCode" width="460" sortable="true" hidden="true">Currency Code</th>
                    </tr>
                    </thead>
                </table>
            </td>
        </tr>
        </table>
    </form>
</div>

<!-- Dialog Button -->
<div id="dialog-buttons-account-payment">
	<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-ok" onClick="choose_account_journal();">OK</a>
	<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onClick="modal_close('dialog-account')">Cancel</a>
</div>

<div id="dialog-vendor" class="easyui-dialog" style="width:575px;height:523px;top:27px;background-color:#F8F8F8;padding:0px;" closed="true" data-options="modal:true" buttons="#dialog-vendor-button">
	<table id="dtg_vendor" class="easyui-datagrid"  
        singleSelect="true" fitColumns="true" 
        pageSize="20" pagination="true" 
        remoteSort="false" rowNumbers="true" 
        fit="true"
    >
		<thead>
			<tr>
                 <th field="VendorCode" width="150" sortable="true">Vendor Code</th>
                 <th field="VendorName" width="460" sortable="true">Vendor Name</th>
            </tr>
		</thead>
	</table>
</div>
<div id="dialog-vendor-button" style="border:none;">
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-ok" onClick="choose_vendor();">OK</a>
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onClick="modal_close('dialog-vendor')">Close</a>
</div>

<div id="dialog-customer" class="easyui-dialog" style="width:575px;height:523px;top:27px;background-color:#F8F8F8;padding:0px;" closed="true" data-options="modal:true" buttons="#dialog-customer-button">
	<table id="dtg_customer" class="easyui-datagrid"  
        singleSelect="true" fitColumns="true" 
        pageSize="20" pagination="true" 
        remoteSort="false" rowNumbers="true" 
        
        fit="true"
    >
		<thead>
			<tr>
                 <th field="CustomerCode" width="150" sortable="true">Customer Code</th>
                 <th field="CustomerName" width="460" sortable="true">Customer Name</th>
            </tr>
		</thead>
	</table>
</div>
<div id="dialog-customer-button" style="border:none;">
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-ok" onClick="choose_customer();">OK</a>
    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onClick="modal_close('dialog-customer')">Close</a>
</div>

<div id="dialog-type-journal" class="easyui-dialog" style="width:655px; left:150px; top:50px; background-color:#F8F8F8; height:400px; padding: 0 0px 0 0px" closed="true" data-options="modal:true, iconCls:'icon-save'" buttons="#dialog-buttons-account-payment">
    <table width="100%" align="left" border="0">
    <tr>
        <td colspan="3">
            <table id="tt_choose_type_journal" class="easyui-datagrid" 
                url="<?php echo base_url(); ?>activities/journal_voucher/get_type_journal" 
                singleSelect="true" 
                style="width:635px;height:290px"
            >
                <thead>
                <tr>
                    <th field="TypeID" width="150" sortable="true">Type ID</th>
                    <th field="TypeName" width="460" sortable="true">Name</th>
                </tr>
                </thead>
            </table>
        </td>
    </tr>
    </table>
</div>

<!-- Dialog Button -->
<div id="dialog-buttons-account-payment">
	<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-ok" onClick="choose_type_journal();">OK</a>
	<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onClick="modal_close('dialog-type-journal')">Cancel</a>
</div>
<script type="text/javascript">
$(function(){
	$('#dtg_journal_voucher').datagrid({
		 columns:[[
			{field:'AccountName',title:'Account Name',width:220,halign:'center',align:'left', editor:{type:'AccountName',options:{required:true}}},
			{field:'Debit',title:'Debit',width:150,halign:'center',align:'center', editor:{type:'Debit',options:{precision:2,decimalSeparator:',',groupSeparator:'.'}}},
			{field:'Credit',title:'Credit',width:150,halign:'center',align:'left', editor:{type:'Credit',options:{precision:2,decimalSeparator:',',groupSeparator:'.'}}},
			{field:'Rate',title:'Exchange Rate',width:108,halign:'center',align:'center', editor:{type:'Rate',options:{required:true}}},
			{field:'TotRateDebet',title:'Total Rate Debet',width:210,halign:'center',align:'left', editor:{type:'numberbox'}, hidden:'true'},
			{field:'TotRateCredit',title:'Total Rate Credit',width:210,halign:'center',align:'left', editor:{type:'numberbox'}, hidden:'true'},
			{field:'Vendor',title:'Vendor Name',width:150,halign:'center',align:'left', editor:{type:'Vendor'}},
			{field:'Customer',title:'Customer Name',width:150,halign:'center',align:'left', editor:{type:'Customer'}},
		
			{field:'AccountNumber',title:'Account Code',width:178,halign:'center',align:'center', editor:{type:'AccountNumber'}, hidden:'true'},
			{field:'AccountSubTypeID',title:'Sub Type',width:178,halign:'center',align:'center', editor:{type:'AccountSubTypeID'}, hidden:'true'},
			{field:'Memo',title:'Memo',width:140,halign:'center',align:'left', editor:{type:'text'}},
			
			{field:'CurrencyCode',title:'Currency Code',width:178,halign:'center',align:'center', editor:{type:'CurrencyCode'}, hidden:'true'},
			{field:'flag',title:'Flag',width:178,halign:'center',align:'center', editor:{type:'flag'}, hidden:'true'},
			{field:'VendorCode',title:'Vendor Code',width:178,halign:'center',align:'center', editor:{type:'VendorCode'}, hidden:'true'},
			{field:'CustomerCode',title:'Customer Code',width:178,halign:'center',align:'center', editor:{type:'CustomerCode'}, hidden:'true'}
		 ]]
	});
	
	//$('#dtg_journal_voucher').datagrid('hideColumn','Rate');
	
});

function choose_akun(){
	$('#dtg_journal_voucher').edatagrid('saveRow');
	
	//modal_close('dialog-expense');
}

function search_customer(CurrencyCode){
	$('#dtg_customer').datagrid({
		url:'<?php echo base_url(); ?>activities/journal_voucher/customer/'+CurrencyCode
	});
}

function search_vendor(CurrencyCode){
	$('#dtg_vendor').datagrid({
		url:'<?php echo base_url(); ?>activities/journal_voucher/vendor/'+CurrencyCode
	});
}

function addRow(){
	$('#dtg_journal_voucher').edatagrid('addRow');
	var rows = $('#dtg_journal_voucher').edatagrid('getRows');
	var jumr = rows.length;
	var new_db = 0;
	var new_cd = 0;
	var tot_new_db = 0;
	var tot_new_cd = 0;
	 
	for(var i=1; i<rows.length; i++){
	   
		var db =  parseInt(rows[i-1].Debet);
		var cd =  parseInt(rows[i-1].Credit);
		
		
		
		new_db += db;
		new_cd += cd;
		tot_new_db = new_db - new_cd;
		tot_new_dc = new_cd - new_db;
		
		if(jumr > 2){
			if(tot_new_db < 0){
			   $('.Credit').val(0);
			} else {
				$('.Credit').val(0);
			}
			
			if(tot_new_dc < 0){
			   $('.Debit').val(0);
			} else {
				$('.Debit').val(0);
			}	
		} 
		else {
			if(isNaN(db)){
			   $('.Credit').val(0);
			} else {
				$('.Credit').val(0);
			}
			
			if(isNaN(cd)){
			   $('.Debit').val(0);
			} else {
				$('.Debit').val(0);
			}	
		}
		//alert(db);
		$('#dtg_journal_voucher').edatagrid('saveRow');
		
		//new_db++;
	}
	
}

function open_account_journal(){ 
   modal_open('Account List','dialog-account');
}

function open_vendor(){ 
   modal_open('Vendor List','dialog-vendor');
}

function open_customer(){ 
   modal_open('Customer List','dialog-customer');
}

function choose_vendor() {
	var selected_vendor = $('#dtg_vendor').datagrid('getSelected');
	$('.Vendor').val(selected_vendor.VendorName);
	$('.VendorCode').val(selected_vendor.VendorCode);
	modal_close('dialog-vendor');
}

function choose_customer() {
	var selected_customer = $('#dtg_customer').datagrid('getSelected');
	$('.Customer').val(selected_customer.CustomerName);
	$('.CustomerCode').val(selected_customer.CustomerCode);
	modal_close('dialog-customer');
}

function choose_account_journal() {
	var selected_data=$('#tt_choose_account_journal').datagrid('getSelected');
	$('.AccountName').val(selected_data.AccountName);
	$('.AccountNumber').val(selected_data.AccountNumber);
	$('.AccountSubTypeID').val(selected_data.AccountSubTypeID);
	$('.Rate').val(selected_data.Rate);
	$('#sub_type').val(selected_data.AccountSubTypeID);
	$('.CurrencyCode').val(selected_data.CurrencyCode);
	$('#txt_currency').val(selected_data.CurrencyCode);
	
	var sub_type_data = $('#sub_type').val();
	var txt_currency_code_hidden = $('#txt_currency').val(selected_data.CurrencyCode);
	if(sub_type_data == 31 || sub_type_data == 32 || sub_type_data == 33 || sub_type_data == 34 || sub_type_data == 35 || sub_type_data == 36 || sub_type_data == 37 || sub_type_data == 44 || sub_type_data == 46 || sub_type_data == 47 || sub_type_data == 63 || sub_type_data == 25 || sub_type_data == 26 || sub_type_data == 27){
		var cek_data = $('#cek_data').val(2);
		$('.flag').val(1);
		$('#flag_status_vendor').val(1);
		$('.Customer').prop('disabled', true);
		$('.Vendor').prop('disabled', false);
		search_vendor(selected_data.CurrencyCode);
		/*if(selected_data.CurrencyCode == "USD"){
			$('#dtg_journal_voucher').datagrid('showColumn','Rate');
		}*/
	}
	else if(sub_type_data == 9 || sub_type_data == 10 || sub_type_data == 11 || sub_type_data == 12 || sub_type_data == 13 || sub_type_data == 14 || sub_type_data == 39 || sub_type_data == 40 || sub_type_data == 41){
		
		var cek_data = $('#cek_data').val(1);
		$('#flag_status_customer').val(2);
		$('.flag').val(2);
		$('.Vendor').prop('disabled', true);
		$('.Customer').prop('disabled', false);
		search_customer(selected_data.CurrencyCode);
		/*if(selected_data.CurrencyCode == "USD"){
			$('#dtg_journal_voucher').datagrid('showColumn','Rate');
		}*/
	}
	else{
		
		$('.Vendor').prop('disabled', true);
		$('.Customer').prop('disabled', true);
	}

	if(selected_data.CurrencyCode == 'IDR' || selected_data.CurrencyCode == ""){
		$('.Rate').prop('readonly', true);
		$('.Rate').val("1");
	}
	else if(selected_data.CurrencyCode != 'IDR' || selected_data.CurrencyCode != ""){
		$('.Rate').prop('readonly', false);
	}
	modal_close('dialog-account');
}

function open_type_journal(){ 
   modal_open('Type Journal','dialog-type-journal');
}

function choose_type_journal() {
	var selected_data=$('#tt_choose_type_journal').datagrid('getSelected');
	$('.TypeJournal').val(selected_data.TypeName);
	$('.TypeJournalID').val(selected_data.TypeID);
	modal_close('dialog-type-journal');
}

function search_account_popup(){
	$('#tt_choose_account_journal').datagrid('load',{
		AccountNo 	: $('#txt_account_number').val(),
		AccountName : $('#txt_account_name').val()
	});
}

function saveData(){
	var total_debit = 0;
	var total_credit = 0;
	var flag_status =  $('.flag').val();
	var vendor 		=  $('.Vendor').val();
	var customer 	=  $('.Customer').val();
	
	$('#dtg_journal_voucher').edatagrid('saveRow');
	var rows = $('#dtg_journal_voucher').edatagrid('getRows');
	var jum = rows.length;
	var items = JSON.stringify(rows);
	$("#detail_jv").val(items);
	
	for(var i=0; i<rows.length; i++){
		if(rows[i].Debit == ''){ rows[i].TotRateDebet = 0; }
			total_debit += parseInt(rows[i].TotRateDebet);
		if(rows[i].Credit == ''){ rows[i].TotRateCredit = 0; }
			total_credit += parseInt(rows[i].TotRateCredit);
	}
	
	cek_data = $('#cek_data').val();
	if(total_debit != total_credit){
		$.messager.alert('Unseccessfull','Account Tidak Balance','error');
		return false;
	}/*
	else if(flag_status == 1 || flag_status == 2){
		if(customer == ""){
			$.messager.alert('Unseccessfull','Customer Tidak Boleh Kosong','error');
			return false;
		}
		else if(vendor == ""){
			$.messager.alert('Unseccessfull','Vendor Tidak Boleh Kosong','error');
			return false;
		}
	}*/
	else {
		//$("#fm_journal_voucher").submit();
		save_transaction('<?php echo"$menu_link"; ?>', '#fm_journal_voucher','1');
	}
}

$.extend($.fn.datagrid.defaults.editors, {
	Debit: {
		init: function(container, options){
			var input = $('<input type="text" class="Debit" />').appendTo(container);
			return input.validatebox(options);
		},
		destroy: function(target){
			$(target).remove();
		},
		getValue: function(target){
			 return $(target).val();
		},
		setValue: function(target, value){
			  $(target).val(value);
		},
		resize: function(target, width){
			   $(target)._outerWidth(width);
		}
	},
	Credit: {
		init: function(container, options){
			var input = $('<input type="text" class="Credit" />').appendTo(container);
			return input.validatebox(options);
		},
		destroy: function(target){
			$(target).remove();
		},
		getValue: function(target){
			 return $(target).val();
		},
		setValue: function(target, value){
			  $(target).val(value);
		},
		resize: function(target, width){
			   $(target)._outerWidth(width);
		}
	},
	Rate: {
		init: function(container, options){
			var input = $('<input type="text" class="Rate" />').appendTo(container);
			return input.validatebox(options);
		},
		destroy: function(target){
			$(target).remove();
		},
		getValue: function(target){
			 return $(target).val();
		},
		setValue: function(target, value){
			  $(target).val(value);
		},
		resize: function(target, width){
			   $(target)._outerWidth(width);
		}
	},
	AccountName: {
		init: function(container, options){
			var input = $('<input type="text" class="AccountName" onClick="open_account_journal()" style="height:18px;" readonly/>').appendTo(container);
			return input.validatebox(options);
		},
		destroy: function(target){
			$(target).remove();
		},
		getValue: function(target){
			 return $(target).val();
		},
		setValue: function(target, value){
			  $(target).val(value);
		},
		resize: function(target, width){
			   $(target)._outerWidth(width);
		}
	},
	Vendor: {
		init: function(container, options){
			var input = $('<input type="text" class="Vendor" onClick="open_vendor()" style="height:18px;" />').appendTo(container);
			return input.validatebox(options);
		},
		destroy: function(target){
			$(target).remove();
		},
		getValue: function(target){
			 return $(target).val();
		},
		setValue: function(target, value){
			  $(target).val(value);
		},
		resize: function(target, width){
			   $(target)._outerWidth(width);
		}
	},
	
	VendorCode: {
		init: function(container, options){
			var input = $('<input type="text" class="VendorCode" style="height:18px;" />').appendTo(container);
			return input.validatebox(options);
		},
		destroy: function(target){
			$(target).remove();
		},
		getValue: function(target){
			 return $(target).val();
		},
		setValue: function(target, value){
			  $(target).val(value);
		},
		resize: function(target, width){
			   $(target)._outerWidth(width);
		}
	},
	Customer: {
		init: function(container, options){
			var input = $('<input type="text" class="Customer" onClick="open_customer()" style="height:18px;" />').appendTo(container);
			return input.validatebox(options);
		},
		destroy: function(target){
			$(target).remove();
		},
		getValue: function(target){
			 return $(target).val();
		},
		setValue: function(target, value){
			  $(target).val(value);
		},
		resize: function(target, width){
			   $(target)._outerWidth(width);
		}
	},
	CustomerCode: {
		init: function(container, options){
			var input = $('<input type="text" class="CustomerCode" style="height:18px;" />').appendTo(container);
			return input.validatebox(options);
		},
		destroy: function(target){
			$(target).remove();
		},
		getValue: function(target){
			 return $(target).val();
		},
		setValue: function(target, value){
			  $(target).val(value);
		},
		resize: function(target, width){
			   $(target)._outerWidth(width);
		}
	},
	flag: {
		init: function(container, options){
			var input = $('<input type="text" class="flag" style="height:18px;" />').appendTo(container);
			return input.validatebox(options);
		},
		destroy: function(target){
			$(target).remove();
		},
		getValue: function(target){
			 return $(target).val();
		},
		setValue: function(target, value){
			  $(target).val(value);
		},
		resize: function(target, width){
			   $(target)._outerWidth(width);
		}
	},
	AccountNumber: {
		init: function(container, options){
			var input = $('<input type="text" class="AccountNumber" style="height:18px;"/>').appendTo(container);
			return input.validatebox(options);
		},
		destroy: function(target){
			$(target).remove();
		},
		getValue: function(target){
			 return $(target).val();
		},
		setValue: function(target, value){
			  $(target).val(value);
		},
		resize: function(target, width){
			   $(target)._outerWidth(width);
		}
	},
	AccountSubTypeID: {
		init: function(container, options){
			var input = $('<input type="text" class="AccountSubTypeID" style="height:18px;"/>').appendTo(container);
			return input.validatebox(options);
		},
		destroy: function(target){
			$(target).remove();
		},
		getValue: function(target){
			 return $(target).val();
		},
		setValue: function(target, value){
			  $(target).val(value);
		},
		resize: function(target, width){
			   $(target)._outerWidth(width);
		}
	},
	TypeJournal: {
		init: function(container, options){
			var input = $('<input type="text" class="TypeJournal" onClick="open_type_journal()" style="height:18px;" readonly/>').appendTo(container);
			return input.validatebox(options);
		},
		destroy: function(target){
			$(target).remove();
		},
		getValue: function(target){
			 return $(target).val();
		},
		setValue: function(target, value){
			  $(target).val(value);
		},
		resize: function(target, width){
			   $(target)._outerWidth(width);
		}
	},
	TypeJournalID: {
		init: function(container, options){
			var input = $('<input type="text" class="TypeJournalID" style="height:18px;"/>').appendTo(container);
			return input.validatebox(options);
		},
		destroy: function(target){
			$(target).remove();
		},
		getValue: function(target){
			 return $(target).val();
		},
		setValue: function(target, value){
			  $(target).val(value);
		},
		resize: function(target, width){
			   $(target)._outerWidth(width);
		}
	},
	CurrencyCode: {
		init: function(container, options){
			var input = $('<input type="text" class="CurrencyCode" style="height:18px;" />').appendTo(container);
			return input.validatebox(options);
		},
		destroy: function(target){
			$(target).remove();
		},
		getValue: function(target){
			 return $(target).val();
		},
		setValue: function(target, value){
			  $(target).val(value);
		},
		resize: function(target, width){
			   $(target)._outerWidth(width);
		}
	},
});
	
$(document).ready(function(){
	$('#dtg_journal_voucher').datagrid({
		onBeginEdit:function(rowIndex){
			editors = $('#dtg_journal_voucher').datagrid('getEditors',rowIndex);
			debet			= $(editors[1].target);
			credit			= $(editors[2].target);
			rate			= $(editors[3].target);
			TotRateDebet	= $(editors[4].target);
			TotRateCredit	= $(editors[5].target);
			sub_type		= $(editors[9].target);
			currency		= $(editors[11].target);
			
			sub_type 		= $(sub_type).val();
			currency_code 	= $(currency).val();
			
			if(sub_type == 31 || sub_type == 32 || sub_type == 33 || sub_type == 34 || sub_type == 35 || sub_type == 36 || sub_type == 37 || sub_type == 44 || sub_type == 46 || sub_type == 47 || sub_type == 63 || sub_type == 25 || sub_type == 26 || sub_type == 27){
				$('.Customer').prop('disabled', true);
				$('.Vendor').prop('disabled', false);
			}
			else if(sub_type == 9 || sub_type == 10 || sub_type == 11 || sub_type == 12 || sub_type == 13 || sub_type == 14 || sub_type == 39 || sub_type == 40 || sub_type == 41){
				$('.Vendor').prop('disabled', true);
				$('.Customer').prop('disabled', false);
			}
			else{
				if(sub_type != ""){
					$('.Customer').prop('disabled', true);
					$('.Vendor').prop('disabled', true);
				}
			}
			
			if(currency_code == 'IDR' || currency_code == ""){
				$('.Rate').prop('readonly', true);
				$('.Rate').val(1);
			}
			else if(currency_code != 'IDR' || currency_code != ""){
				$('.Rate').prop('readonly', false);
			}
	
			rate.numberbox({
				onChange:function(){
					debet_val 	= $(debet).val();
					credit_val 	= $(credit).val();
					sub_type 	= $(sub_type).val();
					rate_2 		= $(rate).val();
					rate_val 	= $(rate).numberbox('getValue');
					
					if(sub_type == 31 || sub_type == 32 || sub_type == 33 || sub_type == 34 || sub_type == 35 || sub_type == 36 || sub_type == 37 || sub_type == 44 || sub_type == 46 || sub_type == 47 || sub_type == 63 || sub_type == 25 || sub_type == 26 || sub_type == 27){
						if(debet_val != 0){
							$('#txt_hidden_debit').val(debet_val);
							$('#txt_hidden_rate').val(rate_2);
						}
						else{
							$('#txt_hidden_debit').val(credit_val);
							$('#txt_hidden_rate').val(rate_2);
						}
					}
					else if(sub_type == 9 || sub_type == 10 || sub_type == 11 || sub_type == 12 || sub_type == 13 || sub_type == 14 || sub_type == 39 || sub_type == 40 || sub_type == 41){
						if(debet_val != 0){
							$('#txt_hidden_debit').val(debet_val);
							$('#txt_hidden_rate').val(rate_2);
						}
						else{
							$('#txt_hidden_debit').val(credit_val);
							$('#txt_hidden_rate').val(rate_2);
						}
					}
				
					amount = parseInt(debet_val) * parseInt(rate_val);
					amount2 = parseInt(credit_val) * parseInt(rate_val);
					$(TotRateDebet).val(amount);
					$(TotRateCredit).val(amount2);
				}
			}),
			debet.numberbox({
				onChange:function(){
					debet_val 	= $(debet).val();
					credit_val 	= $(credit).val();
					sub_type 	= $(sub_type).val();
					rate_2 		= $(rate).val();
					rate_val 	= $(rate).numberbox('getValue');
					
					if(sub_type == 31 || sub_type == 32 || sub_type == 33 || sub_type == 34 || sub_type == 35 || sub_type == 36 || sub_type == 37 || sub_type == 44 || sub_type == 46 || sub_type == 47 || sub_type == 63 || sub_type == 25 || sub_type == 26 || sub_type == 27){
						if(debet_val != 0){
							$('#txt_hidden_debit').val(debet_val);
							$('#txt_hidden_rate').val(rate_2);
						}
						else{
							$('#txt_hidden_debit').val(credit_val);
							$('#txt_hidden_rate').val(rate_2);
						}
					}
					else if(sub_type == 9 || sub_type == 10 || sub_type == 11 || sub_type == 12 || sub_type == 13 || sub_type == 14 || sub_type == 39 || sub_type == 40 || sub_type == 41){
						if(debet_val != 0){
							$('#txt_hidden_debit').val(debet_val);
							$('#txt_hidden_rate').val(rate_2);
						}
						else{
							$('#txt_hidden_debit').val(credit_val);
							$('#txt_hidden_rate').val(rate_2);
						}
					}
				
					amount = parseInt(debet_val) * parseInt(rate_2);
					amount2 = parseInt(credit_val) * parseInt(rate_2);
					
					$(TotRateDebet).val(amount);
					$(TotRateCredit).val(amount2);
				}
			}),
			credit.numberbox({
				onChange:function(){
					debet_val 	= $(debet).val();
					credit_val 	= $(credit).val();
					sub_type 	= $(sub_type).val();
					rate_2 		= $(rate).val();
					rate_val 	= $(rate).numberbox('getValue');
					
					if(sub_type == 31 || sub_type == 32 || sub_type == 33 || sub_type == 34 || sub_type == 35 || sub_type == 36 || sub_type == 37 || sub_type == 44 || sub_type == 46 || sub_type == 47 || sub_type == 63 || sub_type == 25 || sub_type == 26 || sub_type == 27){
						if(debet_val != 0){
							$('#txt_hidden_debit').val(debet_val);
							$('#txt_hidden_rate').val(rate_2);
						}
						else{
							$('#txt_hidden_debit').val(credit_val);
							$('#txt_hidden_rate').val(rate_2);
						}
					}
					else if(sub_type == 9 || sub_type == 10 || sub_type == 11 || sub_type == 12 || sub_type == 13 || sub_type == 14 || sub_type == 39 || sub_type == 40 || sub_type == 41){
						if(debet_val != 0){
							$('#txt_hidden_debit').val(debet_val);
							$('#txt_hidden_rate').val(rate_2);
						}
						else{
							$('#txt_hidden_debit').val(credit_val);
							$('#txt_hidden_rate').val(rate_2);
						}
					}
				
					amount = parseInt(debet_val) * parseInt(rate_2);
					amount2 = parseInt(credit_val) * parseInt(rate_2);
					
					$(TotRateDebet).val(amount);
					$(TotRateCredit).val(amount2);
				}
			});
		}
	});
	
	$('#dtg_journal_voucher').edatagrid('addRow');
	$('.Credit').val(0);
	$('.Debit').val(0);
	
	$('#tt_choose_account_journal').datagrid({
		onDblClickRow:function(){
			choose_account_journal();
		}
	});
	
	$('#fm_journal_voucher').click(function(){
		$('#dtg_journal_voucher').edatagrid('saveRow');
	});
	
	$('#txt_voucher_no').val('<?php echo $journal_number; ?>');
	
	$('#txt_voucher_date').datebox({
		onChange:function(){
			url  = base_url + 'activities/journal_voucher/get_jv_number';                
			var jv_af_date   = $('#txt_voucher_date').datebox('getValue');
			
			$.post(url, {jv_af_date:jv_af_date}, function(result){
				if (result.status === true){
					$('#txt_voucher_no').val(result.number);
				} 
			},'json');
		}
	});
	
});
</script>