Runner.buttonEvents['2592'] = function( pageObj, proxy, pageid ) {
	var gid = '2592';
	//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;
			// ======================================================
// CLIENT BEFORE - CREATE PO FROM PR
// ======================================================

var refNo = row.getFieldValue("reference_number");
var existingPoId = row.getFieldValue("existing_po_id");
var poRef = row.getFieldValue("po_reference_number");

params["pr_reference_number"] = refNo;

if (existingPoId && existingPoId !== "" && existingPoId !== null) {
    swal(
        "Already Created",
        "This Purchase Request already has a Purchase Order.\nExisting PO: " + poRef,
        "warning"
    );
    return false;
}

if (!refNo) {
    swal("Error", "Unable to get Purchase Request reference number from this row.", "error");
    return false;
}

return confirm("Create Purchase Order from PR " + refNo + "?");			
		}
	}

	if ( !pageObj.buttonEventAfter[ gid ] ) {
		pageObj.buttonEventAfter[ gid ] = function( result, ctrl, pageObj, proxy, pageid, rowData, row, params ) {
			var ajax = ctrl;
			// ======================================================
// CLIENT AFTER - CREATE PO FROM PR
// ======================================================

if (!result || !result.success) {
    swal(
        "Error",
        result ? result.message : "No response from server. Please check the Server event code.",
        "error"
    );
    return;
}

swal({
    title: "Created",
    text:
        result.message +
        "\nPR No: " + result.pr_reference_number +
        "\nPO No: " + result.po_reference_number,
    icon: "success"
}).then(function () {
    window.location.href = "purchaseorder_edit.php?editid1=" + result.po_id;
});
		}
	}

	$('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 ]} );
	});

};

