/* All Javascript Modules */
var visibleMenu = "";
var inty;
var ii;


function cookieCheck()
{
	var cookieEnabled=(navigator.cookieEnabled)? true : false;
        if (typeof navigator.cookieEnabled=="undefined" && !cookieEnabled)
	{ 
		document.cookie="testcookie";
		cookieEnabled=(document.cookie.indexOf("testcookie")!=-1)? true : false;
	}

	if (cookieEnabled)
	{
		document.getElementById("usingcookies").value='1';
	}
	else
	{
		document.getElementById("usingcookies").value='0';
	}
}

function numberformat(num,dec)
{

	mul=Math.pow(10,dec);
	num=num*mul;
	num=Math.round(num);
	num = num/mul;
	var numstr=String(num);
	if(numstr.indexOf(".") == -1)
	{
		numstr = numstr + ".";
		for(nfi=0;nfi<dec;nfi++) numstr = numstr + "0";
	}
	decpl = numstr.length - numstr.indexOf(".");
	decpl = decpl - 1;
	if (decpl < dec)
	{
		for(nfi=decpl;nfi<dec;nfi++) numstr = numstr + "0";
	}
	return (numstr);

}

//shows/hides the object (pass in the id)
function toCurrency(val)
{
	return numberformat(val,2);
}

function showHide(divobj)
{
	if(document.getElementById(divobj).style.display=="")
	{
		document.getElementById(divobj).style.display="none";
	}
	else
	{
		document.getElementById(divobj).style.display="";
	}
}

function verifyNumeric(obj)
{
	var strValidChars = "0123456789";
	var strChar;
	var blnResult = true;

	val = obj;
	if (val.length == 0) return false;
	if((val.length==1)&&(val.charAt(0)=="0"))
	{
		return false;
	}

   //  test obj consists of valid characters listed above
	for (i = 0; i < val.length && blnResult == true; i++)
	{
		strChar = val.charAt(i);
		if (strValidChars.indexOf(strChar) == -1)
		{
			blnResult = false;
		}
	}
	return blnResult;	   
}


//updates the price of a product, given the options
function updatePrice()
{
	var x;

	var unitprice = document.getElementById("p_unitprice").value;
	var addvat = document.getElementById("p_addvat").value;
	var totalprice = 0;

	if(addvat>0)
	{
		totalprice = 1.15 * unitprice;
	}
	else
	{
		totalprice = unitprice;
	}
	

	
	for(x in arrP)
	{
		var y;
		var t = arrP[x];
		for(y in t)
		{
			if(document.getElementById("optionselection_"+x).value>"")
			{
				actualid=-1;
				actualidpos = document.getElementById("optionselection_"+x).value.lastIndexOf("_");
				if(actualidpos>-1)
				{
					actualid = document.getElementById("optionselection_"+x).value.substr(actualidpos+1);
				}
				
				if(y==actualid)
				{
					if(t[y][1]!=0)
					{
						if(t[y][0]!=0)
						{
							totalprice = 1.15*(parseFloat(t[y][1])+parseFloat(unitprice));
						}
						else
						{
							totalprice = parseFloat(totalprice) + parseFloat(t[y][1]);
						}
					}
					else
					{
						//some options don't
						//have a price
						//difference, AND they
						//don't add vat

						if(t[y][0]==0)
						{
							//this is the
							//case
							totalprice = parseFloat(unitprice);
						}
						else
						{
							totalprice = 1.15 * parseFloat(unitprice);
						}
					}
				}
			}
		}
	}
	var head1 = document.getElementById("pprice");
//	head1.firstChild.nodeValue = "£" + toCurrency(totalprice) + " each";
	head1.firstChild.nodeValue = toCurrency(totalprice);
	
	
}


function IsNumeric(sText)
{
	var ValidChars = "0123456789.";
	var IsNumber=true;
	var Char;

	for (i = 0; i < sText.length && IsNumber == true; i++) 
	{ 
		Char = sText.charAt(i); 
		if (ValidChars.indexOf(Char) == -1) 
		{
			IsNumber = false;
		}
	}
	return IsNumber;
}


function addToBasket(pid,path)
{
	err=0;

	//check that the quantity value submitted is numeric
	if(!IsNumeric(document.getElementById("pquantity").value))
	{
		err++;
	}

	//check that all options available have been selected
	for(x in arrP)
	{
		var y;
		var t = arrP[x];
		for(y in t)
		{
			if(document.getElementById("optionselection_"+x).value>"")
			{
				actualid=-1;
				actualidpos = document.getElementById("optionselection_"+x).value.lastIndexOf("_");
				if(actualidpos>-1)
				{
					actualid = document.getElementById("optionselection_"+x).value.substr(actualidpos+1);
				}
				else
				{
					err++;
				}
			}
		}
	}

	if(err>0)
	{
		alert("There was an error creating your order.\nPlease ensure that you have entered a valid quantity for the product and have selected all options for this product.");
	}
	else
	{
		document.getElementById("productform").action = path + "/addtobasket";
		document.getElementById("productform").submit();
	}
}

//removes an item from the user's basket, given the basket contents id
function removeBasketItem(bcid, path)
{
	if(confirm("Are you sure that you want to remove this item from your shopping basket?"))
	{
		document.getElementById("vvar").value=bcid;
		document.getElementById("task").value="deleteitem";
		document.getElementById("userbasket").action = path + "/viewbasket";
		document.getElementById("userbasket").submit();
	}
}

function updateBasketQuantity(bcid,path)
{
	p = document.getElementById("basketQuantity"+bcid).value;
	if(verifyNumeric(p))
	{
		document.getElementById("vvar").value="basketQuantity"+bcid;
		document.getElementById("task").value="updatequantity";
		document.getElementById("userbasket").action = path + "/viewbasket";
		document.getElementById("userbasket").submit();
	}
	else
	{
		alert("Please enter a positive integer for the quantity.");
	}
}


function getCurrentPageAddress()
{
	slashpos = document.location.href.lastIndexOf("/");
	phpos = document.location.href.indexOf("php",slashpos+1);
	pagestr = document.location.href.substr(slashpos+1, (phpos-slashpos)+2);
	return pagestr;
}

function viewBasket(path)
{
	//get current page address
	document.getElementById("productform").action = path + "/viewbasket";
	document.getElementById("productform").submit();
}


//initially this just pops up the menu, but eventually this will scale
//n shit
function HideMenu(divobj)
{
	document.getElementById("menu_"+divobj).style.display="none";
	restoreBG("menu_item"+divobj);
	clearInterval(inty);
	clearInterval(ii);
	if(visibleMenu	==divobj)
	{
		visibleMenu="";
	}
}

function hideImmediately(divobj)
{
	document.getElementById("menu_"+divobj).style.display="none";
	restoreBG("menu_item"+divobj);
	clearInterval(inty);
	clearInterval(ii);
}

function showHideMenu(divobj)
{
	//hide all open menus
	//show the selected menu
	if(visibleMenu!="")
	{
		hideImmediately(visibleMenu);
	}
	document.getElementById("menu_"+divobj).style.display="";
//	doWidthChangeMem(document.getElementById(divobj),0,480,15,15,10);
	setOpacity(document.getElementById("menu_"+divobj),0);
	fadeIn("menu_"+divobj,0);
	visibleMenu = divobj;
}

function startCloseTimer(obj)
{
	inty = setInterval('HideMenu("'+obj+'")',500);
	ii = setInterval('restoreBG("menu_item'+obj+'")',500);
}

function resetCloseTimer(obj)
{
	clearInterval(inty);
	clearInterval(ii);
}

function doWidthChangeMem(elem,startWidth,endWidth,steps,intervals,powr) { 
//Width changer with Memory by www.hesido.com
	if (elem.widthChangeMemInt)
		window.clearInterval(elem.widthChangeMemInt);
	var actStep = 0;
	elem.widthChangeMemInt = window.setInterval(
		function() { 
		elem.currentWidth = easeInOut(startWidth,endWidth,steps,actStep,powr);
		elem.style.width = elem.currentWidth + "px"; 
		actStep++;
		if (actStep > steps) window.clearInterval(elem.widthChangeMemInt);
	} 
	,intervals)
}

function easeInOut(minValue,maxValue,totalSteps,actualStep,powr) { 
//Generic Animation Step Value Generator By www.hesido.com 
	var delta = maxValue - minValue; 
	var stepp = minValue+(Math.pow(((1 / totalSteps) * actualStep), powr) * delta); 
	return Math.ceil(stepp) 
}

function setOpacity(obj, opacity) {
	opacity = (opacity == 100)?99.999:opacity;

  // IE/Win
	obj.style.filter = "alpha(opacity:"+opacity+")";

  // Safari<1.2, Konqueror
	obj.style.KHTMLOpacity = opacity/100;

  // Older Mozilla and Firefox
	obj.style.MozOpacity = opacity/100;

  // Safari 1.2, newer Firefox and Mozilla, CSS3
	obj.style.opacity = opacity/100;
}

function fadeIn(objId,opacity) {
	if (document.getElementById) {
		obj = document.getElementById(objId);
		if (opacity <= 100) {
			setOpacity(obj, opacity);
			opacity += 10;
			window.setTimeout("fadeIn('"+objId+"',"+opacity+")", 30);
		}
	}
}

function changeBG(obj)
{
	document.getElementById(obj).style.backgroundColor='#D1CDB7';

}

function restoreBG(obj)
{
	document.getElementById(obj).style.backgroundColor='';
	clearInterval(ii);

}

function resizeInfoDiv()
{
	h = document.getElementById("ing").height;
	document.getElementById("inf").style.height = h + 60;
}

function doCheckout(path)
{
	document.getElementById("userbasket").action = path + "/checkout";
	document.getElementById("userbasket").submit();
}

function pointerMouse()
{
	document.cursor = 'pointer';
}

function arrowMouse()
{
	document.body.style.cursor = 'default';
}

function shoplinkOver(obj)
{
	obj.style.cursor = "pointer";
	obj.style.background = "#dddddd";
	obj.style.border = "solid 1px #aaaaaa";	
}

function shoplinkOut(obj)
{
	obj.style.cursor = "default";
	obj.style.background = "#eeeeee";
	obj.style.border = "dashed 1px #cccccc";	
}

function getProduct(pstrurl)
{
	document.getElementById("productform").action = pstrurl;
	document.getElementById("productform").submit();
}

function checkForm()
{
	err=0;
	errStr = "Please correct the following errors:\n";

	if(document.getElementById("personname").value=="")
	{
		err++;
		errStr += "Please enter your name\n";
	}
	
	if((document.getElementById("add1").value=="")&&(document.getElementById("add1").value=="")&&(document.getElementById("add1").value==""))
	{
		err++;
		errStr += "Please enter your address\n";
	}
	
	
	if(document.getElementById("town").value=="")
	{
		err++;
		errStr += "Please enter your town\n";
	}
	
	if(document.getElementById("county").value=="")
	{
		err++;
		errStr += "Please enter your county\n";
	}
	
	if(document.getElementById("postcode").value=="")
	{
		err++;
		errStr += "Please enter your postcode\n";
	}
	
	if(document.getElementById("tel").value=="")
	{
		err++;
		errStr += "Please enter your telephone number\n";
	}
	
	if(document.getElementById("email").value=="")
	{
		err++;
		errStr += "Please enter your email address\n";
	}
	
	if(err>0)
	{
		alert(errStr);
		return false;
	}
	else
	{
		return true;
	}
   
	
}