// NOTE: [This JS must loaded as last item]


	// ===============================================================
	// Initialize Form
	// ===============================================================
	function FORM_LOAD()
	{
		// Initialize
		// _______________________________________________________________________		
		
		PRODUCT_BRANCH_LV = LayoutView("PRODUCT_BRANCH_LV");

		CART_DGV = DataGridView("CART_DGV");
		DISCOUNT_DGV = DataGridView("DISCOUNT_DGV");
		CUSTOMERS_DGV = DataGridView("CUSTOMERS_DGV");
		PRICE_MODEL_DGV = DataGridView("PRICE_MODEL_DGV");

		POS_SALE_DGV = DataGridView("POS_SALE_DGV");

		JOB_ORDER_DGV = DataGridView("JOB_ORDER_DGV");

		PRODUCT_BRANCH_LV.ON_CLICK
		(			
			function(COL_INDEX, ROW_INDEX)
			{
				if(PROPERTY_REC_TRANSACTION_REFNO == '')
				{
					// If dialog supported
					// ...........................................
					if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("POS_QTY_MODAL") == true)
					{
						// Clear Modal
						if(JSVC_VAR.VAR_ISSET(INPUT_INSTANCE))
						{
							JSOC_WINDOW.VAR_INPUT_INSTANCE_RESTORE(INPUT_INSTANCE);
						}
							
						// Display Modal
						PROPERTY_REC_PRODUCT_ID = PRODUCT_BRANCH_LV.CELL.VALUE('CAT_PRODUCT_ID', ROW_INDEX);
						PROPERTY_EDIT_ROW_INDEX = ROW_INDEX;

						JSOC_ELEMENT.VAR_GET("ADD_ITEM_BBI").classList.remove("d-none");
						JSOC_ELEMENT.VAR_GET("CHANGE_QTY_BBI").classList.add("d-none");
						
						JSOC_DIALOG.VAR_MODAL("POS_QTY_MODAL", true);
						JSOC_ELEMENT.VAR_GET("POS_QTY_MODAL").style.top = '20%';
						return;
					}
				}
				else
				{
					JSOC_DIALOG.VAR_MESSAGE_BOX
					(
						'Adding item is not allowed',
						'Information',
						'OK', 'ERROR',
						function(){}
					);
				}
			}
		); 

		CART_DGV.ON_CLICK
		(			
			function(COL_INDEX, ROW_INDEX)
			{
				//check if row clicked has highlight already
				if(CART_DGV.ROW.ELEMENT(ROW_INDEX).classList.contains("table-warning") == true)
				{
					//remove highlight current row
					CART_DGV.ROW.ELEMENT(ROW_INDEX).classList.remove('table-warning');

					PROPERTY_REC_PRODUCT_ID = -1;
					PROPERTY_REC_PRODUCT_QTY = -1;
					PROPERTY_CART_ROW_INDEX = -1;
				}
				else
				{
					//remove all highlights
					for(let i = 0; CART_DGV.ROW.COUNT() > i; i++)
					{
						CART_DGV.ROW.ELEMENT(i).classList.remove('table-warning');
					}

					//highlight current row
					CART_DGV.ROW.ELEMENT(ROW_INDEX).classList.add('table-warning');

					PROPERTY_REC_PRODUCT_ID = JSVC_NUM.VAR_NVALL(CART_DGV.CELL.VALUE('PRODUCT_ID', ROW_INDEX));
					PROPERTY_REC_PRODUCT_QTY = JSVC_NUM.VAR_NVAL(CART_DGV.CELL.VALUE('QTY', ROW_INDEX));
					PROPERTY_CART_ROW_INDEX = ROW_INDEX;
				}

				// Void/Remove Item
		 		// ...........................................
				if(CART_DGV.COLUMN.NAME(COL_INDEX) == "REMOVE_ITEM_CMD")
				{
					if(PROPERTY_REC_TRANSACTION_REFNO == '')
					{
						alertify.confirm
						(
							'PeakPOS', 
							'Are you sure you want to remove ' + CART_DGV.CELL.VALUE("QTY", ROW_INDEX) + ' ' + CART_DGV.CELL.VALUE("UOM_CODE", ROW_INDEX) + ' of ' + CART_DGV.CELL.VALUE("PRODUCT_DESCRIPTION", ROW_INDEX) + '?',
							function()
							{
								HTTP_DELETE
								(
									function(IS_SUCCESS, RESPONSE)
									{
										if(IS_SUCCESS)
										{
											alertify.error(RESPONSE + ' removed', 'success');
										}
									}, 
									ROW_INDEX
								);	
							},
							function(){}
						).set('labels', {ok:'YES', cancel:'NO'});
					}
					else
					{
						JSOC_DIALOG.VAR_MESSAGE_BOX
						(
							'Removing item is not allowed',
							'Information',
							'OK', 'ERROR',
							function(){}
						);
					}
				}
			}
		); 

		DISCOUNT_DGV.ON_CLICK
		(
			function(COL_INDEX, ROW_INDEX)
			{
				//check if row clicked has highlight already
				if(DISCOUNT_DGV.ROW.ELEMENT(ROW_INDEX).classList.contains("table-warning") == true)
				{
					//remove highlight current row
					DISCOUNT_DGV.ROW.ELEMENT(ROW_INDEX).classList.remove("table-warning");

					PROPERTY_REC_DISCOUNT_INDEX = -1;
					PROPERTY_REC_DISCOUNT_TYPE = -1;
					PROPERTY_DISCOUNT_ROW_INDEX = -1;
				}
				else
				{
					//remove all highlights
					for(let i = 0; DISCOUNT_DGV.ROW.COUNT() > i; i++)
					{
						DISCOUNT_DGV.ROW.ELEMENT(i).classList.remove('table-warning');
					}

					//highlight current row
					DISCOUNT_DGV.ROW.ELEMENT(ROW_INDEX).classList.add("table-warning");

					PROPERTY_REC_DISCOUNT_INDEX = JSVC_NUM.VAR_NVALL(DISCOUNT_DGV.CELL.VALUE('CAT_DISCOUNT_ID', ROW_INDEX));
					PROPERTY_REC_DISCOUNT_TYPE = JSVC_NUM.VAR_NVALL(DISCOUNT_DGV.CELL.VALUE('TYPE', ROW_INDEX));
					PROPERTY_DISCOUNT_ROW_INDEX = ROW_INDEX+1;
				}
			}
		);

		CUSTOMERS_DGV.ON_CLICK
		(
			function(COL_INDEX, ROW_INDEX)
			{
				//check if row clicked has highlight already
				if(CUSTOMERS_DGV.ROW.ELEMENT(ROW_INDEX).classList.contains("table-warning") == true)
				{
					//remove highlight current row
					CUSTOMERS_DGV.ROW.ELEMENT(ROW_INDEX).classList.remove("table-warning");

					PROPERTY_REC_CUSTOMER_INDEX = -1;
					PROPERTY_REC_CUSTOMER_NAME = '';
					PROPERTY_REC_CUSTOMER_TIN = '';
					PROPERTY_REC_CUSTOMER_ADDRESS = '';

					PROPERTY_CUSTOMER_ROW_INDEX = -1;
				}
				else
				{
					//remove all highlights
					for(let i = 0; CUSTOMERS_DGV.ROW.COUNT() > i; i++)
					{
						CUSTOMERS_DGV.ROW.ELEMENT(i).classList.remove('table-warning');
					}

					//highlight current row
					CUSTOMERS_DGV.ROW.ELEMENT(ROW_INDEX).classList.add("table-warning");

					PROPERTY_REC_CUSTOMER_INDEX = JSVC_NUM.VAR_NVALL(CUSTOMERS_DGV.CELL.VALUE('REC_CLIENT_ID', ROW_INDEX));
					PROPERTY_REC_CUSTOMER_NAME = CUSTOMERS_DGV.CELL.VALUE('REC_CLIENT_LASTNAME', ROW_INDEX);
					PROPERTY_REC_CUSTOMER_TIN = CUSTOMERS_DGV.CELL.VALUE('REC_CLIENT_TIN', ROW_INDEX);
					PROPERTY_REC_CUSTOMER_ADDRESS = CUSTOMERS_DGV.CELL.VALUE('REC_CLIENT_MIDDLENAME', ROW_INDEX);

					PROPERTY_CUSTOMER_ROW_INDEX = ROW_INDEX+1;
				}
			}
		);

		POS_SALE_DGV.ON_CLICK
		(
			function(COL_INDEX, ROW_INDEX)
			{
				HTTP_CALCULATE_SELECTED();
			}
		);

		PRICE_MODEL_DGV.ON_CLICK
		(
			function(COL_INDEX, ROW_INDEX)
			{
				//check if row clicked has highlight already
				if(PRICE_MODEL_DGV.ROW.ELEMENT(ROW_INDEX).classList.contains("table-warning") == true)
				{
					//remove highlight current row
					PRICE_MODEL_DGV.ROW.ELEMENT(ROW_INDEX).classList.remove("table-warning");

					PRICE_MODEL_ID = 1;

					JSOC_ELEMENT.VAR_VALUE("PRICE_MODEL_TXT", 'SRP');
				}
				else
				{
					//remove all highlights
					for(let i = 0; PRICE_MODEL_DGV.ROW.COUNT() > i; i++)
					{
						PRICE_MODEL_DGV.ROW.ELEMENT(i).classList.remove('table-warning');
					}

					JSOC_ELEMENT.VAR_VALUE("PRICE_MODEL_TXT", PRICE_MODEL_DGV.CELL.VALUE('CAT_PRICEMDL_DESCRIPTION', ROW_INDEX));

					//highlight current row
					PRICE_MODEL_DGV.ROW.ELEMENT(ROW_INDEX).classList.add("table-warning");

					PRICE_MODEL_ID = JSVC_NUM.VAR_NVALL(PRICE_MODEL_DGV.CELL.VALUE('CAT_PRICEMDL_ID', ROW_INDEX));
				}
			}
		);

		JOB_ORDER_DGV.ON_CLICK
		(
			function(COL_INDEX, ROW_INDEX)
			{
				//check if row clicked has highlight already
				if(JOB_ORDER_DGV.ROW.ELEMENT(ROW_INDEX).classList.contains("table-warning") == true)
				{
					//remove highlight current row
					JOB_ORDER_DGV.ROW.ELEMENT(ROW_INDEX).classList.remove("table-warning");

					PROPERTY_REC_JOBORDER_ID = -1;
					PROPERTY_REC_TRANSACTION_REFNO = '';
					PROPERTY_REC_JOBORDER_INDEX = -1;
				}
				else
				{
					//remove all highlights
					for(let i = 0; JOB_ORDER_DGV.ROW.COUNT() > i; i++)
					{
						JOB_ORDER_DGV.ROW.ELEMENT(i).classList.remove('table-warning');
					}

					//highlight current row
					JSOC_ELEMENT.VAR_GET('JOB_ORDER_DGV_ROW_'+JSVC_NUM.VAR_NVALL(ROW_INDEX+1)).classList.add("table-warning");

					PROPERTY_REC_JOBORDER_ID = JSVC_NUM.VAR_NVALL(JOB_ORDER_DGV.CELL.VALUE('REC_JORDER_ID', ROW_INDEX));
					PROPERTY_REC_TRANSACTION_REFNO = JOB_ORDER_DGV.CELL.VALUE('REC_JORDER_REFERENCE_NUMBER', ROW_INDEX);
					PROPERTY_REC_JOBORDER_INDEX = ROW_INDEX;
				}
			}
		);

		// Show Progress
		// _______________________________________________________________________	
		JSOC_DIALOG.VAR_LOADING_SPINNER(true);
		
		
		// Requires Authentication
		// _______________________________________________________________________	
		PAGE_AUTH_REQUIRES_LOGIN(HTTP_PRELOAD_DATA);		
		
		// Get Modal Instance (Add Modal Adjustment before this line)
		// _______________________________________________________________________	
		if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("POS_QTY_MODAL") == true)
		{
			INPUT_INSTANCE = JSOC_WINDOW.VAR_INPUT_INSTANCE_CLONE("POS_QTY_MODAL");
		}
		
		
		// Pre-Load Items (uncomment if no foreign key pre-fetch)
		// _______________________________________________________________________	
		if(CUS_SEND_REQUEST_HAS_QUEUE() == false)
		{
			HTTP_SEARCH();
		}
		
		
		// Disable Dialogs
		// _______________________________________________________________________	
		CUS_SEND_DIALOG_TRY_DISMISS();
	}


	// ===============================================================
	// Preload Data
	// ===============================================================

	function HTTP_PRELOAD_DATA(IS_SUCCESS, HTTP_RESPONSE)
	{
		// Disable Dialogs
		CUS_SEND_DIALOG_TRY_DISMISS();
			
		// Handle Error
		if(IS_SUCCESS === false)
		{
			alert("Unable to load data.");
		}
		else
		{
			PRELOAD_POS_SHIFT
			(
				function(IS_SUCCESS, RESPONSE)
				{
					if(IS_SUCCESS == true)
					{
						// if((JSVC_DATE.VAR_FORMAT(JSVC_DATE.VAR_NOW(), "YYYY-MM-DD") > JSVC_DATE.VAR_FORMAT(JSVC_DATE.VAR_TO_DATE(SHIFT_TIME_START), "YYYY-MM-DD")) == true)
						// {
						// 	PRELOAD_LOCK_POS_SCREEN();

						// 	JSOC_DIALOG.VAR_MESSAGE_BOX
						// 	(
						// 		'You have an open shift, the system will automatically end your shift and generate X Reading.',
						// 		'Information',
						// 		'OK', 'ERROR',
						// 		function() {
						// 			END_SHIFT();
						// 		}
						// 	);
						// }
						// else
						// {
							if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("POS_MAIN_CONTAINER"))
							{
								HTTP_SEARCH();

								LOAD_BRANCH();

								PRELOAD_POS_CODE();
								PRELOAD_DISCOUNTS();
								PRELOAD_BANKS();
								PRELOAD_CARD_TYPES();
								PRELOAD_PAYMENT_TERMS();
								PRELOAD_PRICING_SCHEMES();

								AUTO_UPDATE_PRODUCT_DATA();
							}
						//}
					}
					else
					{
						//check if a zread has been generated
						CHECK_ZREAD_EXIST
		    			(
		    				function(IS_SUCCESS)
		    				{
		    					if(IS_SUCCESS == true)
		    					{
		    						PRELOAD_LOCK_POS_SCREEN();

		    						JSOC_DIALOG.VAR_MESSAGE_BOX
									(
										'You cannot transact any further, Z Reading report has been generated.',
										'Information',
										'OK', 'ERROR',
										function() {}
									);
		    					}
		    					else
		    					{
		    						if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("POS_MAIN_CONTAINER"))
									{
										if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("SHIFT_START_MODAL") == true)
										{
											// Clear Modal
											if(JSVC_VAR.VAR_ISSET(INPUT_INSTANCE))
											{
												JSOC_WINDOW.VAR_INPUT_INSTANCE_RESTORE(INPUT_INSTANCE);
											}
															
											// Display Modal
											JSOC_DIALOG.VAR_MODAL("SHIFT_START_MODAL", true);
											return;
										}
									}
		    					}
		    				}
		    			);
					}
				}
			);
		}
	}
	
	
	function LOAD_BRANCH()
	{
		if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("SEARCH_REC_JORDER_ACC_GROUP_ID_CMB") == true)
		{
			JSCUS_SQL._ACCOUNT._GROUP.CUS_GET_THEN_POPULATE_COMBOBOX
			(
				function()
				{
					JSOC_CMB.VAR_SELECTED_VALUE(SEARCH_REC_JORDER_ACC_GROUP_ID_CMB, JSVC_NUM.VAR_NVALL(JSCUS_SQL._ACCOUNT._LOGIN.ACC_ACCOUNT_ACC_GROUP_ID));
					CUS_SEND_DIALOG_TRY_DISMISS();
				},
				"SEARCH_REC_JORDER_ACC_GROUP_ID_CMB",
				true, 2
			);
		}
	}

	// ===============================================================
	// Request Search - PART.A
	// ===============================================================
	function HTTP_SEARCH(NEW_ITEM_ID, EDITED_ROW_INDEX)
	{	
		// Actual Function
		var GET_MAP_ARRAY = JSCUS_SQL._CATEGORY._PRODUCT_BRANCH.CUS_PREPARE_MAPPED_ARRAY_FOR_GET();

		JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_CAT_PRODBRANCH_ACC_GROUP_ID', JSVC_NUM.VAR_NVALL(JSCUS_SQL._RECORD._POSDEVICE._LOGIN.REC_POSDEVICE_ACC_GROUP_ID));
		JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_CAT_PRODBRANCH_CAT_PRICEMDL_ID', PRICE_MODEL_ID);
		JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_CAT_PRODBRANCH_STATUS', 2);

		JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_ORDER_FIELD', 'CAT_PRODUCT_DESCRIPTION');
		JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_ORDER_TYPE', 'ASC'); 
			
		JSOC_DIALOG.VAR_LOADING_SPINNER(true);

		JSCUS_SQL._CATEGORY._PRODUCT_BRANCH.CUS_GET
		(
			GET_MAP_ARRAY,
			HTTP_SEARCH_CALLBACK,
			true
		);       		
	}
	
	
	// ===============================================================
	// Request Search - PART.B
	// ===============================================================
	function HTTP_SEARCH_CALLBACK(IS_SUCCESS, RESPONSE_STRING)
	{
		// Disable Dialogs
		// _______________________________________________________________________
		CUS_SEND_DIALOG_TRY_DISMISS();
		
		// Validate
		// _______________________________________________________________________
		if(IS_SUCCESS === false)
		{
			console.error("CONNECTION ERROR: \n" + RESPONSE_STRING);
			return;
		}
		
		// Validate JSON Response
		// _______________________________________________________________________
		var JSON_OBJECT = JSRC_JSON.REF_CREATE(RESPONSE_STRING);
		if(JSRC_JSON.REF_IS_SUCCESS(JSON_OBJECT) === false)
		{
			console.error("REQUEST ERROR: \n" + RESPONSE_STRING);
			return;
		}
		
		// JSON Empty (Clears the DataGrodView)
		// _______________________________________________________________________
		if(JSRC_JSON.REF_READ(JSON_OBJECT, "JSON_COUNT") <= 0)
		{
			PRODUCT_BRANCH_LV.ROW.CLEAR();

			JSOC_DIALOG.VAR_LOADING_SPINNER(false);

			JSOC_DIALOG.VAR_MESSAGE_BOX
			(
				'No products found for the selected pricing scheme. System will go back to default SRP.',
				'Information',
				'OK', 'ERROR',
				function() {
					PRICE_MODEL_ID = 1;
					HTTP_SEARCH();
				}
			);

			return;
		}

		// Populate DGV
		// _______________________________________________________________________

		PRODUCT_BRANCH_LV.ROW.CLEAR();

		var JSON_COUNT = JSRC_JSON.REF_READ(JSON_OBJECT, "JSON_COUNT");
		var COUNTER = 1;

		var IS_INSERTED = false;

		while(COUNTER <= JSON_COUNT)
		{
			var R_INDEX = 1; 
			var IS_INSERTED = 0;

			while(PRODUCT_BRANCH_LV.ROW.COUNT() >= R_INDEX)
			{
				if
				(
					(JSVC_NUM.VAR_NVALL(PRODUCT_BRANCH_LV.CELL.VALUE("CAT_PRODUCT_ID", R_INDEX-1)) ==
					JSVC_NUM.VAR_NVALL(JSRC_JSON.REF_READ(JSON_OBJECT, "CAT_PRODUCT_ID", COUNTER))) &&
					(JSVC_NUM.VAR_NVALL(JSRC_JSON.REF_READ(JSON_OBJECT, "CAT_PRODUCT_ID", COUNTER)) !== 1)
				)
				{
					IS_INSERTED = 1;
				}

				R_INDEX++;
			}

			if(IS_INSERTED == 0)
			{
				if(JSVC_NUM.VAR_NVALL(JSRC_JSON.REF_READ(JSON_OBJECT, "CAT_PRODUCT_IS_INVENTORY_ONLY", COUNTER)) == 1)
				{
					var RINDEX = PRODUCT_BRANCH_LV.ROW.COUNT();

					PRODUCT_BRANCH_LV.ROW.INSERT_AT(RINDEX);

					PRODUCT_BRANCH_LV.CELL.VALUE('CAT_PRODUCT_ID', RINDEX, JSRC_JSON.REF_READ(JSON_OBJECT, "CAT_PRODBRANCH_CAT_PRODUCT_ID", COUNTER));
					PRODUCT_BRANCH_LV.CELL.VALUE('CAT_PRODUCT_CAT_PRODTYPE_ID_LV1', RINDEX, JSRC_JSON.REF_READ(JSON_OBJECT, "CAT_PRODUCT_CAT_PRODTYPE_ID_LV1", COUNTER));

					PRODUCT_BRANCH_LV.CELL.VALUE('CAT_PRODUCT_AMOUNT_VAT', RINDEX, JSRC_JSON.REF_READ(JSON_OBJECT, "CAT_PRODUCT_AMOUNT_VAT", COUNTER));
					PRODUCT_BRANCH_LV.CELL.VALUE('CAT_PRODUCT_AMOUNT_POINT', RINDEX, JSRC_JSON.REF_READ(JSON_OBJECT, "CAT_PRODUCT_AMOUNT_POINT", COUNTER));

					PRODUCT_BRANCH_LV.CELL.VALUE('CAT_PRODUCT_ALLOW_DISCOUNT', RINDEX, JSRC_JSON.REF_READ(JSON_OBJECT, "CAT_PRODUCT_ALLOW_DISCOUNT", COUNTER));
					PRODUCT_BRANCH_LV.CELL.VALUE('CAT_PRODUCT_ALLOW_SENIOR', RINDEX, JSRC_JSON.REF_READ(JSON_OBJECT, "CAT_PRODUCT_ALLOW_SENIOR", COUNTER));

					PRODUCT_BRANCH_LV.CELL.VALUE('CAT_PRODUCT_PER_PIECE_QTY', RINDEX, JSRC_JSON.REF_READ(JSON_OBJECT, "CAT_PRODUCT_PER_PIECE_QTY", COUNTER));

					PRODUCT_BRANCH_LV.CELL.VALUE('CAT_PRODUCT_CAT_TAXMODEL_ID', RINDEX, JSRC_JSON.REF_READ(JSON_OBJECT, "CAT_PRODUCT_CAT_TAXMODEL_ID", COUNTER));
					PRODUCT_BRANCH_LV.CELL.VALUE('CAT_TAXMODEL_AMOUNT', RINDEX, JSRC_JSON.REF_READ(JSON_OBJECT, "CAT_TAXMODEL_AMOUNT", COUNTER));
					PRODUCT_BRANCH_LV.CELL.VALUE('CAT_TAXMODEL_PERCENTAGE', RINDEX, JSRC_JSON.REF_READ(JSON_OBJECT, "CAT_TAXMODEL_PERCENTAGE", COUNTER));

					PRODUCT_BRANCH_LV.CELL.VALUE('CAT_DISCOUNT_ID', RINDEX, JSRC_JSON.REF_READ(JSON_OBJECT, "CAT_DISCOUNT_ID", COUNTER));
					PRODUCT_BRANCH_LV.CELL.VALUE('CAT_DISCOUNT_PERCENTAGE', RINDEX, JSRC_JSON.REF_READ(JSON_OBJECT, "CAT_DISCOUNT_PERCENTAGE", COUNTER));
					PRODUCT_BRANCH_LV.CELL.VALUE('CAT_DISCOUNT_AMOUNT', RINDEX, JSRC_JSON.REF_READ(JSON_OBJECT, "CAT_DISCOUNT_AMOUNT", COUNTER));
					PRODUCT_BRANCH_LV.CELL.VALUE('CAT_DISCOUNT_VAT', RINDEX, JSRC_JSON.REF_READ(JSON_OBJECT, "CAT_DISCOUNT_VAT", COUNTER));
					PRODUCT_BRANCH_LV.CELL.VALUE('CAT_DISCOUNT_HAS_POINT', RINDEX, JSRC_JSON.REF_READ(JSON_OBJECT, "CAT_DISCOUNT_HAS_POINT", COUNTER));
					PRODUCT_BRANCH_LV.CELL.VALUE('CAT_DISCOUNT_CHANGE_TAX_TYPE', RINDEX, JSRC_JSON.REF_READ(JSON_OBJECT, "CAT_DISCOUNT_CHANGE_TAX_TYPE", COUNTER));
					PRODUCT_BRANCH_LV.CELL.VALUE('CAT_DISCOUNT_CAT_TAXMODEL_ID', RINDEX, JSRC_JSON.REF_READ(JSON_OBJECT, "CAT_DISCOUNT_CAT_TAXMODEL_ID", COUNTER));

					PRODUCT_BRANCH_LV.CELL.VALUE('CAT_PRODUCT_CAT_UOM_ID', RINDEX, JSRC_JSON.REF_READ(JSON_OBJECT, "CAT_PRODUCT_CAT_UOM_ID", COUNTER));
					PRODUCT_BRANCH_LV.CELL.VALUE('CAT_UOM_CODE', RINDEX, JSRC_JSON.REF_READ(JSON_OBJECT, "CAT_UOM_CODE", COUNTER));
					
					PRODUCT_BRANCH_LV.CELL.VALUE('CAT_PRODUCT_IMAGE', RINDEX, '<img src="' + JSRC_UPLOAD.REF_SUGGEST_PATH(JSCUS_CACHE.SERVER_URL + '/upload/pub/Category/product/', JSRC_JSON.REF_READ(JSON_OBJECT, "CAT_PRODBRANCH_CAT_PRODUCT_ID", COUNTER)) + '.jpg" class="card-img-top">');

					PRODUCT_BRANCH_LV.CELL.VALUE('CAT_PRODBRANCH_CAT_PRODUCT_ID_DESCRIPTION', RINDEX, JSRC_JSON.REF_READ(JSON_OBJECT, "CAT_PRODBRANCH_CAT_PRODUCT_ID_DESCRIPTION", COUNTER));
					PRODUCT_BRANCH_LV.CELL.VALUE('CAT_PRODBRANCH_AMOUNT_PRICE', RINDEX, JSVC_NUM.VAR_CFORMAT(JSRC_JSON.REF_READ(JSON_OBJECT, "CAT_PRODBRANCH_AMOUNT_PRICE", COUNTER)));

					if(PROPERTY_REC_PRODUCT_TYPES.includes(JSVC_NUM.VAR_NVALL(JSRC_JSON.REF_READ(JSON_OBJECT, "CAT_PRODUCT_CAT_PRODTYPE_ID_LV1", COUNTER))) === false)
						PROPERTY_REC_PRODUCT_TYPES.push(JSVC_NUM.VAR_NVALL(JSRC_JSON.REF_READ(JSON_OBJECT, "CAT_PRODUCT_CAT_PRODTYPE_ID_LV1", COUNTER)));
				}
				
			}

			COUNTER++;
		}

		HTTP_LOAD_CATEGORIES();
	}


	function HTTP_LOAD_CATEGORIES()
	{
		// Actual Function
		// _______________________________________________________________________
		var GET_MAP_ARRAY = JSCUS_SQL._CATEGORY._PRODTYPE.CUS_PREPARE_MAPPED_ARRAY_FOR_GET();
		JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_CAT_PRODTYPE_STATUS', 2);


		// Declare Post Request Handler
		// _______________________________________________________________________
		var RESPONSE_HANDLER = 
			function(IS_SUCCESS, HTTP_RESPONSE)
			{
				// Disable Dialogs
				// ...........................................
				CUS_SEND_DIALOG_TRY_DISMISS();
					
					
				// Handle Error
				// ...........................................
				if(IS_SUCCESS === false)
				{
					METHOD_UNABLE_TO_SEND_REQUEST(CALLBACK_METHOD);
					return;
				}
				
				// Catch JSON Error
				// ...........................................
				else if(JSRC_JSON.REF_IS_JSTRING(HTTP_RESPONSE) == false)
				{
					METHOD_SERVER_ISSUE(CALLBACK_METHOD, HTTP_RESPONSE);
					return;			
				}
				
				
				// Read Response
				// ...........................................
				else
				{
					// Load Response
					var JSON_OBJECT = JSRC_JSON.REF_CREATE(HTTP_RESPONSE);
					if(JSRC_JSON.REF_IS_SUCCESS(JSON_OBJECT) === false)
					{
						METHOD_REQUEST_FAILURE(CALLBACK_METHOD, JSON_OBJECT);
						return;
					}
					
					else
					{
						var CTR = 0;
						var select = JSOC_ELEMENT.VAR_GET('CATEGORY_CMB');

						//remove child / clear options
						if(JSOC_ELEMENT.VAR_GET('CATEGORY_CMB').hasChildNodes() == true)
						{
							$('#CATEGORY_CMB').empty();
						}
						

						// add default item
						var option = document.createElement('option');

						option.setAttribute('value', 0);
						option.appendChild(document.createTextNode('All Items'));

						JSOC_ELEMENT.VAR_GET('CATEGORY_CMB').appendChild(option);

						while(JSRC_JSON.REF_READ(JSON_OBJECT, "JSON_COUNT") > CTR)
						{
							if(PROPERTY_REC_PRODUCT_TYPES.includes(JSVC_NUM.VAR_NVALL(JSRC_JSON.REF_READ(JSON_OBJECT, "CAT_PRODTYPE_ID", CTR))) === true)
							{
								var option = document.createElement('option');

						   		option.setAttribute('value', JSRC_JSON.REF_READ(JSON_OBJECT, "CAT_PRODTYPE_ID", CTR));
						    	option.appendChild(document.createTextNode(JSRC_JSON.REF_READ(JSON_OBJECT, "CAT_PRODTYPE_DESCRIPTION", CTR)));

						    	JSOC_ELEMENT.VAR_GET('CATEGORY_CMB').appendChild(option);
							}

							CTR++;
						}

						$('#CATEGORY_CMB').select2();
					}
				}
			};
		
		
		// Save Item [Send Request]
		// _______________________________________________________________________
		JSOC_DIALOG.VAR_LOADING_SPINNER(true);
		JSCUS_SQL._CATEGORY._PRODTYPE.CUS_GET
		(
			GET_MAP_ARRAY,
			RESPONSE_HANDLER,
			true
		);
	}

	function HTTP_LOAD_CUSTOMERS()
	{
		var GET_MAP_ARRAY = JSCUS_SQL._RECORD._CLIENT.CUS_PREPARE_MAPPED_ARRAY_FOR_GET();
		JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_REC_CLIENT_STATUS', 2);
		
		JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_ORDER_FIELD', 'REC_CLIENT_CODE');
		JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_ORDER_TYPE', 'ASC');

		if(JSVC_STR.VAR_IS_BLANK(JSOC_ELEMENT.VAR_VALUE("SEARCH_CUSTOMER_FN_TXT")) == false)
		{
			JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_REC_CLIENT_FIRSTNAME', JSOC_ELEMENT.VAR_VALUE("SEARCH_CUSTOMER_FN_TXT"));
		}

		if(JSVC_STR.VAR_IS_BLANK(JSOC_ELEMENT.VAR_VALUE("SEARCH_CUSTOMER_LN_TXT")) == false)
		{
			JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_REC_CLIENT_LASTNAME', JSOC_ELEMENT.VAR_VALUE("SEARCH_CUSTOMER_LN_TXT"));
		}

		var RESPONSE_HANDLER =
			function(IS_SUCCESS, RESPONSE_STRING)
			{				
				// Connection issue
				// ...........................................
				if(IS_SUCCESS === false) 
				{
					alert("CONNECTION ERROR: \n" + RESPONSE_STRING);
					return;
				}
		
				// Response is not valid JSON
				// ...........................................
				var JSON_OBJECT = JSRC_JSON.REF_CREATE(RESPONSE_STRING);
				if(JSRC_JSON.REF_IS_SUCCESS(JSON_OBJECT) === false)
				{
					alert("REQUEST ERROR: \n" + RESPONSE_STRING); 
					return;
				}
		
				// No result
				// ...........................................
				if(JSRC_JSON.REF_READ(JSON_OBJECT, "JSON_COUNT") <= 0)
				{
					CUSTOMERS_DGV.ROW.CLEAR();
					return; 
				}
		
				// Populate discounts
				// ...........................................
				CUSTOMERS_DGV.POPULATE_FROM_QUERY
				(
					JSON_OBJECT, 
					'',
					false,
					false,
					1,
					[2, 1]
				);
				

				CACHE_CUSTOMER_JSON = JSON_OBJECT;
			};
		
		JSCUS_SQL._RECORD._CLIENT.CUS_GET
		(
			GET_MAP_ARRAY, 
			RESPONSE_HANDLER, 
			true
		); 
	}
	
	function HTTP_LOAD_TRANSACTIONS()
	{
		var GET_MAP_ARRAY = JSCUS_SQL._RECORD._JOB_ORDER.CUS_PREPARE_MAPPED_ARRAY_FOR_GET();

		if
		(
			JSOC_ELEMENT.VAR_ELEMENT_EXISTS("SEARCH_REC_JORDER_REFERENCE_NUMBER_TXT") == true &&
			JSVC_STR.VAR_IS_BLANK( JSOC_ELEMENT.VAR_VALUE("SEARCH_REC_JORDER_REFERENCE_NUMBER_TXT")) == false
		)
		{
			JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_REC_JORDER_REFERENCE_NUMBER', JSOC_ELEMENT.VAR_VALUE("SEARCH_REC_JORDER_REFERENCE_NUMBER_TXT"));
		}
		
		// JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_REC_JORDER_ACC_GROUP_ID', JSVC_NUM.VAR_NVALL(JSCUS_SQL._ACCOUNT._LOGIN.ACC_ACCOUNT_ACC_GROUP_ID));
		JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_REC_JORDER_ACC_GROUP_ID', JSVC_NUM.VAR_NVAL(JSOC_CMB.VAR_SELECTED_VALUE("SEARCH_REC_JORDER_ACC_GROUP_ID_CMB")));
		JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_REC_JORDER_LONG_B_FROM', 0);
		JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_REC_JORDER_LONG_B_TO', 0);
		JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_REC_JORDER_STATUS', 4);

		var RESPONSE_HANDLER =
			function(IS_SUCCESS, RESPONSE_STRING)
			{				
				// Disable Dialogs
				// _______________________________________________________________________
				CUS_SEND_DIALOG_TRY_DISMISS();

				//RESET DGV
				// _______________________________________________________________________
				JOB_ORDER_DGV.ROW.CLEAR();

				// Connection issue
				// ...........................................
				if(IS_SUCCESS === false) 
				{
					alert("CONNECTION ERROR: \n" + RESPONSE_STRING);
					return;
				}
		
				// Response is not valid JSON
				// ...........................................
				var JSON_OBJECT = JSRC_JSON.REF_CREATE(RESPONSE_STRING);
				if(JSRC_JSON.REF_IS_SUCCESS(JSON_OBJECT) === false)
				{
					alert("REQUEST ERROR: \n" + RESPONSE_STRING); 
					return;
				}
		
				// No result
				// ...........................................
				if(JSRC_JSON.REF_READ(JSON_OBJECT, "JSON_COUNT") <= 0)
				{
					JSOC_DIALOG.VAR_MESSAGE_BOX
					(
						'No transactions found',
						'Information',
						'OK', 'ERROR',
						function() {
							PRICE_MODEL_ID = 1;
							//HTTP_SEARCH();
						}
					);
					return; 
				}
		
				// Populate discounts
				// ...........................................
				JOB_ORDER_DGV.POPULATE_FROM_QUERY
				(
					JSON_OBJECT, 
					'',
					false,
					false,
					1,
					[2, 1]
				);

				// Initialize
				// _______________________________________________________________________	
				var WEBTABLE_DATE = null;
				var ROW_INDEX = 0;

				while(JOB_ORDER_DGV.ROW.COUNT() > ROW_INDEX)
				{
					if(JSVC_NUM.VAR_NVALL(JOB_ORDER_DGV.CELL.VALUE('REC_JORDER_DECIMAL_A', ROW_INDEX)) == 2)
						JOB_ORDER_DGV.CELL.VALUE('REC_JORDER_DECIMAL_A_DESCRIPTION', ROW_INDEX, 'Cash');
					else if(JSVC_NUM.VAR_NVALL(JOB_ORDER_DGV.CELL.VALUE('REC_JORDER_DECIMAL_A', ROW_INDEX)) == 3)
						JOB_ORDER_DGV.CELL.VALUE('REC_JORDER_DECIMAL_A_DESCRIPTION', ROW_INDEX, 'Credit');
					else
						JOB_ORDER_DGV.CELL.VALUE('REC_JORDER_DECIMAL_A_DESCRIPTION', ROW_INDEX, '');

					JOB_ORDER_DGV.CELL.VALUE
					(
						'REC_CLIENT_NAME',
						ROW_INDEX,
						JOB_ORDER_DGV.CELL.VALUE('REC_CLIENT_FIRSTNAME', ROW_INDEX) +
						' ' +
						JOB_ORDER_DGV.CELL.VALUE('REC_CLIENT_LASTNAME', ROW_INDEX)
					);

					ROW_INDEX++;
				}
			
				// DateTime Format
				// _______________________________________________________________________	
				if(JOB_ORDER_DGV.COLUMN.IS_EXISTS("REC_JORDER_DATE_POST"))
				{
					WEBTABLE_DATE = null;
					ROW_INDEX = 0;
					while(JOB_ORDER_DGV.ROW.COUNT() > ROW_INDEX)
					{
						// Read Current Value
						// ...........................................
						 WEBTABLE_DATE = JSVC_DATE.VAR_TO_DATE
							(
								JOB_ORDER_DGV.CELL.VALUE
								(
									"REC_JORDER_DATE_POST", 
									ROW_INDEX
								)
							);
						
						// Apply CFormat, and DateFormat
						// ...........................................
						if(JSVC_VAR.VAR_ISSET(WEBTABLE_DATE))
						{
							JOB_ORDER_DGV.CELL.VALUE
							(
								"REC_JORDER_DATE_POST", ROW_INDEX, 
								JSVC_DATE.VAR_FORMAT(WEBTABLE_DATE, "MMM DD, YYYY hh:mm TT")
							);				
						}

						ROW_INDEX ++;
					}			
				}
			};

		JSOC_DIALOG.VAR_LOADING_SPINNER(true);
		JSCUS_SQL._RECORD._JOB_ORDER.CUS_GET
		(
			GET_MAP_ARRAY,
			RESPONSE_HANDLER,
			true
		);
	}

	// ===============================================================
	// Begin Processing the Page
	// ===============================================================
	FORM_INITIALIZE();
	FORM_LOAD();