$(document).ready(function() {
    $(".hh_question").hide();
    $("#hh_question_1").show();
    $("#hh_q1").submit(function() {
        if ($(".hh_q1_answer:checked").val() == 'yes') { hh_continue(2); }
        else { hh_ineligible(''); }
	return false;
    });
    $("#hh_q2").submit(function() {
 	today = new Date;
	bd = new Date(parseInt($(".hh_q2_year").val()), parseInt($(".hh_q2_month").val())-1, parseInt($(".hh_q2_day").val()));
	
	bddiff = today.getTime() - bd.getTime();
	bddiff = parseFloat(bddiff) / 1000 / 86400 / 365;
	
        if (bddiff > 63) { hh_ineligible(2); }
        else if (bddiff < 19) { hh_ineligible(2); }	
	else { hh_continue(3); }
	return false;
    });
    $("#hh_q3").submit(function() {
        if ($(".hh_q3_answer:checked").val() == 'no') { 
		hh_continue(4); 
	}
	else if ($(".hh_q3b_answer:checked").val() == 'yes') {
		hh_continue(4); 		
	}
        else { hh_ineligible(3); }
	return false;
    });    
    $("#hh_q4").submit(function() {
        if ($(".hh_q4_answer:checked").val() == 'yes') { hh_continue(5); }
        else { hh_ineligible(4); }
	return false;
    });    
    $("#hh_q5").submit(function() {
    	income = $(".hh_q5_income").val();
	income = income.split('.');
	income = parseInt(income[0].replace(/[^\d]/g, ""));
	family = parseInt($(".hh_q5_family").val().replace(/[^\d]/g, ""));

	if ($(".hh_q5_employed:checked").val() == 'yes') {
		income = income / 2;
	}
	else {
		hh_ineligible(5);
	}

	switch(family) {
		case 1:
			income_high = 2708;
			income_low = 1047;
		break;
		case 2:
			income_high = 3643;
			income_low = 1408;
		break;
		case 3:
			income_high = 4578;
			income_low = 1770;
		break;
		case 4:
			income_high = 5513;
			income_low = 2132;
		break;
		case 5:
			income_high = 6448;
			income_low = 2493;
		break;		
		case 6:
			income_high = 7383;
			income_low = 2855;
		break;				
	}

	if (income >= income_low) {
		if (income <= income_high) {
			hh_eligible();
		}
		else {
			hh_ineligible(5);
		}
	}
	else {
		hh_ineligible(5);
	}

	return false;
    });    
});

function hh_continue(question) {
	$(".hh_question").hide();
	$("#hh_question_" + question).show();
}
function hh_ineligible(page) {
	document.location = 'apply/ineligible' + page;
}
function hh_eligible() {
	document.location = 'apply/form';
}

