var msg = 0;

function addItemCart( docID, price) {
  imgID  = obj( 'img_id_' + docID );
  linkID = obj( 'link_id_' + docID );
  linkCart = obj( 'showCart' );
  basketCount = obj( 'basket_count' );
  basketPrice = obj( 'basket_price' );
      
  if ( imgID  != undefined ) imgID.src = '/img/doc_in_folder.png';
  if ( linkID != undefined ){
    linkID.style.cursor = 'default';
    linkID.onclick = function(){};
    linkID.title = '';
  }
  
  if ( basketCount != undefined ) basketCount.innerHTML = parseInt( basketCount.innerHTML ) + 1;
  if ( basketPrice != undefined ) basketPrice.innerHTML = price + ' грн';
  if ( linkCart    != undefined ) linkCart.style.display = 'block';
}

function toCart( doc_id ) {
  var docID = doc_id
  req = new BGRequest();
  if ( req.ready ) {
    req.open( "GET", "/ua/docs/basket.html?action=add&id="+docID );
    req.onComplete = function() {
      addItemCart( docID, parseInt( req.responseText ) );
    }
    req.send();
  }
  
  return false;
}

function clearCartList() {
  req = new BGRequest();
  if ( confirm('Ви д╕йсно хочете очистити Ваш кошик?') ) {
    if ( req.ready ) {
      req.open( "GET", "/goods/cart.html?action=clear" );
      req.onComplete = function() {
      }
    
      req.send();
    }
  }
  return false;
}

function updateCart( id, price ) {
  req = new BGRequest();
  if ( req.ready ) {
    countVal = 0;
    idValue = obj( 'id'+id );
    if ( idValue != undefined ) countVal = idValue.value;
    req.open( "GET", "/goods/cart.html?action=update&id="+id+'&count='+ countVal );
    req.onComplete = function() {
      blSumma = obj( 'summa_' );
      if ( blSumma != undefined ) {
        hiddem_ = obj( 'value_' + id );
        if ( hiddem_ != undefined ) count = parseInt( hiddem_.value );
        countVal = parseInt( countVal );
        price = parseInt( price );
        count = countVal - count;
        suma = parseInt( blSumma.innerHTML );
        suma = suma + ( count * price );
        blSumma.innerHTML = suma;
        hiddem_.value = countVal;
      }
    }
  }
  req.send();
}

function deleteItem( id, price ) {

  req = new BGRequest();
  if ( req.ready ) {
    req.open( "GET", "/goods/cart.html?action=delete&id="+id );
    req.onComplete = function() {
      countVal = 0;
      idValue = obj( 'id'+id );
      if ( idValue != undefined ) countVal =  parseInt( idValue.value );
      blSumma = obj( 'summa_' );
      summa = 0
      if ( blSumma != undefined ) {
        summa = parseInt( blSumma.innerHTML );
        summa = summa - ( countVal * parseInt( price ) );
        blSumma.innerHTML = summa;
      }
      blTD = obj( 'td_div' + id  );
      if ( blTD != undefined ) removeObj( blTD );
      if ( summa == 0 ) {
        req1 = new BGRequest();
        if ( req1.ready ) {
          req1.open( "GET", "/goods/cart.html?action=clear" );
          req1.onComplete = function() {
            list = obj( 'list_' );
            if ( list != undefined ) list.style.display = 'none';
            list = obj( 'sum_' );
            if ( list != undefined ) list.style.display = 'none';
            links = obj( 'links_' );
            if ( links != undefined ) links.style.display = 'none';
          }
        }  
        req1.send();
      }
    }
  }
  req.send()
  return false;
}
