	// ===============================================================
	// Global Var
	// ===============================================================

	// Cache
	var AUTH_IS_LOADED = false;
	var AUTH_CALLBACK_METHOD = null;
	
	
	// Control
	var LOGOUT_CMD = null;

	var AUTH_ACC_ACCOUNT_FIRST_NAME_LBL = null;
	var AUTH_ACC_ACCOUNT_MIDDLE_NAME_LBL = null;
	var AUTH_ACC_ACCOUNT_LAST_NAME_LBL = null;
	var AUTH_ACC_ACCOUNT_NAME_LBL = null;
	
	var AUTH_ACC_GROUP_DESCRIPTION_LBL = null;
	var AUTH_SYS_GENDER_DESCRIPTION_LBL = null;
	var AUTH_ACC_ACCOUNT_POSITION_LBL = null;

	
	// ===============================================================
	// Initialize Controls
	// ===============================================================     
	function PAGE_AUTH_FORM_INITIALIZE()
	{
		// Logout Buttons
		// _______________________________________________________________________
    	if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("LOGOUT_CMD"))
		{
    		LOGOUT_CMD = JSOC_ELEMENT.VAR_GET("LOGOUT_CMD");      		
    		LOGOUT_CMD.onclick = LOGOUT_CMD_CLICKED;
		}      
    	
    	
    	// Auth Details
    	// _______________________________________________________________________
    	if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("AUTH_ACC_ACCOUNT_FIRST_NAME_LBL"))
		{
    		AUTH_ACC_ACCOUNT_FIRST_NAME_LBL = JSOC_ELEMENT.VAR_GET("AUTH_ACC_ACCOUNT_FIRST_NAME_LBL", "LABEL");      		
		}        	
    	if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("AUTH_ACC_ACCOUNT_MIDDLE_NAME_LBL"))
		{
    		AUTH_ACC_ACCOUNT_MIDDLE_NAME_LBL = JSOC_ELEMENT.VAR_GET("AUTH_ACC_ACCOUNT_MIDDLE_NAME_LBL", "LABEL");      		
		}   
    	if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("AUTH_ACC_ACCOUNT_LAST_NAME_LBL"))
		{
    		AUTH_ACC_ACCOUNT_LAST_NAME_LBL = JSOC_ELEMENT.VAR_GET("AUTH_ACC_ACCOUNT_LAST_NAME_LBL", "LABEL");      		
		}   
    	if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("AUTH_ACC_ACCOUNT_NAME_LBL"))
		{
    		AUTH_ACC_ACCOUNT_NAME_LBL = JSOC_ELEMENT.VAR_GET("AUTH_ACC_ACCOUNT_NAME_LBL");   
		}   

    	
    	// Auth Sub Details
    	// _______________________________________________________________________
    	if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("AUTH_ACC_GROUP_DESCRIPTION_LBL"))
		{
    		AUTH_ACC_GROUP_DESCRIPTION_LBL = JSOC_ELEMENT.VAR_GET("AUTH_ACC_GROUP_DESCRIPTION_LBL", "LABEL");      		
		}    	
    	if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("AUTH_SYS_GENDER_DESCRIPTION_LBL"))
		{
    		AUTH_SYS_GENDER_DESCRIPTION_LBL = JSOC_ELEMENT.VAR_GET("AUTH_SYS_GENDER_DESCRIPTION_LBL", "LABEL");      		
		}  
    	if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("AUTH_ACC_ACCOUNT_POSITION_LBL"))
		{
    		AUTH_ACC_ACCOUNT_POSITION_LBL = JSOC_ELEMENT.VAR_GET("AUTH_ACC_ACCOUNT_POSITION_LBL", "LABEL");      		
		}   
    	
    	// Auth Change Password
    	// _______________________________________________________________________
    	if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("CHANGE_PASSWORD_CMD"))
    	{
    		JSOC_ELEMENT.VAR_GET("CHANGE_PASSWORD_CMD").onclick = CHANGE_PASSWORD_CMD_CLICKED;
    	}
	}
	
	
	// ===============================================================
	// Change Password Request - Part.A
	// ===============================================================		
	function CHANGE_PASSWORD_CMD_CLICKED()
	{
		JSOC_DIALOG.VAR_INPUT_BOX
		(
			'Old Password', 
			'Change Password', 
			'',
			true,
			function(OLD_PASSWORD, IS_SUCCESS)
			{
				if(IS_SUCCESS == true && JSVC_STR.VAR_IS_BLANK(OLD_PASSWORD, true) == false)
				{
					JSOC_DIALOG.VAR_LOADING_SPINNER(true);		
					JSCUS_SQL._ACCOUNT._LOGIN.CUS_VERIFY_PASSWORD 
					(
						OLD_PASSWORD,
						function(IS_SUCCESS)
						{
							CUS_SEND_DIALOG_TRY_DISMISS();
							if(IS_SUCCESS == false)
							{
								JSOC_DIALOG.VAR_MESSAGE_BOX
								(
									"Invalid Password", "Change Password",
									"OK", "ERROR",
									function(){}
								);
								return;			
							}
							else
							{
								CHANGE_PASSWORD_CMD_OLD_PASSWORD_GIVEN(OLD_PASSWORD);		
							}
						}
					);	
				}
			}
		);	
	}
	
	// ===============================================================
	// Change Password Request - Part.B
	// ===============================================================	
	function CHANGE_PASSWORD_CMD_OLD_PASSWORD_GIVEN(OLD_PASSWORD)
	{	
		JSOC_DIALOG.VAR_INPUT_BOX
		(
			'New Password', 
			'Change Password', 
			'',
			true,
			function(NEW_PASSWORD, IS_SUCCESS)
			{
				if(IS_SUCCESS == true && JSVC_STR.VAR_IS_BLANK(NEW_PASSWORD, true) == false)
				{
					CHANGE_PASSWORD_CMD_NEW_PASSWORD_GIVEN(OLD_PASSWORD, NEW_PASSWORD);
				}			
			}
		);	
	}	
	
	// ===============================================================
	// Change Password Request - Part.C
	// ===============================================================	
	function CHANGE_PASSWORD_CMD_NEW_PASSWORD_GIVEN(OLD_PASSWORD, NEW_PASSWORD)
	{		
		JSOC_DIALOG.VAR_INPUT_BOX
		(
			'Confirm New Password', 
			'Change Password', 
			'',
			true,
			function(CONFIRM_NEW_PASSWORD, IS_SUCCESS)
			{
				if(IS_SUCCESS == true && JSVC_STR.VAR_IS_BLANK(CONFIRM_NEW_PASSWORD, true) == false)
				{
					if(CONFIRM_NEW_PASSWORD != NEW_PASSWORD)
					{
						JSOC_DIALOG.VAR_MESSAGE_BOX
						(
							"Confirm Password does not match.", 
							"Change Password",
							"OK",
							"WARNING",
							function(){}
						);
					}
					else
					{
						CHANGE_PASSWORD_CMD_VERIFY_PASSWORD(OLD_PASSWORD, NEW_PASSWORD);
					}
				}
			}
		);	
	}	
	
	// ===============================================================
	// Change Password Request - Part.D
	// ===============================================================	
	function CHANGE_PASSWORD_CMD_VERIFY_PASSWORD(OLD_PASSWORD, NEW_PASSWORD)
	{		
		var SAVE_MAP_ARRAY = JSCUS_SQL._ACCOUNT._MEMBER.CUS_PREPARE_MAPPED_ARRAY_FOR_SAVE();
		SAVE_MAP_ARRAY["PARAM_ACC_ACCOUNT_ID"] = JSCUS_SQL._ACCOUNT._LOGIN.ACC_ACCOUNT_ID;
		SAVE_MAP_ARRAY["PARAM_ACC_ACCOUNT_PASSWORD"] = NEW_PASSWORD;
		
		JSOC_DIALOG.VAR_LOADING_SPINNER(true);	
		JSCUS_SQL._ACCOUNT._MEMBER.CUS_SAVE
		(
				SAVE_MAP_ARRAY,
				CHANGE_PASSWORD_CMD_RESULT
		);				
	}	
	
	
	// ===============================================================
	// Change Password Request - Part.E
	// ===============================================================	
	function CHANGE_PASSWORD_CMD_RESULT(IS_SUCCESS, RESPONSE_STRING)
	{		
		// Dismiss Progress Bar
		// _______________________________________________________________________
		CUS_SEND_DIALOG_TRY_DISMISS();
		
		// Initialize
		// _______________________________________________________________________
		var REQUEST_CALLBACK = JSCUS_SQL._ACCOUNT._LOGIN.CACHE_CALLBACK;
		JSCUS_SQL._ACCOUNT._LOGIN.CACHE_CALLBACK = null;
		
		// Abort on connection failure.
		// _______________________________________________________________________
		if(IS_SUCCESS === false)
		{
			JSOC_DIALOG.VAR_MESSAGE_BOX
			(
				"CONNECTION ERROR: " + "Try again later.", "Change Password",
				"OK", "ERROR",
				function(){}
			);
			return;
		}
		
		
		// Initialize JSON
		// _______________________________________________________________________
		var JSON_OBJECT = JSRC_JSON.REF_CREATE(RESPONSE_STRING);
		
		
		// Abort on Server Failure
		// _______________________________________________________________________
		if(JSRC_JSON.REF_IS_SUCCESS(JSON_OBJECT) === false)
		{
			console.log(RESPONSE_STRING);
			JSOC_DIALOG.VAR_MESSAGE_BOX
			(
				"SERVER ERROR", "Change Password",
				"OK", "ERROR",
				function(){}
			);
			return;	
		}
		
		// Success
		// _______________________________________________________________________
		JSOC_DIALOG.VAR_MESSAGE_BOX
		(
			"Password Successfully Changed", "Change Password",
			"OK", "INFO",
			function(){}
		);
		return;
	}	
	
	
	// ===============================================================
	// Logout Request - Part.A
	// ===============================================================		
	function LOGOUT_CMD_CLICKED()
	{
		JSOC_DIALOG.VAR_MESSAGE_BOX
		(
			"Do you want to logout?", 
			"Logout",
			"YES NO",
			"QUESTION",
			function(MESSAGE_BOX_RESPONSE)
			{
				if(JSOC_DIALOG.MSGBOX_YES == MESSAGE_BOX_RESPONSE)
				{
					JSOC_DIALOG.VAR_LOADING_SPINNER(true);	
					JSCUS_SQL._ACCOUNT._LOGIN.CUS_LOGOUT
					(
						LOGOUT_CMD_CLICKED_CALLBACK,
						JSCUS_CACHE.LOGOUT_REDIRECT_URL[JSCUS_CACHE.LOGIN_CLASS]
					);				
				}			
			}
		)
	}
	
	
	// ===============================================================
	// Logout Request - Part.B
	// ===============================================================		
	function LOGOUT_CMD_CLICKED_CALLBACK(IS_SUCCESS)
	{
		// Disable Dialogs
		// ...........................................
		CUS_SEND_DIALOG_TRY_DISMISS();
	}	
	
	
	// ===============================================================
	// Auth Request - Part.A
	// ===============================================================		
	function PAGE_AUTH_REQUIRES_LOGIN(CALLBACK_METHOD)
	{
		// Initialize
		// ...........................................
		JSOC_DIALOG.VAR_LOADING_SPINNER(true);	
		var REDIRECT_URL = "";
		
		// Set Redirect URL
		// ...........................................
		REDIRECT_URL = JSCUS_CACHE.LOGOUT_REDIRECT_URL[JSCUS_CACHE.LOGIN_CLASS];

		
		// Abort if not authenticated
		// ...........................................
		if(JSCUS_CACHE.IS_AUTHENTICATED === false)
		{
			window.location.replace(REDIRECT_URL);
			return;
		}   

		// On-going request exists
		// ...........................................
		if(JSVC_VAR.VAR_ISSET(AUTH_CALLBACK_METHOD))
		{
			throw 'On-going request exists';
			return;
		}
		else
		{
			AUTH_CALLBACK_METHOD = CALLBACK_METHOD;
		}
		
		// Proceed to request
		// ...........................................
    	JSCUS_SQL._ACCOUNT._LOGIN.CUS_ME
    	(
    		PAGE_AUTH_REQUIRES_LOGIN_CALLBACK, 
    		REDIRECT_URL
    	);		
	}
	
	
	// ===============================================================
	// Auth Request - Part.B
	// ===============================================================		
	function PAGE_AUTH_REQUIRES_LOGIN_CALLBACK(IS_SUCCESS)
	{
		// Initialize
		// _______________________________________________________________________
		var CALLBACK_METHOD = AUTH_CALLBACK_METHOD;
		AUTH_CALLBACK_METHOD = null;
		
		
		// Disable Dialogs
		// _______________________________________________________________________
		CUS_SEND_DIALOG_TRY_DISMISS();
			
		
		// Handle Error
		// _______________________________________________________________________
		if(IS_SUCCESS === true)
		{
			AUTH_IS_LOADED = true;

			// Auth Details
			// ...........................................
			if(JSVC_VAR.VAR_ISSET(AUTH_ACC_ACCOUNT_FIRST_NAME_LBL))
			{
				JSOC_ELEMENT.VAR_VALUE
				(
					AUTH_ACC_ACCOUNT_FIRST_NAME_LBL, 
					JSCUS_SQL._ACCOUNT._LOGIN.ACC_ACCOUNT_FIRST_NAME
				);
			}
			if(JSVC_VAR.VAR_ISSET(AUTH_ACC_ACCOUNT_MIDDLE_NAME_LBL))
			{
				JSOC_ELEMENT.VAR_VALUE
				(
					AUTH_ACC_ACCOUNT_MIDDLE_NAME_LBL, 
					JSCUS_SQL._ACCOUNT._LOGIN.ACC_ACCOUNT_MIDDLE_NAME
				);
			}		
			if(JSVC_VAR.VAR_ISSET(AUTH_ACC_ACCOUNT_LAST_NAME_LBL))
			{
				JSOC_ELEMENT.VAR_VALUE
				(
					AUTH_ACC_ACCOUNT_LAST_NAME_LBL, 
					JSCUS_SQL._ACCOUNT._LOGIN.ACC_ACCOUNT_LAST_NAME
				);			
			}	
			if(JSVC_VAR.VAR_ISSET(AUTH_ACC_ACCOUNT_NAME_LBL))
			{
				JSOC_ELEMENT.VAR_VALUE
				(
					AUTH_ACC_ACCOUNT_NAME_LBL, 
					JSCUS_SQL._ACCOUNT._LOGIN.ACC_ACCOUNT_NAME
				);				
			}	


			// Auth Sub Details
			// ...........................................
			if(JSVC_VAR.VAR_ISSET(AUTH_ACC_GROUP_DESCRIPTION_LBL))
			{
				JSOC_ELEMENT.VAR_VALUE
				(
					AUTH_ACC_GROUP_DESCRIPTION_LBL, 
					JSCUS_SQL._ACCOUNT._LOGIN.ACC_GROUP_DESCRIPTION
				);				
			}	
			if(JSVC_VAR.VAR_ISSET(AUTH_SYS_GENDER_DESCRIPTION_LBL))
			{
				JSOC_ELEMENT.VAR_VALUE
				(
					AUTH_SYS_GENDER_DESCRIPTION_LBL, 
					JSCUS_SQL._ACCOUNT._LOGIN.SYS_GENDER_DESCRIPTION
				);					
			}	
			if(JSVC_VAR.VAR_ISSET(AUTH_ACC_ACCOUNT_POSITION_LBL))
			{
				JSOC_ELEMENT.VAR_VALUE
				(
					AUTH_ACC_ACCOUNT_POSITION_LBL, 
					JSCUS_SQL._ACCOUNT._LOGIN.ACC_ACCOUNT_POSITION
				);				
			}
			
			// Prepare Callback
			// ...........................................
			if(JSVC_VAR.VAR_ISSET(CALLBACK_METHOD))
			{
				CALLBACK_METHOD();
				return;
			}
		}
	}
	
	
	// ===============================================================
	// Begin Processing the Page
	// ===============================================================
	PAGE_AUTH_FORM_INITIALIZE();