var SideCart = {

	_COOKIE_NAME : 'sidecart',

	// These store the options.
	package : '',
	heading_count : '',
	zone_cost : '',
	position : '',
	advert : '',
	// The costs, except zone_cost, are totals.
	package_cost : 0,
	heading_cost : 0,
	heading_cost : 0,
	advert_cost : 0,
	locations : 0,
	// The rates store the price from the DB.
	PACKAGE_RATE : 0,
	HEADING_RATE : 0,
	ADVERT_RATE : 0,
	
	locates : new Array(),

	Draw : function()
	{
		// Only draw if the container exists.
		if ( document.getElementById( 'sidecart' ) == null ) {
			return;
		}
	
		this.package_cost = 0;
		this.heading_cost = 0;
		this.position_cost = 0;
		this.advert_cost = 0;
	
		var html = '';
		
		// Intro
		html += '<div id="sidecart-box"><div id="sidecart-inner"><img src="/img/cart/purchase-summary-header.gif" />';
		
		
		// Package
		if ( this.package == 'bundle' ) {
		
			this.package_cost = this.PACKAGE_RATE;
			html += '<h3>Bloo Bundle</h3>' +
				'<div class="info-box"><ul>' +
					'<li>Free Call ($200 credit)</li>' +
					'<li>Free SMS ($50 credit)</li>' +
					'<li>Professional Advert</li>' +
					'<li>Free Info</li>' +
					'<li>Thumbnail</li>' +
					'<li>Standard business details</li>' +
					'<li>bloo portfolio</li>' +
					'<li>Google Local Review</li>' +
				'</ul></div>' +
				'<div class="cart-price">' +
					'<p class="wording">Listing Price: </p><p class="pricing">$' + this.package_cost + '</p>' +
				'</div>';
		}
		else if ( this.package == 'free' ) {
		
			this.package_cost = this.PACKAGE_RATE;
			html += '<h3>Bloo Free</h3>' +
				'<div class="info-box"><ul>' +
					'<li>Standard business details</li>' +
				'</ul></div><div style="clear:both;"></div><br />';
		}
		
		// Locations
		
		if ( this.locations > 0 )
			{
			html += '<h3>' + ( this.locations ) + '&nbsp;Location(s)</h3>' +
				'<div class="info-box">';
			
			for(i=0;i<this.locates.length;i++)
				{
				html += this.locates[i] + '<br />';
				}
			
			html +=	'</div>' +
				'<div class="cart-price"><p class="wording">x ' + this.locations + ' location(s): </p><p class="pricing">$'+ ( this.package_cost*this.locations) + '</p></div>'

			}
		
		
		
		
		// Heading Count
		if ( this.heading_count > 2 && this.package == 'bundle') {
			if( this.locations > 0)
				{
				html += '<h3>' + ( this.heading_count - (2*this.locations) ) + '&nbsp;Additional Heading(s)</h3>';
				
				this.heading_cost = this.HEADING_RATE * ( this.heading_count - (2*this.locations) );
				html += '<div class="cart-price"><p class="wording">Price: </p><p class="pricing">$' + this.heading_cost + '/year</p></div>';
				}
			else
				{
				html += '<h3>' + ( this.heading_count - 2 ) + '&nbsp;Additional Heading(s)</h3>';
				
				this.heading_cost = this.HEADING_RATE * ( this.heading_count - 2 );
				html += '<div class="cart-price"><p class="wording">Price: </p><p class="pricing">$' + this.heading_cost + '/year</p></div>';
				}
		}
		else if ( this.heading_count > 1 && this.package == 'free') {
				if( this.locations > 0)
					{
					html += '<h3>' + ( this.heading_count - this.locations ) + '&nbsp;Additional Heading(s)</h3>';
					
					this.heading_cost = this.HEADING_RATE * ( this.heading_count - this.locations );
					html += '<div class="cart-price"><p class="wording">Price: </p><p class="pricing">$' + this.heading_cost + '/year</p></div>';
					}
				else
					{
					html += '<h3>' + ( this.heading_count - 1 ) + '&nbsp;Additional Heading(s)</h3>';
					
					this.heading_cost = this.HEADING_RATE * ( this.heading_count - 1 );
					html += '<div class="cart-price"><p class="wording">Price: </p><p class="pricing">$' + this.heading_cost + '/year</p></div>';
					}
			}
			
		
		// TOTAL Heading count, primarily for positioning upgrade
		
		if ( this.heading_total > 0 )
			{
				html += '<h3>Total Headings</h3>' +
				'<div class="cart-price"><p class="wording">Price: </p><p class="pricing">$' + this.heading_total + '/year</p></div>';

			}


		// Zoning
		if ( typeof( this.zone_cost ) == 'number' ) {
			if ( this.zone_cost > 0 ) {
			
				html += '<h3>Zoning</h3>' +
					'<div class="cart-price"><p class="wording">Price: </p><p class="pricing">$' + this.zone_cost + '/year</p></div>';
			}
			else if ( this.zone_cost == 0 ) {
			
				html += '<h3>Zoning</h3>' +
					'<div class="cart-price"><p class="wording">Price: </p><p class="pricing">FREE</p></div>';
			}
		}
		
		
		// Position
		if ( this.position == 'secure' ) {
		
			html += '<h3>Positioning</h3>' +
				'<div class="info-box"><p>Secure Listing</p></div><br/>';
		}
		else if ( this.position == 'priority' ) {
		
			this.position_cost = this.POSITION_RATE;
			html += '<h3>Positioning</h3>' +
				'<div class="info-box"><p>Priority Listing</p></div><br/>';
		}
		else if ( this.position == 'base' ) {
		
			this.position_cost = this.POSITION_RATE;

			html += '<h3>Positioning</h3>' +
				'<div class="info-box"><p>Standard Listing</p></div><br/>';
		}

		
		// Advert
		if ( this.advert == 'professional' ) {

			var cost = this.ADVERT_RATE;
			this.advert_cost = cost;
			html += '<h3>Advert Design</h3>' +
				'<div class="info-box"><p>Professional advert</p></div>' +
				'<div class="cart-price"><p class="wording">Price: </p><p class="pricing">$' + cost + '</p></div>';
		}
		else if ( this.advert == 'thumb' ) {

			this.advert_cost = this.ADVERT_RATE;
			html += '<h3>Advert Design</h3>' +
				'<div class="info-box"><p>Use website thumbnail</p></div>' +
				'<div class="cart-price"><p class="wording">Price: </p><p class="pricing">FREE</p></div>';
		}
		else if ( this.advert == 'diy' ) {

			this.advert_cost = this.ADVERT_RATE;
			html += '<h3>Advert Design</h3>' +
				'<div class="info-box"><p>Create your own advert</p></div>' +
				'<div class="cart-price"><p class="wording">Price: </p><p class="pricing">FREE</p></div>';
		}
		

		html += '<div style="clear:both;"></div></div></div><div style="clear:both;"></div>';

	
		// Prices
		subtotal = 0;
		if ( this.package_cost > 0 ) {
			subtotal += this.package_cost;
		}
		if ( this.heading_cost > 0 ) {
			subtotal += this.heading_cost;
		}
		if ( this.heading_total > 0 ) {
			subtotal += this.heading_total;
		}		
		if ( this.zone_cost > 0 ) {
			subtotal += this.zone_cost;
		}
		if ( this.position_cost > 0 ) {
			subtotal += this.position_cost;
		}
		if ( this.advert_cost > 0 ) {
			subtotal += this.advert_cost;
		}
		if ( this.locations > 0 ) {
			subtotal += (this.package_cost * (this.locations - 1));
		}
		gst = ( subtotal * 0.1 );
		total = subtotal + gst;
		
		html += '<table width="170" cellspacing="0">' +
			'<tr align="right" height="22">' +
				'<th>Listing:</th>' +
				'<td width="58">$' + this.package_cost.toFixed( 2 ) + '</td>' + 
			'</tr>'; 
		if ( this.locations > 0 ) {
			html += '<tr align="right" height="22">' +
					'<th>x location(s):</th>' +
					'<td>$' + (this.package_cost * this.locations).toFixed( 2 ) + '</td>' + 
				'</tr>'; 
		}
		if ( this.heading_cost > 0 ) {
			html += '<tr align="right" height="22">' +
					'<th>Additional Heading(s):</th>' +
					'<td>$' + this.heading_cost.toFixed( 2 ) + '</td>' + 
				'</tr>'; 
		}
		if ( this.heading_total > 0 ) {
			html += '<tr align="right" height="22">' +
					'<th>Total Headings:</th>' +
					'<td>$' + this.heading_total.toFixed( 2 ) + '</td>' + 
				'</tr>'; 
		}
		if ( this.zone_cost > 0 ) {
			html += '<tr align="right" height="22">' +
					'<th>Zoning:</th>' +
					'<td>$' + this.zone_cost + '</td>' + 
				'</tr>'; 
		}
		if ( this.advert_cost > 0 ) {
			html += '<tr align="right" height="22">' +
					'<th>Advert:</th>' +
					'<td>$' + this.advert_cost.toFixed( 2 ) + '</td>' + 
				'</tr>'; 
		}
		html += '<tr align="right" height="22">' +
				'<th>GST:</th>' +
			 	'<td>$' + gst.toFixed( 2 ) + '</td>' + 
//				'<td>$' + gst + '</td>' + 
			'</tr>' + 
			'<tr class="total" align="right" height="20">' +
				'<th>Total:</th>' +
				'<td>$' + Math.round(total).toFixed( 2 ) + '</td>' + 
//				'<td>$' + total + '</td>' + 
			'</tr>' + 
			'</table>';
		
		var container = document.getElementById( 'sidecart' );
		container.innerHTML = html;
	},
	
	SetPackage : function( package, package_rate )
	{
		this.package = package;
		this.PACKAGE_RATE = package_rate;
		this.Update();
	},

	SetHeadingCount : function( count, heading_rate )
	{
		this.heading_count = count;
		this.HEADING_RATE = heading_rate;
		this.Update();
	},
	
	SetHeadingTotal : function( heading_tot , heading_tot_cnt)
	{
		this.heading_total = heading_tot;
		this.heading_total_count = heading_tot_cnt;
		this.Update();
	},

	SetPosition : function( position, package_rate, heading_rate )
	{
		this.position = position;
		this.PACKAGE_RATE = package_rate;
		this.HEADING_RATE = heading_rate * 12;
		if(position == 'secure')
			{
			//this.heading_total = ( this.heading_total_count * this.HEADING_RATE );
			this.heading_total = ( this.heading_total_count * this.HEADING_RATE );
			}
		this.Update();
	},
	
	SetZoneCost : function( cost )
	{
		this.zone_cost = cost / 100;
		this.Update();
	},

	SetAdvert : function( advert, advert_rate )
	{
		this.advert = advert;
		this.ADVERT_RATE = advert_rate
		this.Update();
	},
	
	SetLocations : function( locs )
	{
		this.locations = locs
		this.Update();
	},
	
	AddLocation : function ( locate )
	{
		this.locations++;
		this.locates.push(locate);
		this.Update();
	},
	
	AddHeading : function ()
	{
		this.heading_count++;
		this.Update();
	},

	Update : function()
	{
		this.Save();
		this.Draw();
	},

	/**
	*	Writes the current cart details to a cookie.
	*/
	Save : function()
	{
		var cart_content = this.package + 
			',' + this.PACKAGE_RATE +
			',' + this.heading_count +
			',' + this.heading_total +
			',' + this.heading_total_count +
			',' + this.HEADING_RATE +
			',' + this.zone_cost +
			',' + this.position + 
			',' + this.advert +
			',' + this.ADVERT_RATE;
		Cookie.Create( this._COOKIE_NAME, cart_content, 0 );
	},
	
	/**
	*	Restores the cart from a cookie if one has been written.
	*/
	Load : function()
	{
		var cookie = Cookie.Read( this._COOKIE_NAME );

		if ( cookie == null ) {
			return;
		}
		
		var values = cookie.toString().split( ',' );
		if ( values.length != 10 ) {
			return;
		}
		
		this.package = values[ 0 ];
		this.PACKAGE_RATE = parseFloat( values[ 1 ] );
		this.heading_count = parseInt( values[ 2 ] );
		this.heading_total = parseInt( values[ 3 ] );
		this.heading_total_count = parseInt( values[ 4 ] );
		this.HEADING_RATE = parseInt( values[ 5 ] );
		this.zone_cost = ( values[ 4 ] == '' ) ? '' : parseInt( values[ 6 ] );
		this.position = values[ 7 ];
		this.advert = values[ 8 ];
		this.ADVERT_RATE = parseInt( values[ 9 ] );
		this.Draw();
	}
}
