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


	// ===============================================================
	// Initialize Form
	// ===============================================================
	function FORM_LOAD()
	{
		// Initialize
		// _______________________________________________________________________	
		var DGV_ELEMENT_CHILD = null;
		var DGV_ELEMENT_CHILD_INDEX = 0;
		
		
		// Load DataGridVuew
		// _______________________________________________________________________	
		RELOAD_DGV = DataGridView("RELOAD_DGV");
		//RELOAD_DGV.SETTING_ALLOW_USER_TO_ADD_ROWS = true;
		//RELOAD_DGV.SETTING_EDITABLE = true;
			
		// Show Progress
		// _______________________________________________________________________	
		JSOC_DIALOG.VAR_LOADING_SPINNER(true);
				
		// Requires Authentication
		// _______________________________________________________________________	
		PAGE_AUTH_REQUIRES_LOGIN(HTTP_POPULATE_RESULT);			
		
		// Status		
		// _______________________________________________________________________	
		if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("SEARCH_REC_RELOAD_STATUS_CMB") == true)
		{
			JSOC_CMB.VAR_CLEAR("SEARCH_REC_RELOAD_STATUS_CMB");
			JSOC_CMB.VAR_ADD("SEARCH_REC_RELOAD_STATUS_CMB", "0","Any Status");
			JSOC_CMB.VAR_ADD("SEARCH_REC_RELOAD_STATUS_CMB", "1","Inactive");
			JSOC_CMB.VAR_ADD("SEARCH_REC_RELOAD_STATUS_CMB", "2","Active");
			// JSOC_CMB.VAR_ADD("SEARCH_REC_RELOAD_STATUS_CMB", "3","Closed");
			// JSOC_CMB.VAR_ADD("SEARCH_REC_RELOAD_STATUS_CMB", "4","Completed");
			JSOC_CMB.VAR_SELECTED_VALUE("SEARCH_REC_RELOAD_STATUS_CMB", "2");
		}			
		
		if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("REC_RELOAD_STATUS_CMB") == true)
		{
			JSOC_CMB.VAR_CLEAR("REC_RELOAD_STATUS_CMB");
			JSOC_CMB.VAR_ADD("REC_RELOAD_STATUS_CMB", "1","Inactive");
			JSOC_CMB.VAR_ADD("REC_RELOAD_STATUS_CMB", "2","Active");
			// JSOC_CMB.VAR_ADD("REC_RELOAD_STATUS_CMB", "3","Closed");
			// JSOC_CMB.VAR_ADD("REC_RELOAD_STATUS_CMB", "4","Completed");
			JSOC_CMB.VAR_SELECTED_VALUE("REC_RELOAD_STATUS_CMB", "2");
		}
		
		
		
		// Get Modal Instance (Add Modal Adjustment before this line)
		// _______________________________________________________________________	
		if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("RELOAD_MODAL") == true)
		{
			INPUT_INSTANCE = JSOC_WINDOW.VAR_INPUT_INSTANCE_CLONE("RELOAD_MODAL");
		}
		
		// Add Search Events for the SEARCH_DESCRIPTION_TXT - TextInput
		// _______________________________________________________________________		
		if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("SEARCH_REC_RELOAD_CODE_TXT"))
		{
			var SEARCH_REC_RELOAD_CODE_TXT = JSOC_ELEMENT.VAR_GET("SEARCH_REC_RELOAD_CODE_TXT");
			SEARCH_REC_RELOAD_CODE_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();
						}
					};
				}();
		}
		
	
		// 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(NEW_ITEM_ID, EDITED_ROW_INDEX)
	{
		// Optional Value
		// _______________________________________________________________________
		NEW_ITEM_ID = JSVC_VAR.VAR_OPTIONAL(NEW_ITEM_ID, -1);
		EDITED_ROW_INDEX = JSVC_VAR.VAR_OPTIONAL(EDITED_ROW_INDEX, -1);
		
		// Parameter Validate
		// _______________________________________________________________________
		JSVC_AV.VAR_VALIDATE_ARGUMENT_TYPE(NEW_ITEM_ID, 'NEW_ITEM_ID', 'NUMERIC');
		JSVC_AV.VAR_VALIDATE_ARGUMENT_TYPE(EDITED_ROW_INDEX, 'EDITED_ROW_INDEX', 'NUMERIC');
		
		
		// Actual Function
		// _______________________________________________________________________
		GET_MAP_ARRAY = JSCUS_SQL._MOBILE._SMS.CUS_PREPARE_MAPPED_ARRAY_FOR_BILL();	
	
		if
		(
			JSOC_ELEMENT.VAR_ELEMENT_EXISTS("SEARCH_REC_RELOAD_CODE_TXT") == true &&
			JSVC_STR.VAR_IS_BLANK( JSOC_ELEMENT.VAR_VALUE("SEARCH_REC_RELOAD_CODE_TXT")) == false
		)
		{
			JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_REC_RELOAD_CODE', JSOC_ELEMENT.VAR_VALUE("SEARCH_REC_RELOAD_CODE_TXT"));
		}
	
		if
		(
			JSOC_ELEMENT.VAR_ELEMENT_EXISTS("SEARCH_REC_RELOAD_DATE_TRANSACT_FROM_DTP") == true &&
			JSOC_STYLE.VAR_IS_VISIBLE('SEARCH_REC_RELOAD_DATE_TRANSACT_FROM_DTP') == true &&
			JSVC_STR.VAR_IS_BLANK(JSOC_ELEMENT.VAR_VALUE("SEARCH_REC_RELOAD_DATE_TRANSACT_FROM_DTP")) == false
		)
		{
			JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_REC_RELOAD_DATE_TRANSACT_FROM', JSVC_DATE.VAR_FORMAT(JSOC_ELEMENT.VAR_VALUE("SEARCH_REC_RELOAD_DATE_TRANSACT_FROM_DTP"), "YYYY-MM-DD") + " 00:00:00.000000");
		}
		if
		(
			JSOC_ELEMENT.VAR_ELEMENT_EXISTS("SEARCH_REC_RELOAD_DATE_TRANSACT_TO_DTP") == true &&
			JSOC_STYLE.VAR_IS_VISIBLE('SEARCH_REC_RELOAD_DATE_TRANSACT_TO_DTP') == true &&
			JSVC_STR.VAR_IS_BLANK(JSOC_ELEMENT.VAR_VALUE("SEARCH_REC_RELOAD_DATE_TRANSACT_TO_DTP")) == false
		)
		{
			JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_REC_RELOAD_DATE_TRANSACT_TO', JSVC_DATE.VAR_FORMAT(JSOC_ELEMENT.VAR_VALUE("SEARCH_REC_RELOAD_DATE_TRANSACT_TO_DTP"), "YYYY-MM-DD") + " 23:59:59.999999");
		}
		
		if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("SEARCH_REC_RELOAD_HAS_BILL_CMB") == true)
		{
			JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_REC_RELOAD_HAS_BILL', JSOC_CMB.VAR_SELECTED_VALUE("SEARCH_REC_RELOAD_HAS_BILL_CMB"));
		}
		
		if
		(
			JSOC_ELEMENT.VAR_ELEMENT_EXISTS("SEARCH_REC_RELOAD_REFERENCE_CODE_TXT") == true &&
			JSVC_STR.VAR_IS_BLANK( JSOC_ELEMENT.VAR_VALUE("SEARCH_REC_RELOAD_REFERENCE_CODE_TXT")) == false
		)
		{
			JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_REC_RELOAD_REFERENCE_CODE', JSOC_ELEMENT.VAR_VALUE("SEARCH_REC_RELOAD_REFERENCE_CODE_TXT"));
		}
		
		if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("SEARCH_ORDER_BY_CMB") == true)
		{
			var SELECTED_VAL = JSOC_CMB.VAR_SELECTED_VALUE("SEARCH_ORDER_BY_CMB");
			if(JSVC_STR.VAR_CONTAINS_RIGHT(SELECTED_VAL, '_ASC'))
			{
				JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_ORDER_FIELD', JSVC_STR.VAR_DRIGHT(SELECTED_VAL, 4));
				JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_ORDER_TYPE', 'ASC');		
			}
			else
			{
				JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_ORDER_FIELD', SELECTED_VAL);
				JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_ORDER_TYPE', 'DESC');
			}
		}	
		if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("SEARCH_LIMIT_CMB") == true)
		{
			JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_LIMIT', JSOC_CMB.VAR_SELECTED_VALUE("SEARCH_LIMIT_CMB"));		
		}			
		
		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);
		
						
		// REGULAR Search Mode
		// _______________________________________________________________________
		RELOAD_LAST_GET_MAP_ARRAY = GET_MAP_ARRAY;
		JSCUS_SQL._MOBILE._SMS.CUS_BILL
		(
			GET_MAP_ARRAY,
			HTTP_SEARCH_CALLBACK,
			true
		); 							
	};
	
	
	// ===============================================================
	// Request Search - PART.A1
	// ===============================================================
	function HTTP_SEARCH_LOAD_MORE()
	{
		// Abort if no GET cache is found
		// _______________________________________________________________________		
		if(JSVC_VAR.VAR_ISSET(RELOAD_LAST_GET_MAP_ARRAY) == false)
		{
			return;
		}
		
		// Update Paging row count
		// _______________________________________________________________________	
		RELOAD_LAST_GET_MAP_ARRAY['PARAM_SKIP'] = 
			JSVC_NUM.VAR_NVALL(RELOAD_LAST_GET_MAP_ARRAY['PARAM_SKIP']) + 
			JSVC_NUM.VAR_NVALL(RELOAD_LAST_GET_MAP_ARRAY['PARAM_LIMIT']);	
			
		if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("SEARCH_LIMIT_CMB") == true)
		{
			JSVC_MAP_ARRAY.VAR_SET
			(
				RELOAD_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._RELOAD.CUS_GET
		(
			RELOAD_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(RELOAD_LAST_GET_MAP_ARRAY) == false ||
			JSOC_CMB.VAR_SELECTED_INDEX("LOAD_PAGING_CMB") < 0
		)
		{
			return;
		}
		
		// Update Paging row count
		// _______________________________________________________________________
		RELOAD_LAST_GET_MAP_ARRAY['PARAM_SKIP'] = 
		JSVC_NUM.VAR_NVALL(RELOAD_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._RELOAD.CUS_GET
		(
			RELOAD_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(RELOAD_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;
		}
		
		// JSON Empty (Clears the DataGrodView)
		// _______________________________________________________________________
		if(JSRC_JSON.REF_READ(JSON_OBJECT, "JSON_COUNT") <= 0)
		{
			HTTP_SEARCH_LOAD_PAGING_UPDATE(0);
			RELOAD_DGV.ROW.CLEAR();
			return;
		}
		HTTP_SEARCH_LOAD_PAGING_UPDATE
		(
			JSVC_NUM.VAR_NVALL
			(
				JSRC_JSON.REF_READ(JSON_OBJECT, "RECORD_COUNT", 1)
			)
		);
		
		// Populate DGV
		// _______________________________________________________________________
		RELOAD_DGV.POPULATE_FROM_QUERY
		(
				JSON_OBJECT, 
				'',
				KEEP_DGV_ITEMS,
				false,
				1,
				[2, 1]
		);
		
		
		// For Applying CFormat
		// _______________________________________________________________________	
		RELOAD_DGV.COLUMN.CFORMAT("REC_RELOAD_PRICE_SMS");
		RELOAD_DGV.COLUMN.CFORMAT("REC_RELOAD_PRICE_TOTAL");
		
		
		// Initialize
		// _______________________________________________________________________	
		var WEBTABLE_DATE = null;
		var CURRENT_CELL_VALUE = null;
		var ROW_INDEX = 0;
	
	
		// DateTime Format
		// _______________________________________________________________________	
		if(RELOAD_DGV.COLUMN.IS_EXISTS("REC_RELOAD_DATE_TRANSACT"))
		{
			WEBTABLE_DATE = null;
			ROW_INDEX = 0;
			while(RELOAD_DGV.ROW.COUNT() > ROW_INDEX)
			{
				// Get Value
				// ...........................................
				CURRENT_CELL_VALUE = RELOAD_DGV.CELL.VALUE
					(
						"REC_RELOAD_DATE_TRANSACT", 
						ROW_INDEX
					);
				
				// Read Current Value
				// ...........................................
				if(JSVC_STR.VAR_IS_BLANK(CURRENT_CELL_VALUE) == false)
				{
					WEBTABLE_DATE = JSVC_DATE.VAR_TO_DATE(CURRENT_CELL_VALUE);			
				}
				else
				{
					WEBTABLE_DATE = null;
				}
				
				// Apply CFormat, and DateFormat
				// ...........................................
				if(JSVC_VAR.VAR_ISSET(WEBTABLE_DATE))
				{
					RELOAD_DGV.CELL.VALUE
					(
							"REC_RELOAD_DATE_TRANSACT", ROW_INDEX, 
							JSVC_DATE.VAR_FORMAT(WEBTABLE_DATE, "MMM DD, YYYY")
					);				
				}			
				
				// Color Coding (Uncomment to use)
				// ...........................................	
				//RELOAD_DGV.CELL.ELEMENT("COLUMN_NAME", ROW_INDEX).style.color = "#1cc88a"; // Safe
				//RELOAD_DGV.CELL.ELEMENT("COLUMN_NAME", ROW_INDEX).style.color = "#5a5c69"; // Neutral
				//RELOAD_DGV.CELL.ELEMENT("COLUMN_NAME", ROW_INDEX).style.color = "#f6c23e"; // Caution
				//RELOAD_DGV.CELL.ELEMENT("COLUMN_NAME", ROW_INDEX).style.color = "#e74a3b"; // Warning
				
				// To next loop
				// ...........................................	
				ROW_INDEX ++;
			}			
		}
		if(RELOAD_DGV.COLUMN.IS_EXISTS("REC_RELOAD_DATE_BILL"))
		{
			WEBTABLE_DATE = null;
			ROW_INDEX = 0;
			while(RELOAD_DGV.ROW.COUNT() > ROW_INDEX)
			{
				// Get Value
				// ...........................................
				CURRENT_CELL_VALUE = RELOAD_DGV.CELL.VALUE
					(
						"REC_RELOAD_DATE_BILL", 
						ROW_INDEX
					);
				
				// Read Current Value
				// ...........................................
				if(JSVC_STR.VAR_IS_BLANK(CURRENT_CELL_VALUE) == false)
				{
					WEBTABLE_DATE = JSVC_DATE.VAR_TO_DATE(CURRENT_CELL_VALUE);			
				}
				else
				{
					WEBTABLE_DATE = null;
				}
				
				// Apply CFormat, and DateFormat
				// ...........................................
				if(JSVC_VAR.VAR_ISSET(WEBTABLE_DATE))
				{
					RELOAD_DGV.CELL.VALUE
					(
							"REC_RELOAD_DATE_BILL", ROW_INDEX, 
							JSVC_DATE.VAR_FORMAT(WEBTABLE_DATE, "MMM DD, YYYY")
					);				
				}			
				
				// Color Coding (Uncomment to use)
				// ...........................................	
				//RELOAD_DGV.CELL.ELEMENT("COLUMN_NAME", ROW_INDEX).style.color = "#1cc88a"; // Safe
				//RELOAD_DGV.CELL.ELEMENT("COLUMN_NAME", ROW_INDEX).style.color = "#5a5c69"; // Neutral
				//RELOAD_DGV.CELL.ELEMENT("COLUMN_NAME", ROW_INDEX).style.color = "#f6c23e"; // Caution
				//RELOAD_DGV.CELL.ELEMENT("COLUMN_NAME", ROW_INDEX).style.color = "#e74a3b"; // Warning
				
				// To next loop
				// ...........................................	
				ROW_INDEX ++;
			}			
		}

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