function createCookie(name,value,days) {
 if (days) {
  var date = new Date();
  date.setTime(date.getTime()+(days*24*60*60*1000));
  var expires = "; expires="+date.toGMTString();
 }
 else var expires = "";
 document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name) {
 var nameEQ = name + "=";
 var ca = document.cookie.split(';');
 for(var i=0;i < ca.length;i++) {
  var c = ca[i];
  while (c.charAt(0)==' ') c = c.substring(1,c.length);
  if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
 }
 return null;
}
function eraseCookie(name) {
 createCookie(name,"",-1);
}

function cart(action, id, attr) {
 if (action == "add") {
  current_attr1 = window.document.getElementById("option_"+id).value;
  current_attr2 = window.document.getElementById("size_"+id).value;
  var oldcookie = readCookie("store");
  if (oldcookie == null) {
   createCookie("store", id + "-" + current_attr1 + "-" + current_attr2);
  } else {
   createCookie("store", oldcookie + "," + id + "-" + current_attr1 + "-" + current_attr2);
  }
  window.document.location = '?cart=show';
 }
 if (action == "show") {
  window.document.location = '?cart=show';
 }
 if (action == "remove") {
  var attr_values = attr.split("-");
  var cid = id + "-" + attr_values[0] + "-" + attr_values[1];
  var oldcookie = readCookie("store");
  if (oldcookie == null) {
   oldcookie = "";
  } else {
   var items = new Array();
   items = oldcookie.split(",");
   var l = items.length-1;
   for (k=l; k>=0; k--) {
    if (items[k] == cid) {
     items.splice(k, 1);
    }
   }
  }
  oldcookie = "";
  for (j=0; j<items.length; j++) {
   if (j == 0) {
    oldcookie += items[j];
   } else {
    oldcookie += "," + items[j];
   }
  }
  createCookie("store", oldcookie);
  window.document.location = '?cart=show';
 }
 if (action == "option") {
  if (attr != null) {
   nextattr = 0;
   nextoption = window.document.getElementById("option_"+id+"_"+nextattr);
   while (nextoption != null) {
    window.document.getElementById("option_"+id+"_"+nextattr).setAttribute("class", "");
    nextattr++;
    nextoption = window.document.getElementById("option_"+id+"_"+nextattr);
   }
   window.document.getElementById("option_"+id+"_"+attr).setAttribute("class", "shopoptions_selected");
   window.document.getElementById("option_"+id).value = attr;
  }
 }
 if (action == "size") {
  if (attr != null) {
   nextattr = 0;
   nextsize = window.document.getElementById("size_"+id+"_"+nextattr);
   while (nextsize != null) {
    window.document.getElementById("size_"+id+"_"+nextattr).setAttribute("class", "");
    nextattr++;
    nextsize = window.document.getElementById("size_"+id+"_"+nextattr);
   }
   window.document.getElementById("size_"+id+"_"+attr).setAttribute("class", "shopoptions_selected");
   window.document.getElementById("size_"+id).value = attr;
  }
 }
 if (action == "update") {
  var attr_values = attr.split("-");
  var cid = id + "-" + attr_values[0] + "-" + attr_values[1];
  var oldcookie = readCookie("store");
  if (oldcookie == null) {
   oldcookie = "";
  }
  var new_quantity = window.document.getElementById("change_quantity_"+id+"_"+attr_values[0]+"_"+attr_values[1]).value;
  var anum = /(^\d+$)|(^\d+\.\d+$)/;
  if (anum.test(new_quantity)) {
   var remove_id_a = new RegExp(cid + ",", "g");
   var remove_id_b = new RegExp("," + cid, "g");
   var remove_id_c = new RegExp(cid, "g");
   oldcookie = oldcookie.replace(remove_id_a, '');
   oldcookie = oldcookie.replace(remove_id_b, '');
   oldcookie = oldcookie.replace(remove_id_c, '');
   if (oldcookie == "") {
    oldcookie = "";
    for (x=1; x<=new_quantity; x++) {
     if (x == 1) { oldcookie += cid; }
     if (x != 1) { oldcookie += "," + cid; }
    }
   } else {
    for (x=1; x<=new_quantity; x++) {
     oldcookie += "," + cid;
    }
   }
   createCookie("store", oldcookie);
  }
  window.document.location = '?cart=show';
 }
 if (action == "thumbnail") {
  var th_div = window.document.getElementById("thumb_"+id);
  th_div.innerHTML = '<img src="'+attr+'" alt="" />';
  var th_image = th_div.getElementsByTagName("img");
  attr2 = attr.replace("th_", "");
  if(typeof MojoZoom == 'object') {
   MojoZoom.makeZoomable(th_image[0], attr2);
  }
 }
 if (action == "promo") {
  if (attr == "add") {
   id.style.display = 'none';
   window.document.getElementById('shop_promo_code').style.display = 'block';
  }
  if (attr == "apply") {
   var promovalue = window.document.getElementById('referred_by').value.toLowerCase();
   createCookie("store_promotions", promovalue);
   window.document.location.reload(true);
  }
 }
}

