// calculator
  var json_delivery = {
    usa: {
      ua:   {0:8,  1:8,  2:16, 3:24, 4:32, 5:40, 6:48, 7:56, 8:64, 9:72, 10:80,  15:100, 16:100 },
      ru:   {0:37, 1:37, 2:47, 3:54, 4:61, 5:75, 6:85, 7:92, 8:100, 9:107, 10:118, 11:126, 12:134, 13:143, 14:155, 15:163, 16:168 },
      be:   {0:37, 1:37, 2:47, 3:54, 4:61, 5:75, 6:85, 7:92, 8:100, 9:107, 10:118, 11:126, 12:134, 13:143, 14:155, 15:163, 16:168 },
      othe: {0:37, 1:37, 2:47, 3:54, 4:61, 5:75, 6:85, 7:92, 8:100, 9:107, 10:118, 11:126, 12:134, 13:143, 14:155, 15:163, 16:168 }
    },
    ger: {
      ua: {0:12, 1:12, 2:12, 3:12, 4:12, 5:12, 6:12, 7:12, 8:12, 9:14, 10:15, 15:20, 16:0},
      ru: {0:32, 1:32, 2:32, 3:32, 4:32, 5:32, 6:32, 7:32, 8:32, 9:35, 10:36, 15:40, 16:0},
      be: {0:32, 1:32, 2:32, 3:32, 4:32, 5:32, 6:32, 7:32, 8:32, 9:35, 10:36, 15:40, 16:0},
      othe: {0:32, 1:32, 2:32, 3:32, 4:32, 5:32, 6:32, 7:32, 8:32, 9:35, 10:36, 15:40, 16:0}
    },
    gbp:{
      ua: {0:11.5, 1:11.5, 2:21, 3:49, 4:52, 5:55, 6:58, 7:61, 8:64, 9:67, 10:70, 15:80, 16:0},
      ru: {0:11.5, 1:11.5, 2:21, 3:49, 4:52, 5:55, 6:58, 7:61, 8:64, 9:67, 10:70, 15:80, 16:0},
      be: {0:11.5, 1:11.5, 2:21, 3:49, 4:52, 5:55, 6:58, 7:61, 8:64, 9:67, 10:70, 15:80, 16:0},
      othe: {0:11.5, 1:11.5, 2:21, 3:49, 4:52, 5:55, 6:58, 7:61, 8:64, 9:67, 10:70, 15:80, 16:0}
    }
  };

  var resA = "Это стоимость доставки товара «под ключ» - с учетом уплаты всех таможенных пошлин и иных платежей, предусмотренных таможенным и иным законодательством";
  var resB = "Эта сумма не включает в себя уплату таможенных пошлин, предусмотренных законодательством вашей страны. Чтобы узнать, возможно ли избежать или сократить таможенные платежи, оставьте свою заявку на сайте";
  var resC = "Недостаточно данных для расчета стоимости заказа. Уточните данные или оставьте свою заявку на сайте";
  var resD = "Прямая доставка от продавца в данном случае – нецелесообразна, так как может привести к уплате таможенных пошлин, предусмотренных законодательством вашей страны. Для подсчета альтернативного способа доставки, введите стоимости доставки данного товара внутри страны его местонахождения, а также укажите его ориентировочный вес.";
  var resE = "Стоимость заказа в данном случае расчитана только для  г. Москва, Санкт-Петербург, Минск». Если вы из другого города, свяжитесь с нами через форму заявки для более точного расчета.";

  var resMoreGbp = "Пожалуйста, уточняйте стоимость и возможность доставки у наших менеджеров.";
  var resMoreUsa = "Поскольку вес вашего товара превышает стандартные размеры, просим связаться с нами для более точного расчета.";

  function calc(){
    document.getElementById("country_from").disabled                           = false;
    document.getElementById("country_from").style.backgroundColor              = '#fff';
    document.getElementById("cost_delivery_in_country").disabled               = false;
    document.getElementById("cost_delivery_in_country").style.backgroundColor  = '#fff';
    document.getElementById("weight").disabled                                 = false;
    document.getElementById("weight").style.backgroundColor                    = '#fff';
    document.getElementById("cost_delivery").disabled                          = false;
    document.getElementById("cost_delivery").style.backgroundColor             = '#fff';

    var country_from             = $("#country_from").val(),
        country_to               = $("#country_to").val(),
        cost_product             = $("#cost_product").val().replace(/[^0-9]+/, ""),
        cost_delivery            = $("#cost_delivery").val().replace(/[^0-9]+/, ""),
        cost_delivery_in_country = $("#cost_delivery_in_country").val().replace(/[^0-9]+/, ""),
        weight                   = $("#weight").val(),
        currency                 = $("#currency").val(),
        delivery                 = $("#delivery").val(),
        totall                   = 0,
        msg                      = '',
        color                    = 'black';

    $("#cost_product").val(cost_product);
    $("#cost_delivery").val(cost_delivery);
    $("#cost_delivery_in_country").val(cost_delivery_in_country);
    cost_product = Number(cost_product);
    cost_delivery = Number(cost_delivery);
    cost_delivery_in_country = Number(cost_delivery_in_country);

    write('','',color);

    if (country_from == '' || country_to == '' || cost_product == 0 || currency == '' || delivery == '') return false;

    if (delivery == '1'){
      if (cost_delivery == 0) return false;
      document.getElementById("cost_delivery_in_country").value                    = '';
      document.getElementById("cost_delivery_in_country").disabled                 = 'disabled';
      document.getElementById("cost_delivery_in_country").style.backgroundColor    = '#f2f2ed';
      document.getElementById("weight").value                                      = '';
      document.getElementById("weight").disabled                                   = 'disabled';
      document.getElementById("weight").style.backgroundColor                      = '#f2f2ed';
    }
    if (delivery == '2' || delivery == '3'){
      document.getElementById("cost_delivery").value                               = '';
      document.getElementById("cost_delivery").disabled                            = 'disabled';
      document.getElementById("cost_delivery").style.backgroundColor               = '#f2f2ed';
    }

    switch(country_to){
      case 'ua':
        switch(delivery){
          case '1':
            var json_if1 =  { usd:285, eur:199, gbp:165 };
            if ( (cost_product + cost_delivery) < json_if1[currency] ){
              totall = ( cost_product + cost_delivery );
              totall += ( cost_product + cost_delivery > 105 ) ? ( cost_product + cost_delivery ) * 0.1 : 10;
              msg = resA;
              color = 'blue';
            } else {
              switch(country_from){
                case 'usa':
                case 'ger':
                  document.getElementById("delivery").value = '2';
                  document.getElementById("cost_delivery").value = '';
                  document.getElementById("cost_delivery").disabled = 'disabled';
                  document.getElementById("cost_delivery").style.backgroundColor = '#f2f2ed';
                  totall = ( cost_product + cost_delivery_in_country ) + json_delivery[country_from][country_to][weight];
                  totall += ( cost_product + cost_delivery_in_country > 105 ) ? ( cost_product + cost_delivery_in_country ) * 0.1 : 10;
                  totall += ( cost_product > 105 ) ? ( cost_product ) * 0.1 : 10;
                  msg = resD;
                  color = 'red';
                break;
                case 'gbp':
                  document.getElementById("delivery").value = '2';
                  document.getElementById("cost_delivery").value = '';
                  document.getElementById("cost_delivery").disabled = 'disabled';
                  document.getElementById("cost_delivery").style.backgroundColor = '#f2f2ed';
                  totall = ( cost_product + cost_delivery_in_country ) + json_delivery[country_from][country_to][weight];
                  totall += ( cost_product + cost_delivery_in_country > 105 ) ? ( cost_product + cost_delivery_in_country ) * 0.1 : 10;
                  msg = resD;
                  color = 'red';
                break;
                case 'othe':
                  totall = ( cost_product + cost_delivery );
                  totall += ( cost_product + cost_delivery > 105 ) ? ( cost_product + cost_delivery ) * 0.1 : 10;
                  msg = resB;
                  color = 'red';
                break;
              }
            }
          break;
          case '2':
          case '3':
            var json_if1 =  { usd:299, eur:199, gbp:150 };
            if ( cost_product < json_if1[currency] ){
              switch(country_from){
                case 'usa':
                case 'ger':
                case 'gbp':
                  totall = ( cost_product + cost_delivery_in_country ) + json_delivery[country_from][country_to][weight];
                  totall += ( cost_product + cost_delivery_in_country > 105 ) ? ( cost_product + cost_delivery_in_country ) * 0.1 : 10;
                  msg = resA;
                  color = 'blue';
                break;
                case 'othe':
                  totall = 0;
                  msg = resC;
                  color = '#94362a';
                break;
              }
            } else {
              switch(country_from){
                case 'usa':
                case 'ger':
                  totall = ( cost_product + cost_delivery_in_country ) + json_delivery[country_from][country_to][weight];
                  totall += ( cost_product + cost_delivery_in_country > 105 ) ? ( cost_product + cost_delivery_in_country ) * 0.1 : 10;
                  totall += ( cost_product > 105 ) ? ( cost_product ) * 0.1 : 10;
                  msg = resA;
                  color = 'blue';
                break;
                case 'gbp':
                  totall = ( cost_product + cost_delivery_in_country ) + json_delivery[country_from][country_to][weight];
                  totall += ( cost_product + cost_delivery_in_country > 105 ) ? ( cost_product + cost_delivery_in_country ) * 0.1 : 10;
                  msg = resB;
                  color = 'red';
                break;
                case 'othe':
                  totall = 0;
                  msg = resC;
                  color = '#94362a';
                break;
              }
            }
          break;
        }
      break;
      case 'ru':
      case 'othe':
        switch(delivery){
          case '1':
            var json_if1 =  { usd:320, eur:210, gbp:175 };
            var json_if2 =  { usd:500, eur:400, gbp:260 };
            if ( (cost_product + cost_delivery) < json_if1[currency] ){
              totall = ( cost_product + cost_delivery );
              totall += ( cost_product + cost_delivery > 105 ) ? ( cost_product + cost_delivery ) * 0.1 : 10;
              msg = resA;
              color = 'blue';
            } else if ( (cost_product + cost_delivery) < json_if2[currency] ){
              switch(country_from){
                case 'usa':
                case 'gbp':
                  document.getElementById("delivery").value = '2';
                  document.getElementById("cost_delivery").value = '';
                  document.getElementById("cost_delivery").disabled = 'disabled';
                  document.getElementById("cost_delivery").style.backgroundColor = '#f2f2ed';
                  totall = 0;
                  msg = resD;
                  color = 'red';
                break;
                case 'ger':
                case 'othe':
                  totall = ( cost_product + cost_delivery );
                  totall += ( cost_product + cost_delivery > 105 ) ? ( cost_product + cost_delivery ) * 0.1 : 10;
                  msg = resB;
                  color = 'red';
                break;
              }
            } else {
              switch(country_from){
                case 'usa':
                  totall = ( cost_product + cost_delivery_in_country ) + json_delivery[country_from][country_to][weight];
                  totall += ( cost_product + cost_delivery_in_country > 105 ) ? ( cost_product + cost_delivery_in_country ) * 0.1 : 10;
                  totall += ( cost_product > 105 ) ? ( cost_product ) * 0.1 : 10;
                  totall += 30;
                  msg = resE;
                  color = '#7030a0';
                break;
                case 'ger':
                  totall = ( cost_product + cost_delivery_in_country ) + json_delivery[country_from][country_to][weight];
                  totall += ( cost_product + cost_delivery_in_country > 105 ) ? ( cost_product + cost_delivery_in_country ) * 0.1 : 10;
                  totall += ( cost_product > 105 ) ? ( cost_product ) * 0.1 : 10;
                  msg = resE;
                  color = '#7030a0';
                break;
                case 'gbp':
                  document.getElementById("delivery").value = '2';
                  document.getElementById("cost_delivery").value = '';
                  document.getElementById("cost_delivery").disabled = 'disabled';
                  document.getElementById("cost_delivery").style.backgroundColor = '#f2f2ed';
                  totall = 0;
                  msg = resD;
                  color = 'red';
                break;
                case 'othe':
                  totall = ( cost_product + cost_delivery );
                  totall += ( cost_product + cost_delivery > 105 ) ? ( cost_product + cost_delivery ) * 0.1 : 10;
                  msg = resB;
                  color = 'red';
                break;
              }
            }
          break;
          case '2':
          case '3':
            var json_if1 =  { usd:380, eur:190, gbp:160 };
            var json_if2 =  { usd:500, eur:400, gbp:260 };
            if ( cost_product < json_if1[currency] ){
              switch(country_from){
                case 'usa':
                case 'gbp':
                  totall = ( cost_product + cost_delivery_in_country ) + json_delivery[country_from][country_to][weight];
                  totall += ( cost_product + cost_delivery_in_country > 105 ) ? ( cost_product + cost_delivery_in_country ) * 0.1 : 10;
                  msg = resA;
                  color = 'blu';
                break;
                case 'ger':
                case 'othe':
                  totall = 0;
                  msg = resC;
                  color = '#94362a';
                break;
              }
            } else if ( cost_product < json_if2[currency] ){
              switch(country_from){
                case 'usa':
                case 'gbp':
                  totall = ( cost_product + cost_delivery_in_country ) + json_delivery[country_from][country_to][weight];
                  totall += ( cost_product + cost_delivery_in_country > 105 ) ? ( cost_product + cost_delivery_in_country ) * 0.1 : 10;
                  msg = resB;
                  color = 'red';
                break;
                case 'ger':
                case 'othe':
                  totall = 0;
                  msg = resC;
                  color = '#94362a';
                break;
              }
            } else {
              switch(country_from){
                case 'usa':
                  totall = ( cost_product + cost_delivery_in_country ) + json_delivery[country_from][country_to][weight];
                  totall += ( cost_product + cost_delivery_in_country > 105 ) ? ( cost_product + cost_delivery_in_country ) * 0.1 : 10;
                  totall += ( cost_product > 105 ) ? ( cost_product ) * 0.1 : 10;
                  totall += 30;
                  msg = resE;
                  color = '#7030a0';
                break;
                case 'ger':
                  totall = ( cost_product + cost_delivery_in_country ) + json_delivery[country_from][country_to][weight];
                  totall += ( cost_product + cost_delivery_in_country > 105 ) ? ( cost_product + cost_delivery_in_country ) * 0.1 : 10;
                  totall += ( cost_product > 105 ) ? ( cost_product ) * 0.1 : 10;
                  msg = resE;
                  color = '#7030a0';
                break;
                case 'gbp':
                  totall = ( cost_product + cost_delivery_in_country ) + json_delivery[country_from][country_to][weight];
                  totall += ( cost_product + cost_delivery_in_country > 105 ) ? ( cost_product + cost_delivery_in_country ) * 0.1 : 10;
                  msg = resB;
                  color = 'red';
                break;
                case 'othe':
                  totall = 0;
                  msg = resC;
                  color = '#94362a';
                break;
              }
            }
          break;
        }
      break;
      case 'be':
        switch(delivery){
          case '1':
            var json_if1 =  { usd:165, eur:120, gbp:95 };
            var json_if2 =  { usd:500, eur:400, gbp:160 };
            if ( (cost_product + cost_delivery) < json_if1[currency] ){
              totall = ( cost_product + cost_delivery );
              totall += ( cost_product + cost_delivery > 105 ) ? ( cost_product + cost_delivery ) * 0.1 : 10;
              msg = resA;
              color = 'blue';
            } else if ( (cost_product + cost_delivery) < json_if2[currency] ) {
              switch(country_from){
                case 'usa':
                case 'gbp':
                  document.getElementById("delivery").value = '2';
                  document.getElementById("cost_delivery").value = '';
                  document.getElementById("cost_delivery").disabled = 'disabled';
                  document.getElementById("cost_delivery").style.backgroundColor = '#f2f2ed';
                  totall = 0;
                  msg = resD;
                  color = 'red';
                break;
                case 'ger':
                case 'othe':
                  totall = ( cost_product + cost_delivery );
                  totall += ( cost_product + cost_delivery > 105 ) ? ( cost_product + cost_delivery ) * 0.1 : 10;
                  msg = resB;
                  color = 'red';
                break;
              }
            } else {
              switch(country_from) {
                case 'usa':
                  totall = ( cost_product + cost_delivery_in_country ) + json_delivery[country_from][country_to][weight];
                  totall += ( cost_product + cost_delivery_in_country > 105 ) ? ( cost_product + cost_delivery_in_country ) * 0.1 : 10;
                  totall += ( cost_product > 105 ) ? ( cost_product ) * 0.1 : 10;
                  totall += 30;
                  msg = resE;
                  color = '#7030a0';
                break;
                case 'ger':
                  totall = ( cost_product + cost_delivery_in_country ) + json_delivery[country_from][country_to][weight];
                  totall += ( cost_product + cost_delivery_in_country > 105 ) ? ( cost_product + cost_delivery_in_country ) * 0.1 : 10;
                  totall += ( cost_product > 105 ) ? ( cost_product ) * 0.1 : 10;
                  msg = resE;
                  color = '#7030a0';
                break;
                case 'gbp':
                  document.getElementById("delivery").value = '2';
                  document.getElementById("cost_delivery").value = '';
                  document.getElementById("cost_delivery").disabled = 'disabled';
                  document.getElementById("cost_delivery").style.backgroundColor = '#f2f2ed';
                  totall = 0;
                  msg = resD;
                  color = 'red';
                break;
                case 'othe':
                  totall = ( cost_product + cost_delivery );
                  totall += ( cost_product + cost_delivery > 105 ) ? ( cost_product + cost_delivery ) * 0.1 : 10;
                  msg = resB;
                  color = 'red';
                break;
              }
            }
          break;
          case '2':
          case '3':
            var json_if1 =  { usd:165, eur:120, gbp:95 };
            var json_if2 =  { usd:500, eur:400, gbp:160 };
            if ( cost_product < json_if1[currency] ){
              switch(country_from){
                case 'usa':
                case 'gbp':
                  totall = ( cost_product + cost_delivery_in_country ) + json_delivery[country_from][country_to][weight];
                  totall += ( cost_product + cost_delivery_in_country > 105 ) ? ( cost_product + cost_delivery_in_country ) * 0.1 : 10;
                  msg = resA;
                  color = 'blue';
                break;
                case 'ger':
                case 'othe':
                  totall = 0;
                  msg = resC;
                  color = '#94362a';
                break;
              }
            } else if ( cost_product < json_if2[currency] ) {
              switch(country_from){
                case 'usa':
                case 'gbp':
                  totall = ( cost_product + cost_delivery_in_country ) + json_delivery[country_from][country_to][weight];
                  totall += ( cost_product + cost_delivery_in_country > 105 ) ? ( cost_product + cost_delivery_in_country ) * 0.1 : 10;
                  msg = resB;
                  color = 'red';
                break;
                case 'ger':
                case 'othe':
                  totall = 0;
                  msg = resC;
                  color = '#94362a';
                break;
              }
            } else {
              switch(country_from){
                case 'usa':
                  totall = ( cost_product + cost_delivery_in_country ) + json_delivery[country_from][country_to][weight];
                  totall += ( cost_product + cost_delivery_in_country > 105 ) ? ( cost_product + cost_delivery_in_country ) * 0.1 : 10;
                  totall += ( cost_product > 105 ) ? ( cost_product ) * 0.1 : 10;
                  totall += 30;
                  msg = resE;
                  color = '#7030a0';
                break;
                case 'ger':
                  totall = ( cost_product + cost_delivery_in_country ) + json_delivery[country_from][country_to][weight];
                  totall += ( cost_product + cost_delivery_in_country > 105 ) ? ( cost_product + cost_delivery_in_country ) * 0.1 : 10;
                  totall += ( cost_product > 105 ) ? ( cost_product ) * 0.1 : 10;
                  msg = resE;
                  color = '#7030a0';
                break;
                case 'gbp':
                  totall = ( cost_product + cost_delivery_in_country ) + json_delivery[country_from][country_to][weight];
                  totall += ( cost_product + cost_delivery_in_country > 105 ) ? ( cost_product + cost_delivery_in_country ) * 0.1 : 10;
                  msg = resB;
                  color = 'red';
                break;
                  case 'othe':
                  totall = 0;
                  msg = resC;
                  color = '#94362a';
                break;
              }
            }
          break;
        }
      break;
    }
    totall = (totall>0)?totall.toFixed(2)+' '+currency.toUpperCase():'';
    write(totall, msg, color);
  };

  function write(totall, msg, color){
    document.getElementById("totall").innerHTML=totall;
    if (typeof(color) == 'undefined') color = 'black';
    document.getElementById("msg").style.color=color;
    document.getElementById("msg").innerHTML=msg;
  };

  function reset(){
    document.getElementById("country_from").disabled=false;
    document.getElementById("country_from").style.backgroundColor = '#fff';
    document.getElementById("cost_delivery_in_country").disabled=false;
    document.getElementById("cost_delivery_in_country").style.backgroundColor = '#fff';
    document.getElementById("weight").disabled=false;
    document.getElementById("weight").style.backgroundColor = '#fff';
    document.getElementById("cost_delivery").disabled=false;
    document.getElementById("cost_delivery").style.backgroundColor = '#fff';

    document.getElementById("country_to").value='';
    document.getElementById("cost_product").value='';
    document.getElementById("currency").value='';
    document.getElementById("delivery").value='';
    document.getElementById("cost_delivery").value='';
    document.getElementById("country_from").value='';
    document.getElementById("cost_delivery_in_country").value='';
    document.getElementById("weight").value='';
    write('','','black');
  };

