/*
-----------------------------------------------
Currency Script [c] mojdenar.com 2001
language=JavaScript1.1
-----------------------------------------------
*/

String.prototype.reverse = function() {
	var sRev = new String();
	var iCount = this.length;
	for (var iCount = this.length - 1; iCount >= 0; iCount--) {
		sRev += this.charAt(iCount);
	}
	return sRev
}

function Currency(sCurrency) {
	if (typeof sCurrency == 'string') {
		this.value = parseFloat(sCurrency.replace(/[^\d\,]/ig,'').replace(/\,/,'.'));
	}
	else // typeof rValue = 'float' or 'integer'
		this.value = sCurrency;
}

Currency.prototype.valueOf = function() {return this.value;}
Currency.prototype.format = '#.##0,00';
Currency.prototype.toString = function() {	
	if (isNaN(this.value))
		return 'NaN';
	else if (typeof this.format == 'string') {
		var srTail = new String();
		var srHead = new String();
		var sTail, sHead;
		var sfHead, sfTail;
		var sVrednost;
		var re = /([^#0\.\,]*)([#\.0]*)(\,{0,1})([#0]*)([^#0]*)/i;
		//var re = /([^#0\,\.]*)([#\,0]*)(\.{0,1})([#0]*)([^#0]*)/i;				
		var reRes = this.format.match(re);
		sfHead = reRes[2]; // glava formata (#,##0 <- #,##0.00)
		sfTail = reRes[4]; // rep formata (00 <- #,##0.00)	

		if (sfTail != '')  // rep obstaja <=> decimalna mesta obstajajo!
			{
			sTail = new String(Math.round(this.value * Math.pow(10,sfTail.length))/Math.pow(10,sfTail.length));
			// sTail - Pol formatirana vrednost -> 12345.6789 <=> 12345.68, brez razmejitvenih pik (vejic), vendar na pravo število decimalk
			
			if (sTail.search(/\./i) >= 0) // 12345.34
				{ 				
				sHead = sTail.replace(/\..*/i,'');  
				sTail = sTail.replace(/.*\./i,'');
				}
			else 
				{
				sHead = sTail;
				sTail = '';
				}
			while (sfTail != '') {
				if (sTail != '') {
					srTail += sTail.charAt(0);
					sTail = sTail.slice(1);
				}
				else if (sfTail.charAt(0) == '0') {
					srTail += '0';
				}
				sfTail = sfTail.slice(1);
			}
		}
		else {
			sHead = new String(Math.round(this.value));
		}

		// - Do sem dobimo pravilno formatirano število, ki jo razume JavaScript
		// - ločeno na Glavo in rep		
		var bThou = false;
		var iCount = 0;
		if (sfHead.search(/./i) >= 0)  // Ali naj vpišem razmejitveni znak za tisočice?
			{
			bThou = true;
			sfHead = sfHead.replace(/,/i,''); 
			}		
		sfHead = sfHead.reverse();
		sHead = sHead.reverse();		
		while (sfHead != '') 
		{
			if (sfHead.charAt(0) == '0') {
				iCount++;
				if (iCount == 4 && bThou) {
					srHead = '.' + srHead; //srHead = ',' + srHead;
					iCount = 1;
				}
				if (sHead != '') {
					srHead = sHead.charAt(0) + srHead;
					sHead = sHead.slice(1);
				}
				else
					srHead = '0' + srHead;
				sfHead = sfHead.slice(1);
			}
			else 
				{
				while (sHead != '') 
					{
					iCount++;
					if (iCount == 4 && bThou) {
						srHead = '.' + srHead; //srHead = ',' + srHead;
						iCount = 1;
					}
					srHead = sHead.charAt(0) + srHead;
					sHead = sHead.slice(1);
				}
				sfHead = '';
			}
		}
		//return reRes[1] + srHead + reRes[3] + srTail + reRes[5];		
		return reRes[1] + srHead + ',' + srTail + reRes[5];
	}
	else
		return this.value;
}

