<!--//--><![CDATA[//><!--

grossprice=true;
deliverytype = 'inc';
deliveryamount = 0;
deliverytax = 17.5;


deliveryrecs= new Array();
deliveryrecitem = new deliveryrec( 1 , 'Item Delivery', 'peritem' ); 


deliverydetailitem = new deliverydetail( 'UK Std' , 'United Kingdom' , 0 , 51.75 , 1 );
deliveryrecitem.details.push( deliverydetailitem );
	

deliveryrecs.push( deliveryrecitem );


deliveryrecitem = new deliveryrec( 2 , 'Total Delivery', 'totqty' ); 

deliverydetailitem = new deliverydetail( 'UK Std' , 'United Kingdom' , 1 , 51.75 , 1 );
deliveryrecitem.details.push( deliverydetailitem );

deliveryrecs.push( deliveryrecitem );


deliveryrecitem = new deliveryrec( 3 , 'Total Delivery', 'totqty' ); 


deliverydetailitem = new deliverydetail( 'UK Std' , 'United Kingdom' , 0 , 7.50 , 1 );
deliveryrecitem.details.push( deliverydetailitem );
	

deliveryrecs.push( deliveryrecitem );

deliverylocation = 'United Kingdom';



cart = new cart ();

function deliveryrec ( id , title,dtype ) {
	this.id = id;
	this.title = title;
	this.dtype = dtype;
	this.totqty = 0;
	this.totunt = 0;
	this.totalamount =0;
	this.details = new Array();	

}

function updatedelivery(id , qty ,unt ) {
	for (idx in deliveryrecs) {
		if (deliveryrecs[idx].id==id) {
			deliveryrecs[idx].totqty = eval(deliveryrecs[idx].totqty)+eval(qty);
			deliveryrecs[idx].totunt = eval(deliveryrecs[idx].totunt)+eval(unt);
		}
	}
}
function resetdelivery( ) {
	for (idx in deliveryrecs) {
    	deliveryrecs[idx].totqty = 0;
		deliveryrecs[idx].totunt = 0;
		deliveryrecs[idx].totalamount = 0;			
	}
}

function deliverydetail ( description , location, cutin, amount, taxcode ) {
	this.description = description;
	this.location = location;
	this.cutin = cutin;
	this.amount = amount;
	this.taxcode = taxcode;
}

//=========================================================================================
// OBJECTS
//=========================================================================================


function cart() {

        this.items = new Array();
        this.nettotal=0;
        this.vat=0;
        this.grosstotal=0;

}


function totalcart() {
        cart.nettotal=0;
        cart.vat=0;
        cart.delivery=0;
        cart.grosstotal=0;
		deliveryamount=0;
		resetdelivery();
				
        for (idx in cart.items)
        {
                thisitem = cart.items[idx];
                cart.vat = cart.vat + thisitem.taxamount;
//              cart.delivery = cart.delivery + thisitem.idel;
               if (grossprice) {
                    netp = thisitem.amount-thisitem.taxamount;
                    cart.nettotal = cart.nettotal + netp ;
                } else {
                        cart.nettotal = cart.nettotal + (thisitem.amount);                              
                }

				updatedelivery(thisitem.deliveryid, thisitem.qty ,thisitem.idel );

				cart.delivery = calcdelivery( thisitem.deliveryid ,deliverylocation, thisitem.amount, thisitem.idel, thisitem.qty );
			


        }

        cart.grosstotal = (cart.nettotal + cart.vat + cart.delivery);
        writecarttocookie();
        
}

function item(pid, code, description,price,qty,itax,idel,deliveryid ) {

    description = cleanstr(description);
        this.pid = pid;
        this.code = code;
        this.description=description;
        this.ioptions = new Array();
        this.price=price;

	if (!grossprice) {
	   this.netprice = this.price;
	} else {
	   this.netprice = this.price-( tax(this.price,itax,grossprice) );
	}

        this.qty = qty;
        this.amount = eval(price*qty);
        this.itax=itax;
        this.idel=idel;
        this.deliveryid=deliveryid;
        this.taxamount = tax(this.amount,itax,grossprice);

	this.netamount = eval(this.netprice*qty);

}


function additem(cart, pid , code, description,price,qty, itax,idel,deliveryid) {

        sitem =  new item(pid ,code, description,price,qty,itax,idel,deliveryid );
        cart.items.push( sitem );        
//      return(cart.items[idx])

}

function removeitem(idx) {
        cart.items.splice(idx,1);
//              parent.cartwin.load('inline', showcart() , 'Shopping cart' );
        
}

function removeitembycode(icode) {

  for ( idx=cart.items.length-1;idx>=0;idx--) {
                if (cart.items[idx].code==icode) {
                        cart.items.splice(idx,1);
                }
        }

		if (cart.nettotal==0) {
			deliveryamount=0;
		}
//      parent.cartwin.load('inline', showcart() , 'Shopping cart' );
}

function removeitembypid(ipid) {

  for ( idx=cart.items.length-1;idx>=0;idx--) {
                if (cart.items[idx].pid==ipid) {
                        cart.items.splice(idx,1);
                }
        }
		if (cart.nettotal==0) {
			deliveryamount=0;
		}		
//      parent.cartwin.load('inline', showcart() , 'Shopping cart' );
}

function clearcart() {
        for ( idx=cart.items.length-1;idx>=0;idx--) {
                        cart.items.splice(idx,1);
        }
}


function option(code,description,price,qty,itax,idel) {
        this.code = code;
        this.description  = description;
        this.price = price;
        this.itax=itax;
        this.idel=idel;
        this.taxamount = tax(price,itax,grossprice);
}


function addoption(item,code,description,price,itax,idel) {
        soption =  new option(code,description,price,qty,itax,idel);
        item.ioptions.push(soption);
//      return (item.options[idx]);

}



//======================================================================================
//  CALCULATIONS
//======================================================================================


function tax(amount,itax,grossprice ) {

        if (grossprice==true) {
                // tax included
                taxamount =  eval(amount) - ( eval(amount) / ( 1+(itax/100) ) ) ;   

        } else {
                // need to add tax
            taxamount = ((eval(amount)/100)*itax );   
        }

        taxamount = (Math.round( ((taxamount*100)) ))/100;

        return(taxamount);
}


function calcdelivery(deliveryid,deliverylocation,value, idel, qty ) {
	  // value can either be an amount, a quantity or a weight
	  // it compares with the cut in point so if the value is a quanity the cutin point should be quantity as well
	  theresult = 0;
	  testvalue= value;

      for (idx in deliveryrecs)
      {
         if ( deliveryrecs[idx].id == deliveryid ) {		 

                for ( ddx in deliveryrecs[idx].details ) {
                  if ( deliveryrecs[idx].details[ddx].location==deliverylocation ) {			  		

				    switch ( deliveryrecs[idx].dtype) {
					  case 'totqty': testvalue = deliveryrecs[idx].totqty;break;
					  case 'totunt': testvalue = deliveryrecs[idx].totunt;break;
					  case 'amt': testvalue = value;break;
					  case 'peritem': testvalue = value;break;
					}

                    if ( deliveryrecs[idx].details[ddx].cutin <= testvalue ) {
						 if ( deliveryrecs[idx].dtype=='peritem' ) {
                           deliveryrecs[idx].totalamount =  deliveryrecs[idx].totalamount + ( eval( deliveryrecs[idx].details[ddx].amount ) * qty  );
						 } else {
                           deliveryrecs[idx].totalamount =  eval( deliveryrecs[idx].details[ddx].amount );
						 }
                         if (deliveryrecs[idx].details[ddx].taxcode == 0) {
                              novat=true;
                         } else {
                              deltax = tax ( deliveryrecs[idx].details[ddx].amount , deliveryrecs[idx].details[ddx].taxcode , 0 );
                         }
                     }  // if is less than cutin
                   }  // if in same area
                }
         }
      }
	  
      for (idx in deliveryrecs)
      {
	  	theresult =  theresult + eval(deliveryrecs[idx].totalamount);
	  }	  

      return (theresult);
}



function discount() {



}

//======================================================================================
// CART STORAGE
//======================================================================================


function writecarttocookie() {
  var cookiecart='';

  for (idx in cart.items)
  {
    prdpid = cart.items[idx].pid;
    prdcde = cart.items[idx].code;
    prddsc = escape( cart.items[idx].description);       // product description
    prdpri = cart.items[idx].price;       // product price
    prdqty = cart.items[idx].qty;       // product quantity
    prditax = cart.items[idx].itax;
    prdidel = cart.items[idx].idel;
    prddeliveryid = cart.items[idx].deliveryid;

    if (prdcde>' ') {
       cookiecart=cookiecart+prdpid+'~'+prdcde+'~'+prddsc+'~'+prdpri+'~'+prdqty+'~'+prditax+'~'+prdidel+'~'+prddeliveryid+'~|';
    }

  }


  writeCookie('cart', escape(cookiecart),24);


}


function getcartfromcookie() {

  cart.items.length=0;

   if (readCookie('cart')>' ') {
          cookiecart=unescape(readCookie('cart'));
   } else {
          cookiecart='';   
   }

  if (cookiecart.indexOf('~|')>0) {

          cookieitems=cookiecart.split('~|');
          for ( idx in cookieitems ) {
              idetail= cookieitems[idx].split('~');
              ipid = idetail[0];
              icode = idetail[1];               
              idescription = unescape(idetail[2]);
              iprice = idetail[3];
              iqty = idetail[4];
              itax=idetail[5];
              idel=idetail[6];
			  deliveryid=idetail[7];			  
              if (icode>' ') {
                    additem(cart, ipid, icode,idescription,iprice, iqty, itax,idel,deliveryid );
              }
          }

        }

//  totcalculate();
  
}

function readCookie(name)
{
  var cookieValue = "";
  var search = name + "=";

  if(document.cookie.length > 0)
  { 
    offset = document.cookie.indexOf(search);
    if (offset != -1)
    { 
      offset += search.length;
      end = document.cookie.indexOf(";", offset);
      if (end == -1) end = document.cookie.length;
      cookieValue = unescape(document.cookie.substring(offset, end))
    }
  }

  return cookieValue;
}

// Example:
// writeCookie("myCookie", "my name", 24);
// Stores the string "my name" in the cookie "myCookie" which expires after 24 hours.

function writeCookie(name, value, hours)
{
  var expires = new Date();
  var expire = "";
  var path = "/";
//  var domain="<#WEBADDRESS>";

  if(hours != null)
  {
    expires.setTime( expires.getTime() + (hours * 60 * 60)  ); // set expires
//    expires = new Date((new Date()).getTime() + (hours * 36000));
     expire = "; expires=" + expires.toGMTString();
  }
  document.cookie = name + "=" +value+expire+";path="+path;
//alert(document.cookie);
}

function DelCookie(sName) { 
  document.cookie = sName + "=" + escape(sValue) + "; expires=Fri, 31 Dec 1999 23:59:59 GMT;"; 
} 



//======================================================================================
// CART CONTROL
//======================================================================================
function formatnumber(fstr,ino) {
ino=ino+'';
fstr=fstr+'';
var ndecidx= ino.lastIndexOf('.');
if (ndecidx<0 ) {
   ino=ino+'.';
   ndecidx=ino.lastIndexOf('.');
}
var sdecidx= fstr.lastIndexOf('.');
if (sdecidx<0 ) {
   sdecidx=fstr.length+1;
}

inoafterdec= ino.length-ndecidx;
fstrafterdec = fstr.length-sdecidx;
if (inoafterdec>fstrafterdec) {
  rf = inoafterdec-fstrafterdec;
  ino = ino*(100*rf);
  ino= Math.round(ino);
  ino= ino / (100*rf);
}

padv=sdecidx-ndecidx;

if (padv>0) {
   pads='';
   for (idx=0;idx<padv;idx++) {
     if (fstr.substr(idx,1)=='#') { pads=pads+' '};
         if (fstr.substr(idx,1)=='0') { pads=pads+'0'};
   }
   ino=pads+ino;
   lfstr=fstr.length;
   lino=ino.length;
   if (lfstr>=lino) {
      pads='';
      for (idx=lino; idx<=lfstr;idx++) {
         if (fstr.substr(idx,1)=='#') { pads=pads+' '};
             if (fstr.substr(idx,1)=='0') { pads=pads+'0'};  
          }
          ino=ino+pads;
   } else {
   
   }
} else {
   
}

return(ino);
}
function stdf(ino) {
    ono= formatnumber('######.00',ino);
    return ( ono )
}

function refresh(ctype,template ) {
        if (ctype=='win') {
                cartwin.load('inline', showcart() , 'Shopping cart' );
        } else {
		if (ctype=='scr') {
	                totalcart();
        	        document.location = 'shop.php?action=cart';
		} else {
			if (ctype='ajax') {
				document.getElementById('ajax').innerHTML = showcart();
			}
		}
        }
}

function showcart(ctype,template) {
	totalcart();
    cartashtml = "";
    cartashtml = cartashtml+"<table id='scart'>";
    cartashtml = cartashtml +"<tr><th>No.</th><th>Code</th><th>Description</th><th>Qty</th><th>Price</th><th>Amount</th></tr>";
        itemcolour1 = '#E4FEB4';
        itemcolour2 = '#FED3FB';
        itemcolour = itemcolour2;
        oldpid = '';
        for (x in cart.items)
        {
            idx = eval(x)+1;
        if (cart.items[x].pid!=oldpid) {
                        if (itemcolour==itemcolour1) {
                                itemcolour=itemcolour2;
                        } else {
                                itemcolour=itemcolour1;
                        }
                        oldpid=cart.items[x].pid;
                }

                cartashtml = cartashtml +"<tr style='background-color:"+itemcolour+";' >";
                cartashtml = cartashtml +"<td style='text-align:left;' >"+idx+"</td>";
                cartashtml = cartashtml +"<td style='text-align:left;' >"+cart.items[x].code +"</td>";
                cartashtml = cartashtml +"<td style='text-align:left;'>"+cart.items[x].description + "</td>";
                cartashtml = cartashtml +"<td style='text-align:right;'>"+cart.items[x].qty + "</td>";
                cartashtml = cartashtml +"<td style='text-align:right;'>"+stdf(cart.items[x].netprice) + "</td>";
                cartashtml = cartashtml +"<td style='text-align:right;'>"+stdf(cart.items[x].netamount) + "</td>";

				tdel=cart.delivery;						
//                cartashtml = cartashtml +"<td style='text-align:right;'>"+stdf(tdel) + "</td>";

//              cartashtml = cartashtml +"<td style='text-align:right;'><a href='javascript:removeitem("+x+");refresh();' >rem</a>" + "</td>";
                cartashtml = cartashtml +"<td style='text-align:right;'><a href='javascript:removeitembypid(\""+cart.items[x].pid+"\");";
                cartashtml = cartashtml +"refresh(\""+ctype+"\",\""+template+"\");' style='background-color:red;color:white;' title='Remove' > remove </a>" + "</td>";
                cartashtml = cartashtml +"</tr>";               
        }

        totalcart();
        
        cartashtml = cartashtml +"<tr><td></td><td></td><td></td><td></td>";
        cartashtml = cartashtml +"<th>Net total: </th><td style='text-align:right;' >"+stdf(cart.nettotal)+"</td></tr>";

        cartashtml = cartashtml +"<tr><td></td><td></td><td></td><td></td>";
        cartashtml = cartashtml +"<th>VAT: </th><td style='text-align:right;' >"+stdf(cart.vat)+"</td></tr>";
        cartashtml = cartashtml +"<tr><td></td><td></td><td></td><td></td>";
        cartashtml = cartashtml +"<th>Delivery: </th><td style='text-align:right;'>"+stdf(cart.delivery)+"</td></tr>";  
        cartashtml = cartashtml +"<tr><td></td><td></td><td></td><td></td>";
        cartashtml = cartashtml +"<th>Total: </th><td style='text-align:right;'>"+stdf(cart.grosstotal)+"</td></tr>";

//                              "Delivery: "+cart.delivery+" <br/>"+
                                                                
        cartashtml = cartashtml +"</table>";

        
        if (ctype=='win') {
                cartashtml = cartashtml +"<br/><input type=\"button\" value=\"Checkout\" onclick=\"checkout();\" /><br/>";
                cartashtml = cartashtml +"<br/><input type=\"button\" value=\"Continue Shopping\" onclick=\"cartwin.hide();\" /><br/>";         
        } else {
                cartashtml = cartashtml +"<p style='text-align:center;'><input type=\"button\" value=\"Continue Shopping\" onclick=\"javascript:history.go(-1)\" />&nbsp;";                     
                cartashtml = cartashtml +"<input type=\"button\" value=\"Checkout\" onclick=\"checkout();\" /></p>";   
        }
//      document.getElementById("cartdisplay").innerHTML = cartashtml;
        
        return cartashtml;
}

function onpageload() {

        getcartfromcookie();



//      additem(cart, '0001', 'My widget' ,100,1, 17.5,0);
        
//      for (x in cart.items)
//      {
//              document.write(cart.items[x].code +'  '+ cart.items[x].description + "  <br />");
//      }
        
        
//      writecarttocookie();
        
}

function checkout() {

        cartdesc="";
    cartashtml = "<table id='scart'>";
    cartashtml = cartashtml +"<tr><th>Code</th><th>Description</th><th>Qty</th><th>Price</th><th>Amount</th></tr>";
                
        for (x in cart.items)
        {
            idx = eval(x)+1;
                cartdesc = cartdesc +"<item code=\""+cart.items[x].code +"\" description=\""+cart.items[x].description + "\" qty=\""+cart.items[x].qty + "\" ";
                cartdesc = cartdesc +"price=\""+stdf(cart.items[x].price) + "\" ";
                cartdesc = cartdesc +"amount=\""+stdf(cart.items[x].amount) + "\" ";
                cartdesc = cartdesc +"</item>";
                
                cartashtml = cartashtml +"<tr>";
                cartashtml = cartashtml +"<td style='text-align:left;' >"+cart.items[x].code +"</td>";
                cartashtml = cartashtml +"<td style='text-align:left;'>"+cart.items[x].description + "</td>";
                cartashtml = cartashtml +"<td style='text-align:right;'>"+cart.items[x].qty + "</td>";
                cartashtml = cartashtml +"<td style='text-align:right;'>"+stdf(cart.items[x].price) + "</td>";
                cartashtml = cartashtml +"<td style='text-align:right;'>"+stdf(cart.items[x].amount) + "</td>";
                cartashtml = cartashtml +"</tr>";               
        }
        
        cartashtml = cartashtml +"<tr><td></td><td></td><td></td>";
        cartashtml = cartashtml +"<th>Net total: </th><td style='text-align:right;'>"+stdf(cart.nettotal)+"</td></tr>";
        cartashtml = cartashtml +"<tr><td></td><td></td><td></td>";
        cartashtml = cartashtml +"<th>VAT: </th><td style='text-align:right;'>"+stdf(cart.vat)+"</td></tr>";
        cartashtml = cartashtml +"<tr><td></td><td></td><td></td>";
        cartashtml = cartashtml +"<th>Delivery: </th><td style='text-align:right;'>"+stdf(cart.delivery)+"</td></tr>";
        cartashtml = cartashtml +"<tr><td></td><td></td><td></td>";
        cartashtml = cartashtml +"<th>Total: </th><td style='text-align:right;'>"+stdf(cart.grosstotal)+"</td></tr>";           

        cartashtml = cartashtml +"</table>";
        cartashtml = cartashtml;

    document.getElementById("idescription").value=cartashtml;
    document.getElementById("descriptionxml").value=cartdesc;   

    document.getElementById("cost").value=stdf(cart.grosstotal);
        document.getElementById("grossamount").value=stdf(cart.grosstotal);

    document.getElementById("netamount").value=stdf(cart.nettotal);     
    document.getElementById("vatamount").value=stdf(cart.vat);  
    document.getElementById("deliveryamount").value=stdf(cart.delivery);
        
    document.getElementById("order").submit();
        
}

function cleanstr(istr) {
    re = new RegExp(" & " , "g" );
        istr = istr.replace(re,' &amp; ');
    re = new RegExp("'" , "g" );
        istr = istr.replace(re,"\'");
        
        return istr
}


function cartintemplate(template) {
        tstr = ajaxget(template,0);
        tstr = tstr.replace( '<%cart%>' , showcart('scr',template ) );
        return tstr;
        
}


//--><!]]>
