
		/***********************************************************************
		*
		* 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.0050   
        * 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
        * 
        * 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><br />' + 
									'<p><a href="#null">close</a></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();
				});	
			}); 
		});
