/*** COPYRIGHT 2009  BY CUTSEY BUSINESS SYSTEMS LTD. - ALL RIGHTS RESERVED ***/
/* parts.js -- Parts.w Page Javascript                                       */
/*****************************************************************************/
/* 010001 10/19/09 APY- Fix redirect to product page for parts/acc items.    */
/* 010000 10/06/09 APY- Added to RTB, updated logic for search.              */
/*****************************************************************************/
var resultsMinHeight = 210;
if($.browser.msie) { resultsMinHeight += 8; }
/*****************************************************************************/ 
var getURLParam = function(strParamName){
 var strReturn = "";
 var strHref = window.location.href;
 var bFound=false;
 
 var cmpstring = strParamName + "=";
 var cmplen = cmpstring.length;

 if ( strHref.indexOf("?") > -1 ){
   var strQueryString = strHref.substr(strHref.indexOf("?")+1);
   var aQueryString = strQueryString.split("&");
   for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
     if (aQueryString[iParam].substr(0,cmplen)==cmpstring){
       var aParam = aQueryString[iParam].split("=");
       strReturn = aParam[1];
       bFound=true;
       break;
     }
     
   }
 }
 if (bFound==false) return "";
 return strReturn;
}
/*****************************************************************************/
function getParts(vProdNum) {
  var vCurrTime = new Date();
  var vCurrSecs  = (((vCurrTime.getHours() * 60) + vCurrTime.getMinutes()) * 60) + vCurrTime.getSeconds();
  var vNoCache = "&nocache=" + vCurrSecs;
  var vNoAcc = false;
  var vNoParts = false;
  $.ajax({
    type: "GET",
    url: hostDomain + appPath + "/olc/get-comparables.w?products=" + vProdNum + "&compType=" + vAccCode + "," + vPrtCode + "&frames=no&target=main&sponsor=" + fdmSponsor + vNoCache,
    dataType: "xml",
    complete: function(data) {
      var vAccHtml = "";
      var vPartsHtml = "";
      var vCompPrice = 0.00;

      var vTmpObj = $(data.responseXML).find("ttCompType[compType=" + vAccCode + "]");
      if(vTmpObj.length >= 1) { 
        vAccHtml = "<div class='cartMsg cartMsgACC'>&nbsp;</div><div class='titleOuter'><div class='titleH2'><h2 class='repTitle'>" + $(vTmpObj).attr("typeDesc") + "</h2></div>"; 
        vAccHtml += "<div class='titleAdd'><h2 class='repTitle'>Add To Cart</h2></div><div class='titleQty'><h2 class='repTitle'>Qty.</h2></div></div>"; 
        vAccHtml += "<div class='resultsOuter'>";
      }
      else { vNoAcc = true; }
      vTmpObj = $(data.responseXML).find("ttCompDetail[compType=" + vAccCode + "]");

      $.each(vTmpObj,function(){
        if($(this).attr("compImage")==""){$(this).attr("compImage","error");}
        vAccHtml += "<div class='resultsInner'><table cellspacing='0' cellpadding='0' border='0'><tr>";
        vAccHtml += "<td class='compProdImage'><a class='repLink' href='javascript:golink(\"b2c/product.w?product=" + $(this).attr("compStyleCode") + "\",\"main\");' ><img src='" + $(this).attr("compImage") + "' alt='" + $(this).attr("compDesc") + "' onerror='top.ImageError(this,\"thumb\")' /></a></td>";
        
        vAccHtml += "<td class='compProdDesc'><ul><li>" + $(this).attr("compDesc") + "<p>" + $(this).attr("compDescLong") + "</p></li></ul></td>";

        vAccHtml += "<td class='compProdPrice'>";
        if($(this).attr("salePrice") <= 0 ){
          vCompPrice = parseFloat($(this).attr("regPrice"));
          vCompPrice =  vCompPrice.toFixed(2);
          vAccHtml += '<div class="RegPriceDiv"><span class="regPrice">$' + vCompPrice + '</span></div>';
        }
        else{
          vCompPrice = parseFloat($(this).attr("regPrice"));
          vCompPrice =  vCompPrice.toFixed(2);
          vAccHtml += '<div class="RegPriceDiv"><span class="regPriceStrike">$' + vCompPrice + '</span></div>';
          vCompPrice = parseFloat($(this).attr("salePrice"));
          vCompPrice =  vCompPrice.toFixed(2);
          vAccHtml += '<div class="salePriceDiv"><span class="salePriceLabel">' + $(this).attr("saleLabel") + '</span><span class="salePrice">$' + vCompPrice + '</span></div>';
        }
        vAccHtml += "</td>";
        vAccHtml += "<td class='compProdAdd'><img class='addtocart' src='" + hostDomain + imgPath + "/buttons/Add-to-Bag.jpg' alt='Add to bag' onclick='setTopScroll(true);addToCart(\"" + $(this).attr("compStyleCode") + "\",\"\",\"\",document.getElementById(\"qty" + $(this).attr("compStyleCode") + "\").value,\"C\",\".cartMsgACC\");'/></td>";
        vAccHtml += "<td class='compProdQty'><input name='qty" + $(this).attr("compStyleCode") + "' id='qty" + $(this).attr("compStyleCode") + "' size='2' value='1' /></td>";
        vAccHtml += "</tr></table></div>";
      });
      if (vAccHtml != "")
        vAccHtml += "</div>";

      vTmpObj = $(data.responseXML).find("ttCompType[compType=" + vPrtCode + "]");
      if(vTmpObj.length >= 1) { 
        vPartsHtml = "<div class='cartMsg cartMsgPAR'>&nbsp;</div><div class='titleOuter'><div class='titleH2'><h2 class='repTitle'>" + $(vTmpObj).attr("typeDesc") + "</h2></div>"; 
        vPartsHtml += "<div class='titleAdd'><h2 class='repTitle'>Add To Cart</h2></div><div class='titleQty'><h2 class='repTitle'>Qty.</h2></div></div>"; 
        vPartsHtml += "<div class='resultsOuter'>";
      }
      else { vNoParts = true; }
      vTmpObj = $(data.responseXML).find("ttCompDetail[compType=" + vPrtCode + "]");

      $.each(vTmpObj,function(){
        if($(this).attr("compImage")==""){$(this).attr("compImage","error");}
        vPartsHtml += "<div class='resultsInner'><table cellspacing='0' cellpadding='0' border='0'><tr>";
        vPartsHtml += "<td class='compProdImage'><a class='repLink' href='javascript:golink(\"b2c/product.w?product=" + $(this).attr("compStyleCode") + "\",\"main\");' ><img src='" + $(this).attr("compImage") + "' alt='" + $(this).attr("compDesc") + "' onerror='top.ImageError(this,\"thumb\")' /></a></td>";
        vPartsHtml += "<td class='compProdDesc'><ul><li>" + $(this).attr("compDesc") + "<p>" + $(this).attr("compDescLong") + "</p></li></ul></td>";
        vPartsHtml += "<td class='compProdPrice'>";
        if($(this).attr("salePrice") <= 0 ){
          vCompPrice = parseFloat($(this).attr("regPrice"));
          vCompPrice =  vCompPrice.toFixed(2);
          vPartsHtml += '<div class="RegPriceDiv"><span class="regPrice">$' + vCompPrice + '</span></div>';
        }
        else{
          vCompPrice = parseFloat($(this).attr("regPrice"));
          vCompPrice =  vCompPrice.toFixed(2);
          vPartsHtml += '<div class="RegPriceDiv"><span class="regPriceStrike">$' + vCompPrice + '</span></div>';
          vCompPrice = parseFloat($(this).attr("salePrice"));
          vCompPrice =  vCompPrice.toFixed(2);
          vPartsHtml += '<div class="salePriceDiv"><span class="salePriceLabel">' + $(this).attr("saleLabel") + '</span><span class="salePrice">$' + vCompPrice + '</span></div>';
        }
        vPartsHtml += "</td>";
        vPartsHtml += "<td class='compProdAdd'><img class='addtocart' src='" + hostDomain + imgPath + "/buttons/Add-to-Bag.jpg' alt='Add to bag' onclick='setTopScroll(true);addToCart(\"" + $(this).attr("compStyleCode") + "\",\"\",\"\",document.getElementById(\"qty" + $(this).attr("compStyleCode") + "\").value,\"C\",\".cartMsgPAR\");'/></td>";
        vPartsHtml += "<td class='compProdQty'><input name='qty" + $(this).attr("compStyleCode") + "' id='qty" + $(this).attr("compStyleCode") + "' size='2' value='1' /></td>";
        vPartsHtml += "</tr></table></div>";
      });
      if (vPartsHtml != "")
        vPartsHtml += "</div>";

      $("#prodResults").html(vAccHtml + vPartsHtml);

      if (vNoAcc && vNoParts)
      { $("#prodResults").html("<h2 class='repNotFound'>No replacement parts or accessories found for entered model/part #</h2>");  }
      
      // Height Fix for results
      $(".resultsOuter").each(function(){
        var vTmpHeight = 0;
        var hObj = $(this).find(".resultsInner")
        $.each(hObj,function(){
          vTmpHeight += $(this).outerHeight();          
        });        

        if (vTmpHeight > 0 && vTmpHeight < resultsMinHeight)
        { if($.browser.msie) $(this).height(vTmpHeight + 8); else $(this).height(vTmpHeight);  }
      });

    }
  });
}
/*****************************************************************************/
function showHelp() { 
  if(document.getElementById("inputHelp")) { 
    $("#inputHelp").dialog("destroy");
  }  
  $("body").append('<div id="inputHelp" title="How to find your item #"><div style="margin:0;width:400px;height:325px;"><img src="/europro/images/HowToFindFAQPage.png" alt="" /><br/><img src="/europro/images/Labels.jpg" alt="" /></div></div>'); 
  $("#inputHelp").dialog({ width:500,height:350,resizable:false });
  $("#inputHelp").css("height",350);
}
/*****************************************************************************/
function showAllProd() { 
  var vCurrTime = new Date();
  var vCurrSecs  = (((vCurrTime.getHours() * 60) + vCurrTime.getMinutes()) * 60) + vCurrTime.getSeconds();
  var vNoCache = "&nocache=" + vCurrSecs;

  $.ajax({
    type: "GET",
    url: hostDomain + appPath + "/b2c/parts-parent.w?compType=" + vAccCode + "," + vPrtCode + "&frames=no&target=main&sponsor=" + fdmSponsor + vNoCache,
    dataType: "xml",
    complete: function(data) {
      var vTmpObj = $(data.responseXML).find("parentProdRow");
      var vTmpHTML = "<div class='repTitle'>Products</div><table class='prodParent' cellspacing='0' cellpadding='0'>";
      $.each(vTmpObj,function(){
        vTmpHTML += "<tr><td class='pProdImg' onclick='setProd(\"" + $(this).attr("product") + "\")'>" +
                    "<img src='" + $(this).attr("prodImg") + "' alt='' onerror='top.ImageError(this,\"thumb\")' />" + 
                    "</td><td class='pProdDesc'>" + $(this).attr("product") + "<br/>" + $(this).attr("prodDesc") + "</td></tr>";
      });
      vTmpHTML += "</table>";
      $("#prodResults").html(vTmpHTML);
    }
  });


}
function setProd(vProdInput) { 
   document.prodFiles.product.value = vProdInput;
   getParts(vProdInput);
}
/*****************************************************************************/
function checkProduct(vProdNum) {
  var vCurrTime = new Date();
  var vCurrSecs  = (((vCurrTime.getHours() * 60) + vCurrTime.getMinutes()) * 60) + vCurrTime.getSeconds();
  var vNoCache = "&nocache=" + vCurrSecs;
  var vNoAcc = false;
  var vNoParts = false;
  var vParent;
  var vRelation;
  var vParentProd = "";
  var vParentCount = 0;
  var vRelationProd = "";
  var vRelationCount = 0;
  $.ajax({
    type: "GET",
    url: hostDomain + appPath + "/b2c/parts-work.w?product=" + vProdNum + "&compType=" + vAccCode + "," + vPrtCode + "&frames=no&target=main&sponsor=" + fdmSponsor + vNoCache,
    dataType: "xml",
    complete: function(data) {
      xmlObj = $.xmlToJSON(data.responseXML); // Save XML in JSON
      if (xmlObj.validItem=="yes"&&xmlObj.parentItemFound=="yes"){ 
        $("#product").attr("value",vProdNum);
        getParts(vProdNum);     
      }
      else if (xmlObj.validItem=="yes"&&xmlObj.relationItemFound=="yes"){ 
        golink("b2c/product.w?product=" + vProdNum,"main");   /* 010001 */
      }
      else{
       if (xmlObj.result){
         $.each(xmlObj.result,function(){
           if(this.image=="") this.image="missing_small.gif";
           if(this.isParent=="yes"){
             vParentProd += "<div class='prodSelect'><a href='#' onclick='$(\"#product\").attr(\"value\",\"" + this.prodCode + "\");getParts(\"" + this.prodCode + "\");$(\"#selectProd\").dialog(\"close\"); return false;'><img src='"+this.image+"' onerror='top.ImageError(this,\"small\");' /><span class='prodDesc'>"+this.desc+"</span></a></div>";
             vParentCount++;
             vParent = this.prodCode;
           }
           else{
             vRelationProd += "<div class='prodSelect'><a href='#' onclick='javascript:golink(\"b2c/product.w?product=" + this.prodCode + "\",\"main\");return false;'><img src='"+this.image+"' onerror='top.ImageError(this,\"small\");' /><span class='prodDesc'>"+this.desc+"</span></a></div>";
             vRelationCount++;
             vRelation = this.prodCode;
           }
         });
         
         if(!document.getElementById("selectProd"))
           $("body").append("<div id='selectProd' title='" + $("#search-title").html() + "' style='width:500px;' />");

         if(vParentCount==1){
           $("#product").attr("value",vParent);
           getParts(vParent);
           return;
         }
         else if(vParentCount>1){
           $("#selectProd").html("<h1>Products</h1>" + $("#search-parent").html() + vParentProd);
         }
         else if(vRelationCount==1){
           golink("b2c/product.w?product=" + vRelation,"main");     
           return;
         }
         else{
           $("#selectProd").html("<h1>Parts and Accessories</h1>" + $("#search-relation").html() + vRelationProd);
         }

         $("#selectProd").dialog({width:550,height:475,resizable:false} );
         $("#selectProd").dialog("open");

         if($.browser.msie){
           $("#selectProd").css("height","475px");
           $("#selectProd").parent().css("top","58px"); /* 010001 */
         }
       }
       else{
         $("#prodResults").html("<h2 class='repNotFound'>No products, parts or accessories were found for value entered.</h2>");
       }
      }
    }
  });

}
/*****************************************************************************/
