/*
*   JS Functions
*/
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 isFloat(num){
  return num%Math.floor(num);
}



/*
*   jQuery Functions
*/
jQuery.fn.ForceNumericOnly =
function()
{
    return this.each(function()
    {
        $(this).keydown(function(e)
        {
            var key = e.charCode || e.keyCode || 0;
            // allow backspace, tab, delete, pageup, pagedown, home, end, arrows, numbers and keypad numbers ONLY
            return (
                key == 8 ||
                key == 9 ||
                key == 46 ||
                (key >= 33 && key <= 40) ||
                (key >= 48 && key <= 57) ||
                (key >= 96 && key <= 105));
        })
    })
};

jQuery.fn.ForceNumericOnlyFloat =
function()
{
    return this.each(function()
    {
        $(this).keydown(function(e)
        {
            var key = e.charCode || e.keyCode || 0;
            // allow backspace, tab, delete, pageup, pagedown, home, end, arrows, numbers and keypad numbers ONLY
            return (
                key == 188 ||   // comma ,
                key == 190 ||   // normal .
                key == 110 ||   // keypad .
                key == 8 ||
                key == 9 ||
                key == 46 ||
                (key >= 33 && key <= 40) ||
                (key >= 48 && key <= 57) ||
                (key >= 96 && key <= 105));
        })
    })
};



/*
*   JS
*/
function cenaZmenaDual () {

  var item   = '#' + $(this).attr('rel'),
      cena_j = $(item).attr('rel'),
      pocet  = $(this).val(),
      suma, sumaDual, cena, cenaDual;

  pocet = pocet.replace(/,/gi, ".");

  if (pocet<0) $(this).val(0).change();


  if (!isNaN(pocet)) {

    pocet  = pocet*1;
    suma   = 0;
    cena   = (pocet.toFixed(1)*cena_j).toFixed(2);
    cena   = cena*1;
    cenaDual = (cena*CURRENCY_RATE).toFixed(2);
    cenaDual = cenaDual*1;

    $(item).html( cena.formatMoney(2, ',', '.') + ' ' + CURRENCY);
    $(item+'_dual').html( '(' + cenaDual.formatMoney(0, ',', '.') + ' ' + CURRENCY_DUAL + ')');

    $(".kus").each(function(index) {
      item_cena_j = $('#' + $(this).attr('rel')).attr('rel');
      item_pocet  = $(this).val().replace(/,/gi, ".");
      item_pocet  = item_pocet*1;
      suma = suma + (item_cena_j*item_pocet);
    });

    suma_bezdph = (suma/DPH_VALUE).toFixed(2);
    suma_bezdph = suma_bezdph*1;

    sumaDual = (suma*CURRENCY_RATE).toFixed(2);
    sumaDual = sumaDual*1;
    sumaDual_bezdph = (sumaDual/DPH_VALUE).toFixed(2);
    sumaDual_bezdph = sumaDual_bezdph*1;

    $('#suma').html('<strong>' + suma.formatMoney(2, ',', '.') + ' ' + CURRENCY + '</strong> (' + suma_bezdph.formatMoney(2, ',', '.') + ' ' + CURRENCY + ' bez DPH)');
    $('#suma_dual').html('<strong>' + sumaDual.formatMoney(0, ',', '.') + ' ' + CURRENCY_DUAL + '</strong> (' + sumaDual_bezdph.formatMoney(0, ',', '.') + ' ' + CURRENCY_DUAL + ' bez DPH)');

    if ( isFloat($(this).val()) )
      $(this).val(pocet.toFixed(1));

    $(this).val( $(this).val().replace(/,/gi, ".") );
  }
}

function cenaZmena () {

  var item   = '#' + $(this).attr('rel'),
      cena_j = $(item).attr('rel'),
      pocet  = $(this).val(),
      suma, cena;

  pocet = pocet.replace(/,/gi, ".");

  if (pocet<0) $(this).val(0).change();


  if (!isNaN(pocet)) {

    pocet  = pocet*1;
    suma   = 0;
    cena   = (pocet.toFixed(1)*cena_j).toFixed(2);
    cena   = cena*1;

    $(item).html( cena.formatMoney(2, ',', '.') + ' ' + CURRENCY);

    $(".kus").each(function(index) {
      item_cena_j = $('#' + $(this).attr('rel')).attr('rel');
      item_pocet  = $(this).val().replace(/,/gi, ".");
      item_pocet  = item_pocet*1;
      suma = suma + (item_cena_j*item_pocet);
    });

    suma_bezdph = (suma/DPH_VALUE).toFixed(2);
    suma_bezdph = suma_bezdph*1;

    $('#suma').html('<strong>' + suma.formatMoney(2, ',', '.') + ' ' + CURRENCY + '</strong> (' + suma_bezdph.formatMoney(2, ',', '.') + ' ' + CURRENCY + ' bez DPH)');

    if ( isFloat($(this).val()) )
      $(this).val(pocet.toFixed(1));

    $(this).val( $(this).val().replace(/,/gi, ".") );
  }
}

function cenaZmenaArr(el,e) {

  var key = e.charCode || e.keyCode || 0,
      val = el.val()*1;

  if (!isNaN(val)) {
    if ( key == 38 )
      el.val( isFloat(val) ? (val + 1).toFixed(1) : val + 1 );

    if ( (key == 40) && (val > 1) )
      el.val( isFloat(val) ? (val - 1).toFixed(1) : val - 1 );
  }

  el.change();
}
