// JavaScript Document

function calcMort(){
	var sp; 		// sale price variable
	var ir; 		// interest rate variable
	var dp; 		// down payment variable
	var dpa; 	// down payment amount variable
	var ltv;		// loan to value ration variable
	var la; 		// loan amount variable
	var mm;		// monthly maintenance variable
	var ml; 		// mortgage length variable
	// set mortgage length	
	ml=$F('mortL');
	// set sale price
	sp=$F('saleprice');
	//check number
	sp=checkNum(sp);
	// set interest rate
	ir=$F('interest');
	//check number
	ir=checkNum(ir);
	//divide by 100 for percentage value
	ir=ir/100;
	// set down payment
	dp=$F('downP');
	//check number
	dp=checkNum(dp);
	//set ltv value
	ltv=100-dp;
	//divide by 100 for percentage value
	dp=dp/100;
	//set down payment amount
	dpa=dp*sp;
	//set loan amount
	la=sp-dpa;
	// set monthly maintenance
	// mm=$F('maint');
	//check number
	// mm=checkNum(mm);
	// check if sales price is at 0 it must be changed
	if (sp==0){
		alert('Attention:\nIn order to get correct estimates the sales price can not be left blank or set to 0.');	
		return false;
	}
	// check if interest rate is at 0 it must be changed
	if (ir==0){
		alert('Attention:\nIn order to get correct estimates the interest rate can not be left blank or set to 0. The value must be 0.01 or higher');	
		return false;
	}
	// set downpayment amounts
	$('dp1').innerHTML= formatCurrency2(dpa);
	//set ltv value
	$('ltv1').innerHTML=ltv+"%";
	//set loan amount
	$('la1').innerHTML= formatCurrency2(la);
	//set mortgage payment #1
	mp_0=calculate_payment(la,ir/12,ml*12);
	//set mortgage payment #2
	mp_1=(la*ir)/12;
	mp_2=round_decimals((0.02*sp)/12,2);
	mp_3=round_decimals((0.00375*sp)/12,2);
	mp_4=round_decimals(mm,2);
	tt_1=mp_0+mp_2+mp_3+mp_4;
	tt_2=mp_1+mp_2+mp_3+mp_4;
	$('mp1').innerHTML = formatCurrency(mp_0);
	$('mp2').innerHTML = formatCurrency(mp_1);
	/*
	$('mt1').innerHTML = formatCurrency(mp_2);
	$('mi1').innerHTML = formatCurrency(mp_3);
	$('mm1').innerHTML = formatCurrency(mp_4);
	$('tm1').innerHTML = formatCurrency(tt_1);
	$('mt2').innerHTML = formatCurrency(mp_2);
	$('mi2').innerHTML = formatCurrency(mp_3);
	$('mm2').innerHTML = formatCurrency(mp_4);
	$('tm2').innerHTML = formatCurrency(tt_2);
	*/
}

function resetCalc(){
	$('dp1').innerHTML=  '$&nbsp;';
	$('ltv1').innerHTML= '&nbsp;';
	$('la1').innerHTML=  '$&nbsp;';
	$('mp1').innerHTML = '$&nbsp;';
	$('mp2').innerHTML = '$&nbsp;';
	/*
	$('mt1').innerHTML = '$&nbsp;';
	$('mi1').innerHTML = '$&nbsp;';
	$('mm1').innerHTML ='$&nbsp;';
	$('tm1').innerHTML = '$&nbsp;';
	$('mt2').innerHTML = '$&nbsp;';
	$('mi2').innerHTML = '$&nbsp;';
	$('mm2').innerHTML = '$&nbsp;';
	$('tm2').innerHTML = '$&nbsp;';	
	*/
}

function checkNum(theVal){
	theVal=theVal.replace(/,/g,"");
	if ( theVal =="" || isNaN(theVal) ){
		theVal=0;
	}
	return theVal;
}

function formatCurrency(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 + '.' + cents);
}

function formatCurrency2(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 calculate_payment(PV, IR, NP) {
  var PMT = (PV * IR) / (1 - Math.pow(1 + IR, -NP))
  return round_decimals(PMT, 2)
}

function round_decimals(original_number, decimals) {
  var result1 = original_number * Math.pow(10, decimals)
  var result2 = Math.round(result1)
  var result3 = result2 / Math.pow(10, decimals)
  return (result3)
}

// REGISTRATION VALIDATION EXTRA
function valPwd(){
	var pwdCheck=/^(?=.*\w).{6,16}$/;
	
	if ( document.memberFrm.password1.value.length>0  && document.memberFrm.password1.value.search(pwdCheck)==-1){
		alert('A 6-16 Alpha Numeric Password Required.')
		document.memberFrm.password1.focus();
		return false;
	}
	
	if ( document.memberFrm.password1.value != document.memberFrm.password2.value ){
		alert('Your password did not match the confirmation password. Please enter again.');
		document.memberFrm.password2.focus();
		return false;
	}
	return true;
}


// open a Centered Window
function openWin(url,win,myWidth,myHeight){
	openWinHandler(url,win,myWidth,myHeight,'yes');
	/*
	var tHeight=screen.height/2;
	var tWidth=screen.width/2;
	var top=tHeight-myHeight/2;
	var left=tWidth-myWidth/2;
	var Feats="width="+myWidth+",height="+myHeight+",top="+top+",left="+left+", screenY=" +top+",screenX="+left+",scrollbars=no, toolbar=no, status=yes, resizable=no,alwayRaised=yes";
	window.open(url,win,Feats);
	*/
}
// open a Centered Window with Scrolls
function openWin2(url,win,myWidth,myHeight){
	openWinHandler(url,win,myWidth,myHeight,'yes');
	/*
	var tHeight=screen.height/2;
	var tWidth=screen.width/2;
	var top=tHeight-myHeight/2;
	var left=tWidth-myWidth/2;
	var Feats="width="+myWidth+",height="+myHeight+",top="+top+",left="+left+", screenY=" +top+",screenX="+left+",scrollbars=yes, toolbar=no, status=yes, resizable=no,alwayRaised=yes";
	window.open(url,win,Feats);
	*/
}
// open a Resizable Window with Scrolls
function openWin3(url,win,myWidth,myHeight){
	openWinHandler(url,win,myWidth,myHeight,'yes');
	/*
	var tHeight=screen.height/2;
	var tWidth=screen.width/2;
	var top=tHeight-myHeight/2;
	var left=tWidth-myWidth/2;
	var Feats="width="+myWidth+",height="+myHeight+",top="+top+",left="+left+", screenY=" +top+",screenX="+left+",scrollbars=yes, toolbar=yes, status=yes, resizable=yes,alwayRaised=yes";
	window.open(url,win,Feats);
	*/
}
// JavaScript Document
/*
**********************************
WINDOW OBJECT VARIABLES
**********************************
*/
	var WindowObjectReference = null; // global variable
	var PreviousUrl;	
/*
*	function openWin (Opens a centered window object)
*
*	@url = URL String (Required)
*	@win = Window Name (Required)
*	@myWidth = Window Width (Required)
*	@myHeight = Window Height (Required)
*	@scrollBars = Yes/No Value (Required)		
*/
function openWinHandler(url,win,myWidth,myHeight,scrollBars){
	var tHeight=screen.height/2;
	var tWidth=screen.width/2;
	var top=tHeight-myHeight/2;
	var left=tWidth-myWidth/2;
	var Feats="width="+myWidth+",height="+myHeight+",top="+top+",left="+left+", screenY=" +top+",screenX="+left+",scrollbars="+scrollBars+", toolbar=no, status=yes, resizable=no,alwayRaised=yes";
	if(WindowObjectReference == null || WindowObjectReference.closed)
		{
			WindowObjectReference=window.open(url,win,Feats);
			WindowObjectReference.focus();
		}
		else if(previousUrl !=url)
		{
			WindowObjectReference=window.open(url,win,Feats);
			WindowObjectReference.focus();
		}
		else
		{
			WindowObjectReference.focus();
		}
	previousUrl=url;
}


// showHide Group of Divs
function showHideList(theOnList, theOffList){
	//split the lists
	thisOffList	=	theOffList.split(',');
	thisOnList	=	theOnList.split(',');
	 //hide the off list items
	 for (i=0;i<thisOffList.length;i++){
		  document.getElementById(thisOffList[i]).style.display = "none";
	 }
	 //show the on list items
	 for (i=0;i<thisOnList.length;i++){
		  document.getElementById(thisOnList[i]).style.display = "block";
	 }
}

// showHide Pair of Divs
function showHideSimple(theList){
	//split the list
	thisList=theList.split(',');
	// show the specified div
	document.getElementById(thisList[0]).style.display = "block";
	document.getElementById(thisList[1]).style.display = "none";
}

// showHide Single Divs
function showHideSimpleSingle(theItem){
	if(document.getElementById(theItem).style.display == "none") {
		document.getElementById(theItem).style.display = "block";
	} else {
		document.getElementById(theItem).style.display = "none";
	}
}