// Required: common.js

// CATEGORY_SUFFIX GET
JSCUS_SQL._MOBILE._SMS = function(){};

	/** 
	 * =======================================================================================================
	 * Perform SQL GET request to the table SMS Balance. Returns String
	 * @param CALLBACK_ADDRESS(bool, String) A callback method when the server response is received.
	 * =======================================================================================================
	 */
	JSCUS_SQL._MOBILE._SMS.CUS_BALANCE_PRELOAD = function()
	{		
		// Check if auto populate element exists
		// _______________________________________________________________________		
		if
		(
			JSOC_ELEMENT.VAR_ELEMENT_EXISTS('AUTO_SMS_SERVER_BALANCE_LBL') == false &&
			JSOC_ELEMENT.VAR_ELEMENT_EXISTS('AUTO_SMS_SERVER_BALANCE_TXT') == false
		)
		{
			return;
		}
		
		// Load Balance
		// _______________________________________________________________________	
		JSRC_THREAD.REF_POST_DELAYED
		(
			function()
			{
				JSCUS_SQL._MOBILE._SMS.CUS_BALANCE
				(
					function(IS_SUCCESS, RESPONSE_STRING)
					{
						// Validate
						// ...........................................
						if(IS_SUCCESS === false)
						{
							return;
						}
						
						// Validate JSON Response
						// ...........................................
						var JSON_OBJECT = JSRC_JSON.REF_CREATE(RESPONSE_STRING);
						if(JSRC_JSON.REF_IS_SUCCESS(JSON_OBJECT) === false)
						{
							return;
						}
						
						// JSON Empty (Clears the DataGrodView)
						// ...........................................
						if(JSRC_JSON.REF_READ(JSON_OBJECT, "JSON_COUNT") <= 0)
						{
							return;
						}
						var SMS_BALANCE_VALUE = JSRC_JSON.REF_READ(JSON_OBJECT, "REC_BALANCE_VALUE", 1);
						SMS_BALANCE_VALUE = JSVC_NUM.VAR_CFORMAT(JSVC_NUM.VAR_NVAL(SMS_BALANCE_VALUE));
						
						// Auto Load
						// ...........................................
						if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS('AUTO_SMS_SERVER_BALANCE_LBL'))
						{
							JSOC_ELEMENT.VAR_VALUE('AUTO_SMS_SERVER_BALANCE_LBL', SMS_BALANCE_VALUE);
						}
						
						// Auto Load
						// ...........................................
						if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS('AUTO_SMS_SERVER_BALANCE_TXT'))
						{
							JSOC_ELEMENT.VAR_VALUE('AUTO_SMS_SERVER_BALANCE_TXT', SMS_BALANCE_VALUE);
						}
						
					}
				);
			},
			3000
		);	
	};
	
	
	
	/** 
	 * =======================================================================================================
	 * Perform SQL GET request to the table SMS Balance. Returns String
	 * @param CALLBACK_ADDRESS(bool, String) A callback method when the server response is received.
	 * =======================================================================================================
	 */
	JSCUS_SQL._MOBILE._SMS.CUS_BALANCE_INITIALIZE = function()
	{		
		
		// Call methods for Auto-populate label (if exists)
		// _______________________________________________________________________	
		JSCUS_SQL._MOBILE._SMS.CUS_BALANCE_PRELOAD();
		
		
		// Check if balance check button element exists
		// _______________________________________________________________________		
		if
		(
			JSOC_ELEMENT.VAR_ELEMENT_EXISTS('AUTO_SMS_SERVER_BALANCE_BBI') == false &&
			JSOC_ELEMENT.VAR_ELEMENT_EXISTS('AUTO_SMS_SERVER_BALANCE_CMD') == false
		)
		{
			return;
		}
		
		// Declare Function
		// _______________________________________________________________________	
		var SMS_CALLBACK_FUNCTION =	function()
		{
			JSOC_DIALOG.VAR_LOADING_SPINNER(true);
			JSCUS_SQL._MOBILE._SMS.CUS_BALANCE_ON_INITIALIZE
			(
				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;
					}
					var SMS_BALANCE_VALUE = JSRC_JSON.REF_READ(JSON_OBJECT, "REC_BALANCE_VALUE", 1);
					SMS_BALANCE_VALUE = JSVC_NUM.VAR_CFORMAT(JSVC_NUM.VAR_NVAL(SMS_BALANCE_VALUE));
					
					// Show Message
					// ...........................................
					JSOC_DIALOG.VAR_MESSAGE_BOX
					(
						'Your remaining SMS balance is : ' + SMS_BALANCE_VALUE, 
						'SMS Balance', 
						JSOC_DIALOG.MSGBOX_TYPE_OK_ONLY,
						JSOC_DIALOG.MSGBOX_ICON_INFO,
						null
					);
				}
			);
		};
		
		// Declare Event
		// _______________________________________________________________________	
		if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS('AUTO_SMS_SERVER_BALANCE_BBI'))
		{
			JSOC_ELEMENT.VAR_GET("AUTO_SMS_SERVER_BALANCE_BBI").onclick = SMS_CALLBACK_FUNCTION;  
		}
		if(JSOC_ELEMENT.VAR_ELEMENT_EXISTS('AUTO_SMS_SERVER_BALANCE_CMD'))
		{
			JSOC_ELEMENT.VAR_GET("AUTO_SMS_SERVER_BALANCE_CMD").onclick = SMS_CALLBACK_FUNCTION;  
		}		
	};
	JSCUS_SQL._MOBILE._SMS.CUS_BALANCE_INITIALIZE();	
	
	
	/** 
	 * =======================================================================================================
	 * Perform SQL GET request to the table SMS Balance. Returns String
	 * @param CALLBACK_ADDRESS(bool, String) A callback method when the server response is received.
	 * =======================================================================================================
	 */
	JSCUS_SQL._MOBILE._SMS.CUS_BALANCE_ON_INITIALIZE = function
	(
		CALLBACK_ADDRESS
	)
	{		
		// Check if pending requet exists
		// _______________________________________________________________________	
		if(CUS_SEND_REQUEST_HAS_QUEUE())
		{
			JSRC_THREAD.REF_POST_DELAYED
			(
				function()
				{
					JSCUS_SQL._MOBILE._SMS.CUS_BALANCE_ON_INITIALIZE
					(
						CALLBACK_ADDRESS
					);
				},
				1000 * 3
			);
			return;
		}
		
		// Send SQL Request
		// _______________________________________________________________________					
		JSCUS_SQL._MOBILE._SMS.CUS_BALANCE(CALLBACK_ADDRESS);	
	};
	
	
	/** 
	 * =======================================================================================================
	 * Perform SQL GET request to the table SMS Balance. Returns String
	 * @param CALLBACK_ADDRESS(bool, String) A callback method when the server response is received.
	 * =======================================================================================================
	 */
	JSCUS_SQL._MOBILE._SMS.CUS_BALANCE = function
	(
		CALLBACK_ADDRESS
	)
	{		
		// Send SQL Request
		// _______________________________________________________________________					
		CUS_SEND_REQUEST
		(
			CALLBACK_ADDRESS,
			JSCUS_CACHE.SERVER_URL + '/Mobile/sms/balance',
			JSCUS_CACHE.SERVER_URL + '/System/nonce/get', ''
		);	
	};


	/** 
	 * =======================================================================================================
	 * Perform SQL GET request to the table SMS Balance. Returns String
	 * @param CALLBACK_ADDRESS(bool, String) A callback method when the server response is received.
	 * =======================================================================================================
	 */
	JSCUS_SQL._MOBILE._SMS.CUS_OUTBOX = function
	(
		VAR_PARAMETER_ARRAY,
		CALLBACK_ADDRESS
	)
	{		
		// Initialize
		// _______________________________________________________________________
		var PARAM_SET = "";	
		PARAM_SET = JSRC_HTTP.REF_ADD(PARAM_SET, 'PARAM_REC_SMS_PHONE', VAR_PARAMETER_ARRAY["PARAM_REC_SMS_PHONE"]);
		PARAM_SET = JSRC_HTTP.REF_ADD(PARAM_SET, 'PARAM_REC_SMS_DATE_SCHEDULE_FROM', VAR_PARAMETER_ARRAY["PARAM_REC_SMS_DATE_SCHEDULE_FROM"]);
		PARAM_SET = JSRC_HTTP.REF_ADD(PARAM_SET, 'PARAM_REC_SMS_DATE_SCHEDULE_TO', VAR_PARAMETER_ARRAY["PARAM_REC_SMS_DATE_SCHEDULE_TO"]);
		
		PARAM_SET = JSRC_HTTP.REF_ADD(PARAM_SET, 'PARAM_ORDER_FIELD', VAR_PARAMETER_ARRAY["PARAM_ORDER_FIELD"]);
		PARAM_SET = JSRC_HTTP.REF_ADD(PARAM_SET, 'PARAM_ORDER_TYPE', VAR_PARAMETER_ARRAY["PARAM_ORDER_TYPE"]);
		PARAM_SET = JSRC_HTTP.REF_ADD(PARAM_SET, 'PARAM_SKIP', VAR_PARAMETER_ARRAY["PARAM_SKIP"]);
		PARAM_SET = JSRC_HTTP.REF_ADD(PARAM_SET, 'PARAM_LIMIT', VAR_PARAMETER_ARRAY["PARAM_LIMIT"]);

		// Send SQL Request
		// _______________________________________________________________________					
		CUS_SEND_REQUEST
		(
			CALLBACK_ADDRESS,
			JSCUS_CACHE.SERVER_URL + '/Mobile/sms/outbox',
			JSCUS_CACHE.SERVER_URL + '/System/nonce/get',
			PARAM_SET
		);	
	};
	
	/** 
	 * =======================================================================================================
	 * Populate SP-OUTBOX request parameters with default value.
	 * =======================================================================================================
	 */
	JSCUS_SQL._MOBILE._SMS.CUS_PREPARE_MAPPED_ARRAY_FOR_OUTBOX = function()
	{
		// Initialize
		var RESULT_MAPARRAY = [];

		// Populate default fields
		RESULT_MAPARRAY["PARAM_REC_SMS_PHONE"] = FLAG_NVARCHAR_NULL;
		RESULT_MAPARRAY["PARAM_REC_SMS_DATE_SCHEDULE_FROM"] = JSVC_DATE.VAR_FORMAT(JSVC_DATE.VAR_NOW(), "YYYY-MM-DD HH:mm:ss") + '.999999';
		RESULT_MAPARRAY["PARAM_REC_SMS_DATE_SCHEDULE_TO"] = JSVC_DATE.VAR_FORMAT(JSVC_DATE.VAR_NOW(), "YYYY-MM-DD HH:mm:ss") + '.999999';

		RESULT_MAPARRAY["PARAM_ORDER_FIELD"] = 'REC_SMS_DATE_REQUEST';
		RESULT_MAPARRAY["PARAM_ORDER_TYPE"] = 'DESC';
		RESULT_MAPARRAY["PARAM_SKIP"] = 0;
		RESULT_MAPARRAY["PARAM_LIMIT"] = 1000;

		// Return prepared MapArray
		return RESULT_MAPARRAY;		
	};
	
	/** 
	 * =======================================================================================================
	 * Perform SQL GET request to the table SMS Balance. Returns String
	 * @param CALLBACK_ADDRESS(bool, String) A callback method when the server response is received.
	 * =======================================================================================================
	 */
	JSCUS_SQL._MOBILE._SMS.CUS_BILL = function
	(
		VAR_PARAMETER_ARRAY,
		CALLBACK_ADDRESS
	)
	{		
		// Initialize
		// _______________________________________________________________________
		var PARAM_SET = "";	
		PARAM_SET = JSRC_HTTP.REF_ADD(PARAM_SET, 'PARAM_REC_RELOAD_CODE', VAR_PARAMETER_ARRAY["PARAM_REC_RELOAD_CODE"]);
		PARAM_SET = JSRC_HTTP.REF_ADD(PARAM_SET, 'PARAM_REC_RELOAD_DATE_TRANSACT_FROM', VAR_PARAMETER_ARRAY["PARAM_REC_RELOAD_DATE_TRANSACT_FROM"]);
		PARAM_SET = JSRC_HTTP.REF_ADD(PARAM_SET, 'PARAM_REC_RELOAD_DATE_TRANSACT_TO', VAR_PARAMETER_ARRAY["PARAM_REC_RELOAD_DATE_TRANSACT_TO"]);
		PARAM_SET = JSRC_HTTP.REF_ADD(PARAM_SET, 'PARAM_REC_RELOAD_HAS_BILL', VAR_PARAMETER_ARRAY["PARAM_REC_RELOAD_HAS_BILL"]);
		PARAM_SET = JSRC_HTTP.REF_ADD(PARAM_SET, 'PARAM_REC_RELOAD_REFERENCE_CODE', VAR_PARAMETER_ARRAY["PARAM_REC_RELOAD_REFERENCE_CODE"]);
		
		PARAM_SET = JSRC_HTTP.REF_ADD(PARAM_SET, 'PARAM_ORDER_FIELD', VAR_PARAMETER_ARRAY["PARAM_ORDER_FIELD"]);
		PARAM_SET = JSRC_HTTP.REF_ADD(PARAM_SET, 'PARAM_ORDER_TYPE', VAR_PARAMETER_ARRAY["PARAM_ORDER_TYPE"]);
		PARAM_SET = JSRC_HTTP.REF_ADD(PARAM_SET, 'PARAM_SKIP', VAR_PARAMETER_ARRAY["PARAM_SKIP"]);
		PARAM_SET = JSRC_HTTP.REF_ADD(PARAM_SET, 'PARAM_LIMIT', VAR_PARAMETER_ARRAY["PARAM_LIMIT"]);

		// Send SQL Request
		// _______________________________________________________________________					
		CUS_SEND_REQUEST
		(
			CALLBACK_ADDRESS,
			JSCUS_CACHE.SERVER_URL + '/Mobile/sms/bill',
			JSCUS_CACHE.SERVER_URL + '/System/nonce/get',
			PARAM_SET
		);	
	};
		
	/** 
	 * =======================================================================================================
	 * Populate SP-OUTBOX request parameters with default value.
	 * =======================================================================================================
	 */
	JSCUS_SQL._MOBILE._SMS.CUS_PREPARE_MAPPED_ARRAY_FOR_BILL = function()
	{
		// Initialize
		var RESULT_MAPARRAY = [];

		// Populate default fields
		RESULT_MAPARRAY["PARAM_REC_RELOAD_CODE"] = FLAG_NVARCHAR_NULL;
		RESULT_MAPARRAY["PARAM_REC_RELOAD_DATE_TRANSACT_FROM"] = JSVC_DATE.VAR_FORMAT(JSVC_DATE.VAR_NOW(), "YYYY-MM-DD HH:mm:ss") + '.999999';
		RESULT_MAPARRAY["PARAM_REC_RELOAD_DATE_TRANSACT_TO"] = JSVC_DATE.VAR_FORMAT(JSVC_DATE.VAR_NOW(), "YYYY-MM-DD HH:mm:ss") + '.999999';
		RESULT_MAPARRAY["PARAM_REC_RELOAD_HAS_BILL"] = FLAG_TINYINT_NULL;
		RESULT_MAPARRAY["PARAM_REC_RELOAD_REFERENCE_CODE"] = FLAG_NVARCHAR_NULL;
		
		RESULT_MAPARRAY["PARAM_ORDER_FIELD"] = 'REC_RELOAD_DATE_TRANSACT';
		RESULT_MAPARRAY["PARAM_ORDER_TYPE"] = 'DESC';
		RESULT_MAPARRAY["PARAM_SKIP"] = 0;
		RESULT_MAPARRAY["PARAM_LIMIT"] = 1000;

		// Return prepared MapArray
		return RESULT_MAPARRAY;		
	};
	
	/** 
	 * =======================================================================================================
	 * Perform SQL SAVE request to the table CATEGORY_SUFFIX. Returns DataSet.
	 *
	 * @param VAR_PARAMETER_ARRAY Array with Request ParameterSet.
	 * @param CALLBACK_ADDRESS(bool, String) A callback method when the server response is received.
	 * =======================================================================================================
	 */
	JSCUS_SQL._MOBILE._SMS.CUS_SAVE = function
	(
		VAR_PARAMETER_ARRAY,
		CALLBACK_ADDRESS
	)
	{
		// Parameter Validation
		// _______________________________________________________________________
		JSVC_AV.VAR_VALIDATE_ARGUMENT_TYPE(VAR_PARAMETER_ARRAY, 'VAR_PARAMETER_ARRAY', 'ARRAY');
		
		// Process SQL Request
		// _______________________________________________________________________
		var PARAM_SET = "";	
		var ARRAY_INDEX = 0;
		
		if(JSVC_VAR.VAR_IS_ARRAY(VAR_PARAMETER_ARRAY[0]) == true)
		{
			// Check limits per request 
			if(VAR_PARAMETER_ARRAY.length > CUSTOM_SITE_ARRAY_REQUEST_LIMIT)
			{
				throw "HTTP request is too large for single request. Limit : " + CUSTOM_SITE_ARRAY_REQUEST_LIMIT;
			}
			
			// Populate Array Items
			while(VAR_PARAMETER_ARRAY.length > 0 && ARRAY_INDEX < CUSTOM_SITE_ARRAY_REQUEST_LIMIT)
			{
				PARAM_SET = JSRC_HTTP.REF_ADD(PARAM_SET, 'PARAM_REC_BROADCAST_PHONE[]', VAR_PARAMETER_ARRAY[0]["PARAM_REC_BROADCAST_PHONE"]);
				PARAM_SET = JSRC_HTTP.REF_ADD(PARAM_SET, 'PARAM_REC_BROADCAST_MESSAGE[]',  VAR_PARAMETER_ARRAY[0]["PARAM_REC_BROADCAST_MESSAGE"]);
				PARAM_SET = JSRC_HTTP.REF_ADD(PARAM_SET, 'PARAM_REC_BROADCAST_REQUEST_DATE[]',  VAR_PARAMETER_ARRAY[0]["PARAM_REC_BROADCAST_REQUEST_DATE"]);
				
				VAR_PARAMETER_ARRAY.shift();
				ARRAY_INDEX ++;
			}					
		}
		else
		{
			PARAM_SET = JSRC_HTTP.REF_ADD(PARAM_SET, 'PARAM_REC_BROADCAST_PHONE[]', VAR_PARAMETER_ARRAY["PARAM_REC_BROADCAST_PHONE"]);
			PARAM_SET = JSRC_HTTP.REF_ADD(PARAM_SET, 'PARAM_REC_BROADCAST_MESSAGE[]',  VAR_PARAMETER_ARRAY["PARAM_REC_BROADCAST_MESSAGE"]);
			PARAM_SET = JSRC_HTTP.REF_ADD(PARAM_SET, 'PARAM_REC_BROADCAST_REQUEST_DATE[]',  VAR_PARAMETER_ARRAY["PARAM_REC_BROADCAST_REQUEST_DATE"]);
		}

		// Send SQL Request
		// _______________________________________________________________________		
		CUS_SEND_REQUEST
		(
			CALLBACK_ADDRESS,
			JSCUS_CACHE.SERVER_URL + '/Mobile/sms/send',
			JSCUS_CACHE.SERVER_URL + '/System/nonce/get',
			PARAM_SET
		);	
	};
	
	/** 
	 * =======================================================================================================
	 * Populate SP-SAVE request parameters with default value.
	 * =======================================================================================================
	 */
	JSCUS_SQL._MOBILE._SMS.CUS_PREPARE_MAPPED_ARRAY_FOR_SAVE = function()
	{
		// Initialize
		var RESULT_MAPARRAY = [];

		// Populate default fields
		RESULT_MAPARRAY["PARAM_REC_BROADCAST_PHONE"] = FLAG_NVARCHAR_NULL;
		RESULT_MAPARRAY["PARAM_REC_BROADCAST_MESSAGE"] = FLAG_NVARCHAR_NULL;
		RESULT_MAPARRAY["PARAM_REC_BROADCAST_REQUEST_DATE"] = JSVC_DATE.VAR_FORMAT(JSVC_DATE.VAR_NOW(), "YYYY-MM-DD HH:mm:ss") + '.999999';

		// Return prepared MapArray
		return RESULT_MAPARRAY;		
	};
	
	/** 
	 * =======================================================================================================
	 * Add SP-GET request for multiple record save request.
	 * =======================================================================================================
	 */	
	JSCUS_SQL._MOBILE._SMS.CUS_ADD_MAPPED_ARRAY_FOR_SAVE = function
	(
		VAR_GROUPED_PARAMETER_ARRAY,
		VAR_ADD_PARAMETER_ARRAY
	)
	{
		// Parameter Validation
		// _______________________________________________________________________
		JSVC_AV.VAR_VALIDATE_ARGUMENT_TYPE(VAR_GROUPED_PARAMETER_ARRAY, 'VAR_GROUPED_PARAMETER_ARRAY', 'ARRAY');
		JSVC_AV.VAR_VALIDATE_ARGUMENT_TYPE(VAR_ADD_PARAMETER_ARRAY, 'VAR_ADD_PARAMETER_ARRAY', 'ARRAY');
		
		// Push ParameterSet
		// _______________________________________________________________________
		VAR_GROUPED_PARAMETER_ARRAY.push(VAR_ADD_PARAMETER_ARRAY);
		return VAR_GROUPED_PARAMETER_ARRAY;
	};