	//MMortCalc Class.. 
	//
	//Note: some of the caculations for this object were used from
	//Dave Tufts [dt] <dave@imarc.net> php Mortage Calc
	//
	//Written by : Mark Dean Fitzgerald (fitz)
	//Date: November 11/24/2008
	//
	function MyrsolMortCalc(aOptions)
	{
		var m_thisobject = this;
		var m_values = new Array();

		//Options for this Object
		var m_formid = aOptions.formid || '';
		var m_priceid = aOptions.priceid || '';
		var m_price = aOptions.price || '';
		var m_getpriceid = aOptions.getpriceid || '';
		var m_percdownid = aOptions.percdownid || '';
		var m_amountdownid = aOptions.amountdownid || '';
		var m_termid = aOptions.termid || '';
		var m_interestrateid = aOptions.interestrateid || '';
		var m_assessedamountid = aOptions.assessedamountid || '';
		var m_taxrateid = aOptions.taxrateid || '';
		var m_poafeesid = aOptions.poafeesid || '';
		var m_totalid = aOptions.totalid || '';
		var m_totalformatstr = aOptions.totalformatstr || 'Your total monthly payment is <strong>[TOTAL]</strong>';
		var m_addcommas = aOptions.addcommas || true;
		
		MMC_Init();
		MMC_Compute();

		//-----------------------------------------
		//Access Functions
		//-----------------------------------------
		
		//MMC_Init
		//////////////////////////////	
		this.MMC_Init = MMC_Init;
		function MMC_Init()
		{
			var iprice = 0;
			 m_values = new Array();
			
			if (m_price && m_price != '')
			{	
				m_price = MMC_CleanNumber(m_price);
				m_price = MMC_FormatNumber(m_price,2);
				$(m_priceid).value = m_price;		
			}
			else
			{
				if (typeof(m_getpriceid) != 'undefined' && $(m_getpriceid) != null && $(m_getpriceid).value != ''){
					tvalue = $(m_getpriceid).innerHTML;
					tvalue = MMC_CleanNumberById(m_getpriceid);
					tvalue = MMC_FormatNumber(tvalue,2);
					$(m_priceid).value = tvalue;
				}
				else
				{
					$(m_priceid).value = iprice;		
				}
			}
			
			//We need to be able to change Assessed home value when the price changes.
			Event.observe($(m_priceid), 'change', function(){MMC_ResetOfPrice_CB();});
			
			//Save values
			if ($(m_priceid)){
				MMC_AddToReset(m_priceid);
			}
			if ($(m_percdownid)){
				MMC_AddToReset(m_percdownid);
			}
			if ($(m_amountdownid)){
				MMC_AddToReset(m_amountdownid);
			}
			if ($(m_termid)){
				MMC_AddToReset(m_termid);
			}
			if ($(m_interestrateid)){
				MMC_AddToReset(m_interestrateid);
			}
			if ($(m_assessedamountid)){
				MMC_AddToReset(m_assessedamountid);
			}
			if ($(m_taxrateid)){
				MMC_AddToReset(m_taxrateid);
			}
			if ($(m_poafeesid)){
				MMC_AddToReset(m_poafeesid);
			}
			if ($(m_totalid)){
				MMC_AddToReset(m_totalid);
			}
			
			return true;
		} //End MMC_Init()

		//MMC_ResetOfPrice_CB
		//////////////////////////////	
		this.MMC_ResetOfPrice_CB = MMC_ResetOfPrice_CB;
		function MMC_ResetOfPrice_CB()
		{
			$(m_assessedamountid).value = '';
			MMC_Compute();
		} //MMC_AddToReset


		//MMC_AddToReset
		//////////////////////////////	
		this.MMC_AddToReset = MMC_AddToReset;
		function MMC_AddToReset(elimid)
		{
			var odata = new Object;
			odata.value = $(elimid).value;
			odata.id = elimid;
			m_values.push(odata);
		} //MMC_AddToReset

		//MMC_Reset
		//////////////////////////////	
		this.MMC_Reset = MMC_Reset;
		function MMC_Reset()
		{
			for (var x=0;x<m_values.length;x++)
			{
				var iItem = m_values[x];
				$(iItem.id).value = iItem.value;
			}
			
			MMC_Compute();
		} //MMC_Reset
		

		//MMC_Compute
		//////////////////////////////	
		this.MMC_Compute = MMC_Compute;
		function MMC_Compute()
		{
			var pmi_month	= 0;
			var total_month = 0;
			var amountDown = 0;
			var assessedValue = 0;
			
			
			if (($(m_termid).value <= 0) || ($(m_priceid).value < 0) || ($(m_interestrateid).value <= 0)) 
			{
				alert('You must enter:\n- Sale Price\n- Length of Mortgage\n- Annual Interest Rate');
				return false;
			}

			//Format price
			aprice = MMC_CleanNumberById(m_priceid);
			aprice = aprice.split('.');
			salePrice = aprice[0]; //get rid of cents
			$(m_priceid).value = MMC_FormatNumber(salePrice,2);

			//Compute max pmi
			maxpmi = salePrice *  .20;

			//Figure out the assessed amount
			assessedpercent = .85;
			if ( $(m_assessedamountid) != null && $(m_assessedamountid).value > 0 && $(m_assessedamountid).value != '')
			{
				tvalue = MMC_CleanNumberById(m_assessedamountid);
				tvalue = tvalue.split('.');
				assessedValue = tvalue[0]; //get rid of cents
				assessedValue = assessedValue.replace(/[,.$]/gi,'');
			}
			else{
				assessedValue = salePrice * assessedpercent;
				$(m_assessedamountid).value = total_month = MMC_FormatNumber(assessedValue,2);
			}
			
			//Figure out amount down.
			if ($(m_percdownid) != null && $(m_percdownid).value != '' && $(m_percdownid).value != 0)
			{
				amountpercent = MMC_CleanNumberById(m_percdownid);
				if (amountpercent >=1.0) //keep base 100
				{
					amountpercent/=100;
					//$(m_percdownid).value = amountpercent;
				}

				amountDown = salePrice*amountpercent;
				$(m_amountdownid).value = '';
			}
			else
			{
				if ($(m_amountdownid) != null && $(m_amountdownid).value != '' && $(m_amountdownid).value != '')
				{
					amountDown = $(m_amountdownid).value;
					amountDown = MMC_CleanNumberById(m_amountdownid);
					$(m_percdownid).value = '';
				}
				else	
					amountDown = 0;
			}
			
			//Term
			if ($(m_termid) != null && $(m_termid).value >=0)
			{
				iterm = $(m_termid).value;
				iterm = MMC_CleanNumberById(m_termid);
			}
			else
				iterm = 30;
			
			//Interest Rate
			if ($(m_interestrateid) != null && $(m_interestrateid).value != '')
			{
				iInterestRate = $(m_interestrateid).value;
				iInterestRate = MMC_CleanNumberById(m_interestrateid);
				if (iInterestRate >=1.0) //keep base 100
				{
					iInterestRate/=100;
					//$(m_interestrateid).value = iInterestRate;

				}
			}
			else
				iInterestRate = .50;
				
			//taxrate id	
			if ($(m_taxrateid) != null && $(m_taxrateid).value != '')
			{
				itaxrate = $(m_taxrateid).value;
				itaxrate = MMC_CleanNumberById(m_taxrateid);
			}
			else
				itaxrate = 0;
			
			//poa
			if ($(m_poafeesid) != null && $(m_poafeesid).value != '')
			{
				ipoa = $(m_poafeesid).value;
				ipoa = MMC_CleanNumberById(m_poafeesid);
			}
			else
				ipoa = 0;
			
			// calculations
			var month_term             	= parseFloat(iterm) * 12;
			var down_payment           	= parseFloat(amountDown);
			var annual_interest_rate   	= parseFloat(iInterestRate); // / 100;
			var monthly_interest_rate  	= parseFloat(annual_interest_rate) / 12;
			var financing_price        	= parseFloat(salePrice) - parseFloat(down_payment);
			var monthly_payment			= parseFloat(financing_price) / parseFloat(MMC_InterestFactor(iterm, monthly_interest_rate));
			var property_yearly_tax		= (parseFloat(assessedValue) / 1000) * parseFloat(itaxrate);
			var property_monthly_tax	= property_yearly_tax / 12;
		
			if (down_payment <=  maxpmi) 
			{
				pmi_month = 55 * (financing_price / 100000);
			}

			//compute monthly
			total_month	= parseFloat(monthly_payment) + parseFloat(pmi_month) + parseFloat(property_monthly_tax) + parseFloat(ipoa);
			
			//format monthly			
			total_month = MMC_FormatNumber(total_month,2);
			
	// calculations
/*
	$month_term              = $year_term * 12;
	$down_payment            = $sale_price * ($down_percent / 100);
	$annual_interest_rate    = $mortgage_interest_percent / 100;
	$monthly_interest_rate   = $annual_interest_rate / 12;
	$financing_price         = $sale_price - $down_payment;
	$monthly_payment         = $financing_price / _get_interest_factor($year_term, $monthly_interest_rate);
	$property_yearly_tax     = ($assessed_value / 1000) * $property_tax_rate;
	$property_monthly_tax    = $property_yearly_tax / 12;

	if ($down_percent < 20) { 
		$pmi_per_month       = 55 * ($financing_price / 100000);
	}

	$total_monthly_bill      = $monthly_payment + $pmi_per_month + $property_monthly_tax + $condo_fee;			
*/			
			
			//Populate the Monthly
			sztotal = m_totalformatstr;
			sztotal = sztotal.replace(/\[TOTAL\]/gi,total_month);
			//sztotal +='<br/>Term: '+month_term;
			//sztotal +='<br/>down_payment: '+down_payment;
			//sztotal +='<br/>annual_interest_rate: '+annual_interest_rate;
			//sztotal +='<br/>monthly_interest_rate: '+monthly_interest_rate;
			//sztotal +='<br/>financing_price: '+financing_price;
			//sztotal +='<br/>monthly_payment: '+monthly_payment;
			//sztotal +='<br/>['+$(m_taxrateid).value+'] property_yearly_tax: '+property_yearly_tax;
			//sztotal +='<br/>property_monthly_tax: '+property_monthly_tax;
			$(m_totalid).update(sztotal);
			return total_month;		
		} //MMC_Compute


		//MMC_InterestFactor
		this.MMC_InterestFactor = MMC_InterestFactor;
		function MMC_InterestFactor(year_term, monthly_interest_rate)
		{
			var factor      = 0;
			var base_rate   = 1 + monthly_interest_rate;
			var denominator = base_rate;
			for (var i=0; i < (year_term * 12); i++) {
				factor += (1 / denominator);
				denominator *= base_rate;
			}
			return factor;
		}
		
		//MMC_CleanNumberById
		//////////////////////////////	
		this.MMC_CleanNumberById = MMC_CleanNumberById;
		function MMC_CleanNumberById(elemid)
		{
			//Clean number
			if ($(elemid).value){
				var ivalue = $(elemid).value;
				ivalue = MMC_CleanNumber(ivalue);
				$(elemid).value = ivalue;
			}
			else if ($(elemid)){
				var ivalue = $(elemid).innerHTML;
				ivalue = MMC_CleanNumber(ivalue);
				$(elemid).value = ivalue;
			}
			else
				var ivalue=0;	
				
			return ivalue;
		} // MMC_CleanNumber

		//MMC_CleanNumber
		//////////////////////////////	
		this.MMC_CleanNumber = MMC_CleanNumber;
		function MMC_CleanNumber(value)
		{
			//Clean number
			value = value.replace(/[^0-9\.]/gi,'');

			return value;
		} // MMC_CleanNumber

		//MMC_FormatNumber
		//////////////////////////////	
		this.MMC_FormatNumber= MMC_FormatNumber;
		function MMC_FormatNumber(pnumber,decimals)
		{
		   if (isNaN(pnumber)) { return 0};
		   if (pnumber=='') { return 0};

		   var snum = new String(pnumber);
		   var sec = snum.split('.');
		   var whole = parseFloat(sec[0]);
	   
		   var result = '';
		   if(sec.length > 1){
			   var dec = new String(sec[1]);
			   dec = String(parseFloat(sec[1])/Math.pow(10,(dec.length - decimals)));
			   dec = String(whole + Math.round(parseFloat(dec))/Math.pow(10,decimals));
			   var dot = dec.indexOf('.');
			   if(dot == -1){
				   dec += '.';
				   dot = dec.indexOf('.');
			   }
			   while(dec.length <= dot + decimals) { dec += '0'; }
			   result = dec;
		   } else{
			   var dot;
			   var dec = new String(whole);
			   dec += '.';
			   dot = dec.indexOf('.');       
			   while(dec.length <= dot + decimals) { dec += '0'; }
			   result = dec;
		   }
		   
		   if (m_addcommas)
		   	result = MMC_AddCommas(result);
		   return result;
	   } //MMC_FormatNumber
	   
	   
		//MMC_AddCommas
		//////////////////////////////	
		this.MMC_AddCommas= MMC_AddCommas;
		function MMC_AddCommas(nStr)
		{
			nStr += '';
			x = nStr.split('.');
			x1 = x[0];
			x2 = x.length > 1 ? '.' + x[1] : '';
			var rgx = /(\d+)(\d{3})/;
			while (rgx.test(x1)) {
				x1 = x1.replace(rgx, '$1' + ',' + '$2');
			}
			return '$'+x1 + x2;
		} // MMC_AddCommas
	   
		
	} //End 
