// NOTE: [Must this JS loaded as last item]


	// ===============================================================
	// Initialize Form
	// ===============================================================
	function FORM_LOAD()
	{
		// Load DataGridVuew
		// _______________________________________________________________________	
		LEVEL_DGV = DataGridView("LEVEL_DGV");
		LEVEL_DGV.ON_CLICK
		(			
			function(COL_INDEX, ROW_INDEX)
			{
				// Delete Button
				// ...........................................
				if(LEVEL_DGV.COLUMN.NAME(COL_INDEX) == "DELETE_CMD")
				{
					HTTP_DELETE
					(
						function(IS_SUCCESS)
						{
							console.log('Record Deleted.');
						}, 
						ROW_INDEX
					);
				}			
		
				// Access Rights Button
				// ...........................................
				if(LEVEL_DGV.COLUMN.NAME(COL_INDEX) == "ACCESS_RIGHTS_CMD")
				{
					// Display Modal
					PROPERTY_ACC_LEVEL_ID = LEVEL_DGV.CELL.VALUE('ACC_LEVEL_ID', ROW_INDEX);
					JSOC_ELEMENT.VAR_VALUE('LEVEL_DESCRIPTION_LBL', LEVEL_DGV.CELL.VALUE('ACC_LEVEL_DESCRIPTION', ROW_INDEX));
					PROPERTY_EDIT_ROW_INDEX = ROW_INDEX;
					ACCESS_RIGHTS_HTTP_SEARCH();
					return;
				}	
				
				// If dialog supported
				// ...........................................
				else if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("LEVEL_MODAL") == true)
				{
					// Clear Modal
					if(JSVC_VAR.VAR_ISSET(INPUT_INSTANCE))
					{
						JSOC_WINDOW.VAR_INPUT_INSTANCE_RESTORE(INPUT_INSTANCE);
					}
						
					// Display Modal
					PROPERTY_ACC_LEVEL_ID = LEVEL_DGV.CELL.VALUE('ACC_LEVEL_ID', ROW_INDEX);
					PROPERTY_EDIT_ROW_INDEX = ROW_INDEX;
					JSOC_DIALOG.VAR_MODAL("LEVEL_MODAL", true);
					HTTP_LOAD();
					return;
				}
				
				// Open View / Edit Page
				// ...........................................
				else
				{
					// Cache Primary Key
					JSVC_COOKIE.VAR_ADD
					(
						'PROPERTY_PARERNT_ID', 
						LEVEL_DGV.CELL.VALUE('ACC_LEVEL_ID', ROW_INDEX), 
						5, 
						JSCUS_CACHE.SERVER_URL
					);
					JSVC_COOKIE.VAR_ADD
					(
						'PROPERTY_PARENT_DESCRIPTION', 
						LEVEL_DGV.CELL.VALUE('ACC_LEVEL_DESCRIPTION', ROW_INDEX), 
						5, 
						JSCUS_CACHE.SERVER_URL
					);
					
					// Open EditPage
					window.location='level_info.htm';				
				}

			}
		);   


		// Load DataGridVuew
		// _______________________________________________________________________	
		ACCESS_RIGHTS_DGV = DataGridView("ACCESS_RIGHTS_DGV");
		ACCESS_RIGHTS_DGV.ON_CLICK
		(			
			function(COL_INDEX, ROW_INDEX)
			{	
			}
		);   
		
		
		// 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_ACC_LEVEL_STATUS_CMB") == true)
		{
			JSOC_CMB.VAR_CLEAR("SEARCH_ACC_LEVEL_STATUS_CMB");
			JSOC_CMB.VAR_ADD("SEARCH_ACC_LEVEL_STATUS_CMB", "0","Any Status");
			JSOC_CMB.VAR_ADD("SEARCH_ACC_LEVEL_STATUS_CMB", "1","Inactive");
			JSOC_CMB.VAR_ADD("SEARCH_ACC_LEVEL_STATUS_CMB", "2","Active");
			JSOC_CMB.VAR_ADD("SEARCH_ACC_LEVEL_STATUS_CMB", "3","Closed");
			JSOC_CMB.VAR_ADD("SEARCH_ACC_LEVEL_STATUS_CMB", "4","Completed");
		}			
		
		if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("ACC_LEVEL_STATUS_CMB") == true)
		{
			JSOC_CMB.VAR_CLEAR("ACC_LEVEL_STATUS_CMB");
			JSOC_CMB.VAR_ADD("ACC_LEVEL_STATUS_CMB", "1","Inactive");
			JSOC_CMB.VAR_ADD("ACC_LEVEL_STATUS_CMB", "2","Active");
			JSOC_CMB.VAR_ADD("ACC_LEVEL_STATUS_CMB", "3","Closed");
			JSOC_CMB.VAR_ADD("ACC_LEVEL_STATUS_CMB", "4","Completed");
			JSOC_CMB.VAR_SELECTED_VALUE("ACC_LEVEL_STATUS_CMB", "2");
		}
		
		
		
		// Get Modal Instance (Add Modal Adjustment before this line)
		// _______________________________________________________________________	
		if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("LEVEL_MODAL") == true)
		{
			INPUT_INSTANCE = JSOC_WINDOW.VAR_INPUT_INSTANCE_CLONE("LEVEL_MODAL");
		}
		
		
		// Add Search Events for the SEARCH_CMD - Button
		// _______________________________________________________________________	
		if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("SEARCH_CMD"))
		{
			var SEARCH_CMD = JSOC_ELEMENT.VAR_GET("SEARCH_CMD");
			SEARCH_CMD.onclick = HTTP_SEARCH;
		}
		
		
		// Add Search Events for the SEARCH_DESCRIPTION_TXT - TextInput
		// _______________________________________________________________________		
		if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("SEARCH_ACC_LEVEL_DESCRIPTION_TXT"))
		{
			var SEARCH_ACC_LEVEL_DESCRIPTION_TXT = JSOC_ELEMENT.VAR_GET("SEARCH_ACC_LEVEL_DESCRIPTION_TXT");
			SEARCH_ACC_LEVEL_DESCRIPTION_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()
	{
		GET_MAP_ARRAY = JSCUS_SQL._ACCOUNT._LEVEL.CUS_PREPARE_MAPPED_ARRAY_FOR_GET();	
		// JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_PARENT_ID', PROPERTY_PARENT_ID));		// Parent ID Sample
		
		if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("SEARCH_ACC_LEVEL_SERVER_ID_TXT") == true)
		{
			JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_ACC_LEVEL_SERVER_ID', JSOC_ELEMENT.VAR_VALUE("SEARCH_ACC_LEVEL_SERVER_ID_TXT"));
		}
		
		if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("SEARCH_ACC_LEVEL_TAG_ID_TXT") == true)
		{
			JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_ACC_LEVEL_TAG_ID', JSOC_ELEMENT.VAR_VALUE("SEARCH_ACC_LEVEL_TAG_ID_TXT"));
		}
		
		if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("SEARCH_ACC_LEVEL_DESCRIPTION_TXT") == true)
		{
			JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_ACC_LEVEL_DESCRIPTION', JSOC_ELEMENT.VAR_VALUE("SEARCH_ACC_LEVEL_DESCRIPTION_TXT"));
		}
		
		if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("SEARCH_ACC_LEVEL_STATUS_CMB") == true)
		{
			JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_ACC_LEVEL_STATUS', JSOC_CMB.VAR_SELECTED_VALUE("SEARCH_ACC_LEVEL_STATUS_CMB"));
		}
		
		if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("SEARCH_ACC_LEVEL_SYSTEM_GENERATED_TXT") == true)
		{
			JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_ACC_LEVEL_SYSTEM_GENERATED', JSOC_ELEMENT.VAR_VALUE("SEARCH_ACC_LEVEL_SYSTEM_GENERATED_TXT"));
		}
		
		
		JSOC_DIALOG.VAR_LOADING_SPINNER(true);
		JSCUS_SQL._ACCOUNT._LEVEL.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)
		{
			alert("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)
		{
			alert("REQUEST ERROR: \n" + RESPONSE_STRING);
			return;
		}
		
		// JSON Empty (Clears the DataGrodView)
		// _______________________________________________________________________
		if(JSRC_JSON.REF_READ(JSON_OBJECT, "JSON_COUNT") <= 0)
		{
			LEVEL_DGV.ROW.CLEAR();
			return;
		}

		// Populate DGV
		// _______________________________________________________________________
		LEVEL_DGV.POPULATE_FROM_QUERY
		(
				JSON_OBJECT, 
				'',
				false,
				false,
				1,
				[2, 1]
		);
		
		// For Applying CFormat
		/*
		LEVEL_DGV.POPULATE_FROM_QUERY
		(
				JSON_OBJECT, 
				'',
				false,
				true,
				1,
				[2, 1]
		);
		*/
		
		// Initialize
		// _______________________________________________________________________	
		var WEBTABLE_DATE = null;
		var ROW_INDEX = 0;
	
	
		// DateTime Format
		// _______________________________________________________________________	

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