function in2CartItem(cart)
{
this.create=function()
							{
							//INITIALIZE VARS		
              this.cart=cart;
              this.ref="";
              this.qty=0;
              this.price=0.0;
              this.title="";
							this.xml=null;							
							}
							
this.loadXMLStr=function(xmlstr)
							{
							var xmlparser=new XMLParser(xmlstr);
							this.loadXML(xmlparser.doc);
							}
							
this.loadXML=function(xmldoc)
						     {						
						      this.xml=xmldoc;		
							this.ref=this.xml.getElementsByTagName("ref")[0].childNodes[0].data;
							this.qty=parseFloat(this.xml.getElementsByTagName("qty")[0].childNodes[0].data);
							this.title=this.xml.getElementsByTagName("title")[0].childNodes[0].data;
							this.price=parseFloat(this.xml.getElementsByTagName("price")[0].childNodes[0].data);
							}

this.setPrice=function(price)
						     {
						     this.price=price;
						     this.row.childNodes[3].childNodes[0].nodeValue=price;
						     this.cart.updateTotal();
						     }
						     
this.compare=function(otherItem)
							{
							if (otherItem.ref!=this.ref) return false;

							var otherOpts=otherItem.xml.getElementsByTagName("options")[0];
							var theseOpts=this.xml.getElementsByTagName("options")[0];

							if (otherOpts.childNodes.length!=theseOpts.childNodes.length) return false;

							for (var i=0;i<theseOpts.childNodes.length; i++)
							  {
							  var otherNode=otherOpts.childNodes[i];
							  var thisNode=theseOpts.childNodes[i];
                                                          var otherval=otherNode.childNodes.length>0?otherNode.childNodes[0].nodeValue:'';
                                                          var thisval=thisNode.childNodes.length>0?thisNode.childNodes[0].nodeValue:'';

							  if (otherval!=thisval)
                                                                  return false;
							  }

							return true;
							}
					
this.calcTotalPrice=function() {return this.qty*this.price;}

this.addQty=function(qty)
							{
							this.qty+=qty;
							this.row.childNodes[0].childNodes[0].nodeValue=this.qty;
							this.xml.getElementsByTagName("qty")[0].childNodes[0].data=this.qty;
							
							this.cart.updateTotal();
							}
							
this.create(cart);

return this;
}

//LOCAL JAVASCRIPT CART CLASS
function in2Cart(id,cssClassPrefix)
{
 	this.create=function(id,csscp)
					 {
					 document.writeln("<div id='"+id+"_Frame' class='"+csscp+"_Frame'></div>");
					 
					 this.frame=document.getElementById(id+"_Frame");
					 
					 this.header=document.createElement("div");
					 this.header.id=id+"_Header";
					 this.header.className=csscp+"_Header";
					 this.header.appendChild(document.createTextNode("Basket"));
					 this.frame.appendChild(this.header);
					 
					 var table=document.createElement("table");
					 table.id=id+"_ItemBox";
					 table.className=csscp+"_ItemBox";
					 this.itemBox=document.createElement("tbody");
					 table.appendChild(this.itemBox);
					 this.frame.appendChild(table);
					 
					 this.footer=document.createElement("div");
					 this.footer.id=id+"_Footer";
					 this.footer.className=csscp+"_Footer";
                     
                     this.totalBox=document.createElement("div");
                     this.totalBox.className=csscp+"_Total";
					 this.totalBox.appendChild(document.createTextNode("Total: £0.00"));
                     this.footer.appendChild(this.totalBox);
                     
                     var btnBar=document.createElement("div");
                     var checkoutButton=document.createElement("a");
                     checkoutButton.className=csscp+"_CheckoutBtn";
                     checkoutButton.href=in2CartCheckoutUrl;
                     checkoutButton.appendChild(document.createTextNode("Checkout"));
                     btnBar.appendChild(checkoutButton);
                     this.footer.appendChild(btnBar);
                     
					 this.frame.appendChild(this.footer);

					 this.items=new Array();
					 }
					 
	this.load=function()
					 {
					 var request=new ajaxRequest(this);
					 request.onResponse=function(xmlHttp)
					 					{
										this.callerObj.loadXML(xmlHttp.responseXML);
										}
										
					 request.sendRequest("GET",in2CartInteractionUrl+"?ac=loadCart",null);
					 }
	
	this.loadXML=function(xmldoc)
					{
					var itemsnode=xmldoc.getElementsByTagName("item");
					this.items=new Array();
					
					for (var i=0; i<itemsnode.length; i++)
					  {
					  var cartItem=new in2CartItem(this);
					  
					  cartItem.loadXML(itemsnode[i]);
					  
					  this.addItemToItemBox(cartItem);
					  cartItem.itemNum=1+this.items.length;
					  this.items[this.items.length]=cartItem;
  				  this.updateTotal();
					  }
					}
						
	this.addFormItem=function(form)
                  {
                  var xml="<item><ref>"+form.ref.value+"</ref><qty>"+form.qty.value+"</qty><title>"+
                  			  form.title.value+"</title><price>"+form.price.value+"</price><options>";
                  
                  inputs=form.elements;
                  
                  for (i=0; i<inputs.length; i++)
                    {
                  	var field=inputs[i];
                  	
                  	if (field.name.search("opt_")==0)
                  	  {
                  		var tag=field.name.substr(4);
                  		xml+="<"+tag+">"+field.value+"</"+tag+">";
                  		}
                  	}
                  
                  xml+="</options></item>";
                  
                  this.addItem(xml);
                  }
				 
	this.addItem=function(xmlstr)
					 {
					 //CREATE THE CART ITEM
					 var cartItem=new in2CartItem(this);
					 
					 cartItem.loadXMLStr(xmlstr);
					 
					 //CHECK IF AN IDENTICAL ITEM ALREADY EXISTS
					 var existingItem=this.hasItem(cartItem);
					 
					 if (existingItem!=null)
					   {
                       existingItem.addQty(cartItem.qty);
                       cartItem=existingItem;
                       }
					 else
					   {
					   this.addItemToItemBox(cartItem);
					   cartItem.itemNum=1+this.items.length;
					   this.items[this.items.length]=cartItem;
					   }
					   
					 //POST THE DATA TO THE SERVER
					 var request=new ajaxRequest(cartItem);
                     request.onResponse=function(xmlHttp)
					 					{
										if (xmlHttp.responseXML)
										  {
										  var nodes=xmlHttp.responseXML.getElementsByTagName("price");
										  
										  if (nodes.length==0)
										    alert("Cannot retrieve item price.");
										  else
										    this.callerObj.setPrice(parseFloat(nodes[0].childNodes[0].data));
										  }
										}
										
					 request.sendRequest("POST",in2CartInteractionUrl+"?ac=addItem",xmlstr);//Node2Str(cartItem.xml));
					 }
					 
	this.addItemToItemBox=function(cartItem)
					 {
					 //CREATE HTML ELEMENTS FOR BASKET ENTRY	
  				   cartItem.row=document.createElement("tr");
  					 
  				   //CREATE CELLS
  				   qtytd=document.createElement("td");
  				   xtd=document.createElement("td");
  				   titletd=document.createElement("td");
  				   pricetd=document.createElement("td");
  				   deltd=document.createElement("td");
  					 
  				   //ADD QTY AND TITLE
  				   qtytd.appendChild(document.createTextNode(cartItem.qty));
  				   xtd.appendChild(document.createTextNode("x"));
  				   titletd.appendChild(document.createTextNode(cartItem.title));
  				   pricetd.appendChild(document.createTextNode("£"+cartItem.price));
  					 
  				   //CREATE THE REMOVE BUTTON
  				   img=document.createElement("img");
					   img.style.cursor="pointer";
  				   img.src=in2CartImagePath+"del.gif";
  				   img.item=cartItem;
  				   img.onclick=function() {this.item.cart.removeItem(this.item.itemNum);}
  				   deltd.appendChild(img);
  					 
  				   //ADD ELEMENTS TO THE DOCUMENT
  				   cartItem.row.appendChild(qtytd);
  				   cartItem.row.appendChild(xtd);
  				   cartItem.row.appendChild(titletd);
  				   cartItem.row.appendChild(pricetd);
  				   cartItem.row.appendChild(deltd);
  					 
  				   this.itemBox.appendChild(cartItem.row);
					 }
					 
	this.removeItem=function(itemNum)
					 {
					 var cartItem=this.items[itemNum-1];

                     var request=new ajaxRequest(this);
                     request.sendRequest("POST",in2CartInteractionUrl+"?ac=delItem&num="+itemNum,null);
					 this.itemBox.removeChild(cartItem.row);
					 
					 //REMOVE FROM ARRAY
					 this.items.splice(itemNum-1,1);

					 //RE-NUMBER REMAINING
					 for (var i=0; i<this.items.length; i++)
					   this.items[i].itemNum=1+i;
					   
					 this.updateTotal();
					 }
					 
	this.hasItem=function(item2compare)
					 {
					 for (i=0; i<this.items.length; i++)
					   {
						 var thisitem=this.items[i];
						 
						 if (thisitem.compare(item2compare)==true)
						   return thisitem;
						 }
						 
					 return null;
					 }
					 
	this.updateTotal=function()
											{
											var price=0.0;
											
											for (i=0; i<this.items.length; i++)
											  {
												var cartItem=this.items[i];
												
												price+=this.items[i].calcTotalPrice();
												}
												
											this.totalBox.childNodes[0].nodeValue="Total: £"+price.toFixed(2); 
											}
											
    this.getElement=function() {return this.frame;}
    
	this.create(id,cssClassPrefix);
	
	return this;
}
