
/* tools/grass_seed_calculator.js */

 var MMH;
if (typeof MMH === 'undefined') {
	MMH = {};
}
(function () {
	MMH.Calculator.GrassSeed = {
		init: function() { 			
			/* Load the params, if any */
			var params =location.href.toQueryParams();
			if(params.area && params.area != $('yard_area').title) {
				$('yard_area').value = params['area'].replace("%20"," ");
				$('yard_area').removeClassName('prompt_text');
			} if(params.length && params.length != $('yard_length').title) {
				$('yard_length').value = params['length'].replace("%20"," ");
				$('yard_length').removeClassName('prompt_text');
			} if(params.width && params.width != $('yard_width').title) {
				$('yard_width').value = params['width'].replace("%20"," ");
				$('yard_width').removeClassName('prompt_text');
			}
			new MMH.Modal("zone_map_link", { contentElement:'zone_map', overlayStyles:'opacity:.5;' });
		},
		validationRules: { 
			js_validate_area: { is_float: true, is_present: true, message: 'Please enter a length and width in feet or total area in square feet' }
		},
		calculate: function() {
			$('square_feet_amount').innerHTML = $('yard_area').value;
			var lawnSunValue;
			var zoneValue;
			var historyValue;
			var seedHash = [];
			var seedWeight = " lbs"
			grassHash = MMH.Calculator.GrassSeed.generateHash()
			var form = $('calculator_form');
			
			var radioZoneValue = form.getInputs('radio','lawn_zone');
			radioZoneValue.each(function(radioZone) {
				if (radioZone.checked) zoneValue = radioZone.value
			})
			
			var radioLawnSun = form.getInputs('radio','lawn_sun');
			radioLawnSun.each(function(lawnSun) {
				if (lawnSun.checked) lawnSunValue = lawnSun.value
			})
			
			var lawnHistory = form.getInputs('radio','lawn_history');
			lawnHistory.each(function(history) {
				if (history.checked) historyValue = history.value
			})
			
			grassHash.each(function(grassItem){
				if ((grassItem.sunShade == lawnSunValue || grassItem.sunShade == 'all') && grassItem.zone == zoneValue){

					seedRate = (grassItem.seedRate*parseInt($('yard_area').value))/1000
					seedRateHigh = (grassItem.seedRateHigh*parseInt($('yard_area').value))/1000 ;

					if (historyValue == "existing"){
						seedRate = seedRate*.75;
						seedRateHigh = seedRateHigh*.75;
					}
					if(seedRate === seedRateHigh){
						seedRate = Math.round(seedRate);
						if(seedRate <= 1) seedRate = 1;
						if(seedRate == 1) seedWeight = " lb"
					}else{
						if(seedRate <= 1) seedRate = 1;
						if(seedRateHigh <= 2) seedRateHigh = 2;
						seedRate = Math.round(seedRate) + "&#8211;" + Math.round(seedRateHigh);
					}
					$("seed_results").innerHTML += "<li>"+ grassItem.grassType+": "+seedRate+seedWeight+"</li>";
					seedHash.push({seedRate: seedRate,grassType: grassItem.grassType});
				}
			});
			if (seedHash.length <= 0){
				$("result_content").innerHTML = "Please make sure all options above are checked.";
			}
			$('form_buttons').hide();
			$('result').show();
		},
		generateHash: function () {
			var grassHash = [
				{ grassType: 'Centipede', seedRate: .25 , seedRateHigh: .25, sunShade: 'sun' , zone: 'south' },
				{ grassType: 'Kentucky blue, common', seedRate: 1 , seedRateHigh: 3, sunShade: 'sun' , zone: 'north' },
				{ grassType: 'Kentucky blue, common', seedRate: 1, seedRateHigh: 3, sunShade: 'sun' , zone: 'transitional' },
				{ grassType: 'Kentucky blue, improved', seedRate: 1, seedRateHigh: 2, sunShade: 'sun', zone: 'north' },
				{ grassType: 'Kentucky blue, improved', seedRate: 1 , seedRateHigh: 2, sunShade: 'sun' , zone: 'transitional' },
				{ grassType: 'Fescue, tall', seedRate: 6 , seedRateHigh: 9, sunShade: 'sun' , zone: 'north' },
				{ grassType: 'Fescue, tall', seedRate: 6 , seedRateHigh: 9, sunShade: 'shade' , zone: 'transitional' },
				{ grassType: 'Fescue, chewings red', seedRate: 3 , seedRateHigh: 5 , sunShade: 'shade' , zone: 'north' },
				{ grassType: 'Fescue, creeping red', seedRate: 3, seedRateHigh: 5 , sunShade: 'all' , zone: 'north' },
				{ grassType: 'Buffalo', seedRate: 3 , seedRateHigh: 3, sunShade: 'sun' , zone: 'all' },
				{ grassType: 'Bermuda, common', seedRate: 1, seedRateHigh: 2 , sunShade: 'sun' , zone: 'south' },
				{ grassType: 'Bermuda, common', seedRate: 1, seedRateHigh: 2 , sunShade: 'sun' , zone: 'transitional' },
				{ grassType: 'Zoysia', seedRate: 1, seedRateHigh: 2 , sunShade: 'all' , zone: 'south' },
				{ grassType: 'Zoysia', seedRate: 1, seedRateHigh: 2 , sunShade: 'sun' , zone: 'transitional' },
				{ grassType: 'Mix of Kentucky blue, creeping red fescue and perennial rye (1:1:1)', seedRate:6 , seedRateHigh: 7 , sunShade: 'mixed' , zone: 'north' },
				{ grassType: 'Mix of Kentucky blue and tall fescue (1:1)', seedRate: 7 , seedRateHigh: 7, sunShade: 'mixed' , zone: 'north' },
				{ grassType: 'Mix of Kentucky blue and tall fescue (1:1)', seedRate: 7 , seedRateHigh: 7, sunShade: 'mixed' , zone: 'transitional' },
				{ grassType: 'Mix of Kentucky blue and creeping red fescue ( 1:1)', seedRate: 3, seedRateHigh: 5 , sunShade: 'mixed' , zone: 'north' },
				{ grassType: 'Mix of Kentucky blue and perennial rye (9:1)', seedRate: 5 , seedRateHigh: 5, sunShade: 'sun' , zone: 'north' },
				{ grassType: 'Mix of Kentucky blue and perennial rye (9:1)', seedRate: 5 , seedRateHigh: 5, sunShade: 'sun' , zone: 'transitional' },
				{ grassType: 'Mix of Perennial rye, Kentucky blue and red fescue (2:1:1)', seedRate: 4, seedRateHigh: 5 , sunShade: 'mixed' , zone: 'north' },
				{ grassType: 'Mix of Perennial rye, Kentucky blue and red fescue (2:1:1)', seedRate: 4, seedRateHigh: 5 , sunShade: 'mixed' , zone: 'transitional' }
			];
			return grassHash;
		}
	};
})();
