<?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(
		'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',
		'AR' => 'country_ar',
		'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',
		'CN' => 'country_cn',
		'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',
		'HK' => 'country_hk',
		'HU' => 'country_hu',
		'IS' => 'country_is',
		'IN' => 'country_in',
		'ID' => 'country_id',
		'IR' => 'country_ir',
		'IQ' => 'country_iq',
		'IE' => 'country_ie',
		'IM' => 'country_im',
		'IL' => 'country_il',
		'IT' => 'country_it',
		'JM' => 'country_jm',
		'JP' => 'country_jp',
		'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',
		'MY' => 'country_my',
		'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',
		'NP' => 'country_np',
		'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',
		'PH' => 'country_ph',
		'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',
		'SG' => 'country_sg',
		'SK' => 'country_sk',
		'SI' => 'country_si',
		'SB' => 'country_sb',
		'SO' => 'country_so',
		'ZA' => 'country_za',
		'GS' => 'country_gs',
		'KR' => 'country_kr',
		'ES' => 'country_es',
		'LK' => 'country_lk',
		'SD' => 'country_sd',
		'SR' => 'country_sr',
		'SJ' => 'country_sj',
		'SZ' => 'country_sz',
		'SE' => 'country_se',
		'CH' => 'country_ch',
		'SY' => 'country_sy',
		'TW' => 'country_tw',
		'TJ' => 'country_tj',
		'TZ' => 'country_tz',
		'TH' => 'country_th',
		'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',
		'VN' => 'country_vn',
		'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',
	);
		
	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_usd_without_symbol',
		2 => 'currency_cny_without_symbol',
		3 => 'currency_myr_without_symbol',
		4 => 'currency_idr_without_symbol',
		5 => 'currency_usdt',
		6 => 'currency_thb_without_symbol',
		7 => 'currency_vnd_without_symbol',
		8 => 'currency_sgd_without_symbol',
	);
		
	if($type)
		return $list[$type];
	else
		return $list;	
}

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

/**
 * Get Currency rates
 *
 * @access	public
 * @return	string
 */	
function get_curency_rates() {
	$obj =& get_instance();
	
	$obj->load->model('user_model');
	
	$result = $obj->user_model->get_curency_rates();
	
	return $result;
}

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

/**
 * 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',
		5 => 'level_5',
		6 => 'level_6',
		7 => 'level_7',
		8 => 'level_8',
	);
		
	if($type > 0) {
		return $list[$type];
	}
	else if($type == 0) {
		return 'level_0';
	}	
	else {
		return $list;
	}	
}

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