// NOTE: [This JS must loaded as last item]


	// ===============================================================
	// Initialize Form
	// ===============================================================
	function FORM_LOAD()
	{
		// Initialize
		// _______________________________________________________________________	
		var DGV_ELEMENT_CHILD = null;
		var DGV_ELEMENT_CHILD_INDEX = 0;
		
		
		// Load DataGridVuew
		// _______________________________________________________________________	
		JSTOOL_CUSTOMIZE.DGVREORDER.APPLY("PSACTUAL_DGV");
		PSACTUAL_DGV = DataGridView("PSACTUAL_DGV");
		NEW_SCHEDULE_PREPARE_DGV_COLS_COUNT();
		//PSACTUAL_DGV.SETTING_ALLOW_USER_TO_ADD_ROWS = true;
		//PSACTUAL_DGV.SETTING_EDITABLE = true;
	

		// Auto Compute
		// _______________________________________________________________________	
		var AUTO_CALC = JSTOOL_CUSTOMIZE.DGVCALC.APPLY(PSACTUAL_DGV);
		var AUTO_COMPUTE_BLOCKS = AUTO_CALC[0];
		var CURRENT_AUTO_COMPUTE = '';
		var CURRENT_POSTFIX = '';
		var COLUMN_INDEX = 1;
		
		while(PSACTUAL_DGV_COLUMN_COUNTS > COLUMN_INDEX)
		{
			CURRENT_POSTFIX = JSVC_STR.VAR_FIX(COLUMN_INDEX.toString(), null, 2, 2, "0");
			if(COLUMN_INDEX == 1)
			{
				CURRENT_AUTO_COMPUTE = '`REC_PSACTUAL_QTY_' + CURRENT_POSTFIX + '`';
			}
			else
			{
				CURRENT_AUTO_COMPUTE = CURRENT_AUTO_COMPUTE  + ' + `REC_PSACTUAL_QTY_' + CURRENT_POSTFIX + '`';
			}
			COLUMN_INDEX ++;
		}
		
		AUTO_COMPUTE_BLOCKS.push
		(
			[
				'TOTAL', 
				CURRENT_AUTO_COMPUTE,
				true
			]
		);
		AUTO_COMPUTE_BLOCKS.push
		(
			[
				'YIELD_RATE', 
				'`TOTAL` / `YIELD`',
				true
			]
		);
		AUTO_COMPUTE_BLOCKS.push
		(
			[
				'SKU_RATE', 
				'`TOTAL` * 100 / `TOTAL:TOTAL`',
				true
			]
		);		
		AUTO_COMPUTE_BLOCKS.push
		(
			[
				'GM', 
				'`PASS_ON` - `COST`',
				true
			]
		);
		AUTO_COMPUTE_BLOCKS.push
		(
			[
				'GM_PERCENT', 
				'`GM` / `TOTAL:GM`',
				true
			]
		);
		
		AUTO_COMPUTE_BLOCKS.push
		(
			[
				'GGROSS', 
				'`TOTAL` * `GM`',
				true
			]
		);
		
		AUTO_COMPUTE_BLOCKS.push
		(
			[
				'97PERCENT', 
				'`TOTAL` * `GM` * 0.97',
				true
			]
		);
		
		AUTO_COMPUTE_BLOCKS.push
		(
			[
				'COGS', 
				'`COST` / `PASS_ON`', 
				true
			]
		);
		PSACTUAL_DGV.COLUMN.AUTO_COMPUTE_CREATE(AUTO_COMPUTE_BLOCKS);
		
		
		// Auto Total
		// _______________________________________________________________________	
		var AUTO_TOTAL_COLUMNS = AUTO_CALC[1];
		var AUTO_TOTAL_COPY_TO_ELEMENT = AUTO_CALC[2];
		
		/*
			AUTO_TOTAL_COLUMNS.push('COLUMN_A');					// Column to Auto Total
			AUTO_TOTAL_COPY_TO_ELEMENT.push(["COL_ID", "ELEM_ID"]);	// Source Column, Element ID where total is copied to
		*/
		
		AUTO_TOTAL_COLUMNS.push('TOTAL');	
		AUTO_TOTAL_COLUMNS.push('GM');
		AUTO_TOTAL_COLUMNS.push('GGROSS');
		AUTO_TOTAL_COLUMNS.push('97PERCENT');
		PSACTUAL_DGV.COLUMN.TOTAL_CREATE
		(
			AUTO_TOTAL_COLUMNS,
			'CAT_PRODUCT_DESCRIPTION',									// Column, where you want to display the caption `Total:`.
			AUTO_TOTAL_COPY_TO_ELEMENT
		);		
		
		
		// DGV Events
		// _______________________________________________________________________			
		PSACTUAL_DGV.ON_CLICK
		(			
			function(COL_INDEX, ROW_INDEX)
			{
				var ADDED_COUNT = "0";
				var REC_PSCHEDULE_QTY = null
				for(var ROW_COUNT = 1; ROW_COUNT < 31; ROW_COUNT++)
				{
					if(ROW_COUNT.toString().length < 2)
						ADDED_COUNT = "0" + ROW_COUNT.toString();
					else
						ADDED_COUNT = ROW_COUNT.toString();

					if(PSACTUAL_DGV.COLUMN.NAME(COL_INDEX) == "REC_PSACTUAL_QTY_"+ADDED_COUNT)
					{
						REC_PSCHEDULE_QTY = PSACTUAL_DGV.CELL.ELEMENT("REC_PSACTUAL_QTY_"+ADDED_COUNT, ROW_INDEX);
						REC_PSCHEDULE_QTY.select();

						break;
					}
				}
			}
		);   

		PSACTUAL_DGV.ON_CHANGE
		(			
			function(COL_INDEX, ROW_INDEX)
			{
				// Update Change Status
				// ...........................................
				EDIT_HANDLER.REF_IS_MODIFIED = true;
				
				// Initialize
				// ...........................................
				var COLUMN_NAME = PSACTUAL_DGV.COLUMN.NAME(COL_INDEX);
				
				// Abort if not qty changes
				// ...........................................
				if(JSVC_STR.VAR_CONTAINS_LEFT(COLUMN_NAME, "REC_PSACTUAL_QTY_") == false)
				{
					return;
				}				
				
				// Update Values
				// ...........................................
				PSACTUAL_DGV.COLUMN.AUTO_COMPUTE_EXECUTE(ROW_INDEX);
				PSACTUAL_DGV.COLUMN.TOTAL_REFRESH();
			}
		);
		
		
		// Show Progress
		// _______________________________________________________________________	
		JSOC_DIALOG.VAR_LOADING_SPINNER(true);
		
		
		// Requires Authentication
		// _______________________________________________________________________	
		PAGE_AUTH_REQUIRES_LOGIN(HTTP_POPULATE_RESULT);			


		
		
		// Foreign Key [Fixed]
		// _______________________________________________________________________	
		if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("REC_PSACTUAL_ACC_GROUP_ID_CMB") == true)
		{
			// Set Default Item, if used in DataGridView
			JSOC_CMB.VAR_ADD("REC_PSACTUAL_ACC_GROUP_ID_CMB", FLAG_BIGINT_NULL.toString(), "-");
			// To use custom populate
			// var GET_MAP_ARRAY = JSCUS_SQL._ACCOUNT._GROUP.CUS_PREPARE_MAPPED_ARRAY_FOR_GET();
			// JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_', JSCUS_SQL._SYSTEM._STATUS.STATUS_ACTIVE);	
			JSCUS_SQL._ACCOUNT._GROUP.CUS_GET_THEN_POPULATE_COMBOBOX
			(
				function(IS_SUCCESS, RESPONSE_STRING)
				{
					// Set Default index
					JSOC_CMB.VAR_SELECTED_INDEX("REC_PSACTUAL_ACC_GROUP_ID_CMB", 0);
					
					// Clone to DGV Cell if ComboBox Exists
					if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("PSACTUAL_DGV_ROW_REC_PSACTUAL_ACC_GROUP_ID") == true)
					{
						DGV_ELEMENT_CHILD = JSOC_ELEMENT.VAR_SUB_ELEMENTS("PSACTUAL_DGV_ROW_REC_PSACTUAL_ACC_GROUP_ID");
						DGV_ELEMENT_CHILD_INDEX = 0;
						while(DGV_ELEMENT_CHILD.length > DGV_ELEMENT_CHILD_INDEX)
						{
							if
							(
								DGV_ELEMENT_CHILD[DGV_ELEMENT_CHILD_INDEX].tagName == "SELECT" &&
								JSVC_STR.VAR_IS_BLANK(DGV_ELEMENT_CHILD[DGV_ELEMENT_CHILD_INDEX].id.toString())
							)
							{
								JSOC_ELEMENT.VAR_INNER_HTML
								(
									DGV_ELEMENT_CHILD[DGV_ELEMENT_CHILD_INDEX], 
									JSOC_ELEMENT.VAR_INNER_HTML("REC_PSACTUAL_ACC_GROUP_ID_CMB")
								);
							}	
							DGV_ELEMENT_CHILD_INDEX ++;
						}
					}
					
					// Copy items to edit control
					if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("SEARCH_REC_PSACTUAL_ACC_GROUP_ID_CMB") == true)
					{
						JSOC_CMB.VAR_CLEAR("SEARCH_REC_PSACTUAL_ACC_GROUP_ID_CMB");
						JSOC_CMB.VAR_ADD("SEARCH_REC_PSACTUAL_ACC_GROUP_ID_CMB", FLAG_BIGINT_NULL.toString(),"Any Group");
						JSOC_ELEMENT.VAR_INNER_HTML("SEARCH_REC_PSACTUAL_ACC_GROUP_ID_CMB", JSOC_ELEMENT.VAR_INNER_HTML("REC_PSACTUAL_ACC_GROUP_ID_CMB"), true);
						JSOC_CMB.VAR_SELECTED_INDEX("SEARCH_REC_PSACTUAL_ACC_GROUP_ID_CMB", 0);
						$('.BSSP_SEARCH_REC_PSACTUAL_ACC_GROUP_ID_CMB').selectpicker('refresh');
					}				
					
					// Update Modal Settings
					if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("PSACTUAL_MODAL") == true)
					{
						INPUT_INSTANCE = JSOC_WINDOW.VAR_INPUT_INSTANCE_CLONE("PSACTUAL_MODAL");
					}
					
					// Forward populate complete event.
					$('.BSSP_REC_PSACTUAL_ACC_GROUP_ID_CMB').selectpicker('refresh');
					HTTP_POPULATE_RESULT();		
				},
				"REC_PSACTUAL_ACC_GROUP_ID_CMB",
				true, 2
				// , GET_MAP_ARRAY 
			);
		}
		if
		(
			JSOC_ELEMENT.VAR_ELEMENT_EXISTS("SEARCH_REC_PSACTUAL_ACC_GROUP_ID_CMB") == true &&
			JSOC_ELEMENT.VAR_ELEMENT_EXISTS("REC_PSACTUAL_ACC_GROUP_ID_CMB") == false
		)
		{
			JSOC_CMB.VAR_CLEAR("SEARCH_REC_PSACTUAL_ACC_GROUP_ID_CMB");
			JSOC_CMB.VAR_ADD("SEARCH_REC_PSACTUAL_ACC_GROUP_ID_CMB", FLAG_BIGINT_NULL.toString(),"Any Group");
			// To use custom populate
			// var GET_MAP_ARRAY = JSCUS_SQL._ACCOUNT._GROUP.CUS_PREPARE_MAPPED_ARRAY_FOR_GET();
			// JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_', JSCUS_SQL._SYSTEM._STATUS.STATUS_ACTIVE);	
			JSCUS_SQL._ACCOUNT._GROUP.CUS_GET_THEN_POPULATE_COMBOBOX
			(
				function(IS_SUCCESS, RESPONSE_STRING)
				{
					// Set Default index
					JSOC_CMB.VAR_SELECTED_INDEX("SEARCH_REC_PSACTUAL_ACC_GROUP_ID_CMB", 0);
					
					// Forward populate complete event.		
					$('.BSSP_SEARCH_REC_PSACTUAL_ACC_GROUP_ID_CMB').selectpicker('refresh');
					HTTP_POPULATE_RESULT();
				},		
				"SEARCH_REC_PSACTUAL_ACC_GROUP_ID_CMB",
				true, 2
				// , GET_MAP_ARRAY 
			);
		}
		
		// Foreign Key [Fixed]
		// _______________________________________________________________________	
		if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("REC_PSACTUAL_CAT_PRODUCT_ID_CMB") == true)
		{
			// Set Default Item, if used in DataGridView
			JSOC_CMB.VAR_ADD("REC_PSACTUAL_CAT_PRODUCT_ID_CMB", FLAG_BIGINT_NULL.toString(), "-");
			// To use custom populate
			// var GET_MAP_ARRAY = JSCUS_SQL._CATEGORY._PRODUCT.CUS_PREPARE_MAPPED_ARRAY_FOR_GET();
			// JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_', JSCUS_SQL._SYSTEM._STATUS.STATUS_ACTIVE);	
			JSCUS_SQL._CATEGORY._PRODUCT.CUS_GET_THEN_POPULATE_COMBOBOX
			(
				function(IS_SUCCESS, RESPONSE_STRING)
				{
					// Set Default index
					JSOC_CMB.VAR_SELECTED_INDEX("REC_PSACTUAL_CAT_PRODUCT_ID_CMB", 0);
					
					// Clone to DGV Cell if ComboBox Exists
					if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("PSACTUAL_DGV_ROW_REC_PSACTUAL_CAT_PRODUCT_ID") == true)
					{
						DGV_ELEMENT_CHILD = JSOC_ELEMENT.VAR_SUB_ELEMENTS("PSACTUAL_DGV_ROW_REC_PSACTUAL_CAT_PRODUCT_ID");
						DGV_ELEMENT_CHILD_INDEX = 0;
						while(DGV_ELEMENT_CHILD.length > DGV_ELEMENT_CHILD_INDEX)
						{
							if
							(
								DGV_ELEMENT_CHILD[DGV_ELEMENT_CHILD_INDEX].tagName == "SELECT" &&
								JSVC_STR.VAR_IS_BLANK(DGV_ELEMENT_CHILD[DGV_ELEMENT_CHILD_INDEX].id.toString())
							)
							{
								JSOC_ELEMENT.VAR_INNER_HTML
								(
									DGV_ELEMENT_CHILD[DGV_ELEMENT_CHILD_INDEX], 
									JSOC_ELEMENT.VAR_INNER_HTML("REC_PSACTUAL_CAT_PRODUCT_ID_CMB")
								);
							}	
							DGV_ELEMENT_CHILD_INDEX ++;
						}
					}
					
					// Copy items to edit control
					if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("SEARCH_REC_PSACTUAL_CAT_PRODUCT_ID_CMB") == true)
					{
						JSOC_CMB.VAR_CLEAR("SEARCH_REC_PSACTUAL_CAT_PRODUCT_ID_CMB");
						JSOC_CMB.VAR_ADD("SEARCH_REC_PSACTUAL_CAT_PRODUCT_ID_CMB", FLAG_BIGINT_NULL.toString(),"Any Product");
						JSOC_ELEMENT.VAR_INNER_HTML("SEARCH_REC_PSACTUAL_CAT_PRODUCT_ID_CMB", JSOC_ELEMENT.VAR_INNER_HTML("REC_PSACTUAL_CAT_PRODUCT_ID_CMB"), true);
						JSOC_CMB.VAR_SELECTED_INDEX("SEARCH_REC_PSACTUAL_CAT_PRODUCT_ID_CMB", 0);
						$('.BSSP_SEARCH_REC_PSACTUAL_CAT_PRODUCT_ID_CMB').selectpicker('refresh');
					}				
					
					// Update Modal Settings
					if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("PSACTUAL_MODAL") == true)
					{
						INPUT_INSTANCE = JSOC_WINDOW.VAR_INPUT_INSTANCE_CLONE("PSACTUAL_MODAL");
					}
					
					// Forward populate complete event.
					$('.BSSP_REC_PSACTUAL_CAT_PRODUCT_ID_CMB').selectpicker('refresh');
					HTTP_POPULATE_RESULT();		
				},
				"REC_PSACTUAL_CAT_PRODUCT_ID_CMB",
				true, 2
				// , GET_MAP_ARRAY 
			);
		}
		if
		(
			JSOC_ELEMENT.VAR_ELEMENT_EXISTS("SEARCH_REC_PSACTUAL_CAT_PRODUCT_ID_CMB") == true &&
			JSOC_ELEMENT.VAR_ELEMENT_EXISTS("REC_PSACTUAL_CAT_PRODUCT_ID_CMB") == false
		)
		{
			JSOC_CMB.VAR_CLEAR("SEARCH_REC_PSACTUAL_CAT_PRODUCT_ID_CMB");
			JSOC_CMB.VAR_ADD("SEARCH_REC_PSACTUAL_CAT_PRODUCT_ID_CMB", FLAG_BIGINT_NULL.toString(),"Any Product");
			// To use custom populate
			// var GET_MAP_ARRAY = JSCUS_SQL._CATEGORY._PRODUCT.CUS_PREPARE_MAPPED_ARRAY_FOR_GET();
			// JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_', JSCUS_SQL._SYSTEM._STATUS.STATUS_ACTIVE);	
			JSCUS_SQL._CATEGORY._PRODUCT.CUS_GET_THEN_POPULATE_COMBOBOX
			(
				function(IS_SUCCESS, RESPONSE_STRING)
				{
					// Set Default index
					JSOC_CMB.VAR_SELECTED_INDEX("SEARCH_REC_PSACTUAL_CAT_PRODUCT_ID_CMB", 0);
					
					// Forward populate complete event.		
					$('.BSSP_SEARCH_REC_PSACTUAL_CAT_PRODUCT_ID_CMB').selectpicker('refresh');
					HTTP_POPULATE_RESULT();
				},		
				"SEARCH_REC_PSACTUAL_CAT_PRODUCT_ID_CMB",
				true, 2
				// , GET_MAP_ARRAY 
			);
		}
		
		// Status		
		// _______________________________________________________________________	
		if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("SEARCH_REC_PSACTUAL_STATUS_CMB") == true)
		{
			JSOC_CMB.VAR_CLEAR("SEARCH_REC_PSACTUAL_STATUS_CMB");
			JSOC_CMB.VAR_ADD("SEARCH_REC_PSACTUAL_STATUS_CMB", "0","Any Status");
			JSOC_CMB.VAR_ADD("SEARCH_REC_PSACTUAL_STATUS_CMB", "1","Inactive");
			JSOC_CMB.VAR_ADD("SEARCH_REC_PSACTUAL_STATUS_CMB", "2","Active");
			// JSOC_CMB.VAR_ADD("SEARCH_REC_PSACTUAL_STATUS_CMB", "3","Closed");
			// JSOC_CMB.VAR_ADD("SEARCH_REC_PSACTUAL_STATUS_CMB", "4","Completed");
			JSOC_CMB.VAR_SELECTED_VALUE("SEARCH_REC_PSACTUAL_STATUS_CMB", "2");
		}			
		
		if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("REC_PSACTUAL_STATUS_CMB") == true)
		{
			JSOC_CMB.VAR_CLEAR("REC_PSACTUAL_STATUS_CMB");
			JSOC_CMB.VAR_ADD("REC_PSACTUAL_STATUS_CMB", "1","Inactive");
			JSOC_CMB.VAR_ADD("REC_PSACTUAL_STATUS_CMB", "2","Active");
			// JSOC_CMB.VAR_ADD("REC_PSACTUAL_STATUS_CMB", "3","Closed");
			// JSOC_CMB.VAR_ADD("REC_PSACTUAL_STATUS_CMB", "4","Completed");
			JSOC_CMB.VAR_SELECTED_VALUE("REC_PSACTUAL_STATUS_CMB", "2");
		}
		
		
		
		// Get Modal Instance (Add Modal Adjustment before this line)
		// _______________________________________________________________________	
		if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("PSACTUAL_MODAL") == true)
		{
			INPUT_INSTANCE = JSOC_WINDOW.VAR_INPUT_INSTANCE_CLONE("PSACTUAL_MODAL");
		}
		
		// Add Search Events for the SEARCH_DESCRIPTION_TXT - TextInput
		// _______________________________________________________________________		
		if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("SEARCH_REC_PSACTUAL_ID_TXT"))
		{
			var SEARCH_REC_PSACTUAL_ID_TXT = JSOC_ELEMENT.VAR_GET("SEARCH_REC_PSACTUAL_ID_TXT");
			SEARCH_REC_PSACTUAL_ID_TXT.onkeydown = 
				function() 
				{ 
					return function(e) 
					{
						if
						(
							(
								e.which == 13 || 
								e.keyCode == 13 || 
								e.key === "Enter"
							) &&
							JSOC_ELEMENT.VAR_ELEMENT_EXISTS("SEARCH_CMD")
						)
						{
							HTTP_SEARCH();
						}
					};
				}();
		}
		
		
		// Load Parent Data (if Exists)
		// _______________________________________________________________________	
		HTTP_PARENT_LOAD();
		
		
		// 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
		{
			// Disable Dialogs
			if(CUS_SEND_REQUEST_HAS_QUEUE() == false)
			{
				//HTTP_SEARCH();
			}
		}
	};

	
	// ===============================================================
	// Request Search - PART.A
	// ===============================================================
	function HTTP_SEARCH(NEW_ITEM_ID, EDITED_ROW_INDEX)
	{
		// Optional Value
		// _______________________________________________________________________
		NEW_ITEM_ID = JSVC_VAR.VAR_OPTIONAL(NEW_ITEM_ID, -1);
		EDITED_ROW_INDEX = JSVC_VAR.VAR_OPTIONAL(EDITED_ROW_INDEX, -1);
		
		// Parameter Validate
		// _______________________________________________________________________
		JSVC_AV.VAR_VALIDATE_ARGUMENT_TYPE(NEW_ITEM_ID, 'NEW_ITEM_ID', 'NUMERIC');
		JSVC_AV.VAR_VALIDATE_ARGUMENT_TYPE(EDITED_ROW_INDEX, 'EDITED_ROW_INDEX', 'NUMERIC');
		
		
		// Actual Function
		// _______________________________________________________________________
		GET_MAP_ARRAY = JSCUS_SQL._RECORD._PSACTUAL.CUS_PREPARE_MAPPED_ARRAY_FOR_GET();	
		
		
		// Search Inserted / Saved Record
		// _______________________________________________________________________
		JSOC_DIALOG.VAR_LOADING_SPINNER(true);
		NEW_SCHEDULE_PREPARE_DGV_COLS
		(
			JSOC_ELEMENT.VAR_VALUE('SEARCH_REC_PSACTUAL_DATE_FROM_DTP')
		);
		
	};
				
	// ===============================================================
	// Begin Processing the Page
	// ===============================================================
	FORM_INITIALIZE();
	FORM_LOAD();