/* ####################################################################
#   © 2008 JonWilde.net & The Martino Group    All rights reserved.   #
#                                                                     #
#   This file and its contents are protected under U.S. copyright     #
#   law. Any unauthorized alteration, distribution, or reproduction   #
#   will be prosecuted to the maximum possible extent of the law.     #
#                                                                     #
#                       www.TheMartinoGroup.com                       #
#################################################################### */

$(document).ready(function() {

	// Navigation Menu toggle script
	var toggle = function(direction, display) {
		return function() {
			var self	= this;
			var ul		= $("ul", this);
			if ( ul.css("display") == display && !self["block" + direction] )
			{
				self["block" + direction] = true;
				ul["slide" + direction]("fast", function() {
					self["block" + direction] = false;
				});
			}
		};
	}
	$("li.subMenu").hover(toggle("Down", "none"), toggle("Up", "block"));
	$("li.subMenu ul").hide();

});


function updateProfile()
{
	document.formMain.process.value = 'updateUser';
	document.formMain.submit();
}

function go(loc) {
	window.location.href = loc;
}


///////////////////////////////////
//// Begin Mortgage Calculator ////
///////////////////////////////////

// Principal amount
//	a: original amount
//	d: down payment
//	p: true if down payment is a percentage
//
function calculatePrincipal(a,d,p)
{
	var amount	= 0;
	if (p != null && p) {
		amount	= a - (a * d / 100);
	}
	else {
		amount	= a - d;
	}
	return Math.round(amount*100)/100
}

// Mortgage calcualtion
//	p: Principal amount
//	i: interest rate (yearly)
//	n: Number of years
//	q: Number of installments (payments) per year
//
//	return = pi/[q(1-[1+(i/q)] ^(-nq) )].
//
function mortgage(p,i,n,q)
{
	return (p*i)/(q * (1- Math.pow( (1+(i/q)), (-(n*q)) )));
}

// Monthly payment:
//	m:	mortgage payment
//	t:	yearly taxes
//	i:	yearly insurance
//
function monthlyPayment(m,t,i)
{
	return (m+((t+i)/12));
}

function calculateMortgage()
{
	var princ	= stripFormatting( document.formMain.mAmount.value );

	var rate	= document.formMain.mRate.value/100;
	var term	= document.formMain.mTerm.value;
	var down	= stripFormatting( document.formMain.mDown.value );
	princ		= calculatePrincipal(princ,down,false);
	var payment	= mortgage( princ, rate, term, 12 );
	var mTax	= stripFormatting( document.formMain.mTax.value ) / 12;
	var mIns	= stripFormatting( document.formMain.mInsur.value ) / 12;


	document.formMain.mCalcBase.value	= formatCurrency(payment);
	document.formMain.mCalcTax.value	= formatCurrency(mTax);
	document.formMain.mCalcInsur.value	= formatCurrency(mIns);
	document.formMain.mCalcTotal.value	= formatCurrency(payment + mTax + mIns);

}

function formatCurrency(amount)
{
	amount		= (isNaN(amount)) ? "0" : ""+Math.floor(amount * 100);
	while ( amount.length < 3){ amount = "0" + amount;}
	var endIx	= amount.length-3;
	var format	= "$";
	for (var i=0; i<=endIx; i++) {
		format	+= amount.charAt(i);
		if ((endIx-i)%3==0 && endIx!=i) {
			format	+= ",";
		}
	}
	format	+= "." + amount.substring( endIx+1, amount.length );
	return format;
}

function correctFormat(element,stripDecimal)
{
	var val			= stripFormatting(element.value);
	val				= formatCurrency(val);
	if (stripDecimal) {
		val			= val.replace(/\..+$/, "");
	}
	element.value	= val;
}

function stripFormatting(value)
{
	return value.replace(/[$,]/g, "");
}
