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


	// ===============================================================
	// Initialize Form
	// ===============================================================
	function FORM_LOAD()
	{
		// Load DataGridVuew
		// _______________________________________________________________________	
		AUDIT_TRAIL_DGV = DataGridView("AUDIT_TRAIL_DGV");
		AUDIT_TRAIL_DGV.ON_CLICK
		(			
			function(COL_INDEX, ROW_INDEX)
			{
				// Delete Button
				// ...........................................
				if(AUDIT_TRAIL_DGV.COLUMN.NAME(COL_INDEX) == "DELETE_CMD")
				{
					HTTP_DELETE
					(
						function(IS_SUCCESS)
						{
							console.log('Record Deleted.');
						}, 
						ROW_INDEX
					);
				}			
				
				// If dialog supported
				// ...........................................
				else if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("AUDIT_TRAIL_MODAL") == true)
				{
					// Clear Modal
					if(JSVC_VAR.VAR_ISSET(INPUT_INSTANCE))
					{
						JSOC_WINDOW.VAR_INPUT_INSTANCE_RESTORE(INPUT_INSTANCE);
					}
						
					// Display Modal
					PROPERTY_ACC_TRAIL_ID = AUDIT_TRAIL_DGV.CELL.VALUE('ACC_TRAIL_ID', ROW_INDEX);
					PROPERTY_EDIT_ROW_INDEX = ROW_INDEX;
					JSOC_DIALOG.VAR_MODAL("AUDIT_TRAIL_MODAL", true);
					HTTP_LOAD();
					return;
				}
				
				// Open View / Edit Page
				// ...........................................
				else
				{
					// Cache Primary Key
					JSVC_COOKIE.VAR_ADD
					(
						'PROPERTY_PARENT_ID', 
						AUDIT_TRAIL_DGV.CELL.VALUE('ACC_TRAIL_ID', ROW_INDEX), 
						5, 
						JSCUS_CACHE.ROOT_PATH
					);
					JSVC_COOKIE.VAR_ADD
					(
						'PROPERTY_PARENT_DESCRIPTION', 
						AUDIT_TRAIL_DGV.CELL.VALUE('ACC_TRAIL_DESCRIPTION', ROW_INDEX), 
						5, 
						JSCUS_CACHE.ROOT_PATH
					);
					
					// Open EditPage
					window.location='audit_trail_info.html';				
				}

			}
		);   

		
		// Show Progress
		// _______________________________________________________________________	
		JSOC_DIALOG.VAR_LOADING_SPINNER(true);
		
		
		// Requires Authentication
		// _______________________________________________________________________	
		PAGE_AUTH_REQUIRES_LOGIN(HTTP_POPULATE_RESULT);			
		
		
		// Foreign Key [Fixed]
		// _______________________________________________________________________	

		
		// Foreign Key [Fixed]
		// _______________________________________________________________________	

		
		// Foreign Key [Fixed]
		// _______________________________________________________________________	
		if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("ACC_TRAIL_ACC_ACCOUNT_ID_CMB") == true)
		{
			JSCUS_SQL._ACCOUNT._MEMBER.CUS_GET_THEN_POPULATE_COMBOBOX
			(
				function()
				{
					// Set Default index
					JSOC_CMB.VAR_SELECTED_INDEX("ACC_TRAIL_ACC_ACCOUNT_ID_CMB", 0);
					
					// Copy items to edit control
					if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("SEARCH_ACC_TRAIL_ACC_ACCOUNT_ID_CMB") == true)
					{
						JSOC_CMB.VAR_CLEAR("SEARCH_ACC_TRAIL_ACC_ACCOUNT_ID_CMB");
						JSOC_CMB.VAR_ADD("SEARCH_ACC_TRAIL_ACC_ACCOUNT_ID_CMB", FLAG_BIGINT_NULL.toString(),"Any Member");
						JSOC_ELEMENT.VAR_INNER_HTML("SEARCH_ACC_TRAIL_ACC_ACCOUNT_ID_CMB", JSOC_ELEMENT.VAR_INNER_HTML("ACC_TRAIL_ACC_ACCOUNT_ID_CMB"), true);
						JSOC_CMB.VAR_SELECTED_INDEX("SEARCH_ACC_TRAIL_ACC_ACCOUNT_ID_CMB", 0);
					}				
					
					// Update Modal Settings
					if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("AUDIT_TRAIL_MODAL") == true)
					{
						INPUT_INSTANCE = JSOC_WINDOW.VAR_INPUT_INSTANCE_CLONE("AUDIT_TRAIL_MODAL");
					}
					
					// Forward populate complete event.
					HTTP_POPULATE_RESULT();		
				},
				"ACC_TRAIL_ACC_ACCOUNT_ID_CMB",
				false,
				2
			);
		}
		
		// Foreign Key [Optional]
		// _______________________________________________________________________	
		if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("ACC_TRAIL_PROXY_USER_CMB") == true)
		{
			JSOC_CMB.VAR_CLEAR("ACC_TRAIL_PROXY_USER_CMB");
			JSOC_CMB.VAR_ADD("ACC_TRAIL_PROXY_USER_CMB", "0", "-");
			JSCUS_SQL._ACCOUNT._MEMBER.CUS_GET_THEN_POPULATE_COMBOBOX
			(
				function()
				{
					// Set Default index
					JSOC_CMB.VAR_SELECTED_INDEX("ACC_TRAIL_PROXY_USER_CMB", 0);
					
					// Update Modal Settings
					if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("AUDIT_TRAIL_MODAL") == true)
					{
						INPUT_INSTANCE = JSOC_WINDOW.VAR_INPUT_INSTANCE_CLONE("AUDIT_TRAIL_MODAL");
					}
					
					// Forward populate complete event.		
					HTTP_POPULATE_RESULT();
				},		
				"ACC_TRAIL_PROXY_USER_CMB",
				true,
				2
			);
		}
		if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("SEARCH_ACC_TRAIL_PROXY_USER_CMB") == true)
		{
			JSOC_CMB.VAR_CLEAR("SEARCH_ACC_TRAIL_PROXY_USER_CMB");
			JSOC_CMB.VAR_ADD("SEARCH_ACC_TRAIL_PROXY_USER_CMB", FLAG_BIGINT_NULL.toString(),"Any Member");
			JSCUS_SQL._ACCOUNT._MEMBER.CUS_GET_THEN_POPULATE_COMBOBOX
			(
				function()
				{
					// Set Default index
					JSOC_CMB.VAR_SELECTED_INDEX("SEARCH_ACC_TRAIL_PROXY_USER_CMB", 0);
					
					// Forward populate complete event.		
					HTTP_POPULATE_RESULT();
				},		
				"SEARCH_ACC_TRAIL_PROXY_USER_CMB",
				true,
				2
			);
		}
		
		// Foreign Key [Optional]
		// _______________________________________________________________________	

		
		// Foreign Key [Fixed]
		// _______________________________________________________________________	
		if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("ACC_TRAIL_SYS_MODULE_ID_CMB") == true)
		{
			JSCUS_SQL._SYSTEM._SYSTEM_MODULE.CUS_GET_THEN_POPULATE_COMBOBOX
			(
				function()
				{
					// Set Default index
					JSOC_CMB.VAR_SELECTED_INDEX("ACC_TRAIL_SYS_MODULE_ID_CMB", 0);
					
					// Copy items to edit control
					if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("SEARCH_ACC_TRAIL_SYS_MODULE_ID_CMB") == true)
					{
						JSOC_CMB.VAR_CLEAR("SEARCH_ACC_TRAIL_SYS_MODULE_ID_CMB");
						JSOC_CMB.VAR_ADD("SEARCH_ACC_TRAIL_SYS_MODULE_ID_CMB", FLAG_BIGINT_NULL.toString(),"Any System_Module");
						JSOC_ELEMENT.VAR_INNER_HTML("SEARCH_ACC_TRAIL_SYS_MODULE_ID_CMB", JSOC_ELEMENT.VAR_INNER_HTML("ACC_TRAIL_SYS_MODULE_ID_CMB"), true);
						JSOC_CMB.VAR_SELECTED_INDEX("SEARCH_ACC_TRAIL_SYS_MODULE_ID_CMB", 0);
					}				
					
					// Update Modal Settings
					if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("AUDIT_TRAIL_MODAL") == true)
					{
						INPUT_INSTANCE = JSOC_WINDOW.VAR_INPUT_INSTANCE_CLONE("AUDIT_TRAIL_MODAL");
					}
					
					// Forward populate complete event.
					HTTP_POPULATE_RESULT();		
				},
				"ACC_TRAIL_SYS_MODULE_ID_CMB",
				false,
				2
			);
		}
		
		
		
		// Get Modal Instance (Add Modal Adjustment before this line)
		// _______________________________________________________________________	
		if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("AUDIT_TRAIL_MODAL") == true)
		{
			INPUT_INSTANCE = JSOC_WINDOW.VAR_INPUT_INSTANCE_CLONE("AUDIT_TRAIL_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_TRAIL_DESCRIPTION_TXT"))
		{
			var SEARCH_ACC_TRAIL_DESCRIPTION_TXT = JSOC_ELEMENT.VAR_GET("SEARCH_ACC_TRAIL_DESCRIPTION_TXT");
			SEARCH_ACC_TRAIL_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._AUDIT_TRAIL.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_TRAIL_REQUEST_IP_ADDRESS_TXT") == true)
		{
			JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_ACC_TRAIL_REQUEST_IP_ADDRESS', JSOC_ELEMENT.VAR_VALUE("SEARCH_ACC_TRAIL_REQUEST_IP_ADDRESS_TXT"));
		}
		
		if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("SEARCH_ACC_TRAIL_SYS_METHOD_ID_CMB") == true)
		{
			JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_ACC_TRAIL_SYS_METHOD_ID', JSOC_CMB.VAR_SELECTED_VALUE("SEARCH_ACC_TRAIL_SYS_METHOD_ID_CMB"));
		}
		
		if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("SEARCH_ACC_TRAIL_ACC_ACCOUNT_ID_CMB") == true)
		{
			JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_ACC_TRAIL_ACC_ACCOUNT_ID', JSOC_CMB.VAR_SELECTED_VALUE("SEARCH_ACC_TRAIL_ACC_ACCOUNT_ID_CMB"));
		}
		
		if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("SEARCH_PARAM_ACC_ACCOUNT_NAME_TXT") == true)
		{
			JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_ACC_ACCOUNT_NAME_TXT', JSOC_ELEMENT.VAR_VALUE("SEARCH_ACC_ACCOUNT_NAME_TXT"));
		}
		
		if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("SEARCH_ACC_TRAIL_ACC_SERVER_ID_CMB") == true)
		{
			JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_ACC_TRAIL_ACC_SERVER_ID', JSOC_CMB.VAR_SELECTED_VALUE("SEARCH_ACC_TRAIL_ACC_SERVER_ID_CMB"));
		}
		
		
		
		if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("SEARCH_ACC_TRAIL_SYS_MODULE_ID_CMB") == true)
		{
			JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_ACC_TRAIL_SYS_MODULE_ID', JSOC_CMB.VAR_SELECTED_VALUE("SEARCH_ACC_TRAIL_SYS_MODULE_ID_CMB"));
		}
	
		if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("SEARCH_PARAM_SYS_MODULE_KEYWORD_TXT") == true)
		{
			JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_SYS_MODULE_KEYWORD', JSOC_ELEMENT.VAR_VALUE("SEARCH_SYS_MODULE_KEYWORD_TXT"));
		}
		
		if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("SEARCH_ACC_TRAIL_TARGET_ID_TXT") == true)
		{
			JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_ACC_TRAIL_TARGET_ID', JSOC_ELEMENT.VAR_VALUE("SEARCH_ACC_TRAIL_TARGET_ID_TXT"));
		}
		
		if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("SEARCH_ACC_TRAIL_DESCRIPTION_TXT") == true)
		{
			JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_ACC_TRAIL_DESCRIPTION', JSOC_ELEMENT.VAR_VALUE("SEARCH_ACC_TRAIL_DESCRIPTION_TXT"));
		}
		
		if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("SEARCH_ACC_TRAIL_PARAMETERS_TXT") == true)
		{
			JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_ACC_TRAIL_PARAMETERS', JSOC_ELEMENT.VAR_VALUE("SEARCH_ACC_TRAIL_PARAMETERS_TXT"));
		}
		
		
		
		if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("SEARCH_ACC_TRAIL_TIMESTAMP_FROM_DTP") == true)
		{
			JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_ACC_TRAIL_TIMESTAMP_FROM', JSOC_ELEMENT.VAR_VALUE("SEARCH_ACC_TRAIL_TIMESTAMP_FROM_DTP").toString() + " 00:00:00");
		}
		
		if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("SEARCH_ACC_TRAIL_TIMESTAMP_TO_DTP") == true)
		{
			JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_ACC_TRAIL_TIMESTAMP_TO', JSOC_ELEMENT.VAR_VALUE("SEARCH_ACC_TRAIL_TIMESTAMP_TO_DTP").toString() + " 23:59:59");
		}
		
		if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("SEARCH_ACC_TRAIL_UNIQUE_SESSION_ID_TXT") == true)
		{
			JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_ACC_TRAIL_UNIQUE_SESSION_ID', JSOC_ELEMENT.VAR_VALUE("SEARCH_ACC_TRAIL_UNIQUE_SESSION_ID_TXT"));
		}
		
		
		JSOC_DIALOG.VAR_LOADING_SPINNER(true);
		JSCUS_SQL._ACCOUNT._AUDIT_TRAIL.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)
		{
			AUDIT_TRAIL_DGV.ROW.CLEAR();
			return;
		}

		// Populate DGV
		// _______________________________________________________________________
		AUDIT_TRAIL_DGV.POPULATE_FROM_QUERY
		(
				JSON_OBJECT, 
				'',
				false,
				false,
				1,
				[2, 1]
		);
		
		// For Applying CFormat
		/*
		AUDIT_TRAIL_DGV.POPULATE_FROM_QUERY
		(
				JSON_OBJECT, 
				'',
				false,
				true,
				1,
				[2, 1]
		);
		*/
		
		// Initialize
		// _______________________________________________________________________	
		var WEBTABLE_DATE = null;
		var ROW_INDEX = 0;
	
	
		// DateTime Format
		// _______________________________________________________________________	
		if(AUDIT_TRAIL_DGV.COLUMN.IS_EXISTS("ACC_TRAIL_TIMESTAMP"))
		{
			WEBTABLE_DATE = null;
			ROW_INDEX = 0;
			while(AUDIT_TRAIL_DGV.ROW.COUNT() > ROW_INDEX)
			{
				// Read Current Value
				// ...........................................
				 WEBTABLE_DATE = JSVC_DATE.VAR_TO_DATE
					 (
							AUDIT_TRAIL_DGV.CELL.VALUE
							(
									"ACC_TRAIL_TIMESTAMP", 
									ROW_INDEX
							)
					 );
				
				// Apply CFormat, and DateFormat
				// ...........................................
				if(JSVC_VAR.VAR_ISSET(WEBTABLE_DATE))
				{
					AUDIT_TRAIL_DGV.CELL.VALUE
					(
							"ACC_TRAIL_TIMESTAMP", ROW_INDEX, 
							JSVC_DATE.VAR_FORMAT(WEBTABLE_DATE, "MMM DD, YYYY")
					);				
				}			
				
				// Adjust Audit Trail Display
				// ...........................................	
				AUDIT_TRAIL_DGV.CELL.VALUE
				(
					"ACC_TRAIL_PARAMETERS", ROW_INDEX, 
					JSVC_STR.VAR_REPLACE
					(
						AUDIT_TRAIL_DGV.CELL.VALUE
						(
								"ACC_TRAIL_PARAMETERS", ROW_INDEX	
						),
						"\" | ",
						"\"<br />"
					)
				);	
				
				// Color Coding (Uncomment to use)
				// ...........................................	
				//AUDIT_TRAIL_DGV.CELL.ELEMENT("COLUMN_NAME", ROW_INDEX).style.color = "#1cc88a"; // Safe
				//AUDIT_TRAIL_DGV.CELL.ELEMENT("COLUMN_NAME", ROW_INDEX).style.color = "#5a5c69"; // Neutral
				//AUDIT_TRAIL_DGV.CELL.ELEMENT("COLUMN_NAME", ROW_INDEX).style.color = "#f6c23e"; // Caution
				//AUDIT_TRAIL_DGV.CELL.ELEMENT("COLUMN_NAME", ROW_INDEX).style.color = "#e74a3b"; // Warning
				
				// To next loop
				// ...........................................	
				ROW_INDEX ++;
			}			
		}

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