// JavaScript Document
$(document).ready(function(){
$("#btn_back").hide();

// escape key closes details window
$(document).keyup(function(e) {
	  if (e.keyCode == 27) { // escape key maps to keycode `27`
		$("div[id^='details_o_window'],div[id^='details_e_window']").hide();
		}
	});


/*$("#continue_button").click(function(){
	var sessionID = "";
	var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  
	for (var i = 0; i < 10; i++)
	  sessionID += possible.charAt(Math.floor(Math.random() * possible.length));
	  
	  var hit_status = "includes/track.php?started&sessionID="+sessionID+"&tool=PSHARE";
	  //alert("Started: " + hit_status);
	  jQuery.ajax({
		 type: "GET",
		 url: hit_status,
		 cache: false,
		 success: function(response)
		 {
		 }
	   });
	 });*/
	
	// Details link and close
	$("body").on("click", "[id^='e_detail_link'],[id^='o_detail_link']", function(event){
		//$("[id^='details_']").remove(); 
		  var detail_link_clicked = event.target.id;
		  
		  var e_or_o   = detail_link_clicked.charAt(0);
		   var id_of_detail = detail_link_clicked.replace(""+e_or_o+"_detail_link_","");
		  $("[id^='details_']").hide();
		  
		  /*if ($("#details_"+e_or_o+"_window_"+id_of_detail+"") != null){
			  }*/
		  //disabled for testing
		  $("#details_"+e_or_o+"_window_"+id_of_detail+"").show();
		  $("#details_"+e_or_o+"_window_"+id_of_detail+"").trigger( "focus" );
		  });
		  
	//$("[id^='close_']").click(function(){
		$("body").on("click", "[id^='close_']", function(){
		  $("[id^='details_']").hide();
		});
$(document).on("mouseup change","#increment_slider,#decrement_slider,#scr5_year_drop,#scr_5_check_noservice,#scr_5_check_yesservice,#scr5_age_selector,#scr_5_age_no,#scr_5_age_yes,.calculate,#contribution_range",function(){ 
console.log("up-down is clicked");
	$("#scr5_year").text($("#scr2_date option:selected").val());
  //APPEND OWNER --------------------------------------------------------------------------------
  //remove existing to replace with new data
  $("td[id^='o_results_row_'],tr[id^='o_results_row_']").remove();
	var owner_row_count = 1;
	while(owner_row_count <= $("input[name^='scr3_i1_row_']").length){ 
	
	
	//console.log('owner_CV from results.js: ' + sessionStorage.getItem("owner_CV_"+owner_row_count+""));
	
	
		if (sessionStorage.getItem("owner_CV_"+owner_row_count) == 'yes'){
			//var results_AGCTBN = Math.round(sessionStorage.getItem('owner_AGCTBN_'+owner_row_count)).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","); 
			var business_type_selected = $("input[name='scr1_q1']:checked").val();
			if (business_type_selected === 'Partnership'){
				var results_FERCTBN  = Math.round(sessionStorage.getItem("FERCTBN"+owner_row_count)).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
				} else {
					var results_FERCTBN  = Math.round(sessionStorage.getItem("owner_FCTBN_"+owner_row_count)).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
					}
			
			var input_5        = Math.round(sessionStorage.getItem("sess_owner"+owner_row_count+"_input_5")).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
			var hidden_dollar = '';
			//console.log('owner show dollar');
			} else {
				var results_AGCTBN = "Not Eligible";
				var results_FERCTBN  = "Not Eligible";
				var input_5        = "Not Eligible";
				var hidden_dollar = 'hide_dollar';
				//console.log('owner hide dollar');
				}
	  $("#o_results_row_1").append('<tr id="o_results_row_'+owner_row_count+'">'+
		'<td id="o_results_row_'+owner_row_count+'_col_1" tabindex="0" title="Owner Name">'+sessionStorage.getItem("sess_owner"+owner_row_count+"_input_1")+ ' <span>' +
		  sessionStorage.getItem("sess_owner"+owner_row_count+"_input_2")+'</span></td>'+
		'<td id="o_results_row_'+owner_row_count+'_col_2" tabindex="0" title="Owner">Yes</td>'+
		'<td id="o_results_row_'+owner_row_count+'_col_3" tabindex="0" title="Is eligible">'
		+sessionStorage.getItem("owner_CV_"+owner_row_count+"").charAt(0).toUpperCase() + sessionStorage.getItem("owner_CV_"+owner_row_count+"").slice(1)+
		'</td>'+
		'<td id="o_results_row_'+owner_row_count+'_col_5" tabindex="0" title="Estimated Owner Contribution"><span class="results_dollar_signs ' + hidden_dollar + '">$</span>'+results_FERCTBN+'</td>'+
		'<td id="o_results_row_'+owner_row_count+'_col_6"><span id="o_detail_link_'+owner_row_count+'" style="color:#333333; text-decoration:underline;cursor:pointer;" tabindex="0" title="Calculation details for row '+owner_row_count+'">Details</span></td>'+
	  '</tr>' + 
	  '');
	  owner_row_count ++ ;
	  } //end append while
	  
//OWNER DETAILS   --------------------------------------------------------------------------------------
var own_details_count = 1;
$("[id^='details_o']").remove();
while(own_details_count <= $('[id^=scr3_q1_row]').length){ //spawn details windows screen_containers	
	detail_o_eligible_YOS = sessionStorage.getItem("owner_CV_"+own_details_count+"");
	if (detail_o_eligible_YOS == "no"){ // owner is ineligible
		//sessionStorage.getItem("own_ineligible_reason","The owner is not eligible because of insufficient years of service.");
		
		$("#screen-5").append('<div id="details_o_window_'+ own_details_count +'" style="display:none;">' +
		
		'<div class="container_close"><span id="close_o_details_'+ own_details_count +'" class="closeBTN" tabindex="0" title="Close Details" tabindex="0">X</span></div>' +
		
	'<h1>Calculation for <span id="detail_o_name_'+ own_details_count +'"> ' + sessionStorage.getItem("sess_owner"+own_details_count+"_input_1") + '</span> <span id="detail_o_last1_'+ own_details_count +'"> ' + sessionStorage.getItem("sess_owner"+own_details_count+"_input_2") + '</span> </h1> ' +
	'<p>'+sessionStorage.getItem("own_ineligible_reason"+own_details_count)+'</p></div>');
		} else { //owner is eligible
	// DO UNINCORPORATED FIRST - (PARTNERSHIP)---------------------------------------------------------
	if ($("[name='scr1_q1']:checked").val() == 'Partnership'){
		// set up step two for owners
	var own_detail_ASD = sessionStorage.getItem("sess_owner"+own_details_count+"_input_5");
	var age_requirement_YN = $("[name='scr5_age_req']:checked").val();
	if (age_requirement_YN === 'scr_5_age_req_yes'){
		var match_percentage = ".0" + match_selected ; 
		} else {
			var match_percentage = ".02";
			}
		
	/*if (age_requirement_YN != 'scr_5_age_req_yes'){ // is non-elective
	if (sessionStorage.getItem("owner_ECTBN_"+own_details_count) == 'Not Eligible'){
		var hidden_dollar = 'hide_dollar';
		} else {
			var hidden_dollar = '';
			}
	//console.log("match_percent (non-elective): " + sessionStorage.getItem("match_percent"));
		var step_two_desc =  '<h3>Step Two: Calculate Estimated  Employer Contribution</h3>' +
					'<p>Estimated Employer Contribution = Estimated Earned Income<sup>2</sup> X Nonelective Contribution Percentage</p>';
		var step_two =  '<h3>Step Two: Calculate Estimated  Employer Contribution</h3>' +	
					'<p>Estimated Employer Contribution = Estimated Earned Income<sup>2</sup> X Nonelective Contribution Percentage</p>' + 
					'<p><span class="results_dollar_signs '+ hidden_dollar +'">$</span>' + Math.round(sessionStorage.getItem("owner_ECTBN_"+own_details_count)).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",") + 
					' = <span class="results_dollar_signs '+ hidden_dollar +'">$</span>' + Math.round(sessionStorage.getItem("owner_AC_"+own_details_count)).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",") + 
					' X ' + match_percentage + '</p>';
					// disclaimer text
					//var disclaimer_text_unincorp = '<p style="color:#666; font-size:.9rem;"><sup>2</sup>The maximum amount of Estimated Earned Income that may be considered for purposes of a 2 percent nonelective employer contribution under a SIMPLE IRA plan for ' + sessionStorage.getItem("Q1A") + ' is <span class="results_dollar_signs">$</span>' + sessionStorage.getItem("comp_cap"+own_details_count).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",") + '&#46;' + '</p>';
		} else { // is matching
		if (sessionStorage.getItem("owner_ECTBN_"+own_details_count) == 'Not Eligible'){
		var hidden_dollar = 'hide_dollar';
		} else {
			var hidden_dollar = '';
			}
		results_for_A_1 = sessionStorage.getItem("owner_NE_"+own_details_count) * Number("." + sessionStorage.getItem("match_percent"));
		//console.log("match_percent (matching): " + sessionStorage.getItem("match_percent"));
			var step_two_desc =  '<h3>Step Two: Calculate Estimated  Employer Contribution</h3>' +
					'<p>Estimated Employer Contribution = Lesser of </p>' + 	
					'<ol type="A">' +
					'<li>100% of Anticipated Salary  Deferral, or </li>' +
					'<li>Estimated Earned Income X Matching Contribution Percentage</li>' +
					'</ol>';
			var step_two =  '<h3>Step Two: Calculate Estimated  Employer Contribution</h3>' +
					'<p>Estimated  Employer Contribution = Lesser of </p>' +	
					'<ol type="A">' +
					'<li>100% of Anticipated Salary  Deferral, or</li>' +
					'<li>Estimated Earned Income X Matching Contribution Percentage</li>' +
					'</ol>' +
					
					'<p><span class="results_dollar_signs '+ hidden_dollar +'">$</span>' + Math.round(sessionStorage.getItem("owner_ECTBN_"+own_details_count)).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",") + ' = Lesser of	</p>' + 
					'<ol type="A">' +
					'<li><span class="results_dollar_signs '+ hidden_dollar +'">$</span>'+ Math.round(sessionStorage.getItem("sess_owner"+own_details_count+"_input_5")).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")+' or </li>' +
					'<li><span class="results_dollar_signs '+ hidden_dollar +'">$</span>' + Math.round(results_for_A_1).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",") + 
					' (<span class="results_dollar_signs '+ hidden_dollar +'">$</span>' + Math.round(sessionStorage.getItem("owner_NE_"+own_details_count)).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",") + 
					' X .' + sessionStorage.getItem("match_percent") + ')</li>' +
					'</ol>' ;
					// disclaimer text
					var disclaimer_text_unincorp = '';
			}*/ // end step two check (age_requirement_YN)
	
	$("#screen-5").append('<div id="details_o_window_'+ own_details_count +'" style="display:none;" tabindex="0">' +
	
	 '<div class="container_close"><span id="close_o_details_'+ own_details_count +'" class="closeBTN" tabindex="0">X</span></div>' +
    
	'<h1 class="calcKey">Calculation Key</h1>' +
	  '<h3>Step One: Calculate Estimated Earned Income</h3>' +
	  '<table>' +
	  '<tr>' +
	  '<td rowspan="2">Estimated Earned Income =</td>' +
	    '<td style="border-bottom:thin solid #ccc;">A - B</td>' +
	  '</tr>' +
	  '<tr>' +
	    '<td>1 + C</td>' +
	  '</tr>' +
	  '</table>' +
	  '<p>A = Estimated Self-Employment Earnings,  Minus Estimated Deduction for Contributions to Non-Owner Employees<sup>1</sup></p>' +
	  '<p>B = Estimated Deduction for &frac12; Self-Employment Tax</p>' +
	  '<p>C = Contribution Percentage</p>' +
	  
	  '<h3>Step Two: Calculate Estimated  Employer Contribution</h3>' +
					'<p>Estimated Employer Contribution = Lesser of </p>' + 	
					'<ol type="A">' +
					'<li>Estimated Earning Income<sup>2</sup> X Contribution Percentage, or </li>' +
					'<li>Internal Revenue Code 415(c) Contribution Limit for ' + sessionStorage.getItem("Q1A") + '</li>' +
					'</ol>' + 
	'<p style="color:#666; font-size:.9rem;">Note:  If modeling for a future year and one or more of the COLA adjustment figures for the year selected is not yet available, the plan modeling projections for the selected year are approximations based on current-year limitations.</p>' +
	  '<hr />' + 
	  '<h1>Calculation for  ' + sessionStorage.getItem("sess_owner"+own_details_count+"_input_1") + '  ' + sessionStorage.getItem("sess_owner"+own_details_count+"_input_2") + '</h1>' +
	  
	  '<h3>Step One: Calculate Estimated Earned Income</h3>' +
	  '<p>Estimated Earned Income = $' + Math.round(sessionStorage.getItem("owner_" + own_details_count + "_ANBI")).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",") + '</p>' +
	  '<table>' +
		'<tr>' +
		'<td rowspan="2" align="left">$'+ 
		      Math.round(sessionStorage.getItem("owner_" + own_details_count + "_ANBI")).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",") + ' = </td>'              
		      +'<td align="center" style="border-bottom:thin solid #ccc;">$' + 
			  Math.round(sessionStorage.getItem("owner_PSE_"+own_details_count+"")).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",") + 
			  ' - $' + 
			  Math.round(sessionStorage.getItem("owner_" + own_details_count + "_5xSET")).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",") + '</td>' +
			'</tr>' +
			'<tr>' +
			  '<td align="center">'+
			'1.' + sessionStorage.getItem("session_slider_value") + '</td>' +
			'</tr>' +
	  '</table>' +
	  '<p>A = $' + Math.round(sessionStorage.getItem("owner_PSE_"+own_details_count+"")).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",") + '</p>' +
	  '<p>B = $' + Math.round(sessionStorage.getItem("owner_" + own_details_count + "_5xSET")).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",") +'</p>' +
	  '<p>C = 1.' + sessionStorage.getItem("session_slider_value") + '</p>' +
	  
	  '<h3>Step Two: Calculate Estimated  Employer Contribution</h3>' +
					'<p>Estimated  Employer Contribution = $'+ Math.round(sessionStorage.getItem("FERCTBN" + own_details_count)).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",") +'</p>' + 
					'<p>$'+ Math.round(sessionStorage.getItem("FERCTBN" + own_details_count)).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",") +' = Lesser of </p>' + 	
					'<ol type="A">' +
					'<li>' + 
					//PERCTTBN(ERCOMP<sup>2</sup> X Q4C [as decimal]) or,
					'$' + Math.round(sessionStorage.getItem("owner_" + own_details_count + "_PERCTBN")).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",") + 
					' ($'+Math.round(sessionStorage.getItem("owner_" + own_details_count + "_ERCOMP")).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")+
					'<sup>2</sup> X ' + sessionStorage.getItem("sliderValue") + ')' + 
					', or </li>' +
					'<li>$'+sessionStorage.getItem("employee_contribution_amt").toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")+'</li>' +
					'</ol>' + 
					
	  '<p style="color:#666; font-size:.9rem;">Note:  If modeling for a future year and one or more of the COLA adjustment figures for the year selected is not yet available, the plan modeling projections for the selected year are approximations based on current-year limitations.</p>' +
	  
	  '<p style="color:#666; font-size:.9rem;"><sup>1</sup>The estimated deduction for contributions to non-owner employees is based on the aggregate contributions projected for all eligible non-owner employees based on the employer contribution method selected. In the case of multiple business owners, the aggregate projected contributions for non-owner employees are prorated to each business owner based on the ratio of the business owner\'s Estimated Self-Employment Earnings to the aggregate Estimated Self-Employment Earnings of all business owners (regardless of whether all business owners are eligible for participation in the plan).</p>' +
	  
	  '<p style="color:#666; font-size:.9rem;"><sup>2</sup>The maximum amount of Earned Income that may be considered for purposes of contributing to a profit sharing plan for ' + sessionStorage.getItem("Q1A") + ' is <span class="results_dollar_signs">$</span>' + sessionStorage.getItem("comp_cap").toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",") + '&#46;' + '</p>'+
	
		  '</div>');
		}// END owner UNINCORPORATED - (PARTNERSHIP) -------------------------------------------------------------------------
		  else { // is owner CORPORATION (INCORPORATED) ----------------------------------------------------------------------
var age_requirement_YN = $("[name='scr5_age_req']:checked").val();
var match_selected = $("#scr5_age_selector option:selected").val();
var own_detail_year = $("#scr2_date option:selected").val();
var own_w2_amt = sessionStorage.getItem("sess_owner"+own_details_count+"_input_4");
var own_detail_ASD = sessionStorage.getItem("sess_owner"+own_details_count+"_input_5"); 
var own_comp_cap = sessionStorage.getItem("comp_cap");
var results_o_ECTBN  = sessionStorage.getItem("owner_ECTBN_"+own_details_count+"");
if (age_requirement_YN === 'scr_5_age_req_yes'){
	var match_percentage = ".0" + match_selected ; 
	} else {
		var match_percentage = ".02";
		}
		
var result_for_A = own_w2_amt * Number(match_percentage);
			/*if (age_requirement_YN != 'scr_5_age_req_yes'){ // is match
				var result_for_A = own_w2_amt * Number(match_percentage);
				var step_one_own_detail = '<p>Estimated Employer Contribution = Lesser of </p>' + 
				'<ol type="A">' + 
				  '<li>100% of Anticipated Salary Deferral, or</li>' + 
				  '<li>W-2 Income X Matching Contribution Percentage</li>' + 
				'</ol>';
			     var step_one_own_calc = '<p> $'+ Math.round(results_o_ECTBN).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",") +' = Lesser of </p>' + 
										  '<ol type="A">' + 
											'<li><span class="results_dollar_signs">$</span>'+ Math.round(own_detail_ASD).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",") +', or</li>' + 
											'<li><span class="results_dollar_signs">$</span>'+ Math.round(result_for_A).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",") +
											' (<span class="results_dollar_signs">$</span>'+ Math.round(own_w2_amt).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",") + 
											' X '+ match_percentage +')</li>' + 
										  '</ol>';
				var owner_detail_message = '';
			} else { // is nonelective 
					var result_for_A = sessionStorage.getItem("owner_AC_"+own_details_count) * Number(match_percentage);
					var step_one_own_detail = '<p>Estimated Employer Contribution = Estimated W-2 Income<sup>1</sup> X Nonelective Contribution Percentage</p>';
					var step_one_own_calc = '<span class="results_dollar_signs">$</span>'+ Math.round(result_for_A).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",") +
					'  = <span class="results_dollar_signs">$</span>' + Math.round(sessionStorage.getItem("owner_AC_"+own_details_count)).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",") + 
					' X ' + match_percentage + '</p>';
					var owner_detail_message = '<p><sup>1</sup>The maximum amount of Estimated W-2 Income that may be considered for purposes of a 2 percent nonelective employer contribution under a SIMPLE IRA plan for '+ own_detail_year +
				' is <span class="results_dollar_signs">$</span>'+ own_comp_cap.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",") + '&#46;' + 
				'</p>';
				}*/ // end age_requirement_YN for incorporated owners
				
				$("#screen-5").append('<div id="details_o_window_'+ own_details_count +'" style="display:none;">' +
				 '<div class="container_close"><span id="close_o_details_'+ own_details_count +'" class="closeBTN" tabindex="0">X</span></div>' +
				 '<h1>Calculation Key</h1>' +
					'<h3>Calculate Estimated Employer Contribution</h3>' +
					'<p>Estimated Employer Contribution = Lesser of</p>' + 
					'<ol type="A">' + 
					'<li>Estimated W-2 Compensation<sup>1</sup> X Contribution Percentage, or</li>' + 
					'<li>Internal Revenue Code 415(c) Contribution Limit for ' + sessionStorage.getItem("Q1A") + '</li>' + 
					'</ol>' + 
				'<p style="color:#666; font-size:.9rem;">Note:  If modeling for a future year and one or more of the COLA adjustment figures for the year selected is not yet available, the plan modeling projections for the selected year are approximations based on current-year limitations.</p>' +
					'<hr />' +
					'<h1>Calculation for ' + sessionStorage.getItem("sess_owner"+own_details_count+"_input_1") + '  ' + sessionStorage.getItem("sess_owner"+own_details_count+"_input_2") + '</h1>' +
					'<h3>Calculate Estimated Employer Contribution</h3>' +
					'<p>$'+Math.round(sessionStorage.getItem("owner_FCTBN_" + own_details_count)).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")+' = Lesser of</p>' + 
					'<ol type="A">' + 
					'<li>$'+Math.round(sessionStorage.getItem("PCTBN_" + own_details_count)).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")+
					' ($'+sessionStorage.getItem("owner_AC_"+ own_details_count).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")+
					'<sup>1</sup> X '+sessionStorage.getItem("slider_percent")+')'+
					', or</li>' + 
					'<li>$'+sessionStorage.getItem("employee_contribution_amt").toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")+'</li>' + 
					'</ol>' + 
					'<p style="color:#666; font-size:.9rem;">Note:  If modeling for a future year and one or more of the COLA adjustment figures for the year selected is not yet available, the plan modeling projections for the selected year are approximations based on current-year limitations.</p>' +
					'<p style="color:#666; font-size:.9rem;"><sup>1</sup>'+
					'The maximum amount of W-2 Income that may be considered for purposes of contributing to a profit sharing plan for ' + 
					sessionStorage.getItem("Q1A") + ' is $'+sessionStorage.getItem("comp_cap").toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")+''+
					'</p>' +
				  '</div>');
			}// END is owner CORPORATION (INCORPORATED) 
	
	} // end if owner is eligible
	own_details_count ++ ;
 }//END OWNER DETAILS ----------------------------------------------------------------------------------------------------
	  
  //APPEND EMPLOYEE --------------------------------------------------------------------------------
  var employees_applicable = $("input[name='scr2_q1']:checked").val();
  $("td[id^='e_results_row_'],tr[id^='e_results_row_']").remove();
  if (employees_applicable === 'yes'){ 
  
	var employee_row_count = 1;
	  while(employee_row_count <= $("input[name^='scr4_i1_row_']").length){
		  if (sessionStorage.getItem("employee_CV_"+employee_row_count) == 'yes'){
			var results_e_AGCTBN = Math.round(sessionStorage.getItem("employee_AGCTBN_"+employee_row_count+"")).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
			var results_e_FCTBN  = Math.round(sessionStorage.getItem("employee_FCTBN_"+employee_row_count+"")).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
			var input_e_ASD      = Math.round(sessionStorage.getItem("sess_employee"+employee_row_count+"_input_5")).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
			var hidden_dollar = '';
			//console.log('emp ' + employee_row_count + ' CV is yes: ' + sessionStorage.getItem("employee_CV_"+employee_row_count));
			} else {
				var results_e_AGCTBN = "Not Eligible";
				var results_e_FCTBN  = "Not Eligible";
				var input_e_ASD      = "Not Eligible";
				var hidden_dollar = 'hide_dollar';
				//console.log('emp ' + employee_row_count + ' CV is no: ' + sessionStorage.getItem("employee_CV_"+employee_row_count));
				} 
		$("#e_results_row_1").append('<tr id="e_results_row_'+employee_row_count+'">'+
		  '<td id="e_results_row_'+employee_row_count+'_col_1" tabindex="0">'+sessionStorage.getItem("sess_employee"+employee_row_count+"_input_1")+ ' ' +
		  sessionStorage.getItem("sess_employee"+employee_row_count+"_input_2")+'</td>'+
		  '<td id="e_results_row_'+employee_row_count+'_col_2" tabindex="0">No</td>'+
		  '<td id="e_results_row_'+employee_row_count+'_col_3" tabindex="0">'+sessionStorage.getItem("employee_CV_"+employee_row_count+"").charAt(0).toUpperCase() + sessionStorage.getItem("employee_CV_"+employee_row_count+"").slice(1)+'</td>'+
		  '<td id="e_results_row_'+employee_row_count+'_col_5" tabindex="0"><span class="results_dollar_signs '+ hidden_dollar +'">$</span>'+results_e_FCTBN+'</td>'+
		  '<td id="e_results_row_'+employee_row_count+'_col_6" tabindex="0"><span id="e_detail_link_'+employee_row_count+'" style="color:#333333; text-decoration:underline;cursor:pointer;">Details</span></td>'+
		'</tr>' + 
		'');
		employee_row_count ++ ;
		}
	  }//end if employees_applicable is yes
	  
//EMPLOYEE DETAILS   --------------------------------------------------------------------------------------
var emp_details_count = 1;
$("[id^='details_e']").remove();
while(emp_details_count <= $('[id^=scr4_q1_row]').length){
if (sessionStorage.getItem("employee_CV_"+emp_details_count) == 'yes'){
//if (results_e_ECTBN != 'Not Eligible'){
	
	//console.log("session storage for emp CV: " + sessionStorage.getItem("employee_CV_"+emp_details_count) + " (should be 'yes') for emp# " + emp_details_count + " (details)");
	
var age_requirement_YN = $("[name='scr5_age_req']:checked").val();
var match_selected = $("#scr5_age_selector option:selected").val();
var emp_detail_year = $("#scr2_date option:selected").val();
var emp_w2_amt = Math.round(sessionStorage.getItem("sess_employee"+emp_details_count+"_input_4"));
var emp_detail_ASD = Math.round(sessionStorage.getItem("sess_employee"+emp_details_count+"_input_5")); 
var emp_comp_cap = sessionStorage.getItem("comp_cap");
var results_e_AGCTBN = Math.round(sessionStorage.getItem("employee_AGCTBN_"+emp_details_count+"")).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
var results_e_ECTBN  = Math.round(sessionStorage.getItem("employee_ECTBN_"+emp_details_count+"")).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
var input_e_ASD      = Math.round(sessionStorage.getItem("sess_employee"+emp_details_count+"_input_5")).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
if (age_requirement_YN === 'scr_5_age_req_yes'){
	var match_percentage = ".0" + match_selected ; 
	} else {
		var match_percentage = ".02";
		}
	
//console.log("employee " + emp_details_count + ": "+ result_for_A + " (emp_w2_amt - "  + emp_w2_amt + ", (X) match_percentage: " + match_percentage + ")");
			/*if (age_requirement_YN != 'scr_5_age_req_yes'){ // is match
				var step_one_emp_detail = '<p>Estimated Employer Contribution = Lesser of </p>' + 
				'<ol type="A">' + 
				  '<li>100% of Anticipated Salary Deferral, or</li>' + 
				  '<li>W-2 Income X Matching Contribution Percentage</li>' + 
				'</ol>';
				var result_for_A = Math.round(emp_w2_amt * Number(match_percentage));
				var results_for_A_emp = Math.min(result_for_A,emp_detail_ASD);
				//console.log("MATCH: min of: result for A: " + result_for_A + ", and w2 amt: " + emp_w2_amt);
			     var step_one_emp_calc = '<p><span class="results_dollar_signs">$</span>'+ results_for_A_emp.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",") +' = Lesser of </p>' + 
										  '<ol type="A">' + 
											'<li><span class="results_dollar_signs">$</span>'+ emp_detail_ASD.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",") +', or</li>' + 
											'<li><span class="results_dollar_signs">$</span>'+ result_for_A.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",") +
											' (<span class="results_dollar_signs">$</span>'+ Number(emp_w2_amt).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",") + 
											' X '+ match_percentage +
											')</li>' + 
										  '</ol>';
				var employee_detail_message = '';
			} else { // is nonelective 
			//console.log("emp AC: " + sessionStorage.getItem("employee_AC_"+ emp_details_count));
					var results_for_A_emp = Number(sessionStorage.getItem("employee_AC_"+ emp_details_count) * match_percentage) ;
					var step_one_emp_detail = '<p>Estimated Employer Contribution = Estimated W-2 Income<sup>1</sup> X Nonelective Contribution Percentage</p>';
					var step_one_emp_calc = '<p><span class="results_dollar_signs">$</span>'+ results_for_A_emp.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",") +
					'  = <span class="results_dollar_signs">$</span>' + Number(sessionStorage.getItem("employee_AC_"+ emp_details_count)).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",") + 
					' X ' + match_percentage + 
					'</p>';
					var employee_detail_message = '<p><sup>1</sup>The maximum amount of Estimated W-2 Income that may be considered for purposes of a 2 percent nonelective employer contribution under a SIMPLE IRA plan for '+ emp_detail_year +
				' is <span class="results_dollar_signs">$</span>'+ emp_comp_cap.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",") + '&#46;' + 
				'</p>';
				}*/ // end age_requirement_YN for incorporated owners
		} // end - results_e_ECTBN != 'Not Eligible'
				//alert("results_e_ECTBN: " + results_e_ECTBN);
				//if (results_e_ECTBN == 'Not Eligible'){ // is not eligible
				if (sessionStorage.getItem("employee_CV_"+emp_details_count) == 'no'){
				
				//console.log("results_e_ECTBN: " + results_e_ECTBN + " (should be 'no') for emp# " + emp_details_count + " (details)");
				
				$(".results_dollar_signs").hide();
					$("#screen-5").append('<div id="details_e_window_'+ emp_details_count +'" style="display:none;">' +
					   '<div class="container_close"><span id="close_e_details_'+ emp_details_count +'" class="closeBTN" tabindex="0">X</span></div>' +
					   '<h1>Calculation for ' + sessionStorage.getItem("sess_employee"+emp_details_count+"_input_1") + 
					'  ' + sessionStorage.getItem("sess_employee"+emp_details_count+"_input_2") + '</h1>' +
						  '<p>'+sessionStorage.getItem("emp_ineligible_reason"+emp_details_count)+'</p>' +
					  '</div>');
					} else { //is eligible
					$(".results_dollar_signs").show();
					//console.log('results_e_ECTBN is Eligible: ' + results_e_ECTBN);
				$("#screen-5").append('<div id="details_e_window_'+ emp_details_count +'" style="display:none;">' +
				 '<div class="container_close"><span id="close_e_details_'+ emp_details_count +'" class="closeBTN" tabindex="0">X</span></div>' +
				 '<h1>Calculation Key</h1>' +
					'<h3>Calculate Estimated Employer Contribution</h3>' +
					'<p>Estimated Employer Contribution = Lesser of</p>' + 
					'<ol type="A">' + 
					'<li>Estimated W-2 Compensation<sup>1</sup> X Contribution Percentage, or</li>' + 
					'<li>Internal Revenue Code 415(c) Contribution Limit for ' + sessionStorage.getItem("Q1A") + '</li>' + 
					'</ol>' + 
				'<p style="color:#666; font-size:.9rem;">Note:  If modeling for a future year and one or more of the COLA adjustment figures for the year selected is not yet available, the plan modeling projections for the selected year are approximations based on current-year limitations.</p>' +
					'<hr />' +
					'<h1>Calculation for ' + sessionStorage.getItem("sess_employee"+emp_details_count+"_input_1") + 
					'  ' + sessionStorage.getItem("sess_employee"+emp_details_count+"_input_2") + '</h1>' +
					'<h3>Calculate Estimated Employer Contribution</h3>' +
					'<p>Estimated Employer Contribution = $'+Math.round(sessionStorage.getItem("employee_FCTBN_" + emp_details_count)).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")+'</p>' + 
					'<p>$'+Math.round(sessionStorage.getItem("employee_FCTBN_" + emp_details_count)).toString().replace(/\B(?=(\d{3})+(?!\d))/g, "," )+' = Lesser of</p>' + 
					'<ol type="A">' + 
					'<li>$'+Math.round(sessionStorage.getItem("emp_PCTBN_" + emp_details_count)).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")+
					' ($'+sessionStorage.getItem("employee_AC_"+ emp_details_count).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")+'<sup>1</sup> X '+sessionStorage.getItem("slider_percent")+')'+
					', or</li>' + 
					'<li>$'+sessionStorage.getItem("employee_contribution_amt").toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")+'</li>' + 
					'</ol>' + 
					'</p>' +
					'<p style="color:#666; font-size:.9rem;">Note:  If modeling for a future year and one or more of the COLA adjustment figures for the year selected is not yet available, the plan modeling projections for the selected year are approximations based on current-year limitations.</p>' +
					'<p style="color:#666; font-size:.9rem;"><sup>1</sup>'+
					'The maximum amount of W-2 Income that may be considered for purposes of contributing to a profit sharing plan for ' + sessionStorage.getItem("Q1A") + ' is $'+sessionStorage.getItem("comp_cap").toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",")+''+
					'</p>' +
				  '</div>');
				}// end is eligible
emp_details_count ++ ;	
} // end while emp_details_count
	  
   });//end when results form inputs change functio
  //} //end if screen-5 is visible
  $("#continue_button,.calculate").click(function(){
		if($("#screen-5").is(":visible")){
		var hit_status = "includes/track.php?finished&sessionID="+sessionID+"&tool=PSHARE";
		jQuery.ajax({
		   type: "GET",
		   url: hit_status,
		   cache: false,
		   success: function(response)
		   {
			 //("UPDATED 'finished' where session = "+sessionID+"");
		   }
		 });
		}
	  });
 
 // find dollar signs and hide if not eligible
$("#results_options_container_pShare1,results_options_container_pShare2, #btn_continue,#screen-4").click(function (e){
//$("input[name='scr5_service'], #btn_continue,#screen-4").click(function (e){
	if ($("#screen-5").is(":visible") || $("#screen-4").is(":visible") || ($("#screen-3").is(":visible") && $("input[name='scr2_q1']:checked").val() == 'no')){
	setTimeout(function(){
	//console.log(Object.keys(sessionStorage));
	//console.log("number of owners: " + sessionStorage.getItem('owner_rows_added'));
	//var number_of_col4_rows = $("td[id^='o_results_row']").substr(id.length - 5) == 'col_4';
	var number_of_col4_rows = Number(sessionStorage.getItem('owner_rows_added')) + Number(sessionStorage.getItem('employee_rows_added'));
	//console.log('Number of Owner Rows is: ' + number_of_col4_rows);
	var count_col4_rows = 1;
	$(".results_dollar_signs").show();
	$(".hide_dollar").hide();
	/*var text_of_all_rows = $("td[id$='_col_4']").text();
	console.log("Col 4 text for all rows(?): " + text_of_all_rows);
	if (text_of_all_rows == '$Not Eligible') {
		
		}*/
	
	while (count_col4_rows <= number_of_col4_rows){
		//if ($("#o_results_row_" + count_owner_rows + "_col_4:contains('Not')")){
		//var text_of_col4_td = $("#o_results_row_" + count_col4_rows + "_col_4").text();
		//var text_of_row = $("td[id$='results_row_" + count_col4_rows + "_col_4']").text();
		//console.log("Col 4 text for row " + count_col4_rows + ": " + text_of_row);
		//if (text_of_col4_td + ":contains('Not Eligible')")){
		//if ($("#results_table").find('td:eq(4)').text() == '$Not Eligible'){
		//if ($("#o_results_row_" + count_col4_rows + "_col_4").text() == '$Not Eligible'){
		//if (text_of_row == '$Not Eligible'){
			//$('#results_table tr:eq(3) > td:eq(4)')
			//console.log(count_col4_rows + " says Not Eligible");
			//$(".hide_dollar").hide();
			//var id_of_owner_row = "#o_results_row_" + count_owner_rows + "_col_4";
			//var id_of_col4_row = $("#o_results_row_" + count_col4_rows + "_col_4").closest('tr').attr('id');
			//console.log(count_col4_rows + " says Not Eligible... AND TR is: " + id_of_col4_row);
			//}
		count_col4_rows ++ ;
		}
		}, 10); //end timeout functio
		} // end if screen-5 is visible
  });  
 
 
  
}); // end doc ready