function createItem(COL_NAME, COL_VALUE) {
	localStorage.setItem(COL_NAME, COL_VALUE); 
} 

function getValue(COL_NAME) {
	return localStorage.getItem(COL_NAME);  
}

function SAVE_SCHEDULER()
{
	console.log("SAVE_SCHEDULER : New Requests");

	var LAST_CODE = '';

	var BUNDLE_MAP_ARRAY = [];
	var FINAL_SAVE_MAP_ARRAY = [];

	var CTR = 0;

	if(getValue('CTR') === null)
	{
		// Clear Pending
		if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("SCHEDULER_PENDING_LBL") == true)
		{
			JSOC_ELEMENT.VAR_VALUE("SCHEDULER_PENDING_LBL", 0);
		}
	
		JSRC_THREAD.REF_POST_DELAYED
		(
			function()
			{
				SAVE_SCHEDULER();
				console.log("SAVE_SCHEDULER : No Requests, request aborted");
			},
			10000
		);
		return;
	}
	else
	{
		CTR = getValue('CTR');
	}
	
	// Show current pending queue count
	if(getValue('TOTAL') === null)
	{
		if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("SCHEDULER_PENDING_LBL") == true)
		{
			JSOC_ELEMENT.VAR_VALUE("SCHEDULER_PENDING_LBL", getValue('TOTAL'));
		}
	}
	else
	{
		if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("SCHEDULER_PENDING_LBL") == true)
		{
			JSOC_ELEMENT.VAR_VALUE("SCHEDULER_PENDING_LBL", JSVC_NUM.VAR_NVALL(getValue('TOTAL')) - JSVC_NUM.VAR_NVALL(CTR));
		}	
	}

	console.log("SAVE_SCHEDULER : Current CTR, " + CTR);

	while (CTR != 0)
	{
		if(getValue('LSTS' + CTR) == '0')
		{
			if(JSVC_STR.VAR_IS_BLANK(LAST_CODE))
			{
				LAST_CODE = getValue('GCODE' + CTR);
				FINAL_SAVE_MAP_ARRAY = JSVC_MAP_ARRAY.VAR_TO_MAP_ARRAY(getValue('FINAL_SAVE_MAP_ARRAY' + CTR));
				BUNDLE_MAP_ARRAY = JSCUS_SQL._RECORD._POS_SALE_LIST.CUS_ADD_MAPPED_ARRAY_FOR_PURCHASE(BUNDLE_MAP_ARRAY, FINAL_SAVE_MAP_ARRAY);	
				CTR++;
			}
			else if(LAST_CODE == getValue('GCODE' + CTR))
			{
				FINAL_SAVE_MAP_ARRAY = JSVC_MAP_ARRAY.VAR_TO_MAP_ARRAY(getValue('FINAL_SAVE_MAP_ARRAY' + CTR));
				BUNDLE_MAP_ARRAY = JSCUS_SQL._RECORD._POS_SALE_LIST.CUS_ADD_MAPPED_ARRAY_FOR_PURCHASE(BUNDLE_MAP_ARRAY, FINAL_SAVE_MAP_ARRAY);	
				CTR++;
			}
			else
			{
				break;
			}
		}
		else
		{
			break;
		}
	}
	
	
	var RESPONSE_HANDLER = 			
	function(IS_SUCCESS, HTTP_RESPONSE)
	{
		console.log("SAVE_SCHEDULER : Receive Purchase Request Response");
		
		// Handle Error
		// ...........................................
		if(IS_SUCCESS === false)
		{
			JSRC_THREAD.REF_POST_DELAYED
			(
				function()
				{
					SAVE_SCHEDULER();
				},
				60000
			);
			console.log("SAVE_SCHEDULER : Connection Failure");
			return;
		}
		
		// Catch JSON Error
		// ...........................................
		else if(JSRC_JSON.REF_IS_JSTRING(HTTP_RESPONSE) == false)
		{
			JSRC_THREAD.REF_POST_DELAYED
			(
				function()
				{
					SAVE_SCHEDULER();
				},
				30000
			);
			console.log("SAVE_SCHEDULER : JSON Failure = " + HTTP_RESPONSE);
			return;
		}
			
		// Read Response
		// ...........................................
		else
		{
			// Load Response
			var JSON_OBJECT = JSRC_JSON.REF_CREATE(HTTP_RESPONSE);
			if(JSRC_JSON.REF_IS_SUCCESS(JSON_OBJECT) === false)
			{
				JSRC_THREAD.REF_POST_DELAYED
				(
					function()
					{
						SAVE_SCHEDULER();
					},
					5000
				);
				console.log("SAVE_SCHEDULER : No Inserted Index = " + HTTP_RESPONSE);
				return;
			}
			else
			{
				//Update status of reserve IDs
				if(JSVC_NUM.VAR_NVALL(getValue('RESERVE_IDS_TOTAL')) > 0)
				{
					UPDATE_RESERVATIONS_STATUS
					(
						function(IS_SUCCESS) {
							if(IS_SUCCESS == true)
							{
								//SUCCESS
								if(getValue('LSTS' + CTR) === null)
									localStorage.clear();
								else
									createItem('CTR', CTR);

								BUNDLE_MAP_ARRAY = [];
								FINAL_SAVE_MAP_ARRAY = [];

								LAST_CODE = "";
							}
						}
					);
				}
				else
				{
					//SUCCESS
					if(getValue('LSTS' + CTR) === null)
						localStorage.clear();
					else
						createItem('CTR', CTR);

					BUNDLE_MAP_ARRAY = [];
					FINAL_SAVE_MAP_ARRAY = [];

					LAST_CODE = "";
				}

				JSRC_THREAD.REF_POST_DELAYED
				(
					function()
					{
						SAVE_SCHEDULER();
					},
					5000
				);
				console.log("SAVE_SCHEDULER : Success");
				return;
			}
		}
	};
	
	console.log("SAVE_SCHEDULER : Send Purchase Request");
	JSCUS_SQL._RECORD._POS_SALE_LIST.CUS_PURCHASE
	(
		BUNDLE_MAP_ARRAY,
		RESPONSE_HANDLER
	);
	return;
}

function UPDATE_RESERVATIONS_STATUS(CALLBACK_METHOD)
{
	var RESERVE_SAVE_MAP_ARRAY = [];
	var RESERVE_BUNDLE_MAP_ARRAY = [];

	var CTR = 0;

	while(JSVC_NUM.VAR_NVALL(getValue('RESERVE_IDS_TOTAL')) > CTR)
	{
		RESERVE_SAVE_MAP_ARRAY = JSCUS_SQL._RECORD._POS_RESERVE.CUS_PREPARE_MAPPED_ARRAY_FOR_SAVE();

		JSVC_MAP_ARRAY.VAR_SET(RESERVE_SAVE_MAP_ARRAY, "PARAM_REC_POSRESERVE_ID", JSVC_NUM.VAR_NVALL(getValue('RESERVE_ID_' + CTR)));
		JSVC_MAP_ARRAY.VAR_SET(RESERVE_SAVE_MAP_ARRAY, "PARAM_REC_POSRESERVE_STATUS", 4);

		RESERVE_BUNDLE_MAP_ARRAY = JSCUS_SQL._RECORD._POS_RESERVE.CUS_ADD_MAPPED_ARRAY_FOR_SAVE(RESERVE_BUNDLE_MAP_ARRAY, RESERVE_SAVE_MAP_ARRAY);

		CTR++;
	}

	var RESPONSE_HANDLER = 			
		function(IS_SUCCESS, HTTP_RESPONSE)
		{			
			// Handle Error
			// ...........................................
			if(IS_SUCCESS === false)
			{
				JSRC_THREAD.REF_POST_DELAYED
				(
					function()
					{
						SAVE_SCHEDULER();
					},
					60000
				);
				console.log("SAVE_SCHEDULER : Connection Failure");
				return;
			}
			
			// Catch JSON Error
			// ...........................................
			else if(JSRC_JSON.REF_IS_JSTRING(HTTP_RESPONSE) == false)
			{
				JSRC_THREAD.REF_POST_DELAYED
				(
					function()
					{
						SAVE_SCHEDULER();
					},
					30000
				);
				console.log("SAVE_SCHEDULER : JSON Failure = " + HTTP_RESPONSE);
				return;
			}
				
			// Read Response
			// ...........................................
			else
			{
				// Load Response
				var JSON_OBJECT = JSRC_JSON.REF_CREATE(HTTP_RESPONSE);
				if(JSRC_JSON.REF_IS_SUCCESS(JSON_OBJECT) === false)
				{
					JSRC_THREAD.REF_POST_DELAYED
					(
						function()
						{
							SAVE_SCHEDULER();
						},
						5000
					);
					console.log("SAVE_SCHEDULER : No Inserted Index = " + HTTP_RESPONSE);
					return;
				}
				else
				{
					CALLBACK_METHOD(true);
					return;
				}
			}
		};

	JSCUS_SQL._RECORD._POS_RESERVE.CUS_SAVE
	(
		RESERVE_BUNDLE_MAP_ARRAY,
		RESPONSE_HANDLER
	);
}

SAVE_SCHEDULER();