var TRANS_PENDING_DGV = null;
var TRANS_EMPLOY_DGV = null;
var TRANS_LIST_DGV = null;

var SEARCH_TRANS_PENDING_DATE_DTP = null;
var NEW_TRANS_MODAL_SEARCH_CMD = null;
var NEW_TRANSACTION_BBI = null;

var TRANS_EMPLOY_BBI = null;
var TRANS_EMPLOY_REFRESH_BBI = null;
var TRANS_LIST_BBI = null;
var TRANS_LIST_REFRESH_BBI = null;

	function STATUS_BUTTON_ACTION()
	{
		// Initialize
		// _______________________________________________________________________	
		var READONLY_VALUE = false;
		var ENABLED_VALUE = false;
		
		// Check Status
		// _______________________________________________________________________	
		if(JSOC_CMB.VAR_SELECTED_VALUE_NVAL('REC_TRANS_STATUS_CMB') <= 2)
		{
			READONLY_VALUE = false;
			ENABLED_VALUE = true;	
		}
		else
		{
			READONLY_VALUE = true;
			ENABLED_VALUE = false;		
		}
		
		// Apply Status COntrol
		// _______________________________________________________________________	
		JSOC_ELEMENT.VAR_READONLY(REC_TRANS_QUANTITY_ACTUAL_TXT, READONLY_VALUE);
		JSOC_ELEMENT.VAR_ENABLED(TRANS_LIST_BBI, ENABLED_VALUE);
		JSOC_ELEMENT.VAR_ENABLED(TRANS_EMPLOY_BBI, ENABLED_VALUE);
		JSOC_ELEMENT.VAR_ENABLED(SAVE_BBI, ENABLED_VALUE);	
	};
	
	function FORM_MANAGE_BUTTON()
	{
		JSOC_STYLE.VAR_DISPLAY(DRAFT_TRANSACTION_BBI, 'NONE');
		JSOC_STYLE.VAR_DISPLAY(VOID_TRANSACTION_BBI, 'NONE');
		JSOC_STYLE.VAR_DISPLAY(PRODUCTION_TRANSACTION_BBI, 'NONE');
		JSOC_STYLE.VAR_DISPLAY(COMPLETED_TRANSACTION_BBI, 'NONE');
		
		JSOC_ELEMENT.VAR_ENABLED(DRAFT_TRANSACTION_BBI, true);
		JSOC_ELEMENT.VAR_ENABLED(VOID_TRANSACTION_BBI, true);
		JSOC_ELEMENT.VAR_ENABLED(PRODUCTION_TRANSACTION_BBI, true);
		JSOC_ELEMENT.VAR_ENABLED(COMPLETED_TRANSACTION_BBI, true);	
		
		// Draft
		if(JSOC_CMB.VAR_SELECTED_VALUE(REC_TRANS_STATUS_CMB) == '1')
		{
			JSOC_STYLE.VAR_DISPLAY(VOID_TRANSACTION_BBI, 'DEFAULT');
			JSOC_STYLE.VAR_DISPLAY(PRODUCTION_TRANSACTION_BBI, 'DEFAULT');		
			JSOC_ELEMENT.VAR_VALUE("REC_TRANS_ACC_ACCOUNT_ID_COMPLETE_ACC_ACCOUNT_NAME_TXT", "");	
			JSOC_ELEMENT.VAR_VALUE("REC_TRANS_DATE_COMPLETE_DTP", "");			
		}
		
		// On Production
		else if(JSOC_CMB.VAR_SELECTED_VALUE(REC_TRANS_STATUS_CMB) == '2')
		{
			JSOC_STYLE.VAR_DISPLAY(DRAFT_TRANSACTION_BBI, 'DEFAULT');	
			JSOC_STYLE.VAR_DISPLAY(VOID_TRANSACTION_BBI, 'DEFAULT');		
			JSOC_STYLE.VAR_DISPLAY(COMPLETED_TRANSACTION_BBI, 'DEFAULT');
			JSOC_ELEMENT.VAR_VALUE("REC_TRANS_ACC_ACCOUNT_ID_COMPLETE_ACC_ACCOUNT_NAME_TXT", "");
			JSOC_ELEMENT.VAR_VALUE("REC_TRANS_DATE_COMPLETE_DTP", "");	
		}
		
		// Disable buttons
		STATUS_BUTTON_ACTION();
	};
	
	function FORM_DISABLE_BUTTON()
	{
		JSOC_ELEMENT.VAR_ENABLED(DRAFT_TRANSACTION_BBI, false);
		JSOC_ELEMENT.VAR_ENABLED(VOID_TRANSACTION_BBI, false);
		JSOC_ELEMENT.VAR_ENABLED(PRODUCTION_TRANSACTION_BBI, false);
		JSOC_ELEMENT.VAR_ENABLED(COMPLETED_TRANSACTION_BBI, false);	
		STATUS_BUTTON_ACTION();
	};


	
	function FORM_UPDATE_SELECTED_PLAN(ROW_INDEX)
	{
		// Locate Selected Row
		var ROW_COUNT = TRANS_PENDING_DGV.ROW.COUNT();
		var SELECTED_ROW_INDEX = 0;
		
		if(TRANS_PENDING_DGV.CELL.VALUE_NVALBOOL("SELECT", ROW_INDEX) == true)
		{
			SELECTED_ROW_INDEX = ROW_INDEX;
		}
		else
		{
			while(ROW_COUNT > ROW_INDEX)
			{
				if(TRANS_PENDING_DGV.CELL.VALUE_NVALBOOL("SELECT", ROW_INDEX))
				{
					SELECTED_ROW_INDEX = ROW_INDEX;
					break;
				}
				ROW_INDEX ++;
			}
			
			if(ROW_COUNT <= ROW_INDEX)
			{
				return;
			}
		}		
		
		JSOC_ELEMENT.VAR_VALUE("PLAN_MIX_TXT", TRANS_PENDING_DGV.CELL.VALUE_NVAL("MIX", SELECTED_ROW_INDEX));
	};
	
	
	// ===============================================================
	// Initialize Controls
	// ===============================================================     
	function FORM_CHILD_INITIALIZE()
	{
		if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("SEARCH_TRANS_PENDING_DATE_DTP"))
		{
			SEARCH_TRANS_PENDING_DATE_DTP = JSOC_ELEMENT.VAR_GET("SEARCH_TRANS_PENDING_DATE_DTP");   
			JSOC_ELEMENT.VAR_VALUE
			(
				SEARCH_TRANS_PENDING_DATE_DTP, 
				JSVC_DATE.VAR_FORMAT
				(
					JSVC_DATE.VAR_ADD('DD', 1, JSVC_DATE.VAR_NOW()), 
					"YYYY-MM-DD" + "T00:00"
				)
			);				
		}		
		if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("NEW_TRANS_MODAL_SEARCH_CMD"))
		{
			NEW_TRANS_MODAL_SEARCH_CMD = JSOC_ELEMENT.VAR_GET("NEW_TRANS_MODAL_SEARCH_CMD");
			NEW_TRANS_MODAL_SEARCH_CMD.onclick = function()
			{
				HTTP_PENDING();
			};
		}	
		if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("NEW_TRANSACTION_BBI"))
		{
			NEW_TRANSACTION_BBI = JSOC_ELEMENT.VAR_GET("NEW_TRANSACTION_BBI");
			NEW_TRANSACTION_BBI.onclick = function()
			{
				// Initialize
				// ...........................................
				var ROW_COUNT = TRANS_PENDING_DGV.ROW.COUNT();
				var ROW_INDEX = 0;
				
				// Get Selected Plan
				// ...........................................
				while(ROW_COUNT > ROW_INDEX)
				{
					if(JSVC_NUM.VAR_NVALBOOL(TRANS_PENDING_DGV.CELL.VALUE("SELECT", ROW_INDEX)) == true)
					{
						break;
					}
					ROW_INDEX ++;
				}
				
				// Nothing selected 
				// ...........................................
				if(ROW_COUNT <= ROW_INDEX)
				{
					JSVC_INFO.VAR_NOTHING_TO_LOAD();
					return;
				}
				
				// Prepare Transaction
				// ...........................................
				 HTTP_PENDING_NEW(ROW_INDEX);
				
			};
		}	
		if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("TRANS_EMPLOY_BBI"))
		{
			TRANS_EMPLOY_BBI = JSOC_ELEMENT.VAR_GET("TRANS_EMPLOY_BBI");
			TRANS_EMPLOY_BBI.onclick = function()
			{
				// Validate Current transaction ID
				// ...........................................
				if(PROPERTY_REC_TRANS_ID < 0)
				{
					throw "Invalid Transaction ID : " + PROPERTY_REC_TRANS_ID;
				}
				
				// Get Current Root path
				// ...........................................
				var CURRENT_ROOT_PATH = JSCUS_CACHE.ROOT_PATH + JSVC_STR.VAR_MID(JSVC_STR.VAR_DLEFT(JSCUS_CACHE.AUTH_REDIRECT_URL[JSCUS_CACHE.LOGIN_CLASS], JSCUS_CACHE.SERVER_URL.length), 1, 1);
		
				// Cache Primary Key
				// ...........................................
				JSVC_COOKIE.VAR_ADD
				(
					'PROPERTY_PARENT_ID_TRANS_EMPLOY', 
					PROPERTY_REC_TRANS_ID, 
					5, 
					CURRENT_ROOT_PATH
				);
				JSVC_COOKIE.VAR_ADD
				(
					'PROPERTY_PARENT_DESCRIPTION_TRANS_EMPLOY', 
					JSOC_CMB.VAR_SELECTED_TEXT('REC_TRANS_CAT_PRODUCT_ID_CMB') + ' - ' + TRANS_DGV.CELL.VALUE('REC_TRANS_REFERENCE_CODE', PROPERTY_EDIT_ROW_INDEX), 
					5, 
					CURRENT_ROOT_PATH
				);
				
				// Open EditPage
				// ...........................................
				window.open(JSCUS_CACHE.SERVER_URL + '/p/record/transaction_employ.html', '_blank');	
				return;				
			};
		}			
			
		if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("TRANS_EMPLOY_REFRESH_BBI"))
		{
			TRANS_EMPLOY_REFRESH_BBI = JSOC_ELEMENT.VAR_GET("TRANS_EMPLOY_REFRESH_BBI");
			TRANS_EMPLOY_REFRESH_BBI.onclick = function()
			{
				HTTP_EMPLOYEE_SEARCH();
			};
		}

		
		if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("TRANS_LIST_BBI"))
		{
			TRANS_LIST_BBI = JSOC_ELEMENT.VAR_GET("TRANS_LIST_BBI");
			TRANS_LIST_BBI.onclick = function()
			{
				// Validate Current transaction ID
				// ...........................................
				if(PROPERTY_REC_TRANS_ID < 0)
				{
					throw "Invalid Transaction ID : " + PROPERTY_REC_TRANS_ID;
				}
				
				// Get Current Root path
				// ...........................................
				var CURRENT_ROOT_PATH = JSCUS_CACHE.ROOT_PATH + JSVC_STR.VAR_MID(JSVC_STR.VAR_DLEFT(JSCUS_CACHE.AUTH_REDIRECT_URL[JSCUS_CACHE.LOGIN_CLASS], JSCUS_CACHE.SERVER_URL.length), 1, 1);
		
				// Cache Primary Key
				// ...........................................
				JSVC_COOKIE.VAR_ADD
				(
					'PROPERTY_PARENT_ID_TRANS_LIST', 
					PROPERTY_REC_TRANS_ID, 
					5, 
					CURRENT_ROOT_PATH
				);
				JSVC_COOKIE.VAR_ADD
				(
					'PROPERTY_CAT_PRODUCT_ID_TRANS_LIST', 
					JSOC_CMB.VAR_SELECTED_VALUE('REC_TRANS_CAT_PRODUCT_ID_CMB'), 
					5, 
					CURRENT_ROOT_PATH
				);				
				
				JSVC_COOKIE.VAR_ADD
				(
					'PROPERTY_PARENT_DESCRIPTION_TRANS_LIST', 
					JSOC_CMB.VAR_SELECTED_TEXT('REC_TRANS_CAT_PRODUCT_ID_CMB') + ' - ' + TRANS_DGV.CELL.VALUE('REC_TRANS_REFERENCE_CODE', PROPERTY_EDIT_ROW_INDEX), 
					5, 
					CURRENT_ROOT_PATH
				);
				
				// Open EditPage
				// ...........................................
				window.open(JSCUS_CACHE.SERVER_URL + '/p/record/transaction_list.html', '_blank');	
				return;		
			};
		}				
	
		if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("TRANS_LIST_REFRESH_BBI"))
		{
			TRANS_LIST_REFRESH_BBI = JSOC_ELEMENT.VAR_GET("TRANS_LIST_REFRESH_BBI");
			TRANS_LIST_REFRESH_BBI.onclick = function()
			{
				HTTP_LIST_SEARCH();
			};
		}	


		// ============
		
		
		TRANS_PENDING_DGV = DataGridView("TRANS_PENDING_DGV");
		TRANS_PENDING_DGV.ON_CLICK
		(			
			function(COL_INDEX, ROW_INDEX)
			{
				// Radion On/Off on row click
				// ...........................................
				if(TRANS_PENDING_DGV.COLUMN.NAME(COL_INDEX) != "SELECT")
				{
					TRANS_PENDING_DGV.CELL.VALUE
					(
						"SELECT", ROW_INDEX, 
						(
							TRANS_PENDING_DGV.CELL.VALUE_NVALBOOL("SELECT", ROW_INDEX) == false
						)
					); 
				}		
				FORM_UPDATE_SELECTED_PLAN(ROW_INDEX);
			}
		);   
		
		// ~~~~~
		
		TRANS_EMPLOY_DGV = DataGridView("TRANS_EMPLOY_DGV");
		
		// ~~~~~
		
		TRANS_LIST_DGV = DataGridView("TRANS_LIST_DGV");
		var AUTO_COMPUTE_BLOCKS = [];
		AUTO_COMPUTE_BLOCKS.push
		(
			[
				'REC_TRANSLIST_PRICE_NET',
				'(`REC_TRANSLIST_ING_PRICE` / `REC_TRANSLIST_ING_NET_CONTENT`) * 1.1 * `REC_TRANSLIST_COST_ACTUAL`',	
				false
			]
		);
		AUTO_COMPUTE_BLOCKS.push
		(
			[
				'REC_TRANSLIST_COST_DISCREPANCY',
				'`REC_TRANSLIST_COST_ACTUAL` - `REC_TRANSLIST_COST_BASE`',	
				false
			]
		);
		TRANS_LIST_DGV.COLUMN.AUTO_COMPUTE_CREATE(AUTO_COMPUTE_BLOCKS);
	};
	

	// ===============================================================
	// Request Search - PART.A
	// ===============================================================
	function HTTP_PENDING()
	{
		// Callback Method
		// _______________________________________________________________________
		var CALLBACK_METHOD = function(IS_SUCCESS, RESPONSE_STRING)
		{
			// Disable Dialogs
			// ...........................................
			CUS_SEND_DIALOG_TRY_DISMISS();
			
			// Validate
			// ...........................................
			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)
			// ...........................................
			TRANS_PENDING_DGV.ROW.CLEAR();
			if(JSRC_JSON.REF_READ(JSON_OBJECT, "JSON_COUNT") <= 0)
			{
				TRANS_PENDING_DGV.ROW.CLEAR();
				return;
			}
			
			// Populate DGV
			// ...........................................
			var DATE_PREFIX = JSVC_DATE.VAR_FORMAT(JSOC_ELEMENT.VAR_VALUE(SEARCH_TRANS_PENDING_DATE_DTP), 'DD');
			var JSON_COUNT = JSRC_JSON.REF_READ_NVALL(JSON_OBJECT, "JSON_COUNT");
			var JSON_INDEX = 1;
			var ROW_INDEX = 0;
			
			
			while(JSON_COUNT >= JSON_INDEX)
			{
				// Add Row
				TRANS_PENDING_DGV.ROW.ADD();
				ROW_INDEX = TRANS_PENDING_DGV.ROW.COUNT() - 1;
				
				// Populate Data
				TRANS_PENDING_DGV.CELL.VALUE("ROW_COUNT", ROW_INDEX, ROW_INDEX + 1);
				TRANS_PENDING_DGV.CELL.VALUE("CAT_PRODUCT_ID", ROW_INDEX, JSRC_JSON.REF_READ(JSON_OBJECT, "CAT_PRODUCT_ID", JSON_INDEX));		
				TRANS_PENDING_DGV.CELL.VALUE("CAT_PRODUCT_ING_PRICE", ROW_INDEX, JSRC_JSON.REF_READ(JSON_OBJECT, "CAT_PRODUCT_ING_PRICE", JSON_INDEX));
				TRANS_PENDING_DGV.CELL.VALUE("CAT_PRODUCT_ING_NET_CONTENT", ROW_INDEX, JSRC_JSON.REF_READ(JSON_OBJECT, "CAT_PRODUCT_ING_NET_CONTENT", JSON_INDEX));
				TRANS_PENDING_DGV.CELL.VALUE("CAT_PRODUCT_MIXTURE_QTY", ROW_INDEX, JSRC_JSON.REF_READ(JSON_OBJECT, "CAT_PRODUCT_MIXTURE_QTY", JSON_INDEX));
				TRANS_PENDING_DGV.CELL.VALUE("CAT_PRODUCT_MIXTURE_COST", ROW_INDEX, JSRC_JSON.REF_READ(JSON_OBJECT, "CAT_PRODUCT_MIXTURE_COST", JSON_INDEX));
				
				TRANS_PENDING_DGV.CELL.VALUE("CAT_PRODUCT_DESCRIPTION", ROW_INDEX, JSRC_JSON.REF_READ(JSON_OBJECT, "CAT_PRODUCT_DESCRIPTION", JSON_INDEX));		
				TRANS_PENDING_DGV.CELL.VALUE("QTY", ROW_INDEX, JSRC_JSON.REF_READ_NVAL(JSON_OBJECT, "D" + DATE_PREFIX, JSON_INDEX));
				TRANS_PENDING_DGV.CELL.VALUE("MIX", ROW_INDEX, JSRC_JSON.REF_READ_NVAL(JSON_OBJECT, "D" + DATE_PREFIX, JSON_INDEX) / JSRC_JSON.REF_READ_NVAL(JSON_OBJECT, "CAT_PRODUCT_MIXTURE_QTY", JSON_INDEX));
				TRANS_PENDING_DGV.CELL.VALUE("DATE", ROW_INDEX, JSOC_ELEMENT.VAR_VALUE(SEARCH_TRANS_PENDING_DATE_DTP));
				
				// Next loop
				JSON_INDEX ++;
			}
		
		
			// DateTime Format
			// ...........................................
			TRANS_PENDING_DGV.COLUMN.CFORMAT("QTY");
			TRANS_PENDING_DGV.COLUMN.CFORMAT("MIX");
			TRANS_PENDING_DGV.COLUMN.DATE_FORMAT("DATE", "MMM DD, YYYY");			
		};
		
		
		// REGULAR Search Mode
		// _______________________________________________________________________
		JSOC_DIALOG.VAR_LOADING_SPINNER(true);
		JSCUS_SQL._RECORD._PSAPLAN.CUS_PENDING
		(
			JSVC_DATE.VAR_FORMAT(JSOC_ELEMENT.VAR_VALUE(SEARCH_TRANS_PENDING_DATE_DTP), 'YYYY-MM') + '-01 00:00:00.000000',
			CALLBACK_METHOD
		); 			
	};
	
	
	// ===============================================================
	// Request Search - PART.A
	// ===============================================================
	function HTTP_PENDING_NEW(CURRENT_ROW_INDEX)
	{
		// Close Current Modal
		// _______________________________________________________________________	
		JSOC_DIALOG.VAR_MODAL("NEW_TRANS_MODAL", false);		
		JSOC_DIALOG.VAR_MODAL("TRANS_MODAL", true);		
		
		// Populate Transaction Overview
		// _______________________________________________________________________	
		JSOC_CMB.VAR_SELECTED_VALUE(REC_TRANS_CAT_PRODUCT_ID_CMB, TRANS_PENDING_DGV.CELL.VALUE("CAT_PRODUCT_ID", CURRENT_ROW_INDEX));
		JSOC_ELEMENT.VAR_VALUE(REC_TRANS_QUANTITY_YIELD_TXT, TRANS_PENDING_DGV.CELL.VALUE_NVAL("CAT_PRODUCT_MIXTURE_QTY", CURRENT_ROW_INDEX));
		
		if(JSOC_ELEMENT.VAR_VALUE_NVAL("PLAN_MIX_TXT") == TRANS_PENDING_DGV.CELL.VALUE_NVAL("MIX", CURRENT_ROW_INDEX))
		{
			JSOC_ELEMENT.VAR_VALUE(REC_TRANS_QUANTITY_POST_TXT, TRANS_PENDING_DGV.CELL.VALUE_NVAL("QTY", CURRENT_ROW_INDEX));
			JSOC_ELEMENT.VAR_VALUE(REC_TRANS_QUANTITY_ACTUAL_TXT, TRANS_PENDING_DGV.CELL.VALUE_NVAL("QTY", CURRENT_ROW_INDEX));
			JSOC_ELEMENT.VAR_VALUE(REC_TRANS_QUANTITY_MIX_TXT, TRANS_PENDING_DGV.CELL.VALUE_NVAL("MIX", CURRENT_ROW_INDEX));	
		}
		else
		{
			JSOC_ELEMENT.VAR_VALUE(REC_TRANS_QUANTITY_POST_TXT, JSOC_ELEMENT.VAR_VALUE_NVAL("PLAN_MIX_TXT") * TRANS_PENDING_DGV.CELL.VALUE_NVAL("CAT_PRODUCT_MIXTURE_QTY", CURRENT_ROW_INDEX));
			JSOC_ELEMENT.VAR_VALUE(REC_TRANS_QUANTITY_ACTUAL_TXT, JSOC_ELEMENT.VAR_VALUE_NVAL(REC_TRANS_QUANTITY_POST_TXT));
			JSOC_ELEMENT.VAR_VALUE(REC_TRANS_QUANTITY_MIX_TXT, JSOC_ELEMENT.VAR_VALUE_NVAL("PLAN_MIX_TXT"));			
		}
		JSOC_ELEMENT.VAR_VALUE(REC_TRANS_COST_PPC_TXT, TRANS_PENDING_DGV.CELL.VALUE_NVAL("CAT_PRODUCT_ING_PRICE", CURRENT_ROW_INDEX));
		
		JSOC_ELEMENT.VAR_VALUE(REC_TRANS_DATE_SCHEDULE_DTP, TRANS_PENDING_DGV.CELL.VALUE_TO_DATE("DATE", CURRENT_ROW_INDEX));
		JSOC_ELEMENT.VAR_VALUE(REC_TRANS_DATE_POST_DTP, JSVC_DATE.VAR_NOW());
		JSOC_ELEMENT.VAR_VALUE(REC_TRANS_DATE_COMPLETE_DTP, "");
		JSOC_ELEMENT.VAR_VALUE(REC_TRANS_STATUS_CMB, 1);
		
		REC_TRANS_QUANTITY_ACTUAL_TXT.onchange();

		
		// Save newly created transaction
		// _______________________________________________________________________	
		PROPERTY_REC_TRANS_ID = -1;
		EDIT_HANDLER.REF_IS_MODIFIED = true;
		
		HTTP_SAVE
		(
			function(IS_SUCCESS, JSON_OBJECT)
			{
				// Abort if unable to save data
				// ...........................................
				if(IS_SUCCESS == false)
				{
					JSOC_DIALOG.VAR_MODAL("TRANS_MODAL", false);
					JSOC_DIALOG.VAR_MODAL("NEW_TRANS_MODAL", true);	
					
					alert('Unable to create transaction. Try again.');
					return;
				}
				
				// Display Modal
				// ...........................................
				PROPERTY_REC_TRANS_ID = JSRC_JSON.REF_READ_NVALL(JSON_OBJECT, "INSERTED_ID", 1);
				PROPERTY_EDIT_ROW_INDEX = 0;
				EDIT_HANDLER.REF_IS_MODIFIED = true;
				
				// Get Transaction Code
				// ...........................................
				JSOC_ELEMENT.VAR_VALUE("REC_TRANS_REFERENCE_CODE_TXT", JSRC_JSON.REF_READ(JSON_OBJECT, "REC_TRANS_REFERENCE_CODE", 1));
				TRANS_LIST_REFRESH_BBI.onclick();
			},
			true
		);
			
	};
	
	// ===============================================================
	// Request Search - PART.A
	// ===============================================================
	function HTTP_LIST_SEARCH()
	{
		// Callback method
		// _______________________________________________________________________
		var CALLBACK_METHOD = function(IS_SUCCESS, RESPONSE_STRING, KEEP_DGV_ITEMS = false)
		{
			// Disable Dialogs
			// ...........................................
			CUS_SEND_DIALOG_TRY_DISMISS();
			
			// Validate
			// ...........................................
			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;
			}
			
			// Back to DGV Top
			// ...........................................
			JSOC_ELEMENT.VAR_GET("TRANS_LIST_DGV").scrollIntoView
			(
				{
			  		behavior: 'auto', 
			  		block: 'start'
				}
			);		
			
			// JSON Empty (Clears the DataGrodView)
			// ...........................................
			if(JSRC_JSON.REF_READ(JSON_OBJECT, "JSON_COUNT") <= 0)
			{
				HTTP_MIXTURE_SEARCH();
				return;
			}
			
			// Populate DGV
			// ...........................................
			TRANS_LIST_DGV.POPULATE_FROM_QUERY
			(
					JSON_OBJECT, 
					'',
					KEEP_DGV_ITEMS,
					false,
					1,
					[2, 1]
			);
			
			// For Applying CFormat
			// ...........................................	
			TRANS_LIST_DGV.COLUMN.CFORMAT("REC_TRANSLIST_COST_BASE");
			TRANS_LIST_DGV.COLUMN.CFORMAT("REC_TRANSLIST_COST_PC");
			TRANS_LIST_DGV.COLUMN.CFORMAT("REC_TRANSLIST_COST_ALLOWANCE");
			TRANS_LIST_DGV.COLUMN.CFORMAT("REC_TRANSLIST_PRICE_NET");
			TRANS_LIST_DGV.COLUMN.CFORMAT("REC_TRANSLIST_ING_PRICE");
			TRANS_LIST_DGV.COLUMN.CFORMAT("REC_TRANSLIST_ING_NET_CONTENT");
			
			// Initialize
			// ...........................................
			var WEBTABLE_DATE = null;
			var CURRENT_CELL_VALUE = null;
			var ROW_INDEX = 0;
			JSOC_ELEMENT.VAR_VALUE
			(
				REC_TRANS_COST_TOTAL_TXT,
				TRANS_LIST_DGV.COLUMN.SUM("REC_TRANSLIST_PRICE_NET")
			);
			REC_TRANS_QUANTITY_ACTUAL_TXT.onchange();
			
			// Color Coding (Uncomment to use)
			// ...........................................
			//TRANS_LIST_DGV.CELL.ELEMENT("COLUMN_NAME", ROW_INDEX).style.color = "#1cc88a"; // Safe
			//TRANS_LIST_DGV.CELL.ELEMENT("COLUMN_NAME", ROW_INDEX).style.color = "#5a5c69"; // Neutral
			//TRANS_LIST_DGV.CELL.ELEMENT("COLUMN_NAME", ROW_INDEX).style.color = "#f6c23e"; // Caution
			//TRANS_LIST_DGV.CELL.ELEMENT("COLUMN_NAME", ROW_INDEX).style.color = "#e74a3b"; // Warning
			
	
			// Auto-total tool
			// ...........................................
			if(TRANS_LIST_DGV.COLUMN.TOTAL_IS_READY())
			{
				TRANS_LIST_DGV.COLUMN.TOTAL_REFRESH();
			}
		};
		
		
		// Actual Function
		// _______________________________________________________________________
		GET_MAP_ARRAY = JSCUS_SQL._RECORD._TRANS_LIST.CUS_PREPARE_MAPPED_ARRAY_FOR_GET();			
		JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_REC_TRANSLIST_REC_TRANS_ID', PROPERTY_REC_TRANS_ID);
		JSOC_DIALOG.VAR_LOADING_SPINNER(true);
				
		// REGULAR Search Mode
		// _______________________________________________________________________		
		JSCUS_SQL._RECORD._TRANS_LIST.CUS_GET
		(
			GET_MAP_ARRAY,
			CALLBACK_METHOD
		); 			

	};


	// ===============================================================
	// Request Search - PART.A
	// ===============================================================
	function HTTP_MIXTURE_SEARCH()
	{
		// Callback method
		// _______________________________________________________________________
		var CALLBACK_METHOD = function(IS_SUCCESS, RESPONSE_STRING, KEEP_DGV_ITEMS = false)
		{
			// Disable Dialogs
			// ...........................................
			CUS_SEND_DIALOG_TRY_DISMISS();
			
			// Validate
			// ...........................................
			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;
			}
			
			// Back to DGV Top
			// ...........................................
			JSOC_ELEMENT.VAR_GET("TRANS_LIST_DGV").scrollIntoView
			(
				{
			  		behavior: 'auto', 
			  		block: 'start'
				}
			);		
			
			// JSON Empty (Clears the DataGrodView)
			// ...........................................
			if(JSRC_JSON.REF_READ(JSON_OBJECT, "JSON_COUNT") <= 0)
			{
				TRANS_LIST_DGV.ROW.CLEAR();
				if(TRANS_LIST_DGV.COLUMN.TOTAL_IS_READY())
				{
					TRANS_LIST_DGV.COLUMN.TOTAL_REFRESH();
				}
				return;
			}
			
			
			// Initialize
			// ...........................................
			var WEBTABLE_DATE = null;
			var CURRENT_CELL_VALUE = null;
			var JSON_COUNT = JSRC_JSON.REF_READ_NVALL(JSON_OBJECT, "JSON_COUNT");
			var JSON_INDEX = 1;
			var ROW_INDEX = 0;
			
			
			// Populate DGV
			// ...........................................
			TRANS_LIST_DGV.ROW.CLEAR();
			while(JSON_COUNT >= JSON_INDEX)
			{
				TRANS_LIST_DGV.ROW.ADD();
				ROW_INDEX = TRANS_LIST_DGV.ROW.COUNT() - 1;
				
				TRANS_LIST_DGV.CELL.VALUE("ROW_COUNT", ROW_INDEX, ROW_INDEX + 1);
				TRANS_LIST_DGV.CELL.VALUE("REC_TRANSLIST_ID", ROW_INDEX, "-1");
				TRANS_LIST_DGV.CELL.VALUE("REC_TRANSLIST_REC_TRANS_ID", ROW_INDEX, PROPERTY_PARENT_ID);
				TRANS_LIST_DGV.CELL.VALUE("REC_TRANSLIST_CAT_PRODUCT_ID", ROW_INDEX, JSRC_JSON.REF_READ_NVALL(JSON_OBJECT, "CAT_MIXTURE_ING_CAT_PRODUCT_ID", JSON_INDEX));
				TRANS_LIST_DGV.CELL.VALUE("REC_TRANSLIST_CAT_UOM_ID", ROW_INDEX, JSRC_JSON.REF_READ_NVALL(JSON_OBJECT, "CAT_MIXTURE_ING_CAT_PRODUCT_ID_CAT_PRODUCT_CAT_UOM_ID", JSON_INDEX));
				
				TRANS_LIST_DGV.CELL.VALUE("CAT_PRODUCT_DESCRIPTION", ROW_INDEX, JSRC_JSON.REF_READ(JSON_OBJECT, "CAT_MIXTURE_ING_CAT_PRODUCT_ID_DESCRIPTION", JSON_INDEX));
				
				TRANS_LIST_DGV.CELL.VALUE("REC_TRANSLIST_COST_ACTUAL", ROW_INDEX, JSRC_JSON.REF_READ_NVAL(JSON_OBJECT, "CAT_MIXTURE_QUANTITY", JSON_INDEX) * JSOC_ELEMENT.VAR_VALUE_NVAL(REC_TRANS_QUANTITY_MIX_TXT));
				TRANS_LIST_DGV.CELL.VALUE("REC_TRANSLIST_COST_BASE", ROW_INDEX, JSRC_JSON.REF_READ_NVAL(JSON_OBJECT, "CAT_MIXTURE_QUANTITY", JSON_INDEX) * JSOC_ELEMENT.VAR_VALUE_NVAL(REC_TRANS_QUANTITY_MIX_TXT));
				TRANS_LIST_DGV.CELL.VALUE("REC_TRANSLIST_COST_DISCREPANCY", ROW_INDEX, 0);
				
				TRANS_LIST_DGV.CELL.VALUE("REC_TRANSLIST_ING_PRICE", ROW_INDEX, JSRC_JSON.REF_READ_NVAL(JSON_OBJECT, "CAT_MIXTURE_ING_CAT_PRODUCT_ID_CAT_PRODUCT_ING_PRICE", JSON_INDEX));
				TRANS_LIST_DGV.CELL.VALUE("REC_TRANSLIST_ING_NET_CONTENT", ROW_INDEX, JSRC_JSON.REF_READ_NVAL(JSON_OBJECT, "CAT_MIXTURE_ING_CAT_PRODUCT_ID_CAT_PRODUCT_ING_NET_CONTENT", JSON_INDEX));
				TRANS_LIST_DGV.CELL.VALUE("CAT_UOM_DESCRIPTION", ROW_INDEX, JSRC_JSON.REF_READ(JSON_OBJECT, "CAT_UOM_DESCRIPTION", JSON_INDEX));	
				
				TRANS_LIST_DGV.CELL.VALUE
				(
					"REC_TRANSLIST_COST_PC", ROW_INDEX, 
					JSRC_JSON.REF_READ_NVAL(JSON_OBJECT, "CAT_MIXTURE_ING_CAT_PRODUCT_ID_CAT_PRODUCT_ING_PRICE", JSON_INDEX) / 
					JSRC_JSON.REF_READ_NVAL(JSON_OBJECT, "CAT_MIXTURE_ING_CAT_PRODUCT_ID_CAT_PRODUCT_ING_NET_CONTENT", JSON_INDEX)
				);
				TRANS_LIST_DGV.CELL.VALUE
				(
					"REC_TRANSLIST_COST_ALLOWANCE", ROW_INDEX, 
					(
						JSRC_JSON.REF_READ_NVAL(JSON_OBJECT, "CAT_MIXTURE_ING_CAT_PRODUCT_ID_CAT_PRODUCT_ING_PRICE", JSON_INDEX) / 
						JSRC_JSON.REF_READ_NVAL(JSON_OBJECT, "CAT_MIXTURE_ING_CAT_PRODUCT_ID_CAT_PRODUCT_ING_NET_CONTENT", JSON_INDEX)
					) * 1.1
				);
				TRANS_LIST_DGV.CELL.VALUE
				(
					"REC_TRANSLIST_PRICE_NET", ROW_INDEX, 
					(
						JSRC_JSON.REF_READ_NVAL(JSON_OBJECT, "CAT_MIXTURE_ING_CAT_PRODUCT_ID_CAT_PRODUCT_ING_PRICE", JSON_INDEX) / 
						JSRC_JSON.REF_READ_NVAL(JSON_OBJECT, "CAT_MIXTURE_ING_CAT_PRODUCT_ID_CAT_PRODUCT_ING_NET_CONTENT", JSON_INDEX)
					) * 1.1 * JSRC_JSON.REF_READ_NVAL(JSON_OBJECT, "CAT_MIXTURE_QUANTITY", JSON_INDEX)
				);
				
				JSON_INDEX ++;	
			}


			// For Applying CFormat
			// ...........................................	
			TRANS_LIST_DGV.COLUMN.CFORMAT("REC_TRANSLIST_COST_BASE");
			TRANS_LIST_DGV.COLUMN.CFORMAT("REC_TRANSLIST_COST_ACTUAL");
			TRANS_LIST_DGV.COLUMN.CFORMAT("REC_TRANSLIST_COST_PC");
			TRANS_LIST_DGV.COLUMN.CFORMAT("REC_TRANSLIST_COST_ALLOWANCE");
			TRANS_LIST_DGV.COLUMN.CFORMAT("REC_TRANSLIST_PRICE_NET");
			TRANS_LIST_DGV.COLUMN.CFORMAT("REC_TRANSLIST_ING_PRICE");
			TRANS_LIST_DGV.COLUMN.CFORMAT("REC_TRANSLIST_ING_NET_CONTENT");
			

			
			// Color Coding (Uncomment to use)
			// ...........................................
			//TRANS_LIST_DGV.CELL.ELEMENT("COLUMN_NAME", ROW_INDEX).style.color = "#1cc88a"; // Safe
			//TRANS_LIST_DGV.CELL.ELEMENT("COLUMN_NAME", ROW_INDEX).style.color = "#5a5c69"; // Neutral
			//TRANS_LIST_DGV.CELL.ELEMENT("COLUMN_NAME", ROW_INDEX).style.color = "#f6c23e"; // Caution
			//TRANS_LIST_DGV.CELL.ELEMENT("COLUMN_NAME", ROW_INDEX).style.color = "#e74a3b"; // Warning
	
	
			// Auto-total tool
			// ...........................................
			if(TRANS_LIST_DGV.COLUMN.TOTAL_IS_READY())
			{
				TRANS_LIST_DGV.COLUMN.TOTAL_REFRESH();
			}
			EDIT_HANDLER.REF_IS_MODIFIED = true;
		};
		
		// Actual Function
		// _______________________________________________________________________
		GET_MAP_ARRAY = JSCUS_SQL._CATEGORY._MIXTURE.CUS_PREPARE_MAPPED_ARRAY_FOR_GET();			
		JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_CAT_MIXTURE_BASE_CAT_PRODUCT_ID', JSOC_CMB.VAR_SELECTED_VALUE('REC_TRANS_CAT_PRODUCT_ID_CMB') );
		JSOC_DIALOG.VAR_LOADING_SPINNER(true);
				
		// REGULAR Search Mode
		// _______________________________________________________________________		
		JSCUS_SQL._CATEGORY._MIXTURE.CUS_GET
		(
			GET_MAP_ARRAY,
			CALLBACK_METHOD
		); 			

	};
	
	

	// ===============================================================
	// Request Search - PART.A
	// ===============================================================
	function HTTP_EMPLOYEE_SEARCH()
	{
		// Callback Method
		// _______________________________________________________________________
		var CALLBACK_METHOD = function(IS_SUCCESS, RESPONSE_STRING)
		{
			// Disable Dialogs
			// ...........................................
			CUS_SEND_DIALOG_TRY_DISMISS();
			
			// Validate
			// ...........................................
			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;
			}
			
			// Back to DGV Top
			// ...........................................	
			JSOC_ELEMENT.VAR_GET("TRANS_EMPLOY_DGV").scrollIntoView
			(
				{
			  		behavior: 'auto', 
			  		block: 'start'
				}
			);		
			
			// JSON Empty (Clears the DataGrodView)
			// ...........................................
			if(JSRC_JSON.REF_READ(JSON_OBJECT, "JSON_COUNT") <= 0)
			{
				HTTP_SEARCH_LOAD_PAGING_UPDATE(0);
				TRANS_EMPLOY_DGV.ROW.CLEAR();
				if(TRANS_EMPLOY_DGV.COLUMN.TOTAL_IS_READY())
				{
					TRANS_EMPLOY_DGV.COLUMN.TOTAL_REFRESH();
				}
				return;
			}
			
			// Populate DGV
			// ...........................................
			TRANS_EMPLOY_DGV.POPULATE_FROM_QUERY
			(
					JSON_OBJECT, 
					'',
					false,
					false,
					1,
					[2, 1]
			);
			
			
			// Initialize
			// ...........................................
			var REC_TRANSEMPLOY_TASK_IN_ID = [1, 2, 3];
			var REC_TRANSEMPLOY_TASK_IN_TEXT = ['Baker', 'Ovenman', 'Packer'];
			var ROW_COUNT = TRANS_EMPLOY_DGV.ROW.COUNT();
			var ROW_INDEX = 0;
		
		
			// DateTime Format
			// ...........................................	
			while(ROW_COUNT > ROW_INDEX)
			{
				// Convert REC_TRANSEMPLOY_TASK to Text
				TRANS_EMPLOY_DGV.CELL.VALUE
				(
					"REC_TRANSEMPLOY_TASK", ROW_INDEX,
					REC_TRANSEMPLOY_TASK_IN_TEXT
					[
						REC_TRANSEMPLOY_TASK_IN_ID.indexOf
						(
							TRANS_EMPLOY_DGV.CELL.VALUE_NVALL("REC_TRANSEMPLOY_TASK", ROW_INDEX)
						)
					]
				);
				
				// To next loop
				ROW_INDEX ++;
			}
			
		
			// Color Coding (Uncomment to use)
			// ...........................................
			//TRANS_EMPLOY_DGV.CELL.ELEMENT("COLUMN_NAME", ROW_INDEX).style.color = "#1cc88a"; // Safe
			//TRANS_EMPLOY_DGV.CELL.ELEMENT("COLUMN_NAME", ROW_INDEX).style.color = "#5a5c69"; // Neutral
			//TRANS_EMPLOY_DGV.CELL.ELEMENT("COLUMN_NAME", ROW_INDEX).style.color = "#f6c23e"; // Caution
			//TRANS_EMPLOY_DGV.CELL.ELEMENT("COLUMN_NAME", ROW_INDEX).style.color = "#e74a3b"; // Warning
	
	
			// Auto-total tool
			// ...........................................
			if(TRANS_EMPLOY_DGV.COLUMN.TOTAL_IS_READY())
			{
				TRANS_EMPLOY_DGV.COLUMN.TOTAL_REFRESH();
			}			
		};
		
		// Actual Function
		// _______________________________________________________________________
		GET_MAP_ARRAY = JSCUS_SQL._RECORD._TRANS_EMPLOY.CUS_PREPARE_MAPPED_ARRAY_FOR_GET();	
		JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_REC_TRANSEMPLOY_REC_TRANS_ID', PROPERTY_REC_TRANS_ID);	
		JSCUS_SQL._RECORD._TRANS_EMPLOY.CUS_GET
		(
			GET_MAP_ARRAY,
			CALLBACK_METHOD,
			true
		); 			
	};

	
	FORM_CHILD_INITIALIZE();