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

	// ===============================================================
	// Initialize Form [DGV]
	// ===============================================================
	function FORM_LOAD_DATAGRIDVIEW()
	{
		// Initialize
		// _______________________________________________________________________	
		var DGV_ELEMENT_CHILD = null;
		var DGV_ELEMENT_CHILD_INDEX = 0;
		
		
		// Load DataGridVuew
		// _______________________________________________________________________	
		JSTOOL_CUSTOMIZE.DGVREORDER.APPLY("EOD_PRODUCT_DGV");
		EOD_PRODUCT_DGV = DataGridView("EOD_PRODUCT_DGV");
		//EOD_PRODUCT_DGV.SETTING_ALLOW_USER_TO_ADD_ROWS = true;
		//EOD_PRODUCT_DGV.SETTING_EDITABLE = true;
	

		// Auto Compute
		// _______________________________________________________________________	
		var AUTO_CALC = JSTOOL_CUSTOMIZE.DGVCALC.APPLY(EOD_PRODUCT_DGV);
		var AUTO_COMPUTE_BLOCKS = AUTO_CALC[0];
			
		/*
			// Sample Code
			AUTO_COMPUTE_BLOCKS.push
			(
				[
					'COLUMN_A', 					// Target Column
					'`COLUMN_1` + `COLUMN_2`',		// Math
					true							// Apply CFormat
				]
			);
			
			// Sample Code [Using value from auth-total]
			AUTO_COMPUTE_BLOCKS.push
			(
				[
					'COLUMN_A', 					// Target Column
					'`COLUMN_1` + `TOTAL:COLUMN_2`',// Math
					true							// Apply CFormat
				]
			);
		*/
		
		AUTO_COMPUTE_BLOCKS.push
		(
			[
				'REC_EODPROD_QTY_END_EXPECT', 				
				'`REC_EODPROD_QTY_END` + `REC_EODPROD_STOCK_ADJUSTMENT`',
				true							
			]
		);
		EOD_PRODUCT_DGV.COLUMN.AUTO_COMPUTE_CREATE(AUTO_COMPUTE_BLOCKS);
		
		
		// Auto Total
		// _______________________________________________________________________	
		var AUTO_TOTAL_COLUMNS = AUTO_CALC[1];
		var AUTO_TOTAL_COPY_TO_ELEMENT = AUTO_CALC[2];
		
		/*
			AUTO_TOTAL_COLUMNS.push('COLUMN_A');					// Column to Auto Total
			AUTO_TOTAL_COPY_TO_ELEMENT.push(["COL_ID", "ELEM_ID"]);	// Source Column, Element ID where total is copied to
			
			AUTO_TOTAL_COLUMNS.push('REC_EODPROD_QTY_BEGIN');
			AUTO_TOTAL_COLUMNS.push('REC_EODPROD_QTY_END');
			AUTO_TOTAL_COLUMNS.push('REC_EODPROD_STOCK_IN');
			AUTO_TOTAL_COLUMNS.push('REC_EODPROD_STOCK_OUT');
			AUTO_TOTAL_COLUMNS.push('REC_EODPROD_STOCK_ADJUSTMENT');
			AUTO_TOTAL_COLUMNS.push('REC_EODPROD_MOVE_QTY_01');
			AUTO_TOTAL_COLUMNS.push('REC_EODPROD_MOVE_QTY_02');
			AUTO_TOTAL_COLUMNS.push('REC_EODPROD_MOVE_QTY_03');
			AUTO_TOTAL_COLUMNS.push('REC_EODPROD_MOVE_QTY_04');
			AUTO_TOTAL_COLUMNS.push('REC_EODPROD_MOVE_QTY_05');
		*/
		/*
		EOD_PRODUCT_DGV.COLUMN.TOTAL_CREATE
		(
			AUTO_TOTAL_COLUMNS,
			'REC_EODPROD_ID',									// Column, where you want to display the caption `Total:`.
			AUTO_TOTAL_COPY_TO_ELEMENT
		);		
		*/
		
		
		// DGV OnClick Event
		// _______________________________________________________________________		
		EOD_PRODUCT_DGV.ON_CLICK
		(			
			function(COL_INDEX, ROW_INDEX)
			{
				FORM_ITEM_SHOWHIDE(ROW_INDEX);
			}
		);   


		// DGV OnChange Event
		// _______________________________________________________________________	
		EOD_PRODUCT_DGV.ON_CHANGE
		(			
			function(COL_INDEX, ROW_INDEX)
			{
				// Auto-Calculate tool
				// ...........................................
				// EOD_PRODUCT_DGV.COLUMN.AUTO_COMPUTE_EXECUTE(ROW_INDEX);
				
				// Auto-total tool
				// ...........................................
				if(EOD_PRODUCT_DGV.COLUMN.TOTAL_IS_READY())
				{
					EOD_PRODUCT_DGV.COLUMN.TOTAL_REFRESH();
				}
			}
		);
		
	};


	// ===============================================================
	// Initialize Form [Main]
	// ===============================================================
	function FORM_LOAD()
	{
		// Prepare DGV
		// _______________________________________________________________________	
		FORM_LOAD_DATAGRIDVIEW();
		
		// Show Progress
		// _______________________________________________________________________	
		JSOC_DIALOG.VAR_LOADING_SPINNER(true);
		
		
		// Requires Authentication
		// _______________________________________________________________________	
		PAGE_AUTH_REQUIRES_LOGIN(HTTP_POPULATE_RESULT);			


		
		
		// Foreign Key [Fixed]
		// _______________________________________________________________________	
		if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("REC_EODPROD_CAT_PRODUCT_ID_CMB") == true)
		{
			// Set Default Item, if used in DataGridView
			JSOC_CMB.VAR_ADD("REC_EODPROD_CAT_PRODUCT_ID_CMB", FLAG_BIGINT_NULL.toString(), "-");
			// To use custom populate
			// var GET_MAP_ARRAY = JSCUS_SQL._CATEGORY._PRODUCT.CUS_PREPARE_MAPPED_ARRAY_FOR_GET();
			// JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_', JSCUS_SQL._SYSTEM._STATUS.STATUS_ACTIVE);	
			JSCUS_SQL._CATEGORY._PRODUCT.CUS_GET_THEN_POPULATE_COMBOBOX
			(
				function(IS_SUCCESS, RESPONSE_STRING)
				{
					// Set Default index
					JSOC_CMB.VAR_SELECTED_INDEX("REC_EODPROD_CAT_PRODUCT_ID_CMB", 0);
					
					// Clone to DGV Cell if ComboBox Exists
					if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("EOD_PRODUCT_DGV_ROW_REC_EODPROD_CAT_PRODUCT_ID") == true)
					{
						DGV_ELEMENT_CHILD = JSOC_ELEMENT.VAR_SUB_ELEMENTS("EOD_PRODUCT_DGV_ROW_REC_EODPROD_CAT_PRODUCT_ID");
						DGV_ELEMENT_CHILD_INDEX = 0;
						while(DGV_ELEMENT_CHILD.length > DGV_ELEMENT_CHILD_INDEX)
						{
							if
							(
								DGV_ELEMENT_CHILD[DGV_ELEMENT_CHILD_INDEX].tagName == "SELECT" &&
								JSVC_STR.VAR_IS_BLANK(DGV_ELEMENT_CHILD[DGV_ELEMENT_CHILD_INDEX].id.toString())
							)
							{
								JSOC_ELEMENT.VAR_INNER_HTML
								(
									DGV_ELEMENT_CHILD[DGV_ELEMENT_CHILD_INDEX], 
									JSOC_ELEMENT.VAR_INNER_HTML("REC_EODPROD_CAT_PRODUCT_ID_CMB")
								);
							}	
							DGV_ELEMENT_CHILD_INDEX ++;
						}
					}
					
					// Copy items to edit control
					if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("SEARCH_REC_EODPROD_CAT_PRODUCT_ID_CMB") == true)
					{
						JSOC_CMB.VAR_CLEAR("SEARCH_REC_EODPROD_CAT_PRODUCT_ID_CMB");
						JSOC_CMB.VAR_ADD("SEARCH_REC_EODPROD_CAT_PRODUCT_ID_CMB", FLAG_BIGINT_NULL.toString(),"Any Product");
						JSOC_ELEMENT.VAR_INNER_HTML("SEARCH_REC_EODPROD_CAT_PRODUCT_ID_CMB", JSOC_ELEMENT.VAR_INNER_HTML("REC_EODPROD_CAT_PRODUCT_ID_CMB"), true);
						JSOC_CMB.VAR_SELECTED_INDEX("SEARCH_REC_EODPROD_CAT_PRODUCT_ID_CMB", 0);
						$('.BSSP_SEARCH_REC_EODPROD_CAT_PRODUCT_ID_CMB').selectpicker('refresh');
					}				
					
					// Update Modal Settings
					if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("EOD_PRODUCT_MODAL") == true)
					{
						INPUT_INSTANCE = JSOC_WINDOW.VAR_INPUT_INSTANCE_CLONE("EOD_PRODUCT_MODAL");
					}
					
					// Forward populate complete event.
					$('.BSSP_REC_EODPROD_CAT_PRODUCT_ID_CMB').selectpicker('refresh');
					HTTP_POPULATE_RESULT();		
				},
				"REC_EODPROD_CAT_PRODUCT_ID_CMB",
				true, 2
				// , GET_MAP_ARRAY 
			);
		}
		if
		(
			JSOC_ELEMENT.VAR_ELEMENT_EXISTS("SEARCH_REC_EODPROD_CAT_PRODUCT_ID_CMB") == true &&
			JSOC_ELEMENT.VAR_ELEMENT_EXISTS("REC_EODPROD_CAT_PRODUCT_ID_CMB") == false
		)
		{
			JSOC_CMB.VAR_CLEAR("SEARCH_REC_EODPROD_CAT_PRODUCT_ID_CMB");
			JSOC_CMB.VAR_ADD("SEARCH_REC_EODPROD_CAT_PRODUCT_ID_CMB", FLAG_BIGINT_NULL.toString(),"Any Product");
			// To use custom populate
			// var GET_MAP_ARRAY = JSCUS_SQL._CATEGORY._PRODUCT.CUS_PREPARE_MAPPED_ARRAY_FOR_GET();
			// JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_', JSCUS_SQL._SYSTEM._STATUS.STATUS_ACTIVE);	
			JSCUS_SQL._CATEGORY._PRODUCT.CUS_GET_THEN_POPULATE_COMBOBOX
			(
				function(IS_SUCCESS, RESPONSE_STRING)
				{
					// Set Default index
					JSOC_CMB.VAR_SELECTED_INDEX("SEARCH_REC_EODPROD_CAT_PRODUCT_ID_CMB", 0);
					
					// Forward populate complete event.		
					$('.BSSP_SEARCH_REC_EODPROD_CAT_PRODUCT_ID_CMB').selectpicker('refresh');
					HTTP_POPULATE_RESULT();
				},		
				"SEARCH_REC_EODPROD_CAT_PRODUCT_ID_CMB",
				true, 2
				// , GET_MAP_ARRAY 
			);
		}
		
		
		
		// Get Modal Instance (Add Modal Adjustment before this line)
		// _______________________________________________________________________	
		if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("EOD_PRODUCT_MODAL") == true)
		{
			INPUT_INSTANCE = JSOC_WINDOW.VAR_INPUT_INSTANCE_CLONE("EOD_PRODUCT_MODAL");
		}
		
		// Add Search Events for the SEARCH_DESCRIPTION_TXT - TextInput
		// _______________________________________________________________________		
		if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("SEARCH_REC_EODPROD_ID_TXT"))
		{
			var SEARCH_REC_EODPROD_ID_TXT = JSOC_ELEMENT.VAR_GET("SEARCH_REC_EODPROD_ID_TXT");
			SEARCH_REC_EODPROD_ID_TXT.onkeydown = 
				function() 
				{ 
					return function(e) 
					{
						if
						(
							(
								e.which == 13 || 
								e.keyCode == 13 || 
								e.key === "Enter"
							) &&
							JSOC_ELEMENT.VAR_ELEMENT_EXISTS("SEARCH_CMD")
						)
						{
							HTTP_SEARCH();
						}
					};
				}();
		}
		FORM_LOAD_STOCK_MOVEMENT();
		
		
		// Load Parent Data (if Exists)
		// _______________________________________________________________________	
		HTTP_PARENT_LOAD();
		
		
		// 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();
	};
	
	
	// ===============================================================
	// Populate Foreign Keys
	// ===============================================================
	function HTTP_POPULATE_RESULT(IS_SUCCESS, HTTP_RESPONSE)
	{
		// Disable Dialogs
		CUS_SEND_DIALOG_TRY_DISMISS();
			
		// Handle Error
		if(IS_SUCCESS === false)
		{
			alert("Unable to populate lists.");
		}
		else
		{
			// Disable Dialogs
			if(CUS_SEND_REQUEST_HAS_QUEUE() == false)
			{
				// HTTP_SEARCH();
			}
		}
	};

	
	// ===============================================================
	// Request Search - PART.A
	// ===============================================================
	function HTTP_SEARCH()
	{	
		// Actual Function
		// _______________________________________________________________________
		GET_MAP_ARRAY = JSCUS_SQL._RECORD._EOD_PRODUCT.CUS_PREPARE_MAPPED_ARRAY_FOR_GET();	
		// if(JSVC_VAR.VAR_ISSET("PROPERTY_PARENT_ID"))
		// {
		//	JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_PARENT_ID', PROPERTY_PARENT_ID);
		// }  // Parent ID Sample
		
		JSCUS_SQL._ADD_FROM
		(
			GET_MAP_ARRAY, 'SEARCH_REC_EODPROD_CAT_PRODUCT_ID_CMB', 'PARAM_REC_EODPROD_CAT_PRODUCT_ID'
		);
		JSCUS_SQL._ADD_FROM
		(
			GET_MAP_ARRAY, 'SEARCH_REC_EODPROD_DATE_FROM_DTP', 'PARAM_REC_EODPROD_DATE_FROM'
		);
		JSCUS_SQL._ADD_FROM
		(
			GET_MAP_ARRAY, 'SEARCH_REC_EODPROD_DATE_TO_DTP', 'PARAM_REC_EODPROD_DATE_TO'
		);
		JSCUS_SQL._ADD_FROM
		(
			GET_MAP_ARRAY, 'SEARCH_REC_EODPROD_QTY_BEGIN_FROM_TXT', 'PARAM_REC_EODPROD_QTY_BEGIN_FROM'
		);
		JSCUS_SQL._ADD_FROM
		(
			GET_MAP_ARRAY, 'SEARCH_REC_EODPROD_QTY_BEGIN_TO_TXT', 'PARAM_REC_EODPROD_QTY_BEGIN_TO'
		);
		JSCUS_SQL._ADD_FROM
		(
			GET_MAP_ARRAY, 'SEARCH_REC_EODPROD_QTY_END_FROM_TXT', 'PARAM_REC_EODPROD_QTY_END_FROM'
		);
		JSCUS_SQL._ADD_FROM
		(
			GET_MAP_ARRAY, 'SEARCH_REC_EODPROD_QTY_END_TO_TXT', 'PARAM_REC_EODPROD_QTY_END_TO'
		);
		JSCUS_SQL._ADD_FROM
		(
			GET_MAP_ARRAY, 'SEARCH_REC_EODPROD_STOCK_IN_FROM_TXT', 'PARAM_REC_EODPROD_STOCK_IN_FROM'
		);
		JSCUS_SQL._ADD_FROM
		(
			GET_MAP_ARRAY, 'SEARCH_REC_EODPROD_STOCK_IN_TO_TXT', 'PARAM_REC_EODPROD_STOCK_IN_TO'
		);
		JSCUS_SQL._ADD_FROM
		(
			GET_MAP_ARRAY, 'SEARCH_REC_EODPROD_STOCK_OUT_FROM_TXT', 'PARAM_REC_EODPROD_STOCK_OUT_FROM'
		);
		JSCUS_SQL._ADD_FROM
		(
			GET_MAP_ARRAY, 'SEARCH_REC_EODPROD_STOCK_OUT_TO_TXT', 'PARAM_REC_EODPROD_STOCK_OUT_TO'
		);
		JSCUS_SQL._ADD_FROM
		(
			GET_MAP_ARRAY, 'SEARCH_REC_EODPROD_STOCK_ADJUSTMENT_FROM_TXT', 'PARAM_REC_EODPROD_STOCK_ADJUSTMENT_FROM'
		);
		JSCUS_SQL._ADD_FROM
		(
			GET_MAP_ARRAY, 'SEARCH_REC_EODPROD_STOCK_ADJUSTMENT_TO_TXT', 'PARAM_REC_EODPROD_STOCK_ADJUSTMENT_TO'
		);
		JSCUS_SQL._ADD_FROM
		(
			GET_MAP_ARRAY, 'SEARCH_REC_EODPROD_USAGE_TXT', 'PARAM_REC_EODPROD_USAGE'
		);	
		JSCUS_SQL._ADD_FROM
		(
			GET_MAP_ARRAY, 'SEARCH_REC_EODPROD_MOVE_DESC_01_TXT', 'PARAM_REC_EODPROD_MOVE_DESC_01'
		);	
		JSCUS_SQL._ADD_FROM
		(
			GET_MAP_ARRAY, 'SEARCH_REC_EODPROD_MOVE_DESC_02_TXT', 'PARAM_REC_EODPROD_MOVE_DESC_02'
		);	
		JSCUS_SQL._ADD_FROM
		(
			GET_MAP_ARRAY, 'SEARCH_REC_EODPROD_MOVE_DESC_03_TXT', 'PARAM_REC_EODPROD_MOVE_DESC_03'
		);	
		JSCUS_SQL._ADD_FROM
		(
			GET_MAP_ARRAY, 'SEARCH_REC_EODPROD_MOVE_DESC_04_TXT', 'PARAM_REC_EODPROD_MOVE_DESC_04'
		);	
		JSCUS_SQL._ADD_FROM
		(
			GET_MAP_ARRAY, 'SEARCH_REC_EODPROD_MOVE_DESC_05_TXT', 'PARAM_REC_EODPROD_MOVE_DESC_05'
		);	
		JSCUS_SQL._ADD_FROM
		(
			GET_MAP_ARRAY, 'SEARCH_REC_EODPROD_MOVE_QTY_01_FROM_TXT', 'PARAM_REC_EODPROD_MOVE_QTY_01_FROM'
		);
		JSCUS_SQL._ADD_FROM
		(
			GET_MAP_ARRAY, 'SEARCH_REC_EODPROD_MOVE_QTY_01_TO_TXT', 'PARAM_REC_EODPROD_MOVE_QTY_01_TO'
		);
		JSCUS_SQL._ADD_FROM
		(
			GET_MAP_ARRAY, 'SEARCH_REC_EODPROD_MOVE_QTY_02_FROM_TXT', 'PARAM_REC_EODPROD_MOVE_QTY_02_FROM'
		);
		JSCUS_SQL._ADD_FROM
		(
			GET_MAP_ARRAY, 'SEARCH_REC_EODPROD_MOVE_QTY_02_TO_TXT', 'PARAM_REC_EODPROD_MOVE_QTY_02_TO'
		);
		JSCUS_SQL._ADD_FROM
		(
			GET_MAP_ARRAY, 'SEARCH_REC_EODPROD_MOVE_QTY_03_FROM_TXT', 'PARAM_REC_EODPROD_MOVE_QTY_03_FROM'
		);
		JSCUS_SQL._ADD_FROM
		(
			GET_MAP_ARRAY, 'SEARCH_REC_EODPROD_MOVE_QTY_03_TO_TXT', 'PARAM_REC_EODPROD_MOVE_QTY_03_TO'
		);
		JSCUS_SQL._ADD_FROM
		(
			GET_MAP_ARRAY, 'SEARCH_REC_EODPROD_MOVE_QTY_04_FROM_TXT', 'PARAM_REC_EODPROD_MOVE_QTY_04_FROM'
		);
		JSCUS_SQL._ADD_FROM
		(
			GET_MAP_ARRAY, 'SEARCH_REC_EODPROD_MOVE_QTY_04_TO_TXT', 'PARAM_REC_EODPROD_MOVE_QTY_04_TO'
		);
		JSCUS_SQL._ADD_FROM
		(
			GET_MAP_ARRAY, 'SEARCH_REC_EODPROD_MOVE_QTY_05_FROM_TXT', 'PARAM_REC_EODPROD_MOVE_QTY_05_FROM'
		);
		JSCUS_SQL._ADD_FROM
		(
			GET_MAP_ARRAY, 'SEARCH_REC_EODPROD_MOVE_QTY_05_TO_TXT', 'PARAM_REC_EODPROD_MOVE_QTY_05_TO'
		);
		JSCUS_SQL._ADD_FROM
		(
			GET_MAP_ARRAY, 'SEARCH_REC_EODPROD_STATUS_CMB', 'PARAM_REC_EODPROD_STATUS'
		);
		JSCUS_SQL._ADD_FROM
		(
			GET_MAP_ARRAY, 'SEARCH_CAT_PRODUCT_HAS_RECIPE_CMB', 'PARAM_CAT_PRODUCT_HAS_RECIPE'
		);
		JSCUS_SQL._ADD_FROM
		(
			GET_MAP_ARRAY, 'SEARCH_ORDER_BY_CMB', 'PARAM_ORDER_FIELD'
		);
		JSCUS_SQL._ADD_FROM
		(
			GET_MAP_ARRAY, 'SEARCH_LIMIT_CMB', 'PARAM_LIMIT'
		);		
		
		var SELECTED_SEARCH_MODE = 0;
		if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("SEARCH_MODE_CMB") == true)
		{
			SELECTED_SEARCH_MODE = JSVC_NUM.VAR_NVALL(JSOC_CMB.VAR_SELECTED_VALUE("SEARCH_MODE_CMB"));		
		}						
		JSOC_DIALOG.VAR_LOADING_SPINNER(true);
		
		
		// Search Inserted / Saved Record
		// _______________________________________________________________________
		JSCUS_SQL._RECORD._EOD_PRODUCT.CUS_GET_ALL
		(
			GET_MAP_ARRAY,
			HTTP_SEARCH_CALLBACK,
			true
		); 							
		
		// Update Paging Cache
		// _______________________________________________________________________
		EOD_PRODUCT_LAST_GET_MAP_ARRAY = GET_MAP_ARRAY;
	};
	
	
	// ===============================================================
	// Request Search - PART.A1
	// ===============================================================
	function HTTP_SEARCH_LOAD_MORE()
	{
		// Abort if no GET cache is found
		// _______________________________________________________________________		
		if(JSVC_VAR.VAR_ISSET(EOD_PRODUCT_LAST_GET_MAP_ARRAY) == false)
		{
			return;
		}
		
		// Update Paging row count
		// _______________________________________________________________________	
		EOD_PRODUCT_LAST_GET_MAP_ARRAY['PARAM_SKIP'] = 
			JSVC_NUM.VAR_NVALL(EOD_PRODUCT_LAST_GET_MAP_ARRAY['PARAM_SKIP']) + 
			JSVC_NUM.VAR_NVALL(EOD_PRODUCT_LAST_GET_MAP_ARRAY['PARAM_LIMIT']);	
			
		if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("SEARCH_LIMIT_CMB") == true)
		{
			JSVC_MAP_ARRAY.VAR_SET
			(
				EOD_PRODUCT_LAST_GET_MAP_ARRAY,
				'PARAM_LIMIT', 
				JSOC_CMB.VAR_SELECTED_VALUE("SEARCH_LIMIT_CMB")
			);		
		}
		
		
		// REGULAR Search Mode
		// _______________________________________________________________________
		JSOC_DIALOG.VAR_LOADING_SPINNER(true);
		JSCUS_SQL._RECORD._EOD_PRODUCT.CUS_GET
		(
			EOD_PRODUCT_LAST_GET_MAP_ARRAY,
			function (IS_SUCCESS, RESPONSE_STRING)
			{
				HTTP_SEARCH_CALLBACK(IS_SUCCESS, RESPONSE_STRING, true);
			},
			true
		);
	}
	
	
	// ===============================================================
	// Request Search - PART.A2
	// ===============================================================
	function HTTP_SEARCH_LOAD_PAGE()
	{
		// Abort if no GET cache is found
		// _______________________________________________________________________		
		if
		(
			JSVC_VAR.VAR_ISSET(EOD_PRODUCT_LAST_GET_MAP_ARRAY) == false ||
			JSOC_CMB.VAR_SELECTED_INDEX("LOAD_PAGING_CMB") < 0
		)
		{
			return;
		}
		
		// Update Paging row count
		// _______________________________________________________________________
		EOD_PRODUCT_LAST_GET_MAP_ARRAY['PARAM_SKIP'] = 
		JSVC_NUM.VAR_NVALL(EOD_PRODUCT_LAST_GET_MAP_ARRAY['PARAM_LIMIT']) *
		JSOC_CMB.VAR_SELECTED_INDEX("LOAD_PAGING_CMB");	
		
		// REGULAR Search Mode
		// _______________________________________________________________________
		LOAD_PAGING_IGNORE_NEXT_REQUEST = true;
		JSOC_DIALOG.VAR_LOADING_SPINNER(true);
		JSCUS_SQL._RECORD._EOD_PRODUCT.CUS_GET_ALL
		(
			EOD_PRODUCT_LAST_GET_MAP_ARRAY,
			function (IS_SUCCESS, RESPONSE_STRING)
			{
				HTTP_SEARCH_CALLBACK(IS_SUCCESS, RESPONSE_STRING, false);
			},
			true
		);
	}
	function HTTP_SEARCH_LOAD_PAGING_UPDATE(ROW_COUNT)
	{
		// Abort if no paging function enabled
		// _______________________________________________________________________
		if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("LOAD_PAGING_CMB") == false || LOAD_PAGING_IGNORE_NEXT_REQUEST == true)
		{
			LOAD_PAGING_IGNORE_NEXT_REQUEST = false;
			return;
		}
		
		// Initialize
		// _______________________________________________________________________
		var PAGING_INDEX = 0;
		var PAGING_COUNT = 
			ROW_COUNT / JSVC_NUM.VAR_NVALL(EOD_PRODUCT_LAST_GET_MAP_ARRAY['PARAM_LIMIT']);
		PAGING_COUNT = Math.trunc(PAGING_COUNT) + 1;
		JSOC_CMB.VAR_CLEAR("LOAD_PAGING_CMB");
		
		// Populate Paging
		// _______________________________________________________________________
		while(PAGING_COUNT > PAGING_INDEX)
		{
			JSOC_CMB.VAR_ADD
			(
				"LOAD_PAGING_CMB", 
				(PAGING_INDEX + 1).toString(), 
				(PAGING_INDEX + 1).toString()
			);
			PAGING_INDEX ++;
		}
	}
	
	
	// ===============================================================
	// Request Search - PART.B
	// ===============================================================
	function HTTP_SEARCH_CALLBACK(IS_SUCCESS, RESPONSE_STRING, KEEP_DGV_ITEMS = false)
	{
		// 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;
		}
		
		// Back to DGV Top
		// _______________________________________________________________________	
		if(LOAD_PAGING_IGNORE_NEXT_REQUEST == true)
		{
			JSOC_ELEMENT.VAR_GET("EOD_PRODUCT_DGV").scrollIntoView
			(
				{
			  		behavior: 'auto', 
			  		block: 'start'
				}
			);		
		}
		
		// JSON Empty (Clears the DataGrodView)
		// _______________________________________________________________________
		if(JSRC_JSON.REF_READ(JSON_OBJECT, "JSON_COUNT") <= 0)
		{
			HTTP_SEARCH_LOAD_PAGING_UPDATE(0);
			EOD_PRODUCT_DGV.ROW.CLEAR();
			if(EOD_PRODUCT_DGV.COLUMN.TOTAL_IS_READY())
			{
				EOD_PRODUCT_DGV.COLUMN.TOTAL_REFRESH();
			}
			return;
		}
		HTTP_SEARCH_LOAD_PAGING_UPDATE
		(
			JSRC_JSON.REF_READ_NVALL(JSON_OBJECT, "RECORD_COUNT", 1)
		);
		
		// Populate DGV
		// _______________________________________________________________________
		EOD_PRODUCT_DGV.POPULATE_FROM_QUERY
		(
				JSON_OBJECT, 
				'',
				KEEP_DGV_ITEMS,
				false,
				EOD_PRODUCT_LAST_GET_MAP_ARRAY['PARAM_SKIP'] + 1,
				[2, 1]
		);
		
		
		// For Applying CFormat
		// _______________________________________________________________________	
		EOD_PRODUCT_DGV.COLUMN.CFORMAT("REC_EODPROD_QTY_BEGIN");
		EOD_PRODUCT_DGV.COLUMN.CFORMAT("REC_EODPROD_QTY_END");
		EOD_PRODUCT_DGV.COLUMN.CFORMAT("REC_EODPROD_STOCK_IN");
		EOD_PRODUCT_DGV.COLUMN.CFORMAT("REC_EODPROD_STOCK_OUT");
		EOD_PRODUCT_DGV.COLUMN.CFORMAT("REC_EODPROD_STOCK_ADJUSTMENT");
		EOD_PRODUCT_DGV.COLUMN.CFORMAT("REC_EODPROD_MOVE_QTY_01");
		EOD_PRODUCT_DGV.COLUMN.CFORMAT("REC_EODPROD_MOVE_QTY_02");
		EOD_PRODUCT_DGV.COLUMN.CFORMAT("REC_EODPROD_MOVE_QTY_03");
		EOD_PRODUCT_DGV.COLUMN.CFORMAT("REC_EODPROD_MOVE_QTY_04");
		EOD_PRODUCT_DGV.COLUMN.CFORMAT("REC_EODPROD_MOVE_QTY_05");
		
		
		// Initialize
		// _______________________________________________________________________	
		var WEBTABLE_DATE = null;
		var CURRENT_CELL_VALUE = null;
		var ROW_INDEX = 0;
		
		
		// Color Coding (Uncomment to use)
		// _______________________________________________________________________	
		//EOD_PRODUCT_DGV.CELL.ELEMENT("COLUMN_NAME", ROW_INDEX).style.color = "#1cc88a"; // Safe
		//EOD_PRODUCT_DGV.CELL.ELEMENT("COLUMN_NAME", ROW_INDEX).style.color = "#5a5c69"; // Neutral
		//EOD_PRODUCT_DGV.CELL.ELEMENT("COLUMN_NAME", ROW_INDEX).style.color = "#f6c23e"; // Caution
		//EOD_PRODUCT_DGV.CELL.ELEMENT("COLUMN_NAME", ROW_INDEX).style.color = "#e74a3b"; // Warning


		// Abort if no GET cache is found
		// _______________________________________________________________________		
		if
		(
			JSVC_VAR.VAR_ISSET(EOD_PRODUCT_LAST_GET_MAP_ARRAY) == false ||
			JSOC_CMB.VAR_SELECTED_INDEX("LOAD_PAGING_CMB") < 0
		)
		{
			return;
		}
		
		
		// Update Paging row count
		// _______________________________________________________________________
		EOD_PRODUCT_LAST_GET_MAP_ARRAY['PARAM_SKIP'] = 
		JSVC_NUM.VAR_NVALL(EOD_PRODUCT_LAST_GET_MAP_ARRAY['PARAM_LIMIT']) *
		JSOC_CMB.VAR_SELECTED_INDEX("LOAD_PAGING_CMB");	
		
		
		// Fetch next 500 to prevent unlinked child due to query limit
		// _______________________________________________________________________
		JSOC_DIALOG.VAR_LOADING_SPINNER(true);
		var GET_MAP_ARRAY = JSVC_ARR.VAR_CLONE(EOD_PRODUCT_LAST_GET_MAP_ARRAY, true);

		GET_MAP_ARRAY['PARAM_SKIP'] = 
			JSVC_NUM.VAR_NVALL(EOD_PRODUCT_LAST_GET_MAP_ARRAY['PARAM_LIMIT']) + 
			JSVC_NUM.VAR_NVALL(EOD_PRODUCT_LAST_GET_MAP_ARRAY['PARAM_SKIP']);
			
		// console.log(EOD_PRODUCT_LAST_GET_MAP_ARRAY);
		// console.log(GET_MAP_ARRAY);
				
		JSCUS_SQL._RECORD._EOD_PRODUCT.CUS_GET_ALL
		(
			GET_MAP_ARRAY,
			function (IS_SUCCESS, RESPONSE_STRING)
			{
				HTTP_SEARCH_EXTEND_CALLBACK(IS_SUCCESS, RESPONSE_STRING);
			},
			true
		);
	};

	// Populate only the records with same Product ID as last DGV row item in prev. query.
	function HTTP_SEARCH_EXTEND_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 (Skip to Auto-total tool)
		// _______________________________________________________________________
		if(JSRC_JSON.REF_READ(JSON_OBJECT, "JSON_COUNT") <= 0)
		{
			if(EOD_PRODUCT_DGV.COLUMN.TOTAL_IS_READY())
			{
				EOD_PRODUCT_DGV.COLUMN.TOTAL_REFRESH();
			}
			FORM_GROUP_RESULT();	
			return;
		}		
		
		
		// Check Last Product ID
		// _______________________________________________________________________	
		var CURRENT_PRODUCT_ID = EOD_PRODUCT_DGV.CELL.VALUE_NVALL("REC_EODPROD_CAT_PRODUCT_ID", EOD_PRODUCT_DGV.ROW.COUNT() - 1);
		var JSON_COUNT = JSRC_JSON.REF_READ_NVALL(JSON_OBJECT, "JSON_COUNT");
		var JSON_INDEX = 1;
		var ROW_INDEX = 0;
		
		// Populate Continuation of last product
		while
		(
			JSON_COUNT > JSON_INDEX && 
			JSRC_JSON.REF_READ_NVALL(JSON_OBJECT, "REC_EODPROD_CAT_PRODUCT_ID", JSON_INDEX) == CURRENT_PRODUCT_ID
		)
		{
			EOD_PRODUCT_DGV.ROW.ADD();
			ROW_INDEX = EOD_PRODUCT_DGV.ROW.COUNT() - 1;
			
			EOD_PRODUCT_DGV.CELL.VALUE_APPLY_DATATYPE
			(
				EOD_PRODUCT_DGV.COLUMN.INDEX("REC_EODPROD_CAT_PRODUCT_ID"), ROW_INDEX,
				JSRC_JSON.REF_READ_NVALL(JSON_OBJECT, "REC_EODPROD_CAT_PRODUCT_ID", JSON_INDEX)
			);
			EOD_PRODUCT_DGV.CELL.VALUE_APPLY_DATATYPE
			(
				EOD_PRODUCT_DGV.COLUMN.INDEX("CAT_PRODUCT_DESCRIPTION"), ROW_INDEX,
				JSRC_JSON.REF_READ(JSON_OBJECT, "CAT_PRODUCT_DESCRIPTION", JSON_INDEX)
			);			
			EOD_PRODUCT_DGV.CELL.VALUE_APPLY_DATATYPE
			(
				EOD_PRODUCT_DGV.COLUMN.INDEX("REC_EODPROD_DATE"), ROW_INDEX,
				JSRC_JSON.REF_READ_TO_DATE(JSON_OBJECT, "REC_EODPROD_DATE", JSON_INDEX)
			);				
			EOD_PRODUCT_DGV.CELL.VALUE_APPLY_DATATYPE
			(
				EOD_PRODUCT_DGV.COLUMN.INDEX("REC_EODPROD_QTY_BEGIN"), ROW_INDEX,
				JSRC_JSON.REF_READ_NVAL(JSON_OBJECT, "REC_EODPROD_QTY_BEGIN", JSON_INDEX)
			);				
			EOD_PRODUCT_DGV.CELL.VALUE_APPLY_DATATYPE
			(
				EOD_PRODUCT_DGV.COLUMN.INDEX("REC_EODPROD_STOCK_IN"), ROW_INDEX,
				JSRC_JSON.REF_READ_NVAL(JSON_OBJECT, "REC_EODPROD_STOCK_IN", JSON_INDEX)
			);	
			EOD_PRODUCT_DGV.CELL.VALUE_APPLY_DATATYPE
			(
				EOD_PRODUCT_DGV.COLUMN.INDEX("REC_EODPROD_STOCK_OUT"), ROW_INDEX,
				JSRC_JSON.REF_READ_NVAL(JSON_OBJECT, "REC_EODPROD_STOCK_OUT", JSON_INDEX)
			);				
			EOD_PRODUCT_DGV.CELL.VALUE_APPLY_DATATYPE
			(
				EOD_PRODUCT_DGV.COLUMN.INDEX("REC_EODPROD_USAGE"), ROW_INDEX,
				JSRC_JSON.REF_READ(JSON_OBJECT, "REC_EODPROD_USAGE", JSON_INDEX)
			);						
			EOD_PRODUCT_DGV.CELL.VALUE_APPLY_DATATYPE
			(
				EOD_PRODUCT_DGV.COLUMN.INDEX("REC_EODPROD_QTY_END"), ROW_INDEX,
				JSRC_JSON.REF_READ_NVALL(JSON_OBJECT, "REC_EODPROD_QTY_END", JSON_INDEX)
			);		
			EOD_PRODUCT_DGV.CELL.VALUE_APPLY_DATATYPE
			(
				EOD_PRODUCT_DGV.COLUMN.INDEX("REC_EODPROD_STOCK_ADJUSTMENT"), ROW_INDEX,
				JSRC_JSON.REF_READ_NVALL(JSON_OBJECT, "REC_EODPROD_STOCK_ADJUSTMENT", JSON_INDEX)
			);					

			EOD_PRODUCT_DGV.COLUMN.AUTO_COMPUTE_EXECUTE(ROW_INDEX);
			JSON_INDEX ++;
		}
		
		// Auto-total tool
		// _______________________________________________________________________	
		if(EOD_PRODUCT_DGV.COLUMN.TOTAL_IS_READY())
		{
			EOD_PRODUCT_DGV.COLUMN.TOTAL_REFRESH();
		}
		FORM_GROUP_RESULT();		
		// EOD_PRODUCT_DGV.COLUMN.FILTER_MATH('`REC_EODPROD_STOCK_IN` > 0 AND `REC_EODPROD_STOCK_OUT` > 0');
	};
				
	// ===============================================================
	// Begin Processing the Page
	// ===============================================================
	FORM_INITIALIZE();
	FORM_LOAD();