/* Copyright (c) 2007 Onlyweb Studio | http://www.onlyweb.ru/ */
/*


From Russia with love!

*/

var newClass = function() {
	return function() {
		return this.init.apply(this, arguments);
	}
}


var Investment = newClass();
Investment.prototype = {
  init: function() {
     var t = this;
     t.flags = new Object();
     t.flags = {"invest": false, "attend": false, "profit": false};
     t.proverka();
     //$('#Investment input.btn').attr('disabled','disabled').css('color','#999999');
     $('#Investment input:not(.btn)').bind('keyup', function(){t.replaseSb(this);}).bind('blur', function(){t.replaseSb(this);});
     $('#Investment').submit(function(e){e.preventDefault(); e.stopPropagation(); t.submit();});
  },

  submit: function() {
    var invest = $('#invest').val()*1;
    var attend = $('#attend').val()*1;
    var profit = $('#profit').val()*1;

    var k = invest / (profit * attend*20*0.01);
    k = +k.toFixed(1);

    if(k < 12) {
        var clients = (attend*20*0.01).toFixed(0)

        var clientsCount = 0;
        if(clients >= 20 || 10 > clients)
          clientsCount = clients[clients.length-1];

        switch(clientsCount) {
          case '1':
            $('span.Clients').text('клиент');
            $('span.ClientsStr').text('станет клиентом');
            break;

          case '2':
          case '3':
          case '4':
            $('span.Clients').text('клиента');
            $('span.ClientsStr').text('станут клиентами');
            break;

          default:
            $('span.Clients').text('клиентов');
            $('span.ClientsStr').text('станут клиентами');
            break;
        }

        var people = attend*20;

        $('span.VizitsID').text(people);
        $('span.ClientsID').text(clients);

        $('div.Kruzhok').text(k);
        $('div.InvestmentDoneMoreYear').hide();
        $('div.InvestmentDone').slideDown(300, function(){ $('div.WhyID').show(200); });
    } else {
        $('div.InvestmentDone').hide();
        $('div.InvestmentDoneMoreYear').slideDown(300);
    }


    $('#Investment input.btn').val('Пересчитать');
  },

  replaseSb: function(obj) {
    var t = this;
    $(obj).val($(obj).val().replace(/[^0-9]+/g, ''));
    t.proverka(obj);
  },

  proverka: function(obj) {
    var t = this;
    if(obj) {
      if($(obj).val().length > 0) {
        t.flags[$(obj).attr('id')] = true;
      } else {
        t.flags[$(obj).attr('id')] = false;
      }
    } else {
      $.each($('#Investment input:not(.btn)'), function(i, val){
        if($(val).val().length > 0) {
            t.flags[$(val).attr('id')] = true;
        } else {
            t.flags[$(val).attr('id')] = false;
        }
      });
    }
    t.checkBtn();
  },

  checkBtn: function() {
    var t = this;
    var initBtn = true;
    $.each(t.flags, function(i, val){
      if(val == false) {
        initBtn = false
      }
    });
    if(initBtn) {
        $('#Investment input.btn').removeAttr('disabled').css('color','#000000');
    } else {
        $('#Investment input.btn').attr('disabled','disabled').css('color','#999999');
    }
  }
}