

	function FORM_LOAD()
	{
		JOB_ORDER_DGV = DataGridView("JOB_ORDER_DGV");

		if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("SEARCH_SALES_BY_RIDER_ACC_GROUP_ID_CMB") == true)
		{
			JSOC_CMB.VAR_CLEAR("SEARCH_SALES_BY_RIDER_ACC_GROUP_ID_CMB");
			JSOC_CMB.VAR_ADD("SEARCH_SALES_BY_RIDER_ACC_GROUP_ID_CMB", FLAG_BIGINT_NULL.toString(),"Any Branch");
			//var GET_MAP_ARRAY = JSCUS_SQL._ACCOUNT._GROUP.CUS_PREPARE_MAPPED_ARRAY_FOR_GET();

			JSCUS_SQL._ACCOUNT._GROUP.CUS_GET_THEN_POPULATE_COMBOBOX
			(
				function()
				{
					$('#SEARCH_SALES_BY_RIDER_ACC_GROUP_ID_CMB').select2();
				},
				"SEARCH_SALES_BY_RIDER_ACC_GROUP_ID_CMB",
				true,
				2//,
				//GET_MAP_ARRAY 
			);
		}
		
		if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("SEARCH_SALES_BY_RIDER_REC_RIDER_ID_CMB") == true)
		{
			JSOC_CMB.VAR_CLEAR("SEARCH_SALES_BY_RIDER_REC_RIDER_ID_CMB");
			JSOC_CMB.VAR_ADD("SEARCH_SALES_BY_RIDER_REC_RIDER_ID_CMB", FLAG_BIGINT_NULL.toString(),"Any Rider");

			var GET_MAP_ARRAY = JSCUS_SQL._ACCOUNT._MEMBER.CUS_PREPARE_MAPPED_ARRAY_FOR_GET();
			JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_ACC_ACCOUNT_ACC_LEVEL_ID', 8);

			JSCUS_SQL._ACCOUNT._MEMBER.CUS_GET_THEN_POPULATE_COMBOBOX
			(
				function()
				{ 
					$('#SEARCH_SALES_BY_RIDER_REC_RIDER_ID_CMB').select2();
				},
				"SEARCH_SALES_BY_RIDER_REC_RIDER_ID_CMB",
				true,
				2,
				GET_MAP_ARRAY
			);
		}
	}

	function LOAD_PRODUCT()
	{
		// Refresh DGV
		JOB_ORDER_DGV.ROW.CLEAR();
		JSOC_ELEMENT.VAR_VALUE("SALES_BY_CUSTOMER_TOTAL_SALES", 0.00);
		
		// Remove Column for last product branch get
		while(NEW_COL_CTR > 0)
		{
			var DGV_HEAD = document.getElementById('JOB_ORDER_DGV_HEADER');
			var DGV_ROW = document.getElementById('JOB_ORDER_DGV_ROW');

			DGV_HEAD.deleteCell(-1);
			DGV_ROW.deleteCell(-1);

			NEW_COL_CTR--;
		}

		// Generate New get
		var GET_MAP_ARRAY = JSCUS_SQL._CATEGORY._PRODUCT_BRANCH.CUS_PREPARE_MAPPED_ARRAY_FOR_GET();	
		if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("SEARCH_SALES_BY_RIDER_ACC_GROUP_ID_CMB") == true)
		{
			JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_CAT_PRODBRANCH_ACC_GROUP_ID', JSVC_NUM.VAR_NVAL(JSOC_CMB.VAR_SELECTED_VALUE("SEARCH_SALES_BY_RIDER_ACC_GROUP_ID_CMB")));
		}
		JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_CAT_PRODBRANCH_STATUS', 2);
		JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_CAT_PRODBRANCH_CAT_PRICEMDL_ID', 1);

		JSCUS_SQL._CATEGORY._PRODUCT_BRANCH.CUS_GET
		(
			GET_MAP_ARRAY,
			function(IS_SUCCESS, RESPONSE_STRING)
			{
				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;
				}

				var CAT_PRODUCT_ID;
				var CAT_PRODUCT_DESCRIPTION;

				var NEW_TH = "";
				var NEW_TD = "";

				var TR_HEAD = JSOC_ELEMENT.VAR_GET("JOB_ORDER_DGV_HEADER");
				var TR_ROW = JSOC_ELEMENT.VAR_GET("JOB_ORDER_DGV_ROW");
				var JSON_COUNT = JSRC_JSON.REF_READ(JSON_OBJECT, "JSON_COUNT");
				NEW_COL_CTR = JSON_COUNT;
				var COUNTER = 1;

				while(COUNTER <= JSON_COUNT)
				{
					CAT_PRODUCT_ID = JSRC_JSON.REF_READ(JSON_OBJECT, "CAT_PRODUCT_ID", COUNTER);
					CAT_PRODUCT_DESCRIPTION = JSRC_JSON.REF_READ(JSON_OBJECT, "CAT_PRODUCT_DESCRIPTION", COUNTER);

					NEW_TH = CUSTOM._ELEMENT._CREATE('th', 'JOB_ORDER_DGV_HEADER_ID' + CAT_PRODUCT_ID, CAT_PRODUCT_DESCRIPTION + ' (QTY)');
					NEW_TD = CUSTOM._ELEMENT._CREATE('td', 'JOB_ORDER_DGV_ROW_ID' + CAT_PRODUCT_ID, '');

					TR_HEAD.appendChild(NEW_TH);
					TR_ROW.appendChild(NEW_TD);

					COUNTER++;
				}

				JSOC_STYLE.VAR_DISPLAY("JOB_ORDER_DGV_ROW", "default");
				JOB_ORDER_DGV = DataGridView("JOB_ORDER_DGV");
			},
			true
		);
	}

	function HTTP_SEARCH()
	{
		if
		(
			JSVC_VAR.VAR_ISSET(SEARCH_SALES_BY_RIDER_ACC_GROUP_ID_CMB) &&
			JSOC_CMB.VAR_SELECTED_INDEX(SEARCH_SALES_BY_RIDER_ACC_GROUP_ID_CMB) < 0 ||
			JSVC_NUM.VAR_NVALL(JSOC_CMB.VAR_SELECTED_VALUE(SEARCH_SALES_BY_RIDER_ACC_GROUP_ID_CMB)) < 0
		)
		{
			JSOC_DIALOG.VAR_MESSAGE_BOX
			(
				'You must select branch.', 
				'Information',
				'OK', 'WARNING',
				function(MESSAGE_RESPONSE)
				{
					SEARCH_SALES_BY_RIDER_ACC_GROUP_ID_CMB.style.borderColor = JSCUS_CACHE.SITE_ON_ERROR_BORDER_COLOR;
					SEARCH_SALES_BY_RIDER_ACC_GROUP_ID_CMB.focus();
				}
			);	
			return;
		}

		var GET_MAP_ARRAY = JSCUS_SQL._RECORD._JOB_ORDER.CUS_PREPARE_MAPPED_ARRAY_FOR_GET();
		if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("SEARCH_SALES_BY_RIDER_ACC_GROUP_ID_CMB") == true)
		{
			JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_REC_JORDER_ACC_GROUP_ID', JSVC_NUM.VAR_NVAL(JSOC_CMB.VAR_SELECTED_VALUE("SEARCH_SALES_BY_RIDER_ACC_GROUP_ID_CMB")));
		}	
		if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("SEARCH_SALES_BY_RIDER_REC_CLIENT_ID_CMB") == true)
		{
			JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_REC_JORDER_REC_CLIENT_ID', JSVC_NUM.VAR_NVAL(JSOC_CMB.VAR_SELECTED_VALUE("SEARCH_SALES_BY_RIDER_REC_CLIENT_ID_CMB")));
		}
		JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_REC_JORDER_DATE_POST_FROM', JSVC_DATE.VAR_FORMAT(JSOC_ELEMENT.VAR_VALUE("SEARCH_DATE_FROM_SALES_BY_RIDER_DTP"), "YYYY-MM-DD") + " 00:00:00.000000");
		JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_REC_JORDER_DATE_POST_TO', JSVC_DATE.VAR_FORMAT(JSOC_ELEMENT.VAR_VALUE("SEARCH_DATE_TO_SALES_BY_RIDER_DTP"), "YYYY-MM-DD") + " 23:59:59.999999");
		

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

	function HTTP_LOAD_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 WEBTABLE_DATE = null;
		var ROW_INDEX = 0;

		while(JOB_ORDER_DGV.ROW.COUNT() > ROW_INDEX)
		{
			// Read Current Value
			// ...........................................
			 WEBTABLE_DATE = JSVC_DATE.VAR_TO_DATE
				 (
						JOB_ORDER_DGV.CELL.VALUE
						(
								"REC_JORDER_DATE_POST", 
								ROW_INDEX
						)
				 );
			
			// Apply CFormat, and DateFormat
			// ...........................................
			if(JSVC_VAR.VAR_ISSET(WEBTABLE_DATE))
			{
				JOB_ORDER_DGV.CELL.VALUE
				(
						"REC_JORDER_DATE_POST", ROW_INDEX, 
						JSVC_DATE.VAR_FORMAT(WEBTABLE_DATE, "MMM DD, YYYY")
				);				
			}			
			
			// Color Coding (Uncomment to use)
			// ...........................................	
			//POS_SALE_DGV.CELL.ELEMENT("COLUMN_NAME", ROW_INDEX).style.color = "#1cc88a"; // Safe
			//POS_SALE_DGV.CELL.ELEMENT("COLUMN_NAME", ROW_INDEX).style.color = "#5a5c69"; // Neutral
			//POS_SALE_DGV.CELL.ELEMENT("COLUMN_NAME", ROW_INDEX).style.color = "#f6c23e"; // Caution
			//POS_SALE_DGV.CELL.ELEMENT("COLUMN_NAME", ROW_INDEX).style.color = "#e74a3b"; // Warning
			
			// To next loop
			// ...........................................	
			ROW_INDEX ++;
		}

		ROW_INDEX = JOB_ORDER_DGV.ROW.COUNT() - 1; // Get the last record
		HTTP_LOAD_LINE(ROW_INDEX);

	}

	function HTTP_LOAD_LINE(ROW_INDEX)
	{
		G_ROW_INDEX = ROW_INDEX;
		var GET_MAP_ARRAY = JSCUS_SQL._RECORD._JOB_ORDER_TASK.CUS_PREPARE_MAPPED_ARRAY_FOR_GET();

		JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_REC_JORDER_TASK_LONG_A_FROM', JSVC_NUM.VAR_NVALL(JOB_ORDER_DGV.CELL.VALUE('REC_JORDER_ID', ROW_INDEX)));
		JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_REC_JORDER_TASK_LONG_A_TO', JSVC_NUM.VAR_NVALL(JOB_ORDER_DGV.CELL.VALUE('REC_JORDER_ID', ROW_INDEX)));
		JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_REC_JORDER_TASK_DECIMAL_A_FROM', 2);
		JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_REC_JORDER_TASK_DECIMAL_A_TO', 2);

		JSOC_DIALOG.VAR_LOADING_SPINNER(true);
		JSCUS_SQL._RECORD._JOB_ORDER_TASK.CUS_GET
		(
			GET_MAP_ARRAY,
			HTTP_LOAD_LINE_CALLBACK,
			true
		);
	}

	function HTTP_LOAD_LINE_CALLBACK(IS_SUCCESS, RESPONSE_STRING)
	{
		// Validate
		// _______________________________________________________________________
		if(IS_SUCCESS === false)
		{
			CUS_SEND_DIALOG_TRY_DISMISS();
			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)
		{
			CUS_SEND_DIALOG_TRY_DISMISS();
			alert("REQUEST ERROR: \n" + RESPONSE_STRING);
			return;
		}
		
		// JSON Empty (Clears the DataGrodView)
		// _______________________________________________________________________
		if(JSRC_JSON.REF_READ(JSON_OBJECT, "JSON_COUNT") <= 0)
		{
			//STORE NO DATA
			JOB_ORDER_DGV.CELL.VALUE('REC_JORDER_ITEM_DESCRIPTION', G_ROW_INDEX, 'No record');

			if (G_ROW_INDEX - 1 >= 0)
				HTTP_LOAD_LINE(G_ROW_INDEX - 1);
			else
				CUS_SEND_DIALOG_TRY_DISMISS();
		}
		else
		{
			var JSON_COUNT = JSRC_JSON.REF_READ(JSON_OBJECT, "JSON_COUNT");
			var COUNTER = 1;
			var CHCK_QTY = 0;
			var R_INDEX_HOLDER = G_ROW_INDEX + 1;

			while(COUNTER <= JSON_COUNT)
			{
				QTY_RETURN = JSVC_NUM.VAR_NVALL(JSRC_JSON.REF_READ(JSON_OBJECT, "REC_JORDER_TASK_QTY_ACTUAL", COUNTER));
				if(QTY_RETURN > 0)
				{
					JOB_ORDER_DGV.ROW.INSERT_AT(R_INDEX_HOLDER);
					JOB_ORDER_DGV.CELL.VALUE("REC_CLIENT_CONTACT", R_INDEX_HOLDER, JSRC_JSON.REF_READ(JSON_OBJECT, "REC_JORDER_TASK_DESCRIPTION", COUNTER));
					JOB_ORDER_DGV.CELL.VALUE("REC_CLIENT_MIDDLENAME", R_INDEX_HOLDER, QTY_RETURN);

					R_INDEX_HOLDER++;
				}
				else
					CHCK_QTY++;
				
				COUNTER++;
			}

			if(CHCK_QTY == JSON_COUNT)
				JOB_ORDER_DGV.ROW.DISPLAY(G_ROW_INDEX, "none");

			if (G_ROW_INDEX - 1 >= 0)
				HTTP_LOAD_LINE(G_ROW_INDEX - 1);
			else
				CUS_SEND_DIALOG_TRY_DISMISS();
		}
	}

	FORM_INITIALIZE();
	FORM_LOAD();