/*
Created By: Reverse Mortgage Guides
Website: http://reversemortgagetookit.com
All rights reserved
*/

$page_loaded = false;
$is_ie = false;

if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){ //test for MSIE x.x;
	var ieversion=new Number(RegExp.$1) // capture x.x portion and store as a number
	$is_ie = true;	
}

function stripNonNumeric( str ){
str += '';
var rgx = /^\d|\.|-$/;
var out = '';
for( var i = 0; i < str.length; i++ ){
if( rgx.test( str.charAt(i) ) ){
if( !( ( str.charAt(i) == '.' && out.indexOf( '.' ) != -1 ) ||
( str.charAt(i) == '-' && out.length != 0 ) ) ){
out += str.charAt(i);
}
}
}
return out;
}

function display_errors() {
	
	$error = '';
	
	$raw_home_value = document.getElementById('rmt_customer_home_value').value;
	$home_value = stripNonNumeric($raw_home_value);
	if($home_value <=10000) {
		$error = 'The home value of "' + $raw_home_value + '" was not understood.  Please enter a number greater than $10,000.';
	}

	$raw_zip_code = document.getElementById('rmt_customer_address_zip_code').value;
	if($raw_zip_code.length < 5) {
		$error = 'The zip code "' + $raw_zip_code + '" was not understood.  Please enter a 5-digit zip code.';
	}

	$raw_age = document.getElementById('rmt_customer_person_age').value;
	$age = stripNonNumeric($raw_age);
	if($age < 30 || $age > 105) {
		$error = 'The age "' + $raw_age + '" was not understood.  Please enter an age between 30 and 105 years.';
	}

	if($error.length > 0) {
		document.getElementById('rmt_calc_error_div').innerHTML = $error;
		return false;
	} else {
		document.getElementById('rmt_calc_error_div').innerHTML = '';
		return true;
	}
}



function rmt_calc_submit() 
{

	//Test if the page is fully drawn before you try to submit the form otherwise you will pop a new window
	if(!$page_loaded ) {
		alert('Please wait a moment - the page has not yet finished loading.');
		window.reload();
	}

	calc_form = document.getElementById('rmt_calc_form');
	calc_form.action = 'http://www.reversemortgageguides.org/toolkit/index.php/api/calculator_result';
	calc_form.target = 'rmt_calc_lightbox_iframe';	
	
	$passed = display_errors();
	if(!$passed){
		return false;	
	}

	
	if($is_ie){
		target = document.getElementsByTagName('html')[0];
		target.style.overflow = 'hidden';
	} else {
		target = document.getElementsByTagName('body')[0];
		target.style.overflow = 'hidden';
	}

	overlay = document.getElementById('rmt_calc_overlay');
	lightbox = document.getElementById('rmt_calc_lightbox');
	lightbox_closer = document.getElementById('rmt_calc_lightbox_closer');

	//Position everything manually since page layouts suck.
	overlay.style.width 		= _pageWidth();
	overlay.style.height 		= _pageHeight();

	lightbox_closer.style.left 	= (_pageWidth() - 700) / 2;
	lightbox.style.left 		= (_pageWidth() - 700) / 2;

	lightbox_closer.style.top 	= 25;
	lightbox.style.top 			= 55;


	_changeDisplay(overlay, 'block');
	_changeDisplay(lightbox, 'block');
	_changeDisplay(lightbox_closer, 'block');
	_changeVisibility(overlay, 'visible');
	_changeVisibility(lightbox, 'visible');
	_changeVisibility(lightbox_closer, 'visible');

	var x = document.getElementsByTagName("select");
	for (i = 0; i < x.length; i++) {
		x[i].style.visibility = "hidden";
	}

	var x = document.getElementsByTagName("object");
	for (i = 0; i < x.length; i++) {
		x[i].style.visibility = "hidden";
	}


	scrollTo(0, 0);

	return false;
}



function rmt_calc_close_popup() {
	
	if($is_ie){
		target = document.getElementsByTagName('html')[0];
		target.style.overflow = 'auto';
	} else {
		target = document.getElementsByTagName('body')[0];
		target.style.overflow = 'auto';
	}

	
	lightbox = document.getElementById('rmt_calc_lightbox');
	overlay = document.getElementById('rmt_calc_overlay');
	lightbox_closer = document.getElementById('rmt_calc_lightbox_closer');

	_changeDisplay(overlay, 'none');
	_changeDisplay(lightbox, 'none');
	_changeDisplay(lightbox_closer, 'none');
	_changeVisibility(overlay, 'hidden');
	_changeVisibility(lightbox, 'hidden');
	_changeVisibility(lightbox_closer, 'hidden');

	var x = document.getElementsByTagName("select");
	for (i = 0; i < x.length; i++) {
		x[i].style.visibility = "visible";
	}

	var x = document.getElementsByTagName("object");
	for (i = 0; i < x.length; i++) {
		x[i].style.visibility = "visible";
	}


	//Set focus to lien value rather than age since if focus is on the last element used it won't scroll
	focus_target = document.getElementById('rmt_customer_address_zip_code');
	focus_target.focus();


	return false;
}

function _pageWidth() {
	return window.innerWidth != null? window.innerWidth : document.documentElement && document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body != null ? document.body.clientWidth : null;
} 
function _pageHeight() {
	return  window.innerHeight != null? window.innerHeight : document.documentElement && document.documentElement.clientHeight ?  document.documentElement.clientHeight : document.body != null? document.body.clientHeight : null;
} 


function _changeVisibility(thing, visibility){
	thing.style.visibility = visibility;
	return false;
}

function _changeDisplay(thing, display){
	thing.style.display = display;
	return false;
}


function rmt_calc_form_draw($parameters) {
	
	//Required user_id and tool_id paremeters - may accept more in the future.
	$calc_form_html = "<link rel='stylesheet' type='text/css' href='http://www.reversemortgageguides.org/toolkit/public/css/rmt_calc.css'><form id='rmt_calc_form' method='post' onsubmit='rmt_calc_submit()'><input type='hidden' id='rmt_user_id' name='rmt_user_id' value='" + $parameters['user_id'] + "'/><input type='hidden' id='rmt_tool_id' name='rmt_tool_id' value='" + $parameters['tool_id'] + "'/><table id='rmt_calc_form_table'><tr><td>Home value</td><td><input type='text' size='10' name='rmt_customer_home_value' id='rmt_customer_home_value' value='$'/></td></tr>		<tr><td>Existing mortgages</td><td><input type='text' size='10' name='rmt_customer_lien_value' id='rmt_customer_lien_value' value='$' /></td></tr><tr><td>Property's zip code</td><td><input type='text' size='5' name='rmt_customer_address_zip_code' id='rmt_customer_address_zip_code' value='' /></td></tr><tr><td>Youngest homeowner age</td><td><input type='text' size='3' name='rmt_customer_person_age' id='rmt_customer_person_age' value='' /></td></tr><tr><td colspan='2' id='rmt_calc_calculate_button_td'><input type='submit' value='Calculate' id='rmt_calc_calculate_button'/></td></tr></table></form><div id='rmt_calc_error_div'></div>";
	
	document.write($calc_form_html);

	return false;
}

function rmt_calc_form_overlay_draw() {

	$calc_overlay_html = "<div id='rmt_calc_overlay'></div>\
	<div id='rmt_calc_lightbox_closer'>\
	<div style='padding: 4px 5px 0px 5px; text-align: right; font-size: 18px;'>\
	<a style='color: #333333; font-size: 18px;'  href='http://www.reversemortgageguides.org/toolkit/index.php/api/loading' target='rmt_calc_lightbox_iframe' onclick='rmt_calc_close_popup()'>\
	Close X</a>\
	</div>\
	</div>\
	<div  id='rmt_calc_lightbox'>\
	<iframe id='rmt_calc_lightbox_iframe' name='rmt_calc_lightbox_iframe' frameborder=0 src='http://www.reversemortgageguides.org/toolkit/index.php/api/loading'></iframe>\
	<div id='rmt_calc_disclaimer'>\
	The results of this calculator were generated using the HECM origination calculation specification developed by HUD.  The calculator uses state-specific averages of fees in order to provide an estimate of the net principal limit available to a borrower.  This calculator can not be used in place of a Good Faith Estimate and is not an offer or advertisement for credit.  All liability for representations made by this calculator are fully assumed by the owners of the site displaying this calculator.  The site owners hereby agree to indemnify the developers and publishers of this calculator in all respects.\
	</div>\
	</div>";
	
	bod = document.getElementsByTagName('body')[0];
	bod.innerHTML += $calc_overlay_html;
	
	return false;
}

function rmt_calc_init() {
	$page_loaded = true;
	rmt_calc_form_overlay_draw();
}

window.onload=rmt_calc_init;