<!---- 
    Author 	: Tagor / Tanggal : 03-10-2014	
    Revisi 	: Ferry
    Revisi 	: Fachri 10-Maret-2015
-->

	<?php
			//---------Untuk privellege jika dia ada tombol save----------
				if($group_user->SavePrivillage == 1){
					$cek_autority_save  = "";
				}
				else{
					$cek_autority_save 	= "disabled";
				}
				
				//-------------end----------------------------
	?>

<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" valign="middle">
                        	<a href="#" class="easyui-linkbutton" iconCls="icon-document" plain="true" onclick="location.reload();">New</a>
						</td>							
						<td height="25" align="right" valign="middle">								
							<span class="form-title">
								<?=$menu_label?>
							</span>
						</td>
					</tr>
				</table>		     	
		 </div>			
	</div>
	<!-- end top view -->	
    
    <!-- center view -->
	<div id="form-body" data-options="region:'center',border:false">
		<div id="form-wrapper" class="easyui-panel"  style="width:1180%;height:525px;padding:5px;" fit="true">
			<form id="fm_vendor" name="fm_vendor" method="post">
            	<input type="hidden" id="id" name="id" value="<?php echo $row->VendorCode;?>" />
                <input type="hidden" name="detail" id="detail"/>
                <input type="hidden" name="id_klik" id="id_klik"/>
                <input type="hidden" name="konversi" id="konversi" class="easyui-numberbox"/>
                <input type="hidden" name="rate_data" id="rate_data" class="easyui-numberbox"/>
                 <input type="hidden" name="detail_dp" id="detail_dp"/>
                 <input name="txt_rate" id="txt_rate" type="hidden" class="easyui-numberbox" style="width:110px;" data-options="min:0.01,precision:2,decimalSeparator:',',groupSeparator:'.',prefix:''" formatter="formatRp" />
            	<div id="wrapptabs" name="" class="easyui-tabs">
					<div id="tab_addres" title="Address" style="padding:10px">
                    	<table width="100%" cellpadding="0" cellspacing="0">
                        <tr>
                            <td><b>Vendor Code</b></td>
                            <td width="850" style="padding:1px 0px 2px;">
                                <input type="text" name="txt_vendorno" class="easyui-validatebox" data-options="required:true" value="<?php echo $row->VendorCode;?>" style="width:850px;text-transform:uppercase;background-color:#E3E2E2;" readonly />
                            </td>
                        </tr>
                        <tr>
                            <td><b>Vendor Name</b></td>
                            <td width="850" style="padding:1px 0px 2px;">
                                <input type="text" name="txt_vendorname" id="txt_vendorname" class="easyui-validatebox" data-options="required:true" autocomplete="off" value="<?php echo $VendorName;?>" style="width:650px;text-transform:uppercase;" />
                            	- <input type="text" name="txt_curency_not_real" class="easyui-validatebox" data-options="required:true" value="<?= $row->CurrencyCode;?>" style="width:185px;text-transform:uppercase;"  readonly />
                            </td>
                        </tr>
                        <tr>
                            <td><b>Vendor Address</b></td>
                            <td width="850" style="padding:1px 0px 2px;">
                                <input type="text" name="txt_address" id="txt_address" class="easyui-validatebox" value="<?php echo $row->VendorAddress;?>" style="width:850px;" required="true" autocomplete="off" />
                            </td>
                        </tr>
                        <tr>
                            <td><b></b></td>
                            <td width="850" style="padding:1px 0px 2px;">
                                <input type="text" name="txt_addressdetail" style="width:850px" />
                            </td>
                        </tr>
                        <tr>
                            <td><b>Tax Address</b></td>
                            <td width="850" style="padding:1px 0px 2px;">
                                <input type="text" name="txt_taxaddress" value="<?php echo $row->VendorTaxAddress;?>" style="width:850px;" />
                            </td>
                        </tr>
                        <tr>
                            <td><b></b></td>
                            <td width="850" style="padding:1px 0px 2px;">
                                <input type="text" name="txt_taxaddressdetail" style="width:850px;" />
                            </td>
                        </tr>
                        <tr>
                            <td><b>City/Prov/Zip Code</b></td>
                            <td width="850" style="padding:1px 0px 2px;">
                            	 <select name="cmb_city" id="cmb_city" class="combo-grid" style="width:282px;"  required="true" editable="true"
                                    data-options="
                                    	value:'<?php echo $row->VendorCity; ?>', 
                                        idField:'CityCode',
                                        textField:'CityName',
                                        url:'<?php echo base_url(); ?>custom/get_data/city',
                                        method: 'get',
                                        columns: [[
                                            {field:'CityCode',title:'City Code',width:120,halign:'center',align:'left'},
                                            {field:'CityName',title:'City Name',width:230,halign:'center',align:'left'}
                                        ]],
                                    ">
                                </select>
								<select name="cmb_state" id="cmb_state" class="easyui-combogrid" style="width:282px;"  required="true" editable="false" readonly
                                    data-options="
                                    	value:'<?php echo $row->VendorState; ?>', 
                                        idField:'StateCode',
                                        textField:'StateName',
                                        url:'<?php echo base_url(); ?>custom/get_data/state',
                                        method: 'get',
                                        columns: [[
                                            {field:'StateCode',title:'State Code',width:100,halign:'center',align:'left'},
                                            {field:'StateName',title:'State Name',width:161,halign:'center',align:'left'}
                                        ]],
                                    ">
                                </select>
								<input type="text" name="txt_zipcode" class="easyui-numberbox" style="width:280px;" value="<?php echo $row->VendorPostalCode;?>"/>
                                
                            </td>
                        </tr>
                        <tr>
                            <td><b>Country</b></td>
                            <td width="850" style="padding:1px 0px 2px;">
                                <select name="cmb_country" id="cmb_country" class="easyui-combogrid" style="width:852px;" required="true" editable="false" readonly
                                data-options="
                                	 value:'<?php echo $row->VendorCountry; ?>', 
                                    idField:'CountryCode',
                                    textField:'CountryName',
                                    url:'<?php echo base_url(); ?>custom/get_data/country',
                                    method: 'get',
                                    columns: [[
                                        {field:'CountryCode',title:'Country Code',width:100,halign:'center',align:'left'},
                                        {field:'CountryName',title:'Country Name',width:161,halign:'center',align:'left'}
                                    ]],
                                ">
                            </select>
                            </td>
                        </tr>
                        <tr>
                            <td><b>Phone</b></td>
                            <td width="850" style="padding:1px 0px 2px;">
                                <input type="text" name="txt_phone" id="txt_phone" class="easyui-validatebox" value="<?php echo str_replace(',','',$row->PhoneNumber);?>" style="width:850px" required="true" autocomplete="off" />
                            </td>
                        </tr>
                        <tr>
                            <td><b>Fax</b></td>
                            <td width="850" style="padding:1px 0px 2px;">
                                <input type="text" name="txt_fax" class="easyui-numberbox" value="<?php echo $row->FacsimileNumber;?>" style="width:852px" autocomplete="off" />
                            </td>
                        </tr>
                        <tr>
                            <td><b>Category</b></td>
                            <td width="850" style="padding:1px 0px 2px;">
                                <input type="text" name="txt_kategori" value="<?php echo $row->VendorCategory;?>" style="width:850px" autocomplete="off" />
                            </td>
                        </tr>
                        <tr>
                            <td><b>Group</b></td>
                            <td width="850" style="padding:1px 0px 2px;">
                                <input type="text" name="txt_group" value="<?php echo $row->VendorGroup;?>" style="width:850px" autocomplete="off" />
                            </td>
                        </tr>
                        <tr>
                            <td><b>Contact Person</b></td>
                            <td width="850" style="padding:1px 0px 2px;">
                                <input type="text" name="txt_contactperson" id="txt_contactperson" class="easyui-validatebox" value="<?php echo $row->ContactPerson;?>" style="width:850px" required="true" autocomplete="off" />
                            </td>
                        </tr>
                        <tr>
                            <td><b>E-Mail</b></td>
                            <td width="850" style="padding:1px 0px 2px;">
                                <input type="text" name="txt_email" class="easyui-validatebox" data-options="validType:'email'" value="<?php echo $row->EmailAddress;?>" style="width:850px" autocomplete="off" />
                            </td>
                        </tr>
                        <tr>
                            <td><b>Web Page</b></td>
                            <td width="850" style="padding:1px 0px 2px;">
                                <input type="text" name="txt_webpage" value="<?php echo $row->WebAddress;?>" style="width:850px" autocomplete="off" />
                            </td>
                        </tr>
                        </table>
                    </div>
                    <div id="tab_terms" title="Terms, etc." style="padding:10px">
                        <fieldset>
                            <legend>Terms, etc</legend>
                            <table width="100%" cellpadding="0" cellspacing="0">
                            <tr>
                                <td><b>Terms</b></td>
                                <td width="850" style="padding:1px 0px 2px;">
                                	<select onchange="get_percent()" name="cmb_terms" 
                                    	id="cmb_terms" class="combo-grid"
                                        style="width:852px;"
                                        data-options="
                                        	value:'<?php echo $row->TermsPaymentID; ?>', 
                                            idField:'TermspaymentID', 
                                            textField:'TermDesc', 
                                            url:'<?php echo base_url(); ?>custom/get_data/termspayment',
                                            method: 'get',
                                            columns: [[
                                                {field:'TermspaymentID',title:'Terms ID',width:400,halign:'center',align:'left'},
                                                {field:'TermDesc',title:'Terms Name',width:430,halign:'center',align:'left'}
                                            ]],
                                        " editable="true" required="required" 
                                    >
									</select>
                                </td>
                            </tr>
                            
                            <tr>
                                <td><b>Currency</b></td>
                                <td width="850" style="padding:1px 0px 2px;">
                                    <select name="cmb_currency" id="cmb_currency" 
                                    	class="combo-grid" 
                                        style="width:852px;" readonly
                                        data-options="
                                            value:'<?php echo $row->CurrencyCode; ?>', 
                                            idField:'CurrencyCode', 
                                            textField:'CurrencyName', 
                                            url:'<?php echo base_url(); ?>custom/get_data/currency',
                                            method: 'get',
                                            columns: [[
                                                {field:'CurrencyCode',title:'Currency Code',width:400,halign:'center',align:'left'},
                                                {field:'CurrencyName',title:'Currency Name',width:430,halign:'center',align:'left'}
                                            ]],
                                        " editable="true" required="true" 
                                    >
                                    </select>
                                </td>
                            </tr>
                            <tr>
                                <td><b>Default Description</b></td>
                                <td width="850" style="padding:1px 0px 2px;">
                                    <input type="text" id="" name="txt_termsdefaultdesctiption" value="<?php echo $row->VendorDesc;?>" style="width:850px;" />
                                </td>
                            </tr>
                            </table>
                        </fieldset>
                        <fieldset>
                        	<legend>Taxes</legend>
                            <table width="100%" cellpadding="0" cellspacing="0">
                            <tr>
                                <td><b>Tax 1</b></td>
                                <td width="850" style="padding:1px 0px 2px;">
                                	<select name="cmb_tax1" id="cmb_tax1" 
                                    	class="combo-grid"
                                    	style="width:822px;" 
										data-options="
                                        	value:'<?php echo $row->Tax1Code; ?>',
											idField:'TaxesCode', 
                                            textField:'TaxesName', 
											url:'<?php echo base_url(); ?>custom/get_data/taxes',
                                            method: 'get',
                                            columns: [[
                                                {field:'TaxesCode',title:'Taxes Code',width:370,halign:'center',align:'left'},
                                                {field:'TaxesName',title:'Taxes Name',width:430,halign:'center',align:'left'}
                                            ]],
										" editable="true" 
                                    >
									</select>
									<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#cmb_tax1').combobox('clear')"></a>
                                </td>
                            </tr>
                            <tr>
                                <td><b>Tax 2</b></td>
                                <td width="850" style="padding:1px 0px 2px;">
                                	<select name="cmb_tax2" id="cmb_tax2" on 
                                    	class="combo-grid"
                                   		style="width:822px;" 
										data-options="
                                        	value:'<?php echo $row->Tax2Code; ?>',
											idField:'TaxesCode',textField:'TaxesName',
											url:'<?php echo base_url(); ?>custom/get_data/taxes',
                                            method: 'get',
                                            columns: [[
                                                {field:'TaxesCode',title:'Taxes Code',width:370,halign:'center',align:'left'},
                                                {field:'TaxesName',title:'Taxes Name',width:430,halign:'center',align:'left'}
                                            ]],
										" editable="true" 
                                    >
									</select>
									<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#cmb_tax2').combobox('clear')"></a>
                                </td>
                            </tr>
                            <!--<tr>
                                <td></td>
                                <td width="850" style="padding:1px 0px 2px;">
                                    <input type="checkbox" name="chk_termsdefaultinvoice" value="1" style="margin-left:0px;" <?php echo ($row->TaxIncludedStatus == 1)? "checked" : "";?> /> Default Invoice is Tax Included 
                                </td>
                            </tr>-->
                            <tr>
                                <td><b>Out Standing</b></td>
                                <td width="850" style="padding:1px 0px 2px;">
                                    <input type="text" class="easyui-numberbox" name="txt_vendor_balance" id="txt_vendor_balance" style="width:822px;"  value="<?php echo $row->OutStanding;?>" readonly data-options="precision:2,decimalSeparator:',',groupSeparator:'.'"/>
                                	<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-add" onclick="modal_open('Select Vendor Balance','dialog-vendor-balance')"></a>
                                	<input type="hidden" class="easyui-numberbox" id="txt_vendor_balance_hidden" name="txt_vendor_balance_hidden" value="<?php echo $row->OutStanding;?>" />
                                </td>
                            </tr>
                            <?php if($row->CurrencyCode != "IDR"){ ?>
                            <tr>
                                <td><b>Out Standing Fiscal</b></td>
                                <td width="840" style="padding:1px 0px 2px;">
                                	<input type="text" class="easyui-numberbox" name="txt_out_fiscal" id="txt_out_fiscal" value="<?php echo $row->OutStandingFiscal;?>"  style="width:842px;text-align:left;" readonly autocomplete="off" data-options="precision:2,decimalSeparator:',',groupSeparator:'.'" />
                                   
                                </td>
                            </tr>
                            <?php } ?>
                            <tr>
                                <td><b>Down Payment</b></td>
                               
                                <td width="840" style="padding:1px 0px 2px;">
                                    <input type="text" id="txt_outstanding" style="width:822px;" class="easyui-numberbox" formatter="formatRp" name="txt_outstanding" data-options="precision:2,decimalSeparator:',',groupSeparator:'.'" value="<?php echo $row->SaldoDPBalance;?>" onkeyup="duplicate_outstanding()" readonly="readonly"/>
                                	<input type="hidden" id="txt_outstanding_hidden" name="txt_outstanding_hidden" value="<?php echo $row->SaldoDPBalance;?>" />
                                    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-add" onclick="modal_open('Select Down Payment','dialog-dp-balance')"></a>
                                </td>
                            </tr>
                            
                            <tr>
                                <td><b>Down Payment Partial</b></td>
                                <td width="840" style="padding:1px 0px 2px;">
                                    <input type="text" id="txt_dp_partial" style="width:852px;" class="easyui-numberbox" formatter="formatRp" name="txt_dp_partial" data-options="precision:2,decimalSeparator:',',groupSeparator:'.'" value="<?php echo $row->SaldoDPPartial;?>" onkeyup="duplicate_outstanding()" readonly="readonly"/>
                                	<input type="hidden" id="txt_dp_partial_hidden" name="txt_dp_partial_hidden" value="<?php echo $row->SaldoDPPartial;?>" />
                                </td>
                            </tr>
                             
                            <tr>
                                <td><b>Vendor Type</b></td>
                                <td style="padding:1px 0px 2px;">
                                    <?php if($row->VendorStatus == 0){ ?>
                                        <input name="rd_vendor_type" type="Radio" value="0" checked required="true"/>Badan
                                        <input name="rd_vendor_type" type="Radio" value="1" required="true"/>Perseorangan
                                        
                                     <?php }else if($row->VendorStatus == 1){ ?>
                                        <input name="rd_vendor_type" type="Radio" value="0"  required="true"/>Badan
                                        <input name="rd_vendor_type" type="Radio" value="1" checked required="true"/>Perseorangan
                                        
                                     <?php } else{?>
									 	<input name="rd_vendor_type" type="Radio" value="0"  required="true"/>Badan
                                        <input name="rd_vendor_type" type="Radio" value="1" required="true"/>Perseorangan
									 <?php }?>
                                    <!-- <input type="button" id="btn_unit" name="btn_unit" value="..." /> -->
                                </td>
                        </tr>
                            <tr>
                                <td><b>Vendor Tax No / NPWP</b></td>
                                <td width="850" style="padding:1px 0px 2px;">
                                    <input type="text" name="txt_termsvendortaxno" value="<?php echo $row->VendorTaxNumber;?>" style="width:852px;" />
                                </td>
                            </tr>
                            <tr>
                                <td><b>PKP No.</b></td>
                                <td width="850" style="padding:1px 0px 2px;">
                                    <input type="text" name="txt_termspkpno" value="<?php echo $row->PKPNumber;?>" style="width:852px;" />
                                </td>
                            </tr>
                            <tr>
                                <td><b>Tax Type</b></td>
                                <td width="850" style="padding:1px 0px 2px;">
                                	<select id="cmb_taxtype" name="cmb_taxtype"
                                    	class="combo-grid" 
                                        style="width:822px" 
										data-options="
                                        	value:'<?php echo $row->TaxTypeID; ?>',
											idField:'TaxTypeID',
											textField:'TaxTypeName',
											url:'<?php echo base_url(); ?>custom/get_data/taxtype',
                                            method: 'get',
                                            columns: [[
                                                {field:'TaxTypeID',title:'Taxes Type ID',width:370,halign:'center',align:'left'},
                                                {field:'TaxTypeName',title:'Taxes Type Name',width:430,halign:'center',align:'left'}
                                            ]],
										" editable="true" 
                                    >
									</select>
									<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#cmb_taxtype').combobox('clear')"></a>
                                </td>
                            </tr>
                            </table>
                        </fieldset>
                    </div>
                   <div id="tab_notes" title="Notes">
                        <textarea rows="18" name="txa_notes" style="width:calc(100% - 16px);margin:5px;resize:none;" ><?php echo $row->VendorNotes;?></textarea>
                    </div>
                </div>
                
                <!-- Dialog SI -->
            	<div id="dialog-vendor-balance" class="easyui-dialog" style="width:575px;height:523px;top:27px;background-color:#F8F8F8;padding:0px;" closed="true" data-options="modal:true" buttons="#dialog-buttons-vendor"> 
                	<table toolbar="#tb_dg_item" id="dg_purchase_invoice" class="easyui-edatagrid"  
                        singleSelect="true" fitColumns="true" 
                        pageSize="20" idField="id"
                        remoteSort="false" rowNumbers="true" 
                        fit="true"
                    >
                    	<thead>
                        	<tr>
                            	<th field="OldPInvNumber" width="50" editor="{type:'text',options:{required:true}}" sortable="true">No Purchase Invoice</th>
                            	<th field="OutStanding" width="50" editor="{type:'numberbox',options:{precision:2,decimalSeparator:',',groupSeparator:'.',min:1,required:true,disabled:false}}" formatter="formatRp" sortable="true">Balance</th>
                            	<?php if($row->CurrencyCode != "IDR"){ ?>
                                	<th field="Rate" width="50" editor="{type:'numberbox',options:{precision:2,decimalSeparator:',',groupSeparator:'.',min:1,required:true,disabled:false}}" formatter="formatRp" sortable="true">Exchange Rate</th>
                            	<?php } ?>	
                            	<th field="PInvDate" width="50" editor="{type:'datebox',options:{required:true,editable:false}}" sortable="true">Date</th>
                        	</tr>
                    	</thead>
                    </table>
        	    </div>
        
            <div id="tb_dg_item">
                <a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="new_invoice()">New</a>
                <a href="#" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="remove_row()">Delete</a>
            </div>
            	
            <div id="dialog-buttons-vendor" style="border:none;">
                <a href="javascript:void(0)" class="easyui-linkbutton save-button" iconCls="icon-ok" onClick="choose_vendor_balance();" id="id_save">Ok</a>
                <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onClick="close_popup()">Cancel</a>
            </div>

            <div id="dialog-dp-balance" class="easyui-dialog" style="width:575px;height:523px;top:27px;background-color:#F8F8F8;padding:0px;" closed="true" data-options="modal:true" buttons="#dialog-buttons-dp"> 
                    <table toolbar="#tb_dg_dp" id="dg_dp" class="easyui-edatagrid"  
                        singleSelect="true" fitColumns="true" 
                        pageSize="20" idField="id"
                        remoteSort="false" rowNumbers="true" 
                        fit="true" 
                    >
                        <thead>
                            <tr>
                                <th field="NoTransaksi" width="50" editor="{type:'NoTransaksi',options:{required:true}}">No Transaction</th>
                                <th field="Rate" width="50" editor="{type:'numberbox',options:{precision:2,decimalSeparator:',',groupSeparator:'.',min:0.01,required:true,disabled:false}}" formatter="formatRp">Rate</th>
                                <th field="DownPayment" width="50" editor="{type:'numberbox',options:{precision:2,decimalSeparator:',',groupSeparator:'.',min:0,required:true,disabled:false}}" formatter="formatRp">Down Payment</th>
                            </tr>
                        </thead>
                    </table>
                </div>
                <div id="tb_dg_dp">
                    <a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="addNewRowDP()">New</a>
                    <a href="#" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="remove_row_dp()">Delete</a>
                </div>
                <div id="dialog-buttons-dp" style="border:none;">
                    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-ok" onClick="choose_dp_balance();">Ok</a>
                    <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onClick="close_popup_dp()">Cancel</a>
                </div>
            </form>
    	</div>
    </div>
	
			
    <div id="form-footer" data-options="region:'south',border:false">
			<table cellpadding="0" cellspacing="0" width="100%" height="100%" border="0">
				 <tr>
				   <td align="left">
						
				   </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="update_data()" <?php echo $cek_autority_save;?>>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="parent.tab_close('tt')">Cancel</a>
					</td>
				 </tr>
			</table>				
		</div>
</div>

<script type="text/javascript">
var SavePrivillage 	= '<?php echo $group_user->SavePrivillage;?>';
var newIndex = -1;
$(document).ready(function(){
	$('#dialog-vendor-balance').click(function(){
		$('#dg_purchase_invoice').edatagrid('saveRow');
	});
	
	$('#cmb_city').combogrid({
            onSelect:function(rowIndex, rowData){
                $('#cmb_state').combogrid('setValue', rowData.StateCode);
            }
        });

        $('#cmb_state').combogrid({
            onSelect:function(rowIndex, rowData){
                $('#cmb_country').combogrid('setValue', rowData.CountryCode);
            }
        });
		
		$('#dg_purchase_invoice').edatagrid({
			onBeginEdit:function(rowIndex){
				/*====================on beg in edit saat grid di klik maka tombol save disable===================*/
				$('.save-button').linkbutton('disable');
				/*=================================end============================================================*/
			},
			onEndEdit: function(){
				/*====================on beg in edit saat grid di klik maka tombol save disable===================*/
				$('.save-button').linkbutton('enable');
				/*=================================end============================================================*/
			}
		});

    get_master_rate('<?=$row->CurrencyCode;?>');

    $('#dialog-dp-balance').click(function(){
        var item        = $('#dg_dp').datagrid('getRows');
        var TotalItem   = item.length + 1;

        for(var x=0;x<TotalItem;x++){
            $('#dg_dp').datagrid('endEdit', x);
        }
         $('#dg_dp').edatagrid('saveRow');
    });
});

function addNewRowDP(){
    if(newIndex >= 0){
        $('#dg_dp').datagrid('endEdit', newIndex);
        $('#dg_dp').edatagrid('saveRow');
    }
	
	var rate            = $('#txt_rate').numberbox('getValue');
    var cmb_currency    = $('#cmb_currency').combobox('getValue');
    var dg              = $('#dg_dp');
    var item            = dg.datagrid('getColumnOption','Rate');

    newIndex            = item.length;

    if(cmb_currency == 'IDR'){
       item.editor.options.disabled     = true;
    }
	
	var row 				= $('#dg_dp').datagrid('getRows');
	$('#dg_dp').datagrid('endEdit', (row.length - 1));
		
	$('#dg_dp').edatagrid('addRow',{
		//index: 1,
		row: {
			NoTransaksi     : '',
			Rate            : rate,
			DownPayment     : 0
		}
	});
	
	$('#dg_dp').datagrid('beginEdit', (row.length - 1));
}


function new_invoice(){
	$('#dg_purchase_invoice').edatagrid('addRow');
}

function duplicate_outstanding(){
	var limit_out 			= $('#txt_outstanding').val();
	var limitToAngka_out	= toAngka(limit_out);
	$('#txt_outstanding_hidden').val(limitToAngka_out);
    //$('#txt_outstanding').numberbox('setValue',limitToAngka_out);
}

function close_popup_dp(){
    $('#dg_dp').datagrid('reload');
    modal_close('dialog-dp-balance')
}

function close_popup(){
	
	modal_close('dialog-vendor-balance');
	
	var item = $('#dg_purchase_invoice').datagrid('getData').total;
	
	if(item == 0){
		$('#dg_purchase_invoice').datagrid('reload');
		location.reload();
	}
	$('.save-button').linkbutton('enable');
}

function getkey(e){
	if (window.event)
	   return window.event.keyCode;
	else if (e)
	   return e.which;
	else
	   return null;
}

function goodchars(e, goods, field){
	var key, keychar;
	key = getkey(e);
	if (key == null) return true;
	
	keychar = String.fromCharCode(key);
	keychar = keychar.toLowerCase();
	goods 	= goods.toLowerCase();
	
	// check goodkeys
	if (goods.indexOf(keychar) != -1)
		return true;
	// control keys
	if ( key==null || key==0 || key==8 || key==9 || key==27 )
	   return true;
	   
	if (key == 13) {
		var i;
		for (i = 0; i < field.form.elements.length; i++)
			if (field == field.form.elements[i])
				break;
		i = (i + 1) % field.form.elements.length;
		field.form.elements[i].focus();
		return false;
	};
	// else return false
	return false;
}

function choose_dp_balance(){
    var item        = $('#dg_dp').datagrid('getRows');
    var TotalItem   = item.length + 1;
    var TotalDP     = 0;

    for(var x=0;x<TotalItem;x++){
        $('#dg_dp').datagrid('endEdit', x);
    }

    $.each(item,function(ind,val){
        TotalDP += parseFloat(val.DownPayment);
    });

    $('#txt_outstanding').numberbox('setValue',TotalDP);
    $('#dg_dp').edatagrid('saveRow');

    modal_close('dialog-dp-balance');
}

function choose_vendor_balance(){
	$('#dg_purchase_invoice').edatagrid('saveRow');
	var item 		= $('#dg_purchase_invoice').datagrid('getRows');
	var sub_total   = 0;
	var tot_amount 	= 0;
	var rate 		= 0;
	var tot_rate 	= 0;
	var val_rate 	= 0;
	var tot_rate_outstanding 	= 0;
	var txt_aoutstanding 		= $('#txt_vendor_balance_hidden').numberbox('getValue');
	var cmb_currency 			= $('#cmb_currency').val();
	var keseluruhan 			= 0;
	
	if(cmb_currency == 'IDR'){
		$.each(item, function( index, value ) {
			if(value.OutStanding != ""){
				sub_total+= parseFloat(value.OutStanding);
				$('#id_klik').val('1');
			}	
		});
		tot_amount = parseFloat(txt_aoutstanding) + parseFloat(sub_total);
		$('#txt_vendor_balance').numberbox('setValue',tot_amount);
	}
	else{
		$.each(item, function( index, value ) {
			if(value.OutStanding != ""){
				rate = parseFloat(value.OutStanding) * parseInt(value.Rate);
				sub_total+= parseFloat(value.OutStanding);
				tot_rate+= parseFloat(rate);
				val_rate+= parseFloat(value.OutStanding);
				$('#id_klik').val('1');
			}
		});
		
		outstanding_customer 	 = parseFloat(txt_aoutstanding) + parseFloat(val_rate);
		tot_rate_outstanding 	= parseFloat(tot_rate) / parseFloat(val_rate);
				
		$('#txt_vendor_balance').numberbox('setValue',outstanding_customer);
		
		keseluruhan = parseFloat(tot_amount) * parseFloat(tot_rate_outstanding);
		$('#konversi').numberbox('setValue',keseluruhan);
		$('#rate_data').numberbox('setValue',tot_rate);
	}
	
	//$.post('',{SInvNumber:value.SInvNUmber,SInvDate:value.SInvDate,SInvTotalAmount:CustBalance});
	modal_close('dialog-vendor-balance');
}

function update_data(){
	if($('#txt_vendorname').val() == ""){
		 $.messager.alert('Warning','Vendor Name tidak boleh kosong, silahkan lengkap','warning',function(){
		});		
	}
	else if($('#txt_address').val() == ""){
		 $.messager.alert('Warning','Vendor Addres tidak boleh kosong, silahkan lengkap','warning',function(){
		});	
	}
	else if($('#cmb_city').combogrid('getValue') == ""){
		 $.messager.alert('Warning','City/Prov/Zip Code tidak boleh kosong, silahkan lengkap','warning',function(){
		});	
	}
	else if($('#txt_phone').val() == ""){
		 $.messager.alert('Warning','Phone tidak boleh kosong, silahkan lengkap','warning',function(){
		});	
	}
	else if($('#txt_contactperson').val() == ""){
		 $.messager.alert('Warning','Contact Person tidak boleh kosong, silahkan lengkap','warning',function(){
		});	
	}
	else if($('#cmb_terms').combogrid('getValue') == ""){
		 $.messager.alert('Warning','Terms tidak boleh kosong, silahkan lengkap','warning',function(){
		});	
	}
	else if($('#cmb_currency').combogrid('getValue') == ""){
		 $.messager.alert('Warning','Currency tidak boleh kosong, silahkan lengkap','warning',function(){
		});	
	}
	else{
		$('#dg_purchase_invoice').edatagrid('saveRow');
		var i=$('#dg_purchase_invoice').datagrid('getRows');
		var detail = JSON.stringify(i);
		$("#detail").val(detail);
        var dp=$('#dg_dp').datagrid('getRows');
        var DownPayment = JSON.stringify(dp);
        $("#detail_dp").val(DownPayment);
		edit_data('<?php echo $menu_link?>','#fm_vendor');
	}
}

function remove_row(){
	var selectedrow     = $('#dg_purchase_invoice').datagrid('getSelected');
	var row_index       = $('#dg_purchase_invoice').datagrid('getRowIndex', selectedrow);
	
	$.messager.confirm('Confirmation :', 'Anda yakin, Anda ingin menghapus item "' + selectedrow.OldPInvNumber + '"',  
		function(r){
			if (r){
				/*====================on beg in edit saat grid di klik maka tombol save disable===================*/
				$('.save-button').linkbutton('enable');
				/*=================================end============================================================*/
				
				$('#dg_purchase_invoice').datagrid('deleteRow', row_index);
			}
		}
	);        
						
		
}

$.extend($.fn.datagrid.defaults.editors, {
	NoTransaksi: {
        init: function(container, options){
            var input = $('<input type="text" class="NoTransaksi" 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);
        }
    }
});
</script>
