var cookieName = 'myproducts';

function storeProduct(t) {

  var b = (t.id).split('_');

  var mytype = b[1];
  var id     = b[2];

  var s = readCookie(cookieName);

  var myflag;

  if (s) {

    var a = s.split(',');

    var tmp = '';

    for(var i = 0; i - a.length < 0; i++) {

      var s = a[i];

      if (s == id) {

        myflag = true;
      }
      else {

        if (tmp) {

          tmp += ',' + s;
        }
        else {
                
          tmp = s;
        }
      }
    }

    if (myflag) {
      // delete
    }
    else {

      if (tmp) {

        tmp += ',' + id;
      }
      else {

        tmp = id;
      }
    }

    createCookie(cookieName, tmp, 365);
  }
  else {

    createCookie(cookieName, id, 365);
  }

  // change text or image

  showProducts();

  return false;
}

function clearProducts() {

  createCookie(cookieName, '', -1);

  showProducts();

  return false;
}

function showProducts() {

  var storedProducts = document.getElementById('storedProducts');

  var s = readCookie(cookieName);

  if (s) {

    if (storedProducts) storedProducts.innerHTML = s;
  }
  else {

    if (storedProducts) storedProducts.innerHTML = '';
    s = '';
  }

  //
  // show right text for links and right image: "add" or "remove"
  //
  var a = s.split(',');

  var c = document.links;

  for(var j = 0; j - c.length < 0; j++) {
   
    var t = c[j];

    if (t.id) {

      var found = false;

      for(var i = 0; (i - a.length < 0) && !found; i++) {

        var m = a[i];

        if (t.id == ('product_image_' + m)) {

          showProduct(t, 'image', false);
          found = true;
        }
        else if (t.id == ('product_text_' + m)) {

          showProduct(t, 'text', false);
          found = true;
        }
      }

      if (!found) {
        var b = (t.id).split('_');

        var mytype = b[1];

        showProduct(t, mytype, true);
      }
    }
  }

}

function showProduct(t, mytype, myflag) {

  if (mytype == 'text') {

    if (myflag) {

      t.innerHTML = 'Add Part';
    }
    else {

      t.innerHTML = 'Remove Part';
    }
  }
  if (mytype == 'image') {

    if (myflag) {

      t.innerHTML = '<img src="images/saveplan.png" alt="Save Plan" title="Click To Save This Plan." border="0" style="vertical-align: middle;" />';
    }
    else {

      t.innerHTML = '<img src="images/removeplan.png" alt="Remove Plan" title="Click To Remove This Plan." border="0" style="vertical-align: middle;" />';
    }
  }
}

// Cookies. This standart functions to work with cookies from Javascript.

function clearCookie(n) {
  createCookie(n, '', -1);
}

function readCookie(n) {

  var n2 = n + '=';

  var a = document.cookie.split(';');

  for(var i = 0; i - a.length < 0; i++) {

    var s = a[i];
    while (s.charAt(0)==' ') {

      s = s.substring(1, s.length);
    }

    if (s.indexOf(n2) == 0) {

      return s.substring(n2.length, s.length);
    }
  }
  return null;
}

function createCookie(n, v, d) {

  var e = ''

  if (d) {

    var date = new Date();
    date.setTime((new Date()).getTime() + (d*24*60*60*1000));
    var e = '; expires=' + date.toGMTString();
  }

  document.cookie = n + '=' + v + e + '; path=/';
}
