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

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


/**
 * =================================================================================
 * [0 of 4] Prepares for the next schedule call on scheduller cycle completion 
 * =================================================================================
 */
function SAVE_SCHEDULER_CALL_NEXT(CTR)
{
	// Increment queue index, or if all the queue is processed, clears the list.
	// ------------------------------------------------------------
	if(getValue('LSTS' + CTR) === null)
	{
		localStorage.clear();
		console.log('ls cleared');
	}
	else
	{
		createItem('CTR', CTR);
	}
		
	// Schedules a next request execution
	// ------------------------------------------------------------
	JSRC_THREAD.REF_POST_DELAYED
	(
		function()
		{
			SAVE_SCHEDULER();
		},
		5000
	);	
} 


/**
 * =================================================================================
 * [1 of 3] Prepare and save POS sales 
 * =================================================================================
 */
function SAVE_SCHEDULER()
{
	console.log("SAVE_SCHEDULER : New Requests");

	// Initialize
	// ------------------------------------------------------------
	var LAST_CODE = '';
	var BUNDLE_MAP_ARRAY = [];
	var FINAL_SAVE_MAP_ARRAY = [];
	var RESERVE_SAVE_MAP_ARRAY = [];
	var JOBORDER_SAVE_MAP_ARRAY = [];

	var RESERVE_ID = 0;
	var JOBORDER_ID = 0;
	var JOBORDER_AMOUNT = 0;
	var JOBORDER_MOP = 0;

	var CTR = 0;

	// Check if pending request exists
	// ------------------------------------------------------------
	if(getValue('CTR') === null)
	{
		// Clear Pending
		// ...........................................
		if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("SCHEDULER_PENDING_LBL") == true)
		{
			JSOC_ELEMENT.VAR_VALUE("SCHEDULER_PENDING_LBL", 0);
		}

		// Sleep (10s) to wait for pending requests
		// ...........................................	
		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, display 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);


	// Prepare SAVE param requests. 
	// ------------------------------------------------------------
	while (CTR != 0)
	{
		// Merge same request group into one parameter set
		// ...........................................	
		if(getValue('LSTS' + CTR) == '0')
		{
			// Get current request group
			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);
				
				if(JSVC_NUM.VAR_NVALL(getValue('RESERVE_ID' + CTR)) > 0)
				{
					RESERVE_ID = JSVC_NUM.VAR_NVALL(getValue('RESERVE_ID' + CTR));
					JOBORDER_ID = JSVC_NUM.VAR_NVALL(getValue('JOBORDER_ID' + CTR));
					JOBORDER_AMOUNT = JSVC_NUM.VAR_NVAL(getValue('JOBORDER_AMOUNT' + CTR));
					JOBORDER_MOP = JSVC_NUM.VAR_NVALL(getValue('JOBORDER_MOP' + CTR));
					JOBORDER_MOP -= 1; // Iba kasi reference ng cash/credit sa pag save
				}
				
				CTR++;
			}
			
			// Add to parameter, if same request group 
			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);
				
				if(JSVC_NUM.VAR_NVALL(getValue('RESERVE_ID' + CTR)) > 0)
				{
					RESERVE_ID = JSVC_NUM.VAR_NVALL(getValue('RESERVE_ID' + CTR));
					JOBORDER_ID = JSVC_NUM.VAR_NVALL(getValue('JOBORDER_ID' + CTR));
					JOBORDER_AMOUNT = JSVC_NUM.VAR_NVAL(getValue('JOBORDER_AMOUNT' + CTR));
					JOBORDER_MOP = JSVC_NUM.VAR_NVALL(getValue('JOBORDER_MOP' + CTR));
					JOBORDER_MOP -= 1; // Iba kasi reference ng cash/credit sa pag save
				}
				
				CTR++;
			}
			
			// End of request group
			else
			{
				break;
			}
		}
		else
		{
			break;
		}
	}	
	
	
	// Prepare callbacks
	// ------------------------------------------------------------	
	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
		{
			// Check if legit JSON response is made
			// ...........................................
			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;
			}
			
			// Legit JSON response
			// ...........................................
			else
			{
				// Get inserted ID
				SALES_ID = JSVC_NUM.VAR_NVALL(JSRC_JSON.REF_READ(JSON_OBJECT, "REC_POSSALE_ID", "1"));

				// Reservation-based request
				if(RESERVE_ID !== 0)
				{
					UPDATE_RESERVATION
					(
						SALES_ID,
						RESERVE_ID, 
						JOBORDER_ID,
						JOBORDER_AMOUNT,
						JOBORDER_MOP,
						function()
						{
							SAVE_SCHEDULER_CALL_NEXT(CTR);
						}
					);
				}
				
				// Walk-in purchases
				else
				{
					SAVE_SCHEDULER_CALL_NEXT(CTR);
				}

				console.log("SAVE_SCHEDULER : Success");
				return;
			}
		}
	};
	
	// Send Request
	// ------------------------------------------------------------
	console.log("SAVE_SCHEDULER : Send Purchase Request");
	JSCUS_SQL._RECORD._POS_SALE_LIST.CUS_PURCHASE
	(
		BUNDLE_MAP_ARRAY,
		RESPONSE_HANDLER
	);
	return;
}


/**
 * =================================================================================
 * [2 of 3] Update a status of processed reservation 
 * =================================================================================
 */
function UPDATE_RESERVATION(SALES_ID, RESERVE_ID, JOBORDER_ID, JOBORDER_AMOUNT, JOBORDER_MOP, CALLBACK_METHOD)
{
	// Initialize
	// ------------------------------------------------------------
	var SAVE_MAP_ARRAY = JSCUS_SQL._RECORD._POS_RESERVE.CUS_PREPARE_MAPPED_ARRAY_FOR_SAVE();
	JSVC_MAP_ARRAY.VAR_SET(SAVE_MAP_ARRAY, "PARAM_REC_POSRESERVE_ID", RESERVE_ID);
	JSVC_MAP_ARRAY.VAR_SET(SAVE_MAP_ARRAY, "PARAM_REC_POSRESERVE_STATUS", 4);

	// Prepares Callback
	// ------------------------------------------------------------
	var RESPONSE_HANDLER = function(IS_SUCCESS, HTTP_RESPONSE)
	{			
		// Handle Error
		// ...........................................
		if(IS_SUCCESS === false)
		{
			JSRC_THREAD.REF_POST_DELAYED
			(
				function()
				{
					UPDATE_RESERVATION(SALES_ID, RESERVE_ID, JOBORDER_ID, JOBORDER_AMOUNT, JOBORDER_MOP, CALLBACK_METHOD);
				},
				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()
				{
					UPDATE_RESERVATION(SALES_ID, RESERVE_ID, JOBORDER_ID, JOBORDER_AMOUNT, JOBORDER_MOP, CALLBACK_METHOD);
				},
				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()
					{
						UPDATE_RESERVATION(SALES_ID, RESERVE_ID, JOBORDER_ID, JOBORDER_AMOUNT, JOBORDER_MOP, CALLBACK_METHOD);
					},
					5000
				);
				console.log("SAVE_SCHEDULER : No Inserted Index = " + HTTP_RESPONSE);
				return;
			}
			else
			{
				UPDATE_JOBORDER(SALES_ID, JOBORDER_ID, JOBORDER_AMOUNT, JOBORDER_MOP, CALLBACK_METHOD);
				return;
			}
		}
	};

	// Send Request
	// ------------------------------------------------------------
	JSCUS_SQL._RECORD._POS_RESERVE.CUS_SAVE
	(
		SAVE_MAP_ARRAY,
		RESPONSE_HANDLER
	);
}


/**
 * =================================================================================
 * [3 of 3] Update a status of processed Job order
 * =================================================================================
 */
function UPDATE_JOBORDER(SALES_ID, JOBORDER_ID, JOBORDER_AMOUNT, JOBORDER_MOP, CALLBACK_METHOD) 
{
	// Initialize
	// ------------------------------------------------------------
	SAVE_MAP_ARRAY = JSCUS_SQL._RECORD._JOB_ORDER.CUS_PREPARE_MAPPED_ARRAY_FOR_SAVE();
	JSVC_MAP_ARRAY.VAR_SET(SAVE_MAP_ARRAY, "PARAM_REC_JORDER_ID", JOBORDER_ID);
	JSVC_MAP_ARRAY.VAR_SET(SAVE_MAP_ARRAY, "PARAM_REC_JORDER_AMOUNT", JSVC_NUM.VAR_NVAL(JOBORDER_AMOUNT));
	JSVC_MAP_ARRAY.VAR_SET(SAVE_MAP_ARRAY, "PARAM_REC_JORDER_LONG_B", JOBORDER_MOP); //0 for default //1 for paid //2 for utang *if bayaran update mo ulit ito sa '1'
	JSVC_MAP_ARRAY.VAR_SET(SAVE_MAP_ARRAY, "PARAM_REC_JORDER_DECIMAL_B", SALES_ID); 
	//INSERT MO SGURO SALES ID DTO 'PARAM_REC_JORDER_DECIMAL_B'?
	//PARA PAG BAYARAN GALING SA UTANG ALAM MO ANONG JOB ORDER YUNG I UUPDATE MO INTO PARAM_REC_JORDER_LONG_B = 1?

	// update job order
	// ------------------------------------------------------------
	var RESPONSE_HANDLER = function(IS_SUCCESS, HTTP_RESPONSE)
	{			
		// Handle Error
		// ...........................................
		if(IS_SUCCESS === false)
		{
			JSRC_THREAD.REF_POST_DELAYED
			(
				function()
				{
					UPDATE_JOBORDER(JOBORDER_ID, JOBORDER_AMOUNT, CALLBACK_METHOD) ;
				},
				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()
				{
					UPDATE_JOBORDER(JOBORDER_ID, JOBORDER_AMOUNT, CALLBACK_METHOD);
				},
				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()
					{
						UPDATE_JOBORDER(JOBORDER_ID, JOBORDER_AMOUNT, CALLBACK_METHOD);
					},
					5000
				);
				console.log("SAVE_SCHEDULER : No Inserted Index = " + HTTP_RESPONSE);
				return;
			}
			else
			{
				CALLBACK_METHOD(true);
				return;
			}
		}
	};

	// Send Request
	// ------------------------------------------------------------
	JSCUS_SQL._RECORD._JOB_ORDER.CUS_SAVE
	(
		SAVE_MAP_ARRAY,
		RESPONSE_HANDLER
	);
}


/**
 * =================================================================================
 * Starts a scheduller
 * =================================================================================
 */
SAVE_SCHEDULER();