function addtocart(partid, quantity){
  var f = document.createElement("form");
  f.method = "post";
  f.action = "cart.php";

  var p = document.createElement("input");
  p.setAttribute("type", "hidden");
  p.setAttribute("name", "action");
  p.setAttribute("value", "add");
  f.appendChild(p);

  var p = document.createElement("input");
  p.setAttribute("type", "hidden");
  p.setAttribute("name", "ks");
  p.setAttribute("value", location.href);
  f.appendChild(p);

  var p = document.createElement("input");
  p.setAttribute("type", "hidden");
  p.setAttribute("name", "partid");
  p.setAttribute("value", partid);
  f.appendChild(p);

  var p = document.createElement("input");
  p.setAttribute("type", "hidden");
  p.setAttribute("name", "quantity");
  p.setAttribute("value", quantity);
  f.appendChild(p);

  document.body.appendChild(f);
  f.submit();
}
function removefromcart(cartitemid){
  var f = document.createElement("form");
  f.method = "post";
  f.action = "cart.php";

  var p = document.createElement("input");
  p.setAttribute("type", "hidden");
  p.setAttribute("name", "action");
  p.setAttribute("value", "remove");
  f.appendChild(p);

  var p = document.createElement("input");
  p.setAttribute("type", "hidden");
  p.setAttribute("name", "cartitemid");
  p.setAttribute("value", cartitemid);
  f.appendChild(p);

  document.body.appendChild(f);
  f.submit();
}
function updatequantity1(cartitemid){
  var quantity = prompt("Enter new quantity:");
  if(!(isInteger(quantity))){
    alert("Invalid quantity.");
    return 1;
  }
  var f = document.createElement("form");
  f.method = "post";
  f.action = "cart.php";

  var p = document.createElement("input");
  p.setAttribute("type", "hidden");
  p.setAttribute("name", "action");
  p.setAttribute("value", "updatequantity");
  f.appendChild(p);

  var p = document.createElement("input");
  p.setAttribute("type", "hidden");
  p.setAttribute("name", "cartitemid");
  p.setAttribute("value", cartitemid);
  f.appendChild(p);

  var p = document.createElement("input");
  p.setAttribute("type", "hidden");
  p.setAttribute("name", "quantity");
  p.setAttribute("value", quantity);
  f.appendChild(p);

  document.body.appendChild(f);
  f.submit();
}
function updatequantity(cartitemid){
  var q = document.getElementById(cartitemid);
  var quantity = q.value;

  if(!(isInteger(quantity))){
    alert("Invalid quantity.");
    return 1;
  }
  var f = document.createElement("form");
  f.method = "post";
  f.action = "cart.php";

  var p = document.createElement("input");
  p.setAttribute("type", "hidden");
  p.setAttribute("name", "action");
  p.setAttribute("value", "updatequantity");
  f.appendChild(p);

  var p = document.createElement("input");
  p.setAttribute("type", "hidden");
  p.setAttribute("name", "cartitemid");
  p.setAttribute("value", cartitemid);
  f.appendChild(p);

  var p = document.createElement("input");
  p.setAttribute("type", "hidden");
  p.setAttribute("name", "quantity");
  p.setAttribute("value", quantity);
  f.appendChild(p);

  document.body.appendChild(f);
  f.submit();
}
function isPositiveInteger(s){
  var secondArg = false;
  if (isPositiveInteger.arguments.length > 1)
    secondArg = isPositiveInteger.arguments[1];
    return (isSignedInteger(s, secondArg)
          && ( (isEmpty(s) && secondArg)  || (parseInt (s) > 0) ) );
}
function isInteger(s){
  var x = /^-?\d+$/.test(s);
  if(x && s > 0){
    return true;
  } else {
    return false;
  }
}
function updatezip1(cartid){
  var zip = prompt("Enter zip code:");
  if(!(isInteger(zip))){
    alert("Invalid zip code.");
    return 1;
  }
  var f = document.createElement("form");
  f.method = "post";
  f.action = "cart.php";

  var p = document.createElement("input");
  p.setAttribute("type", "hidden");
  p.setAttribute("name", "action");
  p.setAttribute("value", "updatezip");
  f.appendChild(p);

  var p = document.createElement("input");
  p.setAttribute("type", "hidden");
  p.setAttribute("name", "cartid");
  p.setAttribute("value", cartid);
  f.appendChild(p);

  var p = document.createElement("input");
  p.setAttribute("type", "hidden");
  p.setAttribute("name", "zip");
  p.setAttribute("value", zip);
  f.appendChild(p);

  document.body.appendChild(f);
  f.submit();
}
function updatezip(cartid){
  var z = document.getElementById("zipcode");
  var zip = z.value;

  if(!(isInteger(zip))){
    alert("Invalid zip code.");
    return 1;
  }
  var f = document.createElement("form");
  f.method = "post";
  f.action = "cart.php";

  var p = document.createElement("input");
  p.setAttribute("type", "hidden");
  p.setAttribute("name", "action");
  p.setAttribute("value", "updatezip");
  f.appendChild(p);

  var p = document.createElement("input");
  p.setAttribute("type", "hidden");
  p.setAttribute("name", "cartid");
  p.setAttribute("value", cartid);
  f.appendChild(p);

  var p = document.createElement("input");
  p.setAttribute("type", "hidden");
  p.setAttribute("name", "zip");
  p.setAttribute("value", zip);
  f.appendChild(p);

  document.body.appendChild(f);
  f.submit();
}

