/**

Frédéric Saunier
open-sourcing@tekool.net
http://www.tekool.net/

This program is part of a free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

You can also try : http://www.gnu.org/copyleft/gpl.html

**/

//begin import com.tekool.utils.Relegate;
Relegate = function(){};
Relegate.create = function(t,f)
{
	//Transforme arguments en Array et y applique le `splice`
	var a = new Array();
	for(var i=2; i<arguments.length; i++)
		 a.push(arguments[i]);

	return function()
	{
		//Transforme arguments en Array
		var b = new Array();
		for(var i=0; i<arguments.length; i++)
			 b.push(arguments[i]);

		return f.apply
		(
			t,
			b.concat(a)
		);
	};
}
//end import com.tekool.utils.Relegate;

function Calc(form)
{
	this.tiEuro = form.tiEuro;
	this.btDot = form.btDot;
	this.btClear = form.btClear;
	this.btBackspace = form.btBackspace;
	this.btConvert = form.btConvert;
	
	this.btDot.onclick = Relegate.create(this,this.addDot);
	this.btClear.onclick = Relegate.create(this,this.clear);
	this.btBackspace.onclick = Relegate.create(this,this.backspace);
	this.btConvert.onclick = Relegate.create(this,this.convert);

	//Intégration des boutons du formulaire
	var i;
	for(i=0; i<10; i++)
	{
		this['bt'+i] = form['bt'+i];

		//Firefox passe un objet Event en tant que premier argument sur les observateurs d'évènements 'onclick'
		this['bt'+i].onclick = Relegate.create(this,function(o,i){this.addNumber(i)},i,i);
	}

	//Observateur d'évènement positionné sur le clavier
	form.onkeypress = Relegate.create(this,this.onKeyDown);
	
	//Initialisation des variables de classe
	this.isConverted = true;
}

Calc.prototype.onKeyDown = function()
{
	var code = window.event.keyCode;

	if(code>=48 && code<=57)
	{
		this['bt'+ (code-48)].focus();
		this.addNumber(code-48);
	}
	else
	if(code == 46)
	{
		this.addDot();
		this.btDot.focus();
	}
	else
	if(code == 13)
	{
		this.convert();
		this.btConvert.focus();
	}
	else
	if(code == 8)
	{
		this.backspace();
		this.btBackspace.focus();
	}
	else
	if(code == 99)
	{
		this.clear();
		this.btClear.focus();
	}

	//Évite que l'appuie sur la touche entrée soit pris en compte
	return false;
}

Calc.prototype.addNumber = function(number)
{
	if(this.isConverted)
	{
		this.tiEuro.value = '';
		this.isConverted = false;
	}

	if(this.tiEuro.value.length+1 >=15)
		return;

	if(this.tiEuro.value == '0')
	{
		if(number == 0)
			return;
		else
			this.tiEuro.value = number.toString();
	}
	else
		this.tiEuro.value += number.toString();
}

Calc.prototype.addDot = function()
{
	if(this.isConverted)
	{
		this.isConverted = false;
		this.tiEuro.value = '0';
		return;
	}

	if(this.tiEuro.value.length+1 >=15)
		return;

	if(this.tiEuro.value.indexOf('.') != -1)
		return;
	
	if(this.tiEuro.value == '')
		this.tiEuro.value += '0.';
	else
		this.tiEuro.value += '.';
}

Calc.prototype.clear = function()
{
	this.tiEuro.value = '0';
}

Calc.prototype.backspace = function()
{
	if(this.isConverted)
	{
		this.isConverted = false;
		this.tiEuro.value = '0';
		return;
	}

	if(this.tiEuro.value.length == 1)
		this.tiEuro.value = '0';
	else
		this.tiEuro.value = this.tiEuro.value.substr(0,this.tiEuro.value.length-1);
}

Calc.prototype.convert = function()
{
	if(this.tiEuro.value.substr(this.tiEuro.value.length-1,1) == '.')
		this.tiEuro.value += '00';
	
	this.tiEuro.value = this.formatCurrency(this.tiEuro.value);
		
	this.isConverted = true;
	this.onConvert(parseFloat(this.tiEuro.value));
}

/*
 * Formate une valeur monétaire pour en améliorer la présentation
 */
Calc.prototype.formatCurrency = function(value)
{
	var arr = value.toString().split('.');
	var intPart = arr[0];
	
	if(arr[1] != null && parseInt(arr[1]))
	{
		var floatPart = arr[1];

		//Le nombre à virgule présenté aura toujours au moins 2 chiffres aprés la virgule
		if(floatPart.length == 1)
			floatPart = floatPart + '0';
		else
		{
			var i;
			var trim = null;
			for(i=floatPart.length-1; i>1; i--)
				if( floatPart.substr(i,1) == '0')
					trim = i;
				else
					break;

			if(trim != null)
				floatPart = floatPart.substr(0,trim);
		}
		return intPart + '.' + floatPart;
	}
	else
		return intPart;
}

/*
 * Retourne la valeur actuellement affichée
 */
Calc.prototype.getValue = function()
{
	return parseFloat(this.tiEuro.value);
}

/*
 * Affecte la valeur passée à la valeur portée par la calculatrice
 */
Calc.prototype.setValue = function(value)
{
	var nValueIn = parseFloat(String(value));
	this.tiEuro.value = nValueIn.toString();
}

Calc.prototype.onConvert = function(value){};