//Copyright MacNeil/Lehrer Productions. All Rights Reserved.
//http://www.pbs.org/newshour


tabselected = "basic";


function getRefundAmountBasic(){
	var retnum=300;
	var agi=parseFloat(removeCommas(document.getElementById('agi').value));
	var isspouse=parseInt(document.getElementById('filed').value);
	var istaxdue = parseInt(document.getElementById('taxpaid').value);
	var num_children=parseInt(removeCommas(document.getElementById('numchildren').value));
	if (istaxdue ==1) {
		var pctoff = 0;
		if(isspouse==0){
			if(agi<3000){ return 0;}
			if(agi>=87000){	return 0;}
			if (agi > 75000){ pctoff = Math.ceil((agi-75000)/1000)*5; }
			retnum=600;
			retnum+=num_children*300;
			if (agi > 75000){
				retnum = retnum * (100-pctoff)/100;
			}
		}
		else{
			if(agi<3000){ return 0;}
			if(agi>=174000){ return 0; }
			if (agi > 150000){ pctoff = Math.ceil((agi-150000)/1000)*5; }
			retnum=1200;
			retnum+=num_children*300;
			if (agi > 150000){
				retnum = retnum * (100-pctoff)/100;
			}
		}
	} else {
		if(isspouse==0){
			if(agi<3000){ return 0; }
			else { retnum = 300; }
		} else {
			if(agi<3000){ return 0; }
			else { retnum = 600; }
		}
		retnum+=num_children*300;
	}
	return retnum;
}
function getRefundAmountAdvanced(){
	var refundamount = 0;
	var isspouse=parseInt(document.getElementById('filed').value);
	var agi=parseFloat(removeCommas(document.getElementById('agi').value));
	var nitl=parseFloat(removeCommas(document.getElementById('taxliability').value));
	//var qi=parseFloat(removeCommas(document.getElementById('qualifyingincome').value));
	var qi=parseInt(document.getElementById('qi').value);
	var num_children=parseInt(removeCommas(document.getElementById('numchildren').value));
	var istaxpayer = false;
	if ( qi || (nitl>0)){
		istaxpayer = true;
	}
	
	if (isspouse){
		if (istaxpayer){
			refundamount = nitl;
			if (nitl > 1200){
				refundamount = 1200;
			}
			if (refundamount < 600){
				refundamount = 600;
			}
		}
		if (agi > 150000){ 
			pctoff = Math.ceil((agi-150000)/1000)*5; 
			if (pctoff >100){
				pctoff = 100;
			}
			refundamount = refundamount * (100-pctoff)/100;
		}
	}
	else {
		if (istaxpayer){
			refundamount = nitl;
			if (nitl > 600){
				refundamount = 600;
			}
			if (refundamount < 300){
				refundamount = 300;
			}
		}
		if (agi > 75000){ 
			pctoff = Math.ceil((agi-75000)/1000)*5;
			if (pctoff >100){
				pctoff = 100;
			}
			refundamount = refundamount * (100-pctoff)/100;
		}
	}
	return refundamount;
}

function removeCommas(str){
	str = str.replace ("$","");
	return str.replace(",","");
}



YAHOO.namespace("example.container");

function init() {
	//AGI
	YAHOO.example.container.simpledialog1 = new YAHOO.widget.SimpleDialog("simpledialog1", 
																			 { width: "300px",
																			   fixedcenter: true,
																			   visible: false,
																			   draggable: false,
																			   close: true,
																			   text: "<p>Adjusted gross income is defined as gross income minus adjustments to income. If your income (e.g., change in job) or personal circumstances (e.g., change in marital status) did not change from last year, we suggest you refer to your 2006 federal income tax return to get a quick estimate of your 2007 AGI. On your 2006 federal tax return, please refer to: </p><ul><li>Line 4 if you filed a Form 1040EZ</li><li>Line 21 if you filed a Form 1040A</li><li>Line 37 if you filed a Form 1040</li></ul><p>AGI is defined as your taxable income from all sources including wages, salaries, tips, taxable interest, ordinary dividends, taxable refunds, credits, or offsets of state and local income taxes, alimony received, business income or loss, capital gains or losses, other gains or losses, taxable IRA distributions, taxable pensions and annuities, rental real estate, royalties, farm income or losses, unemployment compensation, taxable social security benefits, and other income minus specific deductions including educator expenses, the IRA deduction, student loan interest deduction, tuition and fees deduction, Archer MSA deduction, moving expenses, one-half of self-employment tax, self-employed health insurance deduction, self-employed SEP, SIMPLE, and qualified plans, penalty on early withdrawal of savings, and alimony paid by you.<p>Do not deduct your standard or itemized deductions.</p><p>Source: <a href='http://www.irs.gov/app/freeFile/html/moreInfo/more_info_agi.html' target='_blank'>Internal Revenue Service</a></p>",
																			   icon: YAHOO.widget.SimpleDialog.ICON_HELP,
																			   constraintoviewport: true,
																			   buttons: [ ]
																			 } );
	YAHOO.example.container.simpledialog1.setHeader("2007 Adjusted Gross Income (AGI)");
	YAHOO.example.container.simpledialog1.render("container");
	//YAHOO.util.Event.addListener("info_agi", "mouseover", YAHOO.example.container.simpledialog1.show, YAHOO.example.container.simpledialog1, true);
	//YAHOO.util.Event.addListener("info_agi", "mouseout", YAHOO.example.container.simpledialog1.hide, YAHOO.example.container.simpledialog1, true);
	//YAHOO.util.Event.addListener("info_agi", "click", YAHOO.example.container.simpledialog1.hide, YAHOO.example.container.simpledialog1, true);

	//
	YAHOO.example.container.simpledialog2 = new YAHOO.widget.SimpleDialog("simpledialog2", 
																			 { width: "300px",
																			   fixedcenter: true,
																			   visible: false,
																			   draggable: false,
																			   close: true,
																			   text: "<p>Net income tax liability is the amount shown on Form 1040, Line 57 plus the amount on Line 52. For 1040A filers, it is the amount on Line 35 plus the amount on Line 32. For Form 1040EZ filers, it is the amount on Line 10.<br />Source: <a href='http://www.irs.gov/newsroom/article/0,,id=179181,00.html'>Internal Revenue Service</a></p>",
																			   icon: YAHOO.widget.SimpleDialog.ICON_HELP,
																			   constraintoviewport: true,
																			   buttons: [ ]
																			 } );
	YAHOO.example.container.simpledialog2.setHeader("Net Income Tax Liability");
	YAHOO.example.container.simpledialog2.render("container");
	//YAHOO.util.Event.addListener("info_nitl", "mouseover", YAHOO.example.container.simpledialog2.show, YAHOO.example.container.simpledialog2, true);
	//YAHOO.util.Event.addListener("info_nitl", "mouseout", YAHOO.example.container.simpledialog2.hide, YAHOO.example.container.simpledialog2, true);
	//YAHOO.util.Event.addListener("info_nitl", "click", YAHOO.example.container.simpledialog2.hide, YAHOO.example.container.simpledialog2, true);

	//
	YAHOO.example.container.simpledialog3 = new YAHOO.widget.SimpleDialog("simpledialog3", 
 { width: "300px",
   fixedcenter: true,
  visible: false,
   draggable: false,
 close: true,
	   text: "<p>To figure your qualifying income, add together the following amounts:<ul><li>Wages that are reported on Form W-2.</li><li>Net self-employment income.</li><li>Social Security benefits reported in box 5 of the 2007 Form 1099-SSA, which would have been received in January 2008. People who do not have a Form 1099-SSA may estimate their annual Social Security benefit by taking their monthly benefit and multiplying it by the number of months during the year they received the benefit.</li><li>Certain Railroad Retirement benefits reported in box 5 of the 2007 Form 1099-RRB, which recipients would have received in January 2008.</li><li>Veterans’ benefits received in 2007, including veterans’ disability compensation and pension or survivors’ benefits received from the Department of Veterans Affairs. People who weren’t required to file a tax return can estimate their annual veterans’ benefits by taking their monthly benefit and multiplying it by the number of months during the year they received the benefit.</li><li>Nontaxable combat pay if the taxpayer elects to include it as earned income.</li></ul>For people filing joint tax returns, only a total of $3,000 of qualifying income from both spouses is required to be eligible for a payment.<br />Source: <a href='http://www.irs.gov/newsroom/article/0,,id=179181,00.html'>Internal Revenue Service</a></p>",
																			   icon: YAHOO.widget.SimpleDialog.ICON_HELP,
																			   constraintoviewport: true,
																			   buttons: [ ]
																			 } );
		 //<p>'Qualifying Income' is the sum of earned income, Social Security benefits, and compensation related to disability or death.</p>
	YAHOO.example.container.simpledialog3.setHeader("Qualifying Income");
	YAHOO.example.container.simpledialog3.render("container");
	//YAHOO.util.Event.addListener("info_qi", "mouseover", YAHOO.example.container.simpledialog3.show, YAHOO.example.container.simpledialog3, true);
	//YAHOO.util.Event.addListener("info_qi", "mouseout", YAHOO.example.container.simpledialog3.hide, YAHOO.example.container.simpledialog3, true);
	//YAHOO.util.Event.addListener("info_qi", "click", YAHOO.example.container.simpledialog3.hide, YAHOO.example.container.simpledialog3, true);







//
	YAHOO.example.container.simpledialog4 = new YAHOO.widget.SimpleDialog("simpledialog4", 
																			 { width: "300px",
																			   fixedcenter: true,
																			   visible: false,
																			   draggable: false,
																			   close: true,
																			   text: "<p>On Feb. 13, 2008, President Bush signed into law the Economic Stimulus Act of 2008 <a href='http://www.pbs.org/newshour/updates/business/jan-june08/stimulus_02-08.html'>$168 billion plan</a> as part of a government effort to boost a slowing U.S. economy. The stimulus plan includes tax rebate payments for millions of Americans.</p><br/><p>This rebate calculator is designed to offer NewsHour Web site visitors an estimate of their rebate. It is not meant to offer financial or tax advice.</p><br/><p>Taxpayers who wish to enter minimal information may use a basic version of the calculator; an advanced version allows taxpayers to enter more detailed tax information to receive a more accurate rebate estimate.</p><br/><p>Both versions make certain assumptions about taxpayer status. Miscalculations could result if your adjusted gross income differs greatly from your tax liability, if your gross income is greater than your standard deduction, or if another taxpayer was eligible to deduct personal exemptions on your behalf, among other reasons.</p><br/><p>If you have more questions or would like more details on the rebate process, the Internal Revenue Service <a href='http://www.irs.gov/irs/article/0,,id=177937,00.html'>Web site</a> hosts detailed rebate information, including information for Social Security recipients. The <a href='http://thomas.loc.gov/cgi-bin/query/z?c110:H.R.5140:'>Library of Congress Web site</a> also has the full text of the final bill.</p><br/><p>Source: <a href='http://thomas.loc.gov/cgi-bin/query/z?c110:H.R.5140:'>Economic Stimulus Act of 2008</a> (110th Congress House Bill H.R. 5140)</p>",
																			   icon: YAHOO.widget.SimpleDialog.ICON_HELP,
																			   constraintoviewport: true,
																			   buttons: [ ]
																			 } );
	YAHOO.example.container.simpledialog4.setHeader("More Information");
	YAHOO.example.container.simpledialog4.render("container");
	//YAHOO.util.Event.addListener("info_asterisk", "mouseover", YAHOO.example.container.simpledialog4.show, YAHOO.example.container.simpledialog4, true);
	//YAHOO.util.Event.addListener("info_asterisk", "mouseout", YAHOO.example.container.simpledialog4.hide, YAHOO.example.container.simpledialog4, true);
	//YAHOO.util.Event.addListener("info_asterisk", "click", YAHOO.example.container.simpledialog4.hide, YAHOO.example.container.simpledialog4, true);






}

YAHOO.util.Event.addListener(window, "load", init);

function selecttab(whichtab){
	if (whichtab=="basic"){
		document.getElementById('calculatorbasic').style.display = "block";
		document.getElementById('calculatoradvanced').style.display = "none";
		document.getElementById('tabbasic').className = "tabselected";
		document.getElementById('tabadvanced').className = "tab";
		tabselected = "basic";
		calcRefund();
	}
	else if (whichtab=="advanced"){
		document.getElementById('calculatorbasic').style.display = "none";
		document.getElementById('calculatoradvanced').style.display = "block";
		document.getElementById('tabbasic').className = "tab";
		document.getElementById('tabadvanced').className = "tabselected";
		tabselected = "advanced";
		calcRefund();
	}
}



//http://www.web-source.net/web_development/currency_formatting.htm
//By William Bontrager
function CommaFormatted(amount)
{
	var delimiter = ","; // replace comma if desired
	var a = amount.split('.',2)
	var d = a[1];
	var i = parseInt(a[0]);
	if(isNaN(i)) { return ''; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	var n = new String(i);
	var a = [];
	while(n.length > 3)
	{
		var nn = n.substr(n.length-3);
		a.unshift(nn);
		n = n.substr(0,n.length-3);
	}
	if(n.length > 0) { a.unshift(n); }
	n = a.join(delimiter);
	if(d.length < 1) { amount = n; }
	else { amount = n + '.' + d; }
	amount = minus + amount;
	return amount;
}



function calcRefund(){
	cleanupfields ();
	setRefundAmount (getRefundAmount());
}

function cleanupfields (){
/*
	tempnum = parseInt(document.getElementById('qualifyingincome').value);
	if (isNaN(tempnum)){ tempnum = 0; }
	document.getElementById('qualifyingincome').value=tempnum;

	tempnum = parseInt(document.getElementById('taxliability').value);
	if (isNaN(tempnum)){ tempnum = 0; }
	document.getElementById('taxliability').value=tempnum;

	tempnum = parseInt(document.getElementById('agi').value);
	if (isNaN(tempnum)){ tempnum = 0; }
	document.getElementById('agi').value=tempnum;

	tempnum = parseInt(document.getElementById('numchildren').value);
	if (isNaN(tempnum)){ tempnum = 0; }
	document.getElementById('numchildren').value=tempnum;
*/
}

function setRefundAmount (refundamount){
	refundamount = "$" + CommaFormatted(refundamount + ".");
	document.getElementById('refundamount').innerHTML=refundamount;
}

function getRefundAmount(){
	if (tabselected == "basic"){
		return getRefundAmountBasic();
	}
	else if (tabselected == "advanced"){
		return getRefundAmountAdvanced();
	}
}


//Copyright MacNeil/Lehrer Productions. All Rights Reserved.
//http://www.pbs.org/newshour


