$(document).ready(function() {
	var red = '#a30000';
	
	$('#calc').find('input').each(function() {
		if ($(this).attr('id') != 'answer') {
			
			$(this).blur(function() {
				if ( jQuery.trim($(this).val()) == '' ) {
					$(this).css({'background-color' : red});
				} else {
					$(this).css({'background-color' : '#fff'});
				}
			})
			
		}
	});

	$('#reset').click(function () {
		$('#calc').find('input').each(function() {
			$(this).val('');
			$(this).css({'background-color' : '#fff'});
		});
	});
	
	$('#calculate').click(function() {

		// Check each box has a value.
		$('#calc').find('input').each(function() {
			if ( jQuery.trim($(this).val()) == '' && $(this).attr('id') != 'answer' ) {
				alert('You must enter a value for: ' + $(this).attr('id'));
				$(this).css({'background-color' : red});
				return false;
			}
		});
		
		// Now check that their Numeric. 
		$('#calc').find('input').each(function() {
			if ( isNumeric(jQuery.trim($(this).val())) == false && $(this).attr('id') != 'answer' ) {
				alert('You must enter a NUMERICAL value for: ' + $(this).attr('id'));
				$(this).css({'background-color' : red});
				return false;
			}
		});
		
		//Everything Checks out, do the math!
		var len = $('#length').val() / 3;
		var wid = $('#width').val() / 3;
		
		//depth is in inches
		var dep = $('#depth').val() / 36;
		
		$('#answer').val( len * wid * dep );
	});
	
});

// Name pretty much sums it up.
function isNumeric(form_value) { 
    if (form_value.match(/^\d+$/) == null) 
        return false; 
    else 
        return true; 
}
