// UNPACKED VERSION
function rrGetCMTag(thisLink){
var rrCMType="NA";
var rrPT = R3_COMMON.placementTypes;
var t="";
if(typeof(rrPT)!="undefined"){
if (rrPT.indexOf('item')>0){
if(thisLink.indexOf('pa=content1')>0){t="2000000351";}
else if(thisLink.indexOf('pa=content2')>0){t="2000000352";}
else if(thisLink.indexOf('pa=content3')>0){t="2000000353";}
else if(thisLink.indexOf('pa=content4')>0){t="2000000354";}
}else if (rrPT.indexOf('category')>0){
if(thisLink.indexOf('pa=content1')>0){t="2000000361";}
else if(thisLink.indexOf('pa=content2')>0){t="2000000362";}
else if(thisLink.indexOf('pa=content3')>0){t="2000000363";}
}else if (rrPT.indexOf('search')>0){
if(thisLink.indexOf('pa=content1')>0){t="2000000371";}
else if(thisLink.indexOf('pa=content2')>0){t="2000000373";}
}else if (rrPT.indexOf('add')>0){
if(thisLink.indexOf('pa=content1')>0){t="2000000381";}
else if(thisLink.indexOf('pa=content2')>0){t="2000000382";}
}else if (rrPT.indexOf('cart')>0){
if(thisLink.indexOf('pa=content1')>0){t="2000000391";}
else if(thisLink.indexOf('pa=content2')>0){t="2000000392";}
}else if (rrPT.indexOf('purchase')==1){
if(thisLink.indexOf('pa=content1')>0){t="2000000401";}
else if(thisLink.indexOf('pa=content2')>0){t="2000000402";}
}else if (rrPT.indexOf('error')>0){
if(thisLink.indexOf('pa=content1')>0){t="2000000411";}
else if(thisLink.indexOf('pa=content2')>0){t="2000000422";}
}
}
return t;
}

function populatePrices(data){
  if (typeof jQuery!='undefined'){
    $.each(data.prices,function(i,prices){     
      var rrPrFor="";
      var rrHref="";
      var rrHref2="";
      // if the API returns valid:true, display add to cart and price 
      if(prices.valid=="true"){       
        var bulkStr="";
        // get the unit of measure value set in the HTML
        var rrUOM=$("div#rrUOM_HIDDEN_"+prices.pid).html();
        // see if there's bulk pricing; toggles message to display
        if(prices.hasBulkPricingAvailable=="true"){bulkStr="As Low As";}else{bulkStr="Your Price";}
        // if there's a price returned, then create the price block
        if(prices.price!==undefined && prices.price!==""){  
          rrPrFor='<span class="merchPrice">';
          rrPrFor+='<label class="price_title">'+bulkStr+'</label>';
          rrPrFor+='<span class="main_price">'+prices.price;
          rrPrFor+='<br/>'+rrUOM;          rrPrFor+='</span></span>';
          $("span.rrPriceElement"+prices.pid).html(rrPrFor);
        }      
        // prefix the SKU box with the string
        $("span.rrSKU"+prices.pid).text("Item #"+prices.pidForDisplay);

        // change the add to cart "pid" to the pidd for display if it exists
        $("input.item_atc_rrSKU"+prices.pid).attr("value",prices.pidForDisplay);

        // get the href
        rrHref=$("a.rrHref"+prices.pid).attr("href");
        // replace pid to display if exists
        rrHref2=rrHref.replace('id%3D'+prices.pid,'id%3D'+prices.pidForDisplay);
        // add the CM tag
        rrHref2+="cm_cat%3D"+rrGetCMTag(rrHref2);
        // if there's a custom flag for this product, 
        if($("div#rrCustomType"+prices.pid).html()!==""){
        // add the conf type
          rrHref2+="%26configurableItemType%3D"+$("div#rrCustomType"+prices.pid).html();
        // change the landing path for US
        rrHref2 = rrHref2.replace("catalogSku.do","vendorRouter.do");
        }
        // rewrite the href for all anchor tags with class rrHref+pid
        $("a.rrHref"+prices.pid).attr("href",rrHref2);    
      }
    });
  } 
} 

// add to cart mousedown tracking
if (typeof jQuery!='undefined'){
  $(document).ready(function() {
    $("input.rrRec").mousedown(function () {
      var i=0;
      var rrClick= new Object();
      $('input.input').each(function(){
        if($(this).val()>0){
          var attrStr = $(this).attr("id").replace("quantityBox atc_","");
          rrClick[i] = new Image();
          rrClick[i].src = $("a.rrHref"+attrStr).attr("href");
          i++;
        }
      });
    });
  });
}