function Currency() { // usd defaulted
	this.currencySimbolLeft = '$';
	this.currencySimbolRight = '';
	this.currencyDecimalPoint = '.';
	this.currencyThousandsPoint = ',';
	this.currencyDecimalPlaces = '2';
}
Currency.prototype.init = currencyInit;
Currency.prototype.setCurrency = currencySetCurrency;
Currency.prototype.display = currencyDisplay;
Currency.prototype.getValue = currencyGetValue;

function currencySetCurrency(xmlHttp) {
	var xmlDoc = xmlHttp.responseXML.documentElement;
	var node = xmlDoc.firstChild;
	while(node!=null) {
		var action = '';
		switch(node.nodeName) {
		case 'simbolleft':
			action = 'currencySimbolLeft';
			break;
		case 'simbolright':
			action = 'currencySimbolRight';
			break;
		case 'decimalpoint':
			action = 'currencyDecimalPoint';
			break;
		case 'thousandspoint':
			action = 'currencyThousandsPoint';
			break;
		case 'decimalplaces':
			action = 'currencyDecimalPlaces';
			break;
		default:
			break;
		}
		if(action!='') {
			var newValue = '';
			if(node.firstChild!=null)
				newValue = node.firstChild.nodeValue;
			eval('this.'+action+'="'+newValue+'";');
		}
		node = node.nextSibling;
	}
}

function currencyInit() {
	var page = '/water-cooling/includes/query/currency.php';
	var xmlHttp = getXmlHttpObject();
	if( xmlHttp != null ) {
		var thisMem = this;
		setXmlHttpOnload(xmlHttp, function(x) { thisMem.setCurrency(x); });
		xmlHttp.open("GET",page,true);
		xmlHttp.send(null);
	} else {
		alert( 'Error loading data from server.' );
	}
}

function currencyDisplay(price) {
	var number = '' + parseInt(price, 10);
	var decimal = price - number;
	var display = '';

	while(number) {
		index = number.length - 3;
		if(index < 0) index = 0;
		part = number.substr(index, number.length);
		number = number.substr(0, index);
		display = part + display;
		if(number) display = this.currencyThousandsPoint + display;
	}

	if(this.currencyDecimalPlaces>0) {
		decimal *= Math.pow(10, this.currencyDecimalPlaces);
		decimal = String(Math.round(decimal));
	}
	while(decimal.length<this.currencyDecimalPlaces)
		decimal = '0' + decimal;
	display = this.currencySimbolLeft + display;
	if(this.currencyDecimalPlaces>0)
		display += this.currencyDecimalPoint + "" + decimal;
	display += this.currencySimbolRight;

	return display;  
}


function currencyGetValue(p) {
	var left = (p.charAt(0)=='&') ? p.indexOf(';')+1 : (this.currencySimbolLeft) ? 1 : 0;
	var right = (this.currencySimbolRight.charAt(0)=='&' && p.indexOf('&', 1)==-1) ? 1 : this.currencySimbolRight.length;
	var noSimbols = p.substring(left, p.length - right);
	var values = noSimbols.split(this.currencyThousandsPoint);
	var v = 0;
	for( i=0; i<values.length-1; ++i) {
		v += parseInt(values[i], 10);
		v *= 1000;
	}
	if( !this.currencyDecimalPoint || values[i].indexOf(this.currencyDecimalPoint)==-1) {
		v += parseInt(values[i], 10);
	} else {
		values = values[i].split(this.currencyDecimalPoint);
		v += parseInt(values[0], 10);
		if( values.length>1) {
			var decimal = parseInt(values[1], 10);
			for( i=0; i<this.currencyDecimalPlaces; ++i) {
				decimal /= 10;
			}
			v += decimal;
		}
	}
	return v;
}

var currency = new Currency();
currency.init();

