jQuery(function($) {

var cart_total	= 0;

unbind_selections();
total();

$("form#member_form").submit( function(event) {
	if ( cart_total == 0 )
	{		
		$("div#form-error2").show();
		$("div#form-error2 ul").append( "<li>There are no items for purchase in your cart. Please return to the previous page and make some selections.</li>" );
		return false;
	}
});

function unbind_selections()
{	
	//	Remove from cart
	$("div#cart table tr td a[@class='minus']").click( function(event) {
		var link		= $(this);
		var purchase_id	= $(this).attr("href");
		
		$.post( "/realtor-enroll/ajax_cart_delete/" + purchase_id, {}, function (data) {
			if ( data.substr(0,1) == "!" )
			{
				alert(data.substr(1));
			}
			else
			{
				link.parents("tr").remove();
				switch_rows();
				total();
			}
		});
		
		return false;
	});
}

function switch_rows()
{
	var swtch = "two";
	
	$("div#cart table tr").not( $("div#cart table tr#total_row") ).each(function(i) {
		if ( swtch == "two" ) { swtch = "one"; } else { swtch = "two"; }
		$(this).children("td").attr("class", swtch);
	});
}

function total()
{
	cart_total	= 0;
	
	$("div#cart table tr td a[@class='minus']").each( function(i,n) {
		cart_total	+= $(this).attr("rel") * 1;
	});
	
	$("div#cart table tr td#total").html("$ " + cart_total.toFixed(0));
}

});
