/*
$ /js/Shop.js | 2008/05/22 08:30 | 2008/07/30 09:23 $
*/

function AJAX() {
  if (window.XMLHttpRequest) {
    xmlhttp = new XMLHttpRequest(); // Mozilla, Firefox, Opera, stb.
  }
  else if (window.ActiveXObject) {
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); // IE
  }
  else {
    xmlhttp = new ActiveXObject('Msxml2.XMLHTTP'); // IE
  }

  return xmlhttp;
}


function addToCart(t, id) {
  var db = document.getElementById('db').value * 1;
  var versionList = document.getElementById('version');

  if (versionList)
    var version = versionList.options[versionList.selectedIndex].value;
  else
    var version = null;

  if (db > 0) {
    dbyet = parseInt(getCookie(t + '|' + id + '|' + version)) * 1;

    if (dbyet > 0)
      db += dbyet;

    setCookie(t + '|' + id + '|' + version, db);

    refreshCart(); // Kosár frissítése

    if (dbyet > 0)
      document.getElementById('shopMessage').innerHTML = 'A termékből <span>' + parseInt(db - dbyet) + ' db</span> bekerült még a kosarába, így összesen <span>' + parseInt(db) + ' db</span> van a kosarában ebből a termékből.';
    else
      document.getElementById('shopMessage').innerHTML = 'A termékből <span>' + parseInt(db) + ' db</span> bekerült a kosarába.';
  }
}


function delFromCart(t, id, v) {
  delCookie(t + '|' + id + '|' + v);
  window.location = location.href;
}


function addToCartN(t, id, v, n) {
  db = parseInt(getCookie(t + '|' + id + '|' + v)) * 1;
  n = parseInt(n);

  if (db > 0)
    db += n;

  setCookie(t + '|' + id + '|' + v, db);

  window.location = location.href;
}


function removeFromCartN(t, id, v, n) {
  db = parseInt(getCookie(t + '|' + id + '|' + v)) * 1;
  n = parseInt(n);

  if (db > 0)
    db -= n;

  setCookie(t + '|' + id + '|' + v, db);

  window.location = location.href;
}


function setCookie(cookie_name, cookie_value) {
  document.cookie = cookie_name + "=" + escape(cookie_value) + "; path=/";
}


function getCookie(cookie_name) {
  if (document.cookie.length > 0) {
    c_start=document.cookie.indexOf(cookie_name + "=");

    if (c_start != -1) {
      c_start = c_start + cookie_name.length + 1;
      c_end = document.cookie.indexOf(";", c_start);

      if (c_end == -1) c_end = document.cookie.length;

      return unescape(document.cookie.substring(c_start,c_end));
    }
  }
  return "";
}


function delCookie(cookie_name) {
  if (getCookie(cookie_name)) {
    document.cookie = cookie_name + "=0; path=/; expires=Thu, 01-Jan-1970 00:00:01 GMT";
  }
}


function refreshCart() {
  var xmlhttp1 = AJAX();
  var xmlhttp2 = AJAX();

  if (!xmlhttp1 | !xmlhttp2) {
    alert('Az Ön böngészője nem támogatja ezt a funkciót!');
  }
  else {
    xmlhttp1.open('GET', '/kosar/AJAX_refreshCart.cgi?type=content', true);
    xmlhttp1.onreadystatechange = function() {
      if ((xmlhttp1.readyState == 4) && (xmlhttp1.status == 200)) {
        document.getElementById('cart').innerHTML = xmlhttp1.responseText;
      }
    }

    xmlhttp1.send(null);

    xmlhttp2.open('GET', '/kosar/AJAX_refreshCart.cgi?type=image', true);
    xmlhttp2.onreadystatechange = function() {
      if ((xmlhttp2.readyState == 4) && (xmlhttp2.status == 200)) {
        document.getElementById('headerRight').innerHTML = xmlhttp2.responseText;
      }
    }

    xmlhttp.send(null);
  }
}
