function get_discount(amount, currency_code){
  if (isNaN(amount)){
      return 0;
  }

  if (currency_code == 'RUR') {
      amount = v + amount;
      table = discounts;
  } else if (currency_code == 'USD') {
      amount = u + amount;
      table = usd_discounts;
  }
  for (var volume in table) {
      if (amount >= volume) {
          //return (table[volume] > d) ? table[volume]: d;
          d1 = (table[volume] > d) ? table[volume]: d;
          return (cd > d1) ? cd : d1;
      }
  }
  return (cd > d) ? cd : d;
}

function set_amount(amount){
  amount = parseFloat(amount).toFixed(2);
  if (!isNaN(amount)){
    $('input#id_amount').val(amount);
  } else {
    $('input#id_amount').val('');
  }
}

function show_result(raw, discount, unit, must_set_amount){
  must_set_amount = typeof(must_set_amount) != 'undefined' ? must_set_amount : false;
  raw = parseFloat(raw).toFixed(2);
  
  if (!isNaN(raw)){
	   $("span#raw").html("Стоимость: <b>{raw} {unit}</b><br>".supplant({'raw' : raw, 'unit' : unit}));
	} else {
	   $("span#raw").html("");
  }
	
	if (discount > 0){
	   //$("span#dsc").html("Скидка: <b>" + discount + "%</b><br>");
       
       $("span#dsc").html("Скидка: <b>{discount}% ({discount_amount} {unit})</b><br>".supplant({'discount' : discount, 'unit' : unit, 'discount_amount' : parseFloat(raw* discount/100).toFixed(2) }));
	} else {
	   $("span#dsc").html("");
  }
	
	var total = raw - raw* discount/100;
	total = parseFloat(total).toFixed(2);
	
	if (!isNaN(total)){
	   $("span#total").html("Итого с учетом скидки: <b>" + total + " "+ unit +"</b><br>");
	   $("span#total-summ").html(total + " "+ unit);
	} else {
	   $("span#total").html("");
	   $("span#total-summ").html(0 + " "+ unit);
  }
	
  
  $("span#amount-unit").html(unit);
  

  
  if (must_set_amount == true) {
      set_amount(total);
  }
}

function update_vd(){
  if (v > 0) {
    //$("span#user-volume").show();
    //$("span#user-volume").html("Объем клиента: <b>" + parseFloat(v).toFixed(2) + "руб. ("+parseFloat(u).toFixed(2)+"$)</b>");
  } else {
    $("span#user-volume").html("");
    $("span#user-volume").hide();
  }
  if (fd > 0) {
    $("span#user-discount").show();
    $("span#user-discount").html("Накопительная скидка: <b>" + fd + "%</b>");
  } else {
    $("span#user-discount").html("");
    $("span#user-discount").hide();
  }
}

function isEmail(email) { 
  var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; 
  return email.match(re); 
}

function uinfo(email){
  if (isEmail(email)) {
    $.getJSON('/dyn/uinfo/?e='+ email, function(json) {
      v = json.v;
      u = json.u;
      d = json.d;
      fd = json.fd;
      update_vd();
      recount();
    });
  }else {
    v = 0;
    u = 0;
    d = 0;
    fd = 0;
    update_vd();
    recount();
  }
}

function init_vd(){
  //update_vd();
  uinfo( $('input#id_email').val()  );
  $('input#id_email').bind("change keyup blur", function(){
     uinfo($(this).val());
  });
}

