﻿jQuery(document).ready(function() {
	$(".about").click(function() {
		$(".aboutSlide").slideToggle();
		$(".about").toggleClass("open");
		if ($(".about").hasClass("open")) {
			$(".about").html("Close");
		}
		else {
			$(".about").html("About this Tool");
		}
		

		$("#costAreaTip").tooltip();


	});

	$("#popOutLink").click(function() {
	 $("#calcPopOut").slideDown();
	});
	
	$("#tablesLink").click(function() {
	 $("#incomeAgeTables").toggle();
	 $("#incomeAgeTables").scrollTo();	 
	});
});

function updateForm(element) {
	recalc_onclick(element);
	$("#map_income_type").text("Enter annual income (" + jQuery.trim($("#income_type").val()) + ")"); 
	$("#map_Exchange_note").text($("#Exchange_note").val());
	$("#map_Proj_income_poverty").text($("#Proj_income_poverty").val() + " of poverty");
	$("#map_Age_note").text($("#Age_note").val());
	$("#map_person_premium_note").text($("#person_premium_note").val());
	$("#map_govt_premium_note").text($("#govt_premium_note").val());
	$("#map_OOP_note").text($("#OOP_note").val()); 
	
	if($('#Income_2010_poverty').val() != ''){
		$('#btnIncomeUse').show();
	}
	else{
		$('#btnIncomeUse').hide();
	}
	if($("#ESI_available").val() == 'Yes'){
		$(".resultsTable").hide();
	}
	else{
		$(".resultsTable").show();
	}
}

function calculateClick() {
	var success = validateForm();
	if (success) {
		$('.addRes').show();
		$('.notes').show();
		$('#calcResults').show();
		$('.povertyPopupOut').hide();
		$(".aboutSlide").slideUp();
		$(".about").removeClass("open");
		$(".about").html("About this Tool");

	}
	else{
		$('.addRes').hide();
		$('.notes').hide();
		$('#calcResults').hide();
		$('.povertyPopupOut').show();
		$("#incomeAgeTables").hide();
	}

	return false;
};

function clearClick() {
	$('.calc-wrap input[type=text]').each(function() {
		$(this).val('');
	});
	$('calc-wrap select').each(function() {
		$(this).val('');
	});

	$('.addRes').hide();
	$('.notes').hide();
	$('#calcResults').hide();
	$('.povertyPopupOut').show();
	$("#calcPopOut").hide();
	$("#incomeAgeTables").hide();
	return false;
};

function useCalculatedIncome() {
	$('#income_type_selected').val('% of poverty');
	$('#Entered_income').val($('#Income_2010_poverty').val());
	updateForm('income_type_selected');
	return false;
};

var checkingForm = false;

function validateForm() {
	var income = $("#Entered_income").val();
	var age = $("#Entered_age").val();
	var result = true;

	checkingForm = true;

	if (income == '' || income < 0) {
		$("#valIncome").show();
		$("#Entered_income").val(savedValue);
		result = false;
	}
	else
	{
		$("#valIncome").hide();
	}

	if (age < 19 || age > 64) {
		if (savedValue < 19) {
			$("#Entered_age").val(19);
		}
		else {
			$("#Entered_age").val(savedValue);
		}
		$("#valAge").show();
		result = false;
	}
	else{
		$("#valAge").hide();
	}
	checkingForm = false;
	return result;
};

function checkForPositiveNumber(obj, alertId) {
	if (!checkingForm) {
		var parsedValue = obj.value;
		if (isNaN(parsedValue.split(",").join("")) || parsedValue < 0) {
			obj.value = savedValue;
			$('#' + alertId).show();
			return false;
		}
	}
	else{
		$('#' + alertId).hide();
	}
	return true;
};

function checkForPositiveFloat(obj, alertId) {
	if (!checkingForm) {
		var parsedValue = obj.value;
		if (isNaN(parsedValue.split(",").join("")) || parsedValue < 0) {
			$('#' + alertId).show();
			obj.value = savedValue;
			return false;
		}
		else{
			$('#' + alertId).hide();
		}
	}
	return true;
};

function checkAge(obj, alertId) {
	if (!checkingForm) {
		if (!checkForPositiveNumber(obj, alertId)) {
			obj.value = savedValue;
			return false;
		}
		if (obj.value < 19 || obj.value > 64) {
			$('#' + alertId).show();
			if (savedValue < 19) {
				obj.value = 19;
			}
			else {
				obj.value = savedValue;
			}
			return false;
		}
			else{
		$('#' + alertId).hide();
	}
	}
	return true;
};

var savedValue;

function saveValue(obj) {
	savedValue = obj.value;
};


