
        /***********************************************************************
        *
        * Shopping Cart Javascript        -    contains code to convert the Actinic 
        *                                    cart to AJAX call and add popups to 
        *                                    display information.
        *
        * Tod Designs (c) 2011
        * 
        * Issue V1.01.0051 a
        * Revised 12/1/2011  V1.00.0041    
        * to add a popup to the form that is used to 
        * add product to cart. This popup displays error messages, and the
        * the Cart popup only appears if products are added to cart.
        * Improved error handling when extracting messages from returned page.
        *
        * Revised 18/1/2011  V1.00.0042 
        * to increase amount of button that is active, added
        * theme margins to cart button pop up.
        * 
        * Revised 22/1/2011  V1.00.0046
        * This version links the main cart pop up to the form itself.
        * 
        * Revised 26/1/2011  V1.01.0047
        * Added new code to attach a popup to the cart summary instead of 
        * using a popup that is displaying the cart contents. This uses
        * code inside the Actinic layouts so that Actinic variables can control
        * how the code works
        * 
        * Revised 6/2/2011 V1.00.0050
        * Added option to select the theme and the popup delay
        * 
        * Revised 9/2/2011 V1.00.0051
        * Removed the close link on a pop up
        * 
        * MUST HAVE FUNCTION
        * showrequest()
        * create_cart_popup()
        * append_basket()
        * in actinic layout for this to work.
        * 
        ************************************************************************/
    
        
        function display_cart($form, PopupType) {
            var seconds_to_wait ;
            seconds_to_wait = PopupDelay();
            $('.form_button_add_to_cart').HideAllBubblePopups();  
            if (PopupType == 'form') {
                $form.ShowAllBubblePopups();  
                $form.FreezeBubblePopup();                 
            } else {
                $('#myBasket').ShowAllBubblePopups();  
                $('#myBasket').FreezeBubblePopup();                 
            }             
            function doCountdown(){
                var timer = setTimeout(function(){
                    seconds_to_wait--;
                    if(seconds_to_wait > 0){
                        doCountdown();
                    }else{
                        $form.RemoveBubblePopup();  
                        $('#myBasket').RemoveBubblePopup();   
                    };
                },1000);
            };
            doCountdown();
        }    
        
        function showresponse(responseText, statusText, xhr, $form) {
            // the function append_basket() uses actinic variables so is defined in an actinic layout.
            myText = append_basket();
            PopupType = 'form';
            // an eorror occurred lets see if we can find it.
            if (myText =='FAIL') {
                // set default message
                myError = "<span class=\"actrequiredcolor\"> THE ITEM WAS NOT ADDED TO THE CART<br /> </span>"
                // first find the start of body
                startpoint= responseText.search("<body") + 18;
                // now find the first error string
                startpoint = responseText.indexOf("actrequiredcolor",startpoint) + 18;
                // did we find it
                if (startpoint > 18) {
                    // see if we can find a second string
                    startMessage = responseText.indexOf("actrequiredcolor",startpoint) + 18;
                    // sometimes there is no second message
                    if (startMessage < 18) {
                        startMessage =  startpoint;
                    }
                    // looking for end of message
                    LengthMessage   = responseText.indexOf("</span>",startMessage) - startMessage;
                    //get the error message otherwise we will use the default set above 
                    if (LengthMessage > 0) {
                        myError = "<span class=\"actrequiredcolor\"> THE ITEM WAS NOT ADDED TO THE CART<br /> " + responseText.substr(startMessage, LengthMessage) + "</span>"
                    }
                }
                //create a new popup to display the error attaching it to the product that generated the error
                $form.CreateBubblePopup({ 
                    position: 'bottom',
                    align: 'center',
                    mouseover: 'hide',
                    manageMouseEvents: false,
                    innerHtml: myError, 
                    innerHtmlStyle: {
                                        color:'#FF0000', 
                                        'text-align':'center'
                                    },
                    tail: { align: 'center', hidden: true },                      
                    themeName:     'orange',
                    themePath:     'jquerybubblepopup-theme'
                });
            } else {
                //create a new popup to display the cart
                //myText = myText + '<p><a onclick="$form.RemoveBubblePopup();" >Close</a> <br /><br /></p>'
                PopupType = create_cart_popup($form,myText);
            }    
            // timed display of cart or error message
            display_cart($form, PopupType);
        }
        
        $(document).ready(function() { 
            // over write default basket
            append_basket();
            var newTheme =  PopupTheme();
            var allTheme = 'all-' +  newTheme;
            // create the popups
            $('.form_button_add_to_cart').CreateBubblePopup({
                align: 'right',
                innerHtml: 'click to add <br />the product!', 
                innerHtmlStyle: {
                                    color: newTheme!='yellow' ? '#FFFFFF' : '#333333', 
                                    'text-align':'center'
                                },                    
                themeMargins:   {
                                    total: '8px',
                                    difference: '5px'
                                },                    
                themeName:     allTheme,
                themePath:     'jquerybubblepopup-theme'
            });
                        
            // convert product form to use AJAX
            if($('.ProductForm').length > 0) {
                $('.ProductForm').ajaxForm({
                    beforeSubmit:  showrequest,
                    success: showresponse
                });
            }

            // display when add to cart is pressed
            $('.form_button_add_to_cart').click(function(){            
                var shoppingcart_button = $(this);
                var bubble_popup_id = shoppingcart_button.GetBubblePopupID(); 
                shoppingcart_button.ShowBubblePopup({        
                    align: 'center',
                    innerHtml:     '<p>Product is being added to the cart</p>',                            
                    innerHtmlStyle:{ 
                                    color:'#666666', 
                                    'text-align':'left'
                    },                    
                    themeName:     newTheme,
                    themePath:     'jquerybubblepopup-theme'
                }, false); 
                shoppingcart_button.FreezeBubblePopup();         
                $('#'+bubble_popup_id+' a:last').click(function(){
                    $(shoppingcart_button).HideBubblePopup();
                });    
            }); 
        });

        function getMiniCart(){

            var tableOpen="<table width='100%'>";
            var itemLine="<tr><td valign='top'>VAR:QUANTITYx </td><td><a href='VAR:LINK'>VAR:TITLE...</a></td><td align='right' valign='top'>VAR:PRICE</td></tr>"
            var shippingLine="<tr><td colspan='2' align='right'>Shipping</td><td align='right'>VAR:SHIPPING</td></tr>"
            var vatLine="<tr><td colspan='2' align='right'>VAT</td><td align='right'>VAR:VAT</td></tr>"
            var discountLine="<tr><td colspan='2' align='right'>Discounts</td><td align='right'>VAR:DISCOUNT</td></tr>"
            var surchargeLine="<tr><td colspan='2' align='right'>Surcharges</td><td align='right'>VAR:SURCHARGE</td></tr>"
            var totalLine="<tr><td colspan='2' align='right'><b>Total</b></td><td align='right'><b>VAR:TOTAL</b></td></tr>"
            var tableClose="</table>"
            var emptyLine="No items in cart.";

            var cookie=getCartItem(4);

            if (!cookie){
                return emptyLine;
            }
            var re=new Array();
            var match=new Array();
            var total=0;
            var str;
            re[1]=/cur=([^&]*)&!/g;
            re[2]=/&!(.*)/g;
            re[3]=/ss=(.*)cur=/i;
            re[4]=/tx=(.*)sh=/i;
            re[5]=/sh=(.*)ss=/i;
            for (i=1;i<re.length;i++){
                match[i]=re[i].exec(cookie);
                match[i]=match[i][1];
            }
            lines=match[2].split("&!");
            store=new Array();
            re[0]=/^(\d*)x/;
            re[1]=/x([^>]*)>/;
            re[2]=/>([^>]*)>/;
            re[3]=/>([^>]*)$/;
            str=tableOpen;
            for (i=0;i<lines.length;i++){
                for (j=0;j<re.length-2;j++){
                    detail=lines[i].match(re[j]);
                    store[j]=detail[1];
                }
                str+=itemLine.replace("VAR:QUANTITY",store[0]).replace("VAR:LINK",match[3]+"?PRODREF="+store[1]+"&NOLOGIN=1").replace("VAR:TITLE",store[2]).replace("VAR:PRICE",match[1]+((store[3]*1).toFixed(2)));
                total+=(store[3]*1);
            }
            if (match[5] > 0){
                str+=shippingLine.replace("VAR:SHIPPING",match[1]+(match[5]/100).toFixed(2));
                total=total+=(match[5]/100);
            }
            if (match[4] > 0){
                str+=vatLine.replace("VAR:VAT",match[1]+(match[4]/100).toFixed(2));
                total=total+=(match[4]/100);
            }
            gTotal=getCartItem(1);
            var r=/&#(\d*);/gi;
            var m=new Array();
            var f=new Array();
            while((m = r.exec(gTotal))!=null){
                f[f.length]=m[1];
            }
            for (i=0;i<f.length;i++){
                gTotal=gTotal.replace("&#"+f[i]+";","");
            }
            total=total.toFixed(2);
            gTotal=(gTotal/100).toFixed(2);
            if (total>gTotal){
                str=str+=discountLine.replace("VAR:DISCOUNT","-"+match[1]+(total-gTotal).toFixed(2));
            }
            else if (total<gTotal){
                str=str+=surchargeLine.replace("VAR:SURCHARGE",match[1]+(gTotal-total).toFixed(2));
            }
            str+=totalLine.replace("VAR:TOTAL",match[1]+(gTotal));
            str+=tableClose;
            return str;
        }
