var user;
var curItem = 0;

function gatherBasket(user) {
   $.facebox(function() {
      $.ajax({
   		url: 'https://www.carats.com/ajax/handle.php?cmd=showbasket&u='+user,
         method: 'get',
   		dataType: "json",
         success: function(trans) {
            var update, bottom;
            for(i=0; i<trans.length; i++) {
               if(trans[i].state) {
                  bottom= '<div style="margin-bottom: 10px; padding-bottom: 10px; text-align: right;"><h5 style="font-size: 9pt;"><strong>Total:</strong> $'+trans[i].total+'</h5> <br class="clr" /><br /></div>';
               } else {
                  update+= '<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="https://www.carats.com/'+trans[i].image+'"><h5 style="font-size: 9pt;">'+trans[i].title+'</h5><br /><strong>Price:</strong> $'+trans[i].price+' <strong style="margin-left: 30px;">Quantity:</strong>'+trans[i].quantity+'<br class="clr" /></div>';
   			   }
            }
            update = update.replace("undefined", "")+bottom;
            
   			$.facebox(update);
            
            /*$('#miniCount').html(trans.length+" items");*/
         }
      });
   });
}

function showCad() {      
   if($('#cadPrice').css('display') == "none") {
      $('#cadPrice').show(); 
   } else {
     $('#cadPrice').hide();
   }
}

function addToBasket(id, user) {
   if(id == 'null') {
      id = $("#subStullerProduct").val().replace("_", ":");
   }
   
   //alert("Adding ID: "+id);
	var params = {p:id, u:user}

   $.ajax({
		url: 'https://www.carats.com/ajax/handle.php?cmd=addtobasket', 
      type: 'post',
      data: params,
      success: function(tran) {
         gatherBasket(user);
			//alert(user);
     },
     error: function() { alert("Failed to add product. Please contact our customer support for assistance."); }
   });
}



function removeFromBasket(id, user) {
   user = user;
   new Ajax.Request('https://www.carats.com/ajax/handle.php?cmd=removefrombasket', {
      method: 'post',
      parameters: {p:id, u:user},
      onSuccess: function(transport) {
         gatherBasket(res);
      }
   });
}

function billingCopy() {
   var ffields = new Array('address', 'address2', 'city', 'postal', 'prov', 'country');
   
   if($('#same').is(":checked")) {
      for(k=0,len=ffields.length; k<len;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').attr({'src':$(i).attr('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();
}

function certViewer(id) {
   $.facebox("Cert Viewer");
}

function switchProductDesc(item) {
   $("#otherDescriptions>div").hide();
   curItem = $(item).val().replace(":", "_");
   //alert(curItem);
   $("#"+curItem).show();
}
