if (document.location.href.indexOf("#id=")>0) {
	document.location.href = document.location.href.replace('#','?');
}
function SetCookie (name, value) {
  var largeExpDate = new Date ();
  largeExpDate.setTime(largeExpDate.getTime() + (365 * 24 * 3600 * 1000));
  var argv = SetCookie.arguments;
  var argc = SetCookie.arguments.length;
  var expires = largeExpDate;
//  var path = location.pathname;
  var path = null;
  var domain = null;
//  var domain = path.substring(0,path.lastIndexOf('/')) +'/';
//  alert(domain);
  var secure = (argc > 5) ? argv[5] : false;
  document.cookie = name + "=" + escape (value) +
  ((expires == null) ? "" : ("; expires=" +
  expires.toGMTString())) +
  ((path == null) ? "" : ("; path=" + path)) +
  ((domain == null) ? "" : ("; domain=" + domain)) +
  ((secure == true) ? "; secure" : "");
}
function getCookieVal (offset) {
  var endstr = document.cookie.indexOf (";", offset);
  if (endstr == -1)
    endstr = document.cookie.length;
  return unescape(document.cookie.substring(offset, endstr));
}
function GetCookie (name) {
  var arg = name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = 0;
  while (i < clen) {
    var j = i + alen;
    if (document.cookie.substring(i, j) == arg)
      return getCookieVal (j);
    i = document.cookie.indexOf(" ", i) + 1;
    if (i == 0)
      break;
  }
  return null;
}
function Buy(product) {
  var value = GetCookie("cart");
  var exists = false;
  if (value == null) {
    SetCookie("cart",product + "j" + "1");
  } else {
      x = value.split('i'); 
      // check if product id is already in the shopping cart and if so increment qty    
      for (i=0; i < x.length; i++) {
        y = x[i].split('j');
        if (y[0] == product) {
          exists = true;
          y[1] = parseInt(y[1]) + 1;
          x[i] = y[0] + "j" + y[1];
          break;
        }
      }
      if (exists) {
        value = x[0];
        for (i=1; i < x.length; i++) {
          value += "i" + x[i];
        }
        SetCookie("cart",value);
      } else {
        SetCookie("cart",value + "i" + product + "j" + "1");
      }
  }
  ViewCart();
}
function ViewCart() {
  document.location.href = "viewcart.php?";
}
function ShowCart() {
  ck = GetCookie("cart");
}
function checkAffiliate() {
	var hashname = document.location.hash;
	if (hashname.substr(0,4)=="#id=") {
		var affiliate = hashname.substr(4);
		document.location.pathname += "?id="+affiliate;
	}
}
function dispatch(product,country) {
	ajaxSubmit("/dispatch.php?product="+product+"&country="+country,"dispatch_result");
}
function dispatch_result(results) {
	if (results!='') {
		document.getElementById("dispatch").innerHTML = results;
	}
}
checkAffiliate();
ShowCart();