// 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("PO_ORDER_DGV");
		PO_ORDER_DGV = DataGridView("PO_ORDER_DGV");
		//PO_ORDER_DGV.SETTING_ALLOW_USER_TO_ADD_ROWS = true;
		//PO_ORDER_DGV.SETTING_EDITABLE = true;
	

		// Auto Compute
		// _______________________________________________________________________	
		var AUTO_CALC = JSTOOL_CUSTOMIZE.DGVCALC.APPLY(PO_ORDER_DGV);
		var AUTO_COMPUTE_BLOCKS = AUTO_CALC[0];
			
		/*
			// Sample Code
			AUTO_COMPUTE_BLOCKS.push
			(
				[
					'COLUMN_A', 					// Target Column
					'`COLUMN_1` + `COLUMN_2`',		// Math
					true							// Apply CFormat
				]
			);
		*/
		// PO_ORDER_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('REC_PORDER_LIST_PRICE_TOTAL');					// Column to Auto Total
		PO_ORDER_DGV.COLUMN.TOTAL_CREATE
		(
			AUTO_TOTAL_COLUMNS,
			'REC_PORDER_LIST_PRICE_PPACK',									// Column, where you want to display the caption `Total:`.
			AUTO_TOTAL_COPY_TO_ELEMENT
		);		
		
		
		
		// DGV OnClick Event
		// _______________________________________________________________________		
		PO_ORDER_DGV.ON_CLICK
		(			
			function(COL_INDEX, ROW_INDEX)
			{
			}
		);   


		// DGV OnChange Event
		// _______________________________________________________________________	
		PO_ORDER_DGV.ON_CHANGE
		(			
			function(COL_INDEX, ROW_INDEX)
			{
			}
		);

		
		// Show Progress
		// _______________________________________________________________________	
		JSOC_DIALOG.VAR_LOADING_SPINNER(true);
		
		
		// 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();
			
		// 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()
	{
		// Actual Function
		// _______________________________________________________________________
		GET_MAP_ARRAY = JSCUS_SQL._RECORD._PO_ORDER.CUS_PREPARE_MAPPED_ARRAY_FOR_GET();	
		JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_REC_PORDER_ID', PROPERTY_REC_PORDER_ID);
		JSCUS_SQL._RECORD._PO_ORDER.CUS_GET
		(
			GET_MAP_ARRAY,
			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)
				if(JSRC_JSON.REF_READ(JSON_OBJECT, "JSON_COUNT") <= 0)
				{
					return;
				}
				
				// Populate Data
				JSOC_ELEMENT.VAR_INNER_HTML
				(
					"REC_PORDER_CODE",
					JSRC_JSON.REF_READ(JSON_OBJECT, "REC_PORDER_CODE", 1) 
				);
				JSOC_ELEMENT.VAR_INNER_HTML
				(
					"REC_PORDER_INVOICE_CODE",
					JSRC_JSON.REF_READ(JSON_OBJECT, "REC_PORDER_INVOICE_CODE", 1) 
				);				
				JSOC_ELEMENT.VAR_INNER_HTML
				(
					"REC_PORDER_AMOUNT",
					JSVC_NUM.VAR_CFORMAT
					(
						JSRC_JSON.REF_READ_NVAL(JSON_OBJECT, "REC_PORDER_AMOUNT", 1) 
					)
				);				
				JSOC_ELEMENT.VAR_INNER_HTML
				(
					"REC_PORDER_REMARKS",
					JSRC_JSON.REF_READ(JSON_OBJECT, "REC_PORDER_REMARKS", 1) 
				);										
				JSOC_ELEMENT.VAR_INNER_HTML
				(
					"REC_PORDER_DATE_EXPECT",
					JSVC_DATE.VAR_FORMAT
					(
						JSRC_JSON.REF_READ_TO_DATE(JSON_OBJECT, "REC_PORDER_DATE_EXPECT", 1),
						"MM/DD/YYYY"
					)
				);					
				JSOC_ELEMENT.VAR_INNER_HTML
				(
					"REC_PORDER_DATE_DELIVERY",
					JSVC_DATE.VAR_FORMAT
					(
						JSRC_JSON.REF_READ_TO_DATE(JSON_OBJECT, "REC_PORDER_DATE_DELIVERY", 1),
						"MM/DD/YYYY"
					)
				);	
				JSOC_ELEMENT.VAR_INNER_HTML
				(
					"REC_PORDER_DATE_REQUEST",
					JSVC_DATE.VAR_FORMAT
					(
						JSRC_JSON.REF_READ_TO_DATE(JSON_OBJECT, "REC_PORDER_DATE_REQUEST", 1),
						"MM/DD/YYYY"
					)
				);					

				// ~~~~~
				
				JSOC_ELEMENT.VAR_INNER_HTML
				(
					"CAT_COMPANY_DESCRIPTION",
					JSRC_JSON.REF_READ(JSON_OBJECT, "CAT_COMPANY_DESCRIPTION", 1) 
				);		
				JSOC_ELEMENT.VAR_INNER_HTML
				(
					"CAT_COMPANY_TIN",
					JSRC_JSON.REF_READ(JSON_OBJECT, "CAT_COMPANY_TIN", 1) 
				);	
				JSOC_ELEMENT.VAR_INNER_HTML
				(
					"CAT_COMPANY_ADDRESS",
					JSRC_JSON.REF_READ(JSON_OBJECT, "CAT_COMPANY_ADDRESS", 1) 
				);	
				JSOC_ELEMENT.VAR_INNER_HTML
				(
					"CAT_COMPANY_MOBILE",
					JSRC_JSON.REF_READ(JSON_OBJECT, "CAT_COMPANY_MOBILE", 1) 
				);	
				JSOC_ELEMENT.VAR_INNER_HTML
				(
					"CAT_COMPANY_PHONE",
					JSRC_JSON.REF_READ(JSON_OBJECT, "CAT_COMPANY_PHONE", 1) 
				);	
				JSOC_ELEMENT.VAR_INNER_HTML
				(
					"CAT_COMPANY_FAX",
					JSRC_JSON.REF_READ(JSON_OBJECT, "CAT_COMPANY_FAX", 1) 
				);	
				JSOC_ELEMENT.VAR_INNER_HTML
				(
					"CAT_COMPANY_EMAIL",
					JSRC_JSON.REF_READ(JSON_OBJECT, "CAT_COMPANY_EMAIL", 1) 
				);	
				JSOC_ELEMENT.VAR_INNER_HTML
				(
					"CAT_COMPANY_WEBSITE",
					JSRC_JSON.REF_READ(JSON_OBJECT, "CAT_COMPANY_WEBSITE", 1) 
				);	
				JSOC_ELEMENT.VAR_INNER_HTML
				(
					"CAT_COMPANY_SNS_PAGE",
					JSRC_JSON.REF_READ(JSON_OBJECT, "CAT_COMPANY_SNS_PAGE", 1) 
				);	
				JSOC_ELEMENT.VAR_INNER_HTML
				(
					"ACC_ACCOUNT_NAME",
					JSRC_JSON.REF_READ(JSON_OBJECT, "ACC_ACCOUNT_NAME", 1) 
				);	
			},
			true
		); 			
		
		
		// Populate Lines
		// _______________________________________________________________________
		GET_MAP_ARRAY = JSCUS_SQL._RECORD._PO_ORDER_LIST.CUS_PREPARE_MAPPED_ARRAY_FOR_GET();
		JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_REC_PORDER_LIST_REC_PORDER_ID', PROPERTY_REC_PORDER_ID);
		JSCUS_SQL._RECORD._PO_ORDER_LIST.CUS_GET
		(
			GET_MAP_ARRAY,
			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)
				// ...........................................
				if(JSRC_JSON.REF_READ(JSON_OBJECT, "JSON_COUNT") <= 0)
				{
					return;
				}
				
				// Populate DGV
				// ...........................................
				PO_ORDER_DGV.POPULATE_FROM_QUERY
				(
						JSON_OBJECT, 
						'',
						false,
						false,
						1,
						[2, 1]
				);
				
				// Auto-total tool
				// ...........................................
				if(PO_ORDER_DGV.COLUMN.TOTAL_IS_READY())
				{
					PO_ORDER_DGV.COLUMN.TOTAL_REFRESH();
				}
			},
			true
		); 		
	};
	
				
	// ===============================================================
	// Begin Processing the Page
	// ===============================================================
	FORM_INITIALIZE();
	FORM_LOAD();