	// ===============================================================

	// Global Var

	// ===============================================================

  	

	

	// ===============================================================

	// 1.A - Login Button

	// ===============================================================											

	function LOGIN_DEVICE()

	{					

		// Actual Function

		// ------------------------------------------------------------------

			

			// Prepare Parameters

			// ...........................................

			GET_MAP_ARRAY = JSCUS_SQL._RECORD._POSDEVICE.CUS_PREPARE_MAPPED_ARRAY_FOR_LOGIN();

			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.SERVER_URL + '/p/water/rider/rider_job_order.html');

					//window.location.replace(JSCUS_CACHE.SERVER_URL + '/p/category/dashboard.html');

					// JSCUS_SQL._RECORD._POSDEVICE.CUS_ME

					// (

					// 	function(IS_SUCCESS, RESPONSE_STRING)

					// 	{

					// 		console.log(RESPONSE_STRING);

					// 	}

					// );

				}

			);	

			

		// ------------------------------------------------------------------

	}

	

	

	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'));

		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)
						{
							LOGIN_DEVICE();
						}
					);
				},
				1000
			);
		}
	});		
	

