// 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
		// _______________________________________________________________________	
		POS_RESERVE_DGV = DataGridView("POS_RESERVE_DGV");
		PRODUCT_BRANCH_DGV = DataGridView("PRODUCT_BRANCH_DGV");

		// Show Progress
		// _______________________________________________________________________	
		JSOC_DIALOG.VAR_LOADING_SPINNER(true);
		
		// Requires Authentication
		// _______________________________________________________________________	
		//PAGE_AUTH_REQUIRES_LOGIN(HTTP_POPULATE_RESULT);			
		PAGE_AUTH_REQUIRES_LOGIN(HTTP_POPULATE_RESULT);			
		
		// Foreign Key [Fixed]
		// _______________________________________________________________________	
		if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("REC_POSRESERVE_ACC_GROUP_ID_CMB") == true)
		{
			// Set Default Item, if used in DataGridView
			JSOC_CMB.VAR_ADD("REC_POSRESERVE_ACC_GROUP_ID_CMB", FLAG_BIGINT_NULL.toString(), "Select Branch");
			// To use custom populate
			// var GET_MAP_ARRAY = JSCUS_SQL._ACCOUNT._GROUP.CUS_PREPARE_MAPPED_ARRAY_FOR_GET();
			// JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_', JSCUS_SQL._SYSTEM._STATUS.STATUS_ACTIVE);	
			JSCUS_SQL._ACCOUNT._GROUP.CUS_GET_THEN_POPULATE_COMBOBOX
			(
				function()
				{
					// Set Default index
					JSOC_CMB.VAR_SELECTED_INDEX("REC_POSRESERVE_ACC_GROUP_ID_CMB", 0);
					
					// Clone to DGV Cell if ComboBox Exists
					if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("POS_RESERVE_DGV_ROW_REC_POSRESERVE_ACC_GROUP_ID") == true)
					{
						DGV_ELEMENT_CHILD = JSOC_ELEMENT.VAR_SUB_ELEMENTS("POS_RESERVE_DGV_ROW_REC_POSRESERVE_ACC_GROUP_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_POSRESERVE_ACC_GROUP_ID_CMB")
								);
							}	
							DGV_ELEMENT_CHILD_INDEX ++;
						}
					}
					
					// Copy items to edit control
					if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("SEARCH_REC_POSRESERVE_ACC_GROUP_ID_CMB") == true)
					{
						JSOC_CMB.VAR_CLEAR("SEARCH_REC_POSRESERVE_ACC_GROUP_ID_CMB");
						//JSOC_CMB.VAR_ADD("SEARCH_REC_POSRESERVE_ACC_GROUP_ID_CMB", FLAG_BIGINT_NULL.toString(),"Any Group");
						JSOC_ELEMENT.VAR_INNER_HTML("SEARCH_REC_POSRESERVE_ACC_GROUP_ID_CMB", JSOC_ELEMENT.VAR_INNER_HTML("REC_POSRESERVE_ACC_GROUP_ID_CMB"), true);
						JSOC_CMB.VAR_SELECTED_INDEX("SEARCH_REC_POSRESERVE_ACC_GROUP_ID_CMB", 0);
						$('#SEARCH_REC_POSRESERVE_ACC_GROUP_ID_CMB').select2();
					}				
					
					// Update Modal Settings
					if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("POS_RESERVE_MODAL") == true)
					{
						INPUT_INSTANCE = JSOC_WINDOW.VAR_INPUT_INSTANCE_CLONE("POS_RESERVE_MODAL");
					}
					
					// Forward populate complete event.
					// $('#REC_POSRESERVE_ACC_GROUP_ID_CMB').select2({
					// 	dropdownParent: $('#POS_RESERVE_MODAL')
					// });
					HTTP_POPULATE_RESULT();		
				},
				"REC_POSRESERVE_ACC_GROUP_ID_CMB",
				true, 2
				// , GET_MAP_ARRAY 
			);
		}
		if
		(
			JSOC_ELEMENT.VAR_ELEMENT_EXISTS("SEARCH_REC_POSRESERVE_ACC_GROUP_ID_CMB") == true &&
			JSOC_ELEMENT.VAR_ELEMENT_EXISTS("REC_POSRESERVE_ACC_GROUP_ID_CMB") == false
		)
		{
			JSOC_CMB.VAR_CLEAR("SEARCH_REC_POSRESERVE_ACC_GROUP_ID_CMB");
			//JSOC_CMB.VAR_ADD("SEARCH_REC_POSRESERVE_ACC_GROUP_ID_CMB", FLAG_BIGINT_NULL.toString(),"Any Group");
			// To use custom populate
			// var GET_MAP_ARRAY = JSCUS_SQL._ACCOUNT._GROUP.CUS_PREPARE_MAPPED_ARRAY_FOR_GET();
			// JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_', JSCUS_SQL._SYSTEM._STATUS.STATUS_ACTIVE);	
			JSCUS_SQL._ACCOUNT._GROUP.CUS_GET_THEN_POPULATE_COMBOBOX
			(
				function()
				{
					// Set Default index
					JSOC_CMB.VAR_SELECTED_INDEX("SEARCH_REC_POSRESERVE_ACC_GROUP_ID_CMB", 0);
					
					// Forward populate complete event.		
					$('#SEARCH_REC_POSRESERVE_ACC_GROUP_ID_CMB').select2();
					HTTP_POPULATE_RESULT();
				},		
				"SEARCH_REC_POSRESERVE_ACC_GROUP_ID_CMB",
				true, 2
				// , GET_MAP_ARRAY 
			);
		}
				
		// 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()
	{
		var GET_MAP_ARRAY = JSCUS_SQL._RECORD._JOB_ORDER.CUS_PREPARE_MAPPED_ARRAY_FOR_GET();
		JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_REC_JORDER_DATE_POST_FROM', JSVC_DATE.VAR_FORMAT(JSOC_ELEMENT.VAR_VALUE("SEARCH_REC_POSRESERVE_DATE_RECORDED_FROM_DTP"), "YYYY-MM-DD") + " 00:00:00.000000");
		JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_REC_JORDER_DATE_POST_TO', JSVC_DATE.VAR_FORMAT(JSOC_ELEMENT.VAR_VALUE("SEARCH_REC_POSRESERVE_DATE_RECORDED_TO_DTP"), "YYYY-MM-DD") + " 23:59:59.999999");
		JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_REC_JORDER_STATUS', 2);

		JSOC_DIALOG.VAR_LOADING_SPINNER(true);
		JSCUS_SQL._RECORD._JOB_ORDER.CUS_GET
		(
			GET_MAP_ARRAY,
			HTTP_SEARCH_CALLBACK,
			true
		);
	}
	
	// ===============================================================
	// Request Search - PART.A
	// ===============================================================
	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)
		{
			POS_RESERVE_DGV.ROW.CLEAR();
			return;
		}

		// Populate DGV
		// _______________________________________________________________________
		POS_RESERVE_DGV.POPULATE_FROM_QUERY
		(
				JSON_OBJECT, 
				'',
				false,
				false,
				1,
				[2, 1]
		);
		
		// Initialize
		// _______________________________________________________________________	
		var WEBTABLE_DATE = null;
		var ROW_INDEX = 0;

		while(POS_RESERVE_DGV.ROW.COUNT() > ROW_INDEX)
		{
			if(JSVC_NUM.VAR_NVALL(JSRC_JSON.REF_READ(JSON_OBJECT, "REC_JORDER_DECIMAL_A", ROW_INDEX + 1)) == 2)
				POS_RESERVE_DGV.CELL.VALUE('REC_JORDER_DECIMAL_A_DESCRIPTION', ROW_INDEX, 'Cash');
			else if(JSVC_NUM.VAR_NVALL(JSRC_JSON.REF_READ(JSON_OBJECT, "REC_JORDER_DECIMAL_A", ROW_INDEX + 1)) == 3)
				POS_RESERVE_DGV.CELL.VALUE('REC_JORDER_DECIMAL_A_DESCRIPTION', ROW_INDEX, 'Credit');
			else
				POS_RESERVE_DGV.CELL.VALUE('REC_JORDER_DECIMAL_A_DESCRIPTION', ROW_INDEX, '');

			POS_RESERVE_DGV.CELL.VALUE
			(
				'REC_CLIENT_NAME',
				ROW_INDEX,
				POS_RESERVE_DGV.CELL.VALUE('REC_CLIENT_FIRSTNAME', ROW_INDEX) +
				' ' +
				POS_RESERVE_DGV.CELL.VALUE('REC_CLIENT_LASTNAME', ROW_INDEX)
			);
			
			HTTP_GET_RESERVED_ITEMS
			(
				JSVC_NUM.VAR_NVALL(POS_RESERVE_DGV.CELL.VALUE('REC_JORDER_ID', ROW_INDEX)),
				ROW_INDEX,
				function(IS_SUCCESS, ROW_INDEX, RESPONSE_STRING)
				{
					if(IS_SUCCESS == true)
						POS_RESERVE_DGV.CELL.VALUE('REC_JORDER_TASK_ITEMS', ROW_INDEX, RESPONSE_STRING);
				}
			);

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

					POS_RESERVE_DGV.CELL.VALUE
					(
							"REC_JORDER_DATE_POST_RAW", ROW_INDEX, 
							JSVC_DATE.VAR_FORMAT(WEBTABLE_DATE, "YYYY-MM-DD HH:mm:ss") + '.999999'
					);				
				}			
				
				// Color Coding (Uncomment to use)
				// ...........................................	
				//POS_RESERVE_DGV.CELL.ELEMENT("COLUMN_NAME", ROW_INDEX).style.color = "#1cc88a"; // Safe
				//POS_RESERVE_DGV.CELL.ELEMENT("COLUMN_NAME", ROW_INDEX).style.color = "#5a5c69"; // Neutral
				//POS_RESERVE_DGV.CELL.ELEMENT("COLUMN_NAME", ROW_INDEX).style.color = "#f6c23e"; // Caution
				//POS_RESERVE_DGV.CELL.ELEMENT("COLUMN_NAME", ROW_INDEX).style.color = "#e74a3b"; // Warning
				
				// To next loop
				// ...........................................	
				ROW_INDEX ++;
			}			
		}
	};

	function HTTP_GET_RESERVED_ITEMS(POSRESERVE_ID, ROW_INDEX, CALLBACK_METHOD)
	{
		var HTML = '';

		var GET_MAP_ARRAY = JSCUS_SQL._RECORD._JOB_ORDER_TASK.CUS_PREPARE_MAPPED_ARRAY_FOR_GET();
		JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_REC_JORDER_TASK_LONG_A_FROM', JSVC_NUM.VAR_NVALL(POSRESERVE_ID));
		JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_REC_JORDER_TASK_LONG_A_TO', JSVC_NUM.VAR_NVALL(POSRESERVE_ID));
		JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_REC_JORDER_TASK_DECIMAL_A_FROM', 1);
		JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_REC_JORDER_TASK_DECIMAL_A_TO', 1);

		var RESPONSE_HANDLER = 			
			function(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)
				{
					CALLBACK_METHOD(false);
				}

				var INDEX = 1;
				var JSON_COUNT = JSVC_NUM.VAR_NVALL(JSRC_JSON.REF_READ(JSON_OBJECT, "JSON_COUNT"));

				while(JSON_COUNT >= INDEX)
				{
					HTML += JSVC_NUM.VAR_CFORMAT(JSRC_JSON.REF_READ(JSON_OBJECT, "REC_JORDER_TASK_QTY_TARGET", INDEX)) + " X " + JSRC_JSON.REF_READ(JSON_OBJECT, "REC_JORDER_TASK_DESCRIPTION", INDEX) + "<br>";

					INDEX++;
				}

				CALLBACK_METHOD(true, ROW_INDEX, HTML);
			};

		
		JSCUS_SQL._RECORD._JOB_ORDER_TASK.CUS_GET
		(
			GET_MAP_ARRAY,
			RESPONSE_HANDLER,
			true
		);	
	}
				
	// ===============================================================
	// Begin Processing the Page
	// ===============================================================
	FORM_INITIALIZE();
	FORM_LOAD();