Runner.buttonEvents['398814'] = function( pageObj, proxy, pageid ) {
	var gid = '398814';
	//register a new button
	pageObj.buttonNames[ pageObj.buttonNames.length ] = gid;
		
	if ( !pageObj.buttonEventBefore[ gid ] ) {
		pageObj.buttonEventBefore[ gid ] = function( params, ctrl, pageObj, proxy, pageid, rowData, row, submit ) {		
			var ajax = ctrl;
			var qaId = "";

// If button is inside row/grid
if (typeof row !== "undefined" && row) {
    qaId = row.getFieldValue("qa_incoming_goods_id") || row.getFieldValue("id");
}

// If using selected record checkbox
if (!qaId && params["keys"] && params["keys"].length) {
    qaId = params["keys"][0];
}

if (!qaId) {
    swal("No record selected", "Please select one QA item first.", "warning");
    return false;
}

params["qa_incoming_goods_id"] = qaId;

return true;			
		}
	}

	if ( !pageObj.buttonEventAfter[ gid ] ) {
		pageObj.buttonEventAfter[ gid ] = function( result, ctrl, pageObj, proxy, pageid, rowData, row, params ) {
			var ajax = ctrl;
			if (!result) {
    swal("Error", "No response from server. Please check PHP error log.", "error");
    return;
}

if (result.success) {
    swal({
        title: "Created",
        text: result.message,
        icon: "success",
        button: "OK"
    }).then(function () {
        window.location.href = "replacement_request_edit.php?editid1=" + result.replacement_request_id;
    });
} else {
    swal("Unable to create", result.message, "warning");
}
		}
	}

	$('a[data-event=' + gid + ']').each( function() {
		if ( $(this).closest('.gridRowAdd').length ) {
			return;
		}
		var rowId = Runner.genId();
		var eventId = gid + "_" + rowId;
		$(this).attr( 'data-event', eventId );
		this.id = this.id + "_" + Runner.genId();
		var buttonObj = new Runner.form.Button({
			id: eventId,
			btnName: gid
		});
		buttonObj.init( {args: [ pageObj, proxy, pageid ]} );
	});

};

