/* Called when the size on the product page is changed */
function CngSz(s)
{
	var sel = document.getElementById("productsizeid" + s);
	var newprice = document.getElementById("price" + sel.value);
	var newprevprice = document.getElementById("prevprice" + sel.value);
	var preorder = document.getElementById("preorder" + sel.value);
	var stock = document.getElementById("stock" + sel.value);
	
	document.getElementById("prodPrice" + s).innerHTML = '&pound;' + newprice.value;
	
	if(stock.value == 'N')
	{
		document.getElementById("AddToBasket" + s).innerHTML = '<img src="/images/outofstock.gif" width="21" height="20" alt="Out of stock" style="float:left;" align="left" /> <b>This size is currently out of stock</b>';
	}
	else
	{
		if(preorder)
			document.getElementById("AddToBasket" + s).innerHTML = '<input type="image" src="/images/btn_preorder.png" alt="Pre-order" style="width: 118px; height: 30px" />';
		else
			document.getElementById("AddToBasket" + s).innerHTML = '<input type="image" src="/images/btn_addtobasket.png" alt="Add To Basket" style="width: 118px; height: 30px" />';
	}
	
	if(newprevprice)
	{
		if(newprevprice.value == "")
		{
			document.getElementById("prodPrevPrice" + s).innerHTML = '';
		}
		else
		{
			document.getElementById("prodPrevPrice" + s).innerHTML = '(Was &pound;' + newprevprice.value + ')';
		}
	}
	else
	{
		document.getElementById("prodPrevPrice" + s).innerHTML = '';
	}
}

function DeliveryChanged(subTotal)
{
	var id;
	var radioGrp = document.delivery.deliveryoptionid;
	for (var i = 0; i< radioGrp.length; i++) {
		if (radioGrp[i].checked) {
			id = radioGrp[i].value;
		}
	}
	var cost = document.getElementById("deliveryoptioncost" + id).value;
	document.getElementById("deliveryPrice").innerHTML = "<b>DELIVERY CHARGE: &pound;" + (eval(cost)).toFixed(2) + "</b>";
	document.getElementById("totalPrice").innerHTML = "TOTAL: &pound;" + (eval(subTotal) + eval(cost)).toFixed(2);
}

function showPopup(url, width, height, left, top)
{
	newWindow = window.open(url, 'name', 'height=' + height + ', width=' + width + ', left=' + left + ' , top=' + top);
	if (window.focus)
	{
		newWindow.focus()
	}
	return false;
}

function checkQuantitySelected(f)
{
	var selects = f.getElementsByTagName('select');
	var i;
	
	for(i = 0; i < selects.length; i++)
	{
		if(selects[i].value > 0)
			return true;
	}
	
	alert('Please select the quantity you require.');
	return false;
}

var timeout = 0;

function startScroll(maxLeft, dir)
{
	if(timeout == 0)
		scrollPane(maxLeft, dir);
}

function scrollPane(maxLeft, dir)
{
	var multiplier = parseInt(dir);
	var step = 5;
	
	// get the left position of the scrolling div
	var elm = document.getElementById('scrollPane');
	var left = parseInt(elm.style.left);
	
	var newLeft = left + (multiplier * step);
	if(newLeft > 0)
		newLeft = 0;
	else if(newLeft < (-1 * maxLeft))
		newLeft = (-1 * maxLeft);
	
	elm.style.left = newLeft + 'px';
	
	timeout = setTimeout('scrollPane('+maxLeft+', '+dir+')', 30);
}

function stopScroll()
{
	clearTimeout(timeout);
	timeout = 0;
}

function updateTimeLeft(){
	foo = new Date;
	unixtime = parseInt(foo.getTime() / 1000);
	timeLeft = endTime - (unixtime + clientDiff);
	minsLeft = Math.floor(timeLeft / 60);
	secondsLeft = timeLeft - (minsLeft * 60);
	if(timeLeft >= 0)
		setTimeout('updateTimeLeft()', 100);
	else
	{
		minsLeft = 0;
		secondsLeft = 0;
	}
	
	document.getElementById('clearanceTimeLeftTime').innerHTML =  minsLeft + ' mins ' + secondsLeft;
}

function updateClearanceTimeLeft(){
	foo = new Date;
	unixtime = parseInt(foo.getTime() / 1000);
	timeLeft = endTime - (unixtime + clientDiff);
	minsLeft = Math.floor(timeLeft / 60);
	secondsLeft = timeLeft - (minsLeft * 60);
	if(timeLeft >= 0)
		setTimeout('updateClearanceTimeLeft()', 100);
	else
	{
		minsLeft = 0;
		secondsLeft = 0;
	}
	
	document.getElementById('clearanceTimeLeftTime').innerHTML =  'Time remaining: ' + minsLeft + ' minutes and ' + secondsLeft + ' seconds';
}

function startMainScroll(itemWidth, maxLeft)
{
	var step = 5;
	
	// get the left position of the scrolling div
	var elm = document.getElementById('mainAdvertScrollPane');
	var left = parseInt(elm.style.left);
	
	// calculate the 'stop' position
	// this is where the next 2 adverts are now being displayed and need to pause for longer
	var stopPos = Math.ceil(Math.abs(left) / (2 * itemWidth)) * (-2 * itemWidth);
	
	// calculate the new left position
	var newLeft = left - step;
	
	if(newLeft < (-1 * maxLeft))
		newLeft = 0;
	else if(left > stopPos && newLeft <= stopPos)
		newLeft = stopPos;
	
	elm.style.left = newLeft + 'px';
		
	if(newLeft == 0 || newLeft == stopPos)
		setTimeout('startMainScroll('+itemWidth+', '+maxLeft+')', 5000);
	else
		setTimeout('startMainScroll('+itemWidth+', '+maxLeft+')', 10);
}