﻿var selections = new Array();
function SetSelections(checkBox)
{
    var productID = checkBox.parentNode.getElementsByTagName("input")[0].value;
    
    if (checkBox.checked)
    {
        AddSelection(productID, checkBox);
    }
    else
    {
        RemoveSelection(productID);
    }
}

function AddSelection(productID, checkBox)
{
    if (selections.length < 4)
    {
        selections.push(productID);
    }
    else
    {
        checkBox.checked = false;
        alert("Only Four(4) items can be selection for comparison at one time.\nPlease remove a selection before adding a new one.");
    }
}

function RemoveSelection(productID)
{
    for (var i = selections.length - 1; i >= 0; i--)
    {
        if (selections[i] == productID)
        {
            selections.splice(i, 1);
        }
    }
}

function CompareSelections()
{    

    var link = "CompareMattresses.aspx?";
    if (selections.length < 2)
    {
        alert("You must Select at least Two(2) products before a comparison can be made.")
        return false;
    }
    else
    {
        for (var i = 0; i < selections.length; i++)
        {
            link += "ID" + (i + 1)  + "=" + selections[i] + "&"
        }
        window.location = link;
        return false;
    }
}