var suppSellProductArray = new Array();
var suppSellStored = false;
// ordered acknow list - used to find if the stored record has a higher value
// than the one passed in, if so don't overwrite it.
var ACKNOWLEDGEMENT = new Array();
ACKNOWLEDGEMENT[0] = "NO DISPLAY";   // 0
ACKNOWLEDGEMENT[1] = "NOT OFFERED";  // 1
ACKNOWLEDGEMENT[2] = "DEFER";        // 2
ACKNOWLEDGEMENT[3] = "ACCEPT";       // 3
ACKNOWLEDGEMENT[4] = "KEYEDACCEPT";  // 4

var SUPP_SELL_OFFERTYPE = new Array();
SUPP_SELL_OFFERTYPE[0] = "WEB ACC OFFER";
SUPP_SELL_OFFERTYPE[1] = "GROUPED AOS";
SUPP_SELL_OFFERTYPE[2] = "WEB XSELL";
SUPP_SELL_OFFERTYPE[3] = "GROUPED XSELL";
SUPP_SELL_OFFERTYPE[4] = "WEB ALT";
SUPP_SELL_OFFERTYPE[5] = "GROUPED ALT";
SUPP_SELL_OFFERTYPE[6] = "WEB TOP ALT";
SUPP_SELL_OFFERTYPE[7] = "GROUPED TOP ALT";
SUPP_SELL_OFFERTYPE[8] = "WEB UPSELL";
SUPP_SELL_OFFERTYPE[9] = "GROUPED UPSELL";
SUPP_SELL_OFFERTYPE[10] = "BASKET XSELL";
SUPP_SELL_OFFERTYPE[11] = "GROUP BASK XSEL";
SUPP_SELL_OFFERTYPE[12] = "BASKET OFFER";
SUPP_SELL_OFFERTYPE[13] = "GROUP BASK OFF";

function SuppSellProduct() {
    this.id = suppSellProductArray.length;
    this.productId;
    this.offerType = '';
    this.acknow = '';
    this.parentId;
    this.isParentMarker = false;
    this.isAddedToBasket = false;
    this.promoId;
    this.campId;
    this.prgId;
    this.segmentId;
    this.cmType;
    this.cmParentDetails;
    this.cmBreadcrumb;
}

function addSuppSellProduct(productId, offerType, acknow, parentId, isParentMarker, isAddedToBasket, promoId, campId, cmType, cmParentDetails, cmBreadcrumb) {
    return addSuppSell(productId, offerType, acknow, parentId, isParentMarker, isAddedToBasket, promoId, campId, null, null, cmType, cmParentDetails, cmBreadcrumb)
}

function addSuppSell(productId, offerType, acknow, parentId, isParentMarker, isAddedToBasket, promoId, campId, prgId, segmentId, cmType, cmParentDetails, cmBreadcrumb) {
    var findSuppSell = getSuppSellProduct(productId, offerType, parentId);
    if (findSuppSell != null) {
        if (ACKNOWLEDGEMENT.indexOf(findSuppSell.acknow) < ACKNOWLEDGEMENT.indexOf(acknow)) {
            findSuppSell.acknow = acknow;
        }
        return findSuppSell;
    } else {
        var suppSell = new SuppSellProduct();
        suppSell.productId = productId;
        suppSell.offerType = offerType;
        suppSell.acknow = acknow;
        suppSell.parentId = parentId;
        suppSell.isParentMarker = isParentMarker;
        suppSell.isAddedToBasket = isAddedToBasket;
        suppSell.promoId = promoId;
        suppSell.campId = campId;
        suppSell.prgId = prgId;
        suppSell.segmentId = segmentId;
        suppSell.cmType = cmType;
        suppSell.cmParentDetails = cmParentDetails;
        suppSell.cmBreadcrumb = cmBreadcrumb;
        suppSellProductArray[suppSellProductArray.length] = suppSell;
        return suppSell;
    }
}

function getSuppSellProduct(productId, offerType, parentId) {
    for (var i=0; i<suppSellProductArray.length; i++) {
        if (suppSellProductArray[i].productId == productId
                && suppSellProductArray[i].offerType == offerType
                && suppSellProductArray[i].parentId == parentId) {
            return suppSellProductArray[i];
        }
    }
    return null;
}

function getSuppSellProductByKey(productId) {
    for (var i=0; i<suppSellProductArray.length; i++) {
        if (suppSellProductArray[i].productId == productId) {
            return suppSellProductArray[i];
        }
    }
    return null;
}

//used in productLayout toXSellProdPage function
function getSuppSellProductById(id) {
    return suppSellProductArray[id];
}

function isSuppSellOffers() {
    for (var i=0; i<suppSellProductArray.length; i++) {
        if (SUPP_SELL_OFFERTYPE.indexOf(suppSellProductArray[i].offerType) > -1) {
            return true;
        }
    }
    return false;
}

function storeSuppSellProductArray(contextPath) {
    suppSellStored = true;
    if (suppSellProductArray.length > 0) {
        new Ajax.Request(contextPath+'/suppsell/store.do', {
            parameters: {suppsells: suppSellProductArray.toJSON()}
        });
    }
}

function resetSuppSellAcknow() {
    for (var i=0; i<suppSellProductArray.length; i++) {
        if (SUPP_SELL_OFFERTYPE.indexOf(suppSellProductArray[i].offerType) > -1) {
            suppSellProductArray[i].acknow = "NO DISPLAY";
        }
    }
}
