Calculator = new Class({

	Implements: [Options, Events],
	options: {
		tables: []
	},
	
	initialize: function(options) {
	
		this.setOptions(options);
		this.selectedItem = {};
		var me = this;
		
		// setup categories
		
		this.options.tables.each(function(item){
		
			var option = new Element('option', {
				'value' : item.id,
				'text' : unescape(item.name)
			});
			option.inject($('calc-category', 'bottom'));
		});
		$('calc-category').addEvent('change', function(ev){
			me.fillProducts(this.options[this.selectedIndex].value);
			me.processResults();
		}, me);
		me.fillProducts($('calc-category').options[0].value);
		me.processResults();
		$$('input.autoupdate').each(function(item){
		
			item.addEvent('keyup', function(ev){
			//return this.value.test('^[0-9]+\.?[0-9]{0,2}$');
				me.processResults();
			});
		});
	},
	
	fillProducts: function(catID) {
		var me = this;
		var selectedCategory = this.options.tables.filter(function(item){
			
			return item.id == catID;
		});
		
		$('calc-product').empty();
		selectedCategory[0].data.each(function(item,index){
			if (index > 0) {
				var option = new Element('option', {
				
					'value': item[3],
					'text' : me.stripslashes(unescape(item[0]))
					
				});
				option.store('item', item);
				option.inject($('calc-product', 'bottom'));
			
			}
		});
		$('calc-product').addEvent('change', function(ev){
			me.selectedItem = this.options[this.selectedIndex].retrieve('item');
			$('calc-product-units').value = me.selectedItem[6];
			$('calc-product-cost').value = me.selectedItem[3];
			
			me.processResults();
		});
		me.selectedItem = $('calc-product').options[0].retrieve('item');
		$('calc-product-units').value = me.selectedItem[6];
		$('calc-product-cases').value = '5';
		$('calc-product-cost').value = me.selectedItem[3];
	},
	
	processResults: function() {
		var pricePerCase = this.sanitizeNumber(this.selectedItem[5]).toFloat();
		var vivUnits = this.sanitizeNumber(this.selectedItem[4]).toInt();
		
		//var caseRatio = vivUnits / this.sanitizeNumber($('calc-product-units').value).toFloat();
		
		
		//it's simply: (Viv Price - Your Price Per Case)* Cases Used Per Month
		var yourPrice = this.sanitizeNumber($('calc-product-cost').value).toFloat();
		//var newUserPrice = yourPrice * caseRatio;
		var casesUsed = this.sanitizeNumber($('calc-product-cases').value).toFloat();
		var savings = ((yourPrice - pricePerCase) * (casesUsed)) * 12;
		var roundedSavings = Math.round(savings*100)/100
		
		$('calc-viv-price').setProperty('text', '$' + pricePerCase);
		
		
		if (roundedSavings < 0.1 || !roundedSavings) {
			$('calc-viv-savings').setProperty('text', 'No Savings');
		} else {
			$('calc-viv-savings').setProperty('text', '$' + roundedSavings);
		}
	
	},
	stripslashes: function(str) {
		str=str.replace(/\\'/g,'\'');
		str=str.replace(/\\"/g,'"');
		str=str.replace(/\\0/g,'\0');
		str=str.replace(/\\\\/g,'\\');
		return str;
	},
	sanitizeNumber: function(string) {
		string = string.replace('$', '');
		string = string.replace(',', '.');
		string = string.replace(' ', '');
		return string;
	}
	
});