
  function addCalculationEvents(){
 	var cboxs= $$(".checkbox");
 	if(cboxs){
 		for(c in cboxs){
 			var cbox=cboxs[c];
 			if(cbox.id){
 				Event.observe(cbox,"click",doCalculation);

 				var act=cbox.id.split("_");
				var num = act[1];
				var cqty=$("qty_option_"+num);
				if(cqty){
					Event.observe(cqty,"change",doRecalculation);
					Event.observe(cqty,"keypress",noenter);
//					Event.observe(cqty,"keypress",doRecalculation);
				}
 				var cqtyX=$("qty_option_"+num+"_x");
				if(cqtyX){
					Event.observe(cqtyX,"change",doRecalculation);
					Event.observe(cqtyX,"keypress",noenter);
//					Event.observe(cqtyX,"keypress",doRecalculation);
				}
 				var cqtyY=$("qty_option_"+num+"_y");
				if(cqtyY){
					Event.observe(cqtyY,"change",doRecalculation);
					Event.observe(cqtyY,"keypress",noenter);
//					Event.observe(cqtyY,"keypress",doRecalculation);
				}
 				if(cbox.checked){
 					calculateSaving(cbox);
 				}
 			}
 		}
 	}
	var custom_boxs= $$(".checkbox2");
 	if (custom_boxs) {
		for (c in custom_boxs) {
			var cbox = custom_boxs[c];
			if (cbox.id) {
				//Event.observe(cbox,"click",doCalculation);

				var act = cbox.id.split("_");
				var num = act[1];
				var ctxt = $("text_option_" + num);
				if (ctxt) {
					Event.observe(ctxt, "change", Function('setCheckBox('+num+');'));
					Event.observe(ctxt, "change", doRecalculation);
					Event.observe(ctxt, "keypress", noenter);
				}
				var cco2 = $("qty_option_" + num);
				if (cco2) {
					Event.observe(cco2, "change", Function('setCheckBox('+num+');'));
					Event.observe(cco2, "change", doRecalculation);
					Event.observe(cco2, "keypress", noenter);
				}
			}
		}
	}
 }

 function doCalculation(event){
 	var cb = Event.element(event);
	calculateSaving(cb);
 }
 function doRecalculation(event){
 	var cqty = Event.element(event);

 	var act=cqty.id.split("_");

	var num = act[2];

	setCheckBox(num);
	calculateSaving($("action_"+num));

 }
 function setCheckBox(num){
 	if($("action_"+num)){

		if((($("qty_option_"+num)&&$("qty_option_"+num).value!='')
		||($("qty_option_"+num+"_x")&&$("qty_option_"+num+"_x").value!='')
		||($("qty_option_"+num+"_y")&&$("qty_option_"+num+"_y").value!=''))
		&&(!$("text_option_"+num)||$("text_option_"+num).value!='')
		){
			$("action_"+num).checked=true;
		}else{
			$("action_"+num).checked=false;
		}
	}
 }

 var $$$id = document.getElementById;
 function calculateSaving(cb){
 	var act=cb.id.split("_");

	var num = act[1];
	var co2_each=0;
	var cost_each=0;
	var qty=0;

	if(cb.checked){
		var qty=1;
		if(document.getElementById("qty_option_"+num)){
			var qty=fixNaN(parseFloat($("qty_option_"+num).value),0);
		}
		if($("qty_option_"+num+'_x')){
			var qty=fixNaN(parseFloat($("qty_option_"+num+"_x").value),1)*fixNaN(parseFloat($("qty_option_"+num+"_y").value),1);
		}

		if($("qty_option_"+num)&&$("qty_option_"+num).value<=0){
			$("qty_option_"+num).value=1
		}
		if($("qty_option_"+num+'_x')&&$("qty_option_"+num+"_x").value<=0){
			$("qty_option_"+num+'_x').value=1
		}
		if($("qty_option_"+num+'_y')&&$("qty_option_"+num+"_y").value<=0){
			$("qty_option_"+num+'_y').value=1
		}


		if(qty<=0){
			qty=1;
		}
		if($("co2_each_"+num)){
			co2_each = fixNaN(parseFloat($("co2_each_"+num).innerHTML),0);
		}
		if($("cost_each_"+num)){
			cost_each = fixNaN(parseFloat($("cost_each_"+num).innerHTML),0);
		}


	}
	if($("co2_savings_"+num)){
		$("co2_savings_"+num).innerHTML=Math.round(co2_each * qty);
	}
	if($("cost_savings_"+num)){
		var cs=cost_each * qty;
		$("cost_savings_"+num).innerHTML=cs.toFixed(2);
//		$("cost_savings_"+num).innerHTML=Math.round(cost_each * qty*100)/100;
	}
	recalculateTotals3();
 }


var household=null;
var vehicles=null;
var flights=null;
var custom_items=null;
var cost_savings=null;

 function getDivs(){
	household = $$('.co2_savings');
	vehicles= $$('.co2_savings_v');
	flights= $$('.co2_savings_flights');
	cost_savings= $$('.cost_savings');
	custom_items= $$('.custom_co2_savings');
	recalculateTotals3();
 }


 function recalculateTotals3(){
	var vtot=0;
	var htot=0;
	var ftot=0;
	var ctot=0;
	var cs_tot=0;

	var h_results=$("co2_results_household_div");
	var i_h_results=$("co2_results_household_input");

	var v_results=$("co2_results_vehicles_div");
	var i_v_results=$("co2_results_vehicles_input");

	var f_results=$("co2_results_flights_div");
	var i_f_results=$("co2_results_flights_input");

	var c_results=$("co2_results_custom_div");
	var i_c_results=$("co2_results_custom_input");

	var co2_results=$("co2_results_div");
	var i_co2_results=$("co2_results_input");

	var cost_results=$("cost_results_div");
	var i_cost_results=$("cost_results_input");

	for (h in household){
		if(household[h].style)
			htot+=fixNaN(parseFloat(household[h].innerHTML),0);
		//household[h].style.background='#AAFFAA';
	}
	h_results.innerHTML=htot;
	i_h_results.value=htot;

	for (v in vehicles){
		if(vehicles[v].style)
			vtot+=fixNaN(parseFloat(vehicles[v].innerHTML),0);
			//vehicles[v].style.background='#FFAAAA';
	}
	v_results.innerHTML=vtot;
	i_v_results.value=vtot;

	for (f in flights){
		if(flights[f].style)
		ftot+=fixNaN(parseFloat(flights[f].innerHTML),0);
//		flights[f].style.background='#AAAAFF';
	}
	f_results.innerHTML=ftot;
	i_f_results.value=ftot;

	for(cc in custom_items){
		if (custom_items[cc].style) {
			var addThis=fixNaN(fixMaxVal(custom_items[cc], 10000), 0);
			if(addThis==0){
				addThis=fixNaN(parseFloat(custom_items[cc].innerHTML),0);
			}
			ctot += addThis;
		}
	}
	c_results.innerHTML=ctot;
	i_c_results.value=ctot;

	co2_results.innerHTML=htot+vtot+ftot+ctot;
	i_co2_results.value=htot+vtot+ftot+ctot;

	for (c in cost_savings){
		if(cost_savings[c].style)
		cs_tot+=fixNaN(parseFloat(cost_savings[c].innerHTML),0);
	}
	cs_tot=cs_tot.toFixed(2);
	cost_results.innerHTML=cs_tot;
	i_cost_results.value=cs_tot;
 }

function fixMaxVal(elem,mval){
	var v= parseFloat(elem.value.replace(/,/,''));
	if(v>mval){
		alert("Please write a value lower than "+mval);
		v=elem.value=mval;
		elem.focus();
	}
	return v;
}

