Runner.buttonEvents['404542'] = function( pageObj, proxy, pageid ) {
	var gid = '404542';
	//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 motherFacilityId = parseInt(
    row.getFieldValue("mother_facility_id") || row.getFieldValue("id"),
    10
) || 0;

var motherFacilityCode = row.getFieldValue("facility_code") || "";

if (motherFacilityId <= 0 && !motherFacilityCode) {
    swal(
        "Error",
        "Cannot get the mother facility ID or facility code from this row.",
        "error"
    );
    return false;
}

params["mother_facility_id"] = motherFacilityId;
params["mother_facility_code"] = motherFacilityCode;

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 the PHP error log.",
        "error"
    );
    return;
}

if (result.success) {

    var runId = parseInt(result.run_id, 10) || 0;

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

    swal({
        title: "Facility 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 || "The facility forecast could not be generated.",
        "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 ]} );
	});

};

