var total_months = 0;
var month_limit = 36;

function show_div(div_id) 
{
    // show the div
    if (document.getElementById(div_id) != null)
    {
    	document.getElementById(div_id).style.display = '';
    }
}

function hide_div(div_id) 
{
	// hide the div
	if (document.getElementById(div_id) != null)
	{
		document.getElementById(div_id).style.display = 'none';
	}
}

function show_divs(position)
{
	// on page load = -1
	
	var months_count = 0;
	var temp_mc = 0;
	
	for (var c=0; c<7; c++)
	{
		temp_mc = GetMonthCount(c);
		months_count += temp_mc;

		if (c < position)
		{
			// not reached updated item yet
			
		}
		else
		{
			if (temp_mc > 0)
			{
				// if this address has years or months on it
				if (months_count < month_limit)
				{
					// show next address section
					show_div("divPrevious" + (c+1));
				}
				else
				{
					// hide all following address sections
					for (var z=c+1; z<7; z++)
					{
						hide_div("divPrevious" + z);
					}
					break;
				}
			}
			else
			{
				// hide all following address sections
				for (var z=c+1; z<7; z++)
				{
					hide_div("divPrevious" + z);
				}
				break;
			}
		}
	}
}

function GetMonthCount(position)
{
	var temp = 0;
	if (document.getElementById('ap_tad_years' + position) != null)
	{
		if (document.getElementById('ap_tad_years' + position).value > 0)
		{
			var t_years = parseInt(document.getElementById('ap_tad_years' + position).value);
			temp += (12 * (t_years-1));
		}
	}
	if (document.getElementById('ap_tad_months' + position) != null)
	{
		if (document.getElementById('ap_tad_months' + position).value > 0)
		{
			var t_months = parseInt(document.getElementById('ap_tad_months' + position).value);
			temp += (t_months -1);
		}
	}
	
	return temp;
}

function GetMonthCountEmp(position)
{
	var temp = 0;
	
	if (document.getElementById('ap_emp_years' + position) != null)
	{
		if (document.getElementById('ap_emp_years' + position).value > 0)
		{
			var t_years = parseInt(document.getElementById('ap_emp_years' + position).value);
			temp += (12 * (t_years-1));
		}
	}
	if (document.getElementById('ap_emp_months' + position) != null)
	{
		if (document.getElementById('ap_emp_months' + position).value > 0)
		{
			var t_months = parseInt(document.getElementById('ap_emp_months' + position).value);
			temp += (t_months -1);
		}
	}
	
	return temp;
}

function show_divs_emp(position)
{
	// on page load = -1
	
	var months_count = 0;
	var temp_mc = 0;
	
	for (var c=0; c<7; c++)
	{
		temp_mc = GetMonthCountEmp(c);
		months_count += temp_mc;

		if (c < position)
		{
			// not reached updated item yet
			
		}
		else
		{
			if (temp_mc > 0)
			{
				// if this address has years or months on it
				if (months_count < month_limit)
				{
					// show next address section
					show_div("divPreviousE" + (c+1));
				}
				else
				{
					// hide all following address sections
					for (var z=c+1; z<7; z++)
					{
						hide_div("divPreviousE" + z);
					}
					break;
				}
			}
			else
			{
				// hide all following address sections
				for (var z=c+1; z<7; z++)
				{
					hide_div("divPreviousE" + z);
				}
				break;
			}
		}
	}
}