var user;

function formatProductRow(d, u) {
	//console.info(d);
	return 
	'<div style="margin-bottom: 10px; padding-bottom: 10px; border-bottom: 1px solid #FFFFFF;">'+
	'	<img width="100px" height="100px" class="left" style="border: 1px solid #DFD1D1; margin-right: 10px;" src="'+d.image+'">'+
	'	<h5 style="font-size: 9pt;">'+d.title+'</h5><br />'+
	'	<strong>Price:</strong> $'+d.price+
	'	<strong style="margin-left: 30px;">Quantity:</strong> '+d.quantity+
	'	<a style="display: block; margin-top: 20px;" href="javascript:removeFromBasket(\''+d.id+'\', \''+u+'\')">Remove From Basket</a>'+
	'	<br class="clr" />'+
	'</div>';	
}

function gatherBasket(user) {
	$.facebox(function() {
		$.ajax({
			url: '/ajax/handle.php?cmd=showbasket&u='+user,
			method: 'get',
			dataType: "json",
			success: function(data) {
				var pText = '';
				var products = data.products;
				var len = products.length;
				
				if(len > 0) {
					for(var x=0; x < len; x+=1) {
						var d = products[x];
						pText += "<div style=\"margin-bottom: 10px; padding-bottom: 10px; border-bottom: 1px solid #FFFFFF;\"><img width=\"100px\" height=\"100px\" class=\"left\" style=\"border: 1px solid #DFD1D1; margin-right: 10px;\" src=\""+d.image+"\"><h5 style=\"font-size: 9pt;\">"+d.title+"</h5><br /><strong>Price:</strong> $"+d.price+"<strong style=\"margin-left: 30px;\">Quantity:</strong>"+d.quantity+"<a style=\"display: block; margin-top: 20px;\" href=\"javascript:removeFromBasket('"+d.id+"', '"+user+"')\">Remove From Basket</a><br class=\"clr\" /></div>";
					}
				} else {
					pText += "<p>No products Found</p>";	
				}
				
				//pText += "<div style=\"margin-bottom: 10px; padding-bottom: 10px; text-align: right; \"><h5 style=\"font-size: 9pt;\"><strong>Total:</strong> $"+data.total+"<br class=\"clr\" /><br /></div>";
				$('miniCount').html(len+" items");
				$.facebox(pText);
			}
		});
	});
}


function showCad() {
   if($('#cadPrice').style.display == "none") {
     $('#cadPrice').show(); 
   } else {
     $('#cadPrice').hide();
   }
}

function addToBasket(id, user) {
	var params = {p:id, u:user}
	
   $.ajax({
		url: '/ajax/handle.php?cmd=addtobasket', 
      type: 'post',
      data: params,
      success: function(tran) {
         gatherBasket(user);
     },
     error: function() { alert("Failed to add product. Please contact our customer support for assistance."); }
   });
}



function removeFromBasket(id, user) {
    var user = user;
    $.ajax({
		url: '/ajax/handle.php?cmd=removefrombasket',
      type: 'post',
      data: {p:id, u:user},
      success: function(d) {
         gatherBasket(user);
      }
   });
}

function billingCopy() {
   var ffields = new Array('address', 'address2', 'city', 'postal', 'prov', 'country');
   
   if($('#same').checked == true) {
      for(k=0; k<ffields.length;k++) {
         $("#s_"+ffields[k]).val($("#b_"+ffields[k]).val());
      }
   } else {
      for(k=0; k<ffields.length;k++) {
         if($("#s_"+ffields[k]).val() == $("#b_"+ffields[k]).val()) {
            $("#s_"+ffields[k]).val('');  
         }
      }
   }
}

function showLarge(i) {
	$('large').src = i.src;	
}

function switchMethod(uthis) {
   if(uthis.value=='phone') {
      $('#paybyphone').show();
      $('#paybycc').hide();
   }
   
   if(uthis.value=='cc') {
      $('#paybyphone').hide();
      $('#paybycc').show();
   }
}

function payOnline() {
   $('#PayOnline').show();
   $('#poButton').hide();
}
