/*
	cartfunctions.js - provides javascript for add-to-cart, change qty, etc.

	10/21/2009 - rewrote all functions
	03/19/2009 - initial write
*/

/* This is old code...
// single item add to cart
function add_to_cart (item_id, quantity) {
	if (!/^[1-9][0-9]*$/.test (quantity)) {
		alert ("Please enter a valid quantity, greater than 0");
	}
	else {
		document.location.href = "/cart.php?cmd=add&id=" + item_id + "&quantity=" + quantity;
	}
}

// single item change qty
function change_qty (item_id, quantity) {
	if (!/^[1-9][0-9]*$/.test (quantity)) {
		alert ("Please enter a valid quantity, greater than 0");
	}
	else {
		document.location.href = "/cart.php?cmd=change&id=" + item_id + "&quantity=" + quantity;
	}
}

// single item delete
function del_item (item_id) {
	document.location.href = "/cart.php?cmd=change&id=" + item_id + "&quantity=0";
}
*/


function addToCart_item (productnum, qty, bulkmin, bulkmultiple, unitprice_avail) {
	qty = qty.replace (",", "");
	if (!/^[1-9][0-9]*$/.test (qty)) {
		alert ("Please enter a valid quantity, greater than 0");
		return;
	}
	qty = parseInt (qty);

	if (unitprice_avail == "n" && qty < bulkmin) {
		alert ("You must purchase at least " + bulkmin + " of this item.");
		return;
	}

	if (qty > bulkmin && qty % bulkmultiple != 0) {
		alert ("You must purchase this item in multiples of " + bulkmultiple + " if you order more than " + bulkmin + ".\nPlease purchase " + (Math.ceil (qty / bulkmultiple) * bulkmultiple));
		return;
	}

	//alert ("add to cart: " + qty);
	document.location.href = "/cart.php?cmd=add&type=item&id=" + productnum + "&quantity=" + qty + "&option=default&params=";
}

function addMultipleItems_to_cart (formname) {
	// see if any items are checked
	if ($("form[name=" + formname + "] input[type=checkbox][name^=addtocart_sku]:checked").length < 1) {
		alert ("Please select some items to add to your cart");
	}
	else {
		// check quantities
		var allclear = true;
		var url_params = "";

		$("form[name=" + formname + "] input[type=checkbox][name^=addtocart_sku]:checked").each (function () {
			if (allclear) {
				var cursku = $(this).val ();
				var qty = $("form[name=" + formname + "] input[type=text][name=qty_" + cursku + "]").val ();
				var skunum = $("form[name=" + formname + "] input[type=hidden][name=sku_" + cursku + "]").val ();
				var bulkqty = $("form[name=" + formname + "] input[type=hidden][name=bulkqty_" + cursku + "]").val ();
				var bulkmultiple = $("form[name=" + formname + "] input[type=hidden][name=bulkmultiple_" + cursku + "]").val ();
				bulkqty = parseInt (bulkqty);
				bulkmultiple = parseInt (bulkmultiple);

				qty = qty.replace (",", "");
				if (!/^[1-9][0-9]*$/.test (qty)) {
					alert ("Please enter a valid quantity for " + skunum + ", greater than 0");
					allclear = false;
				}
				qty = parseInt (qty);

				if (qty > bulkqty && qty % bulkmultiple != 0) {
					alert ("You must purchase item " + skunum + " in multiples of " + bulkmultiple + " if you order more than " + bulkqty + ".\nPlease purchase " + (Math.ceil (qty / bulkmultiple) * bulkmultiple));
					allclear = false;
				}

				url_params += "&id[" + cursku + "]=1&quantity[" + cursku + "]=" + qty;
			}
		});

		if (allclear) {
			document.location.href = "/cart.php?cmd=multiadd" + url_params;
		}
	}
}

function addToList_item (productnum, qty, bulkmin, bulkmultiple, prod_image, prod_title, prod_uom, unitprice_avail) {
	qty = qty.replace (",", "");
	if (!/^[1-9][0-9]*$/.test (qty)) {
		alert ("Please enter a valid quantity, greater than 0");
		return;
	}
	qty = parseInt (qty);

	if (unitprice_avail == "n" && qty < bulkmin) {
		alert ("You must purchase (or add to list) at least " + bulkmin + " of this item.");
		return;
	}

	if (qty > bulkmin && qty % bulkmultiple != 0) {
		alert ("You must purchase (or add to list) this item in multiples of " + bulkmultiple + " if you order more than " + bulkmin + ".\nPlease purchase (add to list) " + (Math.ceil (qty / bulkmultiple) * bulkmultiple));
		return;
	}

	//alert ("add to cart: " + qty);
	//document.location.href = "/cart.php?cmd=add&type=item&id=" + productnum + "&quantity=" + qty + "&option=default&params=";

	load_addtolist_options ();
	$("div.add_to_list_dialog").dialog ('open');
	$("DIV.add_to_list_dialog DIV.thumbnail IMG").attr ("src", prod_image);
	$("DIV.add_to_list_dialog DIV.details DIV.title").text (prod_title);
	//$("DIV.add_to_list_dialog DIV.details DIV.price").text ("$" + prod_price);
	$("DIV.add_to_list_dialog DIV.details DIV.qty").text ("Qty: " + qty);
	$("form[name=add_to_list_form] input[name=prod_type]").val ("item");
	$("form[name=add_to_list_form] input[name=prod_productnum]").val (productnum);
	$("form[name=add_to_list_form] input[name=prod_qty]").val (qty);
	$("form[name=add_to_list_form] input[name=prod_uom]").val (prod_uom);
	$("form[name=add_to_list_form] input[name=prod_option]").val ("default");
	$("form[name=add_to_list_form] input[name=prod_params]").val ("");
}

function changeQty (producttype, productnum, qty, uom, option, params, bulkmin, bulkmultiple, unitprice_avail) {
	qty = qty.replace (",", "");
	if (!/^[1-9][0-9]*$/.test (qty)) {
		alert ("Please enter a valid quantity, greater than 0");
		return;
	}
	qty = parseInt (qty);

	if (unitprice_avail == "n" && qty < bulkmin) {
		alert ("You must purchase at least " + bulkmin + " of this item.");
		return;
	}

	if (qty > bulkmin && qty % bulkmultiple != 0) {
		alert ("You must purchase this item in multiples of " + bulkmultiple + " if you order more than " + bulkmin + ".\nPlease purchase " + (Math.ceil (qty / bulkmultiple) * bulkmultiple));
		return;
	}

	document.location.href = "/cart.php?cmd=change&type=" + producttype + "&id=" + productnum + "&quantity=" + qty + "&uom=" + uom + "&option=" + option + "&params=" + escape (params);
}

function list_changeQty (list_id, producttype, productnum, qty, uom, option, params, bulkmin, bulkmultiple, unitprice_avail) {
	qty = qty.replace (",", "");
	if (!/^[1-9][0-9]*$/.test (qty)) {
		alert ("Please enter a valid quantity, greater than 0");
		return;
	}
	qty = parseInt (qty);

	if (unitprice_avail == "n" && qty < bulkmin) {
		alert ("You must add-to-list / purchase at least " + bulkmin + " of this item.");
		return;
	}

	if (qty > bulkmin && qty % bulkmultiple != 0) {
		alert ("You must add-to-list / purchase this item in multiples of " + bulkmultiple + " if you order more than " + bulkmin + ".\nPlease add-to-list / purchase " + (Math.ceil (qty / bulkmultiple) * bulkmultiple));
		return;
	}

	document.location.href = "/lists.php?cmd=change_item&list_id=" + list_id + "&type=" + producttype + "&id=" + productnum + "&quantity=" + qty + "&uom=" + uom + "&option=" + option + "&params=" + escape (params);
}

function list_removeItem (list_id, producttype, productnum, uom, option, params) {
	document.location.href = "/lists.php?cmd=change_item&list_id=" + list_id + "&type=" + producttype + "&id=" + productnum + "&quantity=0&uom=" + uom + "&option=" + option + "&params=" + escape (params);
}

function removeItem (producttype, productnum, uom, option, params) {
	document.location.href = "/cart.php?cmd=change&type=" + producttype + "&id=" + productnum + "&quantity=0&uom=" + uom + "&option=" + option + "&params=" + escape (params);
}


function list_checkall (checkstate) {
	$("div.shoppinglist_container form[name=listform] input[type=checkbox][name^=itemhash]").attr ("checked", checkstate);
}
function move_list_to_cart (list_id) {
	// check to see any items are selected
	if ($("div.shoppinglist_container form[name=listform] input[type=checkbox][name^=itemhash]:checked").length < 1) {
		alert ("Please select some items to add to your cart");
	}
	else {
		var itemhash_url = "";
		$("div.shoppinglist_container form[name=listform] input[type=checkbox][name^=itemhash]:checked").each (function () {
			itemhash_url += "&itemhash[]=" + $(this).val ();
		});
		list_url = "/lists.php?cmd=add_to_cart&list_id=" + list_id + itemhash_url;
		//alert (list_url);
		document.location.href = list_url;
	}
}
function move_to_list (list_id) {
	// check to see any items are selected
	if ($("div.shoppinglist_container form[name=listform] input[type=checkbox][name^=itemhash]:checked").length < 1) {
		alert ("Please select some items to move to another list");
	}
	else {
		var new_list_id = $("div.shoppinglist_container form[name=listform] select[name=new_list_id]").val ();

		var itemhash_url = "";
		$("div.shoppinglist_container form[name=listform] input[type=checkbox][name^=itemhash]:checked").each (function () {
			itemhash_url += "&itemhash[]=" + $(this).val ();
		});
		list_url = "/lists.php?cmd=move_to_list&list_id=" + list_id + "&new_list_id=" + new_list_id + itemhash_url;
		//alert (list_url);
		document.location.href = list_url;
	}
}


// collections
function addToCart_col (col_id, params) {
	//alert ("Collection ID: " + col_id + "\n\nParameters: " + params);
	document.location.href = "/cart.php?cmd=add&type=collection&id=" + col_id + "&quantity=1&option=default&params=" + escape (params);
}