/********************************************/
/* IPS store javascript						*/
/********************************************/

ips.store = {
	totalProductPrice: 0.00,
	totalServicePrice: 0.00,
	totalAddons: 0,
	hasIPB: false,
	
	init: function()
	{
		document.observe("dom:loaded", function(){
			
			// Set up current customer check
			/*if( $('modal') )
			{
				var body = $$('body')[0];
				
				$('modal').setStyle("width: 100%; height: 100%; position: absolute; top: 0px; left: 0px; overflow: hidden; opacity: 0.8; min-height: " + ( body.getHeight() + 25 ) + "px");
				
			//	body.insert( { bottom: $('modal') } );
				body.insert( { top: $('customer_check') } ).setStyle('width: 100%; height: 100%');
				
				if( $('continue_store') )
				{
					$('continue_store').observe('click', function(e){
						$('modal').hide();
						$('customer_check').hide();
					});
				}			
			}*/
			
			// IPB license stuff
			var checkLicense = function(e)
			{
				if( $('ipb_standard').checked ){
					$('lbl_standard').addClassName('active');
					$('lbl_business').removeClassName('active');
				} else if( $('ipb_business').checked ) {
					$('lbl_business').addClassName('active');
					$('lbl_standard').removeClassName('active');
				}
			}
			
			checkLicense();
			$('ipb_standard').observe('click', checkLicense);
			$('ipb_business').observe('click', checkLicense);
			
			// Set events on relevant checkboxes
			$$('.product_toggle').each( function(item){
				$( item ).observe('click', ips.store.calculateProducts);
			});
			
			$('order_form').observe('submit', ips.store.buildPackageIDs);
			
			if( $('chat_package') ){
				$('chat_package').observe('change', function(e){ 
					ips.store.calculateProducts();
				 } );
			}
			
			ips.store.calculateProducts( null );			
		});		
	},
	
	buildPackageIDs: function( e )
	{
		Event.stop(e);
		var pids = [];
		
		$$('.product_toggle').each( function(item){
			
			if( $( item ).checked )
			{
				if( $( item ).id == 'ipb_standard' || $( item ).id == 'ipb_business' || $( item ).id == 'ipb_none' )
				{
					// Blah
					switch( $(item).id )
					{
						case 'ipb_standard':
							pids.push( products['ipboard']['standard']['pid'] );
							break;
						case 'ipb_business':
							pids.push( products['ipboard']['business']['pid'] );
							break;
						case 'ipb_none':
							Debug.write("No IPB license required");
							break;
					}
				}
				else if( $( item ).id == 'ipchat' )
				{
					// Get the selected chat package
					var plan = $F('chat_package');
					pids.push( services['ipchat'][ plan ]['pid'] );
				}
				else
				{
					if( !Object.isUndefined( products[ $( item ).id ] ) ){
						pids.push( products[ $( item ).id ]['pid'] );
					} else if( !Object.isUndefined( services[ $( item ).id ] ) ){
						pids.push( services[ $( item ).id ]['pid'] );
					} else {
						Debug.write("Can't find " + $( item ).id);
					}
				}
			}
		});
		
		if( pids.length == 0 )
		{
			alert("You'll need to select some products in order to continue!");
			return;
		}
		
		// Send to Nexus
		//Debug.write( submitURL + pids.join(',') );
		
		document.location.href = submitURL + pids.join(',');				
	},
		
	calculateProducts: function( e )
	{
		ips.store.totalProductPrice = 0.00;
		ips.store.totalServicePrice = 0.00;
		ips.store.totalAddons = 0;
		
		if( $('ipb_standard').checked ){
			ips.store.buildIPB( 'ipb_standard' );
		} else if( $('ipb_business').checked ){
			ips.store.buildIPB( 'ipb_business' );
		} else if( $('ipb_none').checked ){
			ips.store.buildIPB( 'ipb_none' );
		}
		
		ips.store.buildProduct( 'ipblog' );
		ips.store.buildProduct( 'ipgallery' );
		ips.store.buildProduct( 'ipnexus' );
		ips.store.buildProduct( 'ipcontent' );
		ips.store.buildProduct( 'ipdownloads' );
		
		ips.store.buildService( 'mssql' );
		ips.store.buildService( 'conversion' );
		ips.store.buildService( 'copyright' );
		ips.store.buildService( 'ipchat' );
		
		ips.store.calculateDiscount();
		ips.store.calculateTotal();
	},
	
	buildProduct: function( product )
	{
		if( $( product ).checked && !$('row_' + product) )
		{
			var price = ips.store.buildPrice( products[ product ]['license'] );
			var name = products[ product ]['name'];
			
			var template = productTemplate.evaluate( { id: product, price: price, name: name } );
			
			if( $('row_discount') ){
				$('row_discount').insert( { before: template } );
			} else {
				$('block_products').insert( { bottom: template } );
			}
			
			ips.store.addProductPrice( products[ product ]['license'] );
			ips.store.totalAddons++;
		}
		else if( $( product ).checked && $('row_' + product) )
		{
			ips.store.addProductPrice( products[ product ]['license'] );
			ips.store.totalAddons++;
		}
		else
		{
			if( $('row_' + product) ){
				$('row_' + product).remove();
			}
		}		
	},
	
	buildService: function( service )
	{
		if( service == 'ipchat' )
		{
			if( $('ipchat').checked )
			{
				if( $('row_ipchat') )
				{
					$('row_ipchat').remove();
				}
				
				var plan = $F('chat_package');
				var name = services[ service ][ plan ]['name'];
				var price = ( plan == 'five' ) ? "FREE" : ips.store.buildPrice( services[ service ][ plan ]['price'] );
				
				var template = productTemplate.evaluate( { id: service, price: price, name: name } );
			
				$('block_services').insert( template );
			
				if( plan != 'five' ){
					ips.store.addServicePrice( services[ service ][ plan ]['price'] );
				}
			}
			else
			{
				if( $('row_ipchat') )
				{
					$('row_ipchat').remove();
				}
			}			
		}
		else
		{
			if( $( service ).checked && !$('row_' + service) )
			{
				var price = ips.store.buildPrice( services[ service ]['price'] );
				var name = services[ service ]['name'];
			
				var template = productTemplate.evaluate( { id: service, price: price, name: name } );
			
				$('block_services').insert( template );
			
				ips.store.addServicePrice( services[ service ]['price'] );
			}
			else if( $( service ).checked && $('row_' + service ) )
			{
				ips.store.addServicePrice( services[ service ]['price'] );
			}
			else
			{
				if( $('row_' + service ) ){
					$('row_' + service).remove();
				}
			}
		}
	},
		
	
	buildIPB: function( id )
	{
		var type = id.replace('ipb_', '');
		
		if( ( id == 'ipb_standard' && $('row_ipb_standard') ) || ( id == 'ipb_business' && $('row_ipb_business') ) ){
			ips.store.addProductPrice( products['ipboard'][ type ]['license'] );
			ips.store.hasIPB = true;
			return;
		}
		
		// Remove current
		if( $('row_ipb_standard') ){ $('row_ipb_standard').remove(); }
		if( $('row_ipb_business') ){ $('row_ipb_business').remove(); }
		
		// No IPB license
		if( type == 'none' ){
			ips.store.hasIPB = false;
			return;
		}
		
		var price = ips.store.buildPrice( products['ipboard'][ type ]['license'] );
		var name = products['ipboard'][ type ]['name'];
		
		var template = productTemplate.evaluate( { id: id, price: price, name: name } );
		$('block_products').insert( { top: template } );
		
		ips.store.addProductPrice( products['ipboard'][ type ]['license'] );
		ips.store.hasIPB = true;
	},
	
	calculateDiscount: function()
	{
		if( $('row_discount') ){ $('row_discount').remove(); }
		if( ips.store.totalAddons == 0 || !ips.store.hasIPB ){ return; }		
		
		var discountPercent = discountRates[ ips.store.totalAddons ];
		
		// calculate discount
		var discount = ( ( ips.store.totalProductPrice.toFixed(2) / 100 ) * discountPercent ).toFixed(2);	
		var template = productTemplate.evaluate( { id: 'discount', name: discountPercent + "% Bundle Discount", price: "-" + ips.store.buildPrice( discount ) } );
		
		$('block_products').insert( { bottom: template } );
		
		// Now subtract discount
		ips.store.totalProductPrice = ips.store.totalProductPrice - discount;
	},
	
	calculateTotal: function()
	{
		var total = ips.store.buildPrice( ( ips.store.totalProductPrice + ips.store.totalServicePrice ) );
		$('total_price').update( total );
		
	},
	
	addProductPrice: function( value )
	{
		ips.store.totalProductPrice = ips.store.totalProductPrice + value;
	},
	
	addServicePrice: function( value )
	{
		ips.store.totalServicePrice = ips.store.totalServicePrice + value;
	},
	
	buildPrice: function( amount )
	{
		var i = parseFloat(amount);
		if(isNaN(i)) { i = 0.00; }
		var minus = '';
		if(i < 0) { minus = '-'; }
		i = Math.abs(i);
		i = parseInt((i + .005) * 100);
		i = i / 100;
		s = new String(i);
		if(s.indexOf('.') < 0) { s += '.00'; }
		if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
		s = minus + s;
		return '$' + s;		
	}	
}

ips.store.init();
