var bOnOff = false;

function updatesubtotal() {
	var intTotalProducts = document.getElementById( "totalproducts" ).value;
	var dblPricePerPack;
	var intQuantity;
	var dblSubTotal = 0.00;

	for ( var i = 0; i <= intTotalProducts-1; i++ ) {
		dblPricePerPack = document.getElementById( "price_" + i).value;
		intQuantity = document.getElementById( "qty_" + i).value;
		if ( intQuantity > 0 ) {
		    dblSubTotal = dblSubTotal + ( intQuantity * dblPricePerPack );
		}
	}

	document.getElementById( "subtotal" ).innerHTML = "<span class='price'>&pound;" + formatSubTotal( dblSubTotal ) + "</span>";
}

function formatSubTotal(n) {
	var s = "" + Math.round(n * 100) / 100;
	var i = s.indexOf('.');
	if (i < 0) return s + ".00";
	var t = s.substring(0, i + 1) + s.substring(i + 1, i + 3);
	if (i + 2 == s.length) t += "0";
	return t;
}

function swapOnOff( strBaseHref, strProductID ) {
    var objOnOffButton = document.getElementById("onoffbutton");

    if ( bOnOff == true ) {
        swapProduct( strBaseHref + "media/product-images/large/" + strProductID + "_0.jpg", 440, 440 )
        objOnOffButton.src = strBaseHref + "media/images/button-off.gif";
        bOnOff = false;
    } else {
        swapProduct( strBaseHref + "media/product-images/large/" + strProductID + "_10.jpg", 440, 440 )
        objOnOffButton.src = strBaseHref + "media/images/button-on.gif";
        bOnOff = true;
    }
}

function swapProduct( strFilename, strHeight, strWidth ) {
    var objProductLarge = document.getElementById("large");
    
    objProductLarge.src = strFilename;
}

function openTerms() {

    //  Open new window.
    var objWindow = window.open( "terms.php", "name", "scrollbars=1, height=600, width=940" );
    objWindow.focus();
}


// William's browse by size code

//setup functions and supporting libs
function querySt(ji) {
hu = window.location.search.substring(1);
gy = hu.split("&");
for (i=0;i<gy.length;i++) {
ft = gy[i].split("=");
if (ft[0] == ji) {
return ft[1];
}
}
return '';
}



function filterbysize(){

	//define
	var strSize;
	
	//get size
	strSize = querySt("size");
	
	//any size specified?
	if (strSize.length > 0){
		
		//get collection of all tr's
		coll = document.getElementsByTagName('tr');

		//loop through collection
		for (i=0;i < coll.length; i++){
			//does this tr have a classname that includes 'sizefilter'?
			if (coll[i].className.indexOf('sizefilter') > -1){
				//match, this tr has size filtering data, check if it should be hidden
				if (coll[i].className.indexOf('sizefilter_' + strSize) == -1){
					//this isn't the size we care about, so hide it..
					coll[i].style.display = 'none';
				}else{
					//this is the size we care about, leave it visible
				}
				
			}else{
				//no match, this tr doesn't contain filtering specifications
			}
		}
		
	}else{
		//nothing to do, success
		return true;
	}
}




//setup onload function
if(typeof window.addEventListener != 'undefined')
{
	//.. gecko, safari, konqueror and standard
	window.addEventListener('load', filterbysize, false);
}
else if(typeof document.addEventListener != 'undefined')
{
	//.. opera 7
	document.addEventListener('load', filterbysize, false);
}
else if(typeof window.attachEvent != 'undefined')
{
	//.. win/ie
	window.attachEvent('onload', filterbysize);
}

//** remove this condition to degrade older browsers
else
{
	//.. mac/ie5 and anything else that gets this far
	
	//if there's an existing onload function
	if(typeof window.onload == 'function')
	{
		//store it
		var existing = onload;
		
		//add new onload handler
		window.onload = function()
		{
			//call existing onload function
			existing();
			
			//call filterbysize onload function
			filterbysize();
		};
	}
	else
	{
		//setup onload function
		window.onload = filterbysize;
	}
}