function presentValue(value)
{
	if(value<0)
	{
		neg='-';
		value=value*-1;
	}
	else
	{
		neg='';
	}		
   if(value<=0.9999)
   {
      newPounds='0';
   }
   else
   {
      newPounds=parseInt(value);
   }

   if (value>0)
   {
      newPence=Math.round((value+.000008 - newPounds)*100);
   }
   else
   {
      newPence=0;
   }
   if (eval(newPence) >= 100) 
   {
      newPence='0';
      newPounds=eval(newPounds)+1;
   }
   if (eval(newPence) <= 9) newPence='0'+newPence;

   newString= neg + '&pound;' + newPounds + '.' + newPence + '';
   return (newString);
}


function showBasket(inc_head)
{
   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;
   ordertotal=0;
   itemlist=0;
   delivery=0;
   deferred_credit=0;
   free_delivery=9;   /* 9-Initial state, free delivery off, 1-Free Delivery, 0-Not Free delivery */

   for (var i=0; i<=fulllist.length;i++)
   {
      if (fulllist.substring(i,i+1)==']')
      {
         itemlist=itemlist+1;
      }
   }

   if (itemlist==0)
   {    
      document.writeln('<font color=#C0C0C0 face="Arial" size=4><B><I> &nbsp; Shopping Basket is empty!</I></B></font>');
      document.writeln('<td><img border="0" src="images/secure.gif" width="113" height="61"></td>');
   }
   else
   {
      if (inc_head==1)
      {
         document.writeln('&nbsp;&nbsp;&nbsp;&nbsp;<a href="javascript:clearBasket();"><img border="0" alt="Click to Empty Basket" src="images/emptybasket.gif" name="emptybasket"></a>&nbsp;&nbsp;&nbsp;');
         document.writeln('<a href="javascript:updateBasket();"><img border="0" alt="Click to Update Basket" src="images/updatebasket.gif" name="updatebasket"></a>');
         document.writeln('</td><td><a href="javascript:showterms();"><font size="1"><u>Terms and Conditions of Sale</u></font></a>');
         document.writeln('</td><td align=center>');
         document.writeln('<img border="0" src="images/secure.gif" width="113" height="61">&nbsp;&nbsp;<img border="0" src="images/hacker_safe.gif" width="89" height="61"></td></tr></table>');
      } 
      document.writeln('<table border=0 cellpadding=0 cellspacing=1 width="100%">');
      document.writeln('<tr>');
      if (inc_head==1)
      {
         document.writeln('  <td width="3%"><font color=#000080 face="Times New Roman" size=2><B>&nbsp;</B></font></td>');
      }
      document.writeln('  <td bgcolor=#E28D28 width="56%"><font color=#000000 face="Arial" size=2><B>&nbsp;Product Name</B></font></td>');
      document.writeln('  <td bgcolor=#E28D28 width="13%" align=right><font color=#000000 face="Arial" size=2><B>Price&nbsp;</B></font></td>');
      document.writeln('  <td bgcolor=#E28D28 width="12%" align=right><font color=#000000 face="Arial" size=2><B>Quantity&nbsp;</B></font></td>');
      document.writeln('  <td bgcolor=#E28D28 width="13%" align=right><font color=#000000 face="Arial" size=2><B>Total Cost&nbsp;</B></font></td>');
      document.writeln('  <td align=right><font color=#000000 face="Arial" size=2> </font></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);

            if (theitem.substring(0,8)=='Gliptone')
            {
               if (delivery==15)
               {
                  delivery=15;
               }
               else
               {
                  delivery=1.95;
               }
            }
            else
            {
               delivery=15;
            }
            itemtotal=0;
            dc_loc=theCode.indexOf("{DC}");
            if (dc_loc > 0)
            {
               deferred_credit=1;
            }
            fd_loc=theCode.indexOf("{FD}");
            if (fd_loc > 0)
            {
               if (free_delivery==0)
               {
                  /* there is already a chargeable item */
                  free_delivery=0;
               }
               else
               {
                  free_delivery=1;
               }
            }
            else
            {
               free_delivery=0;
            }
            itemtotal=(eval(theprice*thequantity));
            temptotal=itemtotal*100;
            totprice=totprice+itemtotal;
            itemlist=itemlist+1;
            //set background colour and icon
            if (theCode=="DS100")
            {
            	bgColor="#B2D8B2";
            	itemicon="images/discount.gif";
            }
         	else
         	{
         		bgColor="#26292E";
            	itemicon="images/link.gif";         		
         	}	
            document.writeln('<tr>');
            if (inc_head==1)
            {
               document.writeln('<td align=right><a href="'+thepage+'"><img border="0" alt="Click to see product page" src="'+itemicon+'" width="17" height="17"></a>&nbsp;</td>');
            }
            document.writeln('<td bgcolor='+bgColor+'><font color=#D0D0D0 face="Arial" size=2>&nbsp;')
				if(the24==1)
				{
					document.writeln('<img src="images/24.gif">');
				}
				document.writeln(theitem+'</font></td>');
            document.writeln('<td bgcolor='+bgColor+' align=right><font color=#D0D0D0 face="Arial" size=2>'+presentValue(eval(theprice))+'&nbsp;</font></td>');
            if ((inc_head==1) & (theCode!="DS100"))
            {
               document.writeln('<td bgcolor='+bgColor+' align=right><font color=#D0D0D0 face="Arial" size=2><input type=text maxlength="2" name="quant'+itemlist+'" value='+thequantity+' size=3></font></td>');
            }
            else if (theCode=="DS100")
            {
               document.writeln('<td bgcolor='+bgColor+' align=right>&nbsp;</td>');
            }            
            else
            {
               document.writeln('<td bgcolor='+bgColor+' align=right><font color=#D0D0D0 face="Arial" size=2>'+thequantity+'&nbsp;</font></td>');
            }
            document.writeln('<td bgcolor='+bgColor+' align=right><font color=#D0D0D0 face="Arial" size=2>'+presentValue(itemtotal)+'&nbsp;</font></td>');
            if (inc_head==1)
            {
               document.writeln('<td align=left><font color=#D0D0D0 face="Arial" size=2><a href="javascript:removeItem('+itemlist+');"><img border="0" alt="Click to remove this item" src="images/removeitem.gif" name="remove'+itemlist+'" width="60" height="18"></a></font></td>');
            }
            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) the24 	  = fulllist.substring(itemstart,i);
            if (thisitem== 4) theCode    = fulllist.substring(itemstart,i);
            if (thisitem== 5) theFabric  = fulllist.substring(itemstart,i);
            if (thisitem== 6) thepage    = fulllist.substring(itemstart,i);

            thisitem++; itemstart=i+1;
         }
      }

      if (totprice > 200)
      {
        delivery=00;
        deferred_credit=1;        
      }
      if (free_delivery==1)
      {
         delivery=00;
      }

      totprice=totprice+delivery;     

      document.writeln('<tr>');
      if (inc_head==1)
      {
         document.writeln('<td rowspan=2>&nbsp;</td><td rowspan=2><a href="javascript:window.print();"><img border=0 alt="Print Basket" src="images/printbasket.gif"></a></td>');
      }
      else {
         document.writeln('<td rowspan=2><a href="javascript:window.print();"><img border=0 alt="Print Basket" src="images/printbasket.gif"></a></td>');
      }
      document.writeln('<td colspan=2 align=right>');
      document.writeln('<font face="Arial" size=2>Delivery <font size=1>(*see below)</font>&nbsp;</font></td>');
      document.writeln('<td bgcolor=#cAd6cA align=right><font face="Arial" color=#000000 size=2>'+presentValue(delivery)+'&nbsp;</font></td>');
      document.writeln('<td align=right><font face="Arial" size=2>&nbsp;</font></td>');
      document.writeln('</tr>');
      
      document.writeln('<tr>');
      document.writeln('<td colspan=2 align=right><font  face="Arial" size=2><B>Total Price (inc VAT)&nbsp;</B></font></td>');
      document.writeln('<td bgcolor=#cAd6cA align=right><font color=#000000 face="Arial" size=2><B>'+presentValue(totprice)+'&nbsp;</B></font></td>');
      document.writeln('<td align=right><font color=#000000 face="Arial" size=2>&nbsp;</font></td>');
      document.writeln('</tr>');

      document.writeln('</table><br><center>');

   }
}

function showBasketBuy()
{

         document.writeln('<table border="0" bgcolor="#26292E" width="450" cellpadding=1 cellspacing=1><tr><td><table border="0" bgcolor="#FFF3D3" width="100%"><tr><td width=50 rowspan=2 valign=top><img src="images/basket_ok.gif" align=left></td><td colspan=2 align="center"><font face=Arial size="2" color="#202020">');         
         document.writeln('Discount Voucher <input type="text" name="voucher"> <a title="Click to add Voucher" href="javascript:addvoucher();"><img border=0 alt="Click to add Voucher" src="images/buy.gif"></a><BR><i><font size=1>Note: Only add discount voucher codes prior to placing your order</font></i><hr>');
         document.writeln('Select your prefered Payment Method and proceed to our Secure Checkout Page<BR clear=all>');
         document.writeln('</font>');
         document.writeln('</td></tr><tr><td valign=top align=left><table border=0 width=240>');
			//document.writeln('<tr><td valign=middle align=left><input type="radio" name="paymentmethod" value="CC" checked></td><td><img src="images/icon_cc.jpg" align="left" style="margin-right:7px;"><div style="font-size:11px; font-family: Arial, Verdana; PADDING-TOP: 3px;">Credit/Debit Card</div></td></tr>');
         //if (deferred_credit==1)
         //{
			//	document.writeln('<tr><td valign=middle align=left><input type="radio" name="paymentmethod" value="FINANCE"></td><td><img src="images/icon_finance.jpg" align="left" style="margin-right:7px;"><div style="font-size:11px; font-family: Arial, Verdana; PADDING-TOP: 3px;">Buy Now Pay Later Finance</div></td></tr>');
			//}
			//document.writeln('<tr><td valign=middle align=left><input type="radio" name="paymentmethod" value="PAYPAL"></td><td><img src="images/icon_paypal.jpg" align="left" style="margin-right:7px;"><div style="font-size:11px; font-family: Arial, Verdana; PADDING-TOP: 3px;">The safer, easier way to pay.</div></td></tr>');

			document.writeln('<select name="paymentmethod">');
         document.writeln('<option value="CC">Credit/Debit Card</option>');
         //document.writeln('<option value="PAYPAL">Paypal</option>');
         if (deferred_credit==1)
         {
            document.writeln('<option value="FINANCE">Buy Now Pay Later</option>');
         }      
         document.writeln('</select>');


         //document.writeln('<img border="0" alt="Click to Purchase" src="images/purchase.gif" onClick="submit();" width="125" height="23">');
         document.writeln('</table></td><td valign=top><input type="image" name="SUBMIT" src="images/basket2.jpg">');
         //document.writeln('&nbsp;<input type="SUBMIT" name="submit" value="Secure Checkout >>"></center>');
         document.writeln('</td></tr></table></td></tr></table><br>');

}

function check_paymethod() 
{
	// if nextday delivery value selected then check payment method is credit/debit card
	if(document.basket.nextday_date)
	{
	   if(document.basket.nextday_date.selectedIndex>0)
		{
			// a 24 hour date is selected
			if(document.basket.paymentmethod.options[document.basket.paymentmethod.selectedIndex].value=="CC")
			{
				//ok
				return true;
			}
			else
			{
				var answer = confirm("** There is a problem with your order **\n\nYou have chosen a 24 Hour Delivery Date\nYou can only select Credit/Debit Card for this delivery option\n\nPlease either\n\n1. Select Cancel and change Payment Method\n2. Select OK to continue with order (24 hour delivery will not be available)");
				if (answer)
				{
					document.basket.nextday_date.selectedIndex=0;
					return true;
				}
				else
				{
					return false;
				}	
			}
		}	
	}
	else{
		// No 24 hour items so ok
		return true;
	}
}

function showHiddenBasket()
{
   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;
   ordertotal=0;
   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);
         
            if (theitem.substring(0,8)=='Gliptone')
            {
               if (delivery==15)
               {
                  delivery=15;
               }
               else
               {
                  delivery=1.95;
               }
            }
            else
            {
               delivery=15;
            }         
         itemtotal=0;
         itemtotal=(eval(theprice*thequantity));
         temptotal=itemtotal*100;
         totprice=totprice+itemtotal;
         itemlist=itemlist+1;
         document.writeln('<input type="hidden" name="basketitem'+itemlist+'" value="'+theitem+'">');
         document.writeln('<input type="hidden" name="basketitemprice'+itemlist+'" value="'+presentValue(eval(theprice))+'">');
         document.writeln('<input type="hidden" name="basketitemqty'+itemlist+'" value="'+thequantity+'">');
         document.writeln('<input type="hidden" name="baskettotalprice'+itemlist+'" value="'+presentValue(itemtotal)+'">');
         document.writeln('<input type="hidden" name="basketfmartcode'+itemlist+'" value="'+theCode+'">');
         document.writeln('<input type="hidden" name="basketfabric'+itemlist+'" value="'+theFabric+'">');
         document.writeln('<input type="hidden" name="basketnxtday'+itemlist+'" value="'+the24+'">');
      }
      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) the24      = fulllist.substring(itemstart,i);
         if (thisitem== 4) theCode    = fulllist.substring(itemstart,i);
         if (thisitem== 5) theFabric  = fulllist.substring(itemstart,i);
         if (thisitem== 6) thepage    = fulllist.substring(itemstart,i);
         thisitem++; itemstart=i+1;
      }
   }
   if (totprice > 200)
   {
     delivery=00;
   }
   if (free_delivery==1)
   {
      delivery=00;
   }

   totprice=totprice+delivery;  
   document.writeln('<input type="hidden" name="basketdelivery" value="'+presentValue(delivery)+'">');
   document.writeln('<input type="hidden" name="basketgrandtotal" value="'+presentValue(totprice)+'">');
   document.writeln('<input type="hidden" name="referrer" value="'+parent.referrer+'">');

}

function removeItem(itemno)
{
   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);	
   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;
	removeBasketVouchers()
   self.location = 'basket.php3';
 
}

function addvoucher()
{
   self.location = 'basket.php3?voucher='+document.basket.voucher.value+'&baskettot='+document.basket.baskettotalhid.value;
}

function clearBasket()
{
   if (confirm('Are you sure you wish to clear the basket?'))
   {
      index=document.cookie.indexOf('TheBasket');
      document.cookie='TheBasket=.';

      self.location = 'basket.php3';
   }
}


function updateBasket()
{
	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);
   
   newItemList='';
   itemlist=0;
   theCode="";

   for(var i=0;i<=fulllist.length; i++)
   {
      if(fulllist.substring(i,i+1)=='[')
      {
         thisitem=1;
         itemstart=i+1;
         linestart=i+1;
         fullstart=i+1;
      }
      else if(fulllist.substring(i,i+1)==']')
      {
         itemend=i;itemlist++;
         if(theCode!="DS100")
         {
         	thiselement=document.basket.elements['quant'+itemlist].value;
         }
      	else
      	{
      		thiselement='1';
      	}
         newItemList=newItemList+'['+partList+'|'+thiselement+']'
      }
      else if(fulllist.substring(i,i+1)=='|')
      {
         if (thisitem== 4) theCode = fulllist.substring(itemstart,i);  
         partList=fulllist.substring(linestart,i);
         thisitem++; itemstart=i+1;
      }
   }
   index=document.cookie.indexOf('TheBasket');
   document.cookie='TheBasket='+newItemList;
	removeBasketVouchers()
   self.location = 'basket.php3';
}

