//
// Adds selected items to the current shopping basket.
//
// Parameters:
//
//  -   itemGroup: Name of the "checkbox" elements that should be checked.
//      There should be one check box for each item on the page, all with
//      the same name, with value equal to the item ID.
//  -   schemaGroupPrefix: Prefix to the "hidden" elements that contain
//      the collection schema ID for each item. Each such hidden element
//      should be named 'schemaGroupPrefix + itemId', where 'itemId' is
//      the ID of an item.
//  -   redirectPath: The user will be redirected to this path upon submission.
//
function addSelectedToBasket(itemGroup, schemaGroupPrefix, redirectPath) {

    // Find out which items have been selected.
    var selectorGroup = document.getElementsByName(itemGroup);
    var itemId;
    var schemaId;
    var schemaList = '';
    var itemList = '';
    for (var i = 0; i < selectorGroup.length; i++) {
        if (selectorGroup[i].checked) {
            itemId = parseInt(selectorGroup[i].value);
            if (itemList == '') {
                itemList = itemId;
            } else {
                itemList = itemList + ',' + itemId;
            }

            schemaId = document.getElementsByName(schemaGroupPrefix + itemId)[0].value;
            if (schemaList == '') {
                schemaList = schemaId;
            } else {
                schemaList = schemaList + ',' + schemaId;
            }
        }
    }
    
    addToBasket(itemList, schemaList, redirectPath);
}    

//
// Adds a comma-separated list of items to the basket. The user is then sent to
// the specified redirect path.
//
function addToBasket(itemList, schemaList, redirectPath) {

    // Construct the Basket Manager URL.
    if (itemList != '' && schemaList != '') {
        var servletPath = (document.location.href.indexOf('vyre4') != -1) ? 
            '/vyre4/servlet/basket/BasketManager' : 
            '/servlet/basket/BasketManager';
        var url = servletPath + '?items=' + itemList + '&stores=' + schemaList + '&redirectPath=' + redirectPath;
        document.location.href = url;
    }
}


