var catalogueLinkTimer = 0;

var isIE = document.all;
document.onmousemove = movePhotoPreview;


function updateQuantity(productID)
{
	var txt = document.getElementById('txtQty'+productID);
	
	var http = new XMLHttpRequest();
	http.open('POST', '/cart.php?cmd=updateQuantity&productID='+productID+'&quantity='+txt.value, true);
	http.onreadystatechange = function()
	{
		if(this.readyState == 4 && this.status == 200) 
		{
			var data = this.responseText.split('^');
			
			document.getElementById('divCartLinks').innerHTML = data[0];

			var price = document.getElementById('tdPrice'+productID);			
			var subtotal = document.getElementById('tdSubTotal'+productID);

			cartItemPrices = data[1].split('|');
			if (price)
				price.innerHTML = cartItemPrices[0];
			if (subtotal)
				subtotal.innerHTML = cartItemPrices[1];
				
			var total = document.getElementById('tdTotal');
			if (total)
				total.innerHTML = data[2];				

			var totalQty = document.getElementById('tdQuantity');
			if (totalQty) 
			{
				totalQty.innerHTML = data[3];
				
				var txt = document.getElementById('txtQty'+productID);
				var tr = document.getElementById('tr'+productID);
				
				if (parseInt(txt.value)==0 && tr)
					tr.parentElement.parentElement.deleteRow(tr.rowIndex);
				
				if (parseInt(data[3]) == 0)
					window.location = '/viewCart.php';
			}
		}
	}
	
	var tr = document.getElementById('tr'+productID);
	var subtotal = document.getElementById('tdSubTotal'+productID);
	
	if (tr && !subtotal)
	{
		if (parseInt(txt.value) > 0 && tr.className.indexOf('Added')==-1)
			tr.className = tr.className + 'Added';
		
		if (parseInt(txt.value) == 0 && tr.className.indexOf('Added') > 0)
			tr.className = tr.className.substring(0, tr.className.length-5);
	}
	
	
	http.send('');
}

function selectAll(e)
{
	try
	{
		e.select();
	}
	catch (ex)
	{
	}
}

function validateNumber(e)
{
	try
	{
		if (isNaN(e.value)==false)
			e.value = parseInt(e.value);
		else
			e.value = 0;
	}
	catch (ex)
	{
		e.value = 0;
	}
}

function showCatalogueLinkItems()
{
	try
	{
		var e = document.getElementById('divCatalogueLinkItems');
				
		if (e)
		{
			if (e.offsetHeight == 32 && catalogueLinkTimer == 0)
			{
				catalogueLinkTimer = setInterval('showCatalogueLinkItemsAnim(100);', 20);
			}
			else
			{
				clearInterval(catalogueLinkTimer);
				catalogueLinkTimer = setInterval('showCatalogueLinkItemsAnim(-100);', 20);
			}
		}
	}
	catch (ex)
	{
	}
}

function showCatalogueLinkItemsAnim(amount)
{
	try
	{
		var e = document.getElementById('divCatalogueLinkItems');
			
		if (e)
		{
			if ((e.offsetHeight < e.scrollHeight && amount > 0) || (e.offsetHeight > 32 && amount < 0))
			{
				var newHeight = (e.offsetHeight + amount);
				if (newHeight > e.scrollHeight)
					newHeight = e.scrollHeight;
				if (newHeight < 27)
					newHeight = 27;
					
				e.style.height = newHeight + 'px';
			} 
			else
			{
				clearInterval(catalogueLinkTimer);
				catalogueLinkTimer = 0;
			}
		}
	}
	catch (ex)
	{
	}
}


function showPhoto(photoID, img, width, height, title, folder) 
{
	var pnl = document.getElementById('divViewPhoto');
	if (pnl.photoID != photoID || pnl.style.visible != 'visible') 
	{
		pnl.photoID = photoID
		pnl.innerHTML = '<div style="background-color:#999999;width:100%;border-bottom:solid 1px #000000;display:block;text-align:left">&nbsp;Photo: ' + title + '</div><img src="/images/' + folder + '/'+photoID+'.jpg" width="'+width+'" height="'+height+'" border="0" title="'+title+'"/>';
		pnl.style.visibility = 'visible';
	}
}

var datasheetPanel = null;
function viewDatasheets(productID) 
{
	if (datasheetPanel != null)
		datasheetPanel.style.visibility = 'hidden';
	
	datasheetPanel = document.getElementById('divDS'+productID);

	if (datasheetPanel) 
	{
		var td = datasheetPanel.parentElement;
		
		if (td == null)
			td = datasheetPanel.parentNode; // firefox
			
		if (datasheetPanel.style.left == '')
		{
			datasheetPanel.style.left = (td.offsetWidth + datasheetPanel.offsetLeft - 50) + 'px';
			datasheetPanel.style.top = (datasheetPanel.offsetTop + 8) + 'px';
		}
		
		if (datasheetPanel.style.visibility == 'hidden' || datasheetPanel.style.visibility == '')
		{
			datasheetPanel.style.visibility = 'visible';
			
			if (datasheetPanel.innerHTML == '')
			{
				datasheetPanel.innerHTML = '<i>Loading...<i>';
				
				var http = new XMLHttpRequest();
				http.open('POST', '/cart.php?cmd=getDatasheetsHTML&productID='+productID, true);
				http.onreadystatechange = function()
				{
					if(this.readyState == 4 && this.status == 200) 
					{
						datasheetPanel.innerHTML = this.responseText;
					}
				}
				
				http.send();
			}
		}
		else
		{
			datasheetPanel.style.visibility = 'hidden';
		}
	}
}

function movePhotoPreview(e) 
{
	var pnl = document.getElementById('divViewPhoto');
	if (!e) e = window.event;
	if (e && pnl) 
	{
		var mouseX = isIE ? (e.clientX + getBodyElement().scrollLeft) : e.pageX;
		var mouseY = isIE ? (e.clientY + getBodyElement().scrollTop) : e.pageY;

		var clientWidth=document.all? getBodyElement().clientWidth : window.innerWidth-15
		var clientHeight=document.all? getBodyElement().clientHeight : window.innerHeight

		var scrollLeft=document.all? getBodyElement().scrollLeft : pageXOffset
		var scrollTop=document.all? getBodyElement().scrollTop : pageYOffset

		var left = mouseX + 15;
		var top = mouseY + 15;

		if (left + pnl.offsetWidth > clientWidth - 5)
			left = mouseX - pnl.offsetWidth - 15;

		if ((top - scrollTop) + pnl.offsetHeight > clientHeight - 5)
			top = (clientHeight + scrollTop) - pnl.offsetHeight - 5;
			
		pnl.style.left = left + 'px';
		pnl.style.top = top + 'px';
	}
}

function getBodyElement() 
{
	return (!window.opera && document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

function hidePhoto() 
{
	var pnl = document.getElementById('divViewPhoto');
	pnl.style.visibility = 'hidden';
}


var indexFeatureTimer = 0;
var indexFeatureIndex = 0;

function moveIndexFeaturePanelLeft()
{
	if (indexFeatureTimer!=0)
	{
		clearInterval(indexFeatureTimer);
		indexFeatureTimer = 0;
	}
	
	if (indexFeatureIndex > 0)
	{
		indexFeatureIndex--;
		indexFeatureTimer = setInterval('moveIndexFeaturePanelAnim(-100);', 20);
	}
}

function moveIndexFeaturePanelRight()
{
	if (indexFeatureTimer!=0)
	{
		clearInterval(indexFeatureTimer);
		indexFeatureTimer = 0;
	}
	
	if (indexFeatureIndex < 4)
	{
		indexFeatureIndex++;
		indexFeatureTimer = setInterval('moveIndexFeaturePanelAnim(100);', 20);
	}
}

function moveIndexFeaturePanelAnim(amount)
{
	var e = document.getElementById('divIndexFeaturePanel');
	
	if (e)
	{
		var destination = indexFeatureIndex * (299 * 3);
		
		if ((e.scrollLeft < destination && amount > 0) || (e.scrollLeft > destination && amount < 0))
		{
			var newscrollLeft = e.scrollLeft + amount;
			if (newscrollLeft > destination && amount > 0)
				newscrollLeft = destination;
			if (newscrollLeft < destination && amount < 0)
				newscrollLeft = destination;
			
			e.scrollLeft = newscrollLeft;
		}
		else
		{
			clearInterval(indexFeatureTimer);
			indexFeatureTimer = 0;
		}
	}
}


function doPredictiveSearch(event, txt, searchDivID)
{
	var e = document.getElementById(searchDivID);
	var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
	
	if (txt.value != '' && keyCode != 27)
	{
		if (e.style.visibility == '' || e.style.visibility == 'hidden')
		{		
			if (e.style.left == '' && searchDivID == 'divSearchPanel')
			{
				e.style.left = 954 - 282 + 'px'; // 954 is the width of the table...
				e.style.top = (txt.offsetTop + txt.offsetHeight + 2) + 'px';
			}
			
			e.style.display = 'inline';
			e.style.visibility = 'visible';
		}
		
		if (e.innerHTML == '')
			e.innerHTML = '<i>Searching...</li>';
	
		var http = new XMLHttpRequest();
		http.open('POST', '/cart.php?cmd=search&search='+txt.value, true);
		http.onreadystatechange = function()
		{
			if(this.readyState == 4 && this.status == 200) 
			{
				document.getElementById(searchDivID).innerHTML = this.responseText;
			}
		}
	
		http.send();
	}
	else
	{
		e.style.visibility = 'hidden';
	}
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}
