﻿


var i;
var ii;

var SavingsModule = {
  init: function () {
      if (document.getElementById('frontprlwrapper')) {
      this.frontPageSalesDiscount();
  }
  if (document.getElementById('productListWrap')) {
      this.plSalesDiscount();
  }
  if (document.getElementById('prCardPriceTD')) {
      this.prCardSalesDiscount();
    }
  },
  prCardSalesDiscount: function () {
    var discountState = false;
    var SpecialOfferPrice_ProductInfo;
    var Price_Productinfo;
    var PriceStriked_Productinfo;
    var spanFinder = document.getElementById('prCardPriceTD').getElementsByTagName('SPAN');
    for (var i = 0, max = spanFinder.length; i < max; i++) {
      if (spanFinder[i].className == "SpecialOfferPrice_ProductInfo") {
        discountState = true;
        SpecialOfferPrice_ProductInfo = spanFinder[i].innerHTML;
        SpecialOfferPrice_ProductInfo = this.cleanPrice(SpecialOfferPrice_ProductInfo);
      }
      if (spanFinder[i].className == "Price_Productinfo") {
        Price_Productinfo = spanFinder[i].innerHTML;
        Price_Productinfo = this.cleanPrice(Price_Productinfo);
      }
    if (spanFinder[i].className == "PriceStriked_Productinfo") {
        PriceStriked_Productinfo = spanFinder[i].innerHTML;
        PriceStriked_Productinfo = PriceStriked_Productinfo.replace(new RegExp("</strike>", "gi"), "");
        PriceStriked_Productinfo = PriceStriked_Productinfo.replace(new RegExp("<strike>", "gi"), "");
        PriceStriked_Productinfo = this.cleanPrice(PriceStriked_Productinfo);
      }
    }
    if (discountState) {
      var finalDiscountPercentage = ((PriceStriked_Productinfo - SpecialOfferPrice_ProductInfo) / PriceStriked_Productinfo) * 100;
      finalDiscountPercentage = parseInt(finalDiscountPercentage);
      if (!isNaN(finalDiscountPercentage)) {
          document.getElementById('prCardSpcOff').innerHTML += '<div id="prCardDiscountPercentage">SPAR<br>' + finalDiscountPercentage + '%</div>';
      }
    }
  },

  frontPageSalesDiscount: function () {

    var position;
    if (document.getElementById('frontprlwrapper')) {
      position = "frontPage";
    }
    if (document.getElementById('relatedTxtDiv')) {
      position = "frontPage";
    }
    var priceTDFinderArray = [];
    var specialOfferTagArray = [];
    var findElms = document.getElementsByTagName('DIV');
    //FRONTPAGE LIST
    for (i = 0, max = findElms.length; i < max; i++) {
        if (findElms[i].className == "frtprlwrapper") {
        var tempTDfind = findElms[i].getElementsByTagName('TD');
        for (ii = 0; ii < tempTDfind.length; ii++) {
            if (tempTDfind[ii].className.search(/frontprlPrice/i) != -1){
            priceTDFinderArray.push(tempTDfind[ii]);
          }
        }
        var tempDIVfind = findElms[i].getElementsByTagName('DIV');
        for (ii = 0; ii < tempDIVfind.length; ii++) {
            if (tempDIVfind[ii].className == "frontprlSpcOff") {
            specialOfferTagArray.push(tempDIVfind[ii]);
          }
        }
      }
    }

    var orgPrice;
    var newPrice;
    var savingsPrice;
    var percentageVar;

    for (i = 0, max = priceTDFinderArray.length; i < max; i++) {
      if (priceTDFinderArray[i].firstChild.nodeName == "STRIKE" || priceTDFinderArray[i].firstChild.className == "PriceStriked_Productlist") {

        if (position == "frontPage") {
          orgPrice = priceTDFinderArray[i].getElementsByTagName('STRIKE')[0].innerHTML;
          newPrice = priceTDFinderArray[i].firstChild.nextSibling.nodeValue;

          newPrice = newPrice.replace(new RegExp(" ", "gi"), "");
        }
        if (position == "prList") {
          orgPrice = priceTDFinderArray[i].firstChild.innerHTML;
          newPrice = priceTDFinderArray[i].firstChild.nextSibling.nodeValue;

          orgPrice = orgPrice.replace(new RegExp("</strike>", "gi"), "");
          orgPrice = orgPrice.replace(new RegExp("<strike>", "gi"), "");
        }


        orgPrice = this.cleanPrice(orgPrice);
        newPrice = this.cleanPrice(newPrice);

        savingsPrice = orgPrice - newPrice;
        percentageVar = (savingsPrice / orgPrice) * 100
        percentageVar = parseInt(percentageVar);

        if (!isNaN(percentageVar)) {
          specialOfferTagArray[i].innerHTML += '<span class="prListDiscountPercentage">SPAR<br>' + percentageVar + '%</span>';
        }
      }
    }
},

plSalesDiscount: function () {

    var position;
    if (document.getElementById('productListWrap')) {
        position = "prList";
    }
    var priceTDFinderArray = [];
    var specialOfferTagArray = [];
    var findElms = document.getElementsByTagName('DIV');
    //FRONTPAGE LIST
    for (i = 0, max = findElms.length; i < max; i++) {
        if (findElms[i].className == "prlistwrapper") {
            var tempTDfind = findElms[i].getElementsByTagName('TD');
            for (ii = 0; ii < tempTDfind.length; ii++) {
                if (tempTDfind[ii].className.search(/prlistPrice/i) != -1) {
                    priceTDFinderArray.push(tempTDfind[ii]);
                }
            }
            var tempDIVfind = findElms[i].getElementsByTagName('DIV');
            for (ii = 0; ii < tempDIVfind.length; ii++) {
                if (tempDIVfind[ii].className == "prlistSpcOff") {
                    specialOfferTagArray.push(tempDIVfind[ii]);
                }
            }
        }
    }

    var orgPrice;
    var newPrice;
    var savingsPrice;
    var percentageVar;

    for (i = 0, max = priceTDFinderArray.length; i < max; i++) {
        if (priceTDFinderArray[i].firstChild.nodeName == "STRIKE" || priceTDFinderArray[i].firstChild.className == "PriceStriked_Productlist") {

            if (position == "prList") {
                orgPrice = priceTDFinderArray[i].getElementsByTagName('STRIKE')[0].innerHTML;
                newPrice = priceTDFinderArray[i].firstChild.nextSibling.nodeValue;

                newPrice = newPrice.replace(new RegExp(" ", "gi"), "");
            }
            if (position == "prListw23") {
                orgPrice = priceTDFinderArray[i].firstChild.innerHTML;
                newPrice = priceTDFinderArray[i].firstChild.nextSibling.nodeValue;

                orgPrice = orgPrice.replace(new RegExp("</strike>", "gi"), "");
                orgPrice = orgPrice.replace(new RegExp("<strike>", "gi"), "");
            }


            orgPrice = this.cleanPrice(orgPrice);
            newPrice = this.cleanPrice(newPrice);

            savingsPrice = orgPrice - newPrice;
            percentageVar = (savingsPrice / orgPrice) * 100
            percentageVar = parseInt(percentageVar);

            if (!isNaN(percentageVar)) {
                specialOfferTagArray[i].innerHTML += '<span class="prListDiscountPercentage">SPAR<br>' + percentageVar + '%</span>';
            }
        }
    }
},
  cleanPrice: function (inVal) {
    this.testOutPut(inVal);
    inVal = inVal.replace(/\./ig, "");
    this.testOutPut(inVal);
    inVal = inVal.replace(/\,/ig, ".");
    this.testOutPut(inVal);
    this.testOutPut(parseFloat(inVal));
    return (parseInt(inVal));
  },
  testOutPut: function (inVal) {
    //document.write('<div style="display:none">' + inVal + '</div');
  }
}


SavingsModule.init();
