function getsstax(income){
	return(getrange(income,sstaxtable));
}

function getadjnetworth(networth){
	return(getrange(networth,networthtable));
}

function getpcaai(aai){
	return(getrange(aai,paaitable));
}

function getscaai(aai){
	return(getrange(aai,saaitable));
}

//function to get values from range type tables
function getrange(inval, inarray){
	var match = 0;
	var outvalinit = inarray[1][0];
	var outval = 0;
	inval = parseFloat(inval);

	for(i=1;i<inarray[0].length;i++)
	{
		if (inval > inarray[0][i])
		{
			match = 1;
			if (i<inarray[0].length)
			{
				if (inarray[0][i] < 0 && inarray[0][i+1] >=0)
					outval = outval + (inarray[1][i] * .01 * (inval > inarray[0][i+1]?inarray[0][i+1]:inval));
				else
					outval = outval + (inarray[1][i] * .01 * (inval > inarray[0][i+1]?(inarray[0][i+1] - inarray[0][i]):(inval - inarray[0][i])));
			}
			else
				outval = outval + (inarray[1][i] * .01 * (inval - inarray[0][i]));
		}
	}
	if (match == 1) return(roundnum(outval, 2));
	else return (roundnum(outvalinit,2));	
}

function getsandotax_dep_parent(state,income){
	return (getsandotax("PDS",state,income));
}

function getsandotax_inddep_student(state,income) //with dependents
{
	return (getsandotax("ISD",state,income));
}

function getsandotax_ind_student(state,income){
	return (getsandotax("IS",state,income));
}
function getsandotax_dep_student(state,income)
{
	return (getsandotax("DS",state,income));
}

//Get State and other tax
function getsandotax(status,state,income){
	var match = 0;
	income = parseFloat(income);
	taxbound = getparam("SandO_Tax_Bound");
	//alert(status + "::" + state + "::" + income);
	for (i=0;i<sandotable[0].length;i++)	{
		if (state == sandotable[0][i])	{
			match = 1;
			switch(status)	{
				case "PDS": 
					if (income < taxbound) percent = sandotable[1][i];
						else percent = sandotable[2][i];
					break;
				case "ISD":
					if (income < taxbound) percent = sandotable[3][i];
						else percent = sandotable[4][i];
					break;
				case "DS":
					percent = sandotable[5][i];
					break;
				case "IS":	
					percent = sandotable[6][i];
					break;
				default:
					break;
			}
		}
		if (match == 1) break;
	}
	sandotax = income * percent * .01
	if (sandotax < 0) sandotax = 0;
	//alert("Sandotax: " + sandotax);
	return (roundnum(sandotax, 2));
}

function getipa(pih,sih){
	var additional_family = 0;
	var additional_student = 0;

	pih = parseInt(pih);
	sih = parseInt(sih);
	if (pih > max_family) { additional_family = pih - max_family; pih = max_family; }
	if (sih > max_coll_stud) { additional_student = sih - max_coll_stud; sih = max_coll_stud; }
	ipa = ipatable[pih][sih];
	if (additional_family != 0 || additional_student != 0)	{
		ipa = ipa + additional_family * getparam("IPA_more_member_add") - additional_student * getparam("IPA_more_student_subtract");
	}
	return(roundnum(ipa,2));
}

function getisdipa(pih,sih){
	var isd_additional_family = 0;
	var isd_additional_student = 0;

	pih = parseInt(pih);
	sih = parseInt(sih);
	if (pih > isd_max_family) { isd_additional_family = pih - isd_max_family; pih = isd_max_family; }
	if (sih > isd_max_coll_stud) { isd_additional_student = sih - isd_max_coll_stud; sih = isd_max_coll_stud; }
	ipa = isdipatable[pih][sih];
	if (isd_additional_family != 0 || isd_additional_student != 0)	{
		ipa = ipa + isd_additional_family * getparam("ISD_IPA_more_member_add") - isd_additional_student * getparam("ISD_IPA_more_student_subtract");
	}
	return(roundnum(ipa,2));
}

function getassetprot_dep_parent(age,maritalstatus){
	return(getassetprot(age, "PDS", maritalstatus));	//maritalstatus can be "yes" or "no"
}

function getassetprot_ind_student(age,maritalstatus){
	return(getassetprot(age, "IS", maritalstatus));	//maritalstatus can be "yes" or "no"
}

function getassetprot_inddep_student(age,maritalstatus){
	return(getassetprot(age, "ISD", maritalstatus));	//maritalstatus can be "yes" or "no"
}

function getassetprot(age, status, maritalstatus){
	var asset_prot_allowance = 0;
	
	age = parseInt(age);
	
	if (age <= 25) age = 25;
	if (age >= 65) age = 65;
	
	switch (status){
		case "PDS":
			if (maritalstatus.toUpperCase() == "Y") asset_prot_allowance = edsavtable[1][age-25];
			else asset_prot_allowance = edsavtable[2][age-25];
			break;
		case "IS":
			if (maritalstatus.toUpperCase() == "Y") asset_prot_allowance = edsavtable[3][age-25];
			else asset_prot_allowance = edsavtable[4][age-25];
			break;
		case "ISD":
			if (maritalstatus.toUpperCase() == "Y") asset_prot_allowance = edsavtable[5][age-25];
			else asset_prot_allowance = edsavtable[6][age-25];
			break;
		default:
			break;
	}
	//alert(status + " :: " + maritalstatus + " :: " + asset_prot_allowance);
	return (roundnum(asset_prot_allowance,2));
}

function getparam(paramname){
	return gettableval(paramtable, paramname);
}

function getfielddesc(fieldname){
	return(gettableval(fieldtable, fieldname));
}

function gettableval(inarray, inname){
	outval = "";
	for(i=0;i<inarray[0].length;i++)
		if (inname == inarray[0][i]) outval = inarray[1][i];
	return(outval);
}

function getpell(efcval){
	pellamt = 0;
	for (i=0;i<pelltable[0].length;i++)
		if (efcval <= pelltable[0][i]) { pellamt = pelltable[1][i]; break; }
	return (pellamt);
}
