if(!bundleBuilder) var bundleBuilder = {};
bundleBuilder.currentBundle = [1,1,1];
bundleBuilder.activeExtras = [0,0];
bundleBuilder.PurchaseFixes = [["=10984&id=10991","=10998"]]; // Workaround to add one id when taking both Sky collections..

bundleBuilder.extras = [
	[ { "Name": "VPlus",
		"Title": "<a href='/html/tv/vplus-hd-box.html'>V+HD Box</a>",
		"Price": 5,
		"Content": "<p><em class='offer'>&pound;75 one-off payment</em><br />Plus &pound;5 a month</p>\n",
		"Purchase": "id=10961",
		"Tracking": "VPlus" },
	  { "Name": "VPlus",
		"Title": "<a href='/html/tv/vplus-hd-box.html'>V+HD Box</a>",
		"Price": 0,
		"Content": "<p><em class='offer'>&pound;75 one-off payment</em></p>\n",
		"Purchase": "id=10961",
		"Tracking": "VPlus" } ],
	[ { "Name": "SkyMovies",
		"Title": "Sky Movies Collection",
		"Price": 26.50,
		"Content": "<p>Get Sky Movies 1 &amp; 2 plus the latest films on Sky Movies Premier</p>\n",
		"Purchase": "id=10984",
		"Tracking": "SkyMoviesCollection",
		"Discounts": [[2,1],[3,3]] },
	  { "Name": "SkyMovies",
		"Title": "Sky Movies Collection",
		"Price": 25.50,
		"Content": "<p>Get Sky Movies 1 &amp; 2 plus the latest films on Sky Movies Premier</p>\n",
		"Purchase": "id=10984",
		"Tracking": "SkyMoviesCollection",
		"Discounts": [[2,1],[3,3]] }	,
	  { "Name": "SkyMovies",
		"Title": "Sky Movies Collection",
		"Price": 19.50,
		"Content": "<p>Get Sky Movies 1 &amp; 2 plus the latest films on Sky Movies Premier</p>\n",
		"Purchase": "id=10984",
		"Tracking": "SkyMoviesCollection",
		"Discounts": [[2,2],[3,4]] } ],
	[ { "Name": "SkySports",
		"Title": "Sky Sports Collection",
		"Price": 24,
		"Content": "<p>Get Sky Sports 1, Sky Sports 2, Sky Sports 3 &amp; Sky Sports Xtra</p>\n",
		"Purchase": "id=10991",
		"Tracking": "SkysportsCollection",
		"Discounts": [[2,3],[3,1]] },
	  { "Name": "SkySports",
		"Title": "Sky Sports Collection",
		"Price": 20.50,
		"Content": "<p>Get Sky Sports 1, Sky Sports 2, Sky Sports 3 &amp; Sky Sports Xtra</p>\n",
		"Purchase": "id=10991",
		"Tracking": "SkysportsCollection",
		"Discounts": [[2,4],[3,2]] } ]
];
	
bundleBuilder.promotions = [
	{
		"ID": "phone-note",
		"Copy": [
			"*When you take a <a href='/html/phone/phoneline.html'>Virgin phone line</a> for &pound;11 a month"
		]
	},
	{
		"ID": "online-discount",
		"Copy": [ 
			"<img alt='Free installation' src='/images/bundle-builder/free-install-stamp.gif'/>",
			"<img alt='&pound;20 off when you buy online' src='/images/bundle-builder/discount-vplus.gif'/>"
		]
	}
];

bundleBuilder.defaults = {
	"Promotions": [[1,1], [2,1]],
	"Extras": [[1,1], [2,1], [3,1]],
	"Discounts": [[1,1]],
	"ContractTerm": 12,
	"Purchase": "/websales/my_basket/shopping-basket.do?",
	"Tracking": "Builder"
};

/* Function to update the display of the current Bundle */
bundleBuilder.displayBundle = function(resetAll) {
	var servicePrice = 0;
	var extrasPrice = 0;
	
	var discountDuration;
	var ongoingServicePrice;
	var ongoingExtrasPrice;
	
	var contractTerm = 0;
	var tracking = "";
	var purchase = "";
	var promos;
	var data = [];
	var currentExtras = [];
	var currentDiscounts = [];
	var redrawExtras = 1;
	var currentPromotions = [];
	var redrawPromotions = 1;
	var tmp = bundleBuilder.productData.Bundles;
	
	data.push(bundleBuilder.defaults);
	
	// Cycle over the current services
	for(var serviceID=0; serviceID<bundleBuilder.productData.Services.length; serviceID++) {
		// Add this service to the list of data
		data.push(bundleBuilder.productData.Services[serviceID]);
		var service = bundleBuilder.productData.Services[serviceID].Name;
		for(var option=0; option<document.forms.tripleBuilder[service].length; option++) {
			if(document.forms.tripleBuilder[service][option].checked) {
				// Set the current bundle variable, and tunnel down in the tmp variable.
				bundleBuilder.currentBundle[serviceID] = option;
				data.push(bundleBuilder.productData.Services[serviceID].Products[option]);
				tmp = tmp[option];
			}
		}
	}
	
	// Add this bundle combination to the list of data
	data.push(tmp);
	
	// Find out what Extras are currently selected (if we aren't reseting the form).
	if(!resetAll) {
		for(var i=0; i<bundleBuilder.extras.length; i++) {
			tmp = document.getElementById('add-extra-' + (i+1));
			if(tmp !== null) {
				if(tmp.checked) {
					bundleBuilder.activeExtras[i] = 1;
				} else {
					bundleBuilder.activeExtras[i] = 0;
				}
			}
		}
	}
	
	// Find out what Extras we should display.
	for(var i=0; i<data.length; i++) {
		if(data[i].Extras != undefined) {
			for(var j=0; j<data[i].Extras.length; j++) {
				var addExtra = 0;

				if(currentExtras[data[i].Extras[j][0] - 1] == undefined) {
					addExtra = 1;
				} else {
					if(currentExtras[data[i].Extras[j][0] - 1] <= data[i].Extras[j][1]) {
						addExtra = 1;
					}
				}
		
				if(addExtra) {
					currentExtras[data[i].Extras[j][0] - 1] = data[i].Extras[j][1];
				}
			}
		}
	}
	
	// Add any active extras to our list of current data and calculate the extras price.
	for(var i=0; i<currentExtras.length; i++) {
		if(bundleBuilder.extras[i][currentExtras[i] - 1] != undefined) {
			if(bundleBuilder.activeExtras[i] > 0) {
				data.push(bundleBuilder.extras[i][currentExtras[i] - 1]);

				if(bundleBuilder.extras[i][currentExtras[i] - 1].Price != undefined) {
					extrasPrice += bundleBuilder.extras[i][currentExtras[i] - 1].Price;
				}
			}
		}
	}
	
	// Find out what Promos we should display.
	for(var i=0; i<data.length; i++) {
		if(data[i].Promotions != undefined) {
			for(var j=0; j<data[i].Promotions.length; j++) {
				var addPromotion = 0;

				if(currentPromotions[data[i].Promotions[j][0] - 1] == undefined) {
					addPromotion = 1;
				} else {
					if(currentPromotions[data[i].Promotions[j][0] - 1] <= data[i].Promotions[j][1]) {
						addPromotion = 1;
					}
				}
		
				if(addPromotion) {
					currentPromotions[data[i].Promotions[j][0] - 1] = data[i].Promotions[j][1];
				}
			}
		}
	}

	tmp ="";
	
	// Determine any applicable discounts.
	for(var i=0; i<data.length; i++) {
		if(data[i].Discounts != undefined) {
			for(var j=0; j<data[i].Discounts.length; j++) {
				if(currentDiscounts[data[i].Discounts[j][0] - 1] != undefined) {
					if(currentDiscounts[data[i].Discounts[j][0] - 1] < data[i].Discounts[j][1]) {
						currentDiscounts[data[i].Discounts[j][0] - 1] = data[i].Discounts[j][1]
					}
				} else {
					currentDiscounts[data[i].Discounts[j][0] - 1] = data[i].Discounts[j][1];
				}
			}
		}
	}
		
	// Calculate the total price - which we will break up shortly.
	for(var i=0; i<data.length; i++) {
		if(data[i].Price != undefined) {
			servicePrice += data[i].Price;
		}
		
		if(data[i].ContractTerm != undefined) {
			if(contractTerm < data[i].ContractTerm) {
				contractTerm = data[i].ContractTerm;
			}
		}
		
		if(data[i].Tracking != undefined) {
			if(tracking != "") {
				tracking += "_";
			}
			
			tracking += data[i].Tracking;
		}
		
		if(data[i].Purchase != undefined) {
			if(purchase != "") {
				purchase += "&";
			}
			
			purchase += data[i].Purchase;
		}
	}
	// Remove the extras price from the total price to determine the actual cost of the services.
	servicePrice -= extrasPrice;
	
	// Calculate the pricing to display, this gets a bit complicated with the application of discounts.
	
	discountDuration = -1;
	ongoingServicePrice = servicePrice;
	ongoingExtrasPrice = extrasPrice;
	
	for(var i=0; i<currentDiscounts.length; i++) {
		if(currentDiscounts[i] != undefined) {
			if((discountDuration == -1)&&(bundleBuilder.productData.Discounts[i][currentDiscounts[i] - 1].Duration != undefined)) {
				discountDuration = bundleBuilder.productData.Discounts[i][currentDiscounts[i] - 1].Duration;
			}
			
			// Check if discount is permanent or matches the current discount duration..
			if(bundleBuilder.productData.Discounts[i][currentDiscounts[i] - 1].Duration == undefined) {
				if(bundleBuilder.productData.Discounts[i][currentDiscounts[i] - 1].SevicePrice != undefined) {
					servicePrice = bundleBuilder.productData.Discounts[i][currentDiscounts[i] - 1].SevicePrice;
					ongoingServicePrice = bundleBuilder.productData.Discounts[i][currentDiscounts[i] - 1].SevicePrice;
				}
				
				if(bundleBuilder.productData.Discounts[i][currentDiscounts[i] - 1].Discount != undefined) {
					servicePrice -=  bundleBuilder.productData.Discounts[i][currentDiscounts[i] - 1].Discount;
					ongoingServicePrice -=  bundleBuilder.productData.Discounts[i][currentDiscounts[i] - 1].Discount;
				}
			} else if(bundleBuilder.productData.Discounts[i][currentDiscounts[i] - 1].Duration == discountDuration) {
				if(bundleBuilder.productData.Discounts[i][currentDiscounts[i] - 1].ServicePrice != undefined) {
					servicePrice = bundleBuilder.productData.Discounts[i][currentDiscounts[i] - 1].ServicePrice;
				}
				
				if(bundleBuilder.productData.Discounts[i][currentDiscounts[i] - 1].Discount != undefined) {
					servicePrice -=  bundleBuilder.productData.Discounts[i][currentDiscounts[i] - 1].Discount;
				}
			}
		}
	}
	
	// Redraw the pricing area.
	if((servicePrice + extrasPrice) != 0) {
		tmp  = '<p class="price">&pound;' + (servicePrice + extrasPrice);

		if(tmp.match(/\.\d$/)) {
			tmp += "0";
		}
		
		if(discountDuration < 1) {
			tmp += ' a month*</p>\n';
		} else {
			tmp += ' for ' + discountDuration + ' months</p>\n';
			tmp += '<p>then &pound;' + (ongoingServicePrice + ongoingExtrasPrice);
			
			if(tmp.match(/\.\d$/)) {
				tmp += "0";
			}
			
			tmp += ' a month*</p>\n';
		}
	} else {
		tmp  = '<p class="price">FREE';
		
		if(discountDuration < 1) {
			tmp += '</p>\n';
		} else {
			tmp += ' for ' + discountDuration + ' months</p>\n';
			tmp += '<p>then &pound;' + (ongoingServicePrice + ongoingExtrasPrice)
			
			if(tmp.match(/\.\d$/)) {
				tmp += "0";
			}
			
			tmp += ' a month*</p>\n';
		}
	}

	if(contractTerm > 0) {
		tmp += '<p class="contract">' + contractTerm + ' month contract</p>';
	}

	document.getElementById('pricing-info').innerHTML = tmp;

	// Redraw the Call to Action.
	for(var i=0; i<bundleBuilder.PurchaseFixes.length; i++) {
		purchase = purchase.replace(bundleBuilder.PurchaseFixes[i][0],bundleBuilder.PurchaseFixes[i][1]);
	}
	
	tmp = '<p class="basket"><a onclick="linkTrack(this,\'' + tracking + '\');" href="' + purchase + '">Add to basket</a></p>\n';
	document.getElementById('call-to-action').innerHTML = tmp;
	
	// Redraw the extras if we need to.
	if(redrawExtras) {
		var extraString = "\n<h2>Add extras</h2>\n";
		
		for(var i=0; i<currentExtras.length; i++) {
			if(currentExtras[i] != undefined) {
				var extra = bundleBuilder.extras[i][currentExtras[i] - 1];
				var checked = ' value="OFF"';
				if(bundleBuilder.activeExtras.length>i) {
					if(bundleBuilder.activeExtras[i] > 0) {
						checked = ' checked="checked"';
					}
				}
				
				extraString += '<div class="extras-option" id="extras-option-' + (i+1) + '">\n';
				extraString += '<h3><label for="add-extra-' + (i+1) + '">' + extra.Title + '</label></h3>\n';
				extraString += '<div class="control"><input type="checkbox" name="add-extra-' + (i+1) + '" id="add-extra-' + (i+1) + '" onclick="bundleBuilder.displayBundle();" ' + checked + ' /></div>\n';
				extraString += '<div id="' + extra.Name + 'Offer">\n';
				extraString += extra.Content + '\n';
				extraString += '</div>\n';
				extraString += '</div>\n';
			}
		}
		
		document.getElementById('extras-section1').innerHTML = extraString;
		
		bundleBuilder.activeExtras = [];
	}
	
	// Redraw the extras if we need to.
	if(redrawPromotions) {
		for(var i=0; i<currentPromotions.length; i++) {
			if(currentPromotions[i] != undefined) {
				document.getElementById(bundleBuilder.promotions[i].ID).innerHTML = bundleBuilder.promotions[i].Copy[currentPromotions[i] - 1];
			}
		}
	}
};

bundleBuilder.InsertTripleBundleBuilder = function() {
	var parms = window.location.search.substring(1).split('&');

	for(var i=0; i<parms.length; i++) {
		if(parms[i].match(/^select=0:\d{3}/)) {
			for(var j=0; (((j+9)<parms[i].length)&&(j<bundleBuilder.currentBundle.length)); j++) {
				var selected = parms[i].charAt(j+9);
				if((selected>0)&&(selected<=bundleBuilder.productData.Services[j].Products.length)) {
					bundleBuilder.currentBundle[j] = selected;
				}
			}
			if(parms[i].match(/^select=0:\d{3}:\d+/)) {
				for(var j=0; ((j+13)<parms[i].length); j++) {
					var selected = parms[i].charAt(j+13);
					if((selected==0)||(selected==1)) {
						bundleBuilder.activeExtras[j] = selected;
					}
				}
			}
		}
	}
	
	document.write('<div class="triple-builder" id="triple-builder">\n');
	document.write('<div class="triple-builder-header" id="triple-builder-header"></div>\n');
	document.write('<div class="triple-builder-content" id="triple-builder-content">\n');
	document.write('<div class="triple-builder-innercontent" id="triple-builder-innercontent">\n');
	document.write('<form name="tripleBuilder">\n');
	document.write('<div class="column-tops"/></div>\n');
	document.write('<div class="columns">\n');
	document.write('<div class="products">\n');

	for(var serviceID=0; serviceID<bundleBuilder.productData.Services.length; serviceID++) {
		var service = bundleBuilder.productData.Services[serviceID].Name;
		document.write('<div class="product-section" id="product-section' + serviceID + '">\n');
		document.write('<img src="' + bundleBuilder.productData.Services[serviceID].Img + '" /><h2>' + service + '</h2>\n');
		for(var productRef=0; productRef<bundleBuilder.productData.Services[serviceID].Products.length; productRef++) {
			var checked = "";
			var size = bundleBuilder.productData.Services[serviceID].Products[productRef].Size;

			if(bundleBuilder.currentBundle[serviceID] == (productRef + 1)) {
				checked = ' checked="checked"';
			}

			document.write('<div class="product-option" id="product-option-' + serviceID + '-' + productRef + '">\n');
			document.write('<h3><label for="' + service + '-Size-' + size + '">Size ' + size + '</label></h3>\n');
			document.write('<div class="control"><input type="radio" name="' + service + '" value="' + size + '" id="' + service + '-Size-' + size + '" onclick="bundleBuilder.displayBundle();" ' + checked + '/></div>\n');
			document.write('<p>' + bundleBuilder.productData.Services[serviceID].Products[productRef].Summary + '<p>\n');
			document.write('<p><a href="' + bundleBuilder.productData.Services[serviceID].Products[productRef].Link + '" title="View details of ' + service + ' Size ' + size + '" onclick="_hbLink(\'' + service + '+Size+' + size + '\');">' + service + ' ' + size + ' &gt;</a></p>\n');
			document.write('</div>\n');
		}
		document.write('</div>\n');
	}

	document.write('<div class="extras-section" id="extras-section1">\n');
	document.write('<h2>Add extras</h2>\n');
	
	document.write('</div>\n');
	document.write('</div>\n');
	document.write('</div>\n');
	document.write('<div class="column-bottoms"></div>\n');
	document.write('<div class="pricing-section" id="pricing-section">\n');
	
	document.write('<div id="what-you-pay"><h2>What you pay</h2></div>\n');
	document.write('<div id="pricing-info">\n');
	document.write('<p class="price"></p>\n');
	document.write('<p class="contract"></p>\n');
	document.write('</div>\n');
	document.write('<div id="call-to-action">\n');
	document.write('</div>\n');
	document.write('<div id="online-discount"></div>\n');

	document.write('</div>\n');
	document.write('<p class="phone-note" id="phone-note"></p>\n');
	document.write('</form>\n');
	document.write('</div>\n');
	document.write('</div>\n');
	document.write('<div class="triple-builder-footer" id="triple-builder-footer"></div>\n');
	document.write('</div>\n');
	
	bundleBuilder.displayBundle(1);
};
 