Runner.buttonEvents['398808'] = function( pageObj, proxy, pageid ) {
	var gid = '398808';
	//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 woId = row.getFieldValue("production_work_order_id");

if (!woId) {
    swal("Error", "Cannot get Production Work Order ID from this row.", "error");
    return false;
}

params["production_work_order_id"] = woId;

return true;			
		}
	}

	if ( !pageObj.buttonEventAfter[ gid ] ) {
		pageObj.buttonEventAfter[ gid ] = function( result, ctrl, pageObj, proxy, pageid, rowData, row, params ) {
			var ajax = ctrl;
			console.log("Create RISP result:", result);

if (!result) {
    swal("Error", "No response from server. Please check PHP error log.", "error");
    return;
}

if (result.success) {

    if (!result.raw_material_request_id) {
        swal("Error", "RISP was created but no ID was returned.", "error");
        return;
    }

    swal({
        title: "Created",
        text: result.message,
        icon: "success",
        button: "OK"
    }).then(function () {
        window.location.href = "raw_material_request_edit.php?editid1=" + result.raw_material_request_id;
    });

} else {
    swal("Unable to create", result.message || "Unknown error occurred.", "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 ]} );
	});

};

