


	

	function FORM_LOAD()
	{
		// Initialize
		// _______________________________________________________________________	
		var DGV_ELEMENT_CHILD = null;
		var DGV_ELEMENT_CHILD_INDEX = 0;
		
		// Load DataGridVuew
		// _______________________________________________________________________	
		JOB_ORDER_DGV = DataGridView("JOB_ORDER_DGV");
		JOB_ORDER_TASK_DELIVER_DGV = DataGridView("JOB_ORDER_TASK_DELIVER_DGV");
		JOB_ORDER_TASK_RETURN_DGV = DataGridView("JOB_ORDER_TASK_RETURN_DGV");

		//JOB_ORDER_DGV.SETTING_ALLOW_USER_TO_ADD_ROWS = true;
		//JOB_ORDER_DGV.SETTING_EDITABLE = true;
		JOB_ORDER_DGV.ON_CLICK
		(
			function(COL_INDEX, ROW_INDEX)
			{
				// If dialog supported
				// ...........................................
				if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("JOB_ORDER_MODAL") == true)
				{
					// Clear Modal
					
					// Display Modal
					PROPERTY_PARENT_ID = JOB_ORDER_DGV.CELL.VALUE('REC_JORDER_LONG_A', ROW_INDEX);
					PROPERTY_REC_JORDER_ID = JOB_ORDER_DGV.CELL.VALUE('REC_JORDER_ID', ROW_INDEX);
					PROPERTY_REC_JORDER_CODE = JOB_ORDER_DGV.CELL.VALUE('REC_JORDER_REFERENCE_NUMBER', ROW_INDEX);
					PROPERTY_EDIT_ROW_INDEX = ROW_INDEX;
					
					if
					(
						JSVC_NUM.VAR_NVALL(JOB_ORDER_DGV.CELL.VALUE('REC_JORDER_STATUS', ROW_INDEX)) == 2 &&
						JSVC_NUM.VAR_NVALL(JOB_ORDER_DGV.CELL.VALUE('REC_JORDER_FOR_COMPLETION', ROW_INDEX)) == 1
					)
					{
						$("#SAVE_BBI").show();
						$("#COMPLETION_BBI").show();
						$("#CANCEL_BBI").show();
					}
					else
					{
						$("#SAVE_BBI").hide();
						$("#COMPLETION_BBI").hide();
						$("#CANCEL_BBI").hide();
					}

					JSOC_DIALOG.VAR_MODAL("JOB_ORDER_MODAL", true);

					JSOC_ELEMENT.VAR_VALUE("REC_JORDER_CODE_LBL", PROPERTY_REC_JORDER_CODE);
					JSOC_ELEMENT.VAR_VALUE("REC_JORDER_REC_CLIENT_LBL", ': ' + JOB_ORDER_DGV.CELL.VALUE('REC_CLIENT_NAME', ROW_INDEX));
					JSOC_ELEMENT.VAR_VALUE("REC_JORDER_REC_CONTACT_LBL", ': ' + JOB_ORDER_DGV.CELL.VALUE('REC_CLIENT_CONTACT', ROW_INDEX));
					JSOC_ELEMENT.VAR_VALUE("REC_JORDER_REC_ADDRESS_LBL", ': ' + JOB_ORDER_DGV.CELL.VALUE('REC_CLIENT_ADDRESS', ROW_INDEX));

					if(JSVC_NUM.VAR_NVALL(JOB_ORDER_DGV.CELL.VALUE('REC_JORDER_DECIMAL_A', ROW_INDEX)) == 2)
						JSOC_ELEMENT.VAR_VALUE("REC_JORDER_DECIMAL_A_LBL", ': Cash');
					else if (JSVC_NUM.VAR_NVALL(JOB_ORDER_DGV.CELL.VALUE('REC_JORDER_DECIMAL_A', ROW_INDEX)) == 3)
						JSOC_ELEMENT.VAR_VALUE("REC_JORDER_DECIMAL_A_LBL", ': Credit');
					else
						JSOC_ELEMENT.VAR_VALUE("REC_JORDER_DECIMAL_A_LBL", ':');

					JSOC_ELEMENT.VAR_VALUE("TOTAL_AMOUNT", JSVC_NUM.VAR_CFORMAT(JOB_ORDER_DGV.CELL.VALUE('REC_JORDER_AMOUNT', ROW_INDEX)));
					
					HTTP_LOAD();
					return;
				}
			}
		);

		JOB_ORDER_TASK_DELIVER_DGV.ON_CHANGE
		(
			function(COL_INDEX, ROW_INDEX)
			{
				if(COL_INDEX == 5)
				{
					var ROW_INDEX = 0;
					var TOTAL = 0;
					while(JOB_ORDER_TASK_DELIVER_DGV.ROW.COUNT() > ROW_INDEX)
					{
						TOTAL += (JSVC_NUM.VAR_NVAL(JOB_ORDER_TASK_DELIVER_DGV.CELL.VALUE('REC_JORDER_TASK_QTY_ACTUAL', ROW_INDEX)) * JSVC_NUM.VAR_NVAL(JOB_ORDER_TASK_DELIVER_DGV.CELL.VALUE('REC_JORDER_TASK_AMT_TARGET', ROW_INDEX)));
						ROW_INDEX++;
					}

					JSOC_ELEMENT.VAR_VALUE("TOTAL_AMOUNT", JSVC_NUM.VAR_CFORMAT(TOTAL));
				}
			}
		);

		// Requires Authentication
		// _______________________________________________________________________
		PAGE_AUTH_REQUIRES_LOGIN(HTTP_POPULATE_RESULT);
		
		// Pre-Load Items (uncomment if no foreign key pre-fetch)
		// _______________________________________________________________________
		if(CUS_SEND_REQUEST_HAS_QUEUE() == false)
		{
			HTTP_SEARCH();
		}

		// Disable Dialogs
		// _______________________________________________________________________	
		CUS_SEND_DIALOG_TRY_DISMISS();
	}

	// ===============================================================
	// Populate Foreign Keys
	// ===============================================================
	function HTTP_POPULATE_RESULT(IS_SUCCESS, HTTP_RESPONSE)
	{
		// Disable Dialogs
		CUS_SEND_DIALOG_TRY_DISMISS();

		// Load device
		//PRELOAD_POSDEVICE();

		// Handle Error
		if(IS_SUCCESS === false)
		{
			alert("Unable to populate lists.");
		}
		else
		{
			// Disable Dialogs
			if(CUS_SEND_REQUEST_HAS_QUEUE() == false)
			{
				HTTP_STORAGE();
				HTTP_SEARCH();
				LOAD_DATA_FRWRD();
			}
		}
	};

	function LOAD_DATA_FRWRD()
	{
		// Disable Dialogs
		if(CUS_SEND_REQUEST_HAS_QUEUE() == false)
		{
			if(JSVC_STR.VAR_IS_BLANK(JSCUS_QUICK_SAVE.CACHE_RETURN_URL) == false &&
				JSVC_STR.VAR_IS_BLANK(JSCUS_QUICK_SAVE.CACHE_SEND_PARAMETER) == false)
			{
				var ROW_INDEX = 0;
				while(JOB_ORDER_DGV.ROW.COUNT() > ROW_INDEX)
				{
					if
					(
						JSVC_NUM.VAR_NVALL(JOB_ORDER_DGV.CELL.VALUE("REC_JORDER_ID", ROW_INDEX)) ==
						JSVC_NUM.VAR_NVALL(JSCUS_QUICK_SAVE.CACHE_SEND_PARAMETER)
					)
					{
						JOB_ORDER_DGV.CELL.ELEMENT(1, ROW_INDEX).onclick();
						break;
					}

					ROW_INDEX++;
				}
			}
		}
		else
		{
			JSRC_THREAD.REF_POST_DELAYED
			(
				function()
				{
					LOAD_DATA_FRWRD();
				},
				500
			);
			return;
		}
	};

	function HTTP_SEARCH()
	{
		var GET_MAP_ARRAY = JSCUS_SQL._RECORD._JOB_ORDER.CUS_PREPARE_MAPPED_ARRAY_FOR_GET();

		if (JSVC_NUM.VAR_NVALL(JSOC_CMB.VAR_SELECTED_VALUE(REC_JORDER_STATUS_CMB)) == 777)
		{
			JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_REC_JORDER_LONG_B_FROM', 1);
			JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_REC_JORDER_LONG_B_TO', 1);
		}
		else
			JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_REC_JORDER_STATUS', JSVC_NUM.VAR_NVALL(JSOC_CMB.VAR_SELECTED_VALUE(REC_JORDER_STATUS_CMB)));

		JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_REC_JORDER_USER_ASSIGN', JSVC_NUM.VAR_NVALL(JSCUS_SQL._ACCOUNT._LOGIN.ACC_ACCOUNT_ID));

		JSOC_DIALOG.VAR_LOADING_SPINNER(true);
		JSCUS_SQL._RECORD._JOB_ORDER.CUS_GET
		(
			GET_MAP_ARRAY,
			HTTP_SEARCH_CALLBACK,
			true
		);
	}

	function HTTP_SEARCH_CALLBACK(IS_SUCCESS, RESPONSE_STRING)
	{
		// Disable Dialogs
		// _______________________________________________________________________
		CUS_SEND_DIALOG_TRY_DISMISS();

		//RESET DGV
		// _______________________________________________________________________
		JOB_ORDER_DGV.ROW.CLEAR();
		
		// Validate
		// _______________________________________________________________________
		if(IS_SUCCESS === false)
		{
			alert("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)
		{
			alert("REQUEST ERROR: \n" + RESPONSE_STRING);
			return;
		}
		
		// JSON Empty (Clears the DataGrodView)
		// _______________________________________________________________________
		if(JSRC_JSON.REF_READ(JSON_OBJECT, "JSON_COUNT") <= 0)
		{
			JOB_ORDER_DGV.ROW.CLEAR();
			return;
		}

		// Populate DGV
		// _______________________________________________________________________
		JOB_ORDER_DGV.POPULATE_FROM_QUERY
		(
				JSON_OBJECT, 
				'',
				false,
				false,
				1,
				[2, 1]
		);

		var ROW_INDEX = 0;
		var WEBTABLE_DATE = null;
		var CURRENT_CELL_VALUE = null;

		while(JOB_ORDER_DGV.ROW.COUNT() > ROW_INDEX)
		{
			// Get Value
			// ...........................................
			CURRENT_CELL_VALUE = JOB_ORDER_DGV.CELL.VALUE
				(
					"REC_JORDER_DATE_COMPLETE", 
					ROW_INDEX
				);
			
			// Read Current Value
			// ...........................................
			if(JSVC_STR.VAR_IS_BLANK(CURRENT_CELL_VALUE) == false)
			{
				WEBTABLE_DATE = JSVC_DATE.VAR_TO_DATE(CURRENT_CELL_VALUE);			
			}
			else
			{
				WEBTABLE_DATE = null;
			}			
			
			// Apply CFormat, and DateFormat
			// ...........................................
			if(JSVC_VAR.VAR_ISSET(WEBTABLE_DATE))
			{
				JOB_ORDER_DGV.CELL.VALUE
				(
						"REC_JORDER_DATE_COMPLETE", ROW_INDEX, 
						JSVC_DATE.VAR_FORMAT(WEBTABLE_DATE, "MMM DD, YYYY hh:mm TT")
				);				
			}
			
			
			if(JSVC_NUM.VAR_NVALL(JOB_ORDER_DGV.CELL.VALUE('REC_JORDER_LONG_B', ROW_INDEX)) == 1)
				JOB_ORDER_DGV.CELL.VALUE('REC_JORDER_STATUS_TEXT', ROW_INDEX, 'Collected');
			else if(JSVC_NUM.VAR_NVALL(JOB_ORDER_DGV.CELL.VALUE('REC_JORDER_STATUS', ROW_INDEX)) == 2)
				JOB_ORDER_DGV.CELL.VALUE('REC_JORDER_STATUS_TEXT', ROW_INDEX, 'Approve');
			else if(JSVC_NUM.VAR_NVALL(JOB_ORDER_DGV.CELL.VALUE('REC_JORDER_STATUS', ROW_INDEX)) == 3)
				JOB_ORDER_DGV.CELL.VALUE('REC_JORDER_STATUS_TEXT', ROW_INDEX, 'Cancelled');
			else if(JSVC_NUM.VAR_NVALL(JOB_ORDER_DGV.CELL.VALUE('REC_JORDER_STATUS', ROW_INDEX)) == 4)
				JOB_ORDER_DGV.CELL.VALUE('REC_JORDER_STATUS_TEXT', ROW_INDEX, 'Delivered');
			
			JOB_ORDER_DGV.CELL.VALUE
			(
				'REC_CLIENT_NAME',
				ROW_INDEX,
				JOB_ORDER_DGV.CELL.VALUE('REC_CLIENT_FIRSTNAME', ROW_INDEX) +
				' ' +
				JOB_ORDER_DGV.CELL.VALUE('REC_CLIENT_LASTNAME', ROW_INDEX)
			);

			ROW_INDEX++;
		}
	}

	function HTTP_STORAGE() 
	{
		// Get Storage ID
		var GET_MAP_ARRAY = JSCUS_SQL._CATEGORY._STORAGE.CUS_PREPARE_MAPPED_ARRAY_FOR_GET();

		JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_CAT_STORAGE_ACC_GROUP_ID', JSVC_NUM.VAR_NVALL(JSCUS_SQL._RECORD._POSDEVICE._LOGIN.REC_POSDEVICE_ACC_GROUP_ID));
		JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_CAT_STORAGE_IS_STORE', 2);
		JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_CAT_STORAGE_STATUS', 2);
		JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_ORDER_FIELD', 'CAT_STORAGE_ID');
		JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_ORDER_TYPE', 'DESC');
		JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_LIMIT', 1);

		var RESPONSE_HANDLER =
			function(IS_SUCCESS, HTTP_RESPONSE)
				{
					// Disable Dialogs
					// ...........................................
					CUS_SEND_DIALOG_TRY_DISMISS();
					
					// Handle Error
					// ...........................................
					if(IS_SUCCESS === false)
					{
						METHOD_UNABLE_TO_SEND_REQUEST(CALLBACK_METHOD);	
						return;
					}
					
					// Catch JSON Error
					// ...........................................
					else if(JSRC_JSON.REF_IS_JSTRING(HTTP_RESPONSE) == false)
					{
						METHOD_SERVER_ISSUE(CALLBACK_METHOD, 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)
						{
							METHOD_REQUEST_FAILURE(CALLBACK_METHOD, JSON_OBJECT);
							return;
						}
						else
						{
							PROPERTY_REC_STORAGE_ID = JSRC_JSON.REF_READ(JSON_OBJECT, "CAT_STORAGE_ID", 1);
						}
					}
				};

		JSCUS_SQL._CATEGORY._STORAGE.CUS_GET
		(
			GET_MAP_ARRAY,
			RESPONSE_HANDLER,
			true
		);
	};

	FORM_INITIALIZE();
	FORM_LOAD();