/**
 * common functions
 *
 */
function getTimeZoneName() {
	var timezoneName = '';
	var timezoneHours = timeZoneOffset / 3600000;
	if (_timezoneStore) {
		var recordIndex = _timezoneStore.find('tzoffset', timezoneHours);
		if (recordIndex >= 0) {
			var record = _timezoneStore.getAt(recordIndex);
			if (record) {
				_timezoneDataKey = record.get('id');
				timezoneName = record.get('name');
			}
		}
	} else {
		var sign = '+';
		if (timezoneHours < 0) {
			sign = '-';
		}
		var hmstr = String(Math.abs(timezoneHours));
		var hms_arr = hmstr.split('.');
		var h = parseInt(hms_arr[0]);
		var m = 0;
		if (hms_arr.length > 1) {
			m = parseInt(hms_arr[1]);
		}
		var hs = '0';
		var ms = '00';
		if (h > 0) {
			if (h < 10) {
				hs = '0' + String(h);
			} else {
				hs = String(h);
			}
		}
		if (m > 0) {
			if (m < 10) {
				ms = '0' + String(m);
			} else {
				ms = String(m);
			}
		}
		if (h == 0) {
			timezoneName = 'GMT' + sign + hs;
		} else {
			timezoneName = 'GMT' + sign + hs + ':' + ms;
		}
	}
	return timezoneName;
}

function changeTimeZone() {
	var timezonesetwintitle = i18n_tzselectorWintitle;
	var timezonesetwin = new Ext.Window({
		id: 'extwin-timezoneset-win',
		title: timezonesetwintitle,
		width: 780,
		height: 550,
		layout: 'fit',
		//buttonAlign: 'center',
		modal: true,
		items: [{
			header: false,
			border: false,
			//bodyStyle: 'padding: 4px',
			autoScroll: false,
			autoLoad: {
				url: 'timeZoneSelection.action',
				timeout: 120,
				scripts: true, 
				nocache: true
			}
		}]
	});
	timezonesetwin.show();
}

function getTimeZoneStr() {
	var tzName = getTimeZoneName();
	var pos = tzName.indexOf("(");
	var pos2 = tzName.indexOf(")", pos);
	if (pos >= 0) {
		if (pos2 > 0) {
			tzName = tzName.substring(pos+1,pos2);
		} else {
			tzName = tzName.substring(pos+1);
		}
	}
	return tzName;
}

function getCurrentDateTime() {
	var nowDate = new Date();
	defaultTZOffset = nowDate.getTimezoneOffset() * -60000;	// 本机时区时差
	nowDate.setTime(nowDate.getTime() - defaultTZOffset + timeZoneOffset);
	return nowDate;
}

function showCurrentDate() {
	if (lang == 'en') {
		if (isCTEDomain()) {
			dspDateFormat = 'j M, Y';
			dateFeildFormat = 'd/m/Y'
		} else {
			dspDateFormat = 'M j, Y';
			dateFeildFormat = 'm/d/Y'
		}
	} else {
		dspDateFormat = 'Y-m-d';
		dateFeildFormat = 'Y-m-d';
	}
	
	currentDate = getCurrentDateTime();
	dateFieldInitValue = currentDate.format(dateFeildFormat);
	homeTopleftDateDsp = currentDate.format(dspDateFormat);
	
	_timezoneName = getTimeZoneName();
	
	var htoolbarTextLeft_el = document.getElementById("htoolbarTextLeft");
	if (htoolbarTextLeft_el) {
		htoolbarTextLeft_el.innerHTML = homeTopleftDateDsp;
	}
	if (_lefttbar_tzSelector) {
		_lefttbar_tzSelector.setText(_timezoneName);
	}
}


function showLoginUser() {
	document.getElementById("htoolbarTextRight").innerHTML=authuser_info.memcode;
}

function allexceptionErrorText(errcode) {
	return i18n_allexception_errtext.replace("{0}",errcode);
}

function open_extWin(urlstr, wtitle, wwidth, itemId, extraHeight, wtools) {
	var wintools = wtools;
	if (!wtools) {
		wintools = [{
			id: 'help',
			qtip: i18n_qtip_help,
			handler: function(){
				Ext.Msg.alert('Message', 'The Help tool was clicked.');
			}
		}];
	}
	
	var extwin_width = wwidth;
	var extwin_height = 130;
	var extwin_x = (document.body.clientWidth - extwin_width)/2;
	var extwin_y = 40;
	if (itemId == 'flashphoneContent') {
		extwin_y = 100;
		extwin_x = 20;
	}
	var newwininstance = new Ext.Window({
		id: 'newetone-extwin',
		title: wtitle,
		x: extwin_x,
		y: extwin_y,
		width: extwin_width,
		//resizable: false,
		layout: 'fit',
		buttonAlign: 'center',
		modal: true,
		items: [{
			header: false,
			border: false,
			//bodyStyle: 'padding: 5px',
			//autoScroll: true,
			autoLoad: {
				url: urlstr, 
				timeout: 120,	// second
				scripts: true,
				nocache: true,
				callback: function(el, success, response, options) {
					var elheight = el.getHeight();
					if (itemId) {
						var itemidEl = Ext.getCmp(itemId);
						if (itemidEl) {
							elheight = itemidEl.getInnerHeight() + extraHeight + 5;
						} else {
							itemidEl = Ext.get(itemId);
							if (itemidEl) {
								elheight = itemidEl.getHeight() + extraHeight + 5;
							}
						}
					}
					if (elheight < 100) {
						elheight = 100;
					} else {
						extwin_height = elheight + 30;
					}
					el.setHeight(elheight);
					if (Ext.getCmp('newetone-extwin-btclose')) {
						Ext.getCmp('newetone-extwin-btclose').enable();
					}
					
					newwininstance.setSize(extwin_width,extwin_height);
				}
			}
		}],
		//tools: wintools,
		buttons: [{
			id: 'newetone-extwin-btclose',
			text: i18n_popwin_btntext_close,
			disabled: true,
			handler: function(){
				newwininstance.destroy();
			}
		}]
	});
	
	newwininstance.setPosition(extwin_x,extwin_y);
	newwininstance.show();
}

function open_PlayerWin(urlstr) {
	var player_url = webrootpath + '/player_ie.jsp?file=';
	var newwininstance = new Ext.Window({
		id: 'newetone-playerwin',
		title: i18n_brandname + '&nbsp;Player',
		width: 400,
		height: 115,
		x: (document.body.clientWidth - 400)/2,
		y: 50,
		layout: 'fit',
		buttonAlign: 'center',
		modal: true,
		items: [{
			header: false,
			border: false,
			autoLoad: {
				url: player_url + urlstr,
				timeout: 120,
				scripts: true, 
				nocache: true
			}
		}],
		buttons: [{
			text: i18n_popwin_btntext_close,
			handler: function(){
				newwininstance.destroy();
			}
		}]
	});
	
	newwininstance.show();
}

function show_accessnumber() {
	var wintools = [{
		id: 'save',
		qtip: i18n_accnumwin_qtip_download,
		handler: function(){
			if (issuecountry == 'GB') {
				if (isCTEDomain()) {
					window.open("downloads/et_accessnumber_pdf_gb.zip");
				} else {
					window.open("downloads/accessnumber_pdf_gb.zip");
				}
			} else {
				if (isCTEDomain()) {
					window.open("downloads/et_accessnumber_pdf_us.zip");
				} else {
					window.open("downloads/accessnumber_pdf_us.zip");
				}
			}
		}
	}];
	var openwintitle = i18n_accessnumber_wintitle;
	open_extWin('AccessNumber.action', openwintitle, 650, 'extwin-accessnumber', 5, wintools);
}

function rates_query() {
	open_extWin('Rates.action', i18n_ratesquery_wintitle, 700, 'extwin-ratesquery', 5, null);
}

function show_support() {
	var url = 'support.action';
	open_extWin(url, i18n_support_title, 720, 'extwin-content-contactus', 5, null);
}

function show_callcenter() {
	var url = 'callcenterEntry.action';
	var callcenter_wintitle = i18n_callcenterWintitle;
	if (isCTEDomain()) {
		callcenter_wintitle = i18n_callcenterWintitle_cte;
	}
	open_extWin(url, callcenter_wintitle, 735, 'extwin-content-callcenter', 20, null);
}

function gotohome() {
	if (_pageContentPanel) {
		_pageContentPanel.load({url:'Home.action',timeout:120,scripts:true,nocache:true});
	} else {
		window.location.reload();
	}
}

function logout_timeout() {
	islogin = 'N';
	
	authuser_info.memid = '';
	authuser_info.memcode = '',
	authuser_info.mememail = '',
	authuser_info.softphoneId = '',
	authuser_info.memtype = '';
	
	pinliststr = '';
	
	service = '';
	
	pininfo.productname =  '';
	pininfo.pin = '';
	pininfo.pintype = '';
	pininfo.pin = '';
	pininfo.balance = '';
	pininfo.pinstatus = '';
	pininfo.expiration = '';
	pininfo.buydate = '';
	pininfo.faxprefix = '';
	pininfo.faxid = '';
	pininfo.faxstatus = '';
	pininfo.followmeprefix = '';
	pininfo.followmeid = '';
	pininfo.followmestatus = '';
	pininfo.confprefix = '';
	pininfo.confid = '';
	pininfo.confstatus = '';
	pininfo.p2pprefix = '';
	pininfo.pc2phoneid = '';
	pininfo.pc2phonestatus = '';
	pininfo.pbxprefix = '';
	pininfo.pbxid = '';
	pininfo.pbxstatus = '';
	pininfo.pinstatusdsp = '';
	pininfo.currency = '';
	pininfo.w8bprefix = '';
	pininfo.w8bid = '';
	pininfo.w8bstatus = '';
	
	canchangeani = 'n';
	canviewcdr = 'n';
	canrecharge = 'n';
	cantransfer = 'n';
	servicerights = '';
	
	productId = '';
	
	if (_pageHeadPanel && _pageContentPanel) {
		_pageHeadPanel.load({
			url: 'head.action',
			params: {tzOffset: timeZoneOffset},
			timeout:120,
			scripts: true,
			nocache: true
		});
		
		_pageContentPanel.load({
			url: 'Home.action',
			timeout: 120,
			scripts: true,
			nocache: true
		});
	} else {
		var requestlocale = 'en_US';
		if (lang == 'zh') {
			if (localecountry == 'TW') {
				requestlocale = 'zh_TW';
			} else {
				requestlocale = 'zh_CN';
			}
		}
		var url = window.location.href;
		if (url.indexOf('request_locale=') == -1) {
			var pathname = window.location.pathname;
			if (pathname == '') {
				pathname = '/index.action';
			} else {
				var pos = pathname.lastIndexOf('/');
				pathname = pathname.substring(0, pos + 1) + 'index.action';
			}
			url = window.location.protocol + '//' + window.location.host + pathname + '?request_locale=' + requestlocale;
			window.location = url;
		} else {
			window.location.reload();
		}
	}
}

function showAccountHelp(topic) {
	var name = 'createaccount';
	if (topic && topic.length > 0) {
		name = topic;
	}
	
	var help_url = webrootpath + '/help/';
	if (isCTEDomain()) {
		help_url = help_url + 'et_';
		return;
	}
	if (name == 'click2phone' || name == 'web2phone') {
		help_url = help_url + 'service_guide';
	} else {
		help_url = help_url + 'account_guide';
	}
	if (lang == 'zh') {
		if (localecountry == 'TW') {
			help_url = help_url + '_tw.jsp';
		} else {
			help_url = help_url + '_cn.jsp';
		}
	} else {
		help_url = help_url + '_en.jsp';
	}
	help_url = help_url + '?name=' + name;
	
	var helpwintitle = i18n_helpWintitle;
	var accounthelpwin = new Ext.Window({
		id: 'account-help-win',
		title: helpwintitle,
		width: 600,
		height: 400,
		//x: (document.body.clientWidth - 400)/2,
		//y: 50,
		layout: 'fit',
		buttonAlign: 'center',
		modal: true,
		//autoScroll: true,
		items: [{
			header: false,
			border: false,
			bodyStyle: 'padding: 10px',
			autoScroll: true,
			autoLoad: {
				url: help_url,
				timeout: 120,
				scripts: true, 
				nocache: true
			}
		}],
		buttons: [{
			text: i18n_popwin_btntext_close,
			handler: function(){
				accounthelpwin.destroy();
			}
		}]
	});
	
	accounthelpwin.show();
}

function showWorldDateTime(dt) {
	var dtDspText = '';
	var dspDateFormat2 = dspDateFormat;
	if (dt) {
		var dspdate = new Date();
		if (typeof(dt) == 'string') {
			var dtstr = dt;
			if (dtstr == '') {
				return '';
			}
			
			dtstr = dtstr.replace(/T/i, ' ');
			if (dtstr.indexOf('-') >= 0) {
				dtstr = dtstr.replace(/-/g, '/');
			}
			dspdate = new Date(Date.parse(dtstr));
			
			if (dtstr.indexOf(':') > 0) {
				dspDateFormat2 = dspDateFormat2 + ' H:i:s';
			}
		} else {
			dspdate = dt;
			dspDateFormat2 = dspDateFormat2 + ' H:i:s';
		}
		dspdate.setTime(dspdate.getTime() + timeZoneOffset);
		dtDspText = dspdate.format(dspDateFormat2);
	}
	return dtDspText;
}

function showWorldDate(dt) {
	var dtDspText = '';
	if (dt) {
		var dspdate = new Date();
		if (typeof(dt) == 'string') {
			var dtstr = dt;
			if (dtstr == '') {
				return '';
			}
			
			dtstr = dtstr.replace(/T/i, ' ');
			if (dtstr.indexOf('-') >= 0) {
				dtstr = dtstr.replace(/-/g, '/');
			}
			dspdate = new Date(Date.parse(dtstr));
		} else {
			dspdate = dt;
		}
		dspdate.setTime(dspdate.getTime() + timeZoneOffset);
		dtDspText = dspdate.format(dspDateFormat);
	}
	return dtDspText;
}

function showDateTime(dt) {
	var dtDspText = '';
	var dspDateFormat2 = dspDateFormat;
	if (dt) {
		var dspdate = new Date();
		if (typeof(dt) == 'string') {
			var dtstr = dt;
			if (dtstr == '') {
				return '';
			}
			
			dtstr = dtstr.replace(/T/i, ' ');
			if (dtstr.indexOf('-') >= 0) {
				dtstr = dtstr.replace(/-/g, '/');
			}
			dspdate = new Date(Date.parse(dtstr));
			
			if (dtstr.indexOf(':') > 0) {
				dspDateFormat2 = dspDateFormat2 + ' H:i:s';
			}
		} else {
			dspdate = dt;
			dspDateFormat2 = dspDateFormat2 + ' H:i:s';
		}
		dtDspText = dspdate.format(dspDateFormat2);
	}
	return dtDspText;
}

function setZIPregex(fieldid,selcountry,fieldregexText) {
	var zipcoderegex = /([0-9]{5}((-[0-9]{4})|[0-9]{1,4})?)|([A-Z][A-Z0-9]{1,3}((( |  )[A-Z0-9]{3,4})|([A-Z0-9]{1,5))?)$/;
	var zipcodemaxlength = 10;;
	if (selcountry == 'US') {
		zipcoderegex = /[0-9]{5}(-[0-9]{4})?$/;
	} else if (selcountry == 'GB' ||selcountry == 'UK') {
		zipcoderegex = /[A-Z]([0-9]|[0-9]{2}|[A-Z][0-9]|[A-Z][0-9]{2}|[0-9][A-Z]|[A-Z][0-9][A-Z]) [0-9][A-Z]{2}|GIR 0AA$/;
		zipcodemaxlength = 9;
	} else if (selcountry == 'CA') {
		zipcoderegex = /[A-Z][0-9][A-Z] [0-9][A-Z][0-9]$/;
		zipcodemaxlength = 7;
	} else if (selcountry == 'AU') {
		zipcoderegex = /(ACT|NSW|NT|QLD|SA|TAS|VIC|WA)( |  )[0-9]{4}$/;
		zipcodemaxlength = 9;
	} else if (selcountry == 'DE') {
		zipcoderegex = /[0-9]{5}$/;
		zipcodemaxlength = 5;
	} else if (selcountry == 'CN') {
		zipcoderegex = /[0-9]{6}$/;
		zipcodemaxlength = 6;
	}
	var fieldCmp = Ext.getCmp(fieldid);
	if (fieldCmp) {
		if (fieldregexText && fieldregexText.length > 0) {
			Ext.apply(fieldCmp, {
				regex: zipcoderegex,
				maxLength: zipcodemaxlength,
				regexText: fieldregexText
			});
		} else {
			Ext.apply(fieldCmp, {
				regex: zipcoderegex,
				maxLength: zipcodemaxlength
			});
		}
	}
}

function getProductName(productId) {
	if (productId == 'CUSGL001') {
		return '<s:text name="CUSGL001"/>';
	} else if (productId == 'CUSSS001') {
		return '<s:text name="CUSSS001"/>';
	} else if (productId == 'CUSGL002') {
		return '<s:text name="CUSGL002"/>';
	} else if (productId == 'CCNGL001') {
		return '<s:text name="CCNGL001"/>';
	} else if (productId == 'CGBGL001') {
		return '<s:text name="CGBGL001"/>';
	} else if (productId == 'CNZGL001') {
		return '<s:text name="CNZGL001"/>';
	} else {
		return productId;
	}
}

function getDateValue(dt, isworld) {
	var datevalue = null;
	if (dt) {
		if (typeof(dt) == 'string') {
			var dtstr = dt;
			if (dtstr == '') {
				return null;
			}
			
			dtstr = dtstr.replace(/T/i, ' ');
			if (dtstr.indexOf('-') >= 0) {
				dtstr = dtstr.replace(/-/g, '/');
			}
			datevalue = new Date(Date.parse(dtstr));
		} else {
			datevalue = dt;
		}
		if (datevalue && isworld) {
			datevalue.setTime(datevalue.getTime() + timeZoneOffset);
		}
	}
	return datevalue;
}

function getDaysOfMonth(year, month) {
	var year2 = currentDate.getFullYear();
	if (typeof(year) === 'string') {
		year2 = parseInt(year);
	} else {
		year2 = year;
	}
	var month2 = currentDate.getMonth();
	if (typeof(month) === 'string') {
		month2 = parseInt(month);
		month2 = month2 - 1;
	} else {
		month2 = month;
	}
	var dt = new Date(year2, month2, 1, 0, 0, 0, 0);
	return dt.getDaysInMonth();
}

function getComboMonthList(startmonth, maxmonth) {
	var monthlist_data = [['01']];
	startmonth2 = 0;
	maxmonth2 = 12;
	if (startmonth) {
		if (typeof(startmonth) == 'sring') {
			startmonth2 = parseInt(startmonth);
		} else {
			startmonth2 = startmonth;
		}
		if (startmonth2 < 0) {
			startmonth2 = 0;
		}
	}
	if (maxmonth) {
		if (typeof(maxmonth) == 'sring') {
			maxmonth2 = parseInt(maxmonth);
		} else {
			maxmonth2 = maxmonth;
		}
		if (maxmonth2 > 12) {
			maxmonth2 = 12;
		}
	}
	if (startmonth2 > 0 || maxmonth2 < 12) {
		var itemvalue = 0;
		var arrindex = 0;
		for (var i = startmonth2; i < maxmonth2; i++) {
			itemvalue = String(i+1);
			if ((i+1) < 10) {
				itemvalue = '0' + itemvalue;
			}
			monthlist_data[arrindex] = [itemvalue];
			arrindex ++;
		}
	} else {
		monthlist_data = [['01'], ['02'], ['03'], ['04'], ['05'], ['06'], ['07'], ['08'], ['09'], ['10'], ['11'], ['12']];
	}
	return monthlist_data;
}

function getComboDateList(maxday) {
	var datelist_data = [['01']];
	var maxday2 = 31;
	if (maxday) {
		if (typeof(maxday) == 'string') {
			maxday2 = parseInt(maxday);
		} else {
			maxday2 = maxday;
		}
		if (maxday2 > 31) {
			maxday2 = 31;
		}
	}
	for (var i = 0; i < maxday2; i++) {
		itemvalue = String(i+1);
		if ((i+1) < 10) {
			itemvalue = '0' + itemvalue;
		}
		datelist_data[i] = [itemvalue];
	}
	return datelist_data;
}

function isBeforeToday(dtvalue, flag) {
	// flag: 0: 到天, 1: 到小时, 2: 到分钟; 3: 到秒
	var dt1 = getDateValue(dtvalue, true);
	if (dt1) {
		var dt2 = getCurrentDateTime();
		if (flag === 1) {	//到小时
			dt1.setMinutes(0,0,0);
			dt2.setMinutes(59,59,0)
		} else if (flag === 2) {	//到分钟
			dt1.setSeconds(0,0);
			dt2.setSeconds(59,0);
		} else if (flag === 3) {	//到秒
			dt1.setMilliseconds(0);
			dt2.setMilliseconds(0);
		} else {	//到天
			dt1.setHours(0,0,0,0);
			dt2.setHours(23,59,59,0);
		}
		var rc = compareDateValue(dt1, dt2);
		if (rc < 0) {
			return true;
		} else {
			return false;
		}
	} else {
		return true;
	}
}

function isExpiryDate(dtvalue, flag) {
	// flag: 0: 到天, 1: 到小时, 2: 到分钟; 3: 到秒
	var dt1 = getDateValue(dtvalue, true);
	if (dt1) {
		var dt2 = getCurrentDateTime();
		if (flag === 1) {	//到小时
			dt1.setMinutes(0,0,0);
			dt2.setMinutes(59,59,0)
		} else if (flag === 2) {	//到分钟
			dt1.setSeconds(0,0);
			dt2.setSeconds(59,0);
		} else if (flag === 3) {	//到秒
			dt1.setMilliseconds(0);
			dt2.setMilliseconds(0);
		} else {	//到天
			dt1.setHours(0,0,0,0);
			dt2.setHours(23,59,59,0);
		}
		var rc = compareDateValue(dt1, dt2);
		if (rc < 0) {
			return true;
		} else {
			return false;
		}
	} else {
		return false;
	}
}

function compareDateValue(dt1, dt2) {
	if (dt1 === null && dt2 === null) {
		return 0;
	} else if (dt1 !== null && dt2 === null) {
		return 1;
	} else if (dt1 === null && dt2 !== null) {
		return -1;
	} else {
		dt1.setMilliseconds(0);
		dt2.setMilliseconds(0);
		if (dt1.getTime() < dt2.getTime()) {
			return -1;
		} else if (dt1.getTime() > dt2.getTime()) {
			return 1;
		} else {
			return 0;
		}
	}
}

function setTextFeildRegex(feildcmpid, regex) {
	if (feildcmpid && feildcmpid != '' && regex) {
		var feildCmp = Ext.getCmp(feildcmpid);
		Ext.apply(feildCmp, {regex: regex});
	}
}

function getTransferRingDurationCombaData(maxvalue) {
	var durationlist_data = [['15']];
	var maxvalue2 = 60;
	if (maxvalue) {
		if (typeof(maxvalue) == 'string') {
			maxvalue2 = parseInt(maxvalue);
		} else {
			maxvalue2 = maxvalue;
		}
	}
	if (maxvalue2 >= 15) {
		var j = 0;
		for (var i = 15; i <= maxvalue2; i=i+5) {
			durationlist_data[j] = [String(i)];
			j = j + 1;
		}
	} else {
		durationlist_data = [];
	}
	return durationlist_data;
}

function getSelectedTransferRingDuration(rd1value, rd2value, maxvalue) {
	var sumRingDuration = 0;
	var maxvalue2 = 60;
	if (maxvalue) {
		if (typeof(maxvalue) == 'string') {
			maxvalue2 = parseInt(maxvalue);
		} else {
			maxvalue2 = maxvalue;
		}
	}
	var rd1value2 = 0;
	if (rd1value) {
		if (typeof(rd1value) == 'string') {
			if (rd1value != '') {
				rd1value2 = parseInt(rd1value);
			}
		} else {
			rd1value2 = rd1value;
		}
	}
	var rd2value2 = 0;
	if (rd2value) {
		if (typeof(rd2value) == 'string') {
			if (rd2value != '') {
				rd2value2 = parseInt(rd2value);
			}
		} else {
			rd2value2 = rd2value;
		}
	}
	sumRingDuration = maxvalue2 - (rd1value2 + rd2value2);
	return sumRingDuration;
}

