function reloadCart(x){
	getCart();
	return false;
}

function getCart() {
	var qs = new Array();
	qs["action"] = "cart";
	booyantAJAX("/ajax",qs,loadCart);
	return false;
}

function loadCart(x) {
	$("#cart").html(x);
	reloadDOM();
	return false;
}

function addToCart(id){
	var qs = new Array();
	qs["action"] = "add";
	qs["product_id"] = id;
	qs["increment"] = "true";
	qs["quantity"] = "1";
	booyantAJAX("/ajax",qs,reloadCart);
	return false;
}

function removeFromCart(id){
	var qs = new Array();
	qs["action"] = "remove";
	qs["product_id"] = id;
	$("#checkbox_"+id).removeAttr("checked");
			   		
	booyantAJAX("/ajax",qs,reloadCart);
	return false;
}

function reloadDOM() {		   
			   $(".cartRemove").click(function(){
			   		var p = $(this).attr("id").split("_");
			   		removeFromCart(p[1]);
			   });
			   
			   $(".changeVersion").change(function(){
   					var p = $(this).val().split("_");
   					removeFromCart(p[0]);
   					addToCart(p[1]);
   				});
}

function countyList(x) {

	var counties = x.split("|");
	
	if (counties.length > 1) {
		var options = "";
		for (c in counties){
			options += "<option value='" + counties[c] + "'>" + counties[c] + "</option>";
		}
		$("#shipping_county").html(options);
		$("#shippingCounty").slideDown(500);
		$("#billingCounty").slideDown(500);
	} else {
		options = "<option value=''></option>";
		$("#shipping_county").html(options);
		$("#shippingCounty").hide();
	}
}

function billingCountyList(x) {

	var counties = x.split("|");
	
	if (counties.length > 1) {
		var options = "";
		for (c in counties){
			options += "<option value='" + counties[c] + "'>" + counties[c] + "</option>";
		}
		$("#billing_county").html(options);
		$("#billingCounty").slideDown(500);
	} else {
		options = "<option value=''></option>";
		$("#billing_county").html(options);
		$("#billingCounty").hide();
	}
}

$(document).ready(function() {
   $(".cartAdd").click(function(){
   		var p = $(this).attr("id").split("_");
   		
   		if ($(this).is(':checked')){
   			addToCart(p[1]);
   		} else {
			removeFromCart(p[1]);
   		}	
   });
   
   $(".changeVersion").change(function(){
   		var p = $(this).val().split("_");
   		removeFromCart(p[0]);
   		addToCart(p[1]);
   });
   
   $(".cartRemove").click(function(){
   		var p = $(this).attr("id").split("_");
		removeFromCart(p[1]);
   });
   
   $("#sameAsShipping").click(function(){
   		$(".billingInfo").slideToggle("slow");
   });
   
   $(".help").click(function(){
   		$(".popup").css("left", ($(window).width()/2)-250).css("top", ($(window).height()/2)-250).show()
   });
   
   $(".close_popup").click(function(){
   		$(".popup").hide();
   });
   
   $("#zipcode").keyup(function(){
   		if ($(this).val().length == 5){
   			var qs = new Array();
			qs["action"] = "counties";
			qs["state"] = $("#shipping_state").val();
			qs["zip"] = $(this).val();
			booyantAJAX("/ajax",qs,countyList);
			return false;
   		}
   });
   
   $("#shipping_state").change(function(){
   		if ($("#zipcode").val().length == 5){
   			var qs = new Array();
			qs["action"] = "counties";
			qs["state"] = $("#shipping_state").val();
			qs["zip"] = $("#zipcode").val();
			booyantAJAX("/ajax",qs,countyList);
			return false;
   		}
   });
   
   $("#billingzipcode").keyup(function(){
   		if ($(this).val().length == 5){
   			var qs = new Array();
			qs["action"] = "counties";
			qs["state"] = $("#billingstate").val();
			qs["zip"] = $(this).val();
			booyantAJAX("/ajax",qs,billingCountyList);
			return false;
   		}
   });
   
   $("#billingstate").change(function(){
   		if ($("#billingzipcode").val().length == 5){
   			var qs = new Array();
			qs["action"] = "counties";
			qs["state"] = $("#billingstate").val();
			qs["zip"] = $("#billingzipcode").val();
			booyantAJAX("/ajax",qs,billingCountyList);
			return false;
   		}
   });
   
});
