	var PAYMENT_DGV = null;
	var REC_POSPAY_CAT_PAYTYPE_ID_CMB = null;

	// ===============================================================
	// Initialize Controls
	// ===============================================================     
	function FORM_INITIALIZE_PAYMENT()
	{
		REC_POSPAY_CAT_PAYTYPE_ID_CMB = JSOC_ELEMENT.VAR_GET("REC_POSPAY_CAT_PAYTYPE_ID_CMB"); 
		REC_POSPAY_CAT_PAYTYPE_ID_CMB.onchange = function()
		{
			var PAYMENT_TYPE = JSVC_NUM.VAR_NVALL
				(
					JSOC_CMB.VAR_SELECTED_VALUE(REC_POSPAY_CAT_PAYTYPE_ID_CMB)
				);

			// Hide All
			JSOC_STYLE.VAR_DISPLAY("REC_POSPAY_CAT_BANK_ID_BLOCK", "NONE");
			JSOC_STYLE.VAR_DISPLAY("REC_POSPAY_CAT_CARDTYPE_ID_BLOCK", "NONE");
			JSOC_STYLE.VAR_DISPLAY("REC_POSPAY_PAYEE_BLOCK", "NONE");
			JSOC_STYLE.VAR_DISPLAY("REC_POSPAY_NUMBER_BLOCK", "NONE");
			JSOC_STYLE.VAR_DISPLAY("REC_POSPAY_CHECK_DATE_BLOCK", "NONE");
			JSOC_STYLE.VAR_DISPLAY("REC_POSPAY_REMARKS_BLOCK", "NONE");
			
			JSOC_STYLE.VAR_DISPLAY("CAT_PAYTERM_ID_BLOCK", "NONE");
			JSOC_STYLE.VAR_DISPLAY("REC_POSSALE_DATE_DUE_BLOCK", "NONE");
			JSOC_STYLE.VAR_DISPLAY("LOYALTY_VOUCHER_CODE_BLOCK", "NONE");
			
			// If Credit card payment
			if(PAYMENT_TYPE == 2)
			{
				// Show Input Blocks
				JSOC_STYLE.VAR_DISPLAY("REC_POSPAY_CAT_BANK_ID_BLOCK", "DEFAULT");
				JSOC_STYLE.VAR_DISPLAY("REC_POSPAY_CAT_CARDTYPE_ID_BLOCK", "DEFAULT");
				JSOC_STYLE.VAR_DISPLAY("REC_POSPAY_PAYEE_BLOCK", "DEFAULT");
				JSOC_STYLE.VAR_DISPLAY("REC_POSPAY_NUMBER_BLOCK", "DEFAULT");
				JSOC_STYLE.VAR_DISPLAY("REC_POSPAY_REMARKS_BLOCK", "DEFAULT");
				
				// Update Labels
				JSOC_ELEMENT.VAR_VALUE("REC_POSPAY_PAYEE_LBL", "Name on card:");
				JSOC_ELEMENT.VAR_VALUE("REC_POSPAY_NUMBER_LBL", "Card Number (LAST 4 DIGITS):");
				JSOC_ELEMENT.VAR_VALUE("REC_POSPAY_REMARKS_LBL", "Reference:");
			}
			
			// If Check Payment
			else if(PAYMENT_TYPE == 2)
			{
				// Show Input Blocks
				JSOC_STYLE.VAR_DISPLAY("REC_POSPAY_CAT_BANK_ID_BLOCK", "DEFAULT");
				JSOC_STYLE.VAR_DISPLAY("REC_POSPAY_PAYEE_BLOCK", "DEFAULT");
				JSOC_STYLE.VAR_DISPLAY("REC_POSPAY_NUMBER_BLOCK", "DEFAULT");
				JSOC_STYLE.VAR_DISPLAY("REC_POSPAY_CHECK_DATE_BLOCK", "DEFAULT");
				JSOC_STYLE.VAR_DISPLAY("REC_POSPAY_REMARKS_BLOCK", "DEFAULT");
				
				// Update Labels
				JSOC_ELEMENT.VAR_VALUE("REC_POSPAY_PAYEE_LBL", "Name on card:");
				JSOC_ELEMENT.VAR_VALUE("REC_POSPAY_NUMBER_LBL", "Check Number:");
				JSOC_ELEMENT.VAR_VALUE("REC_POSPAY_CHECK_DATE_LBL", "Check Date:");
			}
			
			// If Bank Transfer
			
			
		};
	};


	function PAYMENT_QUICK_CASH(AMOUNT_TO_ADD)
	{
		PAYMENT_DGV.CELL.VALUE
		(
			'REC_POSPAY_AMOUNT_PAID', 0,
			JSVC_NUM.VAR_NVAL
			(
				PAYMENT_DGV.CELL.VALUE('REC_POSPAY_AMOUNT_PAID', 0)
			) + AMOUNT_TO_ADD
		);
	};
	
	
	function PAYMENT_SHOW()
	{
		if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("POS_PAYMENT_MODAL") == true)
		{
			if(JSVC_VAR.VAR_ISSET(PAYMENT_DGV) == false)
			{
				PAYMENT_DGV = DataGridView("PAYMENT_DGV");
			}			
			PAYMENT_CALCULATE(true);

			JSOC_DIALOG.VAR_MODAL("POS_BAGS_MODAL", false);
			JSOC_DIALOG.VAR_MODAL("POS_PAYMENT_MODAL", true);
			JSOC_ELEMENT.VAR_GET("POS_PAYMENT_MODAL").style.top = '30%';
			
			return;
		}		
	};
	
	
	function PAYMENT_METHOD_SHOW(REC_POSPAY_CAT_PAYTYPE_ID)
	{
		JSOC_DIALOG.VAR_MODAL("POS_PAYMENT_DETAIL_MODAL", true);
		
		JSOC_CMB.VAR_SELECTED_VALUE(REC_POSPAY_CAT_PAYTYPE_ID_CMB, REC_POSPAY_CAT_PAYTYPE_ID);
		REC_POSPAY_CAT_PAYTYPE_ID_CMB.onchange();
	};
	

	// ===============================================================
	// Calculate Change
	// ===============================================================

	function PAYMENT_CALCULATE(CLEAR_MODAL = false)
	{
		var TOTAL_PAID = JSVC_NUM.VAR_NVAL(PAYMENT_DGV.COLUMN.SUM('REC_POSPAY_AMOUNT_PAID'));
		var TOTAL_DUE = 
			JSVC_NUM.VAR_NVAL(CART_DGV.COLUMN.SUM("AMOUNT")) - 
			JSVC_NUM.VAR_NVAL(CART_DGV.COLUMN.SUM("DISCOUNT_VALUE"));
		var AMOUNT_CHANGE = TOTAL_PAID - TOTAL_DUE;

		if(CLEAR_MODAL == true)
		{
			PAYMENT_DGV.ROW.CLEAR();
			PAYMENT_DGV.ROW.ADD();
			PAYMENT_DGV.CELL.VALUE('REC_POSPAY_CAT_PAYTYPE_ID', 0, 1);
			PAYMENT_DGV.CELL.VALUE('CAT_PAYTYPE_DESCRIPTION', 0, "CASH");
			PAYMENT_DGV.CELL.VALUE('REC_POSPAY_AMOUNT_PAID', 0, JSVC_NUM.VAR_CFORMAT(TOTAL_DUE));			
		}

		// JSOC_ELEMENT.VAR_VALUE('CHECKOUT_CHANGE_VALUE', CHANGE);
		JSOC_ELEMENT.VAR_VALUE('CHECKOUT_TOTAL', JSVC_NUM.VAR_CFORMAT(TOTAL_DUE));
		JSOC_ELEMENT.VAR_VALUE('CHECKOUT_CHANGE', JSVC_NUM.VAR_CFORMAT(AMOUNT_CHANGE));
	};
	
	
	
	FORM_INITIALIZE_PAYMENT();