/*********************************************************
   1999 Taxable social security calculator
   usage:  taxableSocSec = calcTaxableSocSec( agiB4ss, exemptInt, totalSocSec, married, printSchedule );   //married = 1, 
   Copyright 2000 Mark E. Gunnison
  *********************************************************/


function calcTaxableSocSec( agi, exemptInt, totalSocSec, married, printSchedule )
{

var taxWindow;
var wstring;

var sslimit1 = 0;
var sslimit2 = 0;
var ssexcess50=0;
var ssexcess85=0;
var ssincome=0;
var taxableSocSec = 0;

	if (married==1)
	{
		sslimit1 = 32000;            // Married
		sslimit2 = 12000;
	}
	else
	{
		sslimit1 = 25000;           // Single
		sslimit2 = 9000;
	}


	ssincome = agi + exemptInt + Math.round(totalSocSec * .5);  // Line 7

	ssexcess50 = ssincome - sslimit1;             // Line 9
	if (ssexcess50 < 0)
		ssexcess50 = 0;

	ssexcess85 = ssexcess50 - sslimit2;           // Line 11
	if (ssexcess85 < 0)
		ssexcess85 = 0;

	sstaxable50 = ssexcess50;                   // Line 12
	if (sslimit2 < sstaxable50)
		sstaxable50 = sslimit2;

	sstaxable50 = Math.round(sstaxable50 * .5);             // Line 13

	if ((totalSocSec*.5) < sstaxable50)            // Line 14
		sstaxable50 = Math.round(totalSocSec*.5);

	sstaxable85 = Math.round(ssexcess85 * .85);             // line 15
	if (totalSocSec*.85 < sstaxable85)
		sstaxable85 = Math.round(totalSocSec*.85);

	taxableSocSec = sstaxable50 + sstaxable85;   // line 16

	if (totalSocSec*.85 < taxableSocSec)          // line 18
		taxableSocSec = Math.round(totalSocSec*.85);

		
  if (printSchedule == 1)
  {

     taxWindow = window.open('','Note','toobar=no,location=no,directories=no,status=no,scrollbars=yes,resizable=yes,copyhistory=no,width=330,height=330');

		 if (!taxWindow.opener)
		 {
		  	taxWindow.opener = window
		 }


     wstring = "<HTML><TITLE>Taxable Social Security Calculator</TITLE><BODY>"
     wstring += "<CENTER>"
     wstring += "<TABLE BORDER='0'>"
     wstring += "<TR><TD><B>Taxable Social Secuirty:</B>"
     wstring += "<TR><TD> Income plus 1/2 Soc. Sec </TD><TD ALIGN=right>"+ssincome+"</TD></TR>"
     wstring += "<TR><TD> First limit </TD><TD ALIGN=right>"+sslimit1+"</TD></TR>"
		 wstring += "<TR><TD> </TD><TD colspan=2><HR></TD></TR>"
     wstring += "<TR><TD> Excess at 50% </TD><TD ALIGN=right>"+ssexcess50+"</TD><TD ALIGN=right> = "+sstaxable50+"</TR>"
  	 wstring += "<TR><TD> <BR> </TD><TD ALIGN=right> </TD></TR>"
	   wstring += "<TR><TD> Second limit </TD><TD ALIGN=right>"+sslimit2+"</TD></TR>"
		 wstring += "<TR><TD> </TD><TD colspan=2><HR></TD></TR>"
		 wstring += "<TR><TD> Excess at 85% </TD><TD ALIGN=right>"+ssexcess85+"</TD><TD ALIGN=right> = "+sstaxable85+"</TR>"
		 wstring += "<TR><TD> </TD><TD colspan=2><HR></TD></TR>"
		 wstring += "<TR><TD colspan = 2> Taxable Soc. Sec. limited to 85% = </TD><TD ALIGN=right>"+taxableSocSec+"</TD></TR>"
		 //wstring += "<TR><TD><B>  </B>  </TD><TD ALIGN=right>"++"</TD></TR>"
		 //wstring += "<TR><TD><B>  </B>  </TD><TD ALIGN=right>"++"</TD></TR>"
		 wstring += "</TABLE>" 

     wstring += "<FORM><INPUT TYPE='button' NAME='closeBtn' VALUE='Close' onclick='window.close()'></FORM>" 
     wstring += "</CENTER>" 
     wstring += "</BODY></HTML>"

		 location.href="#Top";
     taxWindow.focus()
     
     taxWindow.document.write(wstring);	//stop strange error one second... window
     taxWindow.document.close() 

  }
		
			
		return( Math.round(taxableSocSec));		
}


