/**
 *	Description: provide trolley menu functionalities.
 */

/** Flag used to set trolley dialog close on click. */
var closeTrolleyOnClickFlag = true;


/** Preload needed images. */
var preloadImagesArray = ["../common/layout/trly/anim_carr_giallo.gif"];
var preloadedTrolleyPutAnimation = performImagesPreload();


/**	Number of milliseconds that the quantity will stay highlighted. */
var highlightQuantityTime = 1500;

/** Dummy definition of urls. */
var putInTrolleyUrl = null;
var reloadTrolleyUrl = null;


/**	Function used to refresh trolley area. */
function reloadTrolley()
{
	submitFormExtended("trolleyForm", reloadTrolleyUrl, "bottomframeleft", null);
}


/** Function used to switch put animation image. */
var putImageTag = null;
var putImageRight = null;


function forceSwitchImage() {
	putImageRight.src = preloadedTrolleyPutAnimation[0].src;
}


function switchPutImage() {
    var putInTrolleyAnimationRight = (opener && opener.putInTrolley) ?
                                     opener.top.bottomframeright.document.getElementById("trolleyPutAnimation") :
                                     top.bottomframeright.document.getElementById("trolleyPutAnimation");

	if(putInTrolleyAnimationRight) putInTrolleyAnimationRight.src = preloadedTrolleyPutAnimation[0].src;
}


/**	Functions used to put a product in trolley. */
function setPreviousEducationaPopupFlag(newValue)
{
	if(opener && opener.putInTrolley) {
		opener.top.previousEducationalPopupFlag = newValue;
	} else {
		top.previousEducationalPopupFlag = newValue;
	}
}


function getPreviousEducationaPopupFlag()
{
	if(opener && opener.putInTrolley) {
		return(opener.top.previousEducationalPopupFlag);
	} else {
		return(top.previousEducationalPopupFlag);
	}
}


function putInTrolleyGlobal(storeId, productId, quantity, optionsArray, mode, showEduPopups)
{
	if(productId != null && quantity != null)
	{
		//	Build parameters array.
		var parametersArray = new Array();

        if(storeId != null) {
            parametersArray.push("storeId");
            parametersArray.push(storeId);
        }

        parametersArray.push("productId");
		parametersArray.push(productId);
		parametersArray.push("quantity");
		parametersArray.push(quantity);
		parametersArray.push("showEducationalPopup");
		parametersArray.push(getPreviousEducationaPopupFlag() & showEduPopups);

        parametersArray.push("putMode");
        parametersArray.push(mode);

		//	Push product options if any.
        optionsArray = optionsArray ? optionsArray : new Array();
        parametersArray.push("optionsNumber");
        parametersArray.push(optionsArray.length);

        for(var i = 0; (i < optionsArray.length && i < 4); i++)
        {
            parametersArray.push("optionArray[" + i + "]");
            parametersArray.push(optionsArray[i]);
        }

        for(; i < 4; i++)
        {
            parametersArray.push("optionArray[" + i + "]");
            parametersArray.push("");
        }

        callPutInTrolley(putInTrolleyUrl + "?", parametersArray);
    }
}

function putInTrolley(storeId, productId, quantity, optionsArray, force)
{
    var mode = (force != null) ? 2 : 1;
    putInTrolleyGlobal(storeId, productId, quantity, optionsArray, mode, true);
}


function removePayload(payloadIdentifier)
{
    //noinspection JSUnusedLocalSymbols
    try {
        var parametersArray = new Array();
        parametersArray.push("payloadIdentifier");
        parametersArray.push(payloadIdentifier);

        callPutInTrolley(putInTrolleyUrl + "?actionMethod=removePayload&", parametersArray);
    } catch(e) {}
}


function refreshTrolley()
{
    //	Disable continue button.
	disableContinueToSlotPage();

    submitFormExtended("bodyForm", putInTrolleyUrl, "bottomframeleft", ["actionMethod", "putAllInTrolley", "productsPrefix", "trolleyItem"]);
}


function addAllToTrolley()
{
	//	Hide dialogs.
	hideDialogs();

    var alertMessage = top.updatedProductInTrolley <= 0 ? addAllProductsEmptyConfirmMsg : addAllProductsConfirmMsg;

	if(window.confirm(alertMessage))
	{
        for(var idx = 0; idx < productItems.length; idx++)
		{
			if(!updateFormItems(idx)) return;
		}

		switchPutImage();
		submitFormExtended("bodyForm", putInTrolleyUrl, "bottomframeleft", ["actionMethod", "putAllInTrolley"]);

		for(idx = 0; idx < productItems.length; idx++)
		{
			var inputProdQty = document.getElementById("js-prodinputqty-" + idx);
			if(inputProdQty != null) inputProdQty.value = productItems[idx].originalQuantity;
		}
	}
}


function callPutInTrolley(fullurl, parametersArray)
{
    //	Page type and id parameters. Used to redirect to the correct page after put in trolley login.
    //  noinspection JSUnusedLocalSymbols
    try
    {
        switchPutImage();
        closeExceptionPopups();

        var form = (opener && opener.putInTrolley) ? opener.document.forms["trolleyForm"] : document.forms["trolleyForm"];
        var pageLoginForward = form.elements["pageLoginForward"].value;

        form.elements["pageLoginForward"].value = "";

        if(pageLoginForward != null && pageLoginForward != "")
        {
            parametersArray.push("pageLoginForward");
            parametersArray.push(pageLoginForward);
        }
    }
    catch(e) {/* Nothing */}

    //  Build the URL
    if((parametersArray.length % 2) == 0) {
        for(var i = 0; i < parametersArray.length; i += 2)
        {
            fullurl += parametersArray[i] + "=" + parametersArray[i + 1];
            if (i < parametersArray.length) fullurl += "&";
        }
    }

    //  Replace the URL to skip history entry... but check for browser compatibility
    if(opener && opener.putInTrolley)
    {
        if (opener.top != null && opener.top.bottomframeleft != null) {
            if (opener.document.images)
                opener.top.bottomframeleft.location.replace(fullurl);
            else
                opener.top.bottomframeleft.location.href = fullurl;
        }
        else {
            //	Submit form and call putInTrolleyAction if we have trouble with the replace() trick!
            opener.document.submitFormExtended("trolleyForm", putInTrolleyUrl, "bottomframeleft", parametersArray);
        }
    }
    else
    {
        if (top != null && top.bottomframeleft != null) {
            if (document.images)
                top.bottomframeleft.location.replace(fullurl);
            else
                top.bottomframeleft.location.href = fullurl;
        }
        else {
            //	Submit form and call putInTrolleyAction if we have trouble with the replace() trick!
            document.submitFormExtended("trolleyForm", putInTrolleyUrl, "bottomframeleft", parametersArray);
        }
    }
}


/**	Hide trolley dialog. */
function hideDialogs()
{
	//  noinspection JSUnusedLocalSymbols
    try
	{
		//	Retrieve work area document.
		var workAreaDocument = top.workframe.document;

		if(workAreaDocument != null)
		{
			var trolleyDialog = workAreaDocument.getElementById("trolleydialog");

            if(trolleyDialog && trolleyDialog.style.visibility == "visible")
            {
                //	Hide trolley dialog.
				trolleyDialog.style.visibility = "hidden";

                //  Retrieve trolley frame used as additional layer in order to handle ie "select" bug.
                var trolleyFrame = workAreaDocument.getElementById("trolleyframe");
                if(trolleyFrame) trolleyFrame.style.visibility = "hidden";
            }
		}
	}
	catch(e) {}
}


/**	Hide trolley dialog. */
function closeExceptionPopups()
{
    //  noinspection JSUnusedLocalSymbols
    try {
        for(var popup in top.globalPopupHandles) {
            if(!popup.closed && popup.close) popup.close();
        }
    } catch(e) {}
}


/**
	Code used to close trolley dialog on click.
*/

function setCloseTrolleyOnClickOut()
{
	if(closeTrolleyOnClickFlag)
	{
		//	Set onclick handler in order to hide trolley dialog.
		//  noinspection JSUnusedLocalSymbols
        try {
			document.captureEvents(Event.CLICK);
			document.onclick = checkClickTargetTrlyClose;
		} catch(e) {
			document.onclick = checkClickTargetTrlyClose;
		}
	}
}

function checkClickTargetTrlyClose(currentEvent)
{
	//  noinspection JSUnusedLocalSymbols
    try
	{
		var hideTrolleyDialog = true;

		//	Retrieve event.
		if(!currentEvent) currentEvent = window.event;

		//	Check for trolley dialog.
		var trolleyDialog = document.getElementById("trolleydialog");

		if(trolleyDialog != null && trolleyDialog.style.visibility == "visible")
		{
			var trlyLeft = parseInt(trolleyDialog.style.left);
			var trlyTop = parseInt(trolleyDialog.style.top);
			var trlyWidth = parseInt(trolleyDialog.style.width);
			var trlyHeight = parseInt(trolleyDialog.style.height);

			//	Check click position.
			if(currentEvent.clientX >= trlyLeft && currentEvent.clientX <= (trlyLeft + trlyWidth))
			{
				if(currentEvent.clientY >= trlyTop && currentEvent.clientY <= (trlyTop + trlyHeight))
				{
					//	Inside trolley dialog... return
					hideTrolleyDialog = false;
				}
			}
		}

		if(hideTrolleyDialog) hideDialogs();
	}
	catch(e) { /* Nothing */ }
}


/**----------------------------------------------------------------------------
 *	Description: provide trolley menu functionalities.
 *---------------------------------------------------------------------------*/
/**
 Global variables:

 productNumber 		->	number of products in trolley.
 totalPrice			->	total price of the products in trolley.
 fidelityPoints		->	number of available fidelity points.
 trolleyContent		->	array of ProductLine objects (products) in trolley.
 trolleyPromoContent	->	array of Promo objects containing promotion informations.
 */
var productNumber = 0;
var totalPrice = 0;
var fidelityPoints = 0;
var trolleyContent = null;
var trolleyPromoContent = null;


/**
 Maximum height of the trolley dialog and arrays containing its height depending
 on the number of trolley lines.
 */
var trolleyMaxHeight = 224;
var trolleyHeightArray = [114, 114, 144, 174, 204];
var trolleyMaxHeightScroll = 206;
var trolleyHeightArrayScroll = [96, 96, 126, 156, 186];
var promoHtmlHeight = 31;

/**
 Maximum number of chars in a single line.
 */
var maxCuttedLength = 47;


/**
 Object used to store promo informations that will be showned in promo area.
 */
function PromoLine( iconName, message )
{
    this.iconName = iconName;
    this.message = message;
}


/**
 Object used to store informations that will be showned in trolley dialog.
 */
function ProductLine( productId, price, quantity, description,
                      unitPrice, unitPriceText, retailPrice, originalRetailPrice,
                      packaging, multipack, unitText,
                      showInfoIcon, infoIcon,
                      leadTime, leadTimeTooltip,
                      deliveryExceptionString, surcharge, surchargeGroup,
                      priceOptionRate, priceOptionString, otherOptionsArray )
{
    this.productId = productId;
    this.price = price;
    this.quantity = quantity;
    this.description = description;

    this.packaging = packaging;
    this.multipack = multipack;
    this.unitText = unitText;

    this.showInfoIcon = showInfoIcon;
    this.infoIcon = infoIcon;

    this.unitPrice = unitPrice;
    this.unitPriceText = unitPriceText;
    this.retailPrice = retailPrice;
    this.originalRetailPrice = originalRetailPrice;

    this.leadTime = leadTime;
    this.leadTimeTooltip = leadTimeTooltip;

    this.deliveryDayExceptionString = deliveryExceptionString;
    this.surcharge = surcharge;
    this.surchargeGroup = surchargeGroup;

    this.priceOptionRate = priceOptionRate;
    this.priceOptionString = priceOptionString;
    this.otherOptionsArray = otherOptionsArray;
}


/**
 Change trolley dialog visibility status.
 */
function switchTrolleyVisibility()
{
    //	Get trolley dialog object.
    //  noinspection JSUnusedLocalSymbols
    try
    {
        var trolleyDialog = top.workframe.document.getElementById("trolleydialog");

        //	Return if not defined.
        if ( trolleyDialog != null ) {
            //	Check dialog visibility.
            if ( trolleyDialog.style.visibility == "hidden" ) {
                //	Submit form (load trolley content).
                submitFormExtended("trolleyForm", showTrolleyContentUrl, "bottomframeleft", null);
            } else {
                //	Hide dialog.
                hideDialogs();
            }
        }
    }
    catch(e) { /* Nothing */ }
}


/**
 Load trolley content (if any) and show trolley dialog.
 */
function setTrolleyGeometry( trolleyDialog )
{
    //	Set div height.
    var scrollableDialogSection = top.workframe.document.getElementById("trolleydialogscroll");

    var newTrolleyHeight = 0;
    var newScrollableHeight = 0;

    if ( productNumber <= 4 )
    {
        newTrolleyHeight = trolleyHeightArray[productNumber];
        newScrollableHeight = trolleyHeightArrayScroll[productNumber];
    }
    else
    {
        newTrolleyHeight = trolleyMaxHeight;
        newScrollableHeight = trolleyMaxHeightScroll;
    }

    if ( trolleyPromoContent && trolleyPromoContent.length > 0 )
    {
        newTrolleyHeight += promoHtmlHeight * trolleyPromoContent.length + 4;
        newScrollableHeight += promoHtmlHeight * trolleyPromoContent.length + 4;
    }

    //	Set correct height.
    if ( newTrolleyHeight < trolleyMaxHeight )
    {
        trolleyDialog.style.height = "" + newTrolleyHeight + "px";
        scrollableDialogSection.style.height = "" + newScrollableHeight + "px";
    }
    else
    {
        trolleyDialog.style.height = "" + trolleyMaxHeight + "px";
        scrollableDialogSection.style.height = "" + trolleyMaxHeightScroll + "px";
    }

    //	Scroll to top.
    trolleyDialog.scrollTop = "0px";

    //	Calculate new position in work area and place dialog.
    trolleyDialog.style.left = "7px";
    trolleyDialog.style.top = (parseInt(top.workframe.document.body.clientHeight) - parseInt(trolleyDialog.style.height)) + "px";

    //	Show trolley dialog.
    trolleyDialog.style.visibility = "visible";
    scrollableDialogSection.scrollTop = 0;
    
    var trolleyFrame = top.workframe.document.getElementById("trolleyframe");
    trolleyFrame.style.left = trolleyDialog.style.left;
    trolleyFrame.style.top = trolleyDialog.style.top;
    trolleyFrame.style.visibility = "visible";
}


function showTrolleyDialog()
{
    //	Get trolley dialog object.
    //noinspection JSUnusedLocalSymbols
    try
    {
        var trolleyDialog = top.workframe.document.getElementById("trolleydialog");

        if ( trolleyDialog != null && trolleyContent != null )
        {
            //	Close group limit popups.
            closeExceptionPopups();

            //	Fill promo content.
            var trolleyDialogPromo = top.workframe.document.getElementById("trolleydialogpromo");
            var productPromo = new Array();

            //	Promo header
            productPromo.push(
                    '<table border="0" cellspacing="0" cellpadding="0">' +
                    '<tr>' +
                    '<td><img src="' + layoutImagesUrl + 's.gif" width="46" height="1" border="0" alt="" /></td>' +
                    '<td width="100%"><img src="' + layoutImagesUrl + 's.gif" width="90" height="1" border="0" alt="" /></td>' +
                    '<td><img src="' + layoutImagesUrl + 's.gif" width="4" height="1" border="0" alt="" /></td>' +
                    '<td><img src="' + layoutImagesUrl + 's.gif" width="120" height="1" border="0" alt="" /></td>' +
                    '<td><img src="' + layoutImagesUrl + 's.gif" width="18" height="1" border="0" alt="" /></td>' +
                    '</tr>');

            if ( fidelityPoints > 0 )
            {
                productPromo.push(
                        '<tr class="trolleydialogheader">' +
                        '<td><img src="' + layoutImagesUrl +
                        'promo_icons/promo_personalizzata_m.gif" width="22" height="27" border="0" alt="" hspace="4" vspace="2"></td>' +
                        '<td colspan="3"><span class="trolleydialogheaderred">' + 'Hai diritto a ' + fidelityPoints + ' punti fragola. ' +
                        '</span></td>' +
                        '<td></td>' +
                        '</tr>');

                if ( trolleyPromoContent && trolleyPromoContent.length > 0 )
                {
                    productPromo.push(
                            '<tr>' +
                            '<td colspan="5" height="2"></td>' +
                            '</tr>');
                }
            }

            if ( trolleyPromoContent && trolleyPromoContent.length > 0 )
            {
                for ( var index = 0 ; index < trolleyPromoContent.length ; index++ )
                {
                    var promoIcon = (trolleyPromoContent[index].iconName == null || trolleyPromoContent[index].iconName == '' ?
                                     "promo_personalizzata_m.gif" : trolleyPromoContent[index].iconName);

                    productPromo.push(
                            '<tr class="trolleydialogheader">' +
                            '<td><img src="' + layoutImagesUrl + 'promo_icons/' + promoIcon +
                            '" width="22" height="27" border="0" alt="" hspace="4" vspace="2"></td>' +
                            '<td colspan="3"><span class="trolleydialogheaderred">' + trolleyPromoContent[index].message + '</span></td>' +
                            '<td></td>' +
                            '</tr>');

                    if ( index != (trolleyPromoContent.length - 1) )
                    {
                        productPromo.push(
                                '<tr>' +
                                '<td colspan="5" height="2"></td>' +
                                '</tr>');
                    }
                }
            }

            //	Promo footer.
            productPromo.push(
                    '<tr>' +
                    '<td colspan="5" height="2"></td>' +
                    '</tr>' +
                    '</table>');

            trolleyDialogPromo.innerHTML = productPromo.join('');

            //	Fill product list.
            var trolleyDialogProducts = top.workframe.document.getElementById("trolleydialogproduct");
            var productList = new Array();

            productList.push(
                    '<table border="0" cellspacing="0" cellpadding="0">' +
                    '<tr>' +
                    '<td><img src="' + layoutImagesUrl + 's.gif" width="32" height="1" border="0" alt="" /></td>' +
                    '<td><img src="' + layoutImagesUrl + 's.gif" width="4" height="1" border="0" alt="" /></td>' +
                    '<td><img src="' + layoutImagesUrl + 's.gif" width="10" height="1" border="0" alt="" /></td>' +
                    '<td width="100%"><img src="' + layoutImagesUrl + 's.gif" width="82" height="1" border="0" alt="" /></td>' +
                    '<td><img src="' + layoutImagesUrl + 's.gif" width="8" height="1" border="0" alt="" /></td>' +
                    '<td><img src="' + layoutImagesUrl + 's.gif" width="4" height="1" border="0" alt="" /></td>' +
                    '<td><img src="' + layoutImagesUrl + 's.gif" width="32" height="1" border="0" alt="" /></td>' +
                    '<td><img src="' + layoutImagesUrl + 's.gif" width="4" height="1" border="0" alt="" /></td>' +
                    '<td><img src="' + layoutImagesUrl + 's.gif" width="4" height="1" border="0" alt="" /></td>' +
                    '<td><img src="' + layoutImagesUrl + 's.gif" width="30" height="1" border="0" alt="" /></td>' +
                    '<td><img src="' + layoutImagesUrl + 's.gif" width="48" height="1" border="0" alt="" /></td>' +
                    '</tr>' +
                    '<tr class="trolleydialoginfo">' +
                    '<td align="center" height="18"><span class="trolleydialogheaderred">N.</span></td>' +
                    '<td class="trolleydialoginfo"></td>' +
                    '<td></td>' +
                    '<td colspan="2"><span class="trolleydialogheaderblu">Prodotto</span></td>' +
                    '<td class="trolleydialoginfo"></td>' +
                    '<td align="center"><span class="trolleydialogheaderblu">Info</span></td>' +
                    '<td class="trolleydialoginfo"></td>' +
                    '<td></td>' +
                    '<td><span class="trolleydialogheaderred">Prezzo</span></td>' +
                    '<td></td>' +
                    '</tr>' +
                    '<tr>' +
                    '<td colspan="11" height="5"></td>' +
                    '</tr>');

            var optionString = "";

            //	Span the whole list of trolley items.
            for ( var i = 0 ; i < trolleyContent.length ; i++ )
            {
                optionString = "<br/>";

                if ( trolleyContent[i].priceOptionRate != null )
                {
                    if ( trolleyContent[i].packaging != "" )
                    {
                        optionString += " " + trolleyContent[i].packaging;
                    }

                    optionString += " " + trolleyContent[i].priceOptionString;
                }
                else
                {
                    if ( trolleyContent[i].packaging != "" )
                    {
                        optionString += " " + trolleyContent[i].packaging;
                    }

                    if ( trolleyContent[i].multipack != "" )
                    {
                        optionString += " " + trolleyContent[i].multipack + " " + trolleyContent[i].unitText;
                    }
                }

                if ( trolleyContent[i].otherOptionsArray != null && trolleyContent[i].otherOptionsArray.length > 0 )
                {
                    optionString += " - ";

                    for ( var optI = 0 ; optI < trolleyContent[i].otherOptionsArray.length ; optI++ )
                    {
                        optionString += (optI >= 1 ? " - " : "") + trolleyContent[i].otherOptionsArray[optI];
                    }

                    optionString += " - ";
                }
                else
                {
                    optionString += " ";
                }

                if ( trolleyContent[i].unitPrice != 0 ) optionString += " " + formatEuro(trolleyContent[i].unitPrice) + " Euro" +
                                                                        trolleyContent[i].unitPriceText.substring(4);

                roundedRetailPrice = Math.round(trolleyContent[i].retailPrice * 100) / 100;
                roundedOriginalPrice = Math.round(trolleyContent[i].originalRetailPrice * 100) / 100;

                var infoTooltip = '';

                if ( trolleyContent[i].deliveryDayExceptionString != null )
                {
                    infoTooltip = ('' + trolleyContent[i].deliveryDayExceptionString).replace(/X/g, '\n');
                }

                if ( trolleyContent[i].leadTime != 0 )
                {
                    if ( trolleyContent[i].deliveryDayExceptionString != null ) infoTooltip += '\n';

                    infoTooltip += trolleyContent[i].leadTimeTooltip;
                }

                if ( trolleyContent[i].surcharge != 0.0 )
                {
                    if ( trolleyContent[i].deliveryDayExceptionString != null ||
                         trolleyContent[i].leadTime != 0 )
                    {
                        infoTooltip += '\n';
                    }

                    if ( trolleyContent[i].surcharge != 0.0 )
                    {
                        infoTooltip += surchargeTooltip.replace(/Y/, trolleyContent[i].surcharge).replace(/X/, trolleyContent[i].surchargeGroup);
                    }
                    else
                    {
                        infoTooltip += limitedProductGroupTooltip.replace(/X/, trolleyContent[i].surchargeGroup);
                    }
                }

                //	Choose correct info icon image.
                var infoIconImg = "trly/carrello_info.gif";

                if ( trolleyContent[i].infoIcon != null && trolleyContent[i].infoIcon != '' )
                {
                    infoIconImg = 'promo_icons/trlycontent-' + trolleyContent[i].infoIcon;
                }

                productList.push(
                        '<tr>' +
                        '<td align="center"><span class="trolleydialogproductsred">' + trolleyContent[i].quantity + ' x</span></td>' +
                        '<td colspan="2" height="25"></td>' +
                        '<td colspan="2"><span class="trolleydialogproductsblu">' +
                        checkStringLength(trolleyContent[i].description, maxCuttedLength) + optionString + '</span></td>' +
                        '<td></td>' +
                        (trolleyContent[i].showInfoIcon ?
                         '<td align="center"><img src="' + layoutImagesUrl + infoIconImg + '" title="' + infoTooltip + '" border="0" alt="" /></td>' :
                         '<td align="center"><img src="' + layoutImagesUrl + 's.gif" border="0" alt="" /></td>'
                                ) +
                        '<td colspan="2"></td>' +
                        '<td>' +
                        '<span class="trolleydialogproductprice">Euro</span><br>' +
                        (roundedRetailPrice < roundedOriginalPrice ? '<span class="trolleydialogproductpricedis">Euro</span>' : "") +
                        '</td>' +
                        '<td align="right">' +
                        '<span class="trolleydialogproductprice">' +
                        formatEuro(roundedRetailPrice) +

                        '</span>' +
                        '<br>' +
                        (roundedRetailPrice < roundedOriginalPrice ?
                         '<span class="trolleydialogproductpricedis">' +
                         formatEuro(roundedOriginalPrice) +
                         '</span>' : "") +
                        '</td>' +
                        '</tr>' +
                        (i != (trolleyContent.length - 1) ?
                         '<tr>' +
                         '<td colspan="11"><img src="' + layoutImagesUrl + 's.gif" width="32" height="1" border="0" alt="" /></td>' +
                         '</tr>' +
                         '<tr>' +
                         '<td colspan="11" class="trolleydialogspacer"><img src="' + layoutImagesUrl +
                         's.gif" width="32" height="1" border="0" alt="" /></td>' +
                         '</tr>' +
                         '<tr>' +
                         '<td colspan="11"><img src="' + layoutImagesUrl + 's.gif" width="32" height="2" border="0" alt="" /></td>' +
                         '</tr>' : '')
                        );
            }

            if ( i == 0 )
            {
                //	Empty trolley!
                productList.push(
                        '<tr>' +
                        '<td align="center"><span class="trolleydialogproductsred"></span></td>' +
                        '<td colspan="2" height="25"></td>' +
                        '<td colspan="2"><span class="trolleydialogproductprice">' + emptyTrolleyMessage + '</span></td>' +
                        '<td></td>' +
                        '<td align="center"></td>' +
                        '<td colspan="2"></td>' +
                        '<td></td>' +
                        '<td align="right"></td>' +
                        '</tr></tr>'
                        );
            }

            productList.push('</table>');
            trolleyDialogProducts.innerHTML = productList.join('');


            //	Add reintegrated products section.
            var trolleyDialogReintegrated = top.workframe.document.getElementById("trolleydialogreintegrated");
            var reintegratedList = new Array();

            reintegratedList.push(
                    '<table border="0" cellspacing="0" cellpadding="0">' +
                    '<tr>' +
                    '<td><img src="' + layoutImagesUrl + 's.gif" width="46" height="1" border="0" alt="" /></td>' +
                    '<td width="100%"><img src="' + layoutImagesUrl + 's.gif" width="90" height="1" border="0" alt="" /></td>' +
                    '<td><img src="' + layoutImagesUrl + 's.gif" width="4" height="1" border="0" alt="" /></td>' +
                    '<td><img src="' + layoutImagesUrl + 's.gif" width="120" height="1" border="0" alt="" /></td>' +
                    '<td><img src="' + layoutImagesUrl + 's.gif" width="18" height="1" border="0" alt="" /></td>' +
                    '</tr>');

            //	Promo footer.
            reintegratedList.push(
                    '<tr>' +
                    '<td colspan="5" height="2"></td>' +
                    '</tr>' +
                    '</table>');

            trolleyDialogReintegrated.innerHTML = reintegratedList.join('');


            //	Update total price.
            var trolleyDialogTotal = top.workframe.document.getElementById("trolleydialogtotal");
            trolleyDialogTotal.innerHTML = formatEuro(Math.round(totalPrice * 100) / 100);

            setTrolleyGeometry(trolleyDialog);
        }
    }
    catch(e) { /* Nothing */ }
}


/**
 Update total price.
 */
function updateTotalPrice()
{
    //noinspection JSUnusedLocalSymbols
    try
    {
        var trolleySummaryTag = top.bottomframeleftfront.document.getElementById("trolleySummary");

        if ( trolleySummaryTag )
        {
            // Disabled - total price has been hidden.
            // var totalPriceString = "" + formatEuro(Math.round(totalPrice * 100) / 100) + " euro";

            if ( freeVisitFlag )
            {
                trolleySummaryTag.value = " " + freeVisitTrolleyMessage;
            }
            else
            {
                if ( productNumber <= 0 )
                {
                    trolleySummaryTag.value = " " + emptyTrolleyMessage;
                }
                else if ( productNumber == 1 )
                {
                    //noinspection JSDuplicatedDeclaration
                    var updatedMessage = oneProductTrolleyMessage;
                    //updatedMessage = updatedMessage.replace(/Y/, totalPriceString);
                    trolleySummaryTag.value = " " + updatedMessage;
                }
                else
                {
                    //noinspection JSDuplicatedDeclaration
                    var updatedMessage = manyProductsTrolleyMessage;
                    //updatedMessage = updatedMessage.replace(/X/, productNumber).replace(/Y/, totalPriceString);
                    updatedMessage = updatedMessage.replace(/X/, productNumber);
                    trolleySummaryTag.value = " " + updatedMessage;
                }
            }
        }
    }
    catch( e ) { /* Nothing */ }
}


/**
 Empty trolley
 */
function emptyTrolley()
{
    //  noinspection JSUnusedLocalSymbols
    try
    {
        // Disable continue button.
        disableContinueToSlotPage();

        //	Close group limit popups.
        closeExceptionPopups();

        //	Hide dialog.
        hideDialogs();
    }
    catch(e) {}

    //	Check empty trolley condition.
    if ( !freeVisitFlag )
    {
        //	Check for ssl pages (checkout problem with empty trolley).
        //noinspection JSUnusedLocalSymbols
        try
        {
            top.workframe.testingssl = true;
        }
        catch(e) { return; }

        if ( top.updatedProductInTrolley == 0 )
        {
            //	Enable continue button.
            enableContinueToSlotPage();
            alert(trolleyAlreadyEmpty);
            return;
        }

        if ( window.confirm(trolleyEmptyConfirm) ) {
            //	Submit form (empty action).
            submitFormInOtherFrame(top.bottomframeleft.document, "trolleyForm", emptyTrolleyUrl, null, null);
        } else {
            //	Enable continue button.
            enableContinueToSlotPage();
        }
    }
    else
    {
        //	Enable contine button.
        enableContinueToSlotPage();
        submitFormInOtherFrame(top.bottomframeleft.document, "trolleyForm", emptyTrolleyUrl, null, null);
    }
}


/** Reload work area if needed. */
function reloadWorkAreaOnTrolleyChange()
{
    //	Check if we need to reload workarea.
    if(top.workframe.reloadAreaOnTrolleyChng) {
        top.workframe.document.location = (top.workframe.reloadAreaLocation) ? top.workframe.reloadAreaLocation : top.workframe.document.location;
    }
}


function checkEducationalPopupsForGrps()
{
    //	Iterate over every element of the associative array.
    var limitedGrpHash = $H(showEduPopupLimitedGrpFlagArray);
    limitedGrpHash.each(function(entry) {
        if(entry.value) openPopup(showEducationalPopupUrl + "?actionMethod=limitedGrp&groupId=" + entry.key, entry.key, 486, 135);
    });
}


/**
 Initialize trolley content.
 */
function initLeftTrolley()
{
    //	Update total price.
    updateTotalPrice();

    //	Show trolley dialog (only if needed).
    showTrolleyDialog();
}

function initLeftFrontTrolley()
{
    //	Update total price.
    updateTotalPrice();
}