// Javascript functions

    function goBuy() {

        ilist='';
        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);
        for(var i=0; i<= fulllist.length;i++) {
            if(fulllist.substring(i,i+1)=='[') {
                itemstart=i;
            } else if (fulllist.substring(i,i+1)==']') {
                itemend=i+1;
                ilist=ilist+fulllist.substring(itemstart,itemend);
            }
        }
        ilist=changeSpaces(ilist);
        window.open('https://www.secure-server-hosting.com/secutran/secureforms/sh209651/buy.htm?items='+ilist,"",
		"height=500,width=700,scrollbars,left=200,top=100");
       }

    function changeSpaces(tstring) {
        nstring='';
        for (var i=0; i <= tstring.length; i++) {
            if (tstring.charAt(i)==' ') { nstring=nstring+'^';
            } else { nstring=nstring+tstring.charAt(i); }
        }
        return nstring;
    }

function checkAdvanced(adv,advField1,advField2) {
     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;
            advPrice2=adv.substring(advstart,advend);
            advlist++; applicable=true;
            if (field1!='none' && advField1 != field1) applicable=false;
            if (field2!='none' && advField2 != field2) applicable=false;
            if (applicable==true) {
                newAdv = newAdv+ '{'+field1+'~'+field2+'~'+advFrom+'~'+advTo+'~'+advPrice+'~'+advPrice2+'}'
            }
        } else if (adv.substring(j,j+1)=='~') {
            if (thisadv==1) field1 = adv.substring(advstart, j);
            if (thisadv==2) field2 = adv.substring(advstart, j);
            if (thisadv== 3) advFrom=adv.substring(advstart,j);
            if (thisadv== 4) advTo=adv.substring(advstart,j);
            if (thisadv== 5) advPrice=adv.substring(advstart,j);
            if (thisadv== 6) advPrice2=adv.substring(advstart,j);
            thisadv++;advstart=j+1;
        }
    }
    if (newAdv=='') newAdv='none';
    return newAdv;
}
    function buyItem(newItem,newPrice,newTaxable,newWeight,newField1,newField2,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,newField1,newField2);
                 }
                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);
                 amended = false;
                 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;
                         thequantity=fulllist.substring(itemstart,itemend);
                         itemlist++;
                         if (theItem==newItem  && theField1 == newField1 && theField2 == newField2) {
                             amended=true;
                             tempquantity=eval(thequantity)+eval(newQuantity);
                             newItemList=newItemList+'['+theItem+'|'+thePrice+'|'+theTaxable+'|'+theWeight+'|'+theField1+'|'+theField2+'|'+newAP+'|'+tempquantity+']';
                         } else {
                             newItemList=newItemList+'['+theItem+'|'+thePrice+'|'+theTaxable+'|'+theWeight+'|'+theField1+'|'+theField2+'|'+theAP+'|'+thequantity+']';
                         }
                     } 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) theWeight=fulllist.substring(itemstart,i);
                         if (thisitem== 5) theField1=fulllist.substring(itemstart,i);
                         if (thisitem== 6) theField2=fulllist.substring(itemstart,i);
                         if (thisitem== 7) theAP=fulllist.substring(itemstart,i);
                         thisitem++;itemstart=i+1;
                     }
                 }
                 if (amended==false) {
                     newItemList=newItemList+'['+newItem+'|'+newPrice+'|'+newTaxable+'|'+newWeight+'|'+newField1+'|'+newField2+'|'+newAP+'|'+newQuantity+']'; }
                 index = document.cookie.indexOf('TheBasket');
                 document.cookie='TheBasket='+newItemList+'; expires=Friday, 31-Dec-2010 08:00:00 GMT; path=/';
            }
        }
    }

