//
// Adds selected items to a named basket 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.
//  -   the name of the basket to add the items to
//
function addSelectedToBasket(itemGroup, schemaGroupPrefix, redirectPath, namedBasket) {

    // Find out which items have been selected.
    var selectorGroup = document.getElementsByName(itemGroup);
    var itemId;
    var schemaId;
    var schemaList = '';
    var itemList = '';
    if ( typeof namedBasket == 'undefined' ) {
      namedBasket = "order";
    }
    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, namedBasket);
}    

//
// Adds a comma-separated list of items to a named basket. The user is then sent to
// the specified redirect path.
//
function addToBasket(itemList, schemaList, redirectPath, namedBasket) {
    // Construct the Basket Manager URL.
    if ( typeof namedBasket == 'undefined' ) {
      namedBasket = "order";
    }
    if (itemList != '' && schemaList != '') {
        var servletPath = (document.location.href.indexOf('vyre4') != -1) ? 
            '/vyre4/servlet/basket/BasketManager' : 
            '/servlet/basket/BasketManager';
        var url = servletPath + '?action=addToBasket' + '&items=' + itemList + '&stores=' + schemaList + '&redirectPath=' + redirectPath + '&namedBasket=' + namedBasket;
        document.location.href = url;
    }
}

//
// Removes all the items from the named basket. The user is then sent to 
// the specified redirect path.
//
function emptyBasket(redirectPath, namedBasket) {
    // Construct the Basket Manager URL.
    if ( typeof namedBasket == 'undefined' ) {
      namedBasket = "order";
    }
    
    var servletPath = (document.location.href.indexOf('vyre4') != -1) ? 
            '/vyre4/servlet/basket/BasketManager' : 
            '/servlet/basket/BasketManager';
    var url = servletPath + '?action=emptyBasket&namedBasket=' + namedBasket + '&redirectPath=' + redirectPath;
    document.location.href = url;
}

// 
// Remove selected items from a named basket. The user is then sent to
// the specified redirect path.
//
// 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.
//  -   redirectPath: The user will be redirected to this path upon submission.
//  -   the name of the basket to add the items to
//
function removeSelectedFromBasket(itemGroup, redirectPath, namedBasket) {

    // Find out which items have been selected.
    var selectorGroup = document.getElementsByName(itemGroup);
    var itemId;
    var itemList = '';
    if ( typeof namedBasket == 'undefined' ) {
      namedBasket = "order";
    }
    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;
            }

        }
    }
    
    removeFromBasket(itemList, redirectPath, namedBasket);
}

//
// Remove a comma-separated list of items to a named basket. The user is then sent to
// the specified redirect path.
//
function removeFromBasket(itemList, redirectPath, namedBasket) {
    // Construct the Basket Manager URL.
    if ( typeof namedBasket == 'undefined' ) {
      namedBasket = "order";
    }
    if ( itemList != '' ) {
        var servletPath = (document.location.href.indexOf('vyre4') != -1) ? 
            '/vyre4/servlet/basket/BasketManager' : 
            '/servlet/basket/BasketManager';
        var url = servletPath + '?action=removeFromBasket' + '&items=' + itemList + '&redirectPath=' + redirectPath + '&namedBasket=' + namedBasket;
        document.location.href = url;
    }
}

