/*** COPYRIGHT 2009  BY CUTSEY BUSINESS SYSTEMS LTD. - ALL RIGHTS RESERVED ***/
/* product.js -- Product Page Javascript                                     */
/*****************************************************************************/
/*R010012 11/02/09 APY- Add Wishlist Button.                                 */
/*R010011 09/28/09 APY- Update prePack display.                              */
/*R010010 09/09/09 APY- Add logic for packing details display.               */
/*R010009 08/08/09 JD2- Force default price for selected color.              */
/*R010008 08/07/09 JD2- Use vIsLoggedIn instead.                             */
/*R010007 08/07/09 JD2- Only display strike through when logged in.          */
/*R010006 07/09/09 JB - Fixed spelling mistake - in note as well.            */
/*R010005 04/27/09 APY- Check stock levels before selecting default color.   */
/*R010002 04/16/09 APY- Open packing details in a new window.                */
/*R010001 04/09/09 APY- Do not show OOS colors. Fix Comparable Price Display */
/*R010000 03/27/09 APY- Select size by default if only 1 exists add features.*/
/*                    - Added DisplayStock() function, piece/case prices     */
/* 010004 02/26/09 SB - Add color name under swatch display grid.            */
/* 010003 02/09/08 APY- Updated default multi dim size when on 1 detail rec. */
/* 010002 12/30/08 APY- Corrected swatchHex, add altView update Comparables  */
/* 010001 12/16/08 APY- Add Multi-view, nocache, use hostURL and appURL      */
/*                    - Add alternate comparables display location           */
/* 010000 12/03/08 APY- Created.                                             */
/*****************************************************************************/
/****** Globals JS Vars Defined in product.w ******
var allStylesObj = new Object();
var styleObj     = new Object();
var vLockedImg = "";
var vDefaultWhse  = "";
var vComparableDir = "";
var vSelectedProd = "";
var vSelectedSize  = "";
var vSelectedColor = "";
var vSelectedSizeDesc  = "";
var vSelectedColorDesc = "";
var vSelectedPrice = "";
var vSelectedMultiSize = new Array();
var vRegPriceLabel  = "";
var vRegOnSaleLabel = "";
var vIsRetail = true/false;
var vDispColorDesc = true/false;
var vBrowseParam = ""
var vSponsorCode = ""
** - hostURL and appURL js vars used from header **
******************************************/
var vStyleList = new Array();

//========--------   Load / Display   --------========\\
function loadStyle(vStyle,vSponsor){

 var vFoundStyle = false;
 $.each(vStyleList,function(j){
   if(vStyleList[j] != null && vStyleList[j].styleCode == vStyle &&
      vStyleList[j].styleColor && vStyleList[j].styleColor[0] != null && 
      vStyleList[j].styleSize  && vStyleList[j].styleSize[0] != null)
   { styleObj = vStyleList[j]; vFoundStyle = true; }
 });
 if(vFoundStyle){
  displayStyle();
 }
 else{
  var vCurrTime = new Date();
  var vCurrSecs  = (((vCurrTime.getHours() * 60) + vCurrTime.getMinutes()) * 60) + vCurrTime.getSeconds();
  var vNoCache = "&nocache=" + vCurrSecs;
  $.ajax({
    type: "GET",
    url: hostURL + appURL + "/olc/get-style.w?style=" + vStyle + "&frames=no&target=main&sponsor=" + vSponsor + vNoCache,
    dataType: "xml", 
    complete: function(data) {
       allStylesObj = $.xmlToJSON(data.responseXML); // Save XML in JSON 
       //Return a single style Object
       $.each(allStylesObj.ttStyle,function(i){
          var vAddMe = true;
          $.each(vStyleList,function(j){  
            if(vStyleList[j] != null && vStyleList[j].styleCode == allStylesObj.ttStyle[i].styleCode)
            { vAddMe = false; return; }
          });
          if (vAddMe)
          { vStyleList[vStyleList.length] = allStylesObj.ttStyle[i];}
          if (allStylesObj.ttStyle[i].styleCode == vStyle) {              
            styleObj = allStylesObj.ttStyle[i];
            if (vSelectedProd != vStyle) {
              vSelectedProd = vStyle;
              vSelectedColor = "";
              vSelectedSize = "";
              vLockedImg = "";
              vSelectedSizeDesc = "";
              vSelectedColorDesc = "";
              vSelectedPrice = "";
              vSelectedMultiSize = new Array();
            }
          }
       });
       displayStyle();
    }
  });
 }

}
function displayStyle(){
  var vZoomInit;
   
  if (styleObj.ttSize.length==1){
    vSelectedSize = styleObj.ttSize[0].sizeCode; 
    vSelectedSizeDesc = styleObj.ttSize[0].description;
    $(".selectedSize").html(vSelectedSizeDesc);
  }
  /***vv R010005 vv***/
  var vHasStock = false; 
  if (vSelectedColor != ""){
    /* Ensure selected Color has stock */
    $.each(styleObj.ttItem,function(i){ 
       if(styleObj.ttItem[i].colorCode == vSelectedColor && styleObj.ttItem[i].availQty > 0){
         vHasStock = true;
       }
    });
  }
  if (!vHasStock){
    vSelectedColor = "";
    $.each(styleObj.ttColor,function(i){ 
      $.each(styleObj.ttItem,function(j){ 
         if(styleObj.ttItem[j].colorCode == styleObj.ttColor[i].colorCode && styleObj.ttItem[j].availQty > 0){
            vSelectedColor = styleObj.ttItem[j].colorCode;
            return;
         }
      });
    });    
  }
  /***^^ R010005 ^^***/
  if (vSelectedColor != ""){
    $.each(styleObj.ttColor,function(i){ 
      if (styleObj.ttColor[i].colorCode == vSelectedColor && styleObj.ttColor[i].imageLg != "" ) {     
        $(".imageCell").attr("src",styleObj.ttColor[i].imageLg);
        vLockedImg = styleObj.ttColor[i].imageLg;
        if(styleObj.ttColor[i].imageZm != "")
          vZoomInit = styleObj.ttColor[i].imageZm;
      }
    });         
  }

  if (vLockedImg == "" || vLockedImg == null){
    if (styleObj.ttColor[0].imageLg != "" && styleObj.ttColor[0].imageLg != null){
      $(".imageCell").attr("src",styleObj.ttColor[0].imageLg);
      vLockedImg = styleObj.ttColor[0].imageLg;
      if (styleObj.ttColor[0].imageZm != "" && styleObj.ttColor[0].imageZm != null)
        vZoomInit = styleObj.ttColor[0].imageZm;
    }
    else if(styleObj.imageLg!=""){
      $(".imageCell").attr("src",styleObj.imageLg);
      vLockedImg = styleObj.imageLg;
      if (styleObj.imageZm != "" && styleObj.imageZm != null)
        vZoomInit = styleObj.imageZm;
    }
  }
  $(".prodTitle").html(styleObj.description);
  $(".prodTitle").click(function(){golink('olc/quickorder.w?style=' + vSelectedProd + '&color=' + vSelectedColor,'main');});
  //$(".prodCatalogDesc").html(styleObj.fullDesc);
  if (styleObj.imageMill != "" && styleObj.imageMill != null)
    $(".prodMillImg").html("<img src=\"" + styleObj.imageMill + "\" alt=\"\">");
  if( vSelectedColor=="" || vSelectedColor==null ){
    vSelectedColor = styleObj.ttColor[0].colorCode;
    vSelectedColorDesc = styleObj.ttColor[0].description;
    if (styleObj.ttColor[0].fabDesc != "" && styleObj.ttColor[0].fabDesc != null)
      { vSelectedColorDesc += " " + styleObj.ttColor[0].fabDesc; }
    $(".selectedColor").html(vSelectedColorDesc);
  }
  displayColors();
  if(styleObj.ttSizeScale && styleObj.ttSizeScale[1] != null){    
    styleObj.ttSizeScale.sort(function(a,b){ return a.sortIndex - b.sortIndex }); //Ensure array is Sorted by sortIndex
    $.each(styleObj.ttSizeScale,function(i){
       if(styleObj.ttSizeScale[i].ttSizeScaleValue.length == 1)  /* 010003 */
         vSelectedMultiSize[i] = styleObj.ttSizeScale[i].ttSizeScaleValue[0].dimValue;
       else
         vSelectedMultiSize[i] = "";
    });
    displayMultiSizes();
  }
  else
    displaySizes();         
  displayFeatIcons();
  if(styleObj.ttAltView)  
    showAltViews('');
  if(styleObj.ttCompType)
    displayComprables();
  else{
    $(".prodComparable").html("");
    $(".prodComparable").hide();
  }
  if(vZoomInit != "" && vZoomInit != null)
     $(".MagicZoomBigImageCont > div > img").attr("src",vZoomInit);

  if($(".prodDisplay").css("display")=="none"){
    $(".prodDisplay").fadeIn(250);
  }
  if (vShowImgLib)
     showImgLib(vSelectedProd,vSelectedColor);

  //APY - Force default Price
  priceItem(vSelectedColor,styleObj.ttSize[0].sizeCode); /* R010009 */

 $(".prodPrice").show();
  displayStock(); 

}

function displayColors(){
       var vColorHTML = "";
       var vColorDesc ="";
       var vClass;

       var vHexPercent2 = "75,25";
       var vHexPercent3 = "50,25,25";
       var vHexPercent4 = "25,25,25,25";

       for(var i=0; i<styleObj.ttColor.length; i++){
         if(vSelectedSize == "" || checkItem(styleObj.ttColor[i].colorCode,vSelectedSize) )
         {
           if(vSelectedColor=="" && i==0) { 
             vSelectedColor     = styleObj.ttColor[i].colorCode;             
           }

           if(vSelectedColor==styleObj.ttColor[i].colorCode) {
             vSelectedColorDesc = styleObj.ttColor[i].description; 
             if(styleObj.ttColor[i].attach != "" && styleObj.ttColor[i].attach != null ){
               $("#packDetailLink").attr("href","javascript: viewPacking('" + styleObj.ttColor[i].attach + "');");
               $(".packDetail").show();
             }
             else 
               $(".packDetail").hide();
             if (styleObj.ttColor[i].fabDesc != "" && styleObj.ttColor[i].fabDesc != null)
             { vSelectedColorDesc += " " + styleObj.ttColor[i].fabDesc; } 

             $(".selectedColor").html(vSelectedColorDesc);
             if(vSelectedSize == "" || checkStock(styleObj.ttColor[i].colorCode,vSelectedSize,vDefaultWhse) > 0)
              vClass = "colorOuterSelected";
             else
              continue; //vClass = "colorOuterOOSSelected";
           } else {
             if(vSelectedSize == "" || checkStock(styleObj.ttColor[i].colorCode,vSelectedSize,vDefaultWhse) > 0)
               vClass = "colorOuter";
             else
               continue; //vClass = "colorOuterOOS";
           }
           vColorDesc = styleObj.ttColor[i].description;  
           if (styleObj.ttColor[i].fabDesc != "" && styleObj.ttColor[i].fabDesc != null)
             { vColorDesc += " " + styleObj.ttColor[i].fabDesc; }

           vColorHTML += '<a href="' + styleObj.ttColor[i].imageZm + '" rel="zoom1" rev="' + styleObj.ttColor[i].imageLg + '" ' +
                         'onmouseover="swapImg(\'' + styleObj.ttColor[i].imageLg + '\');' +
                         '$(\'.selectedColor\').html(\'' + vColorDesc + '\'); ';

           if(vClass == "colorOuterOOS" || vClass == "colorOuterOOSSelected")
             vColorHTML += 'showOOSMsg(\'' + styleObj.ttColor[i].description + '\',vSelectedSizeDesc);';

           vColorHTML += ' " onmouseout="restoreImg();$(\'.selectedColor\').html(vSelectedColorDesc); ';

           if(vClass == "colorOuterOOS" || vClass == "colorOuterOOSSelected")
             vColorHTML += 'hideOOSMsg();';

           vColorHTML += ' " onclick="setColor(\'' + styleObj.ttColor[i].colorCode + '\',\'\'); return false;">';

           if (vDispColorDesc) {                                   /* XXX */
             vColorHTML += '<div class="swatchbox">';
             vColorHTML += '<div class="swatchcenter">';
           }

           vColorHTML += '<div class="' + vClass + '" colorcode="' + styleObj.ttColor[i].colorCode + '">';

           if (styleObj.ttColor[i].imageSw == "" || styleObj.ttColor[i].imageSw == null ){
             if (styleObj.ttColor[i].hexColor != "" && styleObj.ttColor[i].hexColor != null && styleObj.ttColor[i].hexColor.indexOf(",") != -1){ //Check for multiple hex values
               vHexArray = styleObj.ttColor[i].hexColor.split(",");
               switch(vHexArray.length){
                 case 2:
                   vHexPerArray = vHexPercent2.split(",");
                   break;
                 case 3:
                   vHexPerArray = vHexPercent3.split(",");
                   break;
                 case 4:
                   vHexPerArray = vHexPercent4.split(",");
                   break;
                 default:
                   vHexPerArray = vHexPercent2.split(",");
               }
               vColorHTML += '<div class="colorInner">';
               for(var j=0; j<vHexArray.length; j++){
                  vColorHTML += '<div style="margin:0px;padding:0px;float:left;height:100%;width:' + vHexPerArray[j] + '%;background-color:#' + vHexArray[j] + ';"></div>';               
               }
               vColorHTML += '</div>';
             }
             else
               vColorHTML += '<div class="colorInner" style="background-color:#' + styleObj.ttColor[i].hexColor + ';"></div>';
           }
           else 
             vColorHTML += '<img src="' + styleObj.ttColor[i].imageSw + '" alt="" class="colorImg"/>';

           if (vDispColorDesc) {                                   /* XXX */
             vColorHTML += '</div>'; // colorOuter
             vColorHTML += '</div>'; // swatchcenter
             vColorHTML += '<span class="swatchboxtxt">' + styleObj.ttColor[i].description + '</span>';
           }

           vColorHTML += '</div></a>'; // swatchbox || colorOuter
         }
       }
       $(".prodColors").html(vColorHTML + "<div class='wishlistBtn'>" + vWishlistBtn + "</div>"); /* R010012 */
}
function displaySizes(){
   var vPackTypeDisplayed = false;
   var vSizeHTML = "";
   var vClass;
   $.each(styleObj.ttSize,function(i){
     if( checkItem(vSelectedColor,styleObj.ttSize[i].sizeCode) )
     { 
       if(vSelectedSize==styleObj.ttSize[i].sizeCode){
         if(checkStock(vSelectedColor,styleObj.ttSize[i].sizeCode,vDefaultWhse) > 0)
          vClass = "sizeOuterSelected";
         else
          vClass = "sizeOuterOOSSelected";
       }
       else{
         if(checkStock(vSelectedColor,styleObj.ttSize[i].sizeCode,vDefaultWhse) > 0)
           vClass = "sizeOuter";
         else
           vClass = "sizeOuterOOS";
       }
       if(this.sizeCode=="Y" && hasPacking(vSelectedColor,styleObj.ttSize[i].sizeCode) ){
         vClass = "sizeOuterPacking";
         vSizeHTML += '<div class="packSize">';
         vSizeHTML += '<div sizecode="' + styleObj.ttSize[i].sizeCode + '"' +
                      'onmouseover="$(\'.selectedSize\').html(\'' + styleObj.ttSize[i].description + '\'); ';

         if(vClass == "sizeOuterOOS" || vClass == "sizeOuterOOSSelected")
           vSizeHTML += 'showOOSMsg(vSelectedColorDesc,\'' + styleObj.ttSize[i].description + '\');';

         vSizeHTML += '" onmouseout="$(\'.selectedSize\').html(vSelectedSizeDesc); ';

         if(vClass == "sizeOuterOOS" || vClass == "sizeOuterOOSSelected")
           vSizeHTML += 'hideOOSMsg();';
         //if (!vIsRetail) { vClass += " sizeb2b"; } APY make selectable
         vSizeHTML += '" onclick="setSize(\'' + styleObj.ttSize[i].sizeCode + '\',this)" class="' + vClass + '" >' +
                      '<div class="sizeInner">' + styleObj.ttSize[i].description + '</div>' +
                      '</div>';
         vSizeHTML += '<div class="sizePackDesc">Pre-Packed Size(s) per Case</div>'; /* R010011 */

         $(styleObj.ttItem).each(function(){
            if(this.sizeCode==styleObj.ttSize[i].sizeCode&&this.colorCode==vSelectedColor){
              vSizeHTML += '<div class="packSizeDetails"><table class="packingTable" cellspacing="0" cellpadding="0" border="1"><tr>';
              $(this.ttItemPack).each(function(){
                vSizeHTML += '<td class="packSizeHead">' + this.sizeDesc + '</td>';
              });
              vSizeHTML += '</tr><tr>';
              $(this.ttItemPack).each(function(){
                vSizeHTML += '<td class="packSizeQty">' + this.qty + '</td>';
              });
              vSizeHTML += '</tr></table>';
              vSizeHTML += '<div class="packItemDesc">*' + this.description + '</div>';
              vSizeHTML += '</div>';
              vSizeHTML += '<div class="packColorDesc">*Color(s) for this packing is/are included above</div>';
            }
         });
         vSizeHTML += '</div>';
       }
       else{
         vSizeHTML += '<div sizecode="' + styleObj.ttSize[i].sizeCode + '"' +
                      'onmouseover="$(\'.selectedSize\').html(\'' + styleObj.ttSize[i].description + '\'); ';

         if(vClass == "sizeOuterOOS" || vClass == "sizeOuterOOSSelected")
           vSizeHTML += 'showOOSMsg(vSelectedColorDesc,\'' + styleObj.ttSize[i].description + '\');';

         vSizeHTML += '" onmouseout="$(\'.selectedSize\').html(vSelectedSizeDesc); ';

         if(vClass == "sizeOuterOOS" || vClass == "sizeOuterOOSSelected")
           vSizeHTML += 'hideOOSMsg();';
         //if (!vIsRetail) { vClass += " sizeb2b"; } APY make selectable
         vSizeHTML += '" onclick="setSize(\'' + styleObj.ttSize[i].sizeCode + '\',this)" class="' + vClass + '" >' +
                      '<div class="sizeInner">' + styleObj.ttSize[i].description + '</div>' +
                      '</div>';
       }
     }
   });
   $(".prodSizes").html(vSizeHTML);
   if($(".stockLevels").size() < 1)
     $(".prodSizes").after("<div class='stockLevels'></div>"); //APY - Added to display whse stock
}
function displayMultiSizes(){
   var vSizeHTML = "";
   var vClass;
   $.each(styleObj.ttSizeScale,function(i){
      vSizeHTML += "<div class='sizeHeading'>" + styleObj.ttSizeScale[i].description + "</div><div class='prodSizes'>";
      $.each(styleObj.ttSizeScale[i].ttSizeScaleValue,function(j){
         if(multiSizeCheckItem(i,styleObj.ttSizeScale[i].ttSizeScaleValue[j].dimValue)){
            if(vSelectedMultiSize[i]==styleObj.ttSizeScale[i].ttSizeScaleValue[j].dimValue){
              if(multiSizeCheckStock(i,styleObj.ttSizeScale[i].ttSizeScaleValue[j].dimValue) > 0)
                vClass = "sizeOuterSelected" + i;
              else
                vClass = "sizeOuterOOSSelected" + i;
            }
            else{
              if(multiSizeCheckStock(i,styleObj.ttSizeScale[i].ttSizeScaleValue[j].dimValue) > 0)
                vClass = "sizeOuter" + i;
              else
                vClass = "sizeOuterOOS" + i;
            }
            vSizeHTML += '<div class="' + vClass + '" dimValue="' + styleObj.ttSizeScale[i].ttSizeScaleValue[j].dimValue + '"' +
                         'onmouseover="$(\'.selectedSize' + i + '\').html(\'' + styleObj.ttSize[i].description + '\'); ';
            if(vClass == "sizeOuterOOS" + i || vClass == "sizeOuterOOSSelected" + i)
              vSizeHTML += 'showMultiSizeOOSMsg(vSelectedColorDesc,' + i + ',\'' + styleObj.ttSizeScale[i].ttSizeScaleValue[j].dimValue + '\');';

            vSizeHTML += '" onmouseout="$(\'.selectedSize' + i + '\').html(vSelectedSizeDesc); ';

            if(vClass == "sizeOuterOOS" + i || vClass == "sizeOuterOOSSelected" + i)
              vSizeHTML += 'hideOOSMsg();';

            vSizeHTML += '" onclick="setMultiSize(' + i + ',\'' + styleObj.ttSizeScale[i].ttSizeScaleValue[j].dimValue + '\',this)">' +
                         '<div class="sizeInner">' + styleObj.ttSizeScale[i].ttSizeScaleValue[j].description + '</div>' +
                         '</div>';
         }    
      }); 
      vSizeHTML += "</div>";   
   }); 
   $(".prodSizes").html(vSizeHTML);
}
function switchComp(vType,curObj){
 $(".compTypeContainerH").hide();
 $(".compSelected").removeClass("compSelected");
 $(curObj).addClass("compSelected");
 vType = "#" + vType;
 $(vType).fadeIn(250);
}
function displayComprables(){
  var vCompHTML = "";
  var vCompPrice = 0.00;
  if (vComparableDir == "horizontal"){
    $.each(styleObj.ttCompType,function(i){
      if (i == 0)
        vCompHTML += "<div class='compTypeTitleH compSelected' onclick='switchComp(\"comp" + i + "\",this);'>" + styleObj.ttCompType[i].typeDesc + "</div>";
      else
        vCompHTML += "<div class='compTypeTitleH' onclick='switchComp(\"comp" + i + "\",this);'>" + styleObj.ttCompType[i].typeDesc + "</div>";
    });
    $.each(styleObj.ttCompType,function(i){
      vCompHTML += "<div class='compTypeContainerH' id='comp" + i + "'";
      if(i!=0) vCompHTML += " style='display:none;'";
      vCompHTML += ">";
      $.each(styleObj.ttCompType[i].ttCompDetail,function(j){
        vCompHTML += "<div class='compDetailH' onclick='goStyle(\"" + styleObj.ttCompType[i].ttCompDetail[j].compStyleCode + "\");'>";
        vCompHTML += "<img class='compDetailImg' src='" + styleObj.ttCompType[i].ttCompDetail[j].compImage + "' alt='" + styleObj.ttCompType[i].ttCompDetail[j].compStyleCode + "' />";
        vCompHTML += "<div class='compDetailDesc'>" + styleObj.ttCompType[i].ttCompDetail[j].compDesc + "</div>";
        if (styleObj.ttCompType[i].ttCompDetail[j].regPrice){  /* R010001 */
          if(styleObj.ttCompType[i].ttCompDetail[j].salePrice <= 0 ){
            vCompPrice = parseFloat(styleObj.ttCompType[i].ttCompDetail[j].regPrice);
            vCompPrice =  vCompPrice.toFixed(2);
            vCompHTML += '<div class="compDetailRegPriceDiv"><span class="compDetailRegPriceLabel"></span><span class="compDetailRegPrice">$' + vCompPrice + '</span></div>';
          }
          else{
            vCompPrice = parseFloat(styleObj.ttCompType[i].ttCompDetail[j].regPrice);
            vCompPrice =  vCompPrice.toFixed(2);
            vCompHTML += '<div class="compDetailRegPriceDiv"><span class="compDetailRegPriceLabel"></span><span class="compDetailRegPriceStrike">$' + vCompPrice + '</span></div>';
            vCompPrice = parseFloat(styleObj.ttCompType[i].ttCompDetail[j].salePrice);
            vCompPrice =  vCompPrice.toFixed(2);
            vCompHTML += '<div class="compDetailSalePriceDiv"><span class="compDetailSalePriceLabel">' + styleObj.ttCompType[i].ttCompDetail[j].saleLabel + '</span><span class="compDetailSalePrice">$' + vCompPrice + '</span></div>';
          }
        }
        vCompHTML += "</div>";
      });
      vCompHTML += "</div>";
    });
    $(".prodComparable").addClass("prodComparableH");
  }
  else { // vComparableDir == "vertical" (default)
    $.each(styleObj.ttCompType,function(i){
      vCompHTML += "<div class='compTypeTitle'>" + styleObj.ttCompType[i].typeDesc + "</div>";
      vCompHTML += "<div class='compTypeContainer'>";
      $.each(styleObj.ttCompType[i].ttCompDetail,function(j){
        vCompHTML += "<div class='compDetail' onclick='goStyle(\"" + styleObj.ttCompType[i].ttCompDetail[j].compStyleCode + "\");'>";
        vCompHTML += "<img class='compDetailImg' src='" + styleObj.ttCompType[i].ttCompDetail[j].compImage + "' alt='" + styleObj.ttCompType[i].ttCompDetail[j].compStyleCode + "' />";
        vCompHTML += "<div class='compDetailDesc'>" + styleObj.ttCompType[i].ttCompDetail[j].compDesc + "</div>";
        if (styleObj.ttCompType[i].ttCompDetail[j].regPrice){  /* R010001 */
          if(styleObj.ttCompType[i].ttCompDetail[j].salePrice <= 0 ){
            vCompPrice = parseFloat(styleObj.ttCompType[i].ttCompDetail[j].regPrice);
            vCompPrice =  vCompPrice.toFixed(2);
            vCompHTML += '<div class="compDetailRegPriceDiv"><span class="compDetailRegPriceLabel"></span><span class="compDetailRegPrice">$' + vCompPrice + '</span></div>';
          }
          else{
            vCompPrice = parseFloat(styleObj.ttCompType[i].ttCompDetail[j].regPrice);
            vCompPrice =  vCompPrice.toFixed(2);
            vCompHTML += '<div class="compDetailRegPriceDiv"><span class="compDetailRegPriceLabel"></span><span class="compDetailRegPriceStrike">$' + vCompPrice + '</span></div>';
            vCompPrice = parseFloat(styleObj.ttCompType[i].ttCompDetail[j].salePrice);
            vCompPrice =  vCompPrice.toFixed(2);
            vCompHTML += '<div class="compDetailSalePriceDiv"><span class="compDetailSalePriceLabel">' + styleObj.ttCompType[i].ttCompDetail[j].saleLabel + '</span><span class="compDetailSalePrice">$' + vCompPrice + '</span></div>';
          }
        }
        vCompHTML += "</div>";
      });
      vCompHTML += "</div>";
    });
    $(".prodDesc").width($(".prodDesc").width() - $(".prodComparable").outerWidth() ); 
    $(".prodComparable").removeClass("prodComparableH");
  }
  $(".prodComparable").html(vCompHTML);
  $(".prodComparable").show();
}
function goStyle(vStyleCode,vStyleColor){
   golink("b2c/product.w?product=" + vStyleCode + "&color=" + vStyleColor + vBrowseParam,"main"); 
}
//========--------   Color/Size Selection   --------========\\
function setSize(vSizeCode,vThis){
  if(vSizeCode=="none" || !vIsRetail ) { return; }
  $(".cartError").html("");
  vSelectedSize = vSizeCode;
  if(document.getElementById("size" + vSelectedProd))
    document.getElementById("size" + vSelectedProd).value = vSizeCode;
  $(".sizeOuterSelected").attr("class","sizeOuter");
  $(".sizeOuterOOSSelected").attr("class","sizeOuterOOS");
  if(checkStock(vSelectedColor,vSelectedSize,vDefaultWhse) > 0)
    vClass="sizeOuterSelected";
  else
    vClass="sizeOuterOOSSelected";
  if(vThis == "")
    $("div[sizeCode='" + vSizeCode + "']").addClass(vClass);
  else
    vThis.className=vClass;

  $.each(styleObj.ttSize,function(i){ 
    if (styleObj.ttSize[i].sizeCode == vSizeCode) {              
      vSelectedSizeDesc = styleObj.ttSize[i].description;
      $(".selectedSize").html(vSelectedSizeDesc);
    }
  });

  if (vSelectedSize != "" && vSelectedColor != "")
  { priceItem(vSelectedColor,vSelectedSize); }

  displayColors();
}
function setColor(vColorCode,vThis,vImgSwitch){
  var newLgImg;
  var newZmImg;
  $(".cartError").html("");
  if(vColorCode=="none") { return; }
  if(document.getElementById("#imgLibDiv"))
      $("#imgLibDiv").hide();
  vSelectedColor = vColorCode;
  if(document.getElementById("color" + vSelectedProd))
    document.getElementById("color" + vSelectedProd).value = vColorCode;

  $(styleObj.ttColor).each(function(i){
      if(styleObj.ttColor[i].colorCode == vColorCode){
         newLgImg = styleObj.ttColor[i].imageLg;
         newZmImg = styleObj.ttColor[i].imageZm;
         if(styleObj.ttColor[i].attach != "" && styleObj.ttColor[i].attach != null ){
           $("#packDetailLink").attr("href","javascript: viewPacking('" + styleObj.ttColor[i].attach + "');"); /* R010002 */      
           $(".packDetail").show();
         }
         else 
           $(".packDetail").hide();
         return;
      }
  });
  if(vImgSwitch)
     swapImg(newLgImg);
  if(newZmImg != "" && newZmImg != null){
    $(".MagicZoomBigImageCont div > img").attr("src",newZmImg); //hack to update zoom image
  }
  vLockedImg = $(".imageCell").attr("src");

  $(".colorOuterSelected").attr("class","colorOuter");
  $(".colorOuterOOSSelected").attr("class","colorOuterOOS");
  if(vSelectedSize == "" || checkStock(vSelectedColor,vSelectedSize,vDefaultWhse) > 0)
    vClass="colorOuterSelected";
  else
    vClass="colorOuterOOSSelected";
  if(vThis == "")
    $("div[colorCode='" + vColorCode + "']").addClass(vClass);
  else
    vThis.className=vClass;

  $.each(styleObj.ttColor,function(i){ 
    if (styleObj.ttColor[i].colorCode == vColorCode) {              
      vColorObj = styleObj.ttColor[i];
    }
  });

  vSelectedColorDesc = vColorObj.description;  
  if (vColorObj.fabDesc != "" && vColorObj.fabDesc != null)
    { vSelectedColorDesc += " " + vColorObj.fabDesc; }
  $(".selectedColor").html(vSelectedColorDesc);

  if (vSelectedSize != "" && vSelectedColor != "")
  { priceItem(vSelectedColor,vSelectedSize); }

  if(styleObj.ttSizeScale && styleObj.ttSizeScale[1] != null)
    displayMultiSizes();
  else
    displaySizes();  
  if(styleObj.ttAltView) 
    switchAltViews(vColorCode);
  
  displayStock();

}
function displayStock(){
  var vTmpHTML = "";
  $.each(styleObj.ttItem,function(i){ 
    if(styleObj.ttItem[i].colorCode == vSelectedColor && styleObj.ttItem[i].sizeCode == vSelectedSize){
      $.each(styleObj.ttItem[i].ttItemStock,function(j){ 
        if (styleObj.ttItem[i].ttItemStock[j].availQty > 0)
          if (styleObj.ttItem[i].ttItemStock[j].availQty >= 720)
          vTmpHTML += "<div class='whseDesc'>" + styleObj.ttItem[i].ttItemStock[j].whseDesc + "</div><div class='whseStock'>720+</div><br/>";
         else
          vTmpHTML += "<div class='whseDesc'>" + styleObj.ttItem[i].ttItemStock[j].whseDesc + "</div><div class='whseStock'>" + styleObj.ttItem[i].ttItemStock[j].availQty + "</div><br/>";
      }); 
    }
  }); 
  if (vTmpHTML != "")
    vTmpHTML = "Available Case(s):<br/>" + vTmpHTML;
  $(".stockLevels").html(vTmpHTML);
}
function setMultiSize(vIndex,vDimValue,vThis){
  if(!vIsRetail ) { return; }
  var vFullSizeCode = "";
  var vFullSize = true;

  vSelectedMultiSize[vIndex] = vDimValue;
  $(".sizeOuterSelected" + vIndex).attr("class","sizeOuter" + vIndex);
  vThis.className="sizeOuterSelected" + vIndex;
  $.each(vSelectedMultiSize, function(i){
     vFullSizeCode += vSelectedMultiSize[i];
     if(vSelectedMultiSize[i] == "")
       vFullSize = false;
  });

  if(vFullSize){
    vSelectedSize = vFullSizeCode;
    $.each(styleObj.ttSize,function(i){ 
      if (styleObj.ttSize[i].sizeCode == vSelectedSize) {              
        vSelectedSizeDesc = styleObj.ttSize[i].description;
        $(".selectedSize").html(vSelectedSizeDesc);
      }
    });
    displayColors();
  }
  else
    vSelectedSize = "";

  if (vSelectedSize != "" && vSelectedColor != "")
  { priceItem(vSelectedColor,vSelectedSize); }
  displayMultiSizes();
}
//========--------   size / stock checking   --------========\\
function checkStock(vColorCode,vSizeCode,vWhse){
  var vAvailQty = 0;
  $.each(styleObj.ttItem,function(i){ 
    if(styleObj.ttItem[i].colorCode == vColorCode && styleObj.ttItem[i].sizeCode == vSizeCode) {  
      if (styleObj.ttItem[i].availQty == null)
        vAvailQty = 1; 
      else
        vAvailQty = styleObj.ttItem[i].availQty; 
      return; // returns from $.each function
    }
  }); 
  return vAvailQty;
}
function multiSizeCheckStock(vIndex,vDimValue,vWhse){
  var vAvailQty = 0;
  var vTempSize = "";
  var vSize     = "";

  //return true if no other size dimension is selected
  $.each(vSelectedMultiSize,function(k){ 
     if (k != vIndex)
        vTempSize += vSelectedMultiSize[k];
     if (k == vIndex)
        vSize += vDimValue;
     else
        vSize += vSelectedMultiSize[k];
  });
  if (vTempSize == "")
  { return 1; }

  $.each(styleObj.ttItem,function(i){ 
    if(styleObj.ttItem[i].colorCode == vSelectedColor && styleObj.ttItem[i].sizeCode == vSize) {  
      if (styleObj.ttItem[i].availQty == null)
        vAvailQty = 1; 
      else
        vAvailQty = styleObj.ttItem[i].availQty; 
      return; // returns from $.each function
    }
  }); 
  return vAvailQty;
}
function checkItem(vColorCode,vSizeCode){
  var vFound = false;
  $.each(styleObj.ttItem,function(k){ 
    if(styleObj.ttItem[k].colorCode == vColorCode && styleObj.ttItem[k].sizeCode == vSizeCode)
    { vFound = true; return; }
  }); 
  return vFound;
}
function multiSizeCheckItem(vIndex,vDimValue){
  var vFound = false;
  var vTempSize = "";
  var vSize     = "";

  //return true if no color or other size dimension is selected
  if(vSelectedColor == "")
  { return true; }
  $.each(vSelectedMultiSize,function(k){ 
     if (k != vIndex)
        vTempSize += vSelectedMultiSize[k];
     if (k == vIndex)
        vSize += vDimValue;
     else
        vSize += vSelectedMultiSize[k];
  });
  if (vTempSize == "")
  { return true; }
  return checkItem(vSelectedColor,vSize);
}
//========----   Feature Icon Display  ----========\\
function displayFeatIcons(){
  if(styleObj.ttFeatIcon){
    $.each(styleObj.ttFeatIcon,function(i){ 
     $(".prodIcons").append("<img src='" + styleObj.ttFeatIcon[i].icon + "' class='featureIcon' alt='" + styleObj.ttFeatIcon[i].description + "' />");
    });
  }
}
//========--------   Update Price   --------========\\
function priceItem(vColorCode,vSizeCode){
  var newPrice = "";
  $.each(styleObj.ttItem,function(k){ 
    if(styleObj.ttItem[k].colorCode == vColorCode && styleObj.ttItem[k].sizeCode == vSizeCode){ 
      if(styleObj.ttItem[k].salePrice <= 0){          /* R010007 */ /* R010009 */
        $(".salePriceDiv").hide();
        $(".regPriceStrike").addClass("regPrice");
        $(".regPriceStrike").removeClass("regPriceStrike");
        newPrice = parseFloat(styleObj.ttItem[k].regPrice);        
        $(".regPrice").html("$" + newPrice.toFixed(2));
        vSelectedPrice = newPrice.toFixed(2);
      }
      else{
        newPrice = parseFloat(styleObj.ttItem[k].regPrice);        
        $(".regPriceLabel").html(vRegOnSaleLabel);
        $(".regPriceStrike").addClass("regPrice");
        $(".regPriceStrike").removeClass("regPriceStrike");
        $(".regPrice").html("$" + newPrice.toFixed(2));
        newPrice = parseFloat(styleObj.ttItem[k].salePrice);        
        vSelectedPrice = newPrice.toFixed(2);
        $(".salePrice").text("$" + newPrice.toFixed(2));
        $(".salePriceDiv").show();
        if ($(".salePriceQty").size() < 1){
          if(styleObj.ttItem[k].sizeCode == "Y" || styleObj.ttItem[k].packQty > 1){
            newPrice = parseFloat(styleObj.ttItem[k].salePrice / styleObj.ttItem[k].packQty);
            $(".salePrice").after("<span class='salePriceQty'> per case, $" + newPrice.toFixed(2) + " per piece</span>");   /* R010006 */
          }
          else
           $(".salePrice").after("<span class='salePriceQty'> per piece</span>");   /* R010006 */
        }
        else{
          if(styleObj.ttItem[k].sizeCode == "Y" || styleObj.ttItem[k].packQty > 1){
            newPrice = parseFloat(styleObj.ttItem[k].salePrice / styleObj.ttItem[k].packQty);
            $(".salePriceQty").html(" per case, $" + newPrice.toFixed(2) + " per piece");  /* R010006 */
          }
          else
           $(".salePriceQty").html(" per piece");   /* R010006 */
        }
      }
      if ($(".priceQty").size() < 1){
        if(styleObj.ttItem[k].sizeCode == "Y" || styleObj.ttItem[k].packQty > 1){
          newPrice = parseFloat(styleObj.ttItem[k].regPrice / styleObj.ttItem[k].packQty);
          $(".regPrice").after("<span class='priceQty'> per case, $" + newPrice.toFixed(2) + " per piece</span>");   /* R010006 */
        }
        else
         $(".regPrice").after("<span class='priceQty'> per piece</span>");   /* R010006 */
      }
      else{
        if(styleObj.ttItem[k].sizeCode == "Y" || styleObj.ttItem[k].packQty > 1){
          newPrice = parseFloat(styleObj.ttItem[k].regPrice / styleObj.ttItem[k].packQty);
          $(".priceQty").html(" per case, $" + newPrice.toFixed(2) + " per piece");  /* R010006 */
        }
        else
         $(".priceQty").html(" per piece");   /* R010006 */
      }
      if(styleObj.ttItem[k].salePrice > 0 && vIsLoggedIn){           /* R010007 */ /* R010009 */
         $(".regPrice").addClass("regPriceStrike");
         $(".regPrice").removeClass("regPrice");
      }
      return;
    }
  }); 
}
//========--------   MouseOvers   --------========\\
function swapImg(vNewImg,vNoLock){
  if(vNewImg != "" ){
    if(!vNoLock)
      vLockedImg = $(".imageCell").attr("src");
    $(".imageCell").attr("src",vNewImg);
  }
}
function restoreImg(){
  var vCurImg = $(".imageCell").attr("src"); /* R010012 */
  if(vLockedImg != "" && vCurImg != vLockedImg && vCurImg != "/missing_large.gif") /* R010012 */
  { $(".imageCell").attr("src",vLockedImg); $(".colorArrow").css("z-index",999); }
}
function showOOSMsg(vColor,vSize){
  var vPosition   = $(".imgUpper").position();

  $(".imgMessageText").html(vColor + " is currently out of stock in " + vSize);

  var vTopOffset  = ( $(".jqzoom").outerHeight() - $("#imgMessage").outerHeight() - ( $("#imgMessage").outerHeight()/2) ); 
  var vLeftOffset = ( $(".imgUpper").outerWidth() - $("#imgMessage").outerWidth() )/2; 

  $("#imgMessage").css("top",vPosition.top + vTopOffset);
  $("#imgMessage").css("left",vPosition.left + vLeftOffset);
  $("#imgMessage").show();
}
function showMultiSizeOOSMsg(vColor,vIndex,vDimValue){
  var vSize     = "";
  var vSizeDesc = "";
  $.each(vSelectedMultiSize,function(k){ 
     if (k == vIndex)
        vSize += vDimValue;
     else
        vSize += vSelectedMultiSize[k];
  });
  $.each(styleObj.ttSize,function(i){  
     if(styleObj.ttSize[i].sizeCode == vSize){
        vSizeDesc = styleObj.ttSize[i].description;
        return;
     }
  });
  showOOSMsg(vColor,vSizeDesc);
}
function hideOOSMsg(){
  $("#imgMessage").hide();
}
/****** Retail Add to Cart ******/
function retailAddToCart(vProduct,vColor,vSize,vOrderType) 
{
  var vOrderQty = $("#qty").attr("value");

  if(vColor == "" || vColor == "none" || vColor == null)
  {
    $(".cartError").html("Please choose a color to continue.");
    return;
  }
  if(vSize == "" || vSize == "none" || vSize == null)
  {
    $(".cartError").html("Please choose a size to continue.");
    return;
  }
  if (vOrderQty == "" )
  {
    $(".cartError").html("Please enter a quantity before adding item(s) to shopping bag.");
    return;
  }
  if (isNumeric(vOrderQty) == false)
  {
    $(".cartError").html("Please enter a numeric quantity before adding item(s) to shopping bag.");
    $("#qty").attr("value","")
    $("#qty").focus();
    return;
  }
  if (vOrderQty <= 0)
  {
    $(".cartError").html("Please enter a quantity before adding item(s) to shopping bag.");
    return;
  }
  var invAvail = parseInt(checkStock(vColor,vSize));
  if(invAvail < vOrderQty)
  {
    if (invAvail <= 0)
     $(".cartError").html("The selected item is currently out of stock.");
    else{
     var alertMsg = "You have ordered " + vOrderQty + " however only ";
     if (invAvail == 1)
       alertMsg += "1 is available.";
     else
       alertMsg += invAvail + " are available.";
     alert(alertMsg);
    }
    if (invAvail <= 0 )
       invAvail = 1;
    $("#qty").attr("value",invAvail)
    $("#qty").focus();
    return;
  }
  addToCart(vProduct,vColor,vSize,vOrderQty,vOrderType);
}
/*===---- Retail Functions ----===*/
function clearmessage(){
  $(".cartError").html("");
}
function enterCheck(event,id)
{
  if(event.keyCode == 13)
  {
    $("#retailAddToCart").click();
    return false;
  }
  return true;
}

function bookmarkMe(){
  if ((navigator.appName == "Microsoft Internet Explorer") && (parseInt(navigator.appVersion) >= 4)) {
  window.external.AddFavorite(location.href,document.title);
  }
  else if (navigator.appName == "Netscape") {
    window.sidebar.addPanel(document.title,location.href,"");
  }
  else {
   if(agt.indexOf("opera")!= -1)
    alert("Press CTRL-T to bookmark this page.");
   else
    alert("Press CTRL-D to bookmark this page.");
  }
}

function openBlogText(vDescription,vImgPth,vFullDesc){
 if (styleObj){
   if (styleObj.description)
     vDescription = styleObj.description;
   if (styleObj.fullDesc != "" && styleObj.fullDesc != null)
     vFullDesc = styleObj.fullDesc;
   if (styleObj.imageTh != "" && styleObj.imageTh != null)
     vImgPth = styleObj.imageTh;
 }
 var vBlogHTML = "<table cellspacing='0' cellpadding='0' border='0' class='blogTable'>" + 
                 "<tr><td colspan='2' style='font-weight:bold;font-size:12pt;'>" + document.title +
                 "</td></tr><tr><td rowspan='2'><img src='" + vImgPth + "' alt=''></td>" +
                 "<td style='text-align:center;vertical-align:top;height:25px;'><a style='font-weight:bold;font-size:11pt;' href='" + hostURL + appURL + "/site.w?location=b2c/product.w&product=" + vSelectedProd + 
                 "&frames=no&target=main&sponsor=" + vSponsorCode + "'>" + vDescription + "</a>" +
                 "</td><tr><td style='vertical-align:top;'>" + vFullDesc + "</td></tr></table>";
 vBlogHTML = "<div style='width:400px;text-align:center;margin:none;pading:none;'><table cellspacing='0' cellpadding='0' border='0' style='width:400px'><tr><td class='blogInstructions' style='text-align:center;'>Copy and paste the HTML text below to share this product on your blog or online profile.</td></tr></table>" + vBlogHTML + "<br/> <textarea>" + vBlogHTML + "</textarea></div>";
 if(!document.getElementById("blogPost")){
   $("body").append("<div id='blogPost' title='Blog HTML' style='width:400px;' />");
   $("#blogPost").html(vBlogHTML);
   $("#blogPost").dialog({width:450,height:475,resizable:false} );
 }
 else
   $("#blogPost").dialog("open");
}

function showAltViews(vColorCode){
 var vAltViewHTML = "";
 if(document.getElementById("altViews")){
   $.each(styleObj.ttAltView,function(i){
     if(styleObj.ttAltView[i].colorCode == vColorCode){
        vAltViewHTML += "<div id=\"altView" + styleObj.ttAltView[i].altId + "\" colorCode=\"" + vColorCode + "\" onmouseover=\"swapImg('" + styleObj.ttAltView[i].imageLg + "',true);\" onmouseout=\"restoreImg();\">" +
                       "<img src='" + styleObj.ttAltView[i].imageSm + "' alt='' />" +
                       "</div>";
     }
   });
   $("#altViews").html(vAltViewHTML);
   if ($(".altViewSide"))
     $(".prodDesc").width($(".prodDesc").width() - $(".altViewSide").outerWidth() );
   $("#altViews").css("display","block");
 }
}
function switchAltViews(vColorCode){
 var vAltViewHTML = "";
 var vAltViewId;
 if(document.getElementById("altViews")){
   $.each(styleObj.ttAltView,function(i){
     if(styleObj.ttAltView[i].colorCode == vColorCode){
       vAltViewId = "#altView" + styleObj.ttAltView[i].altId;
       vAltViewObj = $(vAltViewId);
       if(vAltViewObj){
           $(vAltViewObj).html("<img src='" + styleObj.ttAltView[i].imageSm + "' alt='' />")
                         .attr("colorCode",styleObj.ttAltView[i].colorCode);
           $(vAltViewObj).unbind("onmouseover");
           $(vAltViewObj).mouseover(function(){swapImg(styleObj.ttAltView[i].imageLg,true);});
       }
     }
   });
   //reset all non color specific altViews to defaults
   $.each(styleObj.ttAltView,function(i){
     if(styleObj.ttAltView[i].colorCode == ""){
       vAltViewId = "#altView" + styleObj.ttAltView[i].altId;
       vAltViewObj = $(vAltViewId);
       if(vAltViewObj && $(vAltViewObj).attr("colorCode") != vColorCode && $(vAltViewObj).attr("colorCode") != ""){
           $(vAltViewObj).html("<img src='" + styleObj.ttAltView[i].imageSm + "' alt='' />")
                         .attr("colorCode","");
           $(vAltViewObj).unbind("onmouseover");
           $(vAltViewObj).mouseover(function(){swapImg(styleObj.ttAltView[i].imageLg,true);});
       }
     }
   });
 }
}

function viewPacking(vAttach) { window.open(vAttach,"",""); } 

function hasPacking(vColor,vSize){ 
  var vReturn = false;
  $(styleObj.ttItem).each(function(){
     if(this.sizeCode==vSize&&this.colorCode==vColor&&this.ttItemPack)
       vReturn = true;
  });
  return vReturn;
} 
