var sack = {
	get: {},

	put: function(from)
	{
		$(from).find('textarea, select:visible, input:text:visible, input[type=hidden], input:password, input:submit, input:image, input:reset, input:button, input:checked').each(function()
		{
			var el = $(this), label;

			if (label = el.attr('name'))
			{
				sack.get[label] = el.val();
			}
		});
	},

	sift: function(fix, see)
	{
		var errors = [];

		$.each(this.get, function(label, value)
		{
			if (fix[label])
			{
				sack.get[label] = fix[label](sack.get[label]);
			}
			if (see[label] && !see[label](sack.get[label]))
			{
				errors[errors.length] = label;
			}
		});

		return errors;
	}
};

(function($) { $(document).ready(function($)
{	//common features
	$('textarea[rel]').bind('keydown keyup focus blur change', function()
	{	//put max length in rel attr
		var self = $(this), count = $('#' + self.attr('rel')), size = parseInt(count.attr('rel'));

		if (count.length && size)
		{	//check for numeric max
			if (this.value.length > size) this.value = this.value.substr(0, size);
			count.text(size - this.value.length);
		}
	}).keyup();

	$('input:checkbox[rel]').bind('click change', function()
	{	//toggle something by checkbox
		var self = $(this); $('#' + self.attr('rel')).toggle(self.is(':checked'));
	}).change();

	var q = []; OLFS.addtocart = function()
	{	//actually send
		if (q.length)
		{	//pop one off start
			var self = q.shift();
			OLFS.sendtocart(self[0], self[1]);
		}
	};

	var addtocart = function(e)
	{	//find stuff to add to cart
		var add = '', qty = '', list;

		if (e)
		{	//read form vars
			e.preventDefault();
			list = $('input[name=cart_add], input[name=qty]', this);
		}	//read querystring
		else list = location.search.substr(1).split('&');

		if (OLFS.sendtocart)
		{	//defined in some js
			$.each(list, function()
			{	//read from element or break from
				if (e) var self = $(this), name = self.attr('name'), val = self.val();
				else var self = this.split('='), name = self[0], val = self[1];
	
				if (name == 'qty')
				{	//qty input
					if (add)
					{	//add was first
						q[q.length] = [add, val];
						add = qty = '';
					}	//qty was first
					else qty = val;
				}	//better url checking!
				else if (name == 'cart_add')
				{	//cart_add input
					if (qty)
					{	//add was second
						q[q.length] = [val, qty];
						add = qty = '';
					}
					else
					{	//add was by itself
						q[q.length] = [add, ''];
						add = val;
					}
				}
			});
			//delete values from quick order, add last item, begin queue send
			if (e && $('input[name=cart_add]:visible', this).length) list.val('');
			if (add) q[q.length] = [add, '']; OLFS.addtocart(); //start sending
		}
	};

	$('form.add-to-cart-result, form.add-to-cart, form.product-add').submit(addtocart);
	$('td.reorder').live('click', addtocart); addtocart();//using querystring

        $('select[name="billstate"]').change(function() {
            var county = $('select[name="billcounty"]');
            county = county.parent();
            if ($(this).val() == 'WI') {
                county.show();
            } else {
                county.hide();
            }
        });
        $('select[name="billstate"]').change();  // fire it to ensure initial display is correct

        $('select[name="shipstate"]').change(function() {
            var county = $('div#shipcounty');
            if ($(this).val() == 'WI') {
                county.show();
            } else {
                county.hide();
            }
        });
        $('select[name="shipstate"]').change();  // fire it to ensure initial display is correct


}); })(jQuery);

