
SliceHandler.prototype.hasSlices = SliceHandler_hasSlices;
SliceHandler.prototype.getSku = SliceHandler_getSku;
SliceHandler.prototype.filterSlices = SliceHandler_filterSlices;
SliceHandler.prototype.getSliceById = SliceHandler_getSliceById;
SliceHandler.prototype.validate = SliceHandler_validate;
SliceHandler.prototype.clearErrors = SliceHandler_clearErrors;

function SliceHandler()
{
}

function SliceHandler_hasSlices()
{
    return sliceData.hasSlices;
}

function uptoTwoDesimalDigit(digit)
{
	// Method added for PMO - 1822
	var digLength = digit.length;
	var decimalPlace = digit.indexOf(".");
	var digitAfterDesimal = parseInt(digLength)-(parseInt(decimalPlace)+1);

	if(parseInt(decimalPlace) == -1) {
		digit=digit+".00";
	} else if(digitAfterDesimal==1) {
                digit=digit+"0";
        }
	return digit;
}

function SliceHandler_filterSlices()
{
    if(this.hasSlices())
    {
        var slices = sliceData.slices;
        for(var sid in slices)
        {
            slices[sid].filterSlices();
        }
    }

	// Added under requirement change SMP - 2257
	var allSkusEligibleForInStoreOnly = document.getElementById('allSkusEligibleForInStoreOnly').value=="true";
	// End
    var sku = this.getSku();
    if(sku || allSkusEligibleForInStoreOnly)
    {
		if(sku)
		{
			var price = productSkus[sku].price;
			var basePrice = productSkus[sku].basePrice;
			var priceDisplay = productSkus[sku].priceDisplay;
			var availStr = productSkus[sku].avail;
			var availMsg = productSkus[sku].availMsg;


			if (price && price.length > 0) 
			{
				hideElement("initPriceValue");

				if(priceDisplay != "VP")
					document.getElementById("priceValue").innerHTML = "<b>Our Price: $" + uptoTwoDesimalDigit(price) + "</b>";
				else
					document.getElementById("priceValue").innerHTML = "<b>Price: </b><strike>was $" + uptoTwoDesimalDigit(basePrice) + "</strike><br/><b><font color='#CC0000'>now: $ " + uptoTwoDesimalDigit(price) + "</font></b>";
			}
		}


		var msgElem = document.getElementById('availMsg');
		var isEligibleForInStoreOnly = document.getElementById('isEligibleForInStoreOnly').value=="true";

		var inStoreOnlyImg							= document.getElementById('inStoreOnlyOnProd');
		var inStoreOrPlaceYourOrderImg		= document.getElementById('hdr');
		var topPromoMsg								= document.getElementById('topPromo');
		var findStoreBtn								= document.getElementById('findStore');
		var addToCartBtn								= document.getElementById('addCart');
		var botPromoMsg								= document.getElementById('botPromo');
		var prodNotAvailMsg						= document.getElementById('prodNotAvail');
		var stepQtyBox								= document.getElementById('stepQty');
		var inStoreOnlyOnProdImg				= document.getElementById('inStoreOnlyOnProd');
		var actionButtons								= document.getElementById('actionButtons');
		var mcafee										= document.getElementById('mcafee');
		var actionBMLButtons						= document.getElementById('actionBMLButtons');

		
		if (availStr == "ADVANCED_SALE" || availStr == "ADVANCED_SALE_LIMITED"  || allSkusEligibleForInStoreOnly) 
        {
			if (msgElem && !(isEligibleForInStoreOnly))
			{
				msgElem.innerHTML = availMsg;
				msgElem.style.display = "block";
			}	
			
			if(inStoreOnlyImg && isEligibleForInStoreOnly)
			{
				inStoreOnlyImg.style.display = "block";
			}

			if(inStoreOrPlaceYourOrderImg && isEligibleForInStoreOnly)
			{
				inStoreOrPlaceYourOrderImg.src = "/images/available_in_store_only.JPG";
			}

			if(topPromoMsg && isEligibleForInStoreOnly)
			{
				topPromoMsg.style.display = "none";
			}

			if(findStoreBtn && isEligibleForInStoreOnly)
			{
				findStoreBtn.style.display = "inline";
			}

			if(addToCartBtn && isEligibleForInStoreOnly)
			{
				addToCartBtn.style.visibility="hidden";
			}

			if(botPromoMsg && isEligibleForInStoreOnly)
			{
				botPromoMsg.style.display = "none";
			}

			if(prodNotAvailMsg && isEligibleForInStoreOnly)
			{
				prodNotAvailMsg.style.display = "block";
			}

			if(stepQtyBox && isEligibleForInStoreOnly)
			{
				stepQtyBox.style.display = "none";
			}

			if(inStoreOnlyOnProdImg && isEligibleForInStoreOnly)
			{
				inStoreOnlyOnProdImg.style.display = "block";
			}

			if(actionBMLButtons && isEligibleForInStoreOnly)
			{
				actionBMLButtons.style.display = "none";
			}

			if(mcafee && isEligibleForInStoreOnly)
			{
				mcafee.style.visibility="hidden";
			}
        } 
        else
        {
			if (msgElem) 
			{
				msgElem.style.display = "none";
			}

			if(inStoreOnlyImg)
			{
				inStoreOnlyImg.style.display = "none";
			}

			if(inStoreOrPlaceYourOrderImg)
			{
				inStoreOrPlaceYourOrderImg.src = "/images/hdr_placeOrderHere.gif";
			}

			if(topPromoMsg)
			{
				topPromoMsg.style.display = "block";
			}

			if(findStoreBtn)
			{
				findStoreBtn.style.display = "none";
			}

			if(addToCartBtn)
			{
				addToCartBtn.style.visibility="visible";
				//addToCartBtn.style.display = "block";
			}

			if(botPromoMsg)
			{
				botPromoMsg.style.display = "block";
			}

			if(prodNotAvailMsg)
			{
				prodNotAvailMsg.style.display = "none";
			}

			if(stepQtyBox)
			{
				stepQtyBox.style.display = "block";
			}

			if(inStoreOnlyOnProdImg)
			{
				inStoreOnlyOnProdImg.style.display = "none";
			}

			if(actionBMLButtons && isEligibleForInStoreOnly)
			{
				actionBMLButtons.style.display = "block";
			}

			if(mcafee && isEligibleForInStoreOnly)
			{
				mcafee.style.visibility="visible";
			}
       }
    }
}

function SliceHandler_getSliceById(id)
{
    if(sliceData.hasSlices)
    {
        var slices = sliceData.slices;
        return slices[id];
    }
    return null;
}

function SliceHandler_getSku()
{
    if(sliceData.hasSlices)
    {
        var slices = sliceData.slices;
        for(var pSku in productSkus)
        {
            var found = true;
            for(s in slices)
            {
                var slice = slices[s];
                if(!slice.isSkuSelected(pSku))
                {
                    found = false;
                }
            }
            if(found)
            {
                return pSku;
            }
        }
    }
    else
    {
        for(var pSku in productSkus)
        {
            return pSku;
        }
    }
    return null;
}

function SliceHandler_validate()
{
    if(sliceData.hasSlices)
    {
        var slices = sliceData.slices;
        for(s in slices)
        {
            var slice = slices[s];
            if(!slice.validate())
            {
                showErrorMessage("Please complete the selections marked below.");
                return false;
            }
        }
    }
    var el = document.getElementById("qty_0");
    if(el)
    {
        var errorElementId = "qty_0_Error";
        if(el.value.match("^\\s*$") != null)
        {
            showErrorMessage("Please complete the selections marked below.");
            showElement(errorElementId);
            return false;
        }
    }
    return true;
}

function SliceHandler_clearErrors()
{
    if(sliceData.hasSlices)
    {
        var slices = sliceData.slices;
        for(s in slices)
        {
            var slice = slices[s];
            slice.hideError();
        }
    }
    hideElement("qty_0_Error");
}
