var deferred_credit=0;
var free_delivery=0;
   
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 (eval(newPence) >= 100) 
   {
      newPence='0';
      newPounds=eval(newPounds)+1;
   }
   if (eval(newPence) <= 9) newPence='0'+newPence;

   newString='£' + newPounds + '.' + newPence + '';
   return (newString);
}


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);
   totprice=0;
   ordertotal=0;
   itemlist=0;
   delivery=0;
   deferred_credit=0;   

   for (var i=0; i<=fulllist.length;i++)
   {
      if (fulllist.substring(i,i+1)==']')
      {
         itemlist=itemlist+1;
      }
   }

   if (itemlist==0)
   {    
      document.writeln('<font color=#800000 face="Times New Roman" 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></tr></table>');
   }
   else
   {
      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>');
      document.writeln('<img border="0" src="images/secure.gif" width="113" height="61"></td></tr></table>');
       
      document.writeln('<table border=0 cellpadding=0 cellspacing=1 width="100%">');
      document.writeln('<tr>');
      document.writeln('  <td width="3%"><font color=#000080 face="Times New Roman" size=2><B>&nbsp;</B></font></td>');
      document.writeln('  <td bgcolor=#B3ADFF width="56%"><font color=#000080 face="Times New Roman" size=2><B>&nbsp;Product Name</B></font></td>');
      document.writeln('  <td bgcolor=#B3ADFF width="13%" align=right><font color=#000080 face="Times New Roman" size=2><B>Price&nbsp;</B></font></td>');
      document.writeln('  <td bgcolor=#B3ADFF width="12%" align=right><font color=#000080 face="Times New Roman" size=2><B>Quantity&nbsp;</B></font></td>');
      document.writeln('  <td bgcolor=#B3ADFF width="13%" align=right><font color=#000080 face="Times New Roman" size=2><B>Total Cost&nbsp;</B></font></td>');
      document.writeln('  <td align=right><font color=#000000 face="Times New Roman" 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==25)
               {
                  delivery=25;
               }
               else
               {
                  delivery=1.95;
               }
            }
            else
            {
               delivery=25;
            }
            itemtotal=0;
            dc_loc=theCode.indexOf("{DC}");
            if (dc_loc > 0)
            {
               deferred_credit=1;
            }
            fd_loc=theCode.indexOf("{FD}");
            if (fd_loc > 0)
            {
               free_delivery=1;
            }            
            itemtotal=(eval(theprice*thequantity));
            temptotal=itemtotal*100;
            totprice=totprice+itemtotal;
            itemlist=itemlist+1;
            document.writeln('<tr><td><a href="'+thepage+'"><img border="0" alt="Click to see product page" src="images/link.gif" width="17" height="17"></a></td>');
            document.writeln('<td bgcolor=#ffd694><font color=#000000 face="Times New Roman" size=2>&nbsp;'+theitem+'</font></td>');
            document.writeln('<td bgcolor=#FFD694 align=right><font color=#000000 face="Times New Roman" size=2>'+presentValue(eval(theprice))+'&nbsp;</font></td>');
            document.writeln('<td bgcolor=#FFD694 align=right><font color=#000000 face="Times New Roman" size=2><input type=text maxlength="2" name="quant'+itemlist+'" value='+thequantity+' size=3></font></td>');
            document.writeln('<td bgcolor=#FFD694 align=right><font color=#000000 face="Times New Roman" size=2>'+presentValue(itemtotal)+'&nbsp;</font></td>');
            document.writeln('<td align=left><font color=#000000 face="Times New Roman" size=2><a href="javascript:removeItem('+itemlist+');"><img border="0" alt="Click to remove this item" src="images/removeitem.gif" name="remove'+itemlist+'" width="72" height="18"></a></font></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) 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 > 300)
      {
        delivery=00;
      }
      if (free_delivery==1)
      {
         delivery=00;
      }      
      totprice=totprice+delivery;      
      document.writeln('<tr>');
      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>');
      document.writeln('<td colspan=2 align=right><font color=#000000 face="Times New Roman" size=2>Delivery&nbsp;</font></td>');
      document.writeln('<td bgcolor=#FFD694 align=right><font color=#000000 face="Times New Roman" size=2>'+presentValue(delivery)+'&nbsp;</font></td>');
      document.writeln('<td align=right><font color=#000000 face="Times New Roman" size=2>&nbsp;</font></td>');
      document.writeln('</tr>');
      
      document.writeln('<tr>');
      document.writeln('<td colspan=2 align=right><font color=#000000 face="Times New Roman" size=2><B>Total Price (inc VAT)&nbsp;</B></font></td>');
      document.writeln('<td bgcolor=#FFD694 align=right><font color=#000000 face="Times New Roman" size=2><B>'+presentValue(totprice)+'&nbsp;</B></font></td>');
      document.writeln('<td align=right><font color=#000000 face="Times New Roman" size=2>&nbsp;</font></td>');
      document.writeln('</tr>');

      document.writeln('</table><br><center>');

      //document.writeln('<P>');              
      document.writeln('<table border="0" bgcolor="#008080" width="440"><tr><td align="center"><font size="2" color=white>');         
      document.writeln('Select your prefered Payment Method and proceed to our secure Checkout<BR>');
      document.writeln('</font>');
      document.writeln('<select name="paymentmethod">');
      document.writeln('<option value="CC">Credit Card/Switch</option>');
      document.writeln('<option value="CHEQUE">Cheque</option>');
      if (deferred_credit==1)
      {
         document.writeln('<option value="FINANCE">Free for 6 months</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('<input type="SUBMIT" value="Secure Checkout >>"></center>');

   }
}

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==25)
               {
                  delivery=25;
               }
               else
               {
                  delivery=1.95;
               }
            }
            else
            {
               delivery=25;
            }         
         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+'">');

      }
      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) 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 > 300)
   {
     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)
{
   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 = 'basket.php3';
 
}

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()
{
   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.basket.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 = 'basket.php3';

}

function showterms()
{
   fmwin1=window.open('terms.php3','Terms','toolbar=no,status=no,width=400,height=300,directories=no,scrollbars=yes,location=no,resizable=yes,menubar=no');

}