﻿
window.addEvent('domready', function() {
    writeCartSummary();     
})

function writeCartSummary()
{
    if (Cookie.read("shoppingCart"))
    {
        $('itemCount').set('text', '');
        $('totalPrice').set('text', '');
        
        var totalPrice = 0.00;
        var itemCount = 0;

        var shoppingCart = Cookie.read("shoppingCart");

        var itemArray = shoppingCart.split('&');

        for (var i = 0; i < itemArray.length ; i++)
        {
            var eachItem = new Array();
            eachItem = itemArray[i].split(':~:');

            var price = eachItem[2];
            var numItems = eachItem[3];
            itemCount = (itemCount + parseInt(numItems));
            totalPrice = (totalPrice + ((+price) * numItems));            
        }
        $('itemCount').set('text', 'items: ' + itemCount);
        $('totalPrice').set('text', '£' + totalPrice.toFixed(2));
        
        if($('shoppingCart'))
        {
            if($('checkoutForm'))
            {
                $('itemsTable').set("html", "<table><tr><th class='alignLeft'>product</th><th>price</th><th>quantity</th><th>line price</th></tr>"+getItemRows(itemArray)+"</table>","<p>sub-total: £"  +totalPrice.toFixed(2) + "</p>");
                var listItemArray = $('cartTotal').getChildren('li');
                listItemArray[0].set('text','Items £' +totalPrice.toFixed(2));
                listItemArray[2].set('text','Order Total £' +totalPrice.toFixed(2));
                countryChange($('billingCountryTxt'));
            }
            else
            {
                $('itemsTable').set("html", "<table><tr><th class='alignLeft'>product</th><th>price</th><th>quantity</th><th>line price</th></tr>"+getItemRows(itemArray)+"</table>","<p>sub-total: £"  +totalPrice.toFixed(2) + "</p>"+"<p><a href='checkout.html' title='checkout'>checkout</a></p>");   
            }
        }
    }
    else
    {
        if($('shoppingCart'))
        {
            var anchorArray = $('shoppingCart').getElements('a');
            for (var i = 0; i < anchorArray.length ; i++)
            {
                anchorArray[i].toggleClass('inactiveCart');
            }           
        }
    }
}

function getItemRows(itemArray)
{
    var itemRows = "";
    for (var i = 0; i < itemArray.length ; i++)
    {
        var eachItem = new Array();
        eachItem = itemArray[i].split(':~:');

        var itemDescription = eachItem[1];
        var price = eachItem[2];
        var numItems = eachItem[3];
        var itemLink = eachItem[4];
        var linePrice = ((+numItems) * (+price))
        itemRows += "<tr><td class='cartDescription'><a href='"+itemLink+"' title='"+itemDescription+"'>"+itemDescription+"</a></td><td>&pound;"+price+"</td><td><input type='text' value='"+numItems+"' /></td><td>&pound;"+ linePrice.toFixed(2)+"</td><td><a href='#' onclick='removeItem("+i+"); return false' title=''>remove</a></td></tr>";
    }
    return itemRows;
}

function updateCart()
{
    if (Cookie.read("shoppingCart"))
    {
        var oldShoppingCart = Cookie.read("shoppingCart");
        var itemArray = oldShoppingCart.split('&');
        var itemAmounts = $('itemsTable').getElements('input');

        var newShoppingCart = "";

        for (var i = 0; i < itemArray.length ; i++)
        {
            var eachItem = new Array();
            eachItem = itemArray[i].split(':~:');
            eachItem[3] = itemAmounts[i].get('value');
            
            if(i == 0)
            {
                newShoppingCart =  eachItem[0]+ ":~:" + eachItem[1]+ ":~:" + eachItem[2]+ ":~:" + eachItem[3]+ ":~:" + eachItem[4];
            }
            else
            {
                newShoppingCart += "&" + eachItem[0]+ ":~:" + eachItem[1]+ ":~:" + eachItem[2]+ ":~:" + eachItem[3]+ ":~:" + eachItem[4];
            }
        }
        Cookie.write("shoppingCart",newShoppingCart);
        writeCartSummary();
    }
}

function removeItem(itemIndex)
{
    var oldShoppingCart = Cookie.read("shoppingCart");
    var newShoppingCart = "";
    var itemArray = oldShoppingCart.split('&');
    if(itemArray.length == 1)
    {
        emptyCart()
    }
    else
    {
        var firstItem = true;    
        for (var i = 0; i < itemArray.length ; i++)
        {
            if(i != itemIndex)
            {
                if(firstItem)
                {
                    newShoppingCart = itemArray[i];
                    firstItem = false;
                }
                else
                {
                    newShoppingCart += "&" + itemArray[i];
                }
            }
        }
        Cookie.write("shoppingCart",newShoppingCart);
        writeCartSummary();
    }       
}


function addItem(itemID, price, thisObject)
{
    var numItems = $(thisObject).getParent('div').getElement('input').get('value');
    if(numItems < 1)
    {
        numItems = 1;
    }
        
    if($(thisObject).getParent('div').getProperty('id') == "productDetail")
    {
        var itemLink = document.URL.toLocaleString();
        var itemDescription = $(thisObject).getParent('div').getParent('div').getElement('h1').get('text');
    }
    else
    { 
        var itemLink = $(thisObject).getParent('div').getElement('a').getNext('a').get('href');
        var itemDescription = $(thisObject).getParent('div').getElement('a').getNext('a').get('text');
    }
    
    if (Cookie.read("shoppingCart"))
    {
        var shoppingCart = Cookie.read("shoppingCart");     
    
        //break existing cookie up into individual items in array
        var itemArray = shoppingCart.split('&');

        //initialise new cookie
        var newShoppingCart = "";
        
        var existingItem = -1;
        for (var i = 0; i < itemArray.length ; i++)
        {
            //break up item into separate parts
            var eachItem = new Array();
            eachItem = itemArray[i].split(':~:');
            
            //check to see if this item is already in the shopping basket
            if(eachItem[0] == itemID)
            {
                existingItem = i;
            }
        }
        
        if(existingItem >= 0)
        {
            //break up item into separate parts
            var eachItem = new Array();
            eachItem = itemArray[existingItem].split(':~:');
            
            itemArray[existingItem] = itemID + ":~:" + itemDescription + ":~:" + price + ":~:" + (parseInt(numItems) + parseInt(eachItem[3])) + ":~:" + itemLink;
            for (var i = 0; i < itemArray.length ; i++)
            {
                if(i==0)
                {
                    newShoppingCart = itemArray[i];
                }
                else
                {
                    newShoppingCart += "&" + itemArray[i];
                }
            }
            Cookie.write("shoppingCart",newShoppingCart);       
        }
        else
        {
            shoppingCart += "&" + itemID + ":~:" + itemDescription + ":~:" + price + ":~:" + numItems + ":~:" + itemLink;
            Cookie.write("shoppingCart",shoppingCart);       
        }
        writeCartSummary();
    }
    else
    {
        Cookie.write("shoppingCart",itemID + ":~:" + itemDescription + ":~:" + price + ":~:" + numItems+ ":~:" + itemLink);       
        writeCartSummary();
    }
	$(thisObject).addClass('addedProduct');
    (function(){ $(thisObject).removeClass('addedProduct'); }).delay(3000);
}

function emptyCart()
{
    if (Cookie.read("shoppingCart"))
    {
        Cookie.dispose("shoppingCart");
        $('itemCount').set('text', 'items: 0');
        $('totalPrice').set('text', '£0.00');
        if($('shoppingCart'))
        {
            var anchorArray = $('shoppingCart').getElements('a');
            for (var i = 0; i < anchorArray.length ; i++)
            {
                anchorArray[i].toggleClass('inactiveCart');
            }
            
            $('itemsTable').set('html', '<p>Your Cart is empty</p>'); 
            if($('checkoutForm'))
            {
                var listItemArray = $('cartTotal').getChildren('li');
                listItemArray[0].set('text','Items £0.00');
                listItemArray[2].set('text','Order Total £0.00');
                countryChange($('billingCountryTxt'));
            }
        }
    } 
    var mySlide = new Fx.Slide('toggled');
    mySlide.slideOut();
}

sfHover = function() {
		var sfEls = document.getElementById("topNav").getElementsByTagName("LI");
		for (var i=0; i<sfEls.length; i++) {
			sfEls[i].onmouseover=function() {
				this.className+=" sfhover";
			}
			sfEls[i].onmouseout=function() {
				this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
			}
		}
	}
if (window.attachEvent) window.attachEvent("onload", sfHover);

function paypalCheckout()
{
    if (Cookie.read("shoppingCart"))
    {
        $('checkoutForm').set('action','paypal-checkout.aspx')
        $('checkoutForm').submit();
    }
}

//function updateItem(itemIndex, thisInputElement)
//{
//    var oldShoppingCart = Cookie.read("shoppingCart");
//    var newShoppingCart = "";
//    var itemArray = oldShoppingCart.split('&');

//    for (var i = 0; i < itemArray.length ; i++)
//    {
//        if(i == itemIndex)
//        {
//            var eachItem = new Array();
//            eachItem = itemArray[i].split(':');
//            eachItem[3] = $(thisInputElement).get('value');
//            
//            if(i == 0)
//            {
//                newShoppingCart =  eachItem[0]+ ":" + eachItem[1]+ ":" + eachItem[2]+ ":" + eachItem[3]+ ":" + eachItem[4];
//            }
//            else
//            {
//                newShoppingCart += "&" + itemArray[i];
//            }
//        }
//    }
//    Cookie.write("shoppingCart",newShoppingCart);
//    writeCartSummary();
//}