// csm.js

// dependencies:  common.js, kawa-ObjTree.js

function estimate_child_support()
{
	if ( false == estimate_child_support_GATEKEEPER() ) return;
	estimate_child_support_SETUP_AJAX();
	ajax.onCompletion = estimate_child_support_AJAX_COMPLETE;
	ajax.runAJAX();
}

function estimate_child_support_GATEKEEPER()
{
	var test_dad = parseInt($('#txtDadInc').val());
	if ( isNaN(test_dad) ) test_dad = 0;
	var test_mom = parseInt($('#txtMomInc').val());
	if ( isNaN(test_mom) ) test_mom = 0;
	var test_cai = test_dad + test_mom;
	if ( (test_cai < 800) || (test_cai > 30000) )
	{
		safe_alert("The parties' gross monthly incomes must be numeric values TOTALING between 800 and 30000.");
		$('#txtDadInc').select();
		return false;
	}
	$('#interview').hide();
	return true;
}

function estimate_child_support_SETUP_AJAX()
{
	ajax = new sack();
	ajax.setVar("num_kids", $('#selNumKids').val());
	ajax.setVar("dad_inc", parseInt($('#txtDadInc').val()));
	ajax.setVar("mom_inc", parseInt($('#txtMomInc').val()));
	ajax.setVar("day_care", parseInt($('#txtChildCare').val()));
	ajax.setVar("submission", 'csm');
	ajax.setVar("action", '1');
	ajax.requestFile = "backdoor.php";
}

function estimate_child_support_AJAX_COMPLETE()
{
	$('#csm_report_options').show();
	var problem = false;
	if ( typeof ajax.response != 'undefined' )
	{
		var xotree = new XML.ObjTree();
		var tree = xotree.parseXML(ajax.response);
		if ( typeof tree.matrix != 'undefined' )
		{
			var html = '<p/>';
			html += '<p>Estimated child support payments are derived by looking up the appropriate monthly Basic Child Support Obligation ("BCSO") from the table in the statute for the parties\' combined monthly income, of which each parent is responsible for his or her pro rata share.</p>';
			html += '<p/>';
			html += '<p>';
			html += '<table>';
				html += '<tr>';
					html += '<td class="R" style="width:14px">$</td>';
					html += '<td class="R" style="width:64px">'+tree.matrix.Dinc+'</td>';
					html += '<td class="R" style="width:64px">'+tree.matrix.Dpct+'</td>';
					html += '<td>Father\'s Gross Monthly Income</td>';
				html += '</tr>';
				html += '<tr>';
					html += '<td class="R">$</td>';
					html += '<td class="R">'+tree.matrix.Minc+'</td>';
					html += '<td class="R">'+tree.matrix.Mpct+'</td>';
					html += '<td>Mother\'s Gross Monthly Income</td>';
				html += '</tr>';
				html += '<tr>';
					html += '<td class="R" style="border-top:1px solid black">$</td>';
					html += '<td class="R" style="border-top:1px solid black">'+tree.matrix.cai+'</td>';
					html += '<td />';
					html += '<td>Combined Gross Monthly Income (rounded)</td>';
				html += '</tr>';
				html += '<tr>';
					html += '<td />';
					html += '<td />';
					html += '<td class="R">$'+tree.matrix.day_care+'</td>';
					html += '<td>Estimated Child Care and Health Insurance costs</td>';
				html += '</tr>';
			html += '</table>';
			html += '</p>';
			html += '<p/>';
			
			var working, offset_string;
			switch ( parseInt(tree.matrix.num_kids) )
			{
				case 1:
					html += '<p class="b">The BCSO for 1 child for this combined income is $'+tree.matrix.bcso+'.</p>';
					html += '<p>If the Mother is the Custodial Parent, then the Father\'s share of BCSO will be <span class="b">$'+tree.matrix.dad_pays_full+' per month</span>.</p>';
					html += '<p>If the Father is the Custodial Parent, then the Mother\'s share of BCSO will be <span class="b">$'+tree.matrix.mom_pays_full+' per month</span>.</p>';
					html += '<p/>';
					break;
				case 2:
					html += '<p class="b">The BCSO for 2 children for this combined income is $'+tree.matrix.bcso+'.</p>';
					html += '<p>If the Mother is the Custodial Parent of both children, then the Father\'s share of BCSO will be <span class="b">$'+tree.matrix.dad_pays_full+' per month</span>.</p>';
					html += '<p>If the Father is the Custodial Parent of both children, then the Mother\'s share of BCSO will be <span class="b">$'+tree.matrix.mom_pays_full+' per month</span>.</p>';
					working = parseFloat(tree.matrix.offset.replace(',',''));
					offset_string = "Both parents' obligations offset, and neither will pay child support.";
					if ( working )
					{
						offset_string  = 'Offsetting the two payments, ' + ( (working > 0) ? 'Mother' : 'Father' ) + '\'s obligation will be <span class="b">$';
						offset_string += ( tree.matrix.offset.substring(0,1) == '-' ) ? tree.matrix.offset.substring(1) : tree.matrix.offset;
						offset_string += ' per month</span>.</p>';
					}
					html += "<p>If the parties each have custody of one child, then the Mother's share of BCSO for the child with Father is $"+tree.matrix.splitM+", and the Father's share of BCSO for the child with Mother is $"+tree.matrix.splitD+'. '+offset_string;
					html += '<p/>';
					break;
				case 3:
				case 4:
				case 5:
				case 6:
					html += '<p class="b">The BCSO for '+tree.matrix.num_kids+' children for this combined income is $'+tree.matrix.bcso+'.</p>';
					html += '<p>If the Mother is the Custodial Parent of all '+tree.matrix.num_kids+' children, then the Father\'s share of BCSO will be <span class="b">$'+tree.matrix.dad_pays_full+' per month</span>.</p>';
					html += '<p>If the Father is the Custodial Parent of all '+tree.matrix.num_kids+' children, then the Mother\'s share of BCSO will be <span class="b">$'+tree.matrix.mom_pays_full+' per month</span>.</p>';
					html += '<p/>';
					html += "<p class=\"i\">To calculate child support in a split parenting situation (where each parent has custody of one or more of the parties' children), PeachForms.com employs the following methodology: (1) calculate Mother's share of BCSO as calculated based on the number of children remaining in Father's custody; (2) calculate Father's share of BCSO as calculated based on the number of children remaining in Mother's custody; and (3) offset the two so there is one payment by the parent with the larger share. Using this methodology for all possible combinations of "+tree.matrix.num_kids+" children in a split parenting situation, the following are the estimated child support payments in each situation:</p>";
					html += '<p><table><tr><th/><th>Mother\'s BCSO share for<br/>children with Father</th><th>Father\'s BCSO share for<br/>children with Mother</th><th class="bot">Offset</th></tr>';
					var tmp = 1;
					for ( var loop=0, loop_ct=tree.matrix.splitD.length; loop<loop_ct; loop++ )
					{
						html += '<tr>';
						html += '<td><div>'+tmp+' child'+((tmp==1)?'':'ren')+' with Mother</div><div>'+(tree.matrix.num_kids-tmp)+' child'+(((tree.matrix.num_kids-tmp)==1)?'':'ren')+' with Father</div></td>';
						html += '<td class="mid C">$'+tree.matrix.splitM[loop]+'</td>';
						html += '<td class="mid C">$'+tree.matrix.splitD[loop]+'</td>';
						working = parseFloat(tree.matrix.offset[loop].replace(',',''));
						offset_string = "Both parents' obligations offset";
						if ( working )
						{
							offset_string  = ( (working > 0) ? 'Mother' : 'Father' ) + ' pays $';
							offset_string += ( tree.matrix.offset[loop].substring(0,1) == '-' ) ? tree.matrix.offset[loop].substring(1) : tree.matrix.offset[loop];
						}
						html += '<td class="mid b">'+offset_string+'</td>';
						html += '</tr>';
						tmp++;
					}
					html += '</table></p><p/>';
					break;
			}
			
			if ( parseFloat(tree.matrix.day_care) != 0 )
			{
				html += "<p class=\"i\">The above BCSO shares include each parent's pro rata share of child care and/or health insurance costs.</p><p/>";
			}
			
			$('#csm_results').append(html);
		}
		else
		{
			problem = true;
		}
	}
	else
	{
		problem = true;
	}
	
	if ( problem )
	{
		$('#csm_results').append(ajax.response);
	}
}

function csm_reset()
{
	if ( false == csm_reset_GATEKEEPER() ) return;
	csm_reset_SETUP_AJAX();
	ajax.onCompletion = csm_reset_AJAX_COMPLETE;
	ajax.runAJAX();
}

function csm_reset_GATEKEEPER()
{
	var tmp = $.trim($('#txtName').val());
	$('#txtName').val(tmp);
	
	tmp = parseInt($('#txtDadInc').val());
	$('#txtDadInc').val(tmp);
	
	tmp = parseInt($('#txtMomInc').val());
	$('#txtMomInc').val(tmp);
	
	tmp = parseInt($('#txtChildCare').val());
	$('#txtChildCare').val(tmp);
}

function csm_reset_SETUP_AJAX()
{
	ajax = new sack();
	ajax.setVar("num_kids", $('#selNumKids').val());
	ajax.setVar("dad_inc", parseInt($('#txtDadInc').val()));
	ajax.setVar("mom_inc", parseInt($('#txtMomInc').val()));
	ajax.setVar("day_care", parseInt($('#txtChildCare').val()));
	ajax.setVar("submission", 'csm');
	ajax.setVar("action", '2');
	ajax.requestFile = "backdoor.php";
}

function csm_reset_AJAX_COMPLETE()
{
	if ( typeof ajax.response != 'undefined' )
	{
		redirect('index.php');
	}
}

function check_for_addition(input)
{
	var working = input.value.split(' ');
	var result = 0;
	for ( var loop=0, loop_ct=working.length; loop<loop_ct; loop++ )
	{
		result += parseFloat(working[loop]);
	}
	input.value = result;
}

function csm_print()
{
	safe_alert("For convenience and to hopefully save a few trees, the Child Support Estimate starts printing on its own page (generally, page 2). You might want to use your browser's 'Print Preview' function to avoid wasting pages.");
	window.print();
}

// end of file
