<!-- Original:  Cyanide_7 (leo7278@hotmail.com) -->
<!-- Web Site:  http://www7.ewebcity.com/cyanide7 -->

<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->

<!-- Begin
CUR_TYPE={
	DOLAR:{
		clearregxp:/\$|\,/g,
		replace:null,
		thousand: ',',
		ratio:'.',
		prefix: '$',
		suffix: ''
	},
	EURO:{
		clearregxp:/(Є)|\,/g,
		replace:null,
		thousand: ',',
		ratio:'.',
		prefix: 'Є',
		suffix: ''
	},
	TRL:{
		clearregxp:/(YTL)|\./g,
		replace:[/\,/g,'.'],
		thousand: '.',
		ratio:',',
		prefix: '',
		suffix: ''
	}
}

function formatCurrency(num,curtype,prevtype) {
	if (!curtype) curtype=CUR_TYPE.TRL;
	if (!prevtype) prevtype=curtype;

	num = num.toString().replace(prevtype.clearregxp,'');
	if (prevtype.replace ) {
		num = num.replace(prevtype.replace[0],prevtype.replace[1]);
	}
	if(isNaN(num)) num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10) cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++){
		num = num.substring(0,num.length-(4*i+3))+curtype.thousand+num.substring(num.length-(4*i+3));
	}
	return (((sign)?'':'-') + curtype.prefix + num +curtype.ratio + cents + curtype.suffix);
}
//  End -->

function trackPriceField(price,priceType) {

	this.price=price;
	this.priceType=priceType;
	this.prevPriceType=priceType.selectedIndex;

	this.priceTypes=[
		CUR_TYPE.TRL,
		CUR_TYPE.DOLAR,
		CUR_TYPE.EURO
	];

	this.replaceTimer=null;


	this.keyup=function(){
		clearTimeout(this.replaceTimer);
		this.replaceTimer=setTimeout(this.change.bind(this),800);
	}

	this.focusEvent=function(){
		var prevtype=this.priceTypes[this.priceType.selectedIndex];
		if (prevtype.clearregxp ) {
			this.price.value= this.price.value.replace(prevtype.clearregxp,'');
		}
	}

	this.change=function(){
		if (this.price.value && this.price.value!=""){
			this.price.value=formatCurrency(this.price.value,this.priceTypes[this.priceType.selectedIndex]);
		}
	}

	this.change2=function(){
		if (this.price.value && this.price.value!=""){
			this.price.value=formatCurrency(this.price.value,this.priceTypes[this.priceType.selectedIndex],this.priceTypes[this.prevPriceType]);
		}
		this.prevPriceType=this.priceType.selectedIndex;
	}

//	Event.observe(this.price, 'keyup', this.keyup.bind(this));
	Event.observe(this.price, 'focus', this.focusEvent.bind(this));
	Event.observe(this.price, 'blur', this.change.bind(this));
	Event.observe(this.price, 'change', this.change.bind(this));
	Event.observe(this.priceType, 'change', this.change2.bind(this));

	this.change();
}
