/* This code was written by Patrick McKenzie and is described at 
http://www.bingocardcreator.com/articles/developing-shopping-cart.htm .

I release this work unto the public domain.  You may use it in any fashion you wish. */

function pjm_ReplaceQueryParam(url, queryParam, newValue) {
  var queryParamRegex = queryParam;
  queryParamRegex = queryParamRegex.replace(/[\[]/,"\\\\[").replace(/[\]]/,"\\\\]");
  var regexS = "[\\?&]" + queryParamRegex + "=([^&#]*)";
  var regex = new RegExp(regexS);
  var results = regex.exec(url);
  if (results == null) {
    if (url.indexOf("?") >= 0) {
      return url + "&" + queryParam + "=" + escape(newValue);
    } else {
      return url + "?" + queryParam + "=" + escape(newValue);
    }
  } else {
    return url.replace(queryParam + "=" + results[1], queryParam + "=" + escape(newValue));
  }
}

function pjm_updateCart(parameter, newValue) {
  pjm_updateCart(parameter, newValue, false);
}

function pjm_updateCart(parameter, newValue, paypalOnly) {
  var cart_links = $$('.pjmCartLink');
  cart_links.each(function(link) {
    if (!paypalOnly || (link.className.indexOf("Paypal") >= 0)) {
      var link_url = link.href;
      link_url = pjm_ReplaceQueryParam(link_url, parameter, newValue);
      link.href = link_url;
    }
  });
}

function pjm_setGoogleButtonActive(active) {
  $$('.pjmCartGoogleCheckoutLink').each(function(element) {
    if (!active) {
      element.onclick = function(e) { alert("Google Checkout cannot be used to buy more than one copy.  Please use Paypal instead."); return false;};
    } else { element.onclick = null;}
  });
  $$('.pjmCartGoogleCheckoutImage').each(function(element) {
    element.src = "/images/google-checkout-cart" + (active ? "" : "-bw") + ".gif";
  });
}

function pjm_updateCartButtonItem(productNumber) {
  pjm_productNumber = productNumber;
  pjm_updateCart("i", productNumber);
  var productName = "Bingo Card Creator";
  if (pjm_productNumber == "patio11-001") {
    productName = "Bingo Card Creator w/ CD <br/> (includes free shipping)"
  }
  $$('.pjm_cart_item_name').each(function(element) {element.innerHTML = productName});
  pjm_recalculateSubtotal();
  return pjm_productNumber;
}

function pjm_updateQuantity(quantity) {
  if (quantity >= 1 && quantity <= 100) {
    pjm_productQuantity = Math.round(quantity);
    pjm_recalculateSubtotal();
    pjm_setGoogleButtonActive(pjm_productQuantity == 1);
  } else if (quantity > 100) {
    pjm_updateQuantity(100);
  }
}

function pjm_recalculateSubtotal() {
  var quantity = pjm_productQuantity;
  var unitPrice = 29.95;
  var discountDetails = "";
  if (pjm_productNumber == 'patio11-001') {
     unitPrice += 5;
  }
  var subtotal = 0;
  var totalPrice = 0;
  if (quantity != 0 && (quantity != "") && quantity != 'NaN') {
    totalPrice = unitPrice * quantity;
    subtotal = totalPrice.toFixed(2);
  } else {
    return false;
  }
  
  if ((quantity >= 2) && (quantity < 5)) {
    totalPrice *= .9;
    unitPrice *= .9;
    discountDetails = "<strong>10% volume discount (2-4 copies) applied.</strong>"
  }
  if (quantity >=5) {
    totalPrice *= .8;
    unitPrice *= .8;
    discountDetails = "<strong>20% volume discount (5+ copies) applied.</strong>"
  }
  
  unitPrice = unitPrice.toFixed(2);
  totalPrice = totalPrice.toFixed(2);
  
  pjm_updateCart("quantity", quantity, true);
  pjm_updateCart("amount", unitPrice, true);
  
  $$('.pjm_cart_discount_details').each(function (element) {element.innerHTML = discountDetails});
  $$('.pjm_cart_quantity').each(function (element) {element.value = quantity});
  $$('.pjm_cart_subtotal').each(function (element) {element.innerHTML = "$" + totalPrice});
  $$('.pjm_cart_total').each(function (element) {element.innerHTML = "$" + totalPrice});
  return true;
}

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 pjm_openCart() {
  var params = new Object();
  params.width = 700;
  params.height = 375;
  iBox.show($('pjm_cart').innerHTML, '', params);
  //rectifies issue with quantity and price being inconsistent if you switch item in cart
  pjm_updateQuantity(pjm_productQuantity);
  var ejunkie_custom = readCookie("ejunkie_custom");
  if (ejunkie_custom != null) {
    pjm_updateCart("custom", ejunkie_custom);
  }
  return false;
}
