Ext.namespace('IDX');
IDX.calMortgagePayment = function (country, loan, rate, periods, freq) {
	if (country == 0) return;
    if (country == 2) {
        var COMPOUND = 2;
        var compound = COMPOUND / 12;
        var monTime = periods * 12;
        var RATE = (rate * 1.0) / 100;
        var yrRate = RATE / COMPOUND;
        var rdefine = Math.pow((1.0 + yrRate), compound) - 1.0;
        var PAYMENT = (loan * rdefine * (Math.pow((1.0 + rdefine), monTime))) / ((Math.pow((1.0 + rdefine), monTime)) - 1.0);

		if (freq == 1) {
            var num = PAYMENT * 12;
        }
		else if (freq == 12) {
            var num = PAYMENT;
        }
        else if (freq == 26 || freq == 24) {
            var num = PAYMENT / 2.0;
        }
        else if (freq == 52) {
            var num = PAYMENT / 4.0;
        }
    } else {
//        var num = (loan * (6 / (freq * 100)) / (1 - Math.pow(1 + (6 / (freq * 100)), -(periods * freq))));
		//http://udub.com/a/mortgage_calculator/
		var Principle = loan;
		var AnnualInt  = parseFloat(rate);
		var MonthlyInt = AnnualInt / (12.0 * 100.0);
		var LenMonths  = periods * 12;
	 
		if(MonthlyInt == 0)
			var MonthlyPay = Principle / LenMonths;
		else
			var MonthlyPay = Principle * ( MonthlyInt / ( 1 - Math.pow((1 + MonthlyInt), -LenMonths) ) );
		var num = Math.round(MonthlyPay * 100) / 100;
	}
	num = Math.round(num);
    num = num.toString().replace(/\$|\,/g, '');
    if (isNaN(num)) num = "0";
    sign = (num == (num = Math.abs(num)));
    num = Math.floor(num * 100 + 0.50000000001);
    cents = num % 100;
    num = Math.floor(num / 100).toString();
    if (cents < 10) cents = "0" + cents;
    for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
    num = num.substring(0, num.length - (4 * i + 3)) + ',' + num.substring(num.length - (4 * i + 3));
    return (((sign) ? '' : '-') + '$' + num);
}//

function calEstimatePayment(loan,rate,amor,freq,dnpercent,dnp){
	if (loan==undefined){
		var loan = price * ( 1- (morDefault[country]["downpayment"])/100);
		var rate = morDefault[country]["rate"];
		var amor = morDefault[country]["amortization"];
		var freq = morDefault[country]["freq"];

		var num = IDX.calMortgagePayment(country, loan,rate,amor,freq);	// LEN
		var desc="Based on "+morDefault[country]["downpayment"]+"% Down Payment at "+rate+"% APR Amortized over "+amor+" Yrs.";
	}else{
		var num = IDX.calMortgagePayment(country, loan,rate,amor,freq);	// LEN
		
		if (!(Ext.isEmpty(dnpercent) || dnpercent == 0 || dnpercent == '0')) var dn = dnpercent+"%";
		else var dn="$"+dnp;
		var desc="Based on "+dn+" Down Payment at "+rate+"% APR Amortized over "+amor+" Yrs.";
	}
	Ext.fly('mor_payment').update(num);
	Ext.fly('mor_freq').update(freqDesc[freq]);

	Ext.fly('mor_desc').update(desc);
}

function morgage_settings(){
    if (RSFTLOGGED == '') {
		top.sendmsg("csignup");
    }else{
		top.sendmsg("m");
	}
}

function printToPDF(){
	var url = window.location.href;
}

function isvalidEmail(email) { var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/; if(reg.test(email) == false) { return false; }else return true; }
function isValidateField(fld){ if (Ext.isEmpty(Ext.getCmp(fld).getValue())){ Ext.getCmp(fld).markInvalid("This field is required."); return false; } param[fld] = Ext.getCmp(fld).getValue(); return true;}
function getFieldValue(fld){ if (!Ext.isEmpty(Ext.getCmp(fld).getValue())) param[fld] = Ext.getCmp(fld).getValue(); }
//google = null; // LEN
var _tid = null;
function MyMarker2(param) {this.latlng_ = param.pos;this.setMap(param.map);this.id = (param.id)?param.id:Ext.id();this.icon = param.img; this.msg = (param.msg)?param.msg:'';this.type=(param.type)?param.type:"L";}

var checkGoogle = function(){
    if (google && google.maps && google.maps.OverlayView){
        if (_tid) clearTimeout(_tid);
		MyMarker2.prototype = new google.maps.OverlayView();
		MyMarker2.prototype.draw = function() {
			var me = this; var div = this.div_;
			if (!div) {
			  div = this.div_ = document.createElement('DIV'); div.setAttribute("id","l-"+this.id);div.style.borderWidth = "0px"; div.style.position = "absolute";div.style.paddingLeft = "0px"; div.style.cursor = 'pointer';
			  div.innerHTML = '<img id="i-'+this.id+'" msg="'+this.msg+'" src="'+this.icon+'"  border=0 org="'+this.icon+'" onclick="" onmouseout="localSearchFocusOff()" onmouseover="localSearchFocusOn(\''+this.id+'\')">';
			  var panes = this.getPanes(); panes.overlayImage.appendChild(div);
			}
			
			var point = this.getProjection().fromLatLngToDivPixel(this.latlng_);if (point) {div.style.left = point.x + 'px';div.style.top = point.y + 'px';}
			
		};
		MyMarker2.prototype.onRemove=function() {if (this.div_) {this.div_.parentNode.removeChild(this.div_);this.div_ = null;};};
		MyMarker2.prototype.get_position= function() {return this.latlng_;};
	
	}else{
        _tid = setTimeout (function(){checkGoogle();}, 20 );
    }
}    
_tid = setTimeout ( function(){checkGoogle();}, 20 );    


function catactForm(){
	document.getElementById('formtype').value='contactme';
	document.getElementById('ct_date').style.display = "none";
	Ext.fly('tab_showingreq').removeClass('idxgcc');Ext.fly('tab_showingreq').addClass('idxgca');Ext.fly('tab_contactme').removeClass('idxgca');Ext.fly('tab_contactme').addClass('idxgcc');
	Ext.getCmp('contact_msg').show();
	Ext.getCmp('contact_msg2').hide();
}

function showReqForm(){
	document.getElementById('ct_date').style.display = "block";
	document.getElementById('formtype').value='showingreq';Ext.fly('tab_contactme').removeClass('idxgcc');Ext.fly('tab_contactme').addClass('idxgca');Ext.fly('tab_showingreq').removeClass('idxgca');Ext.fly('tab_showingreq').addClass('idxgcc');
	Ext.getCmp('contact_msg').hide();
	Ext.getCmp('contact_msg2').show();
}

