﻿function updateSummary(optionSelected)
{
    if(!$(optionSelected).getParent('td').getNext('td').getFirst('strong').get('text').contains("included"))
    {
        var selectedPriceArray  = $(optionSelected).get('value').split(':');  
        var priceSelected = parseFloat(selectedPriceArray[1]);

        var optionList = $(optionSelected).getParent('div').getElements('input');

        for (var i = 0; i < optionList.length ; i++)
        {
            if(optionList[i].get('checked'))
            {
                var addCostText = optionList[i].getParent('td').getNext('td').getFirst('strong').get('text');
                var currentCost = parseFloat($('summaryDiv').getElement('p').getElement('span').get('text'));
                if(addCostText.contains("Add"))
                {
                    currentCost = currentCost + (+addCostText.substring(addCostText.indexOf('£')+1,addCostText.indexOf(']')));
                } 
                else
                {
                    currentCost = currentCost - (+addCostText.substring(addCostText.indexOf('£')+1,addCostText.indexOf(']')));
                }
                optionList[i].getParent('td').getNext('td').getFirst('strong').set('text','[included in price]');
                $('pcBuilderCost').getElement('span').set('text',currentCost.toFixed(2));
                var VAT = 0.15;
								var priceIncVat = currentCost * (1 + VAT);
								$('pcBuilderCostIncVat').getElement('span').set('text',priceIncVat.toFixed(2));	
            }
            else
            {
                var idPriceSplit = optionList[i].get('value').split(':');
                var price = parseFloat(idPriceSplit[1]);
                price = price - priceSelected;
                if(price > 0)
                {
                    optionList[i].getParent('td').getNext('td').getFirst('strong').set('text','[Add £'+ price.toFixed(2) +']');
                }
                else
                {
                    price = price * -1;
                    optionList[i].getParent('td').getNext('td').getFirst('strong').set('text','[Subtract £'+ price.toFixed(2) +']');
                }
            }
        }
    }
}

function createCustomPCCookie(cookieName)
{
	var optionList = $('pcBuilder').getElements('input');
	var itemsList = "";
  for (var i = 0; i < optionList.length ; i++)
  {
		if(optionList[i].get('checked'))
		{
			var optionID = optionList[i].get('value');
			var optionText = optionList[i].getParent('td').getNext('td').get('text').replace('[included in price]','');
			itemsList += optionID + "|~|" + optionText + ":~:";    
		}
	}
	Cookie.write(cookieName,itemsList,{});       
}


function populatePCBuilderPrice(price)
{
	var VAT = 0.15;
	var priceIncVat = price * (1 + VAT);
	$('pcBuilderCost').getElement('span').set('text',price);	
	$('pcBuilderCostIncVat').getElement('span').set('text',priceIncVat.toFixed(2));	
}

function activateInfo(activeElement)
{
	activeDiv = $(activeElement).getParent('tr').getNext('tr').getFirst('td').getFirst('div');
	if(activeDiv.hasClass('active'))
	{
		activeDiv.removeClass('active');
		activeDiv.addClass('inactive');
	}
	else
	{
		activeDiv.removeClass('inactive');
		activeDiv.addClass('active');
	}
}

function closeInfo(activeElement)
{
	activeDiv = $(activeElement).getParent('div').getParent('div');
	activeDiv.removeClass('active');
	activeDiv.addClass('inactive');
}
