
/* tools/exterior_paint_calculator.js */

 var MMH;
if (typeof MMH === 'undefined') {
	MMH = {};
}
(function () {
	MMH.Calculator.ExteriorPaint = {
		init: function() { 
			/* Load the params, if any */
			var params =location.href.toQueryParams();
			if(params.length && params.length != $('length').title) {
				$('length').value = params['length'].replace("%20"," ");
				$('length').removeClassName('prompt_text');
			} if(params.width && params.width != $('width').title) {
				$('width').value = params['width'].replace("%20"," ");
				$('width').removeClassName('prompt_text');
			} if(params.height && params.height != $('height').title) { 
				$('height').value = params['height'].replace("%20"," ");
				$('height').removeClassName('prompt_text');
			} if(params.windows) { 
				$('windows').value = params['windows'].replace("%20"," ");
			} if(params.doors) { 
				$('doors').value =params['doors'].replace("%20"," ");
			}
		},
		validationRules: { 
			js_validate_dimension: { is_float: true, is_present: true, message: 'Please enter a length, width, and height in feet' }
		},
		calculate: function() { 
			WINDOW_HEIGHT = 3;
			WINDOW_LENGTH = 4;
			DOOR_HEIGHT = 7;
			DOOR_LENGTH = 3;
			SMOOTH_WALL = 400;
			ROUGH_WALL = 250;
			
			/* Calculations */
			surface = 2 * (parseInt($('width').value) + parseInt($('length').value)) * parseInt($('height').value);
			if(parseInt($('windows').value)) {
				window_surface = (WINDOW_HEIGHT * WINDOW_LENGTH) * parseInt($('windows').value); 
				surface = surface - window_surface;
			}
			if(parseInt($('doors').value)) {
				door_surface = (DOOR_HEIGHT * DOOR_LENGTH) * parseInt($('doors').value); 
				surface = surface - door_surface;
			}
			if($('rough').getValue()) { 
				cans_of_paint = surface / ROUGH_WALL
			} else { 
				cans_of_paint = surface / SMOOTH_WALL
			}
			cans_of_paint = Math.ceil(cans_of_paint);
			
			
			/* Update the UI */
			if(surface < 0) { 
				$('calculation_success').hide();
				$('calculation_error').show();
			} else { 
				$('square_feet_amount').innerHTML = surface;
				$('gallon_amount').innerHTML = cans_of_paint;
				$('calculation_success').show();
				$('calculation_error').hide();
			}
		}
	};
})();
