var planChooser = new function(){
	var price_per_pound = 1.25;
	var price_per_credit = 1.99;
	var plan = {'uses':0,'pounds':0,'credits':0}
	this.set = function(event){
		var el=event.target;
		if(isInt(event)){
			el.onkeyup = function(){
				plan[el.name] = el.value;
				updatePrice();
			}
		} else{
			return false;
		}
	}
	var scDiscount = function(credits){
		var percent = credits ? Math.max(0,4.819*Math.log(4.579*credits)) : 0;
		return percent/100;
	}
	var wdfDiscount = function(pounds){
		var percent = pounds ? Math.max(0,8.011*Math.log(.3519*pounds)) : 0;
		return percent/100;
	}
	var updatePrice = function(){
		if(plan.uses){
			// LAUDNRY
			var pounds_per_month = (plan.pounds*plan.uses);
			var wdf_subtotal = pounds_per_month*price_per_pound;
			var wdf_percent = wdfDiscount(plan.pounds);
			var wdf_discount = wdf_percent*wdf_subtotal;
			var wdf_total = wdf_subtotal-wdf_discount;
			
			// SPECIAL CARE
			var credits_per_month = (plan.credits*plan.uses);
			var sc_subtotal = (credits_per_month*price_per_credit);
			var sc_percent = scDiscount(plan.credits);
			var sc_discount = sc_percent*sc_subtotal;
			var sc_total = (sc_subtotal-sc_discount);
			
			var price = wdf_total + sc_total;
		} else{
			var price = 0;
		}
		$('custom_price').innerHTML = '$'+price.toFixed(2)+' / month';
	}
	var last_active_plan;
	this.setPlan = function(plan){
		if(last_active_plan) removeClassName(last_active_plan, 'active');
		addClassName(plan, 'active');
		last_active_plan = plan;
	}
}





var planCalculator = new function(){
	
	var plan = {'uses':0,'pounds':0,'credits':0}
	var last_els = new Array();
	
	var scDiscount = function(credits){
		var percent = credits ? Math.max(0,4.819*Math.log(4.579*credits)) : 0;
		//var percent = credits ? Math.max(0,3.076*Math.log(0.5301*credits)) : 0;
		return percent/100;
	}
	var wdfDiscount = function(pounds){
		var percent = pounds ? Math.max(0,8.011*Math.log(.3519*pounds)) : 0;
		//var percent = pounds ? Math.max(0,6.048*Math.log(.1277*pounds)) : 0;
		return percent/100;
	}
	this.verifyInput = function(el,type){
		if(el.value.length){
			if(type=="uses"){
				if(el.value < 2) el.value=2;
			}else if(type=="pounds"){
				if(el.value < 15) el.value = 15;
			}else if(type=="credits"){
				if(el.value < 5) el.value = 5;
			}
			this.set(type, el.parentNode, el.value);
		} else{
			removeClassName(el.parentNode,'selected');
		}
	}
	this.getUsesPerMonth = function(){return plan.uses;}
	this.getPoundsPerUse = function(){return plan.pounds;}
	this.getCreditsPerUse = function(){return plan.credits;}
	this.parseInput = function(el,type){
		if(el.value.length){
			return el.value;
			if(type=="uses"){
				if(el.value < 2) el.value=2;
			}else if(type=="pounds"){
				if(el.value < 15) el.value = 15;
			}else if(type=="credits"){
				if(el.value < 5) el.value = 5;
			}
			return el.value;
		} else{
			
			return 0;
		}
	}
	var getTotal = function(){
		if(plan.uses){
			// LAUDNRY
			var pounds_per_month = (plan.pounds*plan.uses);
			var wdf_subtotal = pounds_per_month*price_per_pound;
			var wdf_percent = wdfDiscount(plan.pounds);
			var wdf_discount = wdf_percent*wdf_subtotal;
			var wdf_total = wdf_subtotal-wdf_discount;
			
			// SPECIAL CARE
			var credits_per_month = (plan.credits*plan.uses);
			var sc_subtotal = (credits_per_month*price_per_credit);
			var sc_percent = scDiscount(plan.credits);
			var sc_discount = sc_percent*sc_subtotal;
			var sc_total = (sc_subtotal-sc_discount);
			
			return {'before':(wdf_subtotal+sc_subtotal).toFixed(2),'discount':(wdf_discount+sc_discount).toFixed(2),'after':(wdf_total+sc_total).toFixed(2),'wdf_percent':(wdf_percent*100).toFixed(1),'sc_percent':(sc_percent*100).toFixed(1)};
		} else{
			return {'before':'0.00','discount':'0.00','after':'0.00','wdf_percent':'0.00','sc_percent':'0.00'};
		}
	}
	var showTotal = function(){
		var total = getTotal();
		document.getElementById('usage_total').innerHTML = (total.after/plan.uses).toFixed(2);
		document.getElementById('total').innerHTML = total.after;
		document.getElementById('discount').innerHTML = total.discount;
		//document.getElementById('percent').innerHTML = "("+total.wdf_percent+"%,"+total.sc_percent+"%)";
	}
	this.set = function(prop,el,num){
		plan[prop] = num;
		if(last_els[prop]) removeClassName(last_els[prop],'selected');
		addClassName(el,'selected');
		last_els[prop] = el;
		if($('pp_'+prop)) $('pp_'+prop).value = num;
		showTotal();
	}
	this.hardSet = function(prop,num){
		var div = $('plan_'+prop);
		for(var i=0,els=div.getElementsByTagName('a'),el;i<els.length;i++){
			el=els[i];
			if(parseInt(el.innerHTML) == parseInt(num)){	this.set(prop,el,num); return;}
		}
		el.lastChild.value=num;
		this.set(prop,el,num);
	}
	
	// FOR MARKETING->PLANS
	this.grabTotal = function() {
		return getTotal();
	}
	this.varSet = function(prop,val) {
		plan[prop] = val;
	}
}

