function presentValue(value) {
	if(value<=0.9999) {
		newPounds='0';
	} else {
		newPounds=parseInt(value);
	}
	dec='1';
	for (var i=1; i<=2;i++) {
		dec=dec+'0';
	}
	if (value>0) {
		newPence=Math.round((value+.000008 - newPounds)*(eval(dec)));
	} else {
		newPence=0;
	}
	compstring='9';
	for (var i=1; i <=2-1;i++) {
		if (eval(newPence) <= eval(compstring)) newPence='0'+newPence;
		compstring=compstring+'9';
	}
	if (2>0) {
		newString='' + newPounds + '.' + newPence + '';
	} else {
		newString='' + newPounds + '';
	}
	return (newString);
}

function getAdvanced(adv,curr,qty) {
     newAdv='';advlist=0;applicable=true;advnewprice=0;
    for (var j=0;j<=adv.length;j++) {
            if (adv.substring(j,j+1) == '{') {
            thisadv=1;
            advstart=j+1;
        } else if (adv.substring(j,j+1) == '}') {
            advend=j;
            advPrice2=adv.substring(advstart,advend);
            advlist++; applicable=true;
            if (applicable==true) {
             if (eval(advFrom) > 0 && eval(advFrom) > eval(qty)) applicable=false;
             if (eval(advTo) > 0 && eval(advTo) < eval(qty)) applicable=false;
             if (curr==1 && applicable==true) advnewprice=advPrice;
             if (curr==2 && applicable==true) advnewprice=advPrice2;
            }
        } else if (adv.substring(j,j+1)=='~') {
            if (thisadv== 1) advFrom=adv.substring(advstart,j);
            if (thisadv== 2) advTo=adv.substring(advstart,j);
            if (thisadv== 3) advPrice=adv.substring(advstart,j);
            if (thisadv== 4) advPrice2=adv.substring(advstart,j);
            thisadv++;advstart=j+1;
        }
    }
     return advnewprice;
}

function goBuy() {
	ilist='';
	index=document.cookie.indexOf('TheBasket');
	countbegin=(document.cookie.indexOf('=', index)+1);
	countend=document.cookie.indexOf(';',index);
	if(countend==-1) { countend=document.cookie.length; }
	fulllist=document.cookie.substring(countbegin,countend);
	var prodcount=0;
	for(var i=0; i<= fulllist.length;i++) {
		if(fulllist.substring(i,i+1)=='[') {
			itemstart=i;
		} else if (fulllist.substring(i,i+1)==']') {
			itemend=i+1;
			prodcount++;
			templist = changeSpaces(fulllist.substring(itemstart,itemend),prodcount);
			ilist=ilist+templist;
		}
	}
	document.basketform.secuitems.value = ilist;
	return true;
}

function changeSpaces(tstring,counter) {
	nstring='';
	for (var i=0; i <= tstring.length; i++) {
		if (tstring.charAt(i)==' ') { nstring=nstring+' ';
		} else { nstring=nstring+tstring.charAt(i); }
	
	}

	// assign quantity to variable
	var stopat = nstring.lastIndexOf(']');
	var startat = nstring.lastIndexOf('|');
	var itemqty = nstring.substring(startat+1,stopat);

	// strip redundant parts of string
	stopat=startat;
	nstring = nstring.substring(1,stopat-9);

	// split out amount and title
	var splitat = nstring.lastIndexOf('|');
	var itemtitle = nstring.substring(0,splitat);
	var itemprice = nstring.substring(splitat+1,nstring.length);
	itemprice = eval(presentValue(itemprice * itemqty));
	
	var itemtotal = eval(itemqty * itemprice);
	nstring = '[||'+itemtitle+'|'+presentValue(itemprice)+'|'+itemqty+'|'+presentValue(itemprice)+']';

	return nstring;
}

 taxfield='United Kingdom';

 // assign shipping costs if applicable
 function shipCalc(incQnty){

	if (incQnty < 1){ var shipTemp = '0.00'; }
	else if (incQnty > 1){ var shipTemp = '0.00'; }

	//else { var shipTemp = '2.00'; }
	else { var shipTemp = '0.00'; }

	shipTemp = parseFloat(shipTemp);
	return (shipTemp);
}

function showBasket() {
	 currency=1;
	 index=document.cookie.indexOf('TheBasket');
	 countbegin=(document.cookie.indexOf('=',index)+1);
	 countend=document.cookie.indexOf(';',index);
	 if (countend==-1) { countend=document.cookie.length; }
	 fulllist=document.cookie.substring(countbegin,countend);
	 totprice=0;totprice2=0;shippingtotal=0;shippingtotal2=0;taxtotalone=0;taxtotalone2=0;taxtotaltwo=0;taxtotaltwo2=0;ordertotal=0;ordertotal2=0;
	 taxablestandard=0;taxablesecond=0;taxable=0;
	 document.writeln('<table width="100%" border="0" cellspacing="0" cellpadding="3">');
	 document.writeln('<tr bgcolor="#663366">');
	 document.writeln('<td class="whitebody" width="45%">Product name</td>');
	 document.writeln('<td class="whitebody" width="15%" align="center">Price</td>');
	 document.writeln('<td class="whitebody" width="10%" align="center">Qnty</td>');
	 document.writeln('<td class="whitebody" width="15%" align="center">Total</td>');
	 document.writeln('<td class="whitebody" width="15%" align="center">Remove</td>');
	 document.writeln('</tr>');
	 itemlist=0; totalQnty = parseInt(0);
	 for (var i=0; i<=fulllist.length;i++) {
		 if (fulllist.substring(i,i+1)=='[') {
			 itemstart=i+1;
			 thisitem=1;
		 } else if (fulllist.substring(i,i+1)==']') {
			 itemend=i;
			 thequantity=fulllist.substring(itemstart,itemend);
			 itemtotal=0;itemtotal2=0;
			 if (theAP!='none') {
			 if (currency==1) { tempprice=getAdvanced(theAP,1,thequantity); if (tempprice>0) theprice=tempprice; }
			 if (currency==2) { tempprice=getAdvanced(theAP,2,thequantity); if (tempprice>0) theprice2=tempprice; }
		 }
		 itemtotal=(eval(theprice*thequantity));
		 totprice=totprice+itemtotal;
		 itemlist=itemlist+1;
		 document.writeln('<tr bgcolor="#F9F0F7"><td class="bodytext">'+theitem+'</td>');
		 document.writeln('<td class="bodytext" align="center">£&nbsp;'+presentValue(eval(theprice))+'</td>');
		 document.writeln('<td bgcolor="#F9F0F7" align="center" class="bodytext"><input type="text" size="3" class="normal" name="quant'+itemlist+'" value="'+thequantity+'" onBlur="updateBasket()"></td>');
		 var totalQnty = parseInt(totalQnty) + parseInt(thequantity);
		 document.writeln('<td class="bodytext" align="right">£&nbsp;'+presentValue(itemtotal)+'</td>');
		 document.writeln('<td align="center"><input type="button" name="remove'+itemlist+'" value="Remove" class="normal" onClick="removeItem('+itemlist+')"></td></tr>');
		 } else if (fulllist.substring(i,i+1)=='|') {
				 if (thisitem==1) theitem=fulllist.substring(itemstart,i);
				 if (thisitem== 2) theprice=fulllist.substring(itemstart,i);
				 if (thisitem== 3) thetaxable=fulllist.substring(itemstart,i);
				 if (thisitem== 4) theweight=fulllist.substring(itemstart,i);
				 if (thisitem== 5) theAP=fulllist.substring(itemstart,i);
			   thisitem++; itemstart=i+1;
		 }
	 }
	 var shipTot = shipCalc(parseInt(totalQnty));
	 document.writeln('<tr><td colspan="3" bgcolor="#C9ABBA" class="whitebody" align="right"><b>Total Goods</b></td>');
	 document.writeln('<td bgcolor="#C9ABBA" class="whitebody" align="right"><b>£&nbsp;'+presentValue(totprice)+'</b></td><td bgcolor="#C9ABBA">&nbsp;</td></tr>');
	 document.writeln('<input type="hidden" name="subtotal" value='+presentValue(totprice)+'>');
	 document.writeln('<tr><td colspan="3" bgcolor="#C9ABBA" class="whitebody" align="right"><b>Shipping</b></td>');
	 document.writeln('<input type="hidden" name="shippingcharge" value='+presentValue(shipTot)+'>');
	 document.writeln('<td bgcolor="#C9ABBA" class="whitebody" align="right"><b>£&nbsp;'+presentValue(shipTot)+'</b></td><td bgcolor="#C9ABBA">&nbsp;</td></tr>');
	 var grandtot = parseFloat(totprice) + parseFloat(shipTot);
	 document.writeln('<tr><td colspan="3" bgcolor="#663366" class="whitebody" align="right"><b>Grand Total</b></td>');
	 document.writeln('<td bgcolor="#663366" class="whitebody" align="right"><input type="hidden" name="transactionamount" value='+presentValue(grandtot)+'><b>£&nbsp;'+presentValue(grandtot)+'</b></td>');
	 document.writeln('<td bgcolor="#663366">&nbsp;</td></tr>');
	 document.writeln('</table>');
	 document.writeln('<table width="100%" border="0" cellspacing="0" cellpadding="0">');
	 document.writeln('<tr><td width="27"><img src="../images/left2.gif" width="57" height="18"></td>');
	 document.writeln('<td background="../images/checkback2.gif" width="100%"></td><td align="right" width="307"><img src="../images/right2.gif" width="307" height="18"></td></tr>');
	 document.writeln('</table>');
//	 document.writeln('<br><div align="center"><input type="button" name="clear" class="normal" value="Empty Basket" onClick="clearBasket();">');
	 document.writeln('<input type="button" name="update" class="normal" value="Update Basket" onClick="updateBasket();"> <input type="submit" name="submit" class="normal" value="Go to Secure Checkout""></div>');
}

function removeItem(itemno) {
	newItemList=null;
	itemlist=0;
	for(var i=0;i<=fulllist.length;i++) {
		if (fulllist.substring(i,i+1)=='[') {
			itemstart=i+1;
		} else if (fulllist.substring(i,i+1)==']') {
			itemend=i;
			theitem = fulllist.substring(itemstart,itemend);
			itemlist=itemlist+1;
			if(itemlist != itemno) {
				newItemList = newItemList+'['+fulllist.substring(itemstart,itemend)+']';
			}
		}
	}
	index = document.cookie.indexOf('TheBasket');
	document.cookie='TheBasket='+newItemList+'; expires=Friday, 31-Dec-2010 08:00:00 GMT';
	self.location = 'shopping_cart.htm';
}

function clearBasket() {
	if (confirm('Are you sure you wish to clear the basket?')) {
		index=document.cookie.indexOf('TheBasket');
		document.cookie='TheBasket=.';
		self.location='shopping_cart.htm';
	}
}

function changeCurrency(num) {
	 index=document.cookie.indexOf('TheCurrency');
	 document.cookie='TheCurrency='+num+'; expires=Friday, 31-Dec-2010 08:00:00 GMT';
	 self.location='shopping_cart.htm';
}

function updateBasket() {
	newItemList='';
	itemlist=0;
	for(var i=0;i<=fulllist.length; i++) {
		if(fulllist.substring(i,i+1)=='[') {
			thisitem=1;
			itemstart=i+1;
			fullstart=i+1;
		} else if(fulllist.substring(i,i+1)==']') {
			itemend=i;itemlist++;
			thiselement=document.basketform.elements['quant'+itemlist].value;
			newItemList=newItemList+'['+partList+'|'+thiselement+']'
		} else if(fulllist.substring(i,i+1)=='|') {
			partList=fulllist.substring(itemstart,i);
		}
	}
	index=document.cookie.indexOf('TheBasket');
	document.cookie='TheBasket='+newItemList+'; expires=Friday, 31-Dec-2010 08:00:00 GMT';
	self.location='shopping_cart.htm';
}

function MM_goToURL() { //v3.0
  var i, args=MM_goToURL.arguments; document.MM_returnValue = false;
  for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
}
