﻿function HBI_getProductCompareHF()
{   
    var productCompareListHF = null;
    var inputCol = document.getElementsByTagName("input");
    for (var i = 0; i < inputCol.length; i++) 
    { 
        if(inputCol[i] != null)
        {
            name = inputCol[i].getAttribute("name"); 
            if ( name != null && name.match("productCompareListHF"))
            { 
                productCompareListHF = inputCol[i];
                break; 
            }
        }
    }
    return productCompareListHF;
}

function HBI_cbCompare_OnClick(chkBox, productId)
{   
    var isChecked = false;
    if(chkBox != null)
    {
        isChecked = chkBox.checked;
    }

    if(productId != null && productId != "")
    {
        var name = "";
        var productCompareListHF = HBI_getProductCompareHF();
        if(productCompareListHF != null)
        {
            var compareList = productCompareListHF.value;
            if(compareList != null && compareList != "")
            {
                var index = compareList.indexOf(productId);
                if(isChecked && index == -1) //add if not in
                {
                    compareList = compareList.concat("," , productId);
                }
                else if (!isChecked && index != -1)
                {                
                    var endIndex = index + productId.length + 1;
                    if(endIndex >= compareList.length) 
                    {
                        compareList = compareList.substr(0, Math.max(0, index - 1)) // product at the end or only product
                    }
                    else
                    {
                        compareList = compareList.substr(0, index) + compareList.substr(endIndex, compareList.length - endIndex)
                    }
                }
            }
            else if (isChecked)
            {
                compareList = productId;
            }
            productCompareListHF.value = compareList;
        }
    }
}

function HBI_linkCompare_OnClick()
{   
    var productCompareListHF = HBI_getProductCompareHF();
    if(productCompareListHF != null)
    {
        var bDisplayError = true;
        var compareList = productCompareListHF.value;
        var iMinToCompare = parseInt(productCompareListHF.getAttribute("MinToCompare"));
        var iMaxToCompare = parseInt(productCompareListHF.getAttribute("MaxToCompare"));
        var compareURL = productCompareListHF.getAttribute("CompareURL");
        var errorMessage = productCompareListHF.getAttribute("ErrorMessage");
        var errorMessageLabelId = productCompareListHF.getAttribute("ErrorMessageLabelId");
                 
        if(compareList != null && compareList != "")
        {
            var compareArray = compareList.split(",");
            
            if (compareArray.length >= iMinToCompare && compareArray.length <= iMaxToCompare)
            {
                if(compareURL != null && compareURL != "")
                {
                    window.location.assign(compareURL.replace("{0}", compareList));
                    bDisplayError = false;
                }
            }            
        }
        
        if(bDisplayError)
        {
            if(errorMessageLabelId != null && errorMessageLabelId != "")
            {
                var errorMessageLabel = document.getElementById(errorMessageLabelId);
                if(errorMessageLabelId != null)
                {
                    errorMessageLabel.innerText = errorMessage;
                    window.scrollTo(0, 0);
                }            
            }
        }
    }
}
