function show_warning(method){
    var warning = payment_systems[method]['warning'];
    $('span.warning').html( warning );
}

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] > fixed_discount) ? table[volume]: fixed_discount;
          return (cd > d1) ? cd : d1;
      }
  }
  return (cd > fixed_discount) ? cd : fixed_discount;
}

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("");
  }
	
	$('div.wrapp-sales table tr td').removeClass('active_dsc');
	if (discount > 0){
	   //$("span#dsc").html("Скидка: <b>" + discount + "%</b><br>");
       $('div.wrapp-sales table tr td:contains("'+discount+'%")').addClass('active_dsc');
       $("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) && total != 0 ){
	   $("span#total").html("Итого с учетом скидки: <b>" + total + " "+ unit +"</b><br>");
	   $("p#total-summ").html('Итого: ' + total + " "+ unit);
	} else {
	   $("span#total").html("");
	   /* $("p#total-summ").html('Итого: ' + 0 + " "+ unit); */
	   $("p#total-summ").html('');
  }
	
  
  $("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 (fixed_discount > 0) {
    //$("span#user-discount").show();
    //$("span#user-discount").html("Фиксированная скидка: <b>" + fixed_discount + "%</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;
      if (product_type == 'GOLD') {
          fixed_discount = json.g_dsc;
      } else if ( product_type == 'KEY'){
          fixed_discount = json.k_dsc;          
      }
      update_vd();
      recount();
    });
  }else {
    v = 0;
    u = 0;
    fixed_discount = 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());
  });
}


function update_cd(){
  $("span#coupon-discount").html("Предоставлен купон со скидкой <b>" + cd + "%</b>");
}

function clean_cd(){
  cd = 0;
  $("span#coupon-discount").html('');
}

function is_valid_code(code) { 
  var re = /^\d\d\d\d-\d\d\d\d-\d\d\d\d-\d\d\d\d$/; 
  return code.match(re); 
}

function cinfo(code){
  if (is_valid_code(code)) {
    $.getJSON('/dyn/cinfo/?c='+ code, function(json) {
      if (product_type == 'GOLD') {
          cd = json.gold_discount;
      } else if ( product_type == 'KEY'){
          cd = json.key_discount;          
      }
      if (cd > 0 ){
        update_cd();
        recount();
      } else {
        clean_cd();
        recount();
      }
    });
  } else {
      clean_cd();
      recount();
  }
}



function init_cd(){
  cinfo( $('input#id_coupon_code').val()  );
  $('input#id_coupon_code').bind("change keyup blur", function(){
     cinfo($(this).val());
  });
}

$(document).ready( function () {
    $(".tab-comments").click(function() {
        $(".tab-product span").addClass("second");
        $(".tab-comments span").removeClass("second");
        $(".tab-product-info").hide();
        $(".tab-comments-info").show();
    });

    $(".tab-product").click(function() {
        $(".tab-comments span").addClass("second");
        $(".tab-product span").removeClass("second");
        $(".tab-product-info").show();
        $(".tab-comments-info").hide();
    });
});
