// Function to add 1 into the basket
function addline(obj){
	var x = document.getElementById(obj);
	if (x.value==0){
		x.value = "1";
	}
}

// Function ensure all occurances of the product on the list have the same quantity
function updateQty(pVal,pID) { 
	for (i=0; i<document.productlist.elements.length; i++)
	{
		if ((document.productlist.elements[i].id == pID)){
			document.productlist.elements[i].value = pVal;
		}
	}
} 

// Function to remove '0' from the quantity box
function clearQty(p){
if (p.value == "0")
	p.value = "";
}

// Clear the default search phrase
function clearSearch(p){
if (p.value == p.defaultValue)
	{p.value = "";}
}

// Remove this line from the order
function removeline(obj){
	var x = document.getElementById(obj);
	x.value = "0";
}

// Reload Capatcha image
function reloadvalidation() {
	document.getElementById('validationimg').src='images/img_validation.jpg?'+Date();
}

// Open a new window
function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

// Preload Images
function mm_preloadimages() { //v3.0
var d=document; if(d.images){ if(!d.mm_p) d.mm_p=new Array();
var i,j=d.mm_p.length,a=mm_preloadimages.arguments; for(i=0; i<a.length; i++)
if (a[i].indexOf("#")!=0){ d.mm_p[j]=new Image; d.mm_p[j++].src=a[i];}}
} 

function MM_findObj(n, d) { //v3.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function roundNumber(num, dec) {
		var snum=num.toString()+"000000000000000001";
		var sep=snum.indexOf(".");
		var beg=snum.substring(0,snum.indexOf("."));
		snum=snum.substring(eval(snum.indexOf(".")+1),snum.length);
		var dig=snum.substring(0,eval(dec-1));
		snum=snum.substring(eval(dec-1),dec);
		snum=parseInt(snum);
		gohigher=false;
		if (snum>4) {gohigher=true;}
		if (gohigher) {snum=parseInt(snum);snum++;}
		snum=snum.toString();
		alert(beg+"."+dig+""+snum);
		num=beg+"."+dig+""+snum;
		return num;
}

function Left(str, n)
{
   if (n <= 0)
         return "";
   else if (n > String(str).length)
         return str;
   else
         return String(str).substring(0,n);
}

function updateProductPrice() {// custom function to update product price based on specified options
	// get data from the form
	var newPrice = document.getElementById('base_price').value;
	var oldPrice = document.getElementById('base_price').value;

	for (i=0; i<document.productlist.elements.length; i++)
	{
		if (Left(document.productlist.elements[i].name,4) == "opt_"){
			var thisName = document.getElementById(document.productlist.elements[i].name);
			var elementValue = document.productlist.elements[i].name+':'+thisName.value;
			var getValue = document.getElementById(elementValue);
			var newPrice = newPrice * 1 + getValue.value * 1;
			var oldPrice = oldPrice * 1
		}
	}
	
	var sNewPrice = newPrice
	var sOldPrice = oldPrice
	
	var nf = new NumberFormat(newPrice);
	nf.setPlaces(2);
	nf.setSeparators(false);
	var newPrice = nf.toFormatted();
	
	var nf = new NumberFormat(oldPrice);
	nf.setPlaces(2);
	nf.setSeparators(false);
	var oldPrice = nf.toFormatted();
	
	document.getElementById('pprice').innerHTML = '&pound;'+oldPrice;
	document.getElementById('total_price').innerHTML = '&pound;'+newPrice;
}