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

if (!warehouseId) {
    warehouseId = row.getFieldValue("id");
}

if (!warehouseId) {
    swal("Error", "Cannot get warehouse ID from this row.", "error");
    return false;
}

params["warehouse_id"] = warehouseId;

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) {

    var runId = result.run_id;

    if (!runId || parseInt(runId, 10) <= 0) {
        swal("Error", "Forecast generated but run ID was not returned.", "error");
        return;
    }

    swal({
        title: "Forecast Generated",
        text: result.message,
        icon: "success",
        button: "OK"
    }).then(function () {

        window.location.href =
            "production_suggestion_detail_list.php" +
            "?mastertable=" + encodeURIComponent("production_suggestion_run") +
            "&masterkey1=" + encodeURIComponent(runId);

    });

} else {
    swal("Unable to Generate", 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 ]} );
	});

};

