////////////////////////////
function isInteger(val)
////////////////////////////
	{
	if (isBlank(val)){return false;}
	for(var i=0;i<val.length;i++)
		{
		if(!isDigit(val.charAt(i))){return false;}
		}
	return true;
	}
////////////////////////////
function delete_row(val,attr)
////////////////////////////
	{
	var inputs = [];
	inputs.push('deleterow=1');
	inputs.push('cartrow' + '=' + escape( $(val).attr('rel') ) );

	///>> stupid attributes
	//var build_attributes_id='attributes' + $(val).attr('rel');
        //var attributes_id=$('#' + build_attributes_id).attr('value');
	inputs.push('attributes=' + escape( $('#' + attr).attr('value') ));

	var data = inputs.join('&');
	
	$.ajax({
		data: data,
		dataType: "html",
		success: function(htmlrows) {
		$('#cartrows').html(htmlrows);
		}
	});

        ///>> update blue thingie
        $.post("/checkout.php",{ action: "blue_cart" },
                                function(data) {
                                $('#change_blue_cart').html(data);
                                });

	}

$(document).ready(function() {

	$('#cart').hide();

	$('#addToCartButton').click(function() {

		if ($("#product_id").attr('value') == "")
			{
			$("#option_not_selected").addClass('option_error');
			return false;
			}
		else	{
			$("#option_not_selected").removeClass('option_error');
			$("#option_not_selected").html('');
			}

		var inputs = [];
		inputs.push('quantity' + '=' + escape( $("#product_quantity").val() ) );
		inputs.push('product_id' + '=' + escape( $("#product_id").val() ) );
		inputs.push('attribute_1' + '=' + escape( $("#attribute_1").val() ) );
		inputs.push('attribute_2' + '=' + escape( $("#attribute_2").val() ) );
		inputs.push('attribute_3' + '=' + escape( $("#attribute_3").val() ) );
		inputs.push('addcart=1');
		var data = inputs.join('&');

		 $.ajax({
		 	data: data,
			dataType: "html",
			success: function(htmlrows) {
				$('#cartrows').html(htmlrows);
			}
		});

        ///>> update blue thingie
        $.post("/checkout.php",{ action: "blue_cart" },
                                function(data) {
                                $('#change_blue_cart').html(data);
                                });

	});

	$('#closecart').click(tb_remove);

	$('#continuecart').click(tb_remove);

	$('#updatecart').click(function(){
		///>> if the dolts have nothing
		if (!$('#q1').attr('value'))
			{
			return false;
			}

		var inputs = [];
		var all_products="";

		///>> wow, this is really freakin fun to find all the rows, there is probably a better way to 
		///>> do this but I don't know how
		var prod='q';
    		$('input[type=text]').each(function() {
			var temp=$(this).attr('name');

			if (temp.indexOf(prod) != -1)
				{
				var tmp=new Array();
                		tmp=temp.split(prod);

				if (parseInt(tmp[1]) > 0)
					{
					var build_quantity_id='q' + tmp[1];
					var product_quantity=$('#' + build_quantity_id).attr('value');

					if (!Math.floor(product_quantity) && Math.floor(product_quantity) != 0)
						{
						inputs.push('error=int');
						return false;
						}

					var build_product_id='product' + tmp[1];
					var product_id=$('#' + build_product_id).attr('value');

					var build_attributes_id='attributes' + tmp[1];
					var attributes_id=$('#' + build_attributes_id).attr('value');

					inputs.push(build_product_id + '=' + escape( product_id ));
					inputs.push(build_quantity_id + '=' + escape( product_quantity ));
					inputs.push(build_attributes_id + '=' + escape( attributes_id ));

					all_products += tmp[1] + "|";
					}
				}
    		});
        inputs.push('updatecart=1');
        inputs.push('all_products=' + all_products);
        var data=inputs.join('&');
        $.ajax({
                        data: data,
                        dataType: "html",
                        success: function(htmlrows) {
                       	$('#cartrows').html(htmlrows); 
                        }
        	});

	///>> update blue thingie
        $.post("/checkout.php",{ action: "blue_cart" },
                                function(data) {
                                $('#change_blue_cart').html(data);
                                });


	});

	$('#show_cart').click(function(){
	var inputs = [];
        inputs.push('cartrows=1');
        var data=inputs.join('&');
        $.ajax({
                 data: data,
                 dataType: "html",
                 success: function(htmlrows) {
                 $('#cartrows').html(htmlrows);
                 }
          });	
	});

	$('#checkoutcart').click(function(){
		window.location = '/checkout.php';
	});
	
	
	$('#popupcart').submit(function() {
		return false;
	});

	$.ajaxSetup({
		type: "POST",
		url:  "/ajax.php",
		async: false
	});
	
});
