// hubby.js

var hubby = null;
var setup_in_progress = null;
function hubby_document_ready()
{
	setup_in_progress = true;
	$('#txtEquitDivH')
		.focus(function(){
			VT_PERCENT.prepare(this.value);
			$(this).val(VT_PERCENT.stripped).select();
		})
		.blur(function(){
			VT_PERCENT.prepare(this.value);
			var _val = Math.bound(VT_PERCENT.value, 0, 100, true);
			VT_PERCENT.prepare(_val);
			this.value = VT_PERCENT.formatted;
			_val = 100 - _val;
			VT_PERCENT.prepare(_val);
			$('#txtEquitDivW').val(VT_PERCENT.formatted);
			do_summary_calculations();
		})
		.trigger('blur');
	$('.mtge_description')
		.bind('blur', function(){
			var _val = $.trim(this.value);
			_val = strip_pipe(_val);
			this.value = _val;
		});
	$('.dollar')
		.focus(function(){
			focus_dollar(this);
		})
		.blur(function(){
			blur_dollar(this);
			do_summary_calculations();
		})
		.trigger('blur');
	hubby = {
		status: 0,
		show: function()
		{
			if ( !this.status )
			{
				$("#generic_bg").bind('click', function(){hubby.hide();}).fadeIn("slow");
				$("#popup_hubby_case").fadeIn("slow");
				this.status = 1;
			}
		},
		hide: function()
		{
			if ( this.status )
			{
				$("#generic_bg").unbind('click').fadeOut("slow");
				$("#popup_hubby_case").fadeOut("slow");
				this.status = 0;
			}
		}
	};
	$('#hubby_link')
		.bind('click', function(){
			hubby.show();
		});
	setup_in_progress = false;
}

function focus_dollar(e)
{
	VT_DOLLAR.prepare(e.value);
	e.value = VT_DOLLAR.stripped;
	e.select();
}

function blur_dollar(e)
{
	VT_DOLLAR.prepare(e.value);
	if ( VT_DOLLAR.value < 0 ) VT_DOLLAR.prepare();
	e.value = VT_DOLLAR.formatted;
}

function do_summary_calculations()
{
	if ( setup_in_progress ) return;
	
	var loop, the_id, _val, last_mtge, net_equity_value, nevH, nevW, nevM, eqdivH, eqdivW, nevM_H, nevM_W, nevH_total, nevW_total;
	var totalH = totalW = totalM = total_contrib = 0;
	for ( loop=0; loop<=10; loop++ )
	{
		the_id = '#txtContribH_' + loop;
		VT_DOLLAR.prepare_from_id(the_id);
		totalH += VT_DOLLAR.value;
		
		the_id = '#txtContribW_' + loop;
		VT_DOLLAR.prepare_from_id(the_id);
		totalW += VT_DOLLAR.value;
		
		the_id = '#txtContribM_' + loop;
		VT_DOLLAR.prepare_from_id(the_id);
		totalM += VT_DOLLAR.value;
		
		total_contrib = totalH + totalW + totalM;
	}
	VT_DOLLAR.prepare(totalH);
	$('#txtContribH_Total').val(VT_DOLLAR.formatted);
	VT_DOLLAR.prepare(totalW);
	$('#txtContribW_Total').val(VT_DOLLAR.formatted);
	VT_DOLLAR.prepare(totalM);
	$('#txtContribM_Total').val(VT_DOLLAR.formatted);
	VT_DOLLAR.prepare(total_contrib);
	$('#txtContrib_GrandTotal').val(VT_DOLLAR.formatted);
	
	var pctH = pctW = pctM = 0;
	if ( (totalH == 0) && (totalW == 0) && (totalM == 0) )
	{
		$('#txtContribH_Share,#txtContribW_Share,#txtContribM_Share').val('-');
	}
	else
	{
		pctH = 100 * totalH / total_contrib;
		VT_PERCENT.prepare(pctH);
		_val = pctH ? VT_PERCENT.formatted : '-';
		$('#txtContribH_Share').val(_val);
		
		pctW = 100 * totalW / total_contrib;
		VT_PERCENT.prepare(pctW);
		_val = pctW ? VT_PERCENT.formatted : '-';
		$('#txtContribW_Share').val(_val);
		
		pctM = 100 * totalM / total_contrib;
		VT_PERCENT.prepare(pctM);
		_val = pctM ? VT_PERCENT.formatted : '-';
		$('#txtContribM_Share').val(_val);
	}
	
	last_mtge = get_last_mortgage();
	the_id = '#txtMortgage_' + last_mtge + '_Balance';
	VT_DOLLAR.prepare_from_id(the_id);
	last_mtge = VT_DOLLAR.value;
	VT_DOLLAR.prepare_from_id('#txtCurValue');
	net_equity_value = VT_DOLLAR.value - last_mtge;
	VT_DOLLAR.prepare(net_equity_value);
	$('#txtNetEquity_GrandTotal,#txtNetEquity_GrandTotal2').val(VT_DOLLAR.formatted);
	
	nevH = net_equity_value * pctH / 100;
	VT_DOLLAR.prepare(nevH);
	$('#txtNetEquityH').val(VT_DOLLAR.formatted);
	
	nevW = net_equity_value * pctW / 100;
	VT_DOLLAR.prepare(nevW);
	$('#txtNetEquityW').val(VT_DOLLAR.formatted);
	
	nevM = net_equity_value * pctM / 100;
	VT_DOLLAR.prepare(nevM);
	$('#txtNetEquityM,#txtNetEquityM_Total').val(VT_DOLLAR.formatted);
	
	eqdivH = parseInt($('#txtEquitDivH').val()) / 100;
	nevM_H = nevM * eqdivH;
	VT_DOLLAR.prepare(nevM_H);
	$('#txtNetEquityM_H').val(VT_DOLLAR.formatted);
	
	eqdivW = 1 - eqdivH;
	nevM_W = nevM * eqdivW;
	VT_DOLLAR.prepare(nevM_W);
	$('#txtNetEquityM_W').val(VT_DOLLAR.formatted);
	
	nevH_total = nevH + nevM_H;
	VT_DOLLAR.prepare(nevH_total);
	$('#txtNetEquityH_Total').val(VT_DOLLAR.formatted);
	
	nevW_total = nevW + nevM_W;
	VT_DOLLAR.prepare(nevW_total);
	$('#txtNetEquityW_Total').val(VT_DOLLAR.formatted);
}

function do_mortgage_balance(which)
{
	VT_DOLLAR.prepare_from_id('#txtMortgage_'+which);
	var val_mtge = VT_DOLLAR.value;
	VT_DOLLAR.prepare_from_id('#txtContribH_'+which);
	var val_H = VT_DOLLAR.value;
	VT_DOLLAR.prepare_from_id('#txtContribW_'+which);
	var val_W = VT_DOLLAR.value;
	VT_DOLLAR.prepare_from_id('#txtContribM_'+which);
	var val_M = VT_DOLLAR.value;
	
	var val_bal = val_mtge - (val_H + val_W + val_M);
	val_bal = Math.max(val_bal, 0);
	VT_DOLLAR.prepare(val_bal);
	$('#txtMortgage_'+which+'_Balance').val(VT_DOLLAR.formatted);
	
	do_summary_calculations();
}

function add_mortgage()
{
	var loop, next_row, the_id;
	next_row = 0;
	for ( loop=10; loop>1; loop-- )
	{
		the_id = '#trMtge' + loop + 'a';
		if ( $(the_id).css('display') == 'none' )
		{
			next_row = loop;
		}
	}
	if ( next_row )
	{
		the_id = '#trMtge' + next_row + 'a';
		$(the_id).css('display','');
		the_id = '#trMtge' + next_row + 'b';
		$(the_id).css('display','');
		the_id = '#trMtge' + next_row + 'c';
		$(the_id).css('display','');
		$('#butRemoveMtge').attr('disabled','');
	}
	if ( next_row == 10 )
	{
		$('#butAddMtge').attr('disabled','disabled');
	}
}

function remove_mortgage()
{
	var last_row = get_last_mortgage();
	if ( last_row > 1 )
	{
		the_id = '#txtMortgage_' + last_row + ', #txtContribH_' + last_row + ', #txtContribW_' + last_row + ', #txtContribM_' + last_row + ', #txtMortgage_' + last_row + '_Balance';
		$(the_id).val('0.00');
		the_id = '#trMtge' + last_row + 'a';
		$(the_id).css('display','none');
		the_id = '#trMtge' + last_row + 'b';
		$(the_id).css('display','none');
		the_id = '#trMtge' + last_row + 'c';
		$(the_id).css('display','none');
		$('#butAddMtge').attr('disabled','');
	}
	if ( last_row == 2 )
	{
		$('#butRemoveMtge').attr('disabled','disabled');
	}
}

function get_last_mortgage()
{
	var loop, last_row, the_id;
	last_row = 0;
	for ( loop=1; loop<=10; loop++ )
	{
		the_id = '#trMtge' + loop + 'a';
		if ( $(the_id).css('display') != 'none' )
		{
			last_row = loop;
		}
	}
	return last_row;
}

function toggle_hubby_style()
{
	if ( $('#chxHubbyHeader').attr('checked') )
	{
		$('#txtHubbyHeader').attr('disabled','');
		$('#hubby_style').html($('#txtHubbyHeader').val());
	}
	else
	{
		$('#txtHubbyHeader').attr('disabled','disabled');
		$('#hubby_style').html('');
	}
}

function update_hubby_style()
{
	$('#hubby_style').html($('#txtHubbyHeader').val());
}

function save_file()
{
	if ( false == save_file_GATEKEEPER() ) return;
	save_file_SETUP_AJAX();
	ajax.onCompletion = save_file_AJAX_COMPLETE;
	ajax.runAJAX();
}

function save_file_GATEKEEPER()
{
	$('#butSaveFile').attr('disabled', true);
	return true;
}

function save_file_SETUP_AJAX()
{
	ajax = new sack();
	ajax.setVar('HEADER', $('#txtHubbyHeader').val());
	VT_DOLLAR.prepare_from_id('#txtCurValue');
	ajax.setVar('CURRENT_VALUE', VT_DOLLAR.stripped);
	VT_DOLLAR.prepare_from_id('#txtEquitDivH');
	ajax.setVar('EQUITABLE_DIVISION', VT_DOLLAR.stripped);
	VT_DOLLAR.prepare_from_id('#txtContribH_0');
	ajax.setVar('CONTRIB_H', VT_DOLLAR.stripped);
	VT_DOLLAR.prepare_from_id('#txtContribW_0');
	ajax.setVar('CONTRIB_W', VT_DOLLAR.stripped);
	VT_DOLLAR.prepare_from_id('#txtContribM_0');
	ajax.setVar('CONTRIB_M', VT_DOLLAR.stripped);
	var the_id, loop, loop_ct;
	loop_ct = get_last_mortgage();
	for ( var loop=1; loop<=loop_ct; loop++ )
	{
		the_id = '#txtMortgage_' + loop + '_Description';
		ajax.setVar('DESCRIPTION'+loop, $(the_id).val());
		the_id = '#txtMortgage_' + loop;
		VT_DOLLAR.prepare_from_id(the_id);
		ajax.setVar('MORTGAGE'+loop, VT_DOLLAR.stripped);
		the_id = '#txtContribH_' + loop;
		VT_DOLLAR.prepare_from_id(the_id);
		ajax.setVar('CONTRIB_H'+loop, VT_DOLLAR.stripped);
		the_id = '#txtContribW_' + loop;
		VT_DOLLAR.prepare_from_id(the_id);
		ajax.setVar('CONTRIB_W'+loop, VT_DOLLAR.stripped);
		the_id = '#txtContribM_' + loop;
		VT_DOLLAR.prepare_from_id(the_id);
		ajax.setVar('CONTRIB_M'+loop, VT_DOLLAR.stripped);
	}
	ajax.setVar("action", '1');
	ajax.setVar("submission", 'hubby');
	ajax.requestFile = "backdoor.php";
}

function save_file_AJAX_COMPLETE()
{
	if ( typeof ajax.response != 'undefined' )
	{
		var work = ajax.response.split('|');
		safe_alert(work[1]);
		if ( parseInt(work[0]) != 0 )
		{
			do_download('download.php?fn='+work[2]+'&type=hubby');
		}
	}
	$('#butSaveFile').attr('disabled', false);
}

function load_file()
{
	if ( $('#fileForUpload').val() == '' )
	{
		safe_alert('Please specify a file to upload.');
		return false;
	}
	$('#formFileUploader').submit();
}

function load_file_COMPLETE(status)
{
	switch ( parseInt(status) )
	{
		case 1:
			safe_alert('The file you uploaded does not appear to be a valid Hubby Calculator file.  If you believe this is an error, please contact PeachForms.com immediately for further assistance.');
			return false;
		case 2:
			safe_alert('There was a problem uploading your file.  Please contact PeachForms.com immediately for further assistance.');
			return false;
		case 3:
			safe_alert('The ability to load data into a PeachForms.com application is only available to current subscribers.  Your subscription has expired.');
			return false;
		case 4:
			safe_alert('Please log in first.');
			return false;
		default:
			redirect('index.php?page=hubby');
			return true;
	}
}

function reset_display()
{
	if ( false == reset_display_GATEKEEPER() ) return;
	reset_display_SETUP_AJAX();
	ajax.onCompletion = reset_display_AJAX_COMPLETE;
	ajax.runAJAX();
}

function reset_display_GATEKEEPER()
{
	return true;
}

function reset_display_SETUP_AJAX()
{
	ajax = new sack();
	ajax.setVar("action", '2');
	ajax.setVar("submission", 'hubby');
	ajax.requestFile = "backdoor.php";
}

function reset_display_AJAX_COMPLETE()
{
	if ( typeof ajax.response != 'undefined' )
	{
		redirect('index.php?page=hubby');
	}
}

// end of file
