(function($) {
	
$(function() {
	$("#ship_zip").bind("keyup",function(e) {
		if(e.which == 13) {
			$("#calc_shipping").trigger("click");
		}
	});
});

})(jQuery);

function deleteItems() {
	items = $("#cart_content .cl_num input").not(":eq(0)").filter("[checked]");
	if(items.length > 0 && confirm("Remove the selected item"+( (items.length > 1) ? 's' : '')+" from your cart?")) {
		performDelete(items);
	}
}

function performDelete(obj) {
	var items = '';
	var custs = '';
	var cm = '';
	var cmb = '';
	
	for(i=0;i<obj.length;i++) {
		if(obj[i].id.substring(0,1) == 'c') {
			custs += cmb+obj[i].value;
			cmb = ',';
		}
		else {
			items += cm+obj[i].value;
			cm = ',';
		}
	}
	
	var action = '';
	cm = '';
	sep = '';
	if(items != '') {
		action = 'delete_item';
		cm = ',';
		sep = '_';
	}
	if(custs != '') {
		action += cm+'delete_custom';
		items += sep+custs;
	}
	
	$.post("../operations/cart.php","action="+action+"&list="+items, function() { 
		location.reload(true); 
	});
}

//function deleteCustom(o) {
//	itm = '';
//	cm = '';
//	
//	for(i=0;i<o.length;i++) {
//		itm += cm+o[i];
//		cm = ',';
//	}
//	$.post("../operations/cart.php","action=delete_custom&list="+itm);//obj.value+"&logo_id=null"+custom);
//}

function recalculate() {
	var items = '';
	var cm = '';
	var packages = $("#cart_content tr").not("tr.cl_totals,#cart_head,tr.tr_custom");
	var quants = $("td.cl_qty input");
	for(i=0;i<packages.length;i++) {
		pk = packages[i].id.split("_")[1];
		qty = quants[i].value;
		items += cm+pk+'-'+qty;
		cm = ',';
	}
	$.post("../operations/cart.php","action=modify_quantity&list="+items,function() { location.reload(true); });
}

function calcShipping(weight) {
	$.post("../operations/cart.php","action=get_new_loose_shipquote&pk="+$("#ship_zip").val()+'&q='+weight,function(){ location.reload(true); });
}

function selectAll(select) {
	$("#cart_content .cl_num input").not(":eq(0)").attr({ checked: select });
}

function checkSelect() {
	var all = $("#cart_content .cl_num input").not(":eq(0)");
	var on = all.filter("[checked]");
	$("#cart_head input")[0].checked = (all.length == on.length);
}


// ---------------------------------
// Logo and Custom Text functions

function removeLogo(pk) {
	$.post("/operations/cart.php","pk="+pk+"&logo_id=null",function(){ location.reload(true); });
}

function changeText(pk) {
	var custom = '';
	for(i=0;i<5;i++) {
		custom += "&custom_line[]="+$("#custom"+pk+"-"+i).val().replace(/[&]/g,
			function(c) {
				return '%'+c.charCodeAt(0).toString(16);
			}
		);
	}
	$.post("/operations/cart.php","pk="+pk+custom,function(m){
		location.reload(true); 
	});
}

function removeText(pk) {
	// just send blank lines
	var custom = '';
	for(i=0;i<5;i++) {
		custom += "&custom_line[]=";
	}
	$.post("/operations/cart.php","pk="+pk+custom,function(m){
		location.reload(true); 
	});
}

function toggleCustom(pk) {
	if($("#cwrap"+pk).css("display") == "none") {
		$("#cwrap"+pk).show();
		$("#view"+pk).html("Hide Custom Options");
	}
	else {
		$("#cwrap"+pk).hide();
		$("#view"+pk).html("View/Change Custom Options");
	}
}