$(document).ready(function() {
  $('#vklad-calculator').submit(function(e) {
    e.preventDefault();
    var url = '/service/fizface/deposits/comparison/index.php?';

    var $form = $(this);
    $.ajax({
      type: 'POST',
      url: '/ajax/deposits.php',
      data: $form.serialize(),
      beforeSend: function() {
        $('#vklad-calculator input, #vklad-calculator select').attr('disabled', 'disabled');
        $form.append('<div class="throbber"> </div>');
        $('.calc-result').fadeOut();
        $('.sravnit a').attr('href', url);
      },

      complete: function() {
        var element = document.getElementById('extra-date'),
            options = {};

        if (element) {
          options = element.onclick instanceof Function ? element.onclick() : {};
          element.onclick = null;
          element.removeAttribute('onclick');
        }

        $('#vklad-calculator input, #vklad-calculator select').removeAttr('disabled');
        $('.throbber', $form).remove();

        $('#extra-date-calculate').click(function() {
          var $date = $('#extra-date'),
              rate = $date.attr('rel'),
              summ = $date.attr('data'),
              period = $date.val(),
              profit = 0;
          
          period = intval(period, 10);

          if (options.minDate <= period && period <= options.maxDate) {

            if (options.capitalization) {
              var n = Math.floor(period/30);
              profit = summ * Math.pow((1+ rate * (period / n)/ 365 / 100), n);
              profit -= summ;
            } else {
              profit = ((summ * rate * period)/365) / 100;
            } 
            //var profit = ((summ * rate * period)/365) / 100;
            profit = intval(profit, 10).toString();
            profit = profit.replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1 ");
            profit = profit + ' '+ $date.attr('curr'),
              $('#extra-info-profit').text(profit);
          } else {
            $date.focus();
            if (options.minDate == options.maxDate) {
              alert('Количество дней должно быть ' + options.minDate +' дней!');
            } else {
              alert('Количество дней должно быть в диапазоне ' + options.minDate +' - ' + options.maxDate + ' дней!');
            }
          }
        });
      },

      success: function( data ) {
        var sum      = $('#vklad-sum').val(),
          currency = $('#vklad-currency').val(),
          period   = $('.srok').val();

        var url_ = url + 'sum='+ sum+ '&currency='+ currency+ '&period='+  period;

        $('.sravnit a').attr('href', url_);

        $('#calc-resultaten').html(data);
        $('.calc-result').fadeIn();
      }

    });
  });

});

function intval(mixed_var, base) {
  var type = typeof(mixed_var);

  if (type === 'boolean') {
    return (mixed_var) ? 1 : 0;
  } else if (type === 'string') {
    tmp = parseInt(mixed_var, base || 10);
    return (isNaN(tmp) || !isFinite(tmp)) ? 0 : tmp;
  } else if (type === 'number' && isFinite(mixed_var)) {
    return Math.floor(mixed_var);
  } else {
    return 0;
  }
}
