<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * Array debug
 *
 * @access	public
 * @param	string
 * @return	string
 */	
function ad($data)
{
	echo "<pre>";
	print_r($data);
	echo "</pre>";
}

// ------------------------------------------------------------------------

/**
 * Select language
 *
 * @access	public
 * @return	string
 */	
function get_language()
{
	$obj =& get_instance();
	$obj->load->library('session');
	
	$lang = 'en';
	if($obj->session->userdata('lang')) {
		$lang = $obj->session->userdata('lang');
	}
	
	return $lang;
}

// ------------------------------------------------------------------------

/**
 * Save current url
 *
 * @access	public
 * @param	string
 * @return	string
 */	
function save_current_url($url = NULL)
{
	$obj =& get_instance();
	$obj->load->library('session');
	
	if($url == NULL) {
		$url = base_url();
	}
	
	$obj->session->set_userdata('referrer_url', $url);
}

// ------------------------------------------------------------------------

/**
 * Get previous url
 *
 * @access	public
 * @return	string
 */	
function get_previous_url()
{
	$obj =& get_instance();
	$obj->load->library('session');
	
	if($obj->session->userdata('referrer_url')) {
		$url = $obj->session->userdata('referrer_url');
	}
	else {
		$url = base_url();
	}
	
	return $url;
}

// ------------------------------------------------------------------------

/**
 * Verify email format
 *
 * @access	public
 * @param	string
 * @return	boolean
 */	
function isEmail($email = NULL) {
	return(preg_match("/^[-_.[:alnum:]]+@((([[:alnum:]]|[[:alnum:]][[:alnum:]-]*[[:alnum:]])\.)+(ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|me|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nt|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)$|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i",$email));
}

// ------------------------------------------------------------------------

/**
 * Get Country
 *
 * @access	public
 * @param	string
 * @return	string
 */	
function get_country_name($country_code = null) {
	$list = array(
		'MY' => 'country_my',
		'CN' => 'country_cn',
		'HK' => 'country_hk',
		'TW' => 'country_tw',
		'TH' => 'country_th',
		'ID' => 'country_id',
		'PH' => 'country_ph',
		'SG' => 'country_sg',
		'JP' => 'country_jp',
		'KR' => 'country_kr',
		'VN' => 'country_vn',
		'ES' => 'country_es',
		'NP' => 'country_np',
		'AR' => 'country_ar',
		'AF' => 'country_af',
		'AX' => 'country_ax',
		'AL' => 'country_al',
		'DZ' => 'country_dz',
		'AS' => 'country_as',
		'AD' => 'country_ad',
		'AO' => 'country_ao',
		'AI' => 'country_ai',
		'AQ' => 'country_aq',
		'AG' => 'country_ag',
		'AM' => 'country_am',
		'AW' => 'country_aw',
		'AU' => 'country_au',
		'AT' => 'country_at',
		'AZ' => 'country_az',
		'BS' => 'country_bs',
		'BH' => 'country_bh',
		'BD' => 'country_bd',
		'BB' => 'country_bb',
		'BY' => 'country_by',
		'BE' => 'country_be',
		'BZ' => 'country_bz',
		'BJ' => 'country_bj',
		'BM' => 'country_bm',
		'BT' => 'country_bt',
		'BO' => 'country_bo',
		'BA' => 'country_ba',
		'BW' => 'country_bw',
		'BV' => 'country_bv',
		'BR' => 'country_br',
		'IO' => 'country_io',
		'BN' => 'country_bn',
		'BG' => 'country_bg',
		'BF' => 'country_bf',
		'BI' => 'country_bi',
		'KH' => 'country_kh',
		'CM' => 'country_cm',
		'CA' => 'country_ca',
		'CV' => 'country_cv',
		'KY' => 'country_ky',
		'CF' => 'country_cf',
		'TD' => 'country_td',
		'CL' => 'country_cl',
		'CX' => 'country_cx',
		'CC' => 'country_cc',
		'CO' => 'country_co',
		'KM' => 'country_km',
		'CG' => 'country_cg',
		'CD' => 'country_cd',
		'CK' => 'country_ck',
		'CR' => 'country_cr',
		'CI' => 'country_ci',
		'HR' => 'country_hr',
		'CU' => 'country_cu',
		'CY' => 'country_cy',
		'CZ' => 'country_cz',
		'DK' => 'country_dk',
		'DJ' => 'country_dj',
		'DM' => 'country_dm',
		'DO' => 'country_do',
		'EC' => 'country_ec',
		'EG' => 'country_eg',
		'SV' => 'country_sv',
		'GQ' => 'country_gq',
		'ER' => 'country_er',
		'EE' => 'country_ee',
		'ET' => 'country_et',
		'FK' => 'country_fk',
		'FO' => 'country_fo',
		'FJ' => 'country_fj',
		'FI' => 'country_fi',
		'FR' => 'country_fr',
		'GF' => 'country_gf',
		'PF' => 'country_pf',
		'TF' => 'country_tf',
		'GA' => 'country_ga',
		'GM' => 'country_gm',
		'GE' => 'country_ge',
		'DE' => 'country_de',
		'GH' => 'country_gh',
		'GI' => 'country_gi',
		'GR' => 'country_gr',
		'GL' => 'country_gl',
		'GD' => 'country_gd',
		'GP' => 'country_gp',
		'GU' => 'country_gu',
		'GT' => 'country_gt',
		'GG' => 'country_gg',
		'GN' => 'country_gn',
		'GW' => 'country_gw',
		'GY' => 'country_gy',
		'HT' => 'country_ht',
		'HM' => 'country_hm',
		'HN' => 'country_hn',
		'HU' => 'country_hu',
		'IS' => 'country_is',
		'IN' => 'country_in',
		'IR' => 'country_ir',
		'IQ' => 'country_iq',
		'IE' => 'country_ie',
		'IM' => 'country_im',
		'IL' => 'country_il',
		'IT' => 'country_it',
		'JM' => 'country_jm',
		'JE' => 'country_je',
		'JO' => 'country_jo',
		'KZ' => 'country_kz',
		'KE' => 'country_ke',
		'KI' => 'country_ki',
		'KW' => 'country_kw',
		'KG' => 'country_kg',
		'LA' => 'country_la',
		'LV' => 'country_lv',
		'LB' => 'country_lb',
		'LS' => 'country_ls',
		'LR' => 'country_lr',
		'LY' => 'country_ly',
		'LI' => 'country_li',
		'LT' => 'country_lt',
		'LU' => 'country_lu',
		'MO' => 'country_mo',
		'MK' => 'country_mk',
		'MG' => 'country_mg',
		'MW' => 'country_mw',
		'MV' => 'country_mv',
		'ML' => 'country_ml',
		'MT' => 'country_mt',
		'MH' => 'country_mh',
		'MQ' => 'country_mq',
		'MR' => 'country_mr',
		'MU' => 'country_mu',
		'YT' => 'country_yt',
		'MX' => 'country_mx',
		'FM' => 'country_fm',
		'MD' => 'country_md',
		'MC' => 'country_mc',
		'MN' => 'country_mn',
		'ME' => 'country_me',
		'MS' => 'country_ms',
		'MA' => 'country_ma',
		'MZ' => 'country_mz',
		'MM' => 'country_mm',
		'NA' => 'country_na',
		'NR' => 'country_nr',
		'NL' => 'country_nl',
		'AN' => 'country_an',
		'NC' => 'country_nc',
		'NZ' => 'country_nz',
		'NI' => 'country_ni',
		'NE' => 'country_ne',
		'NG' => 'country_ng',
		'NU' => 'country_nu',
		'NF' => 'country_nf',
		'MP' => 'country_mp',
		'KP' => 'country_kp',
		'NO' => 'country_no',
		'OM' => 'country_om',
		'PK' => 'country_pk',
		'PW' => 'country_pw',
		'PS' => 'country_ps',
		'PA' => 'country_pa',
		'PG' => 'country_pg',
		'PY' => 'country_py',
		'PE' => 'country_pe',
		'PN' => 'country_pn',
		'PL' => 'country_pl',
		'PT' => 'country_pt',
		'PR' => 'country_pr',
		'QA' => 'country_qa',
		'RE' => 'country_re',
		'RO' => 'country_ro',
		'RU' => 'country_ru',
		'RW' => 'country_rw',
		'SH' => 'country_sh',
		'KN' => 'country_kn',
		'LC' => 'country_lc',
		'PM' => 'country_pm',
		'VC' => 'country_vc',
		'WS' => 'country_ws',
		'SM' => 'country_sm',
		'ST' => 'country_st',
		'SA' => 'country_sa',
		'SN' => 'country_sn',
		'RS' => 'country_rs',
		'CS' => 'country_cs',
		'SC' => 'country_sc',
		'SL' => 'country_sl',
		'SK' => 'country_sk',
		'SI' => 'country_si',
		'SB' => 'country_sb',
		'SO' => 'country_so',
		'ZA' => 'country_za',
		'GS' => 'country_gs',
		'LK' => 'country_lk',
		'SD' => 'country_sd',
		'SR' => 'country_sr',
		'SJ' => 'country_sj',
		'SZ' => 'country_sz',
		'SE' => 'country_se',
		'CH' => 'country_ch',
		'SY' => 'country_sy',
		'TJ' => 'country_tj',
		'TZ' => 'country_tz',
		'TL' => 'country_tl',
		'TG' => 'country_tg',
		'TK' => 'country_tk',
		'TO' => 'country_to',
		'TT' => 'country_tt',
		'TN' => 'country_tn',
		'TR' => 'country_tr',
		'TM' => 'country_tm',
		'TC' => 'country_tc',
		'TV' => 'country_tv',
		'UG' => 'country_ug',
		'UA' => 'country_ua',
		'AE' => 'country_ae',
		'GB' => 'country_gb',
		'US' => 'country_us',
		'UM' => 'country_um',
		'UY' => 'country_uy',
		'UZ' => 'country_uz',
		'VU' => 'country_vu',
		'VA' => 'country_va',
		'VE' => 'country_ve',
		'VG' => 'country_vg',
		'VI' => 'country_vi',
		'WF' => 'country_wf',
		'EH' => 'country_eh',
		'YE' => 'country_ye',
		'ZM' => 'country_zm',
		'ZW' => 'country_zw'
	);
		
	if($country_code)
		return $list[$country_code];
	else
		return $list;	
}

// ------------------------------------------------------------------------

/**
 * Get Gender
 *
 * @access	public
 * @param	numeric
 * @return	string
 */	
function get_gender($type = null) {
	$list = array(
		1 => 'gender_male',
		2 => 'gender_female'
	);
		
	if($type)
		return $list[$type];
	else
		return $list;	
}

// ------------------------------------------------------------------------

/**
 * Get Member Type
 *
 * @access	public
 * @param	numeric
 * @return	string
 */	
function get_member_type($type = null) {
	$list = array(
		1 => 'registration_empty_sales',
		2 => 'registration_real_sales',
		3 => 'registration_borrow_sales',
	);
		
	if($type)
		return $list[$type];
	else
		return $list;	
}

// ------------------------------------------------------------------------

/**
 * Get Activated Status
 *
 * @access	public
 * @param	numeric
 * @return	string
 */	
function get_activated_status($type = null) {
	$list = array(
		0 => 'registration_deactivated',
		1 => 'registration_activated'
	);
		
	if($type)
		return $list[$type];
	else
		return $list;	
}

// ------------------------------------------------------------------------

/**
 * Get Account Status
 *
 * @access	public
 * @param	numeric
 * @return	string
 */	
function get_account_status($type = null) {
	$list = array(
		0 => 'registration_suspended',
		1 => 'registration_active'
	);
		
	if($type)
		return $list[$type];
	else
		return $list;	
}

// ------------------------------------------------------------------------

/**
 * Get Package
 *
 * @access	public
 * @param	numeric
 * @return	string
 */	
function get_package($type = null) {
	$list = array(
		1 => 'package_1',
		2 => 'package_2',
		3 => 'package_3',
	);
		
	if($type > 0) {
		return $list[$type];
	}
	else if($type == 0) {
		return 'package_0';
	}	
	else {
		return $list;
	}	
}

// ------------------------------------------------------------------------

/**
 * Get ID Type
 *
 * @access	public
 * @param	string
 * @return	string or array
 */
 
function get_id_type($type = null) {
	$list = array(
		1 => 'id_type_passport',
		2 => 'id_type_ic_no'
	);
		
	if($type)
		return $list[$type];
	else
		return $list;	
}

// ------------------------------------------------------------------------

/**
 * Get Member Unread Message
 *
 * @access	public
 * @return	string
 */	
function get_user_unread_message() {
	$obj =& get_instance();
	
	$obj->load->model('user_model');
	
	$result = $obj->user_model->get_user_unread_message();
	
	return $result;
}

// ------------------------------------------------------------------------

/**
 * Get unique code
 *
 * @access	public
 * @return	string
 */	
function random_string($length) {
    $key = '';
    $keys = array_merge(range(0, 9), range('a', 'z'));

    for ($i = 0; $i < $length; $i++) {
        $key .= $keys[array_rand($keys)];
    }

    return $key;
}

// ------------------------------------------------------------------------

/**
 * Get Permission
 *
 * @access	public
 * @param	array
 * @param	object
 * @return	boolean
 */	
function hasModule($module, $obj) {
	return $obj->isSuperUser || isset($obj->userModules) && in_array($module, $obj->userModules) ? true : false;
}

// ------------------------------------------------------------------------

/**
 * Get Currency Type Without Symbol
 *
 * @access	public
 * @param	numeric
 * @return	string
 */	
function get_currency_type_without_symbol($type = null) {
	$list = array(
		1 => 'currency_cny_without_symbol',
		2 => 'currency_hkd_without_symbol',
		3 => 'currency_myr_without_symbol',
		4 => 'currency_usd_without_symbol',
		5 => 'currency_usdt',
		6 => 'currency_thb_without_symbol',
		7 => 'currency_ntd_without_symbol',
		8 => 'currency_sgd_without_symbol',
		9 => 'currency_vnd_without_symbol',
		10 => 'currency_jpy_without_symbol',
		11 => 'currency_idr_without_symbol',
		12 => 'currency_aud_without_symbol',
		13 => 'currency_krw_without_symbol',
		14 => 'currency_cad_without_symbol',
		15 => 'currency_gbp_without_symbol',
		16 => 'currency_eur_without_symbol',
		17 => 'currency_npr_without_symbol',
		18 => 'currency_ars_without_symbol',
		19 => 'currency_php_without_symbol',
		20 => 'currency_ping',
		21 => 'currency_ping_public',
	);
		
	if($type)
		return $list[$type];
	else
		return $list;	
}

// ------------------------------------------------------------------------

/**
 * Get Level
 *
 * @access	public
 * @param	numeric
 * @return	string
 */	
function get_level($type = null) {
	$list = array(
		1 => 'level_1',
		2 => 'level_2',
		3 => 'level_3',
		4 => 'level_4',
	);
		
	if($type > 0) {
		return $list[$type];
	}
	else if($type == 0) {
		return 'level_0';
	}	
	else {
		return $list;
	}	
}

// ------------------------------------------------------------------------

/**
 * Get China Bank
 *
 * @access	public
 * @param	string
 * @return	string or array
 */
 
function get_china_bank($bank = null) {
	$list = array(
		'bank_icbc' => 'bank_icbc',
		'bank_abc' => 'bank_abc',
		'bank_boc' => 'bank_boc',
		'bank_ccb' => 'bank_ccb',
		'bank_psbc' => 'bank_psbc',
		'bank_bocom' => 'bank_bocom',
		'bank_cmb' => 'bank_cmb',
		'bank_spdb' => 'bank_spdb',
		'bank_citic' => 'bank_citic',
		'bank_ceb' => 'bank_ceb',
		'bank_hxb' => 'bank_hxb',
		'bank_cmbc' => 'bank_cmbc',
		'bank_cgb' => 'bank_cgb',
		'bank_ib' => 'bank_ib',
		'bank_pab' => 'bank_pab',
		'bank_hfb' => 'bank_hfb',
		'bank_czb' => 'bank_czb',
		'bank_cbb' => 'bank_cbb',
		'bank_bb' => 'bank_bb',
		'bank_bt' => 'bank_bt',
		'bank_bh' => 'bank_bh',
		'bank_jh' => 'bank_jh',
		'bank_bsb' => 'bank_bsb',
		'bank_sb' => 'bank_sb',
		'bank_bj' => 'bank_bj',
		'bank_bs' => 'bank_bs',
		'bank_crb' => 'bank_crb',
		'bank_srcb' => 'bank_srcb',
	);
		
	if($bank)
		return $list[$bank];
	else
		return $list;	
}

// ------------------------------------------------------------------------

/**
 * Get Hong Kong Bank
 *
 * @access	public
 * @param	string
 * @return	string or array
 */
 
function get_hong_kong_bank($bank = null) {
	$list = array(
		'bank_hsbc' => 'bank_hsbc',
		'bank_bchk' => 'bank_bchk',
		'bank_hsb' => 'bank_hsb',
		'bank_scbhk' => 'bank_scbhk',
		'bank_icbca' => 'bank_icbca',
		'bank_bea' => 'bank_bea',
		'bank_ccba' => 'bank_ccba',
		'bank_dbshk' => 'bank_dbshk',
		'bank_ncb' => 'bank_ncb',
		'bank_citici' => 'bank_citici',
		'bank_bocomhk' => 'bank_bocomhk',
	);
		
	if($bank)
		return $list[$bank];
	else
		return $list;	
}

// ------------------------------------------------------------------------

/**
 * Get Malaysia Bank
 *
 * @access	public
 * @param	string
 * @return	string or array
 */
 
function get_malaysia_bank($bank = null) {
	$list = array(
		'bank_mb' => 'bank_mb',
		'bank_cimb' => 'bank_cimb',
		'bank_rhb' => 'bank_rhb',
		'bank_hlb' => 'bank_hlb',
		'bank_pb' => 'bank_pb',
		'bank_hsbcmy' => 'bank_hsbcmy',
		'bank_ocbc' => 'bank_ocbc',
		'bank_scb' => 'bank_scb',
		'bank_citi' => 'bank_citi',
		'bank_uob' => 'bank_uob',
		'bank_bsn' => 'bank_bsn',
		'bank_amb' => 'bank_amb',
		'bank_bimb' => 'bank_bimb',
		'bank_alb' => 'bank_alb',
		'bank_afb' => 'bank_afb',
		'bank_arb' => 'bank_arb',
	);
		
	if($bank)
		return $list[$bank];
	else
		return $list;	
}

function get_malaysia_bank_2($bank = null) {
	$list = array(
		'bank_mb' => 'bank_mb_2',
		'bank_cimb' => 'bank_cimb_2',
		'bank_rhb' => 'bank_rhb_2',
		'bank_hlb' => 'bank_hlb_2',
		'bank_pb' => 'bank_pb_2',
		'bank_hsbcmy' => 'bank_hsbcmy',
		'bank_ocbc' => 'bank_ocbc',
		'bank_scb' => 'bank_scb',
		'bank_citi' => 'bank_citi',
	);
		
	if($bank)
		return $list[$bank];
	else
		return $list;	
}

function get_malaysia_bank_3($bank = null) {
	$list = array(
		PG_FPAY_MBB => 'bank_mb',
		PG_FPAY_CIMB => 'bank_cimb',
		PG_FPAY_RHB => 'bank_rhb',
		PG_FPAY_HLB => 'bank_hlb',
		PG_FPAY_PBB => 'bank_pb',
		PG_FPAY_SCB => 'bank_scb',
		PG_FPAY_UOB => 'bank_uob',
		PG_FPAY_HSBC => 'bank_hsbcmy',
		PG_FPAY_CTB => 'bank_citi',
		PG_FPAY_BSN => 'bank_bsn',
		PG_FPAY_AMB => 'bank_amb',
		PG_FPAY_BIMB => 'bank_bimb',
		PG_FPAY_ALB => 'bank_alb',
		PG_FPAY_AFB => 'bank_afb',
		PG_FPAY_ARB => 'bank_arb',
	);
		
	if($bank)
		return $list[$bank];
	else
		return $list;	
}

// ------------------------------------------------------------------------

/**
 * Get Platform Type
 *
 * @access	public
 * @param	numeric
 * @return	string
 */	
function get_platform_type($type = null) {
	$list = array(
		1 => 'platform_1',
		2 => 'platform_2',
	);
		
	if($type)
		return $list[$type];
	else
		return $list;	
}

// ------------------------------------------------------------------------

/**
 * Get Department Type
 *
 * @access	public
 * @param	numeric
 * @return	string
 */	
function get_department_type($type = null) {
	$list = array(
		1 => 'department_1',
		2 => 'department_2',
		3 => 'department_3',
	);
		
	if($type)
		return $list[$type];
	else
		return $list;	
}

// ------------------------------------------------------------------------