today=new Date();
today.setTime(today.getTime() + (3600000));
today=today.toGMTString();
currentdate=new Date();
currentdate=currentdate.toLocaleString();
index = document.cookie.indexOf('MFBasketName');
if (index > -1) {
   countbegin=(document.cookie.indexOf('=',index)+1);
   countend=document.cookie.indexOf(';',index);
   if(countend==-1) {
      fulllist='';
   } else {
      fulllist=document.cookie.substring(countbegin,countend);
   }
} else {
   fulllist='';
}
if(fulllist.length < 1) {
   document.cookie='xCode=.; expires='+today;
   document.cookie='MFBasketName=.; expires='+today;
   document.cookie='MFBasketWeight=.; expires='+today;
   document.cookie='MFBasketTaxable=.; expires='+today;
   document.cookie='MFBasketPrice=.; expires='+today;
   document.cookie='MFBasketExtraFields=.; expires='+today;
   document.cookie='MFBasketAdvanced=.; expires='+today;
   document.cookie='MFBasketQuantity=.; expires='+today;
   document.cookie='TheCurrency=1; expires=Friday, 31-Dec-2030 08:00:00 GMT';
   document.cookie='JShopReferrer=.; expires='+today;
}

currencyCode = new Array(1); currencyName = new Array(1); currencyDecimals = new Array(1); currencyPre = new Array(1); currencyMiddle = new Array(1); currencyPost = new Array(1); 
currencyCode[1]='GBP'; currencyName[1]='GBP'; currencyDecimals[1]='2'; currencyPre[1]='&pound;'; currencyMiddle[1]='.'; currencyPost[1]=''; 
    function goBuy() {
        ilist='';
        ilist=ilist+retrieveCookie('MFBasketCode',0,0)+'^';
        ilist=ilist+retrieveCookie('MFBasketName',0,0)+'^';
        ilist=ilist+retrieveCookie('MFBasketWeight',0,0)+'^';
        ilist=ilist+retrieveCookie('MFBasketTaxable',0,0)+'^';
        ilist=ilist+retrieveCookie('MFBasketPrice',0,0)+'^';
        ilist=ilist+retrieveCookie('MFBasketExtraFields',0,0)+'^';
        ilist=ilist+retrieveCookie('MFBasketAdvanced',0,0)+'^';
        ilist=ilist+retrieveCookie('MFBasketQuantity',0,0)+'^';
        ilist=ilist+retrieveCookie('TheCurrency',0,0)+'^';
        ilist=ilist+retrieveCookie('JShopReferrer',0,0)+'^';
        ilist=changeUnwanted(ilist);
        ilist=escape(ilist);
        ilist=changePercent(ilist);
        if (top.name != '') {
             window.open('baskbuy.asp',ilist);
        } else {
            top.name=ilist;
            top.location='baskbuy.asp';
        }
    }

function changeUnwanted(tstring) {
    nstring='';
    for (var i=0; i <= tstring.length; i++) {
        nchar=tstring.charAt(i);
        if (nchar=='*') { nchar='!&1'; }
        if (nchar=='+') { nchar='!&2'; }
        if (nchar=='@') { nchar='!&3'; }
        if (nchar=='.') { nchar='!&4'; }
        if (nchar=='/') { nchar='!&5'; }
        if (nchar=='-') { nchar='!&6'; }
        nstring=nstring+nchar;
    }
    return nstring;
}
function changePercent(tstring) {
    nstring='';
    for (var i=0; i <= tstring.length; i++) {
        nchar=tstring.charAt(i);
        if (nchar=='%') { nchar='_'; }
        nstring=nstring+nchar;
    }
    return nstring;
}

function popupImage(imagename,imagewidth,imageheight) {
   apopup=window.open(imagename,'jsppopupimage','toolbar=no,location=no,directories=no,status=no,scrollbars=no,resizable=yes,copyhistory=no,width='+(imagewidth+20)+',height='+(imageheight+30));
}
function showPrice(pricestring) {
    index = document.cookie.indexOf('TheCurrency');
    countbegin=(document.cookie.indexOf('=',index)+1);
    countend=document.cookie.indexOf(';',index);
    if(countend==-1) { countend=document.cookie.length; }
    theCurrency=document.cookie.substring(countbegin,countend);
    thisitem=0;
    itemstart=0;
    theprice=0;
    for (var i=0; i<pricestring.length; i++) {
       if (pricestring.substring(i,i+1)=='~') {
          thisitem++;
          if (thisitem==eval(theCurrency)) {
             theprice=pricestring.substring(itemstart,i);
          }
          itemstart=i+1;
       }
    }
    return presentValue(theprice,currencyDecimals[theCurrency],currencyPre[theCurrency],currencyMiddle[theCurrency],currencyPost[theCurrency]);
}
function showPriceSingle(pricestring) {
    index = document.cookie.indexOf('TheCurrency');
    countbegin=(document.cookie.indexOf('=',index)+1);
    countend=document.cookie.indexOf(';',index);
    if(countend==-1) { countend=document.cookie.length; }
    theCurrency=document.cookie.substring(countbegin,countend);
    return presentValue(pricestring,currencyDecimals[theCurrency],currencyPre[theCurrency],currencyMiddle[theCurrency],currencyPost[theCurrency]);
}
function showPricePlain(pricestring) {
    index = document.cookie.indexOf('TheCurrency');
    countbegin=(document.cookie.indexOf('=',index)+1);
    countend=document.cookie.indexOf(';',index);
    if(countend==-1) { countend=document.cookie.length; }
    theCurrency=document.cookie.substring(countbegin,countend);
    return presentValue(pricestring,currencyDecimals[theCurrency],'',currencyMiddle[theCurrency],'');
}
function getPrice(pricestring) {
    index = document.cookie.indexOf('TheCurrency');
    countbegin=(document.cookie.indexOf('=',index)+1);
    countend=document.cookie.indexOf(';',index);
    if(countend==-1) { countend=document.cookie.length; }
    theCurrency=document.cookie.substring(countbegin,countend);
    thisitem=0;
    itemstart=0;
    theprice=0;
    for (var i=0; i<pricestring.length; i++) {
       if (pricestring.substring(i,i+1)=='~') {
          thisitem++;
          if (thisitem==eval(theCurrency)) {
             theprice=pricestring.substring(itemstart,i);
          }
          itemstart=i+1;
       }
    }
    return theprice;
}
    function presentValue(value,dp,pt,mt,at) {
        if(value<=0.9999) {
            newPounds='0';
        } else {
            newPounds=parseInt(value);
        }
        dec='1';
        for (var i=1; i<=dp;i++) {
            dec=dec+'0';
        }
        if (value>0) {
            newPence=Math.round((eval(value)+.000008 - newPounds)*(eval(dec)));
        } else {
            newPence=0;
        }
        compstring='9';
        for (var i=1; i <=dp-1;i++) {
            if (eval(newPence) <= eval(compstring)) newPence='0'+newPence;
            compstring=compstring+'9';
        }
        if (dp>0) {
            if (newPence==eval(dec)) { newPounds++; newPence=0; }
            newString=pt+newPounds+mt+newPence+at;
        } else {
            newString=pt+newPounds+at;
        }
        return (newString);
    }

function retrieveCookie(thecookie,thenumber,theadditional) {
    index = document.cookie.indexOf(thecookie);
    countbegin=(document.cookie.indexOf('=',index)+1);
    countend=document.cookie.indexOf(';',index);
    if(countend==-1) { countend=document.cookie.length; }
    fulllist=document.cookie.substring(countbegin,countend);
    if (thenumber==0) { return fulllist; }
    thisitem=0;
    newcookie='';
    itemstart=0;
    for(var i=0; i<fulllist.length;i++) {
        if (fulllist.substring(i,i+1)=='|') {
            thisitem++;
           itemvalue=fulllist.substring(itemstart,i);
           if (thisitem==thenumber) {
                if (theadditional==0) {
                    return itemvalue;
                } else {
                    itemstarttwo=0;
                    thisitemtwo=0;
                    for (var j=0; j<itemvalue.length; j++) {
                        if (itemvalue.substring(j,j+1)=='~') {
                            thisitemtwo++;
                            itemvaluetwo=itemvalue.substring(itemstarttwo,j);
                            if (thisitemtwo==theadditional) {
                                return itemvaluetwo;
                            }
                            itemstarttwo=j+1;
                        }
                    }
                }
            }
            itemstart=i+1;
        }
    }
    return '';
}
function addToCookie(thecookie,thevalue) {
    fulllist=retrieveCookie(thecookie,0,0);
    if (fulllist=='.') { fulllist=''; }
    fulllist=fulllist+thevalue+'|';
    document.cookie=thecookie+'='+fulllist+'; expires='+today;
}
function removeFromCookie(thecookie,thenumber) {
    fulllist=retrieveCookie(thecookie,0,0);
    thisitem=0;
    newcookie='';
    itemstart=0;
    for(var i=0; i<fulllist.length;i++) {
        if (fulllist.substring(i,i+1)=='|') {
            thisitem++;
            itemvalue=fulllist.substring(itemstart,i);
            if (thisitem!=thenumber) {
                newcookie=newcookie+itemvalue+'|';
            }
            itemstart=i+1;
        }
    }
    if (newcookie=='') { newcookie='.'; }
    document.cookie=thecookie+'='+newcookie+'; expires='+today;
}
function changeCookieValue(thecookie,thenumber,thevalue) {
    fulllist=retrieveCookie(thecookie,0,0);
    thisitem=0;
    newcookie='';
    itemstart=0;
    for(var i=0; i<fulllist.length;i++) {
        if (fulllist.substring(i,i+1)=='|') {
            thisitem++;
            itemvalue=fulllist.substring(itemstart,i);
            if (thisitem==thenumber) {
                itemvalue=thevalue;
            }
            newcookie=newcookie+itemvalue+'|';
            itemstart=i+1;
        }
    }
    document.cookie=thecookie+'='+newcookie+'; expires='+today;
}
function retrieveCookieCount(thecookie) {
   fulllist=retrieveCookie(thecookie,0,0);
   thisitem=0; newcookie=''; itemstart=0;
   for(var i=0; i<fulllist.length;i++) {
    if (fulllist.substring(i,i+1)=='|') {
        thisitem++;
        itemstart=i+1;
    }
   }
   return thisitem;
}
function showCurrency() {   index = document.cookie.indexOf('TheCurrency');   countbegin=(document.cookie.indexOf('=',index)+1);   countend=document.cookie.indexOf(';',index);   if(countend==-1) { countend=document.cookie.length; }   theCurrency=document.cookie.substring(countbegin,countend);   document.basketform.selectcurrency.selectedIndex=theCurrency-1;}function checkAdvanced(adv,advField0,advField1) {
     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;
            advPrice=adv.substring(advstart,advend);
            advlist++; applicable=true;
            if (field0!='none' && advField0 != field0) applicable=false;
            if (field1!='none' && advField1 != field1) applicable=false;
            if (applicable==true) {
                newAdv = newAdv+ '{'+field0+'!'+field1+'!'+advFrom+'!'+advTo+'!'+advPrice+'}'
            }
        } else if (adv.substring(j,j+1)=='!') {
            if (thisadv==1) field0 = adv.substring(advstart, j);
            if (thisadv==2) field1 = adv.substring(advstart, j);
            if (thisadv== 3) advFrom=adv.substring(advstart,j);
            if (thisadv== 4) advTo=adv.substring(advstart,j);
            thisadv++;advstart=j+1;
        }
    }
    if (newAdv=='') newAdv='none';
    return newAdv;
}
    function buyItem(newCode,newItem,newPrice,newTaxable,newWeight,newField0,newField1,newAP,newQuantity) {
        if(newQuantity<=0) {
            rc = alert('The quantity entered is incorrect');
        } else {
            if (confirm('Add '+newQuantity+' x '+newItem+' to basket? ')) {
                 if (newAP!='none') {
                     newAP=checkAdvanced(newAP,newField0,newField1);
                 }
                 amended = false;
                 newItemList=''; itemlist=0;
                 for (var i=1;i<=retrieveCookieCount('MFBasketCode');i++) {
                      theCode=retrieveCookie('MFBasketCode',i,0);
                      theItem=retrieveCookie('MFBasketName',i,0);
                      theQuantity=retrieveCookie('MFBasketQuantity',i,0);
                              theField0 = retrieveCookie('MFBasketExtraFields',i,1);
                              theField1 = retrieveCookie('MFBasketExtraFields',i,2);
                      if (theCode==newCode && theItem==newItem  && theField0 == newField0 && theField1 == newField1) {
                           amended=true;
                           tempquantity=eval(theQuantity)+eval(newQuantity);
                           changeCookieValue('MFBasketQuantity',i,tempquantity);
                      }
                 }
                 if (amended==false) {
                    addToCookie('MFBasketCode',newCode);
                    addToCookie('MFBasketName',newItem);
                    addToCookie('MFBasketPrice',newPrice);
                    addToCookie('MFBasketWeight',newWeight);
                    addToCookie('MFBasketTaxable',newTaxable);
                    addToCookie('MFBasketExtraFields',newField0+'~'+newField1+'~');
                    addToCookie('MFBasketAdvanced',newAP);
                    addToCookie('MFBasketQuantity',newQuantity);
                 }
             }
         }
     }

function flipImage(imgname,imgsrc) {
    document.images[imgname].src=imgsrc;
}
function showBasket() {
   sepchar='"';
   totprice=0;
   document.writeln('<table class=\"BasketTable\" cellspacing=\"0\" cellpadding=\"0\">');
   document.writeln('<tr>');
   document.writeln("<td class=\"BasketTitle\">Name</td>");
   /*document.writeln("<td class=\"BasketTitle\"></td>");*/
   /*document.writeln("<td class=\"BasketTitle\"></td>");*/
   document.writeln("<td class=\"BasketTitle\" align=\"center\">Qty</td>");
   document.writeln("<td class=\"BasketTitle\" align=\"right\">Price</td>");
   document.writeln("<td class=\"BasketTitle\" align=\"right\">Cost</td>");
   document.writeln("<td class=\"BasketTitle\" align=\"right\">Action</td>");
   document.writeln("</tr>");
   for (var i=1;i<=retrieveCookieCount('MFBasketCode');i++) {
       theCode=retrieveCookie('MFBasketCode',i,0);
       theName=retrieveCookie('MFBasketName',i,0);
       theWeight=retrieveCookie('MFBasketWeight',i,0);
       theTaxable=retrieveCookie('MFBasketTaxable',i,0);
       thePrice=retrieveCookie('MFBasketPrice',i,0);
       theField1=retrieveCookie('MFBasketExtraFields',i,1);
       theField2=retrieveCookie('MFBasketExtraFields',i,2);
       theAP=retrieveCookie('MFBasketAdvanced',i,0);
       theQuantity=retrieveCookie('MFBasketQuantity',i,0);
       if (theAP!='none') {
           tempprice=getAdvanced(theAP,theQuantity);
           if (tempprice>'') { thePrice=tempprice; }
       }
       //itemtotal=eval(theQuantity)*eval(getPrice(thePrice));
       itemtotal=eval(theQuantity)*eval(thePrice);
       totprice=totprice+itemtotal;
       document.writeln("<td class=\"BasketRow\">"+theName+"</td>");
       /*document.writeln("<td class=\"BasketRow\">"+theField1+"</td>");*/
       /*document.writeln("<td class=\"BasketRow\">"+theField2+"</td>");*/
       document.writeln("<td class=\"BasketRow\" align=\"center\">"+theQuantity+"</td>");
       document.writeln("<td class=\"BasketRow\" align=\"right\">&pound;"+showPricePlain(thePrice)+"</td>");
       document.writeln("<td class=\"BasketRow\" align=\"right\">&pound;"+showPricePlain(itemtotal)+"</td>");
       document.writeln("<td class=\"BasketRow\" align=\"right\"><input type=button name='add"+i+"' onClick='removeItem("+i+");' value='Remove' class=\"Button\"></td>");
       document.writeln("</tr>");
	   document.writeln("<tr>");
	   document.writeln("<td colspan=3 class=\"BasketBreak\">&nbsp;</td>");
	   document.writeln("</tr>");
   }
   document.writeln("<tr>");
   document.writeln("<td class=\"BasketFooter\" colspan=\"3\">Total</td>");
   document.writeln("<td class=\"BasketFooter\" align=\"right\">&pound;"+showPricePlain(totprice)+"</td>");
   document.writeln("<td class=\"BasketFooter\">&nbsp;</td>");
   /*document.writeln("</tr>");*/
   document.writeln("</table>");
   /*document.writeln("<input type=button name='clearbasket' onClick='clearBasket();' class=""BasketButton"" value='Empty Basket'>&nbsp;");*/
   /*document.writeln("<input type=button name='updatebasket' onClick='updateBasket();' class=""BasketButton"" value='Update Basket'>");*/
}
function showBasket2() {
   sepchar='"';
   totprice=0;
   document.writeln('<table class=\"BasketTable\" cellspacing=\"0\" cellpadding=\"0\">');
   document.writeln('<tr>');
   document.writeln("<td class=\"BasketTitle\">Name</td>");
   /*document.writeln("<td class=\"BasketTitle\"></td>");*/
   /*document.writeln("<td class=\"BasketTitle\"></td>");*/
   document.writeln("<td class=\"BasketTitle\" align=\"center\">Qty</td>");
   document.writeln("<td class=\"BasketTitle\" align=\"right\">Price</td>");
   document.writeln("<td class=\"BasketTitle\" align=\"right\">Cost</td>");
   /*document.writeln("<td class=\"BasketTitle\" align=\"right\">Action</td>");*/
   document.writeln("</tr>");
   for (var i=1;i<=retrieveCookieCount('MFBasketCode');i++) {
       theCode=retrieveCookie('MFBasketCode',i,0);
       theName=retrieveCookie('MFBasketName',i,0);
       theWeight=retrieveCookie('MFBasketWeight',i,0);
       theTaxable=retrieveCookie('MFBasketTaxable',i,0);
       thePrice=retrieveCookie('MFBasketPrice',i,0);
       theField1=retrieveCookie('MFBasketExtraFields',i,1);
       theField2=retrieveCookie('MFBasketExtraFields',i,2);
       theAP=retrieveCookie('MFBasketAdvanced',i,0);
       theQuantity=retrieveCookie('MFBasketQuantity',i,0);
       if (theAP!='none') {
           tempprice=getAdvanced(theAP,theQuantity);
           if (tempprice>'') { thePrice=tempprice; }
       }
       //itemtotal=eval(theQuantity)*eval(getPrice(thePrice));
       itemtotal=eval(theQuantity)*eval(thePrice);
       totprice=totprice+itemtotal;
       document.writeln("<td class=\"BasketRow\">"+theName+"</td>");
       /*document.writeln("<td class=\"BasketRow\">"+theField1+"</td>");*/
       /*document.writeln("<td class=\"BasketRow\">"+theField2+"</td>");*/
       document.writeln("<td class=\"BasketRow\" align=\"center\">"+theQuantity+"</td>");
       document.writeln("<td class=\"BasketRow\" align=\"right\">&pound;"+showPricePlain(thePrice)+"</td>");
       document.writeln("<td class=\"BasketRow\" align=\"right\">&pound;"+showPricePlain(itemtotal)+"</td>");
       /*document.writeln("<td class=\"BasketRow\" align=\"right\"><input type=button name='add"+i+"' onClick='removeItem("+i+");' value='Remove' class=\"Button\"></td>");*/
       document.writeln("</tr>");
	   document.writeln("<tr>");
	   document.writeln("<td colspan=3 class=\"BasketBreak\">&nbsp;</td>");
	   document.writeln("</tr>");
   }
   document.writeln("<tr>");
   document.writeln("<td class=\"BasketFooter\" colspan=\"3\">Goods Total</td>");
   document.writeln("<td class=\"BasketFooterPrice\" align=\"right\">&pound;"+showPricePlain(totprice)+"</td>");
   /*document.writeln("<td class=\"BasketFooter\">&nbsp;</td>");*/
   document.writeln("</tr>");
   if (intPostage>0){
   document.writeln("<tr>");
   document.writeln("<td class=\"BasketFooter\" colspan=3>Delivery Charge</td>");
   document.writeln("<td class=\"BasketFooterPrice\" align=\"right\"><input type=hidden name='Postage_Total' value='"+showPricePlain(intPostage)+"'>&pound;"+showPricePlain(intPostage)+"</td>");
   document.writeln("</tr>");
   }
   if (vat>0){
   document.writeln("<tr>");
   document.writeln("<td class=\"BasketFooter\" colspan=3>VAT</td>");
   document.writeln("<td class=\"BasketFooterPrice\" align=\"right\"><input type=hidden name='VAT_Total' value='"+showPricePlain((totprice+intPostage)*(vat/100))+"'>&pound;"+showPricePlain((totprice+intPostage)*(vat/100))+"</td>");
   document.writeln("</tr>");
   }
   document.writeln("<tr>");
   document.writeln("<td class=\"BasketFooter\" colspan=\"3\">Total</td>");
   document.writeln("<td class=\"BasketFooterPrice\" align=\"right\">&pound;"+showPricePlain((totprice+intPostage)*((vat/100)+1))+"</td>");
   /*document.writeln("<td class=\"BasketFooter\">&nbsp;</td>");*/
   document.writeln("</tr>");
   document.writeln("</table>");
   /*document.writeln("<input type=button name='clearbasket' onClick='clearBasket();' class=""BasketButton"" value='Empty Basket'>&nbsp;");*/
   /*document.writeln("<input type=button name='updatebasket' onClick='updateBasket();' class=""BasketButton"" value='Update Basket'>");*/
}
    function removeItem(itemno) {
        removeFromCookie('MFBasketCode',itemno);
        removeFromCookie('MFBasketName',itemno);
        removeFromCookie('MFBasketWeight',itemno);
        removeFromCookie('MFBasketTaxable',itemno);
        removeFromCookie('MFBasketPrice',itemno);
        removeFromCookie('MFBasketExtraFields',itemno);
        removeFromCookie('MFBasketAdvanced',itemno);
        removeFromCookie('MFBasketQuantity',itemno);
        self.location = 'basket.aspx';
    }

    function clearBasket() {
        if (confirm('Are you sure you wish to clear the basket?')) {
            index=document.cookie.indexOf('MFBasket');
            document.cookie='MFBasketCode=.; expires='+today;
            document.cookie='MFBasketName=.; expires='+today;
            document.cookie='MFBasketWeight=.; expires='+today;
            document.cookie='MFBasketTaxable=.; expires='+today;
            document.cookie='MFBasketPrice=.; expires='+today;
            document.cookie='MFBasketExtraFields=.; expires='+today;
            document.cookie='MFBasketAdvanced=.; expires='+today;
            document.cookie='MFBasketQuantity=.; expires='+today;
            self.location='basket.aspx';
        }
    }
    function changeCurrency(num) {
         index=document.cookie.indexOf('TheCurrency');
         document.cookie='TheCurrency='+(num+1)+'; expires=Friday, 31-Dec-2010 08:00:00 GMT';
         self.location='';
    }

    function updateBasket() {
        newItemList='';
        itemlist=0;
        for (var i=1;i<=retrieveCookieCount('MFBasketCode');i++) {
            if (!isNaN(document.basketform.elements['quant'+i].value)) {
                changeCookieValue('MFBasketQuantity',i,eval(document.basketform.elements['quant'+i].value));
            }
        }
        self.location='basket.aspx';
    }
