var CACHE_CSV_MAP_ARRAY = [];
var CSV_COLUMN_LENGTH = 9;


/**
 * ===============================================================
 * Browse CSV data
 * ===============================================================
 */
function IMPORTER_BROWSE()
{
  	JSOC_DIALOG.VAR_BROWSE_BOX
	(
		'Browse File', 
		'Browse',
		function(BROWSE_FP, IS_SUCCESS)
		{
			if(IS_SUCCESS == true)
			{
				JSOC_INPUT_FILE.VAR_READ_TEXT
				(
					function(CSV_SCRIPT)
					{
						IMPORTER_CSV_DECODER(CSV_SCRIPT);
					},
					BROWSE_FP
				);
			}
			else
			{
				alert('Aborted');
			}
		}
	);
};



/**
 * ===============================================================
 * Load CSV data
 * ===============================================================
 */
function IMPORTER_CSV_DECODER(CSV_SCRIPT)
{
	IMPORT_BBI.disabled = true;
	JSRC_THREAD.REF_ASYNCTASK
	(
		function()
		{
			// Initialize
			// _______________________________________________________________________
			var SCRIPT_ROWS = [];
			var SCRIPT_COLUMNS = [];
			var RESULT_MAPPED_ARRAY = [];
		
			// Split CSV per line
			// _______________________________________________________________________
			SCRIPT_ROWS = CSV_SCRIPT.split(/\r\n|\r|\n/);
			
			
			// Remove First Row (Header)
			// _______________________________________________________________________
			if(JSVC_ARR.VAR_EMPTY(SCRIPT_ROWS) == false)
			{
				SCRIPT_ROWS.shift();
			}
			
		
			// Process Lines
			// _______________________________________________________________________
			while(JSVC_ARR.VAR_EMPTY(SCRIPT_ROWS) == false)
			{
				// Skip line if empty
				// ...........................................
				if(JSVC_STR.VAR_IS_BLANK(SCRIPT_ROWS[0]))
				{
					SCRIPT_ROWS.shift();
					continue;
				}
				
				// Convert line to columns
				// ...........................................
				SCRIPT_COLUMNS = JSRC_EXCEL.REF_TO_MAPARRAY(SCRIPT_ROWS[0]);
				// console.log(SCRIPT_COLUMNS);
				
				// Validate Columns
				// ...........................................
				if(JSVC_ARR.VAR_EMPTY(SCRIPT_COLUMNS) || JSVC_ARR.VAR_EMPTY(SCRIPT_COLUMNS[0]))
				{
					SCRIPT_ROWS.shift();
					continue;				
				}
				else
				{
					SCRIPT_COLUMNS = SCRIPT_COLUMNS[0];
				}
				if(SCRIPT_COLUMNS.length < CSV_COLUMN_LENGTH)
				{
					console.log(SCRIPT_COLUMNS);
					alert("Invalid data - expecting " + CSV_COLUMN_LENGTH + " columns, given " + SCRIPT_COLUMNS.length + " \n" + SCRIPT_COLUMNS.join(', '));
					IMPORT_BBI.disabled = false;
					return false;
				}
				
				// Replace First / Last Line if null
				// ...........................................
				if(JSVC_STR.VAR_IS_BLANK(SCRIPT_COLUMNS[SCRIPT_COLUMNS.length - 1]) && SCRIPT_COLUMNS.length > CSV_COLUMN_LENGTH)
				{
					SCRIPT_COLUMNS.pop();
				}
				
				// Array too large
				// ...........................................
				if(SCRIPT_COLUMNS.length > CSV_COLUMN_LENGTH)
				{
					alert("Invalid data - expecting " + CSV_COLUMN_LENGTH + " columns, given " + SCRIPT_COLUMNS.length + " \n" + SCRIPT_COLUMNS.join(', '));
					IMPORT_BBI.disabled = false;
					return false;				
				}
				
				// Add to result array
				// ...........................................
				RESULT_MAPPED_ARRAY.push(SCRIPT_COLUMNS);
				SCRIPT_ROWS.shift();
			}
			
			// Return Result
			// _______________________________________________________________________
			return RESULT_MAPPED_ARRAY;
		},
		function(DECODE_RESULT)
		{
			// Aborted
			if(DECODE_RESULT == false)
			{
				CACHE_CSV_MAP_ARRAY = [];
				alert("Import Aborted");
				IMPORT_BBI.disabled = false;
				return;
			}
			
			// Continue with populating data
			CACHE_CSV_MAP_ARRAY = DECODE_RESULT;
			IMPORTER_ASSIGN_00();
		}
	);
};


/**
 * ===============================================================
 * Import 00 : Back to Profile Tab
 * ===============================================================
 */
function IMPORTER_ASSIGN_00()
{
	// Initialize
	// _______________________________________________________________________
	JSOC_ELEMENT.VAR_VALUE('TASK_LBL', 'Importing records : [' + CACHE_CSV_MAP_ARRAY.length + '] left.');
	PROPERTY_REC_CLIENT_ID = 0;
	if(JSVC_VAR.VAR_ISSET(INPUT_INSTANCE))
	{
		JSOC_WINDOW.VAR_INPUT_INSTANCE_RESTORE(INPUT_INSTANCE);
	}
	
	// Populate Data
	// _______________________________________________________________________
	JSOC_ELEMENT.VAR_VALUE('REC_CLIENT_LASTNAME_TXT', CACHE_CSV_MAP_ARRAY[0][1]);
	JSOC_ELEMENT.VAR_VALUE('REC_CLIENT_FIRSTNAME_TXT', CACHE_CSV_MAP_ARRAY[0][2]);
	JSOC_ELEMENT.VAR_VALUE('REC_CLIENT_MIDDLENAME_TXT', CACHE_CSV_MAP_ARRAY[0][3]);
	
	JSOC_CMB.VAR_SELECTED_TEXT('REC_CLIENT_CAT_SUFFIX_ID_CMB', CACHE_CSV_MAP_ARRAY[0][4]);
	JSOC_CMB.VAR_SELECTED_TEXT('REC_CLIENT_SYS_GENDER_ID_CMB', CACHE_CSV_MAP_ARRAY[0][5]);
	
	JSOC_ELEMENT.VAR_VALUE('REC_CLIENT_CONTACT_TXT', CACHE_CSV_MAP_ARRAY[0][6]);
	JSOC_ELEMENT.VAR_VALUE('REC_CLIENT_ADDRESS_TXT', CACHE_CSV_MAP_ARRAY[0][7]);
	JSOC_ELEMENT.VAR_VALUE('REC_CLIENT_EMAIL_TXT', CACHE_CSV_MAP_ARRAY[0][8]);
	
	// Save client record
	// _______________________________________________________________________
	HTTP_SAVE
	(
		function(IS_SUCCESS)
		{
			// Abort on failure
			// ...........................................
			if(IS_SUCCESS == false)
			{
				CACHE_CSV_MAP_ARRAY = [];
				alert("Import Aborted");
				IMPORT_BBI.disabled = false;
				return;
			}
			CACHE_CSV_MAP_ARRAY.shift();
			
			// To next request
			// ...........................................
			if(CACHE_CSV_MAP_ARRAY.length > 0)
			{
				ADD_BBI.onclick();	
				IMPORTER_ASSIGN_00();			
			}
			else
			{
				JSVC_INFO.VAR_RECORD_SAVED('');
			}
		}
	);
};