function set_price(obj, price) {
    var tp = document.getElementById('total_price');
    if (!isNaN(parseInt(obj.value))) {
        tp.innerHTML = parseInt(obj.value) * parseInt(price) + ' руб.';
    }
    else {
        tp.innerHTML = '0 руб.';
        obj.value = 0;
    }
    
}

function add_count_to_basket() {
    var count = document.getElementById('goods_count');
    var form = document.getElementById('send_order_form');
    if (!isNaN(parseInt(count.value))) {
        form.action = form.action + count.value + '/';
        form.submit();
    }
    return false
}

function send_http_request(args) {
 args = args || {};

 var is_timed_out = false;
 var timer = null;

 if (args.timeout) {
  timer = setTimeout(function () {
   is_timed_out = true;
   if (args.ontimeout) {
    args.ontimeout();
   }
   if (args.onfail) {
    args.onfail();
   }
   if (args.onfinish) {
    args.onfinish();
   }
  }, args.timeout);
 }

 var xmlhttp = window.XMLHttpRequest?new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");

 xmlhttp.onreadystatechange = function () {
  if ((!is_timed_out) && (xmlhttp.readyState == 4)) {
   clearTimeout(timer);
   timer = null;
   if (xmlhttp.status == 200) {
    if (args.ontextready) {
     args.ontextready(xmlhttp.responseText);
    }
    if (args.ondataready) {
     var data = eval('res=' + xmlhttp.responseText + ';res');
     args.ondataready(data);
    }
    if (args.onfinish) {
     args.onfinish();
    }
   } else {
    if (args.onerror) {
     args.onerror(xmlhttp.status, xmlhttp.responseText);
    }
    if (args.onfail) {
     args.onfail();
    }
    if (args.onfinish) {
     args.onfinish();
    }
   }
  }
 };

 xmlhttp.open(args.method||'GET', args.url, args.async||true, args.user, args.passwd);
 xmlhttp.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT");
 xmlhttp.send(args.data||null);
};


function add_item(pk) {

qty = document.getElementById('goods_count');

obj = document.getElementById('basket_info');

send_http_request({
 url: '/catalog/add/'+pk+'/'+qty.value+'/?ajax=1',
 timeout: 20000,
 method: 'GET',
 async: false,
 ontextready: function(response_text) {
  if (response_text == "ok") {  
    ovl.hide();
    update_info();
    alert("Товар добавлен. Для покупки нажмите кнопку 'Корзина'.");
  }
 },
 onerror: function(status, response_text) {
  alert('Error: ' + status + '\n\n' + response_text);
 },
 ontimeout: function() {
  alert('Timeout');
 },
 onfail: function() {
  alert('Fail');
 },
 onfinish: function() {
 }
});
return false
}

function update_info() {

send_http_request({
 url: '/catalog/info/',
 timeout: 20000,
 method: 'GET',
 async: false,
 ontextready: function(response_text) {
    price = response_text.split('-')[0];
    count = response_text.split('-')[1];
    obj = document.getElementById('full_price');
    if (obj) {
        obj.innerHTML = price;
    }
    obj = document.getElementById('items_count');
    if (obj) {
        if (count != '0') {
            obj.innerHTML = count;
        }
        else {
            obj.innerHTML ='0';
        }
    }
 },
 onerror: function(status, response_text) {
  alert('Error: ' + status + '\n\n' + response_text);
 },
 ontimeout: function() {
  alert('Timeout');
 },
 onfail: function() {
  alert('Fail');
 },
 onfinish: function() {
 }
});
}

function del_item(pk) {

obj = document.getElementById('basket_info');
if (obj) {
obj.innerHTML = '<img src="/base/static/images/loading.gif">';
}
send_http_request({
 url: '/catalog/del/'+pk,
 timeout: 20000,
 method: 'GET',
 async: false,
 ontextready: function(response_text) {
  if (response_text == "ok") {
    obj = document.getElementById('good_'+pk);
    if (obj) {  
        obj.style.display= 'none';
    }
    update_info();
  }
 },
 onerror: function(status, response_text) {
  alert('Error: ' + status + '\n\n' + response_text);
 },
 ontimeout: function() {
  alert('Timeout');
 },
 onfail: function() {
  alert('Fail');
 },
 onfinish: function() {
 }
});

};
