// Cart-A December 14, 2011 Final
// Remember that valid JSON code requires that input variables are double quoted "variable":"value"
var CART = "../cart/cartfunc.htm";
////////////
$(document).ready(function(){
$.post(CART,{cmd:"load"},function(data){$("#shopcart").html(data);},"html"); //  Load Shopping Cart when page is loaded
function addItem(event){
	var thisprod=$(this).attr("id");
	var formdata=$(this).serialize();
	var itemid = new Date().getTime();
	$("#"+thisprod+ "> div.added").show(500);
	$.post(CART,{cmd:"additem",itemid: itemid,formdata:formdata },function(data){$("#shopcart").html(data);},"html");
	event.preventDefault();
}
$(".addtocart").on("submit",addItem);
$("#favoritesection").on("submit",".favtocart", addItem);
$("#favoritesection").on("click",".favitem", function() {$(".checkout").show(); $("#pporcc").hide();$(".immediatePP").show();  });

$("#shopcart").on("click","input['button'].plus",function(){ // console.log("PLUS");
	var itemid = $(this).parent().attr("id");
	var x = +1;
	$.post(CART,{cmd:"plus",itemid: itemid, iqty:x.toFixed(0) },function(data){$("#shopcart").html(data);},"html");
});
/* === */
$("#shopcart").on("click","input['button'].minus",function(){ // console.log("MINUS");
	var itemid = $(this).parent().attr("id");
	var x = -1;
	$.post(CART,{cmd:"minus",itemid: itemid, iqty:x.toFixed(0) } ,function(data){$("#shopcart").html(data);},"html");
});
/* === */
$("#shopcart").on('change',"input['text'].qty",function(){
	var itemid = $(this).parent().attr("id");
	var qty = $("#"+itemid+">.qty");
	var x = +qty.val();
	if (isNaN(x)){
		alert("You just entered something besides a number in the quantity field");
		$(this).css("border","4px solid red").css("font-size","200%");
	}else{
		$(this).css("border","0px solid red").css("font-size","100%");
		$.post(CART,{cmd:"changeqty",itemid: itemid, iqty:x.toFixed(0) } ,function(data){$("#shopcart").html(data);},"html");
	}
});
/* === */
$("#shopcart").on('click',"input['button'].location",function(){var $loc = $(this).val();	$.post(CART,{cmd:"picklocation",userlocation:$loc } ,function(data){$("#shopcart").html(data);},"html");
	//alert('You have chosen '+$loc+' as your Shipping Region' );
});
/* === */

///////////////////////////
});/// End document ready
