$(document).ready(function () {

	/**
	* Funkcja podczepia do podzielonych pól zdarzenie przechodzenia
	* do kolejnego pola po wpisaniu znaku
	*/
	$('div.merged input').live('keyup', function(event) {
		var code = (event.keyCode ? event.keyCode : event.which);

		// jesli nie backspace to przenosimy fokus do przodu
		if (code != '8') {
			var nextInput = $(this).next();
			// sprawdzamy czy jest nastepny input, do ktorego mozna przejsc
			// oraz czy obecny input został wypełniony
			if (nextInput.length>0 && $(this).val()!='') {
				// przenosimy focus do nastepnego elementu
				$(this).next().focus(function(){$(this).select()});
				$(this).next().focus();
			}
		} else {
			// nacisnieto backspace
			var prevInput = $(this).prev();
			// jesli istnieje poprzedni input
			if (prevInput.length>0) {
				// przenosimy focus do poprzedniego inputa
				$(this).prev().focus(function(){$(this).select()});
				$(this).prev().focus();
			}
		}
	});
	
	
    //ograniczenie wartości w polach liczbowych
    $('.float_field').live('keyup', function(e) {
        var $this = $(this);
        var value = $this.val();
        if (value == '') {
            $this.val(0);
        }
        $this.val(value.replace(',', '.'));
        var newValue = parseFloat(value);
        if (!/^\-?[0-9]*(\.[0-9]*)?$/.test(value)) {
            if (!isNaN(newValue)) {
                if ($this.is('.only_positive')) {
                    newValue = Math.abs(newValue);
                }
                if (newValue > 99.99 && $this.is('.percent_discount_field')) {
                    $this.val(99.99);
                } else {
                    $this.val(newValue);
                }
            } else {
                $this.val(0);
            }
        } else {
            if (!isNaN(newValue)) {
				if ($this.is('.only_positive') && newValue < 0) {
                    newValue = Math.abs(newValue);
					$this.val(newValue);
                }
                if (newValue > 99.99 && $this.is('.percent_discount_field')) {
                    $this.val(99.99);
                }
            }
        }
    });

});
