Design = function() {
	return {
		init: function() {
			this.countryEl = Ext.get('top-country');
			if (this.countryEl) {
				this.countryList = Ext.get('country-list-layer');
				this.countryEl.addClassOnOver('top-select-hover');
				this.countryEl.on('click', this.showList, this);
				this.countryEl.unselectable();
			}
			this.currencyEl = Ext.get('top-currency');
			if (this.currencyEl) {
				this.currencyList = Ext.get('currency-list-layer');
				this.currencyEl.addClassOnOver('top-select-hover');
				this.currencyEl.on('click', this.showList, this);
				this.currencyEl.unselectable();
			}
		},
		showList: function(e) {
			this.hideLists();
			var el = Ext.fly(e.getTarget());
			var list, alignEl;
			if (this.countryEl && (e.getTarget().id == this.countryEl.id || e.within(this.countryEl))) {
				list = this.countryList;
				alignEl = this.countryEl;
			} else if (this.currencyEl && (e.getTarget().id == this.currencyEl.id || e.within(this.currencyEl))) {
				list = this.currencyList;
				alignEl = this.currencyEl;
			} else {
				return;
			}
			this.onListShow.defer(1, this);
			list.setStyle('width', '');
			list.setWidth(Math.max(list.getWidth(), alignEl.getWidth()));
			list.alignTo(alignEl, 'tl-bl', [0,-1]);
			list.show();
		},
		onListShow: function() {
			Ext.get(document.body).on('click', this.onDocClick, this);
		},
		hideLists: function() {
			Ext.get(document.body).un('click', this.onDocClick, this);
			if (this.countryList) this.countryList.hide();
			if (this.currencyList) this.currencyList.hide();
		},
		onDocClick: function(e) {
			var el = Ext.fly(e.getTarget());
			if (el.findParent('.top-select-list', 5)) return;
			this.hideLists();
		}
	}
}();
Ext.onReady(Design.init, Design);


String.prototype.repeat = function(l){
	return new Array(l+1).join(this);
};

Array.prototype.inArray = function(value) {
	for (var i = 0; i < this.length; i++) {
	  if (this[i] == value) return true;
	}
	return false;
}
Array.prototype.unique = function() {
	var u = [];
	for (var i = 0; i < this.length; i++) {
	  if (!u.inArray(this[i])) u.push(this[i]);
	}
	return u;
}

function amount_out(v, decimals, thousand) {
	if (typeof v == 'undefined' || v === '') return '';
	if (typeof decimals != 'number') decimals = 2;
	var roundFactor = Math.pow(10, decimals);
  v = (Math.round((v-0)*roundFactor))/roundFactor;
  v = String(v);
  var ps = v.split('.');
  var whole = ps[0];
  var sub = decimals > 0 ? TGSystem.decSep+(ps[1] ? ps[1]+("0").repeat(decimals-ps[1].length) : ("0").repeat(decimals)): '';
  if (thousand !== false && TGSystem.thousandSep) {
	  var r = /(\d+)(\d{3})/;
  	while (r.test(whole)) {
    	  whole = whole.replace(r, '$1' + TGSystem.thousandSep + '$2');
	  }
	}
  v = whole + sub;
  return v;
}
function amount_in(amount) {
  amount = amount + '';
  var re = new RegExp('\\' + TGSystem.thousandSep, 'g');
  amount = amount.replace(re, '');
  var re = new RegExp('\\' + TGSystem.decSep, 'g');
  amount = amount.replace(re, '.');
  amount = amount * 1;
  return amount;
}

handleJSONResponse = function(success, response, config) {
	config = config || {};
  if (success == false) {
    return false;
  }
  try {
	  var json = response.responseText;
	  result = Ext.util.JSON.decode(json);
  } catch(e) {
    return false;
  }
  if (result.success == false && result.errDesc) {
    if (config.silent !== true) {
			alert(result.errDesc);
	  }
    return false;
  }
  return result;
}
