/**
	Shopping Cart Object
**/

			function ShoppingCart( cartName, numValues ) {
				this.cartName = cartName
				this.itemSep = '~';
				this.fieldSep = ':';
				//this.cookie = new String();   - not here, just a buffer in a method
				this.ith = 0;
				this.numValues = numValues;
				this.cart = new Array();
				this.item = new Array();
				this.fieldNameList = new Array();
				this.fieldValues = new Array();
				this.itemString = new String();
			}


		ShoppingCart.prototype.openCookieJar = function() {
				var wholeCookie = new String(document.cookie);
				var crumbs = wholeCookie.split(';');
				var i, a,b;
	
				this.cart.length=0;			
				if( wholeCookie.length == 0 )  { this.cart.length=0; return };
				if( wholeCookie.indexOf( '=' ) < 0 ) { this.cart.length=0; return }

				for( i=0; i<crumbs.length; i++ ) {
					a = crumbs[i].split('=');
					if( a.length < 2 ) { this.cart.length=0; return }
			
					if( this.trim(a[0]) == this.cartName ) {
						this.unpackCart( unescape(a[1]) );
						return;
					}
				}
				this.cart.length=0;
			}
			
			ShoppingCart.prototype.unpackCart = function( c ) {
				if( c.length == 0  ) return;
				if(c.indexOf(this.itemSep)<0 ) 					// Single item in cart
					this.cart[0]=c;
				else
					this.cart = c.split( this.itemSep );		// fetch the still connected cart field items.
			}
			

			ShoppingCart.prototype.rollCart = function() {
				return( this.cart.join( this.itemSep ) );
			
			}

			ShoppingCart.prototype.crush = function() {									//** not used
			
			var wholeCart = this.cart.join( this.itemSep );
			document.cookie = this.cartName + '=' + escape( wholeCart ); 
		}	
		



		ShoppingCart.prototype.isEmpty = function() { return( this.cart.length == 0 ); }
		
		ShoppingCart.prototype.emptyCart = function() { this.cart.length = 0 }

		ShoppingCart.prototype.defineFields = function() {
			var i, fname;
					
			for(i=0; i<arguments.length; i++) {
				 fname = arguments[i];
				if( this.inList( fname, this.fieldNameList ) == -1 ) 
					this.fieldNameList[ this.fieldNameList.length ] = fname;
			}
		}
		
		ShoppingCart.prototype.gatherFormData = function( oForm ) {
			var i, p;
			var e = oForm.elements;

			this.fieldValues.length=0;
			for( i=0; i<this.fieldNameList.length; i++ ) 
				this.fieldValues[this.fieldValues.length]='';

			this.fieldValues.length = this.fieldNameList.length;			
			for( i=0; i<e.length; i++ ) {
				p = this.inList(e[i].name);
				if( p > -1 ) {
					this.fieldValues[p]=e[i].value;
				}
			}
		}		
		
		ShoppingCart.prototype.packFieldValues = function () {
			this.itemString = this.fieldValues.join( this.fieldSep );
		}
		
		ShoppingCart.prototype.addItemStringToCart = function () {
			this.cart[this.cart.length]=this.itemString;
		}
		
		ShoppingCart.prototype.addItemToCart = function () {
			this.cart[this.cart.length] = this.fieldValues.join( this.fieldSep );
		}
		
		ShoppingCart.prototype.replaceItemInCart = function () {	
			this.cart[this.ith] = this.itemString;
		}	
		
		
		
		ShoppingCart.prototype.packCart = function(  ) {
			var i, newCart = new Array();
			var wholeCart = new String();
			newCart.length = 0;
			for( i=0; i<this.cart.length; i++ ) 
				if( this.cart[i] !== '') newCart[newCart.length] = this.cart[i] ;
			this.cart.length=0;				
			this.cart = newCart;
			
			wholeCart = this.cart.join( this.itemSep );
			document.cookie = this.cartName + '=' + escape( wholeCart );
			
		}
		
		ShoppingCart.prototype.getNextItem = function () {
				this.itemString = this.nextItem();
				this.fieldValues = this.itemString.split(this.fieldSep);
				return( this.fieldValues );
		}		
				
		
		ShoppingCart.prototype.removeItem = function( n ) {		//** not used
			if( n < this.cart.length )
				this.cart[n] = '';
		}
		
		ShoppingCart.prototype.inList = function( oName ) {
			var i;
			for(i=0; i<this.fieldNameList.length; i++) 
				if(oName == this.fieldNameList[i]) return( i );
			return( -1 );
		}
		
			
		ShoppingCart.prototype.trim = function(s) {
			var Trim = /^\s*|\s*$/g;
			return( s.replace( Trim, "" ) );
		}

		// Iterator		
		ShoppingCart.prototype.first     = function () {this.ith = 0 }
		ShoppingCart.prototype.atEnd  = function () {return(this.ith >= this.cart.length) }
		ShoppingCart.prototype.next    = function () { this.ith++ }
		ShoppingCart.prototype.item    = function () { return(this.cart[this.ith]) }
		ShoppingCart.prototype.nextItem = function () { return(this.cart[this.ith++]) }
		ShoppingCart.prototype.kill      = function () { this.cart.length=0 }
		
		
		
//--------------Specific to Particular Cart Structure------------//

	
		ShoppingCart.prototype.addItem = function( ) {
			var i, f = this.fieldValues;
			if( this.numValues != arguments.length ) { alert( "Yikes!!\nNot the right amount of values!!" ); return}
			this.openCookieJar();
			f.length = 0;
			for( i=0; i<arguments.length; i++ ) {
				f[f.length] = arguments[i];
			}
			this.addItemToCart();
			this.packCart();
		}

		ShoppingCart.prototype.addFreeItem = function( ) {
			var i, f = this.fieldValues;
			var g, r ,s= new String();
			var found=false;
			if( this.numValues != arguments.length ) { alert( "Yikes!!\nNot the right amount of values!!" ); return}
			this.openCookieJar();
			f.length = 0;
			for( i=0; i<arguments.length; i++ ) {
				f[f.length] = arguments[i];
			}

			g = f.join( this.fieldSep );

			for( i=0; i<this.cart.length; i++ ) {
				s = this.cart[i];
				if(  s.indexOf("Free Sample") > -1 ) {
					this.cart[i] = g;
					found = true;
				}					
			}

			if( !found )
				this.cart[this.cart.length] = g;


			this.packCart();
			
		
		}


//----------------End of the Shopping cart object-------------------//		
		
		function roundOff( c ) {
			c = Math.round( c*100 );
			c = c/100;
			return( c );
		}

		function calcQuant( quantity ) {
			var q = parseInt( quantity);
			if( isNaN(q) ) q = 0;
			return( q );
		}


		function calcCost( quantity, price ) {
			var q = parseFloat( quantity);
			var p = parseFloat( price );
				if( isNaN(q) ) q = 0.00;
				if( isNaN(p) ) p = 0.00;						
			var c = p*q;
			c = Math.round( c*100 );
			c = c/100;
			return( c );
		}

		function makeCents( v ) {
			if( !v ) return;
			var a = v.split('.');
			if( a.length != 2 ) return( v + '.00' );
			var z = a[1];
			if( z.length < 2 ) z += '0';
			if( z.length < 2 ) z += '0';
			return ( a[0] +'.'+ z );
		}			
		
/**
**/		

		
		function emptyNotice() {
			var m =  "<center><font size=4 color='#880000' face='arial'><b>";
			m += "Your cart is empty - Please visit our <a href='x-ray-film.html'>store</a>";
			m += "</b></font></center>";
			return( m );			
		}


		function removeChecked() {
			var i, check = document.cartform.remove;

			if( cart.cart.length == 1 ) {
				if( check.checked ) { cart.emptyCart();	}
			} else {

					for(i=0; i<check.length; i++) {
						if(check[i].checked) cart.cart[i]='';
					}
			}					
			cart.packCart();
			repaintCart();
		}

		function removeAll() {
			cart.emptyCart();
			cart.packCart();
			repaintCart();
		}

		function repaintCart() { self.location.replace( self.location );}


/*
	Example:
	var cart = new ShoppingCart( 'test' );
	cart.defineFields( 'first', 'second', 'third', 'fourth');
	
*/

// Define the shopping cart for this application.
	var cart = new ShoppingCart( 'sqcart', 7);
	var basket = new ShoppingCart( 'sqbasket', 2 );


