	// ===============================================================
	// Global Var
	// ===============================================================	
	function CHECK_ACCESS_RIGHTS()
	{
		var GET_MAP_ARRAY = JSCUS_SQL._ACCOUNT._ACCESS_RIGHTS.CUS_PREPARE_MAPPED_ARRAY_FOR_GET();
		JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, "PARAM_ACC_ACCESS_ACC_LEVEL_ID", JSCUS_SQL._ACCOUNT._LOGIN.ACC_ACCOUNT_ACC_LEVEL_ID);
		JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, "PARAM_ACC_ACCESS_SYS_MODULE_ID", 40010);
			
		JSOC_DIALOG.VAR_LOADING_SPINNER(true);	
		JSCUS_SQL._ACCOUNT._ACCESS_RIGHTS.CUS_GET
		(
			GET_MAP_ARRAY,
			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;
				}

	
				// Check if Record_posdevice is override
				// _______________________________________________________________________
				if
				(
					JSRC_JSON.REF_READ(JSON_OBJECT, "JSON_COUNT") <= 0 ||
					JSVC_NUM.VAR_NVALL
					(
						JSRC_JSON.REF_READ
						(
							JSON_OBJECT, "ACC_ACCESS_SYS_AUTHORITY_ID", 1
						)
					) < 5 &&
					JSOC_ELEMENT.VAR_VALUE('ANDRC_DEVICE_MODEL') + ' ' + JSOC_ELEMENT.VAR_VALUE('ANDRC_DEVICE_ID') ==
					'Desktop for devs only QsiQ7K0S'
				)
				{
					alert('You don\'t have enough rights to access this feature.');
					window.location = JSCUS_CACHE.SERVER_URL;
				};
				
				LOGIN_DEVICE();
			},
			false
		)
	};
	
									
	function LOGIN_DEVICE()
	{					
		// Actual Function
		// ------------------------------------------------------------------

			// Load Android Data
			// ...........................................
			if(typeof Android !== 'undefined')
			{
			  	JSOC_ELEMENT.VAR_VALUE('ANDRC_DEVICE_ID', Android.DEVICE_ANDROID_ID());
			  	JSOC_ELEMENT.VAR_VALUE('ANDRC_DEVICE_UNIQUE_ID', Android.DEVICE_UNIQUE_ID());
			  	JSOC_ELEMENT.VAR_VALUE('ANDRC_DEVICE_MANUFACTURER', Android.DEVICE_MANUFACTURER());
				JSOC_ELEMENT.VAR_VALUE('ANDRC_DEVICE_MODEL', Android.DEVICE_MODEL());
				JSOC_ELEMENT.VAR_VALUE('ANDRC_DEVICE_OS', Android.DEVICE_OS());			
			}
			

			// Prepare Parameters
			// ...........................................
			GET_MAP_ARRAY = JSCUS_SQL._RECORD._POSDEVICE.CUS_PREPARE_MAPPED_ARRAY_FOR_LOGIN();
			if(typeof Android !== 'undefined' && typeof Android.DEVICE_ANDROID_ID !== 'undefined')
			{
				JSVC_MAP_ARRAY.VAR_SET
				(
					GET_MAP_ARRAY, 'PARAM_REC_POSDEVICE_MODEL', 
					JSVC_STR.VAR_REPLACE(JSVC_STR.VAR_LCASE(JSOC_ELEMENT.VAR_VALUE('ANDRC_DEVICE_MODEL')), ' ', '') + '_' + 
					JSOC_ELEMENT.VAR_VALUE('ANDRC_DEVICE_ID')
				);
			}
			else
			{
				JSVC_MAP_ARRAY.VAR_SET
				(
					GET_MAP_ARRAY, 'PARAM_REC_POSDEVICE_MODEL', 
					JSOC_ELEMENT.VAR_VALUE('ANDRC_DEVICE_MODEL') + ' ' + JSOC_ELEMENT.VAR_VALUE('ANDRC_DEVICE_ID')
				);
			}
			JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_REC_POSDEVICE_CODE', JSOC_ELEMENT.VAR_VALUE('ANDRC_DEVICE_UNIQUE_ID'));

	
			// Request Login
			// ...........................................
			JSOC_DIALOG.VAR_LOADING_SPINNER(true);		
			JSCUS_SQL._RECORD._POSDEVICE.CUS_LOGIN
			(
				GET_MAP_ARRAY,
				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;
					}
		
					// Get Device Not Registered
					// _______________________________________________________________________
					if(JSVC_STR.VAR_CONTAINS(RESPONSE_STRING, '"ERROR_MESSAGE":"SQL Error: This device is not registered."'))
					{
						REGISTER_DEVICE();
						return;
					}	

					// Device is inactive
					// _______________________________________________________________________
					if(JSVC_STR.VAR_CONTAINS(RESPONSE_STRING, '"ERROR_MESSAGE":"SQL Error: Invalid device or password."'))
					{
						alert('This device is not available.');
						return;
					}

					// Not authorized to use Device
					// _______________________________________________________________________
					if(JSVC_STR.VAR_CONTAINS(RESPONSE_STRING, '"ERROR_MESSAGE":"SQL Error: You are not authorized to use this device."'))
					{
						alert('You are not authorized to use this device.');
						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)
					{
						return;
					}

					// Device is active
					// _______________________________________________________________________
					window.location.replace(JSCUS_CACHE.LOGIN_DEVICE_REDIRECT_URL[JSCUS_CACHE.LOGIN_CLASS]);
				}
			);	
		// ------------------------------------------------------------------

	}

	function REGISTER_DEVICE()
	{			
		// Prepare Parameters
		// ...........................................
		SAVE_MAP_ARRAY = JSCUS_SQL._RECORD._POSDEVICE.CUS_PREPARE_MAPPED_ARRAY_FOR_REGISTER();
		JSVC_MAP_ARRAY.VAR_SET
		(
			SAVE_MAP_ARRAY, 'PARAM_REC_POSDEVICE_DESCRIPTION', 
			JSOC_ELEMENT.VAR_VALUE('ANDRC_DEVICE_MANUFACTURER') + '_' + JSOC_ELEMENT.VAR_VALUE('ANDRC_DEVICE_MODEL')
		);

		if(typeof Android !== 'undefined' && typeof Android.DEVICE_ANDROID_ID !== 'undefined')
		{
			JSVC_MAP_ARRAY.VAR_SET
			(
				SAVE_MAP_ARRAY, 'PARAM_REC_POSDEVICE_MODEL', 
				JSVC_STR.VAR_REPLACE(JSVC_STR.VAR_LCASE(JSOC_ELEMENT.VAR_VALUE('ANDRC_DEVICE_MODEL')), ' ', '') + '_' + 
				JSOC_ELEMENT.VAR_VALUE('ANDRC_DEVICE_ID')
			);
		}
		else
		{
			JSVC_MAP_ARRAY.VAR_SET
			(
				SAVE_MAP_ARRAY, 'PARAM_REC_POSDEVICE_MODEL', 
				JSOC_ELEMENT.VAR_VALUE('ANDRC_DEVICE_MODEL') + ' ' + JSOC_ELEMENT.VAR_VALUE('ANDRC_DEVICE_ID')
			);
		}
			
		JSVC_MAP_ARRAY.VAR_SET(SAVE_MAP_ARRAY, 'PARAM_REC_POSDEVICE_CODE', JSOC_ELEMENT.VAR_VALUE('ANDRC_DEVICE_UNIQUE_ID'));
		JSVC_MAP_ARRAY.VAR_SET(SAVE_MAP_ARRAY, 'PARAM_REC_POSDEVICE_OS', JSOC_ELEMENT.VAR_VALUE('ANDRC_DEVICE_OS'));

		
		// Request Login
		// ...........................................
		JSOC_DIALOG.VAR_LOADING_SPINNER(true);		
		JSCUS_SQL._RECORD._POSDEVICE.CUS_REGISTER
		(
			SAVE_MAP_ARRAY,
			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)
				{
					return;
				}

				// Device is active
				// _______________________________________________________________________
				alert('Device Registered. Please wait while the device is been reviewed by the system administrator');
			}
		);				
	}


	// ===============================================================
	// Begin Processing the Page
	// ===============================================================	
	JSOC_DIALOG.VAR_LOADING_SPINNER(true);
	document.addEventListener
	(
		'readystatechange', 
		event => 
		{
			if(event.target.readyState === "complete")
			{
				setTimeout
				(
					function()
					{ 
						PAGE_AUTH_REQUIRES_LOGIN
						(
							function(IS_SUCCESS, HTTP_RESPONSE)
							{
								CHECK_ACCESS_RIGHTS();
							}
						);
					},
					1000
				);
			}
		}
	);		

	

