var PO_ORDER_LIST_DGV = null;
var ADD_ITEM_BBI = null;
						
						
	function FORM_CHILD_INITIALIZE()
	{		
		// Button 
		ADD_ITEM_BBI = JSOC_ELEMENT.VAR_GET("ADD_ITEM_BBI");  
		ADD_ITEM_BBI.onclick = function()
		{
			SELECT_MODAL_CATEGORY_PRODUCT.SHOW
			(
				function(PRODUCT_ID, PRODUCT_DESCRIPTION, LINE_MAP_ARRAY)
				{
					// Initialize
					PO_ORDER_LIST_DGV.ROW.ADD();
					var ADD_ITEM_ROW_INDEX = PO_ORDER_LIST_DGV.ROW.COUNT() - 1;
					
					// Abort if exists
					if(PO_ORDER_LIST_DGV.COLUMN.FIND("REC_PORDER_LIST_CAT_PRODUCT_ID", PRODUCT_ID) >= 0)
					{
						alert('This item already exists');
						return;
					}
					
					// Set Value
					PO_ORDER_LIST_DGV.CELL.VALUE("REC_PORDER_LIST_ID", ADD_ITEM_ROW_INDEX, -1);
					PO_ORDER_LIST_DGV.CELL.VALUE("REC_PORDER_LIST_REC_PORDER_ID", ADD_ITEM_ROW_INDEX, PROPERTY_REC_PORDER_ID);
					PO_ORDER_LIST_DGV.CELL.VALUE("REC_PORDER_LIST_CAT_COMPANY_ID", ADD_ITEM_ROW_INDEX, 0);
					PO_ORDER_LIST_DGV.CELL.VALUE("REC_PORDER_LIST_CAT_PRODUCT_ID", ADD_ITEM_ROW_INDEX, PRODUCT_ID);
					PO_ORDER_LIST_DGV.CELL.VALUE("CAT_PRODUCT_DESCRIPTION", ADD_ITEM_ROW_INDEX, PRODUCT_DESCRIPTION);
					PO_ORDER_LIST_DGV.CELL.VALUE("REC_PORDER_LIST_QUANTITY", ADD_ITEM_ROW_INDEX, 0);
					PO_ORDER_LIST_DGV.CELL.VALUE("REC_PORDER_LIST_QUANTITY_NET", ADD_ITEM_ROW_INDEX, LINE_MAP_ARRAY["CAT_PRODUCT_ING_NET_CONTENT"]);
					PO_ORDER_LIST_DGV.CELL.VALUE("REC_PORDER_LIST_PRICE_PPACK", ADD_ITEM_ROW_INDEX, LINE_MAP_ARRAY["CAT_PRODUCT_ING_PRICE"]);
					PO_ORDER_LIST_DGV.CELL.VALUE("REC_PORDER_LIST_QUANTITY_TOTAL", ADD_ITEM_ROW_INDEX, 0);
					PO_ORDER_LIST_DGV.CELL.VALUE("REC_PORDER_LIST_PRICE_TOTAL", ADD_ITEM_ROW_INDEX, 0);
					PO_ORDER_LIST_DGV.CELL.VALUE("REC_PORDER_LIST_STATUS", ADD_ITEM_ROW_INDEX, 2);

					EDIT_HANDLER.REF_IS_MODIFIED = true;		
				}
			);
		};
		
		
		// DGV OnClick Event
		// _______________________________________________________________________	
		PO_ORDER_LIST_DGV = DataGridView("PO_ORDER_LIST_DGV");
		var AUTO_COMPUTE_BLOCKS = [];
		AUTO_COMPUTE_BLOCKS.push
		(
			[
				'REC_PORDER_LIST_QUANTITY_TOTAL', 	// Target Column
				'`REC_PORDER_LIST_QUANTITY_NET` * `REC_PORDER_LIST_QUANTITY`',
				false								// Apply CFormat
			]
		);
		AUTO_COMPUTE_BLOCKS.push
		(
			[
				'REC_PORDER_LIST_PRICE_TOTAL', 	// Target Column
				'`REC_PORDER_LIST_PRICE_PPACK` * `REC_PORDER_LIST_QUANTITY`',
				false								// Apply CFormat
			]
		);		
		PO_ORDER_LIST_DGV.COLUMN.AUTO_COMPUTE_CREATE(AUTO_COMPUTE_BLOCKS);		
		
		var AUTO_TOTAL_COLUMNS = [];
		//AUTO_TOTAL_COLUMNS.push('REC_INVOICELIST_PRICE');
		AUTO_TOTAL_COLUMNS.push('REC_PORDER_LIST_QUANTITY');
		AUTO_TOTAL_COLUMNS.push('REC_PORDER_LIST_QUANTITY_TOTAL');
		AUTO_TOTAL_COLUMNS.push('REC_PORDER_LIST_PRICE_TOTAL');	
		
		PO_ORDER_LIST_DGV.COLUMN.TOTAL_CREATE
		(
			AUTO_TOTAL_COLUMNS,
			'CAT_PRODUCT_DESCRIPTION',
			[]
		);
			
				
		PO_ORDER_LIST_DGV.ON_CLICK
		(			
			function(COL_INDEX, ROW_INDEX)
			{
				// Delete Button
				if(PO_ORDER_LIST_DGV.COLUMN.NAME(COL_INDEX) == "DELETE_CMD")
				{
					JSVC_INFO.VAR_VERIFY_DELETE
					(
						PO_ORDER_LIST_DGV.CELL.VALUE("CAT_PRODUCT_DESCRIPTION", ROW_INDEX), 
						function(MSGBOX_RESULT)
						{
							// User rejected the delete action
							if(MSGBOX_RESULT != JSOC_DIALOG.MSGBOX_YES)
							{
								return;
							}
							
							// Not yet saved record
							else if(PO_ORDER_LIST_DGV.CELL.VALUE_NVAL("REC_PORDER_LIST_STATUS", ROW_INDEX) <= 0)
							{
								PO_ORDER_LIST_DGV.ROW.REMOVE_AT(0, true);
							}
							
							// Saved record
							else
							{
								PO_ORDER_LIST_DGV.CELL.VALUE("REC_PORDER_LIST_QUANTITY", ROW_INDEX, 0);
								PO_ORDER_LIST_DGV.CELL.VALUE("REC_PORDER_LIST_STATUS", ROW_INDEX, 99);
								JSOC_STYLE.VAR_DISPLAY(PO_ORDER_LIST_DGV.ROW.ELEMENT(ROW_INDEX), 'NONE');
								PO_ORDER_LIST_DGV.CELL.ELEMENT("REC_PORDER_LIST_QUANTITY", ROW_INDEX).onchange();
								//PO_ORDER_LIST_DGV.AUTO_COMPUTE_EXECUTE(ROW_INDEX, "REC_PORDER_LIST_QUANTITY");
								
							}
						}
					);
				}
			}
		);   	
		
		// DGV OnChange Event
		// _______________________________________________________________________	
		PO_ORDER_LIST_DGV.ON_CHANGE
		(			
			function(COL_INDEX, ROW_INDEX)
			{
				// Auto-compute tool
				// ...........................................
				PO_ORDER_LIST_DGV.COLUMN.AUTO_COMPUTE_EXECUTE(ROW_INDEX);
				
				// Auto-total tool
				// ...........................................
				if(PO_ORDER_LIST_DGV.COLUMN.TOTAL_IS_READY())
				{
					PO_ORDER_LIST_DGV.COLUMN.TOTAL_REFRESH();
				}
				
				JSOC_ELEMENT.VAR_VALUE("REC_PORDER_AMOUNT_TXT", PO_ORDER_LIST_DGV.COLUMN.SUM('REC_PORDER_LIST_PRICE_TOTAL'));
				EDIT_HANDLER.REF_IS_MODIFIED = true;
			}
		);
		
		STATUS_DELIVERY_CMD = JSOC_ELEMENT.VAR_GET("STATUS_DELIVERY_CMD");  
		STATUS_DELIVERY_CMD.onclick = function()
		{
			if(EDIT_HANDLER.REF_IS_MODIFIED == true)
			{
				alert('There are changes in current data. Please save the current changes to continue.');	
			}
			else if(JSOC_CMB.VAR_SELECTED_VALUE_NVAL('REC_PORDER_STATUS_CMB') == 1)
			{
				JSOC_CMB.VAR_SELECTED_VALUE('REC_PORDER_STATUS_CMB', '2');
				EDIT_HANDLER.REF_IS_MODIFIED = true;
				STATUS_BUTTON_ENABLED(false);
				SAVE_BBI.onclick();
			}			
		};		
		STATUS_VOID_CMD = JSOC_ELEMENT.VAR_GET("STATUS_VOID_CMD");  
		STATUS_VOID_CMD.onclick = function()
		{
			if(EDIT_HANDLER.REF_IS_MODIFIED == true)
			{
				alert('There are changes in current data. Please save the current changes to continue.');	
			}
			else
			{
				JSOC_DIALOG.VAR_MESSAGE_BOX
				(
					'Do you want to void this purchase order? This action cannot be undone.', 
					'Void Transaction', 
					JSOC_DIALOG.MSGBOX_TYPE_YES_NO,
					JSOC_DIALOG.MSGBOX_ICON_QUESTION,
					function (MSGBOX_RESULT)
					{
						if(MSGBOX_RESULT == JSOC_DIALOG.MSGBOX_YES)
						{
							JSOC_CMB.VAR_SELECTED_VALUE('REC_PORDER_STATUS_CMB', '3');	
							EDIT_HANDLER.REF_IS_MODIFIED = true;
							STATUS_BUTTON_ENABLED(false);
							SAVE_BBI.onclick();
						}
						else
						{
							alert('Action Aborted.');
						}
					}
				);
			}
		};					
	};
	
	function LINE_UPDATE_STATUS()
	{
		// Allow Edit Items
		// _______________________________________________________________________
		if(JSOC_CMB.VAR_SELECTED_VALUE_NVAL(REC_PORDER_STATUS_CMB) <= 1)
		{
			PO_ORDER_LIST_DGV.COLUMN.ENABLED("REC_PORDER_LIST_QUANTITY", true);
			PO_ORDER_LIST_DGV.COLUMN.ENABLED("REC_PORDER_LIST_PRICE_PPACK", true);
			PO_ORDER_LIST_DGV.COLUMN.ENABLED("DELETE_CMD", true);
			JSOC_ELEMENT.VAR_ENABLED(ADD_ITEM_BBI, true);
		}
		
		// Do not allow edit items
		// _______________________________________________________________________
		else
		{
			PO_ORDER_LIST_DGV.COLUMN.ENABLED("REC_PORDER_LIST_QUANTITY", false);
			PO_ORDER_LIST_DGV.COLUMN.ENABLED("REC_PORDER_LIST_PRICE_PPACK", false);
			PO_ORDER_LIST_DGV.COLUMN.ENABLED("DELETE_CMD", false);			
			JSOC_ELEMENT.VAR_ENABLED(ADD_ITEM_BBI, false);
		}
		
	};
	
	
	
	function HTTP_SHOW_MODAL(FOR_EDIT = false)
	{
		// Display Modal
		// _______________________________________________________________________
		if(JSVC_VAR.VAR_ISSET(INPUT_INSTANCE))
		{
			JSOC_WINDOW.VAR_INPUT_INSTANCE_RESTORE(INPUT_INSTANCE);
		}
		EDIT_HANDLER.REF_IS_MODIFIED = false;
		JSOC_DIALOG.VAR_MODAL("PO_ORDER_MODAL", true);
		STATUS_BUTTON_ENABLED(true);
		STATUS_BUTTON_MANAGE();
		
		//JSOC_ELEMENT.VAR_VALUE("REC_PORDER_AMOUNT_TXT", "0");
		if(FOR_EDIT == true)
		{
			JSOC_ELEMENT.VAR_ENABLED(REC_PORDER_CAT_COMPANY_ID_CMB, false);
		}
		else
		{
			JSOC_ELEMENT.VAR_ENABLED(REC_PORDER_CAT_COMPANY_ID_CMB, true);
		}
	};
		
		
		
	function STATUS_BUTTON_MANAGE()
	{
		var STATUS_ID = JSOC_CMB.VAR_SELECTED_VALUE_NVAL('REC_PORDER_STATUS_CMB');
		
		JSOC_STYLE.VAR_DISPLAY(STATUS_DELIVERY_CMD, 'none');
		JSOC_STYLE.VAR_DISPLAY(STATUS_VOID_CMD, 'none');
		JSOC_ELEMENT.VAR_ENABLED(SAVE_BBI, true);	
		
		// Draft
		if(STATUS_ID == 1)
		{
			JSOC_STYLE.VAR_DISPLAY(STATUS_DELIVERY_CMD, 'default');
			JSOC_STYLE.VAR_DISPLAY(STATUS_VOID_CMD, 'default');
		}		
		
		// Delivered
		else if(STATUS_ID == 2)
		{
			JSOC_STYLE.VAR_DISPLAY(STATUS_VOID_CMD, 'default');			
		}
		
		// Cancelled
		else if(STATUS_ID == 3)
		{
			JSOC_ELEMENT.VAR_ENABLED(SAVE_BBI, false);		
		}		
	};
	
	
	function STATUS_BUTTON_ENABLED(IS_ENABLED)
	{
		JSOC_ELEMENT.VAR_ENABLED(SAVE_BBI, IS_ENABLED);
		JSOC_ELEMENT.VAR_ENABLED(STATUS_DELIVERY_CMD, IS_ENABLED);
		JSOC_ELEMENT.VAR_ENABLED(STATUS_VOID_CMD, IS_ENABLED);
	};
		
		
	FORM_CHILD_INITIALIZE();