  var thequantity;
  
    function presentValue(value) {
        if(value<=0.9999) {
            newPounds='0';
        } else {
            newPounds=parseInt(value);
        }
        
        if (value>0) {
            newPence=Math.round((value+.000008 - newPounds)*100);
        } else {
            newPence=0;
        }        
        
        if (newPence==100){newPence=0; ++newPounds}//fix bug
        	
        compstring='9';
        for (var i=1; i <=1;i++) {
            if (eval(newPence) <= eval(compstring)) newPence='0'+newPence;
            compstring=compstring+'9';
        }
        newString='£' + newPounds + '.' + newPence + '';

        return (newString);
    }
	
var basketsum,rediv
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 basketsummary() {

        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);
              
        itemlist=0;
        for(var i=0; i<=fulllist.length;i++) {
            if(fulllist.substring(i,i+1)=='['){itemlist=itemlist+1;}
        }
		if ((basketsum!=null)&(basketsum!="")){rediv="yes"}
		basketsum="";
		basketsum=basketsum+'My Basket: ';
		if(itemlist==0){basketsum=basketsum+='empty';}else if(itemlist==1){basketsum=basketsum+='1 item';}else{basketsum=basketsum+=itemlist+' items';}
		basketsum=basketsum+'&nbsp;';
		if (rediv!="yes")document.write(basketsum);
		if (rediv=="yes")basketsumdiv.innerHTML=basketsum;
}

    
function showBuyBasket(cookiereceived) {
	
        //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);
        //alert(cookiereceived)
        fulllist = cookiereceived
	
	
	if (fulllist.length < 2){//||(index==-1)){ 
          document.writeln('<p>Sorry, you have not selected any products</p><br><br>');
          document.writeln('<a href="basket.asp">Return to Shopping Basket</a>');
	}else{        
        
          totprice=0;
          ppcost = 5;


		  filler=' ';
		  theorder='';
          
          document.writeln('<table border=0 cellspacing="2" cellpadding="2" align="center" width=100%>');
          document.writeln('<tr><td align=left><b><p>Item</p></b></td><td align=right><b><p>Qty</p></b></td><td align=right nowrap><b><p>Unit Cost</p></b></td><td colspan=2 align=right><b><p>Total Cost</p></b></td></tr>');
          document.writeln('<tr><td colspan=5><hr noshade size=1></td></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;
                	itemtotal=(eval(theprice*thequantity));
                	temptotal=itemtotal*100;
                	totprice=totprice+itemtotal;
                	itemlist=itemlist+1;
			
			theorder = theorder + '['+itemlist+ ']  '+thequantity+' x '+field1+' @ £'+eval(theprice)+' = £'+itemtotal+'\n';
            
                	document.writeln('<tr><td><input type=hidden name=itemlist value='+itemlist+'><p>'+field1+'&nbsp;</p></td><td align=right><p>'+thequantity+'</p></td><td align=right><p>&nbsp;'+theprice+'</p></td><td align=right colspan=2><p>&nbsp;'+itemtotal+'</p></td></tr>');
  
  } else if (fulllist.substring(i,i+1)=='|') {
                	if (thisitem==1) field1 = fulllist.substring(itemstart,i);
                	if (thisitem==2) theprice = fulllist.substring(itemstart,i);
                	if (thisitem==3) field4 = fulllist.substring(itemstart,i);
                	
                	thisitem++;
                	itemstart=i+1;
            }
        }
document.writeln('<tr><td colspan=5><hr></td></tr>');
        document.writeln('<tr><td colspan=4 align=right><p>Sub Total</p></td><td align=right><p>&nbsp;'+presentValue(totprice)+'</p></td><td></td></tr>');
       	theorder=theorder+'\n\n';
        subtotal=totprice;
        theorder=theorder+'Sub Total       : '+ presentValue(subtotal)+'\n';


        
       
	document.writeln('<tr><td colspan=5><hr></td></tr>');
        document.writeln('<tr><td colspan=4><p><b>Total Goods</b></p></td><td align=right><INPUT TYPE=TEXT NAME="Total" VALUE='+presentValue(subtotal)+' SIZE=8 onFocus="this.blur()"></td><td></td></tr>');
        document.writeln('<tr><td colspan=5><hr></td></tr>');
        document.writeln('</table>');  
  	
  	document.writeln('<input type=hidden name=amount value='+presentValue(subtotal)+'><input type="hidden" name="Order_Summary_" value="'+theorder+'">');                         
      }	
    }
    
 


    function removeItem(itemno) {
        alert(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
        self.location = 'checkout.asp';
    }

    function showBasket() {
        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);
        //alert(fulllist);
        
        totprice=0;
        //document.writeln('<form action="https://secure.esterling.co.uk/fossilcavern.co.uk/checkout.asp" method="post" name="basketform">');
        //document.writeln('<form action="checkout.asp" method="post" name="basketform">');
        document.writeln('<br><table bgcolor=#ffffff width=97% align=center border=0 cellspacing=1 cellpadding=5><tr><td colspan="3"><p>I wish to enquire about the following products: <br><br></p></td></tr>');
        document.writeln('<tr bgcolor=#faac16><td align=left bgcolor=#faac16><p><b>Product</b></p></td><td align=right bgcolor=#faac16><p><b>Quantity</b></p></td><td bgcolor=#faac16><p align="center"><b>Action</b></p></td></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;
                	itemtotal=(eval(theprice*thequantity));
                	temptotal=itemtotal*100;
                	totprice=totprice+itemtotal;
                	itemlist=itemlist+1;
                	
                	
                	if (theprice == 0){
                		displayprice='POA'
                	}else{
                		displayprice=presentValue(eval(theprice))
                	}
	document.writeln('<tr bgcolor=#ffffff><td><p>'+field1+'</p></td><td align=right><p>'+thequantity+'</p></td><td align=center><a href="javascript:removeItem(' +itemlist+ ');"><img src="images/Removeitem.gif" alt="Remove ' +field1+' from enquiry" border="0"></a></td></tr>');



            	} else if (fulllist.substring(i,i+1)=='|') {
                	if (thisitem==1) field1 = fulllist.substring(itemstart,i);
                	if (thisitem==2) theprice = fulllist.substring(itemstart,i);
                	if (thisitem==3) field4 = fulllist.substring(itemstart,i);
                	
  			         
                	thisitem++;
                	itemstart=i+1;
            }
        }
        //document.writeln('<tr><td colspan=3><hr></td></tr>');
        document.writeln('</table><input type="hidden" name="enquirydetails" value="'+fulllist+'"><br>');
       // document.writeln('<p align="center" style="font-weight:bold;">You are about to enter our 128bit secure server.<br>From now on all information given by you will be encrypted.</p><br><input type="hidden" name="thebasket" value="'+fulllist+'"><div align=center><input type="image" border="0" src="i/clearbasket2.gif" alt="Clear your basket" value="Clear basket" onClick="clearBasket();">&nbsp;<input type=image src=i/continue.gif alt="continue to checkout"></a></div>');
        //document.writeln('</form>');
    }

    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
        self.location = 'contact.asp';
    }

    function clearBasket() {
        if (confirm('Are you sure you wish to clear the basket?')) {
            index=document.cookie.indexOf('TheBasket');
            document.cookie='TheBasket=.';
            self.location='basket.asp';
        }
    }

    function updateBasket() {
        newItemList=null;
        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='quant'+itemlist;
                newItemList=newItemList+'['+field1+'|'+field2+'|'+field3+'|'+field4+'|'+document.basketform.elements[thiselement].value+']';
            } else if(fulllist.substring(i,i+1)=='|') {
                if (thisitem==1) field1 = fulllist.substring(itemstart,i);
                if (thisitem==2) field2 = fulllist.substring(itemstart,i);
                if (thisitem==3) field3 = fulllist.substring(itemstart,i);
                if (thisitem==4) field4 = fulllist.substring(itemstart,i);
                thisitem++;
                itemstart=i+1;
            }
        }
        index=document.cookie.indexOf('TheBasket');
        document.cookie='TheBasket='+newItemList
        self.location='basket.asp';
    }

    function BuyItem(newItem, newPrice, newTaxable, newQuantity) {
        if(newQuantity<=0) {
            rc = alert('The quantity entered is incorrect');
        } else {
            if (confirm('Would you like to add '+newQuantity+' x '+newItem+' to your enquiry? ')) {
                index=document.cookie.indexOf('TheBasket');
                countbegin=(document.cookie.indexOf('=',index)+1);
                countend=document.cookie.indexOf(';',index);
                if(countend==-1) {
                    countend=document.cookie.length;
                }              
                //alert('To carry on to the checkout please click the "My Basket" link on the left.')
                document.cookie='TheBasket='+document.cookie.substring(countbegin,countend)+'['+newItem+'|'+newPrice+'|'+newTaxable+'|'+newQuantity+']';
                //basketsummary();
            }            
        }
    }

function finishOrder(){
	theorder = theorder + '\n****************************************************\n\n';
	document.basketform.Order_Summary_.value=theorder;
}
