


	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_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 taken show complete and save button
					//if open show cancel button
					//else hide all 
					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_USER_ASSIGN', ROW_INDEX)) > 1 &&
						JSVC_NUM.VAR_NVALL(JOB_ORDER_DGV.CELL.VALUE('REC_JORDER_FOR_COMPLETION', ROW_INDEX)) == 2
					)
					{	
						$("#REJECT_BBI").show();
						$("#APPROVE_BBI").show();
						$("#SAVE_BBI").show();
					}
					else
					{
						$("#REJECT_BBI").hide();
						$("#APPROVE_BBI").hide();
						$("#SAVE_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_LASTNAME', 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_MIDDLENAME', ROW_INDEX));
					JSOC_ELEMENT.VAR_VALUE("REC_JORDER_REMARKS_TXT", JOB_ORDER_DGV.CELL.VALUE('REC_JORDER_REMARKS', 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;
				}
			}	
		); 

		// Requires Authentication
		// _______________________________________________________________________
		PAGE_AUTH_REQUIRES_LOGIN(HTTP_POPULATE_RESULT);
		
		if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("SEARCH_REC_JORDER_STATUS_CMB") == true)
		{
			JSOC_CMB.VAR_CLEAR("SEARCH_REC_JORDER_STATUS_CMB");
			JSOC_CMB.VAR_ADD("SEARCH_REC_JORDER_STATUS_CMB", "0","Any Status");
			JSOC_CMB.VAR_ADD("SEARCH_REC_JORDER_STATUS_CMB", "2","Open");
			JSOC_CMB.VAR_ADD("SEARCH_REC_JORDER_STATUS_CMB", "888","Taken");
			JSOC_CMB.VAR_ADD("SEARCH_REC_JORDER_STATUS_CMB", "999","For Completion");
			JSOC_CMB.VAR_ADD("SEARCH_REC_JORDER_STATUS_CMB", "3","Cancelled");
			JSOC_CMB.VAR_ADD("SEARCH_REC_JORDER_STATUS_CMB", "4","Completed");
			JSOC_CMB.VAR_SELECTED_VALUE("SEARCH_REC_JORDER_STATUS_CMB", "999");
		}

		// 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();

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

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

		if
		(
			JSOC_ELEMENT.VAR_ELEMENT_EXISTS("SEARCH_REC_JORDER_REFERENCE_NUMBER_TXT") == true &&
			JSVC_STR.VAR_IS_BLANK( JSOC_ELEMENT.VAR_VALUE("SEARCH_REC_JORDER_REFERENCE_NUMBER_TXT")) == false
		)
		{
			JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_REC_JORDER_REFERENCE_NUMBER', JSOC_ELEMENT.VAR_VALUE("SEARCH_REC_JORDER_REFERENCE_NUMBER_TXT"));
		}

		if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("SEARCH_REC_JORDER_STATUS_CMB") == true)
		{
			if(JSVC_NUM.VAR_NVALL(JSOC_CMB.VAR_SELECTED_VALUE(SEARCH_REC_JORDER_STATUS_CMB)) == 999)
			{
				JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_REC_JORDER_FOR_COMPLETION', 2);
				JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_REC_JORDER_STATUS', 2);
			}
			else if (JSVC_NUM.VAR_NVALL(JSOC_CMB.VAR_SELECTED_VALUE(SEARCH_REC_JORDER_STATUS_CMB)) == 888)
			{
				CMB_STATUS_IDENTIFIER = 1; // ALL TAKEN DATA
				JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_REC_JORDER_FOR_COMPLETION', 1);
				JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_REC_JORDER_STATUS', 2);
			}
			else if (JSVC_NUM.VAR_NVALL(JSOC_CMB.VAR_SELECTED_VALUE(SEARCH_REC_JORDER_STATUS_CMB)) == 2)
			{
				CMB_STATUS_IDENTIFIER = 2; // ALL OPEN AND NOT TAKEN
				JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_REC_JORDER_FOR_COMPLETION', 1);
				JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_REC_JORDER_STATUS', JSVC_NUM.VAR_NVALL(JSOC_CMB.VAR_SELECTED_VALUE(SEARCH_REC_JORDER_STATUS_CMB)));
			}
			else
				JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_REC_JORDER_STATUS', JSVC_NUM.VAR_NVALL(JSOC_CMB.VAR_SELECTED_VALUE(SEARCH_REC_JORDER_STATUS_CMB)));
		}

		JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_REC_JORDER_ACC_GROUP_ID', JSVC_NUM.VAR_NVALL(JSCUS_SQL._ACCOUNT._LOGIN.ACC_ACCOUNT_ACC_GROUP_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;
		//HIDE NOT NEEDED;
		if(CMB_STATUS_IDENTIFIER != 0)
		{
			while(JOB_ORDER_DGV.ROW.COUNT() > ROW_INDEX)
			{
				if
				(
					CMB_STATUS_IDENTIFIER == 1 &&
					JSVC_NUM.VAR_NVALL(JOB_ORDER_DGV.CELL.VALUE('REC_JORDER_USER_ASSIGN', ROW_INDEX)) == 1
					// HIDE ALL NOT TAKEN ORDER
				)
				{
					JOB_ORDER_DGV.ROW.DISPLAY(ROW_INDEX, "none");
				}
				else if
				(
					CMB_STATUS_IDENTIFIER == 2 &&
					JSVC_NUM.VAR_NVALL(JOB_ORDER_DGV.CELL.VALUE('REC_JORDER_USER_ASSIGN', ROW_INDEX)) > 1
					// HIDE ALL TAKER ORDER
				)
				{
					JOB_ORDER_DGV.ROW.DISPLAY(ROW_INDEX, "none");
				}

				ROW_INDEX ++;
			}
		}

		ROW_INDEX = 0;
		while(JOB_ORDER_DGV.ROW.COUNT() > 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)) == 2
			)
				JOB_ORDER_DGV.CELL.VALUE('REC_JORDER_STATUS_TEXT', ROW_INDEX, 'for Completion');
			else 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 &&
				JSVC_NUM.VAR_NVALL(JOB_ORDER_DGV.CELL.VALUE('REC_JORDER_USER_ASSIGN', ROW_INDEX)) > 1
			)
				JOB_ORDER_DGV.CELL.VALUE('REC_JORDER_STATUS_TEXT', ROW_INDEX, 'Taken');
			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, 'Open');
			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, 'Completed');

			ROW_INDEX ++;
		}
	}

	FORM_INITIALIZE();
	FORM_LOAD();