function refreshBasket(){
	$(".minibasket .ajax").html("<img width='160' src='/common/sgraphics/load.gif' />");
	$.get("/mm5/merchant.mvc?Screen=MINI",function(data){$(".minibasket .ajax").html(data);UI_Init_Basket();$(".minibasket a.button").button();});
}

function quickBuy(code){
	$("body").append("<div title='Add to Basket' id='ATB' style='text-align:center;padding-top:10px;'>Verifying Inventory<br><img src='/common/Customize/load.gif' /></div>");
	$.ajax({url:"http://www.rnkshops.com/mm5/merchant.mvc?Screen=JPROD&callback=?&Product_Code="+code, type:"GET", dataType: "jsonp" ,success:
		function(data){
			$("#ATB").dialog('close');
			if(data.inv==0){
				$("#ATB").html("<img src='/common/sgraphics/bad.png' /><br>Sorry, "+data.name+" is not available for sale.");
				openDialog("#ATB");	
			}else if(data.attrc>0){
				if(data.attrc>1){
					$("#ATB").html("\<br>Sorry, "+data.name+" has options and cannot be bought on this page at this time.<br><br>Please click 'View Item' instead.");
					openDialog("#ATB");
				}else{
					quickBuyOptions(data);
				}				
			}else if(data.error=="Error"){
				$("#ATB").html("<img src='/common/sgraphics/caution.png' /><br>Unable to verify inventory.<br><br>Please click 'View Item' instead.");
				openDialog("#ATB");	
			}else{
				$("#ATB").html("<img src='/common/sgraphics/good.png' /><br>"+data.name+" added to basket...");
				$(".minibasket .ajax").html("<img width='160' src='/common/sgraphics/load.gif' />");
				$.get("/mm5/merchant.mvc?Screen=BASK&JQuery=1&Action=ADPR&Product_Code="+data.code+"&Quantity=1",function(){$("#ATB").html("<img src='/common/sgraphics/good.png' /><br>"+data.name+" added to basket...");openDialog("#ATB");refreshBasket();});
				openDialog("#ATB");	
			}
		}
	});
	$("#ATB").dialog({modal: true,width: 250,buttons: {Cancel: function() {$(this).dialog('close');$("#ATB").remove();}}});
}
function quickBuyOptions(data){
	var html="<div class='options'>"
	for (var a = 0; a < data.attributes.length; a++){
		html+="<span class='label'>"+data.attributes[a].prompt+"</span>";
		html+="<select index='"+data.attributes[a].index+"' code='"+data.attributes[a].code+"'>";
		html+="<option value='null'>Please Select</option>"
	    for (var o = 0; o < data.attributes[a].options.length; o++){
	    	html+="<option value="+data.attributes[a].options[o].code+">"+data.attributes[a].options[o].prompt+"</option>";
	    }
	    html+="</select>";
	}
	html+="</div>"
	$("#ATB").html(html);
	$("#ATB").dialog({modal: true,width: 250,buttons: {Continue: function() {
		var pass=true;
		var options="";
		$("#ATB option:selected").each(function() {
   			if($(this).val()=="null"){pass=false;}
   			options+="&Product_Attributes["+$(this).parent().attr("index")+"]:code="+$(this).parent().attr("code");
   			options+="&Product_Attributes["+$(this).parent().attr("index")+"]:value="+$(this).val();
		});
		if(pass){$(this).dialog('close');
				$(".minibasket .ajax").html("<img width='160' src='/common/sgraphics/load.gif' />");
				$.get("/mm5/merchant.mvc?Screen=BASK&JQuery=1&Action=ADPR&Product_Code="+data.code+"&Quantity=1"+options,function(){$("#ATB").html("<img src='/common/sgraphics/good.png' /><br>"+data.name+" added to basket...");openDialog("#ATB");refreshBasket();});	
		}
	}}});

}
function openDialog(id){
	$(id).dialog({modal: true,width: 250,buttons: {Ok: function() {$(this).dialog('close');$(id).remove();}}});
}
