var KIOS = {

	ratio: 30.126,
	direction: 'SE',
	skkText: null,
	skkRadio: null,
	eurText: null,
	eurRadio: null,

	init: function() {
		KIOS.skkText = document.getElementById('skkText');
		KIOS.skkRadio = document.getElementById('skkRadio');
		KIOS.eurText = document.getElementById('eurText');
		KIOS.eurRadio = document.getElementById('eurRadio');
	},

	changeDirection: function(dir) {
		if (dir == 'SE') {
			KIOS.direction = 'SE';
			if (KIOS.skkRadio.className.charAt(0) != 'a') {
				KIOS.skkRadio.className = 'active ' + KIOS.skkRadio.className;
				KIOS.eurRadio.className = KIOS.eurRadio.className.slice(7);
			}
			if (KIOS.skkText.value == '0.00') {
				KIOS.skkText.value = '';
			}
		} else {
			KIOS.direction = 'ES';
			if (KIOS.eurRadio.className.charAt(0) != 'a') {
				KIOS.eurRadio.className = 'active ' + KIOS.eurRadio.className;
				KIOS.skkRadio.className = KIOS.skkRadio.className.slice(7);
			}
			if (KIOS.eurText.value == '0.00') {
				KIOS.eurText.value = '';
			}
		}
		KIOS.calculate();
	},

	// TODO: ak uz sa v texte nachadza ., tak nedavat znovu

	keyPress: function(e) {
		var key, obj;

		key = e.which == undefined ? e.keyCode : e.which;
		obj = e.target == undefined ? e.srcElement : e.target;
		if (key >= 48 && key <= 57 || key == 46 || key == 44 || key == 8 || key == 0) {
			return true;
		}
		return false;
	},

	keyUp: function(obj) {
			KIOS.validate(obj);
			KIOS.calculate();
	},

	// obj je input
	validate: function(obj) {
		var aaa;
		// vsetky ciarky na bodky
		aaa = obj.value.replace(/,/g, '.');
		// prvu bodku na #
		aaa = aaa.replace(/\./, '#');
		// vsetky bodky vymazeme
		aaa = aaa.replace(/\./g, '');
		// # naspat na bodku
		aaa = aaa.replace('#', '.');
		// zrusime vsetky ostatne znaky
		obj.value = aaa.replace(/[^0-9\.]/g, '');
	},

	calculate: function() {
		if (KIOS.direction == 'SE') {
			KIOS.eurText.value = (KIOS.skkText.value / KIOS.ratio).toFixed(2);
		} else {
			KIOS.skkText.value = (KIOS.eurText.value * KIOS.ratio).toFixed(2);
		}
	},

	clear: function() {
		KIOS.skkText.value = KIOS.eurText.value = '';
	},

	backspace: function() {
		if (KIOS.direction == 'SE') {
			KIOS.skkText.value = KIOS.skkText.value.slice(0, -1);
		} else {
			KIOS.eurText.value = KIOS.eurText.value.slice(0, -1);
		}
		KIOS.calculate();
	},

	append: function(digit) {
		if (KIOS.direction == 'SE') {
			KIOS.skkText.value = KIOS.skkText.value.concat(digit);
			KIOS.validate(KIOS.skkText);
		} else {
			KIOS.eurText.value = KIOS.eurText.value.concat(digit);
			KIOS.validate(KIOS.eurText);
		}
		KIOS.calculate();
	}
};

