cart = new Object;

cart.scroll = function() {
  if (document.getElementById('cart')) {
    var st = ((window.pageYOffset ? window.pageYOffset : document.documentElement.scrollTop));
    if (!document.getElementById('noCartMovement')) {
      var sh = (document.documentElement.scrollHeight ? document.documentElement.scrollHeight : document.body.scrollHeight);
      var ch = (document.body.clientHeight ? document.body.clientHeight : window.innerHeight);
      document.getElementById('cart').style.marginTop=(parseInt(st)+4)+'px';
    } else {
      if (document.getElementById('cartontop')) {
        document.getElementById('cart').style.marginTop='-29px';
      } else if (document.getElementById('utgavukeyp')) {
        document.getElementById('cart').style.marginTop='85px';
      } else if (document.getElementById('utgavukeyp_cart')) {
        document.getElementById('cart').style.marginTop='85px';
      } else {
        document.getElementById('cart').style.marginTop='85px';
      }
    }
    var l = (1010/2)+((document.documentElement.clientWidth/2))-(255);
    document.getElementById('cart').style.left=l+'px';
    document.getElementById('cart').style.position='absolute';
    document.getElementById('cart').style.visibility='visible';
  }
}


cart.addProduct = function(id,loc,mo,co,tp) {

  xmlHttp_addProduct = ajaxObj();

  xmlHttp_addProduct.onreadystatechange=function() {
    if(xmlHttp_addProduct.readyState==4) {
      if (xmlHttp_addProduct.responseText == "true") {
        cart.updateCart();
        if (loc == 'loc') {
          location.href='./?sida=utgavur&bok='+id;
        }
      } else {
        alert('Ein feilur hendi ... ');
      }
    }
  }

  var cp = '';

  if (mo > 0 && co > 0) {
    cp = '&months='+mo+'&computers='+co;
  }

  var tps = '';
  if (tp != undefined) { tps = '&type='+tp; }

  xmlHttp_addProduct.open("GET","modules/shop/ajax.addProductToCart.php?id="+id+cp+tps,true);
  xmlHttp_addProduct.send(null);

}


cart.removeProduct = function(id,type) {

  xmlHttp_removeProduct = ajaxObj();

  xmlHttp_removeProduct.onreadystatechange=function() {
    if(xmlHttp_removeProduct.readyState==4) {
      if (xmlHttp_removeProduct.responseText == "true") {
        cart.updateCart();
      }
    }
  }

  tp = '';

  if (type != undefined && type != '') {
    tp = '&type='+type;
  }

  xmlHttp_removeProduct.open("GET","modules/shop/ajax.removeProductFromCart.php?id="+id+tp,true);
  xmlHttp_removeProduct.send(null);

}


cart.updateCart = function() {

  cart.scroll();

  if (document.getElementById('menuCartAjaxElement')) {

    xmlHttp_updateCart = ajaxObj();

    xmlHttp_updateCart.onreadystatechange=function() {
      if(xmlHttp_updateCart.readyState==4) {
        if (xmlHttp_updateCart.responseText) {
          document.getElementById('menuCartAjaxElement').innerHTML = xmlHttp_updateCart.responseText;
        }
      }
    }

    xmlHttp_updateCart.open("GET","modules/shop/ajax.cart.php",true);
    xmlHttp_updateCart.send(null);

  }

}


cart.emptyCart = function() {

  xmlHttp_emptyCart = ajaxObj();

  xmlHttp_emptyCart.onreadystatechange=function() {
    if(xmlHttp_emptyCart.readyState==4) {
      if (xmlHttp_emptyCart.responseText == "true") {
        cart.updateCart();
      }
    }
  }

  xmlHttp_emptyCart.open("GET","modules/shop/ajax.emptyCart.php",true);
  xmlHttp_emptyCart.send(null);

}
