var NEW_ACC_GROUP_ID_CMB = null;
var FOR_DATE_DTP = null;
var NEW_INVOICE_BBI = null;
var GENERATE_INVOICE_BBI = null;

var INVOICE_LINE_DGV = null;
var INVOICE_RTN_DGV = null;
var ADD_ITEM_BBI = null;
var ADD_RTN_BBI = null;
var MODAL_PRINT_INVOICE_CMD = null;
var MODAL_PRINT_DR_CMD = null;

var STATUS_DRAFT_CMD = null;
var STATUS_DELIVERY_CMD = null;
var STATUS_VOID_CMD = null;
var STATUS_COMPLETE_CMD = 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
					INVOICE_LINE_DGV.ROW.ADD();
					var ADD_ITEM_ROW_INDEX = INVOICE_LINE_DGV.ROW.COUNT() - 1;
					
					// Abort if exists
					if(INVOICE_LINE_DGV.COLUMN.FIND("REC_INVOICELIST_CAT_PRODUCT_ID", PRODUCT_ID) >= 0)
					{
						alert('This item already exists');
						return;
					}
					
					// Set Value
					INVOICE_LINE_DGV.CELL.VALUE("REC_INVOICELIST_ID", ADD_ITEM_ROW_INDEX, -1);
					INVOICE_LINE_DGV.CELL.VALUE("REC_INVOICELIST_REC_INVOICE_ID", ADD_ITEM_ROW_INDEX, PROPERTY_REC_INVOICE_ID);
					INVOICE_LINE_DGV.CELL.VALUE("REC_INVOICELIST_CAT_PRODUCT_ID", ADD_ITEM_ROW_INDEX, PRODUCT_ID);
					INVOICE_LINE_DGV.CELL.VALUE("CAT_PRODUCT_DESCRIPTION", ADD_ITEM_ROW_INDEX, PRODUCT_DESCRIPTION);
					INVOICE_LINE_DGV.CELL.VALUE("REC_INVOICELIST_PRICE", ADD_ITEM_ROW_INDEX, LINE_MAP_ARRAY["CAT_PRODUCT_ING_PRICE"]);
					INVOICE_LINE_DGV.CELL.VALUE("REC_INVOICELIST_QUANTITY", ADD_ITEM_ROW_INDEX, 0);
					INVOICE_LINE_DGV.CELL.VALUE("REC_INVOICELIST_DISCOUNT", ADD_ITEM_ROW_INDEX, 0);
					INVOICE_LINE_DGV.CELL.VALUE("SUBTOTAL", ADD_ITEM_ROW_INDEX, 0);
					INVOICE_LINE_DGV.CELL.VALUE("REC_INVOICELIST_STATUS", ADD_ITEM_ROW_INDEX, 2);	
					EDIT_HANDLER.REF_IS_MODIFIED = true;		
				}
			);
		};
		
		
		// DGV OnClick Event
		// _______________________________________________________________________	
		INVOICE_LINE_DGV = DataGridView("INVOICE_LINE_DGV");
		var AUTO_COMPUTE_BLOCKS = [];
		/*
		AUTO_COMPUTE_BLOCKS.push
		(
			[
				'SUBTOTAL', 					// Target Column
				'(`REC_INVOICELIST_PRICE` * `REC_INVOICELIST_QUANTITY`) - `REC_INVOICELIST_DISCOUNT`',
				true							// Apply CFormat
			]
		);
		*/
		INVOICE_LINE_DGV.COLUMN.AUTO_COMPUTE_CREATE(AUTO_COMPUTE_BLOCKS);		
		
		var AUTO_TOTAL_COLUMNS = [];
		//AUTO_TOTAL_COLUMNS.push('REC_INVOICELIST_PRICE');
		AUTO_TOTAL_COLUMNS.push('REC_INVOICELIST_QUANTITY');
		AUTO_TOTAL_COLUMNS.push('REC_INVOICELIST_DISCOUNT');
		AUTO_TOTAL_COLUMNS.push('SUBTOTAL');			
		INVOICE_LINE_DGV.COLUMN.TOTAL_CREATE
		(
			AUTO_TOTAL_COLUMNS,
			'CAT_PRODUCT_DESCRIPTION',
			[]
		);
			
				
		INVOICE_LINE_DGV.ON_CLICK
		(			
			function(COL_INDEX, ROW_INDEX)
			{
				// Delete Button
				if(INVOICE_LINE_DGV.COLUMN.NAME(COL_INDEX) == "DELETE_CMD")
				{
					// Abort if no draft
					if(JSOC_CMB.VAR_SELECTED_VALUE_NVAL(REC_INVOICE_STATUS_CMB) != 1)
					{
						return;
					}
					
					JSVC_INFO.VAR_VERIFY_DELETE
					(
						INVOICE_LINE_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(INVOICE_LINE_DGV.CELL.VALUE_NVAL("REC_INVOICELIST_STATUS", ROW_INDEX) <= 0)
							{
								INVOICE_LINE_DGV.ROW.REMOVE_AT(0, true);
							}
							
							// Saved record
							else
							{
								INVOICE_LINE_DGV.CELL.VALUE("REC_INVOICELIST_QUANTITY", ROW_INDEX, 0);
								INVOICE_LINE_DGV.CELL.VALUE("REC_INVOICELIST_STATUS", ROW_INDEX, 99);
								JSOC_STYLE.VAR_DISPLAY(INVOICE_LINE_DGV.ROW.ELEMENT(ROW_INDEX), 'NONE');
								INVOICE_LINE_DGV.CELL.ELEMENT("REC_INVOICELIST_QUANTITY", ROW_INDEX).onchange();
							}
						}
					);
				}
			}
		);   	
		
		// DGV OnChange Event
		// _______________________________________________________________________	
		INVOICE_LINE_DGV.ON_CHANGE
		(			
			function(COL_INDEX, ROW_INDEX)
			{
				// Auto-compute tool
				// ...........................................
				INVOICE_LINE_DGV.COLUMN.AUTO_COMPUTE_EXECUTE(ROW_INDEX);
				INVOICE_LINE_DGV.CELL.VALUE
				(
					'SUBTOTAL', ROW_INDEX,
					JSVC_NUM.VAR_ROUND
					(
						(
							INVOICE_LINE_DGV.CELL.VALUE_NVAL('REC_INVOICELIST_PRICE', ROW_INDEX) *
							INVOICE_LINE_DGV.CELL.VALUE_NVAL('REC_INVOICELIST_QUANTITY', ROW_INDEX)
						) - INVOICE_LINE_DGV.CELL.VALUE_NVAL('REC_INVOICELIST_DISCOUNT', ROW_INDEX),
						2 
					)
				);
								
				// Auto-total tool
				// ...........................................
				if(INVOICE_LINE_DGV.COLUMN.TOTAL_IS_READY())
				{
					INVOICE_LINE_DGV.COLUMN.TOTAL_REFRESH();
				}

				JSOC_ELEMENT.VAR_VALUE("REC_INVOICE_AMOUNT_DUE_TXT", INVOICE_LINE_DGV.COLUMN.SUM('SUBTOTAL'));
				REC_INVOICE_DISCOUNT_OTHER_TXT.onchange();
				EDIT_HANDLER.REF_IS_MODIFIED = true;
			}
		);
		


		// Button 
		ADD_RTN_BBI = JSOC_ELEMENT.VAR_GET("ADD_RTN_BBI");  
		ADD_RTN_BBI.onclick = function()
		{
			SELECT_MODAL_CATEGORY_PRODUCT.SHOW
			(
				function(PRODUCT_ID, PRODUCT_DESCRIPTION, LINE_MAP_ARRAY)
				{
					// Initialize
					INVOICE_RTN_DGV.ROW.ADD();
					var ADD_ITEM_ROW_INDEX = INVOICE_RTN_DGV.ROW.COUNT() - 1;
					
					// Set Value
					INVOICE_RTN_DGV.CELL.VALUE("REC_INVOICERTN_ID", ADD_ITEM_ROW_INDEX, -1);
					INVOICE_RTN_DGV.CELL.VALUE("REC_INVOICERTN_REC_INVOICE_ID", ADD_ITEM_ROW_INDEX, PROPERTY_REC_INVOICE_ID);
					INVOICE_RTN_DGV.CELL.VALUE("REC_INVOICERTN_CAT_PRODUCT_ID", ADD_ITEM_ROW_INDEX, PRODUCT_ID);
					INVOICE_RTN_DGV.CELL.VALUE("CAT_PRODUCT_DESCRIPTION", ADD_ITEM_ROW_INDEX, PRODUCT_DESCRIPTION);
					INVOICE_RTN_DGV.CELL.VALUE("REC_INVOICERTN_PRICE", ADD_ITEM_ROW_INDEX, LINE_MAP_ARRAY["CAT_PRODUCT_ING_PRICE"]);
					INVOICE_RTN_DGV.CELL.VALUE("REC_INVOICERTN_QUANTITY", ADD_ITEM_ROW_INDEX, 0);
					INVOICE_RTN_DGV.CELL.VALUE("REC_INVOICERTN_DISCOUNT", ADD_ITEM_ROW_INDEX, 0);
					INVOICE_RTN_DGV.CELL.VALUE("SUBTOTAL", ADD_ITEM_ROW_INDEX, 0);
					INVOICE_RTN_DGV.CELL.VALUE("REC_INVOICERTN_STATUS", ADD_ITEM_ROW_INDEX, 2);		
					
					EDIT_HANDLER.REF_IS_MODIFIED = true;	
				}
			);
		};
		
		
		// DGV OnClick Event
		// _______________________________________________________________________	
		INVOICE_RTN_DGV = DataGridView("INVOICE_RTN_DGV");
		AUTO_COMPUTE_BLOCKS = [];
		AUTO_COMPUTE_BLOCKS.push
		(
			[
				'SUBTOTAL', 					// Target Column
				'(`REC_INVOICERTN_PRICE` * `REC_INVOICERTN_QUANTITY`) - `REC_INVOICERTN_DISCOUNT`',
				true							// Apply CFormat
			]
		);
		INVOICE_RTN_DGV.COLUMN.AUTO_COMPUTE_CREATE(AUTO_COMPUTE_BLOCKS);		
		
		AUTO_TOTAL_COLUMNS = [];
		//AUTO_TOTAL_COLUMNS.push('REC_INVOICERTN_PRICE');
		AUTO_TOTAL_COLUMNS.push('REC_INVOICERTN_QUANTITY');
		AUTO_TOTAL_COLUMNS.push('REC_INVOICERTN_DISCOUNT');
		AUTO_TOTAL_COLUMNS.push('SUBTOTAL');
		
		INVOICE_RTN_DGV.COLUMN.TOTAL_CREATE
		(
			AUTO_TOTAL_COLUMNS,
			'CAT_PRODUCT_DESCRIPTION',
			[]
		);	
				
		INVOICE_RTN_DGV.ON_CLICK
		(			
			function(COL_INDEX, ROW_INDEX)
			{
				// Delete Button
				if(INVOICE_RTN_DGV.COLUMN.NAME(COL_INDEX) == "DELETE_CMD")
				{
					JSVC_INFO.VAR_VERIFY_DELETE
					(
						INVOICE_RTN_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(INVOICE_RTN_DGV.CELL.VALUE_NVAL("REC_INVOICERTN_STATUS", ROW_INDEX) <= 0)
							{
								INVOICE_RTN_DGV.ROW.REMOVE_AT(0, true);
							}
							
							// Saved record
							else
							{
								INVOICE_RTN_DGV.CELL.VALUE("REC_INVOICERTN_QUANTITY", ROW_INDEX, 0);
								INVOICE_RTN_DGV.CELL.VALUE("REC_INVOICERTN_STATUS", ROW_INDEX, 199);
								JSOC_STYLE.VAR_DISPLAY(INVOICE_RTN_DGV.ROW.ELEMENT(ROW_INDEX), 'NONE');
								INVOICE_RTN_DGV.CELL.ELEMENT("REC_INVOICERTN_QUANTITY", ROW_INDEX).onchange();
							}
						}
					);
				}
			}
		);   	
		
		// DGV OnChange Event
		// _______________________________________________________________________	
		INVOICE_RTN_DGV.ON_CHANGE
		(			
			function(COL_INDEX, ROW_INDEX)
			{
				// Auto-compute tool
				// ...........................................
				INVOICE_RTN_DGV.COLUMN.AUTO_COMPUTE_EXECUTE(ROW_INDEX);
				
				// Auto-total tool
				// ...........................................
				if(INVOICE_RTN_DGV.COLUMN.TOTAL_IS_READY())
				{
					INVOICE_RTN_DGV.COLUMN.TOTAL_REFRESH();
				}
				
				JSOC_ELEMENT.VAR_VALUE("REC_INVOICE_AMOUNT_RETURN_TXT", INVOICE_RTN_DGV.COLUMN.SUM('SUBTOTAL'));
				REC_INVOICE_DISCOUNT_OTHER_TXT.onchange();
				EDIT_HANDLER.REF_IS_MODIFIED = true;
			}
		);
		
		
		// Handle Modal Elements
		// _______________________________________________________________________	
		if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("NEW_ACC_GROUP_ID_CMB"))
		{
			NEW_ACC_GROUP_ID_CMB = JSOC_ELEMENT.VAR_GET("NEW_ACC_GROUP_ID_CMB");      
			NEW_ACC_GROUP_ID_CMB.onchange = 
				function()
				{
				};
			NEW_ACC_GROUP_ID_CMB.onkeypress = function(){NEW_ACC_GROUP_ID_CMB.onchange();};
			NEW_ACC_GROUP_ID_CMB.onpaste = function(){NEW_ACC_GROUP_ID_CMB.onchange();};
			//NEW_ACC_GROUP_ID_CMB.oninput = function(){NEW_ACC_GROUP_ID_CMB.onchange();};
		}
		
		if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("FOR_DATE_DTP"))
		{
			FOR_DATE_DTP = JSOC_ELEMENT.VAR_GET("FOR_DATE_DTP");   	
			if(JSCUS_CACHE.SET_DELIVERY_ADJUSTMENT_DD != 0)
			{
				JSOC_ELEMENT.VAR_VALUE(FOR_DATE_DTP, JSVC_DATE.VAR_ADD('DD', 1, JSVC_DATE.VAR_NOW()));
			}
			else
			{
				JSOC_ELEMENT.VAR_VALUE(FOR_DATE_DTP, JSVC_DATE.VAR_NOW());
			}
			
			   
			FOR_DATE_DTP.onchange = function(){};
			FOR_DATE_DTP.onkeypress = function(){FOR_DATE_DTP.onchange();};
			FOR_DATE_DTP.onpaste = function(){FOR_DATE_DTP.onchange();};
			//FOR_DATE_DTP.oninput = function(){FOR_DATE_DTP.onchange();};
		}		
		
		if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("NEW_INVOICE_BBI"))
		{
			NEW_INVOICE_BBI = JSOC_ELEMENT.VAR_GET("NEW_INVOICE_BBI");      
			NEW_INVOICE_BBI.onclick = function()
			{
				if(JSVC_NUM.VAR_NVALL(JSOC_CMB.VAR_SELECTED_VALUE(NEW_ACC_GROUP_ID_CMB)) <= 0)
				{
					JSVC_INFO.VAR_NOTHING_TO_LOAD();
					return;
				}
				
				HTTP_SHOW_MODAL(JSOC_CMB.VAR_SELECTED_VALUE(NEW_ACC_GROUP_ID_CMB), JSOC_ELEMENT.VAR_VALUE(FOR_DATE_DTP));
				INVOICE_LINE_DGV.ROW.CLEAR();
				INVOICE_RTN_DGV.ROW.CLEAR();
				
				
				if(JSOC_CMB.VAR_SELECTED_VALUE_NVAL(NEW_ACC_GROUP_ID_CMB) != 100)
				{
					JSOC_ELEMENT.VAR_VALUE(REC_INVOICE_NAME_TXT, JSOC_CMB.VAR_SELECTED_TEXT(NEW_ACC_GROUP_ID_CMB));
					REC_INVOICE_NAME_TXT.readOnly = true;
				}
				else
				{
					REC_INVOICE_NAME_TXT.readOnly = false;
				}
				
				PROPERTY_REC_INVOICE_ID = FLAG_BIGINT_NULL;
				PROPERTY_EDIT_ROW_INDEX = -1;
				EDIT_HANDLER.REF_IS_MODIFIED = true;
			};
		}		
		
		if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS("GENERATE_INVOICE_BBI"))
		{
			GENERATE_INVOICE_BBI = JSOC_ELEMENT.VAR_GET("GENERATE_INVOICE_BBI");      
			GENERATE_INVOICE_BBI.onclick = function()
			{
				if(JSVC_NUM.VAR_NVALL(JSOC_CMB.VAR_SELECTED_VALUE(NEW_ACC_GROUP_ID_CMB)) <= 0)
				{
					JSVC_INFO.VAR_NOTHING_TO_LOAD();
					return;
				}
				
				HTTP_PENDING_NEW(JSOC_CMB.VAR_SELECTED_VALUE(NEW_ACC_GROUP_ID_CMB), JSOC_ELEMENT.VAR_VALUE(FOR_DATE_DTP));
				
				PROPERTY_REC_INVOICE_ID = FLAG_BIGINT_NULL;
				PROPERTY_EDIT_ROW_INDEX = -1;
				EDIT_HANDLER.REF_IS_MODIFIED = true;
			};
		}	
		
		STATUS_DRAFT_CMD = JSOC_ELEMENT.VAR_GET("STATUS_DRAFT_CMD");  
		STATUS_DRAFT_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_INVOICE_STATUS_CMB') == 2)
			{
				JSOC_CMB.VAR_SELECTED_VALUE('REC_INVOICE_STATUS_CMB', '1');
				EDIT_HANDLER.REF_IS_MODIFIED = true;
				STATUS_BUTTON_ENABLED(false);
				SAVE_BBI.onclick();
			}
		};
		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_INVOICE_STATUS_CMB') == 1)
			{
				JSOC_CMB.VAR_SELECTED_VALUE('REC_INVOICE_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 transaction? 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_INVOICE_STATUS_CMB', '3');	
							EDIT_HANDLER.REF_IS_MODIFIED = true;
							STATUS_BUTTON_ENABLED(false);
							SAVE_BBI.onclick();
						}
						else
						{
							alert('Action Aborted.');
						}
					}
				);
			}
		};			
		STATUS_COMPLETE_CMD = JSOC_ELEMENT.VAR_GET("STATUS_COMPLETE_CMD");  
		STATUS_COMPLETE_CMD.onclick = function()
		{			
			// Need to save first the changes
			if(EDIT_HANDLER.REF_IS_MODIFIED == true)
			{
				alert('There are changes in current data. Please save the current changes to continue.');	
			}
			
			// Check is delivery date 
			else if
			(
				JSVC_DATE.VAR_BEGIN_OF_DAY(JSOC_ELEMENT.VAR_VALUE_TO_DATE("REC_INVOICE_DATE_DELIVERY_DTP")) > 
				JSVC_DATE.VAR_END_OF_DAY(JSVC_DATE.VAR_NOW()) 
			)
			{
				JSOC_DIALOG.VAR_MESSAGE_BOX
				(
					'Cannot complete delivery tasks in advance.', 
					'Complete Transaction', 
					JSOC_DIALOG.MSGBOX_TYPE_OK_ONLY,
					JSOC_DIALOG.MSGBOX_ICON_WARNING,
					function(){}
				);
				return;
			}
			
			//
			else if(JSOC_CMB.VAR_SELECTED_VALUE_NVAL('REC_INVOICE_STATUS_CMB') == 2)
			{
				JSOC_CMB.VAR_SELECTED_VALUE('REC_INVOICE_STATUS_CMB', '4');
				EDIT_HANDLER.REF_IS_MODIFIED = true;
				STATUS_BUTTON_ENABLED(false);
				SAVE_BBI.onclick();
			}			
		};				
		
		MODAL_PRINT_INVOICE_CMD = JSOC_ELEMENT.VAR_GET("MODAL_PRINT_INVOICE_CMD"); 
		MODAL_PRINT_INVOICE_CMD.onclick = function()
		{
			// Escape if new item
			// _______________________________________________________________________
			if(JSRC_FLAG.IS_NULL_BIGINT(PROPERTY_REC_INVOICE_ID, 1, 1))
			{
				alert('You are required to save the current record to continue with the printing.');
				return;
			}
			
			// 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_INVOICE', 
				PROPERTY_REC_INVOICE_ID, 
				5, 
				CURRENT_ROOT_PATH
			);
			JSVC_COOKIE.VAR_ADD
			(
				'PROPERTY_PARENT_DESCRIPTION_INVOICE', 
				'NA', 
				5, 
				CURRENT_ROOT_PATH
			);
			
			// Open EditPage
			window.open(JSCUS_CACHE.SERVER_URL + '/p/record/print_invoice.html', '_blank');		
		};
		
		MODAL_PRINT_DR_CMD = JSOC_ELEMENT.VAR_GET("MODAL_PRINT_DR_CMD");  
		MODAL_PRINT_DR_CMD.onclick = function()
		{
			// Escape if new item
			// _______________________________________________________________________
			if(JSRC_FLAG.IS_NULL_BIGINT(PROPERTY_REC_INVOICE_ID, 1, 1))
			{
				alert('You are required to save the current record to continue with the printing.');
				return;
			}
			
			// 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_INVOICE', 
				PROPERTY_REC_INVOICE_ID, 
				5, 
				CURRENT_ROOT_PATH
			);
			JSVC_COOKIE.VAR_ADD
			(
				'PROPERTY_PARENT_DESCRIPTION_INVOICE', 
				'NA', 
				5, 
				CURRENT_ROOT_PATH
			);
			
			// Open EditPage
			window.open(JSCUS_CACHE.SERVER_URL + '/p/record/print_dr.html', '_blank');					
		};
	};
	
	
	function HTTP_PENDING_NEW(BRANCH_ID, DATE_TIME)
	{
		// Callback
		// _______________________________________________________________________
		var CALLBACK_METHOD = function(IS_SUCCESS, RESPONSE_STRING)
		{
			// Disable Dialogs
			// ..................................................................
			CUS_SEND_DIALOG_TRY_DISMISS();
		
			// Validate
			// ..................................................................
			if(IS_SUCCESS === false)
			{
				CUS_SEND_DIALOG_TRY_DISMISS();
				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)
			{
				CUS_SEND_DIALOG_TRY_DISMISS();
				console.error("REQUEST ERROR: \n" + RESPONSE_STRING);
				return;
			}
			
			// Back to DGV Top
			// ..................................................................
			if(LOAD_PAGING_IGNORE_NEXT_REQUEST == true)
			{
				JSOC_ELEMENT.VAR_GET("PSACTUAL_DGV").scrollIntoView
				(
					{
				  		behavior: 'auto', 
				  		block: 'start'
					}
				);		
			}
			
			// JSON Empty (Clears the DataGrodView)
			// ..................................................................
			INVOICE_LINE_DGV.ROW.CLEAR();
			INVOICE_RTN_DGV.ROW.CLEAR();
			if(JSRC_JSON.REF_READ(JSON_OBJECT, "JSON_COUNT") <= 0)
			{
				HTTP_SHOW_MODAL(BRANCH_ID, DATE_TIME);
				return;
			}			
			HTTP_SHOW_MODAL(BRANCH_ID, DATE_TIME);
						
			
			// Populate DGV
			// ..................................................................
			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)
			{
				if(JSRC_JSON.REF_READ_NVAL(JSON_OBJECT, "REC_PSACTUAL_QTY_D" + JSVC_DATE.VAR_FORMAT(DATE_TIME, "DD"), JSON_INDEX) != 0)
				{
					INVOICE_LINE_DGV.ROW.ADD();
					ROW_INDEX = INVOICE_LINE_DGV.ROW.COUNT() - 1;
					INVOICE_LINE_DGV.CELL.VALUE("REC_INVOICELIST_ID", ROW_INDEX, "-1");
					
					INVOICE_LINE_DGV.CELL.VALUE("REC_INVOICELIST_CAT_PRODUCT_ID", ROW_INDEX, JSRC_JSON.REF_READ(JSON_OBJECT, "CAT_PRODUCT_ID", JSON_INDEX));
					INVOICE_LINE_DGV.CELL.VALUE("CAT_PRODUCT_DESCRIPTION", ROW_INDEX, JSRC_JSON.REF_READ(JSON_OBJECT, "CAT_PRODUCT_DESCRIPTION", JSON_INDEX));
					INVOICE_LINE_DGV.CELL.VALUE("REC_INVOICELIST_PRICE", ROW_INDEX, JSRC_JSON.REF_READ(JSON_OBJECT, "CAT_PRODUCT_ING_PRICE", JSON_INDEX));
					INVOICE_LINE_DGV.CELL.VALUE("REC_INVOICELIST_QUANTITY", ROW_INDEX, JSRC_JSON.REF_READ(JSON_OBJECT, "REC_PSACTUAL_QTY_D" + JSVC_DATE.VAR_FORMAT(DATE_TIME, "DD"), JSON_INDEX));
					INVOICE_LINE_DGV.CELL.VALUE("REC_INVOICELIST_DISCOUNT", ROW_INDEX, "0");
					INVOICE_LINE_DGV.CELL.VALUE("REC_INVOICELIST_STATUS", ROW_INDEX, "2");
					
					INVOICE_LINE_DGV.COLUMN.AUTO_COMPUTE_EXECUTE(ROW_INDEX);
					INVOICE_LINE_DGV.CELL.VALUE
					(
						'SUBTOTAL', ROW_INDEX,
						JSVC_NUM.VAR_ROUND
						(
							(
								INVOICE_LINE_DGV.CELL.VALUE_NVAL('REC_INVOICELIST_PRICE', ROW_INDEX) *
								INVOICE_LINE_DGV.CELL.VALUE_NVAL('REC_INVOICELIST_QUANTITY', ROW_INDEX)
							) - INVOICE_LINE_DGV.CELL.VALUE_NVAL('REC_INVOICELIST_DISCOUNT', ROW_INDEX),
							2 
						)
					);
				}
				
				JSON_INDEX ++;
			}
			
			
			// Auto-total tool
			// ...........................................
			if(INVOICE_LINE_DGV.COLUMN.TOTAL_IS_READY())
			{
				INVOICE_LINE_DGV.COLUMN.TOTAL_REFRESH();
			}
			
			// Assign branch name to textbox
			// ...........................................
			if(JSOC_CMB.VAR_SELECTED_VALUE_NVAL(NEW_ACC_GROUP_ID_CMB) != 100)
			{
				JSOC_ELEMENT.VAR_VALUE(REC_INVOICE_NAME_TXT, JSOC_CMB.VAR_SELECTED_TEXT(NEW_ACC_GROUP_ID_CMB));
				REC_INVOICE_NAME_TXT.readOnly = true;
			}
			else
			{
				REC_INVOICE_NAME_TXT.readOnly = false;
			}
			
			// Show DGV
			// ..................................................................
			INVOICE_LINE_DGV.ROW.REFRESH_ROW_COUNT();
			JSOC_ELEMENT.VAR_VALUE("REC_INVOICE_AMOUNT_DUE_TXT", INVOICE_LINE_DGV.COLUMN.SUM('SUBTOTAL'));
			REC_INVOICE_DISCOUNT_OTHER_TXT.onchange();
		};
		
		
		// Actual Function
		// _______________________________________________________________________
		GET_MAP_ARRAY = JSCUS_SQL._RECORD._PSACTUAL.CUS_PREPARE_MAPPED_ARRAY_FOR_GET();	
		JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_REC_PSACTUAL_ACC_GROUP_ID', BRANCH_ID);
		JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_REC_PSACTUAL_DATE_FROM', JSVC_DATE.VAR_FORMAT(JSVC_DATE.VAR_BEGIN_OF_MONTH(DATE_TIME), "YYYY-MM-DD") + " 00:00:00.000000");
		JSVC_MAP_ARRAY.VAR_SET(GET_MAP_ARRAY, 'PARAM_REC_PSACTUAL_DATE_TO', JSVC_DATE.VAR_FORMAT(JSVC_DATE.VAR_END_OF_MONTH(DATE_TIME), "YYYY-MM-DD") + " 23:59:59.999999");
	
	
		// Search Inserted / Saved Record
		// _______________________________________________________________________
		JSOC_DIALOG.VAR_LOADING_SPINNER(true);
		JSCUS_SQL._RECORD._PSACTUAL.CUS_GET
		(
			GET_MAP_ARRAY,
			CALLBACK_METHOD,
			true
		);
	};
	
	
	
	function HTTP_SHOW_MODAL(BRANCH_ID = -1, DATE_TIME = null)
	{
		// 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("NEW_INVOICE_MODAL", false);
		JSOC_DIALOG.VAR_MODAL("INVOICE_MODAL", true);
		STATUS_BUTTON_ENABLED(true);
		STATUS_BUTTON_MANAGE();
		
		JSOC_ELEMENT.VAR_VALUE("REC_INVOICE_DISCOUNT_OTHER_TXT", "0");
		JSOC_ELEMENT.VAR_VALUE("REC_INVOICE_AMOUNT_RETURN_TXT", "0");
		JSOC_ELEMENT.VAR_VALUE("REC_INVOICE_AMOUNT_ADJUSTMENT_TXT", "0");
		JSOC_ELEMENT.VAR_VALUE("REC_INVOICE_AMOUNT_PAID_TXT", "0");
			
		// Assign Branch ID
		// _______________________________________________________________________
		if(BRANCH_ID >= 0)
		{
			JSOC_CMB.VAR_SELECTED_VALUE(REC_INVOICE_ACC_GROUP_ID_CMB, BRANCH_ID);
		}
		if(JSVC_VAR.VAR_ISSET(DATE_TIME))
		{
			JSOC_ELEMENT.VAR_VALUE(REC_INVOICE_DATE_DELIVERY_DTP, DATE_TIME);
		}
	};
		
		
		
	function STATUS_BUTTON_MANAGE()
	{
		// Initialize
		// _______________________________________________________________________
		var STATUS_ID = JSOC_CMB.VAR_SELECTED_VALUE_NVAL('REC_INVOICE_STATUS_CMB');
		
		// Disable buttons at begining (will be restored depending on the status)
		// _______________________________________________________________________
		JSOC_STYLE.VAR_DISPLAY(STATUS_DRAFT_CMD, 'none');
		JSOC_STYLE.VAR_DISPLAY(STATUS_DELIVERY_CMD, 'none');
		JSOC_STYLE.VAR_DISPLAY(STATUS_VOID_CMD, 'none');
		JSOC_STYLE.VAR_DISPLAY(STATUS_COMPLETE_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');
			JSOC_ELEMENT.VAR_ENABLED(REC_INVOICE_DATE_DELIVERY_DTP, true);
			JSOC_ELEMENT.VAR_ENABLED(ADD_ITEM_BBI, true);
			JSOC_ELEMENT.VAR_ENABLED(ADD_RTN_BBI, true);
			INVOICE_LINE_DGV.IS_ENABLED(true);
			INVOICE_RTN_DGV.IS_ENABLED(true);
		}		
		
		// On Delivery
		// _______________________________________________________________________
		else if(STATUS_ID == 2)
		{
			JSOC_STYLE.VAR_DISPLAY(STATUS_DRAFT_CMD, 'default');
			JSOC_STYLE.VAR_DISPLAY(STATUS_VOID_CMD, 'default');			
			JSOC_STYLE.VAR_DISPLAY(STATUS_COMPLETE_CMD, 'default');		
			JSOC_ELEMENT.VAR_ENABLED(REC_INVOICE_DATE_DELIVERY_DTP, false);
			JSOC_ELEMENT.VAR_ENABLED(ADD_ITEM_BBI, false);
			JSOC_ELEMENT.VAR_ENABLED(ADD_RTN_BBI, true);
			INVOICE_LINE_DGV.IS_ENABLED(false);
			INVOICE_RTN_DGV.IS_ENABLED(true);
		}
		
		// Cancelled
		// _______________________________________________________________________
		else if(STATUS_ID == 3)
		{
			JSOC_ELEMENT.VAR_ENABLED(SAVE_BBI, false);
			JSOC_ELEMENT.VAR_ENABLED(REC_INVOICE_DATE_DELIVERY_DTP, false);		
			JSOC_ELEMENT.VAR_ENABLED(ADD_ITEM_BBI, false);
			JSOC_ELEMENT.VAR_ENABLED(ADD_RTN_BBI, false);
			INVOICE_LINE_DGV.IS_ENABLED(false);
			INVOICE_RTN_DGV.IS_ENABLED(false);
		}
		
		// Completed
		// _______________________________________________________________________
		else if(STATUS_ID == 4)
		{
			//JSOC_STYLE.VAR_DISPLAY(STATUS_VOID_CMD, 'default');		
			JSOC_ELEMENT.VAR_ENABLED(REC_INVOICE_DATE_DELIVERY_DTP, false);	
			JSOC_ELEMENT.VAR_ENABLED(ADD_ITEM_BBI, false);
			JSOC_ELEMENT.VAR_ENABLED(ADD_RTN_BBI, false);
			INVOICE_LINE_DGV.IS_ENABLED(false);
			INVOICE_RTN_DGV.IS_ENABLED(false);
		}			
	};
	
	
	function STATUS_BUTTON_ENABLED(IS_ENABLED)
	{
		JSOC_ELEMENT.VAR_ENABLED(SAVE_BBI, IS_ENABLED);
		JSOC_ELEMENT.VAR_ENABLED(STATUS_DRAFT_CMD, IS_ENABLED);
		JSOC_ELEMENT.VAR_ENABLED(STATUS_DELIVERY_CMD, IS_ENABLED);
		JSOC_ELEMENT.VAR_ENABLED(STATUS_VOID_CMD, IS_ENABLED);
		JSOC_ELEMENT.VAR_ENABLED(STATUS_COMPLETE_CMD, IS_ENABLED);
	};
		
	FORM_CHILD_INITIALIZE();