$(document).ready(function(){
    page_width      = parseInt($(document).width());
    left_menu_width = 180 // Value from v_mainpage.php(custom/views/v_mainpage.php)
    mainframe_width = page_width - left_menu_width;
	
    box_left_pos    = 0;

    $('.status-box').each(function(){
        temp = mainframe_width/2;
        temp = temp - $(this).width();
        box_left_pos = left_menu_width + temp;
    });

    $('.status-box').css('left',box_left_pos);
	
    /*Datebox setValue to today*/
    $('.valToday').each(function(){
        datebox_id = '#'+ $(this).attr('id');
        $(datebox_id).datebox('setValue','today');
    });
    /*End of Datebox setValue to today*/
	
    /*Datebox validation: Backward*/
    $('.validBackward').datebox({
        onSelect:function(){
            today       = new Date();
            today       = Date.parse(today.getMonth()+1+'/'+today.getDate()+'/'+today.getFullYear());

            datebox_value = $(this).datebox('getValue').split('-');
            datebox_value = datebox_value[1] +'-'+ datebox_value[0] +'-'+ datebox_value[2];
            datebox_value = Date.parse(datebox_value);

            if(datebox_value < today){
                $.messager.alert('Perhatian','Tidak bisa memilih tanggal dibawah tanggal saat ini');
                datebox_id = $(this).attr('id');
                $('#'+ datebox_id).datebox('setValue','today');
            }
        }
    });
    /*End of Datebox Validation: Backward*/
	
    $('#cmb_customer').combogrid({
        onChange: function(){
            item = $(this).combogrid('grid').datagrid('getSelected');			
        }
    });
	
	$('form').on('change keyup paste', ':input', function(){
		$('.app-button').linkbutton('disable');
	});
	
//	$('.easyui-edatagrid :input').bind('change keyup paste', function(){
//		alert();
//	});
});

/* Author : Herpan / Last update : 23/7/2014 / for new coa */
function form_coa(){
    $('#chk_sub_of').click(function(){
        if($(this).is(':checked')){
            $('#cmb_sub_account_wrapper').show();
        }	
        else{
            $('#cmb_sub_account_wrapper').hide(); 
            $('#cmb_account_parent').combogrid('clear');
    
            $.post(base_url + 'list/coa/ajax_account_number',{AccountParent:''},function(result){			   
                if (result.status == true){
                    $('#txt_account_no').val(result.AccountNumber); // reload the user data
                }				
            },'json');
        }
    });	
	
    $('#cmb_account_parent').combogrid({
        onSelect:function(rowIndex, rowData){		      
            $.post(base_url + 'list/coa/ajax_account_number',{AccountParent:rowData.AccountNumber},function(result){
                if (result.status == true){
                    $('#txt_account_no').val(result.AccountNumber); 
                }				
           },'json');
        }		
    });
}

/* Author  : Herpan / Last update : 04/08/2014 / for import journal */
function form_importjurnal(){
    if($('#rdo_source:checked').val() == "local"){
        $('#fil_databaselocation').show();
        $('#txt_databaselocation').hide();
        $('#servername').hide();
    }
    else{
        $('#fil_databaselocation').hide();
        $('#txt_databaselocation').show();
        $('#servername').show();
    }
}

/* Author   : Herpan / Last update : 06/08/2014 / Desc : for new company */
function new_company(){
    location.href='file/new_company/quick_setup';
}

/*
Author  : Tagor / Last update : 18/08/2014 / Desc : for popup roll over */
$(function(){
    $('#tt_rollover').datagrid('appendRow',{});
    $('#tt_toaccount').datagrid('appendRow',{});
    $('#cmb_itemcategory').prop('disabled', true);

    //$('#wrappaccount').hide();

    $('#rdo_toaccount').click(function(){
        //$('#wrappaccount').removeClass('hideWrapDiv');
        $('#wrappaccount').show();
        //$('#wrappitem').removeClass('showWrapDiv');
        $('#wrappitem').hide();

        $('#tt_rollover').datagrid('clear');
    });
	
    $('#rdo_toitem').click(function(){
        //$('#wrappaccount').removeClass('showWrapDiv');
        $('#wrappaccount').hide();
        //$('#wrappitem').removeClass('hideWrapDiv');
        $('#wrappitem').show();

        $('#tt_toaccount').datagrid('clear');
    });	
});

/* Author : Tagor / Tanggal : 18/08/2014 / for popup update password */
function update_password(){
    if($('#txt_oldpassword').val() == '' || $('#txt_newpassword').val() == '' || $('#txt_confirmation').val() == ''){
        $.messager.alert('Info', 'Please fill all field');
        return false;
    }
    else if($('#txt_oldpassword').val() != $('#old_pwd').val()){
        $.messager.alert('Info', 'Please enter right old password');
        return false;
    }
    else if($('#txt_newpassword').val() != $('#txt_confirmation').val()){
        $.messager.alert('Info', 'Password and confirmation must match');
        return false;
    }
    else{
        $.messager.confirm('Confirm','Redirect login', function(r){
            if (r){
                $('form#fm_changepassword').submit();
            }
        });
    }
}

/*
Author 	: Wahyu/ Last update   : 18/08/2014
Desc    : for format tampilan rupiah datagrid
*/
//function formatRp(a,b,c,d,e){
//    if(a == undefined){
//        return 0;
//    }
//    else{
//        a = parseFloat(a).toFixed(2);
//        temp = a.split('.');
//        
//        if(temp.length > 1){
//            decimal = temp[1];
//        }
//        else{
//            decimal = '00';
//        }
//		
//        e = function(f){
//            return f.split('').reverse().join('');
//        };
//		
//        b = e(parseInt(a).toString());
//		
//        for(c=0, d=''; c<b.length; c++){
//            d += b[c];
//            
//            if((c + 1) % 3 === 0 && c !== (b.length - 1)){
//                d += '.';
//            }
//        }
//	
//        return '\t'+e(d) +','+ decimal;
//    }
//}


/* Author : Wahyu/ Tanggal : 18/08/2014 / for format tampilan rupiah datagrid */
function formatRp(a){
    if(!a){
        return 0;
    }
    else{
        a = parseFloat(a).toFixed(2);
        temp = a.split('.');

        if(temp.length > 1){
            decimal = temp[1];
        }
        else{
            decimal = '00';
        }
		
        e = function(f){
            return f.split('').reverse().join('')
        };
		
        b = e(parseInt(a).toString());
		
        for(c=0,d='';c<b.length;c++){
            d += b[c];
            
            if((c + 1) % 3 === 0 && c !== (b.length - 1)){
                d += '.';
            }
        }
		
        return '\t'+ e(d) +','+ decimal;
    }
}

function formatRp_bank(a,b,c,d,e){
    if(a == undefined){
        return 0;
    }
    else{
        a = parseFloat(a).toFixed(2);
        temp = a.split('.');
        
        if(temp.length > 1){
            decimal = temp[1];
        }
        else{
            decimal = '00';
        }
		
        e = function(f){
            return f.split('').reverse().join('');
        };
		
        b = e(parseInt(a).toString());
		
        for(c=0, d=''; c<b.length; c++){
            d += b[c];
            
            if((c + 1) % 3 === 0 && c !== (b.length - 1) && !isNaN(b[c+1])){
                d += '.';
            }
        }
	
        var nominal = e(d);
        
        if(nominal.indexOf('-') === 0){
            nominal = nominal.substring(1, nominal.length);
            nominal = '(' + nominal  + ',' + decimal + ')';
        }
        else {
             nominal = nominal + ',' + decimal;
        }
        
        return nominal;
//        return '\t'+e(d) +','+ decimal;
    }
}

function formatDates(dates){
    date = dates.split(",");
    
    for(i=0;i<date.length;i++){
        temp	= date[i].split("-");
        date[i] = temp[2] +"-"+ temp[1] +"-"+ temp[0];
    }
    return date.join();
}

function beginEditRow(value, row, index){ 
    if(row.editing){
        input = '<a href="javascript:void(0)" onClick="saverow(this)">Save</a> ';
        input += '<a href="javascript:void(0)" onClick="cancelrow(this)">Cancel</a> ';
    }else{
        input = '<a href="javascript:void(0)" onClick="editrow(this)">Edit</a>';
    }
	
    return input;
}

function getRowIndex(target){
    var tr = $(target).closest('tr.datagrid-row');
    return parseInt(tr.attr('datagrid-row-index'));
}

function editrow(target,value){
    $('#dg_item').datagrid('beginEdit', getRowIndex(target));
}

function saverow(target){
    $('#dg_item').datagrid('endEdit', getRowIndex(target));
}

function cancelrow(target){
	$('#dg_item').datagrid('cancelEdit', getRowIndex(target));
}

function toAngka(rp){
   return parseInt(rp.replace(/,.*|\D/g,''),10);
}

function onDblClickCellRollOver(index,field,value){
    $('#tt_rollover').datagrid({
        url: 'custom/ajax/m_roll_over'
    });

    if(value == null){
        modal_open('Item','dlg_searchitem');
    }
    else{
        $(this).datagrid('beginEdit',index);
        var ed = $(this).datagrid('getEditor',{index:index,field:field});
        //$(ed.target).focus();
    }
}

function filterInItemPopup(){
    if($('#cmb_itemsearchby').val() !== ''){
        $('#cmb_itemcategory').prop('disabled', false);
    }
    else{
        $('#cmb_itemcategory').prop('disabled', true);
        $('#cmb_itemcategory').val(' ');
    }
}

function doItemSearch(){
    $('#dg_item').datagrid('load',{
        itemno : $('#txt_filteritemno').val(),
        itemdesc : $('#txt_filteritemdesc').val()
    });
}

function onDblClickRowItem(rowIndex,rowData){
    var ids = [];
    var id;
    var rows = $('#dg_item').datagrid('getSelections');
    
    for(var i=0; i<rows.length; i++){
        ids.push(rows[i].ItemId);
    }

    id = ids.toString();
    
    $('#tt_rollover').datagrid('load',{
        itemid : id
    });

    modal_close('dlg_searchitem');
}

function onDblClickCellToAccount(){
	//behh
}

/* Author : Herpan / 23/7/2014 / for set now all datebox */
function formatDate(date){
    format_date = /^\d{4}\-\d{1,2}\-\d{1,2}$/;
	
    if(!format_date.test(date)){
        temp = date.split('-');
        var y = parseInt(temp[0],10);
        var m = parseInt(temp[1],10);
        var d = parseInt(temp[2],10);
        date = new Date(d,m-1,y);
    }
	
    if(isNaN(date)){
        date = new Date(date);
    }
	
    var y = date.getFullYear();
    var m = date.getMonth()+1;
    var d = date.getDate();

    //return y+'-'+(m<10?('0'+m):m)+'-'+(d<10?('0'+d):d);
    return (d<10?('0'+d):d) +'-'+ (m<10?('0'+m):m) +'-'+ y;
}

/*function formatDates(dates){
    date = dates.split(",");
    
    for(i=0;i<date.length;i++){
        temp	= date[i].split("-");
        date[i] = temp[2] +"-"+ temp[1] +"-"+ temp[0];
    }
    return date.join();
}*/

$.fn.datebox.defaults.formatter = function(date){
    if(isNaN(date)){
        date = new Date(date);
    }

    var y = date.getFullYear();
    var m = date.getMonth()+1;
    var d = date.getDate();

    //return y+'-'+(m<10?('0'+m):m)+'-'+(d<10?('0'+d):d);
    return (d<10?('0'+d):d) +'-'+ (m<10?('0'+m):m) +'-'+ y;
};

$.fn.datebox.defaults.parser = function(s){
    if (!s) return new Date();

    format_date = /^\d{4}\-\d{1,2}\-\d{1,2}$/;

    var ss = (s.split('-'));

    if(!format_date.test(s)){
        var y = parseInt(ss[0],10);
        var m = parseInt(ss[1],10);
        var d = parseInt(ss[2],10);
    }
    else{
        var y = parseInt(ss[2],10);
        var m = parseInt(ss[1],10);
        var d = parseInt(ss[0],10);
    }
	
    if (!isNaN(y) && !isNaN(m) && !isNaN(d)){
        //return new Date(y,m-1,d);
        return new Date(d,m-1,y);
    } 
    else {
        return new Date();
    }
};

/* Author  : Herpan / Last update : 23/7/2014 / fungsi untuk pop up item */
function open_item(){ 
    modal_open('Item','dialog-item');
}

/*  Added By   : Hasan / Added Date : 9 December 2014 */
function open_item2(){
    modal_open('Item','dialog-item');

    table_row = $("tr[id^='datagrid-row-r11-2-']").length;

    if(table_row > 1){
        first_row       = $("#datagrid-row-r11-2-0 td[field=ItemCategoryID] div");
        ItemCategoryID	= first_row.text();

        $("#hdn_item_category").val(ItemCategoryID);
        $("#cmb_item_category").combobox("select", ItemCategoryID);
        $('#cmb_item_category').combobox('readonly');
    }
}
/*End of Added*/

function choose_item() {
    var selected_data = $('#tt_choose_item').datagrid('getSelected');
	
    if($('input[name="rdo_ItemToggle"]:checked').val() == 1){
    	$('.id').val(selected_data.ItemCategoryID);
    }

    $('.item').val(selected_data.ItemCode);
    $('.description').val(selected_data.ItemDesc);
    $('.unit').val(selected_data.ItemUnit);
    $('.price').val(selected_data.ItemPrice);
    $('.warehouse_id').val(selected_data.ItemWHTypeID);
    $('.item_type').val($('input[name="rdo_ItemToggle"]:checked').val());
    $('.qty').val(selected_data.NetDueIn);

    modal_close('dialog-item');
}
 
$.extend($.fn.datagrid.defaults.editors, {
    item: {
        init: function(container, options){
            var input = $('<input type="text" class="item" onClick="open_item()" 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);
        }
    },
    
    item2: {
        init: function(container, options){
            var input = $('<input type="text" class="item" onClick="open_item2()" 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);
        }
    },
            
    item_type: {
        init: function(container, options){
            var input = $('<input type="easyui-numberbox" class="item_type" 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);
        }
    },
            
    item_production: {
        init: function(container, options){
            var input = $('<input type="text" class="item_production" onClick="open_production()" 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);
        }
    },
      
	item_production_amount: {
        init: function(container, options){
            var input = $('<input type="text" class="item_production_amount" onClick="open_production_amount()" 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);
        }
    },
	        
    item_job_costing: {
        init: function(container, options){
            var input = $('<input type="text" class="item_job_costing" onClick="open_job_costing()" 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);
        }
    },
            
    item_inventory: {
        init: function(container, options){
            var input = $('<input type="text" class="item_inventory" onClick="open_inventory()" 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);
        }
    },
            
    item_category: {
        init: function(container, options){
            var input = $('<input type="text" class="item_category" 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);
        }
    },
	
    description: {
        init: function(container, options){
            var input = $('<input type="text" class="description" style="width:350px;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);
        }
    },
            
    unit: {
        init: function(container, options){
            var input = $('<input type="text" class="unit" style="width:82px;height:18px;text-align:center;" 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);
        }
    },
            
    price: {
        init: function(container, options){
            var input = $('<input type="text" class="price" style="height:18px;" onChange="get_amount(this.value)" />').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);
        }
    },

    price2: {
        init: function(container, options){
            var input = $('<input type="text" class="price" style="height:18px;" onChange="get_amount2(this.value)" />').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);
        }
    },
            
    qty: {
        init: function(container, options){
            var input = $('<input type="text" class="qty" onChange="get_amount(this.value)" 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);
        }
    },
            
    amount: {
        init: function(container, options){
            var input = $('<input type="text" class="amount" 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);
        }
    },
            
    memo: {
        init: function(container, options){
            var input = $('<input type="text" class="memo" 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);
        }
    },
            
    max_item: {
        init: function(container, options){
            var input = $('<input type="text" class="max_item" 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);
        }
    },
            
    warehouse_id: {
        init: function(container, options){
            var input = $('<input type="text" class="warehouse_id" 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);
        }
    },
            
    item_return: {
        init: function(container, options){
            var input = $('<input type="text" class="item_return" onClick="open_item_return()" 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);
        }
    },
            
    itemhpp: {
        init: function(container, options){
            var input = $('<input type="text" class="itemhpp" 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);
        }
    },
            
    itemkategori: {
        init: function(container, options){
            var input = $('<input type="text" class="itemkategori" 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);
        }
    },
            
    qty_return: {
        init: function(container, options){
            var input = $('<input type="text" class="easyui-numberbox qty_return" />').appendTo(container);
            return input.numberbox(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);
        }
    },
            
    price_return: {
        init: function(container, options){
            var input = $('<input type="text" class="easyui-numberbox price_return" style="width:200px;" />').appendTo(container);
            return input.numberbox(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);
        }
    },
            
    amount_return: {
        init: function(container, options){
            var input = $('<input type="text" class="easyui-numberbox amount_return" style="width:200px" />').appendTo(container);
            return input.numberbox(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);
        }
    },
            
    id_field: {
        init: function(container, options){
            var input = $('<input type="text" class="easyui-validatebox id" 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);
        }
    }
});

function get_amount(value) {
    var temp_amount = $('.amount').val();	
    var sub_total = $('#sub_total').val();
    var max_item = $('.max_item').val();

    var qty = $('.qty').val();
    var price = $('.price').val();
			
	// if(value <= 0) {
		// $('.qty').val(0);
		// qty = 0;
		// alert("Qty harus lebih besar dari 0");
	// }
	
	// if(value > parseInt(max_item)) {
		// $('.qty').val(0);
		// qty = 0;
		// alert("Qty tidak boleh lebih dari (" + parseInt(max_item) + ")");
	// }
	
    var amount = parseInt(qty) * parseInt(price);
	
    if(isNaN(amount)) {
        $('.amount').val(0);
        $('.qty').val(0);
        $('#sub_total').val(0);
    }
    else if(isNaN(price)) {
        $('.amount').val(0);
        $('.qty').val(0);
        $('#sub_total').val(0);
    }
    else {
        $('.amount').val(amount);
        if(temp_amount != "") {
            sub_total = sub_total - temp_amount;
        }
		
        sub_total =  parseInt(sub_total) + parseInt(amount);	
        $('#sub_total').val(sub_total);
    }
	
    $('#dg_item').edatagrid('saveRow');
}

function get_amount2(value){
    sub_total	= $('#sub_total').val();
    qty         = $('.qty').val();
    price	= $('.price').val();
    // max_item	= $('.max_item').val();
	
    if((qty != 0 || qty != null) || (price != 0 || price != null)){
        amount= parseInt(qty) * parseInt(price);
		
        if(!isNaN(amount)){
            sub_total = parseInt(sub_total) + amount;
            $('.amount').val(amount);
            $('#sub_total').val(sub_total);
        }
    }
}

/* Author : Herpan / Last update   : 23/7/2014 / fungsi untuk pop up expense group */

function open_expense_group() { 
   modal_open('Select Expense','dialog-expense');
}

function choose_expense() {	
    modal_close('dialog-expense');
}

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

/* Author : Ladur / 08/10/2014 / fungsi untuk pop up inventory */

function open_inventory(){ 
   modal_open('Inventory','dialog-inventory');
}

function choose_inventory() {
    var selected_data = $('#tt_choose_inventory').datagrid('getSelected');
    
    $('.item_inventory').val(selected_data.ItemCode);
    $('.description').val(selected_data.ItemDesc);
    $('.unit').val(selected_data.ItemUnit);
    $('.price').val(selected_data.ItemPrice);
    $('.max_item').val(selected_data.ItemQty - selected_data.ItemBooks);
    $('.item_category').val(selected_data.ItemCategoryID);
    
    modal_close('dialog-inventory');
}

/* Author : Fachri / fungsi untuk pop up job cost, daily production , daily production amount */
function open_production() { 
   modal_open('Item','dialog-production');
}

function open_production_amount() { 
   modal_open('Item','dialog-production-amount');
}

function open_job_costing() { 
   modal_open('Item','dialog-job-costing');
}

function choose_production() {
    var selected_data=$('#tt_choose_production').datagrid('getSelected');	
	
    flag = true;
    item = $('#dg_item').datagrid('getRows');

    $.each(item, function(index,value){
        if(selected_data.ItemCode == value.ItemCode){
            $.messager.alert('Unseccessfull','Item sudah tersedia.','error',function(){	
                $('#tt_choose_production').datagrid('reload'); 	
            });
            
            flag = false;
        }
    });
	
    if(flag != false){
        $('.item_production').val(selected_data.ItemCode);
        $('.description').val(selected_data.ItemDesc);
        $('.unit').val(selected_data.ItemUnit);
        $('.warehouse_id').val(selected_data.ItemWHTypeID);
        modal_close('dialog-production');
    }
}

function choose_production_amount() {
    var selected_data=$('#tt_choose_production_amount').datagrid('getSelected');	
	
    flag = true;
    item = $('#dg_item').datagrid('getRows');

    $.each(item, function(index,value){
        if(selected_data.ItemCode == value.ItemCode){
            $.messager.alert('Unseccessfull','Item sudah tersedia.','error',function(){	
                $('#tt_choose_production_amount').datagrid('reload'); 	
            });
            
            flag = false;
        }
    });
	
    if(flag != false){
        $('.item_production_amount').val(selected_data.ItemCode);
        $('.description').val(selected_data.ItemDesc);
        $('.unit').val(selected_data.ItemUnit);
        $('.warehouse_id').val(selected_data.ItemWHTypeID);
        modal_close('dialog-production-amount');
    }
}


function choose_job_costing() {
    var selected_data=$('#tt_choose_job_costing').datagrid('getSelected');	
	
    flag = true;
    item = $('#dg_job').datagrid('getRows');

    $.each(item, function(index,value){
        if(selected_data.ItemCode == value.ItemCode){
            $.messager.alert('Unseccessfull','Item sudah tersedia.','error',function(){	
                $('#tt_choose_job_costing').datagrid('reload'); 	
            });
   
            flag = false;
        }
    });
	
    if(flag != false){
        $('.item_job_costing').val(selected_data.ItemCode);
        $('.description').val(selected_data.ItemDesc);
        $('.unit').val(selected_data.ItemUnit);
        $('.warehouse_id').val(selected_data.WHTypeName);
        $('.itemhpp').val(selected_data.ItemHPP);
        $('.itemkategori').val(selected_data.ItemCategoryID);
        $('.qty').val(selected_data.ItemQty);

        modal_close('dialog-job-costing');
    }
}

/* end */

/* Author : Tagor / Tanggal : 05/11/2014 / fungsi untuk pop up account payment < 250rb */

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

function choose_account_payment() {
    var selected_data = $('#tt_choose_account_payment').datagrid('getSelected');

    $('.PaymentAccountName').val(selected_data.AccountName);
    modal_close('dialog-payment-account');
}

function open_payment_department() { 
   modal_open('Department List','dialog-payment-department');
}

function choose_department_payment() {
    var selected_data = $('#tt_choose_department_payment').datagrid('getSelected');

    $('.DepartmentAccountName').val(selected_data.DepartmentName);
    $('.DepartmentAccountCode').val(selected_data.DepartmentCode);

    modal_close('dialog-payment-department');
}
 
$.extend($.fn.datagrid.defaults.editors, {
    PaymentAccountName: {
        init: function(container, options){
            var input = $('<input type="text" class="PaymentAccountName" onClick="open_payment_account()" 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);
        }
    },
            
    DepartmentAccountName: {
        init: function(container, options){
            var input = $('<input type="text" class="DepartmentAccountName" onClick="open_payment_department()" 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);
        }
    },
            
    DepartmentAccountCode: {
        init: function(container, options){
            var input = $('<input type="text" class="DepartmentAccountCode" onClick="open_payment_department()" 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);
        }
    }
});
//**===== End Tagor ====

function convert_to_dmy(tanggal) {
    var day     = tanggal.substr(8, 2);
    var month   = tanggal.substr(5, 2);	
    var year    = tanggal.substr(0, 4);

    var date    = day + '-' + month + '-' + year;

    return date;
}

 function convert_to_ymd(tanggal) {
    var year    = tanggal.substr(6, 4);
    var month   = tanggal.substr(3, 2);	
    var day     = tanggal.substr(0, 2);

    var date    = year + '-' + month + '-' + day;

    return date;
}

/* Created by Hasan on 2015/02/28 for Refresh Datagrid in List Page. */
function refresh_datagrid(id){
    $('input').each(function(){
        $(this).val('');
		
        if(!$(this).is(':checked')){
            if(!$(this).attr('id')=='chk_ByDate'){
                $(this).prop('checked',true);
            }
        }
        else{
            if($(this).attr('id')=='chk_ByDate'){
                $(this).prop('checked',false);
                $('.filter_date').datebox('disable',true);
            }
        }
    });
	
    $('[class^="cmb_"]').each(function(){
        $(this).combobox('unselect');
    });
	
    $(id).datagrid('reload',{});
}