var buyvalidation="";
var buyform_ok=true;

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]='AUD'; currencyName[1]='Australia Dollars'; currencyDecimals[1]='2'; currencyPre[1]='$AU '; currencyMiddle[1]='.'; currencyPost[1]='';

today=new Date();
today.setTime(today.getTime() + (3600000));
today=today.toGMTString();
currentdate=new Date();
currentdate=currentdate.toLocaleString();
ilist=top.name;
tstring=ilist;
nstring='';
for (var i=0; i <= tstring.length; i++) {
  nchar=tstring.charAt(i);
  if (nchar=='_') { nchar='%'; }
  nstring=nstring+nchar;
}

ilist=nstring;
ilist=unescape(ilist);
tstring=ilist;
nstring='';
for (var i=0; i <= tstring.length; i++) {
  nchar=tstring.charAt(i);
  if (i<tstring.length-1) {
    nchar2=nchar+tstring.charAt(i+1)+tstring.charAt(i+2);
    if (nchar2=='!&1') { nchar='*'; i++;i++;}
    if (nchar2=='!&2') { nchar='+'; i++;i++; }
    if (nchar2=='!&3') { nchar='@'; i++;i++; }
    if (nchar2=='!&4') { nchar='.'; i++;i++; }
    if (nchar2=='!&5') { nchar='/'; i++;i++; }
    if (nchar2=='!&6') { nchar='-'; i++;i++; }
    if (nchar2=='!-1') { nchar='*'; i++;i++;}
    if (nchar2=='!-2') { nchar='+'; i++;i++; }
    if (nchar2=='!-3') { nchar='@'; i++;i++; }
    if (nchar2=='!-4') { nchar='.'; i++;i++; }
    if (nchar2=='!-5') { nchar='/'; i++;i++; }
    if (nchar2=='!-6') { nchar='-'; i++;i++; }
  }
  nstring=nstring+nchar;
}

ilist=nstring;
thearray=ilist.split('^');
document.cookie='APTBasketCode='+thearray[0]+'; expires='+today;
document.cookie='APTBasketName='+thearray[1]+'; expires='+today;
document.cookie='APTBasketWeight='+thearray[2]+'; expires='+today;
document.cookie='APTBasketTaxable='+thearray[3]+'; expires='+today;
document.cookie='APTBasketPrice='+thearray[4]+'; expires='+today;
document.cookie='APTBasketExtraFields='+thearray[5]+'; expires='+today;
document.cookie='APTBasketAdvanced='+thearray[6]+'; expires='+today;
document.cookie='APTBasketQuantity='+thearray[7]+'; expires='+today;
document.cookie='TheCurrency='+thearray[8]+'; expires='+today;
document.cookie='JShopReferrer='+thearray[9]+'; expires='+today;

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 recalculateTax() {
        foundloc = false;
        tottax = 0.00;
        if ((taxfield.toLowerCase() == 'australia') && (taxfield2.toLowerCase() == '')) {
            tottax = (taxablestandard+shippingtotal)*(10/100);
            tottax = tottax + (taxablesecond)*(0/100);
            foundloc = true;
        }
                else {
                  tottax = 0.00
                }
        if (taxfield2.toLowerCase() == 'australia') {
            tottax = (taxablestandard+shippingtotal)*(10/100);
            tottax = tottax + (taxablesecond)*(0/100);
            foundloc = true;
        }
        document.basketform.amt.value = showPricePlain(tottax+totprice+shippingtotal);
        document.basketform.TotalPrice.value = showPriceSingle(tottax+totprice+shippingtotal);
        document.basketform.GSTIncluded.value = showPriceSingle(tottax);
        singletax=""+"Tax"+showPriceSingle(tottax)+"";
        document.basketform.items.value=singlefield+singletax+singleshipping;
            if (taxfield2.toLowerCase() != '') {
            rc=alert('You have chosen a different delivery location.  Please ensure all delivery information is entered.');
        }
    }

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 getAdvanced(adv,qty) {
     newAdv='';advlist=0;applicable=true;advnewprice='';
    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 (applicable==true) {
                if (advFrom > 0 && advFrom > eval(qty)) applicable=false;
                if (advTo > 0 && advTo < eval(qty)) applicable=false;
                if (applicable==true) advnewprice=advPrice;
            }
        } else if (adv.substring(j,j+1)=='!') {
            if (thisadv== 1) advFrom=adv.substring(advstart,j);
            if (thisadv== 2) advTo=adv.substring(advstart,j);
            thisadv++;advstart=j+1;
        }
    }
    return advnewprice;
}

alltheitems=0;taxfield='';singletax='';singleshipping='';taxfield2='';shippingfile='';primaryfield='';secondaryfield='';singlefield='';

function showBasket() {
   totprice=0;shippingtotal=0;taxtotalone=0;taxtotaltwo=0;ordertotal=0;taxablestandard=0;taxablesecond=0;taxable=0;totalforshipping=0;
   document.writeln('<table width=95% border=1 cellpadding=0 cellspacing=0>');
   document.writeln('<tr>');
   document.writeln("<td class=MainBodyBold>Description</td>");
   document.writeln("<td class=MainBodyBold align=center>Qty</td>");
   document.writeln("<td class=MainBodyBold align=center>Cost<br>Each</td>");
   document.writeln("<td class=MainBodyBold align=center>Total Cost</td>");
   document.writeln("</tr>");
   for (var i=1;i<=retrieveCookieCount('APTBasketCode');i++) {
       theCode=retrieveCookie('APTBasketCode',i,0);
       theName=retrieveCookie('APTBasketName',i,0);
       theWeight=retrieveCookie('APTBasketWeight',i,0);
       theTaxable=retrieveCookie('APTBasketTaxable',i,0);
       thePrice=retrieveCookie('APTBasketPrice',i,0);
       theAP=retrieveCookie('APTBasketAdvanced',i,0);
       theQuantity=retrieveCookie('APTBasketQuantity',i,0);
       if (theAP!='none') {
           tempprice=getAdvanced(theAP,theQuantity);
           if (tempprice>'') { thePrice=tempprice; }
       }
       itemtotal=eval(theQuantity)*eval(getPrice(thePrice));
       totprice=totprice+itemtotal;
       if (theTaxable==0) taxablestandard=taxablestandard+itemtotal;
       if (theTaxable==1) taxablesecond=taxablesecond+itemtotal;
       totalforshipping=totalforshipping+eval(theQuantity);
       document.writeln("<td class=MainBody>"+theName+"</td>");
       document.writeln("<td class=MainBody align=center>"+theQuantity+"</td>");
       document.writeln("<td class=MainBody align=right>"+showPrice(thePrice)+"</td>");
       singlefield=singlefield+""+theCode+""+theName+""+showPriceSingle(getPrice(thePrice))+"";
       document.writeln("<td class=MainBody align=right>"+showPriceSingle(itemtotal)+"</td>");
       document.writeln("</tr>");
   }
   alltheitems=i-1;
   document.writeln("<tr>");
   document.writeln("<td class=MainBody colspan=3><input type=hidden name='items' value='"+singlefield+"'>Total Goods</td>");
   document.writeln("<td class=MainBody align=right>"+showPriceSingle(totprice)+"<input type=hidden name='GoodsTotal' value='"+showPriceSingle(totprice)+"'></td>");
   document.writeln("</tr>");
   document.writeln("<tr>");
   document.writeln("<td class=MainBody colspan=3>GST (only Australia)</td>");
   document.writeln("<td class=MainBody align=right><input type=text size=14 name=GSTIncluded value="+showPriceSingle(0)+" class='InputBox' onfocus='this.blur()'></td>");
   document.writeln("</tr>");
   document.writeln("<tr>");
   document.writeln("<td class=MainBodyBold colspan=3>Order Total</td>");
   document.writeln("<td class=MainBodyBold align=right><input type=text size=14 name=TotalPrice value="+showPriceSingle(totprice)+" class='InputBox' onfocus='this.blur()'><input type=hidden name='amt' value='"+showPricePlain(totprice)+"'></td>");
   document.writeln("</tr>");
   document.writeln("</table>");

}

function flipImage(imgname,imgsrc) {
    document.images[imgname].src=imgsrc;
}

function checkPurchaseFields() {

var bg_error   = "#99CC00";
var bg_ok      = "#FFFFFF";

    if (document.basketform.Name.value=='') {
        buyvalidation += "Please enter your name\n";
                setColor(document.basketform.Name, bg_error);
                buyform_ok = false;
    }
        else {
            setColor(document.basketform.Name, bg_ok);
        }
        if (document.basketform.Address1.value=='') {
        buyvalidation += "Please enter your address\n";
                setColor(document.basketform.Address1, bg_error);
                buyform_ok = false;
    }
        else {
            setColor(document.basketform.Address1, bg_ok);
        }
        if (document.basketform.City.value=='') {
        buyvalidation += "Please enter your city\n";
                setColor(document.basketform.City, bg_error);
                buyform_ok = false;
    }
        else {
            setColor(document.basketform.City, bg_ok);
        }
        if (document.basketform.Country.options[document.basketform.Country.selectedIndex].text=='') {
        buyvalidation += "Please enter your country\n";
                setColor(document.basketform.Country, bg_error);
                buyform_ok = false;
    }
        else {
            setColor(document.basketform.Country, bg_ok);
        }
    if (document.basketform.Phone1.value=='') {
        buyvalidation += "Please enter your telephone number\n";
                setColor(document.basketform.Phone1, bg_error);
                buyform_ok = false;
    }
    else {
            setColor(document.basketform.Phone1, bg_ok);
        }
    if ((document.basketform.email.value.indexOf('@')==-1) || (document.basketform.email.value.indexOf('.')==-1) || (document.basketform.email.value.indexOf('.') == document.basketform.email.value.length - 1)) {
              buyvalidation += "Please enter a valid email address\n";
                  setColor(document.basketform.email, bg_error);
                  buyform_ok = false;
                }
                else {
              setColor(document.basketform.email, bg_ok);
            }
        if (buyvalidation != '') {
          rc=alert(buyvalidation);
           buyvalidation='';
        }
        else {
          document.basketform.submit();
        }
}
