$(document).ready(function(){
  $("form#BF").submit(function() { // loginForm is submitted
    var cid = $('#countries2').attr('value'); // get city ID
    var weight = $('#weight').attr('value'); // get weight
    var doo = $('#do').attr('value'); // get submit
    var dostavka_id = $('#dostavka_id').attr('value'); // get dostavka_d
    var svol = $('#SVol').attr('value'); // get product count
    var total_kolvo = readCookie('total_kolvo');
    var ccid = readCookie('cities');
    
//    alert(svol);
    
    if (cid && cid != ccid && dostavka_id==12 || cid && total_kolvo != svol && dostavka_id==12) { // values are not empty
//      alert('YUH');
      $.ajax({
        type: "GET",
        url: "/cgi-bin/ems.pl", // URL of the Perl script
        contentType: "application/json; charset=windows-1251",
        dataType: "json",
        // send username and password as parameters to the Perl script
        data: "countries2=" + cid + "&weight=" + weight,
        beforeSend: function(){
              $('div#loginResult').text("Загрузка...");
            },
        cache: false,
        ajaxOptions: {cache: false},
        // script call was *not* successful
        error: function(XMLHttpRequest, textStatus, errorThrown) { 
          $('div#loginResult').text("responseText: " + XMLHttpRequest.responseText 
            + ", textStatus: " + textStatus 
            + ", errorThrown: " + errorThrown);
          $('div#loginResult').addClass("error");
        }, // error 
        // script call was successful 
        // data contains the JSON values returned by the Perl script 
        success: function(data){
          if (data.error) { // script returned error
            $('div#loginResult').text("Вес товара превышает макс. 31,5кг.");
            $('div#loginResult').addClass("error");
          } // if
          else { // login was successful
//            $('form#loginForm').hide();
//            $('div#loginResult').text("Загрузка...");
//            alert(data.userid);
            $('div#loginResult').text("Срок доставки (дней) " + data.delivery);
            $('div#loginResult').addClass("success");
            eval(f.dostavka_price.value = data.userid);
            CheckVol(1);
            document.cookie = "cities="+cid+"; path=/";
            document.cookie = "dostavka_summa="+data.userid+"; path=/";
            document.cookie = "deliverytime="+data.delivery+"; path=/";
            document.cookie = "total_kolvo="+svol+"; path=/";
          } //else
        } // success
      }); // ajax
    } // if
    else if (!cid && dostavka_id==12) {
      $('div#loginResult').text("Выберите город или область");
      $('div#loginResult').addClass("error");
    }
    else {
      return true;
    }
    $('div#loginResult').fadeIn();
    return false;
  });
});
