var CarbonCalc = function () {
	var ERRS = [];
	
	function add_commas (nStr) {
		nStr += '';
		x = nStr.split('.');
		x1 = x[0];
		x2 = x.length > 1 ? '.' + x[1] : '';
		var rgx = /(\d+)(\d{3})/;
		while (rgx.test(x1)) {
			x1 = x1.replace(rgx, '$1' + ',' + '$2');
		}
		return x1 + x2;
	}
	
	function listErrors () {
		var eList = '';
		ERRS.each(function(e) { eList += '<li>' + e + '</li>'; });
		ERRS = [];
		return '<ol class="warning">' + eList + '</ol>';
	}
	
	var pub = {
		calcBeds : function () {
			var beds = parseInt(document.getElementById('NumBeds').value);
			var fName = document.getElementById('FacilityName').value;
			var fZip = document.getElementById('PCZip').value;
			var results = '<p class="warning">Please enter a number.</p>';
			
			if (fName == '' || fName == 'Enter your name here') { ERRS.push('Please enter your facility\'s name.'); }
			if (fZip == '' || fZip == '5-Digit Zip Code') { ERRS.push('Please enter your facility\'s zip code.'); }
			if (isNaN(beds)) { ERRS.push('Please enter number of beds.'); }
			
			if (ERRS.length > 0) {
				results = listErrors();
			} else {
				var aloc=beds*1.65;
				var gal2=aloc*0.1;
				var gal3=aloc*0.1;
				var gal4=aloc*0.6;
				var gal8=aloc*0.15;
				var gal17=aloc*0.05;
				var gal2w=gal2*0.65;
				var gal3w=gal3*0.4;
				var gal4w=gal4*0.4;
				var gal8w=gal8*0.78;
				var gal17w=gal17*1.02;
				var gal2l=(gal2w*52)*1.2;
				var gal3l=(gal3w*52)*1.8;
				var gal4l=(gal4w*52)*2;
				var gal8l=(gal8w*52)*3.5;
				var gal17l=(gal17w*52)*5;
				var lbsplastic=gal2l+gal3l+gal4l+gal8l+gal17l;
				var lbscardboard=(((gal2w*52)+(gal3w*52)+(gal4w*52))/10*1.6)+(((gal8w*52)/6)*1.6)+(((gal17w*52)/6)*1.6);
				var lbsCO2=((lbscardboard/2000)*604.5)+(lbsplastic*0.57);
				var galsgas=lbsCO2/19.4202454;
				var propanecyl=lbsCO2/52.868476;
				
				results = '<div class="savings">\
				<h2>By using Stericycle\'s Sharps Management Service featuring Bio Systems reusable sharps containers, your facility\'s annual carbon savings would be:</h2>\
				<dl class="first"><dt>Pounds of CO<sub>2</sub> emissions prevented</dt><dd>' + add_commas(Math.round(lbsCO2)) + '</dd></dl>\
				<dl><dt>Pounds of plastic kept out of landfills</dt><dd>' + add_commas(Math.round(lbsplastic)) + '</dd></dl>\
				<dl><dt>Pounds of cardboard kept out of landfills</dt><dd>' + add_commas(Math.round(lbscardboard)) + '</dd></dl>\
				<dl><dt>CO<sub>2</sub> emissions prevented equal to this many gallons of gasoline</dt><dd>' + add_commas(Math.round(galsgas)) + '</dd></dl>\
				<dl><dt>CO<sub>2</sub> emissions prevented equal to not using this many tanks propane gas for home barbecues</dt><dd>' + add_commas(Math.round(propanecyl)) + '</dd></dl>\
				</div>';
			}		
			
			$('prospectCarbonSavings').down('div.results').update(results);
			$('prospectCarbonSavings').down('div.printIt').show();
			return false;
		},
		
		doCarbonCalc : function () {
			var c = document.getElementById('CCNumber').value;
			var z = document.getElementById('CCZip').value;
			var y = document.getElementById('CCYear').value;
			
			if (isNaN(c)) { ERRS.push('Please enter your Customer Number.'); }
			if (z == '' || z == '5-Digit Zip Code') { ERRS.push('Please enter your Zip Code.'); }
			if (y == '') { ERRS.push('Please choose a year.'); }
			
			if (ERRS.length > 0) {
				$('customerCarbonSavings').down('div.results').update(listErrors());
			} else {
				var ajaxScript = 'ajax/carbonCalc.php';
				var ajax_params = {'CustomerNum':c,'Zip':z,'Year':y};
				var ajax_opts = {method:'get',
					parameters:ajax_params,
					onSuccess: function(ajax_resp) {
						$('customerCarbonSavings').down('div.results').update(ajax_resp.responseText);
						$('customerCarbonSavings').down('div.printIt').show();
					}
				}
				new Ajax.Request(ajaxScript,ajax_opts);
			}
			return false;
		},
		
		fieldSwap : function (el,val,typ) {
			switch (typ) {
			case 'foc' :
				if (el.value == val) { el.value = ''; }
				break;
			case 'blr' :
				if (el.value == '') { el.value = val; }
				break;
			}
		},
		
		printIt : function (cType) {
			var cZip = '',cName = '',vals = '',EL = null;
			var params = [];
			
			switch (cType) {
			case 'prospect' :
				EL = $('prospectCarbonSavings');
				break;
			case 'current' :
				EL = $('customerCarbonSavings');
				break;
			}
			
			EL.down('div.results').down('div.savings').select('dd').each(function (v) { vals += '|' + v.innerHTML; } );
			params.push('vals=' + vals.substr(1));
			
			switch (cType) {
			case 'prospect' :
				cZip = $('PCZip').value;
				cName = $('FacilityName').value;
				break;
			case 'current' :
				cZip = EL.down('div.company').down('span.zip').innerHTML;
				cName = EL.down('div.company').down('span.title').innerHTML;
				params.push('Year=' + $('CCYear').value);
				break;
			}
			
			params.push('cType=' + cType);
			params.push('cName=' + cName);
			params.push('cZip=' + cZip);
			
			var winURL = 'carbonFootprintPrintout/index.php?' + params.join('&');
			
			//alert(winURL);
			window.open(winURL,'printfriendly','width=600px,height=800px,scrollbars,resizable');
		}

	}
	return pub;
}();