// SHOPPING CART JAVASCRIPT //



function isNum (str)
{
	if (str != null) {
	    for (var i = 0; i < str.length; i++)
	    {
	     var ch = str.substring(i, i + 1);
	     if (ch < "0" || "9" < ch)
	        return false;
	    }
	    return true;
	}else{
		return false;
	}
}


// Function to save form element's value.
function setValue(element) {
	setCookie(element.name, element.value, exp);
}


function completeFields() {
	var value = getCookie('surname');
	if (value != null) {
		doit=confirm("You have completed this form once before. Would you like to retrieve the name and address information that you used last time?");
		if (doit == 1) {
			if (value != null) d.f.surname.value = value;
			var value = getCookie('firstname');
			if (value != null) d.f.firstname.value = value;
			var value = getCookie('email');
			if (value != null) d.f.email.value = value;
			var value = getCookie('address1');
			if (value != null) d.f.address1.value = value;
			var value = getCookie('address2');
			if (value != null) d.f.address2.value = value;
			var value = getCookie('towncity');
			if (value != null) d.f.towncity.value = value;
			var value = getCookie('county');
			if (value != null) d.f.county.value = value;
			var value = getCookie('postcode');
			if (value != null) d.f.postcode.value = value;
			var value = getCookie('telephone');
			if (value != null) d.f.telephone.value = value;
			var value = getCookie('cardname');
		}	
	}
}


    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);
        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;
                ilist=ilist+fulllist.substring(itemstart,itemend);
            }
        }
        ilist=changeSpaces(ilist);
//        top.location='order_form.shtml?items='+ilist;
		var	 loginName=getCookie('loginname');
		if (loginName != "" && loginName != null && loginName != "null" && loginName != "undefined"){
			loginName=str_to_num(loginName);
			var loginId=getCookie('id');
			loginId=str_to_num(loginId);
			var loginEmail=getCookie('email');
			loginEmail=str_to_num(loginEmail);
			var loginPassword=getCookie('password');
			loginPassword=str_to_num(loginPassword);
			var loginType=getCookie('logintype');
			loginType=str_to_num(loginType);
			var loginLevel=getCookie('loginlevel');
			loginLevel=str_to_num(loginLevel);
		}else{
			var loginId="";
			var loginEmail="";
			var loginPassword="";
			var loginType=""
			var loginLevel="";
		}

		var delivery=getCookie('delivery');
		var delivery_cost=getCookie('delivery_cost');

        top.location='https://securemanager.co.uk/woundsuk/cgi-bin/payments_assemble_purchases.cgi?items='+ilist+'&params=|'+loginId+'|'+loginEmail+'|'+loginPassword+'|'+loginName+'|'+loginType+'|'+loginLevel+'|'+delivery+'|'+delivery_cost+'|';
    }

    function replaceSpaces(tstring) {
        nstring='';
        for(var i=0;i<=tstring.length;i++) { //>
            if (tstring.charAt(i)=='^') { nstring=nstring+' ';
            } else { nstring=nstring+tstring.charAt(i); }
        }
        return nstring;
    }

    function changeSpaces(tstring) {
        nstring='';
        for (var i=0; i <= tstring.length; i++) { //>
            if (tstring.charAt(i)==' ') { nstring=nstring+'^';
            } else { nstring=nstring+tstring.charAt(i); }
        }
        return nstring;
    }

function checkAdvanced(adv) {
     newAdv='';advlist=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) {
                newAdv = newAdv+ '{'+advFrom+'~'+advTo+'~'+advPrice+'~'+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;
        }
    }
    if (newAdv=='') newAdv='none';
    return newAdv;
}


    function buyItem(newItem,newPrice,newTaxable,newWeight,newAP,newQuantity) {
        if(newQuantity<=0) { //>
            rc = alert('The quantity entered is incorrect');
        } else {
            if (confirm('Add '+newQuantity+' copy of `'+newItem+'` to your basket?\n\nYou can check the contents of your shopping basket any time by clicking on the link near the top right of each page, labelled `Basket`. ')) {
                 if (newAP!='none') {
                     newAP=checkAdvanced(newAP);
                 }
                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);
                 amended = false;
                 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;
                         thequantity=fulllist.substring(itemstart,itemend);
                         itemlist++;
                         if (theItem==newItem ) {
                             amended=true;
                             tempquantity=eval(thequantity)+eval(newQuantity);
                             newItemList=newItemList+'['+theItem+'|'+thePrice+'|'+theTaxable+'|'+theWeight+'|'+newAP+'|'+tempquantity+']';
                         } else {
                             newItemList=newItemList+'['+theItem+'|'+thePrice+'|'+theTaxable+'|'+theWeight+'|'+theAP+'|'+thequantity+']';
                         }
                     } 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;
                     }
                 }
                 if (amended==false) {
                     newItemList=newItemList+'['+newItem+'|'+newPrice+'|'+newTaxable+'|'+newWeight+'|'+newAP+'|'+newQuantity+']'; }
                 index = document.cookie.indexOf('TheBasket');
			 	 var now = new Date();
				 now.setTime(now.getTime() + 1000 * 60 * 60 * 1)
                 document.cookie='TheBasket='+newItemList+'; expires='+now;
				window.location.href="basket.shtml";
//				document.location.reload();
            }
        }
    }


    function buyItemSpecialOffer(newItem,newPrice,newTaxable,newWeight,newAP,newQuantity) {
        if(newQuantity<=0) { //>
            rc = alert('The quantity entered is incorrect');
        } else {
            if (confirm('Add '+newQuantity+' copy of `'+newItem+'` to your basket, plus `Honey:A Modern Wound Management Product` for the special price of £10?\n\nYou can check the contents of your shopping basket any time by clicking on the link near the top right of each page, labelled `Basket`. ')) {
                 if (newAP!='none') {
                     newAP=checkAdvanced(newAP);
                 }
                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);
                 amended = false;
                 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;
                         thequantity=fulllist.substring(itemstart,itemend);
                         itemlist++;
                         if (theItem==newItem ) {
                             amended=true;
                             tempquantity=eval(thequantity)+eval(newQuantity);
                             newItemList=newItemList+'['+theItem+'|'+thePrice+'|'+theTaxable+'|'+theWeight+'|'+newAP+'|'+tempquantity+']';
                         } else {
                             newItemList=newItemList+'['+theItem+'|'+thePrice+'|'+theTaxable+'|'+theWeight+'|'+theAP+'|'+thequantity+']';
                         }
                     } 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;
                     }
                 }
                 if (amended==false) {
                     newItemList=newItemList+'['+newItem+'|'+newPrice+'|'+newTaxable+'|'+newWeight+'|'+newAP+'|'+newQuantity+']';
//                     newItemList=newItemList+'[Honey:A Modern Wound Management Product|10.00|0|0|none|1]';
				 }
                 index = document.cookie.indexOf('TheBasket');
			 	 var now = new Date();
				 now.setTime(now.getTime() + 1000 * 60 * 60 * 1)
                 document.cookie='TheBasket='+newItemList+'; expires='+now;
//				window.location.href="basket.shtml";
//				document.location.reload();
				buyItemNoNotice('Honey:A Modern Wound Management Product', 10.00, 0, 0,'none',1);
            }
        }
    }


    function buyItemNoNotice(newItem,newPrice,newTaxable,newWeight,newAP,newQuantity) {
        if(newQuantity<=0) { //>
            rc = alert('The quantity entered is incorrect');
        } else {
//            if (confirm('!!!!Add '+newQuantity+' copy of `'+newItem+'` to your basket, plus `Honey:A Modern Wound Management Product` for the special price of £10?\n\nYou can check the contents of your shopping basket any time by clicking on the link near the top right of each page, labelled `Basket`. ')) {
                 if (newAP!='none') {
                     newAP=checkAdvanced(newAP);
                 }
                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);
                 amended = false;
                 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;
                         thequantity=fulllist.substring(itemstart,itemend);
                         itemlist++;
                         if (theItem==newItem ) {
                             amended=true;
                             tempquantity=eval(thequantity)+eval(newQuantity);
                             newItemList=newItemList+'['+theItem+'|'+thePrice+'|'+theTaxable+'|'+theWeight+'|'+newAP+'|'+tempquantity+']';
//		                       newItemList=newItemList+'[Honey:A Modern Wound Management Product|10.00|0|0|none|1]';
                         } else {
                             newItemList=newItemList+'['+theItem+'|'+thePrice+'|'+theTaxable+'|'+theWeight+'|'+theAP+'|'+thequantity+']';
//		                     newItemList=newItemList+'[Honey:A Modern Wound Management Product|10.00|0|0|none|1]';
                         }
                     } 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;
                     }
                 }
                 if (amended==false) {
                     newItemList=newItemList+'['+newItem+'|'+newPrice+'|'+newTaxable+'|'+newWeight+'|'+newAP+'|'+newQuantity+']';
//                     newItemList=newItemList+'[Honey:A Modern Wound Management Product|10.00|0|0|none|1]';
				 }
                 index = document.cookie.indexOf('TheBasket');
			 	 var now = new Date();
				 now.setTime(now.getTime() + 1000 * 60 * 60 * 1)
                 document.cookie='TheBasket='+newItemList+'; expires='+now;
				window.location.href="basket.shtml";
//				document.location.reload();
//            }
        }
    }






 taxfield='United Kingdom';
 function showBasket() {
     currency=1;
     index=document.cookie.indexOf('TheBasket');

	// CHECK TO SEE IF THE BASKET IS EMPTY //
	var basket=getCookie('TheBasket');
	if (basket == null) {
		document.writeln('<p class="center"><b>Your shopping basket is empty.</p>');
		var basket="";
	}else if (basket.length <= 1) {
		document.writeln('<p class="center"><b>Your shopping basket is empty.</p>');
	}else{
     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 border="2" cellpadding="3" cellspacing="1" bordercolor="#ffffff">');
     document.writeln('<tr>');
     document.writeln('<td><p><b>Item</p></td>');
     document.writeln('<td align=right><p><b>Price</p></td>');
     document.writeln('<td align=right><p><b>Quantity</p></td><td align=right><p><b>Total&nbsp;Cost</p></td><td align=center><p><b>Action</p></td>');
     document.writeln('</tr>');
     itemlist=0;
	 itemcount=0;
	 var foundHoney=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));
             temptotal=itemtotal*100;
             totprice=totprice+itemtotal;
             itemlist=itemlist+1;
			 itemcount=itemcount+eval(thequantity);
			if (theitem == "Honey:A Modern Wound Management Product") { foundHoney=foundHoney+10; }
         document.writeln('<tr><td><p>'+theitem+'</p></td>');
     document.writeln('<td align=right><p>'+presentValue(eval(theprice))+'</p></td>');
     document.writeln('<td align=right><p><input type=text name="quant'+itemlist+'" value='+thequantity+' size=3 onChange="updateBasket();"></p></td>');
     document.writeln('<td align=right><p>'+presentValue(itemtotal)+'</p></td>');
     document.writeln('<td align=right><p><input type=button name="remove'+itemlist+'" value="Remove" onClick="removeItem('+itemlist+');" class="normal;"></p></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;
             }
         }

	// TOTAL GOODS //
     document.writeln('<tr><td colspan=3><p><i>Total Goods</i></p></td>');
     document.writeln('<td align=right><p><i>'+presentValue(totprice)+'</i></p></td>');
     document.writeln('<td align=right><p><i>&nbsp;</i></p></td>');
     document.writeln('</tr>');


	// DISCOUNT //
	// SET DISCOUNT VALUE //
	var discount_rate=0.2;
	var discount_rate_title="20%";
	var discount=getCookie('loginname');
	var logintype=getCookie('logintype');
//	alert("Total Goods="+totprice+"; Honey value="+foundHoney);
	if (logintype != "Athens") {
 	 if (discount != "" && discount != null && discount != "null" && discount != "undefined") {
		var discount_value=totprice*discount_rate;
		var new_discount_value=Math.round(discount_value*Math.pow(10,2))/Math.pow(10,2);
		if (discount_value.toFixed) new_discount_value=discount_value.toFixed(2);
		totprice=totprice-new_discount_value;
		document.writeln('<tr><td colspan=2><p><i>Club Wounds UK Discount</i></p></td>');
		document.writeln('<td align=right><p><i>'+discount_rate_title+'</i></p></td>');
		document.writeln('<td align=right><p><i> -£'+new_discount_value+' </i></p></td>');
		document.writeln('<td align=center><p>&nbsp;</p></td>');
 	    document.writeln('</tr>');
	 }
	}



	// DELIVERY //
	delivery=getCookie('delivery');
	if (delivery == "Worldwide") {
		deliv_title="Worldwide Delivery";
		deliv_cost=9.99*itemcount;
		deliv_cost_value=deliv_cost;
		setCookie("delivery", "Worldwide", null);
		setCookie("delivery_cost", deliv_cost_value, null);
	}else if (delivery == "Europe") {
		deliv_title="Europe Delivery";
		deliv_cost=5.49*itemcount;
		deliv_cost_value=deliv_cost;
		setCookie("delivery", "Europe", null);
		setCookie("delivery_cost", deliv_cost_value, null);
	}else{
		delivery == "UK Delivery";
		deliv_title="UK Delivery";
		deliv_cost=3.99*itemcount;
		deliv_cost_value=deliv_cost;
		setCookie("delivery", "UK Delivery", null);
		setCookie("delivery_cost", deliv_cost_value, null);
	}

     document.writeln('<tr><td colspan=3><p><i>'+deliv_title+'</i></p></td>');
     document.writeln('<td align=right><p><i>£'+deliv_cost+'</i></p></td>');
     document.writeln('<td align=center><p><select name="changedelivery" onChange="changeDelivery(value)" style="font-family: Verdana; size: 2; font-weight:normal;"><option value="">Change...<option value="">-----------<option value="UK Delivery">UK Delivery<option value="Europe">Europe<option value="Worldwide">Worldwide</select></p></td>');
     document.writeln('</tr>');




	// GRAND TOTAL //
	price_total=presentValue(totprice).substring(1,10);
	price_total=parseFloat(price_total);
	new_price=price_total+deliv_cost_value;
	new_price=new_price+".0";
	parts=new_price.split('.');
	pounds=parts[0];
	pence=parts[1];
	pence=pence+"00";
	remainder=pence.substring(2,3);
	pence=pence.substring(0,2);
	if (remainder > 5) {
		pence++;
	}
	price_total=pounds+"."+pence;


	// PRINT TOTAL //
     document.writeln('<tr><td colspan=3><p><b>Total Goods</p></td>');
     document.writeln('<td align=right><p><b>£'+price_total+'</p></td>');
     document.writeln('<td align=right><p><b>&nbsp;</p></td>');
     document.writeln('</tr>');



     document.writeln('</table><br>');
     document.writeln('<center><input type=button name=clear value="Empty Basket" onClick="clearBasket();" class="normal">');
     document.writeln('<input type=button name=update value="Update Basket" onClick="updateBasket();" class="normal"> <input type=button name=orderfordm value="Secure Checkout >>" onClick="goBuy();" class="normal"></center>');

	} // THIS IS THE END OF CHECKING THE BASKET IS EMPTY BRACKET//
}



 taxfield='United Kingdom';
 function showOrderFormSummary(delivery,deliv_cost) {
     currency=1;
     index=self.location.href.indexOf('items');
     countbegin=(self.location.href.indexOf('=',index)+1);
     countend=self.location.href.length;
     fulllist=self.location.href.substring(countbegin,countend);
     fulllist=replaceSpaces(fulllist);
     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;totalforshipping=0;
     document.writeln('<center><table border="2" cellpadding="3" cellspacing="1" bordercolor="#ffffff">');
     document.writeln('<tr>');
     document.writeln('<td><p><b>Product Name</b></p></td>');
     document.writeln('<td align=right><p><b>Price</p></td>');
     document.writeln('<td align=right><p><b>Quantity</p></td><td align=right><p><b>Total Cost</p></td>');
     document.writeln('</tr>');
     itemlist=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 (currency==2) theprice=theprice2;
             if (theAP!='none') {
     tempprice=getAdvanced(theAP,currency,thequantity); if (tempprice>0) theprice=tempprice; }
             itemtotal=(eval(theprice*thequantity));
             temptotal=itemtotal*100;
             totprice=totprice+itemtotal;
             if (thetaxable == 0) taxablestandard = taxablestandard + itemtotal;
             if (thetaxable == 1) taxablesecond = taxablesecond + itemtotal;
             totalforshipping = totalforshipping + eval(thequantity);
             itemlist=itemlist+1;
         document.writeln('<tr><td><p>'+theitem+'</p></td>');
     document.writeln('<input type=hidden name="'+nextField()+'Name'+itemlist+'" value="'+theitem+'"><input type=hidden name="'+nextField()+'Quantity'+itemlist+'" value="'+thequantity+'">');

     document.writeln('<td align=right><p>'+presentValue(eval(theprice))+'</p></td><input type=hidden name="'+nextField()+'Price'+itemlist+'" value="'+presentValue(eval(theprice))+'">');
     document.writeln('<td align=right><p>'+thequantity+'</p></td>');
     document.writeln('<td align=right><p>'+presentValue(itemtotal)+'</p></td><input type=hidden name="'+nextField()+'Item Total'+itemlist+'" value="'+presentValue(eval(itemtotal))+'">');
     document.writeln('</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;
             }
         }


	// TOTAL GOODS //
     document.writeln('<tr><td colspan=3><p><i>Sub Total <input type=hidden name="numberitems" value="'+itemlist+'"> </p></td>');
     if (currency==1) { document.writeln('<td align=right><p><i> '+presentValue(totprice)+'</p></td>');}
     if (currency==2) { document.writeln('<td align=right><p><i> '+presentValue2(totprice)+'</p></td>');}
     document.writeln('</tr>');


	// DISCOUNT //
	// SET DISCOUNT VALUE //
	var discount_rate=0.2;
	var discount_rate_title="20%";
	var discount=getCookie('loginname');
	if (discount != "" && discount != null && discount != "null" && discount != "undefined") {
		var discount_value=totprice*discount_rate;
		var new_discount_value=Math.round(discount_value*Math.pow(10,2))/Math.pow(10,2);
		if (discount_value.toFixed) new_discount_value=discount_value.toFixed(2);
		totprice=totprice-new_discount_value;
		document.writeln('<tr><td colspan=2><p><i>Club Wounds UK Discount</i></p></td>');
		document.writeln('<td align=right><p><i>'+discount_rate_title+'</i></p></td>');
		document.writeln('<td align=right><p><i> -£'+new_discount_value+' </i></p><input type="hidden" name="discount_rate" value="'+discount_rate+'"><input type="hidden" name="discount_rate_title" value="'+discount_rate_title+'"><input type="hidden" name="discount_value" value="'+new_discount_value+'"></td>');
//		document.writeln('<td align=center><p>&nbsp;</p></td>');
 	    document.writeln('</tr>');
	}



	if (delivery != "") {
		if (delivery == "Worldwide") {
			deliv_title="Worldwide Delivery";
		}else if (delivery == "Europe") {
			deliv_title="Europe Delivery";
		}else{
			deliv_title="UK Delivery";
		}
		// DELIVERY COSTS //
	     document.writeln('<tr><td colspan=3><p><i>'+deliv_title+'<input type="hidden" name="deliv_title" value="'+deliv_title+'"></p></td>');
	     if (currency==1) { document.writeln('<td align=right><p><i><input type=hidden name="delivery_cost" value="'+deliv_cost+'">£'+deliv_cost+'</p></td>');}
	     if (currency==2) { document.writeln('<td align=right><p><i><input type=hidden name="delivery_cost" value="'+deliv_cost+'">'+deliv_cost+'</p></td>');}
	     document.writeln('</tr>');
	}else{
		deliv_cost=0;
	}

	price_total=presentValue(totprice).substring(1,10);
	price_total=parseFloat(price_total);
	deliv_cost=parseFloat(deliv_cost);
	new_price=price_total+deliv_cost;
	if (new_price == 0) { new_price="0.00";}
	new_price=new_price+".0";
	parts=new_price.split('.');
	pounds=parts[0];
	pence=parts[1];
	pence=pence+"00";
	remainder=pence.substring(2,3);
	pence=pence.substring(0,2);
	if (remainder > 5) {
		pence++;
	}
	new_price=pounds+"."+pence;
	new_price=eval(new_price);

//	var new_price=Math.round(new_price*Math.pow(10,2))/Math.pow(10,2);
//	if (new_price.toFixed) new_price=new_price.toFixed(2);


	// GRAND TOTAL //
     document.writeln('<tr><td colspan=3><p><b>Order Total</p></td>');
     if (currency==1) { document.writeln('<td align=right><p><b>'+presentValue(new_price)+'</b><input type=hidden name="Goods_Total" value="'+new_price+'"></p></td>');}
     if (currency==2) { document.writeln('<td align=right><p><b>'+new_price+'</b><input type=hidden name="Goods_Total" value="'+new_price+'"></p></td>');}
     document.writeln('</tr>');
     document.writeln('</table><br>');
}

    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-2020 08:00:00 GMT';
		if (newItemList == "" || newItemList == null || newItemList == "null" || newItemList == "undefined") {
            document.cookie='TheBasket=.';
			setCookie('delivery', "standard", 'Friday, 31-Dec-2020 08:00:00 GMT');
		}
		location.reload();
    }

    function clearBasket() {
        if (confirm('Are you sure you wish to clear the basket?')) {
            index=document.cookie.indexOf('TheBasket');
            document.cookie='TheBasket=.';
			setCookie('delivery', "standard", 'Friday, 31-Dec-2020 08:00:00 GMT');
			location.reload();
        }
    }
    function clearBasketNoCheck() {
        index=document.cookie.indexOf('TheBasket');
        document.cookie='TheBasket=.';
		setCookie('delivery', "standard", 'Friday, 31-Dec-2020 08:00:00 GMT');
    }

    function changeCurrency(num) {
         index=document.cookie.indexOf('TheCurrency');
         document.cookie='TheCurrency='+num+'; expires=Friday, 31-Dec-2020 08:00:00 GMT';
		location.reload();
    }

    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-2020 08:00:00 GMT';
		location.reload();
    }

function changeDelivery(type) {
	if (type != "") {
		setCookie('delivery', type, 'Friday, 31-Dec-2020 08:00:00 GMT');
		location.reload();
	}
}
		
    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);
    }

    numFields=0;
    function addFiller(curnum) {
        newnum=parseInt(curnum);
        if (newnum<10) return '00'+newnum;
        if (newnum<100) return '0'+newnum;
        return newnum;
    }

    function nextField() {
        numFields++;
        return addFiller(numFields)+'-';
    }


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;
}










