String.prototype.trim = function() {
	return this.replace(/(^\s*)|(\s*$)/g, "")
}

function calculate(){	
	// Add the limits at the end of each array.
	// There are 4 numbers to update for a new tax year.

	var maxPensionSingle = new Array();
	var maxIntSingle = new Array();
	var maxPensionJoint = new Array();
	var maxIntJoint = new Array();

	// Single or Filing Separate
	maxPensionSingle[0] = 37710;
	maxPensionSingle[1] = 38550;
	maxPensionSingle[2] = 39570;
	maxPensionSingle[3] = 40920;
	maxPensionSingle[4] = 42240;
	maxPensionSingle[5] = 43440;

	maxIntSingle[0] = 8408;
	maxIntSingle[1] = 8595;
	maxIntSingle[2] = 8828;
	maxIntSingle[3] = 9128;
	maxIntSingle[4] = 9420;
	maxIntSingle[5] = 9690;
	
	// Filing Joint
	maxPensionJoint[0] = 75420;
	maxPensionJoint[1] = 77100;
	maxPensionJoint[2] = 79140;
	maxPensionJoint[3] = 81840;
	maxPensionJoint[4] = 84480;
	maxPensionJoint[5] = 86880;

	maxIntJoint[0] = 16815;
	maxIntJoint[1] = 17190;
	maxIntJoint[2] = 17655;
	maxIntJoint[3] = 18255;
	maxIntJoint[4] = 18840;
	maxIntJoint[5] = 19380;
			
	var taxYr =  maxPensionJoint.length - document.getElementById("Year").selectedIndex - 1;
	var pubPen, milPen, privPen, intDiv, maxPen, maxInt, penDeduction, intDeduction, dedBalance;

	if (document.getElementById("Filing_Status").selectedIndex == 0){
		maxPen = maxPensionSingle[taxYr];
		maxInt = maxIntSingle[taxYr];
	}else{
		maxPen = maxPensionJoint[taxYr];
		maxInt = maxIntJoint[taxYr];}

	if (isNaN(parseInt(document.getElementById("Public_Pension").value.trim().replace(/[\$\,]/g,""),10))) 
		pubPen = 0;
	else
		pubPen = parseInt(document.getElementById("Public_Pension").value.trim().replace(/[\$\,]/g,""),10);
	if (isNaN(parseInt(document.getElementById("Military_Pension").value.trim().replace(/[\$\,]/g,""),10))) 
		milPen = 0;
	else
		milPen = parseInt(document.getElementById("Military_Pension").value.trim().replace(/[\$\,]/g,""),10);
	if (isNaN(parseInt(document.getElementById("Private_Pension").value.trim().replace(/[\$\,]/g,""),10))) 
		privPen = 0;
	else
		privPen = parseInt(document.getElementById("Private_Pension").value.trim().replace(/[\$\,]/g,""),10);
	if (isNaN(parseInt(document.getElementById("IntDivCapGain").value.trim().replace(/[\$\,]/g,""),10))) 
		intDiv = 0;
	else
		intDiv = parseInt(document.getElementById("IntDivCapGain").value.trim().replace(/[\$\,]/g,""),10);
			
	if ((pubPen + milPen) > maxPen)
		penDeduction = pubPen + milPen;
	else
		if((pubPen + milPen + privPen) > maxPen)
			penDeduction = maxPen;
		else
			penDeduction = pubPen + milPen + privPen;

	dedBalance = Math.max(maxPen - penDeduction,0)
	document.getElementById("Total_Pension_Sub").value = penDeduction;

	if (penDeduction >= maxInt)
		intDeduction = "Not Eligible";
	else
		intDeduction = Math.min(maxInt,maxInt - penDeduction,intDiv);
	document.getElementById("Total_IntDivCapGain_Sub").value = intDeduction;
	return false;
}

function PopUp(url)
 {
   mywindow = window.open (url,"mywindow","location=1,status=0,scrollbars=0,width=320,height=200");
   mywindow.moveTo(0,0);
 } 