// NativeEnergy Simple Carbon Footprint Calculator


var Calculator = {
	total:0,
	p_wind:50,
	p_meth:50,
	increment:function(which){
		var v = parseInt(document.forms["calculations"].elements[which].value); 
				document.forms["calculations"].elements[which].value = (v + 1);
		Calculator.calc();		
	},
	decrement:function(which){			
		var v = parseInt(document.forms["calculations"].elements[which].value); 
			if(v == 0) return false;
			document.forms["calculations"].elements[which].value = (v - 1);					  
		Calculator.calc();
	},
	manual:function(which, value){
		document.forms["calculations"].elements[which].value = value;				  
		Calculator.calc();
	},
	calc:function(){
		//total up air
		var a  = parseInt(document.forms["calculations"].elements["air"].value); 
		if(isNaN(a)){a=0};
		var at = ((a * .4472 * 2) / 2000);
		
		//total up cars
		var c  = parseInt(document.forms["calculations"].elements["cars"].value); 
		if(isNaN(c)){c=0};
		var ct = (((c/21.1) * 19.56) / 2000);
		
		//update overall total and prices
		Calculator.total = (at + ct);
		if(Calculator.total<1&&Calculator.total!=0){
			Calculator.total=1;	
		}
		Calculator.total = Math.round(Calculator.total);
		//alert(Calculator.total);	
		document.getElementById("totaltons").value = Calculator.total;
		document.getElementById("grand_total").innerHTML = Calculator.total + "<span class=\"tonsText\">tons</span>";
			
		var yr_price = Calculator.total * 14;
		document.getElementById("price_yr").innerHTML = Calculator.dollar_format(yr_price);
		
	},
	manual_update:function(val, tons){
		Calculator.total = tons ? val : Math.ceil(parseInt(val)/2000);
		var yr_price = Calculator.total * 14;
		document.getElementById("price_yr").innerHTML = Calculator.dollar_format(yr_price);
	},
	offset_year:function(){
		var OneTime = document.forms["OneTime"];
		
		var wind_qty = Math.floor(Calculator.total * (Calculator.p_wind/100));
		var meth_qty = Calculator.total - wind_qty;	
			
			document.getElementById("ot_wind_qty").value = Calculator.total; //wind_qty;
			
			OneTime.submit();
	},
	offset_month:function(){
		var Monthly = document.forms["Monthly"];
				
		var wind_qty = Math.floor(Calculator.total * (Calculator.p_wind/100));
		var meth_qty = Calculator.total - wind_qty;	
			
		document.getElementById("mn_wind_qty").value = Calculator.total; //wind_qty;
			//document.getElementById("mn_farm_qty").value = meth_qty;
		
		Monthly.submit();
	},
	set_blend:function(slider){
		var p_meth = Math.floor(slider.value);
		var p_wind = 100 - p_meth;
			if(p_meth == 1){ 
				p_wind = 100;
				p_meth = 0;
			}
		document.getElementById("pc_wind").innerHTML = p_wind + '%';
		document.getElementById("pc_meth").innerHTML = p_meth + '%';
		
		Calculator.p_wind = p_wind;
		Calculator.p_meth = p_meth;
		
	},
	dollar_format:function(num, nosign){
		num = num.toString().replace(/\$|\,/g,'');
		if(isNaN(num))
		num = "0";
		sign = (num == (num = Math.abs(num)));
		num = Math.floor(num*100+0.50000000001);
		cents = num%100;
		num = Math.floor(num/100).toString();
		if(cents<10)
		cents = "0" + cents;
		for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
		num = num.substring(0,num.length-(4*i+3))+','+
		num.substring(num.length-(4*i+3));
		var ds = nosign ? '' : '$';
		return (((sign)?'':'-') + ds + num + '.' + cents);	
	}
}

