jQuery(function(){
	numberInput.init();
});


var toolbox = {
	showFormError: function(id, message) {
		var msg = jQuery('#' + id);
		msg.slideUp(200, function() {
			msg.text(message).slideDown();
		});
	}
};


var numberInput = {
	init: function() {
		jQuery('input.number').keypress(function(e) {
			numberInput.keyPress(e);
		});
	},


	keyPress: function(e) {
		// Ctrl, tab, F5, delete,
		// left, right, home, end
		// backspace, numbers
		var allowed = e.ctrlKey || e.keyCode == 9 || e.keyCode == 116 || e.keyCode == 46 ||
				e.keyCode == 37 || e.keyCode == 39 || e.keyCode == 36 || e.keyCode == 35 ||
				e.which == 8 || e.which >= 48 && e.which <= 57;
		if(!allowed) e.preventDefault();
		return allowed;
	}
};


/*
 * c: tizedesek
 * d: tizedesjel - opcionális
 * t: ezreselválasztó - opcionális
 */
Number.prototype.formatMoney = function(c, d, t) {
	var n = this,
		c = isNaN(c = Math.abs(c)) ? 2 : c,
		d = d == undefined ? ',' : d,
		t = t == undefined ? '.' : t,
		s = n < 0 ? '-' : '',
		i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + '',
		j = (j = i.length) > 3 ? j % 3 : 0;
	return s + (j ? i.substr(0, j) + t : '') + i.substr(j).replace(/(\d{3})(?=\d)/g, '$1' + t) +
			(c ? d + Math.abs(n - i).toFixed(c).slice(2) : '');
};


function dateAdd(date, diff) {
	return new Date(date.substr(0, 4) * 1, date.substr(5, 2) * 1 - 1,
			date.substr(8, 2) * 1 + diff, 0, 0, 0, 0);
}


Array.prototype.keyExists = function(key) {
	return (this.constructor === Array || this.constructor === Object) ? key in this : false;
}


//// shit internet explorer
if(!Array.indexOf) {
	Array.prototype.indexOf = function(obj) {
		for(var i = 0; i < this.length; i++) {
			if(this[i] == obj) return i;
		}
		return -1;
	}
}
