function clear_data() {
    $('#fm').form('clear');
}

function save_data(url,form_id,clear) {
    url = base_url + url;

    $(form_id).form('submit',{
        url: url + '/insert',
        onSubmit: function(){
            return $(this).form('validate');
        },
        success: function(result){
            var result = eval('('+result+')');
            
            if (result.status==true){
                $.messager.alert('Success',result.message,'info',function(){					
                    location.reload();							
                });	

                if(clear==1){
                    $(form_id).form('clear');
                }
            } 
            else {
                $.messager.show({
                    title: 'Unseccessfull',
                    msg: result.message,
                    style:{
                        right:'',
                        bottom:''
                    }
                });
            } 
        },

        statusCode: {
            404: function() {
              alert("Page Not Found");
            }		
        }
    });
}

function save_transaction(url,form_id) {
    url = base_url + url;
	
    $(form_id).form('submit',{
        url: url +'/insert',
        onSubmit: function(){
            return $(this).form('validate');
        },
        success: function(result){
            var result = eval('('+result+')');
	
            if (result.status==true){
                $.messager.alert('Success',result.message,'info',function(){					
                    location.reload();							
                });				
            } 
            else {
                $.messager.show({
                    title: 'Unseccessfull',
                    msg: result.message,
                    style:{
                        right:'',
                        bottom:''
                    }
                });
            } 
        },
        statusCode: {
            404: function() {
                alert("Page Not Found");
            }
        }
    });
}

function save_preferen(url,form_id) {
    url = base_url + url;
	
    $(form_id).form('submit',{
        url: url +'/insert',
        onSubmit: function(){
            return $(this).form('validate');
        },
        success: function(result){
            var result = eval('('+result+')');
	
            if (result.status==true){
                $.messager.alert('Success',result.message,'info',function(){					
                    location.reload();							
                });				
            } 
            else {
                $.messager.show({
                    title: 'Unseccessfull',
                    msg: result.message,
                    style:{
                        right:'',
                        bottom:''
                    }
                });
            } 
        },
        statusCode: {
            404: function() {
                alert("Page Not Found");
            }
        }
    });
}

function edit_data(url,form_id) {
    url = base_url + url;	
    
    $(form_id).form('submit',{
        url: url + '/update',
        onSubmit: function(){
            return $(this).form('validate');
        },
        success: function(result){
            var result = eval('('+result+')');
            
            if (result.status==true){
                $.messager.alert('Success',result.message,'info',function(){						
                    location.reload();							
                });
            } 
            else {
				$.messager.alert('Unseccessfull',result.message,'info');
            }
        },		
        
        statusCode: {
            404: function() {
                alert("Page Not Found");
            }
        }
    });
}

function delete_grid_job(url, id) {
    url = base_url + url;
    $.messager.confirm('Confirm','Are you sure you want to remove this data ?', 

    function(r){
        if (r){
            $.post(url + '/delete_grid',{id:id},function(result){
                if (result.status==true){
                    $.messager.show({ // show error message
                        title: 'Succesfull',
                        msg: result.message,
                        style:{
                            right:'',
                            bottom:''
                        }
                    });
                    $('#dg_job').datagrid('reload'); // reload the user data
                } 
                else {
                    $.messager.show({ // show error message
                        title: 'Error',
                        msg: result.message,
                        style:{
                            right:'',
                            bottom:''
                        }
                    });
                }
            },'json');
        }
    });
}

function delete_data(url, id) {
    url = base_url + url;
    $.messager.confirm('Confirm','Are you sure you want to remove this data ?', 

    function(r){
        if (r){
            $.post(url + '/delete',{id:id},function(result){
                if (result.status==true){
                    $.messager.alert('Success',result.message,'info',function(){						
						location.reload();							
					});
                } 
                else {
                    $.messager.alert('Error',result.message,'error',function(){						
						//location.reload();							
					});
                }
            },'json');
        }
    });
}

function import_data(url,form_id,dialog_id) {
    url = base_url + url;
    $(form_id).form('submit',{
        url: url + '/import',
        onSubmit: function(){
            return $(this).form('validate');
        },
        success: function(result){
            var result = eval('('+result+')');
            if (result.status==true){
                $.messager.show({
                    title: 'Succesfull',
                    msg: result.message,
                    style:{
                        right:'',
                        bottom:''
                    }
                });					
            } 
            else {
                $.messager.show({
                    title: 'Unseccessfull',
                    msg: result.message,
                    style:{
                        right:'',
                        bottom:''
                    }
                });
            }
            modal_close(dialog_id);					
            $('#tt').datagrid('reload'); // reload the user data
        }
    });
}
	
function import_transaksi(url,form_id,dialog_id) {
	url = base_url + url;
	
		$(form_id).form('submit',{
			url: url + '/insert',
			onSubmit: function(){
				return $(this).form('validate');
			},
			success: function(result){
			var result = eval('('+result+')');
				if (result.status==true){
					$.messager.show({
						title: 'Succesfull',
						msg: result.message,
						style:{
								right:'',
								bottom:''
						}
					});					
					
				} 
				else {
					$.messager.show({
						title: 'Unseccessfull',
						msg: result.message,
						style:{
								right:'',
								bottom:''
						}
					});
				}
				modal_close(dialog_id);					
				$('#tt').datagrid('reload'); // reload the user data
			}
		});
	}
/* End Of Javascript */

function check_data(url,form_id) {
    url = base_url + url;	

    $(form_id).form('submit',{
        url: url,
        onSubmit: function(){
            return $(this).form('validate');
        },
        success: function(result){
            var result = eval('('+result+')');
	
    
            },		
            statusCode: {
                        404: function() {
                          alert("Page Not Found");
                        }
                        }
    });
}

function custom_data(url,form_id) {
    url = base_url + url;	

    $(form_id).form('submit',{
        url: url,
        onSubmit: function(){
            return $(this).form('validate');
        },
        success: function(result){
            var result = eval('('+result+')');
	
            if (result.status==true){
                $.messager.alert('Berhasil',result.message,'info',function(){						
                    location.reload();							
                });	
            } 
            else {
                $.messager.alert('Berhasil',result.message,'info',function(){						
                    					
                });
                }
            },		
            statusCode: {
                        404: function() {
                          alert("Page Not Found");
                        }
                        }
    });
}

function custom_data(url,form_id) {
    url = base_url + url;	

    $(form_id).form('submit',{
        url: url,
        onSubmit: function(){
            return $(this).form('validate');
        },
        success: function(result){
            var result = eval('('+result+')');
	
            if (result.status==true){
                $.messager.alert('Berhasil',result.message,'info',function(){						
                    location.reload();							
                });	
            } 
            else {
                $.messager.alert('Unseccessfull',result.message,'info',function(){						
                    					
                });
                }
            },		
            statusCode: {
                        404: function() {
                          alert("Page Not Found");
                        }
                        }
    });
}
/* Wahyu Payment Karyawan */
function save_payment(url,form_id,clear,idgrid) {
	url = base_url + url;
	$(form_id).form('submit',{
		url: url + '/insert',
		onSubmit: function(){
			return $(this).form('validate');
		},
		success: function(result){
		    var result = eval('('+result+')');
			if (result.status==true){
				$.messager.show({
					title: 'Succesfull',
					msg: result.message,
					style:{
							right:'',
							bottom:''
					}
				});
				
				if(clear==1){
					$(form_id).form('clear');
				}
				
				if(idgrid){
					$(idgrid).datagrid('reload');
					location:reload();
				}
				
				$('#print_button').linkbutton('enable');
				$('#save_button').linkbutton('disable');
			} 
			else {
				$.messager.show({
					title: 'Unseccessfull',
					msg: result.message,
					style:{
							right:'',
							bottom:''
					}
				});
				
				$('#print_button').linkbutton('disable');
				$('#save_button').linkbutton('enable');
			} 
		},
		statusCode: {
			404: function() {
			alert("Page Not Found");
			}		
		}
	});
}

function search_data_department_no(){
    $('#txt_find_dep_no').keyup(function() { 
        $("#search_no_department").submit();
    });
}

function search_data_department_name(){
    $('#txt_find_department_name').keyup(function() { 
        $("#search_department_name").submit();
    });
}

function SearchData(){
    $('#txt_journal_voucher_no').keyup(function() { 
        $('#tt').datagrid('reload');
    });
}

function suspend_data(url, datagrid_id, id) {
    url = base_url + url;
    
    $.messager.confirm('Confirm','Anda Yakin Akan Membatalkan Transaksi ?', 
	function(r){
            if (r){
                $.post(url + '/suspend',{id:id},function(result){
                    if (result.status==true){
                        $.messager.alert('Berhasil',result.message,'info',function(){						
                            $(datagrid_id).datagrid('reload'); 								
                        });
                    } 
                    else {
                        $.messager.alert('Maaf',result.message,'error',function(){						
                            $(datagrid_id).datagrid('reload'); 								
                        });
                    }
                },'json');
            }
    });
}

/*Added by Hasan on 12/02/2015*/
function suspend_data_new(location, type, url, id, action){
    $.messager.confirm('Confirm','Anda Yakin Akan Membatalkan Transaksi ?',function(r){
        if(r){
            url = base_url + url;
	
            if(type==1){ // If type is 1=> Default Suspend
                trans_status = 3;
            }
            else if(type==2){ // If type is 2=> Cancel by Dir.
                trans_status = 9;
            }
			
            $.post(url + '/suspend_data',{id:id,trans_status:trans_status},function(result){
                if (result.status==true){
                    $.messager.alert('Berhasil',result.message,'info',function(){						
                        if(location=='L'){ // If Suspend is triggered in Form
                            $(action).datagrid('reload');
                        }
                        else if(location=='F'){ // If Suspend is triggered in List
                            parent.tab_close('tt');
                        }
                    });
                } 
                else {
                    $.messager.alert('Maaf',result.message,'error');
                }
            },'json');
        }
        else{
            $('#chk_suspended').prop('checked',false);
        }
    });
}
/*End of added by Hasan on 12/02/2015*/

function suspend_data_on_new_form(url, id/*, trans_status*/) {
    url = base_url + url;
    
    /*if(trans_status==3)
        confirm_message = "Anda Yakin Akan Mengaktifkan Kembali Transaksi ini?";
    else
    confirm_message = "Anda Yakin Akan Membatalkan Transaksi ini?";*/
	
    $.messager.confirm('Confirm','Anda Yakin Akan Mengaktifkan Kembali Transaksi ?'/*confirm_message*/, 
	function(r){
            if (r){
                $.post(url + '/suspend',{id:id},function(result){
                    if (result.status==true){
                        $.messager.show({ // show sucess message
                                title: 'Success',
                                msg: result.message,
                                style:{
                                    right:'',
                                    bottom:''
                                }
                            });
                        } 
                        else {
                            $.messager.show({ // show error message
                                    title: 'Error',
                                    msg: result.message,
                                    style:{
                                        right:'',
                                        bottom:''
                                    }
                            });
                        }
                    },'json');
            }
            else{
                if($("#chk_status").prop("checked")){
                    $("#chk_status").prop("checked",false);
                }
                else{
                    $("#chk_status").prop("checked",true);
                }
            }
        });
}

/* Added by	: Hasan / Added Date	: - */
function suspend_data_on_new_form2(url, id, trans_status) {
    url = base_url + url;
	
    if(trans_status == 3){
        confirm_message = "Anda Yakin Akan Mengaktifkan Kembali Transaksi ini?";
    }
    else{
        confirm_message = "Anda Yakin Akan Membatalkan Transaksi ini?";
    }
	
    $.messager.confirm("Confirm", confirm_message, function(r){
        if(r){
            $.post(url + '/suspend',{id:id},function(result){
                if (result.status == true){
                    $.messager.show({ // show sucess message
                        title: 'Success',
                        msg: result.message,
                        style:{
                            right:'',
                            bottom:''
                        }
                    });
                } 
                else {
                    $.messager.show({ // show error message
                        title: 'Error',
                        msg: result.message,
                        style:{
                            right:'',
                            bottom:''
                        }
                    });
                }
            },'json');
        }
        else{
            if($("#chk_status").prop("checked")){
                $("#chk_status").prop("checked",false);
            }
            else{
                $("#chk_status").prop("checked",true);
            }
        }
    });
}
