
$(document).ready(function() {

	$('.form_submit').click(function() {

		var isValid = true;
		$('input.required').each(function() {

			if ($(this).attr('value') == '' || $(this).attr('value') == null) {
				isValid = false;
				alert('You are missing required field(s). Please try again.');
				$(this).focus();
				return false;
			}

		});
		
		$('input.greaterthanzero').each(function() {
			
			var value = $(this).attr('value');
			if(!isInt(value))
			{
				isValid = false;
				alert('Illegal value.');
				$(this).focus();
				return false;
			}
			
			if(value < 1)
			{
				isValid = false;
				alert('There must be at least 1 adult.');
				$(this).focus();
				return false;
			}
			
		});

		if (!isValid)
			return false;

		$('#prescreenappform').submit();
		return false;

	});

	$('.calculate_submit').click(function() {

		var totalIncome = 0;
		var totalMonthlyIncome = 0;

		$('.incometable input').each(function() {

			var value = $(this).attr("value");
			value = value.replace(/[^0-9.]/g, '');
			$(this).parent('td').next('td').text("$" + (value * 12));
			totalIncome += (value * 12);
			totalMonthlyIncome += (value * 1);
		});

		$(".incometable .totalMonthlyIncome").text("$" + totalMonthlyIncome);
		$(".incometable .totalIncome").text("$" + totalIncome);

		return false;

	});
});

function isInt(x) {
	var y = parseInt(x);
	if (isNaN(y))
		return false;
	return x == y && x.toString() == y.toString();
}
