//
//
function PHMCPrintable()
{
	document.PHMCIntOnlyCalc.submit();
}

function PHMCFloor(number)
{
	return Math.floor(number*Math.pow(10,2))/Math.pow(10,2);
}

function PHMCComputeForm(form)
{
	PHMCBasicEdit();

	var m=document.PHMCIntOnlyCalc.YR.value*12;
	if (m< 70)
	{
		alert ("Term in Years must be at least " + Math.round(70/12));
		document.PHMCIntOnlyCalc.YR.value='30';
	}
	else
	if (document.PHMCIntOnlyCalc.YR.value > 30)
	{
		alert ("Term in Years must not be greater than " + 30);
		document.PHMCIntOnlyCalc.YR.value='30';
	}

	if (PHMCReplacePct(document.PHMCIntOnlyCalc.IR.value) > 20 )
	{
		alert ("Interest Rate must not be greater than " + 20 + " percent");
		document.PHMCIntOnlyCalc.IR.value=PHMCAddPct('20');
	}
	else
	if (PHMCReplacePct(document.PHMCIntOnlyCalc.IR.value) < 1 )
	{
		alert ("Interest Rate must be greater than " + 1 + " percent");
		document.PHMCIntOnlyCalc.IR.value=PHMCAddPct('1');
	}

	//* Compute Conventional Loan Principle & Interest Payment
	var IR = PHMCReplacePct(document.PHMCIntOnlyCalc.IR.value);
	var mi = IR / 1200;
	var base = 1;
	var mbase = 1 + mi;
	var loanA = PHMCReplaceChars(document.PHMCIntOnlyCalc.LA.value);
	var loanA = PHMCReplaceChar(loanA);
	if ( PHMCCheckForm(IR) && PHMCCheckForm(loanA) && PHMCCheckForm(document.PHMCIntOnlyCalc.YR.value));
	{
		for (i=0; i<document.PHMCIntOnlyCalc.YR.value * 12; i++)
		base = base * mbase
	}
	document.PHMCIntOnlyCalc.PI1.value = PHMCFormatCurrency(PHMCFloor(loanA * mi / ( 1 - (1/base))));

	//* Compute Interest-Only Loan Principle & Interest Payment
	var IR = PHMCReplacePct(document.PHMCIntOnlyCalc.IR.value);
	var mi = IR / 1200;
	var loanA = PHMCReplaceChars(document.PHMCIntOnlyCalc.LA.value);
	var loanA = PHMCReplaceChar(loanA);
	document.PHMCIntOnlyCalc.PI2.value = PHMCFormatCurrency(PHMCFloor(loanA * mi));
	document.PHMCIntOnlyCalc.PIDiff.value = PHMCFormatCurrency(PHMCFloor(loanA * mi / ( 1 - (1/base))) - (PHMCFloor(loanA * mi)));
}

function PHMCReplaceChar(entry)
{
	out = "$";
	add = "";
	temp = "" + entry;
	while (temp.indexOf(out)>-1)
	{
		pos= temp.indexOf(out);
		temp = "" + (temp.substring(0, pos) + add +
		temp.substring((pos + out.length), temp.length));
	}
	return temp;
}

function PHMCReplacePct(entry)
{
	out = "%";
	add = "";
	temp = "" + entry;
	while (temp.indexOf(out)>-1)
	{
		pos= temp.indexOf(out);
		temp = "" + (temp.substring(0, pos) + add + temp.substring((pos + out.length), temp.length));
	}
	return temp;
}

function PHMCReplaceChars(entry)
{
	out = ",";
	add = "";
	temp = "" + entry;
	while (temp.indexOf(out)>-1)
	{
		pos= temp.indexOf(out);
		temp = "" + (temp.substring(0, pos) + add + temp.substring((pos + out.length), temp.length));
	}
	return temp;
}

function PHMCFormatCurrency(num)
{
	num=PHMCReplaceChar(num);
	num=PHMCReplaceChars(num);
	if ( PHMCCheckForm(num));
	{
		num = num.toString().replace(/$|,/g,'');
		if(isNaN(num)) num = "0";
			num = Math.floor(num).toString();
		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 ('$' + num );
	}
}

function PHMCBasicEdit()
{
	if 	(!PHMCIsMoney(document.PHMCIntOnlyCalc.LA.value) || document.PHMCIntOnlyCalc.LA.value.length ==0)
		document.PHMCIntOnlyCalc.LA.value='$125,000';

	if 	(!PHMCIsPct(document.PHMCIntOnlyCalc.IR.value) || document.PHMCIntOnlyCalc.IR.value.length ==0)
		document.PHMCIntOnlyCalc.IR.value='6.0%';

	if 	(!PHMCIsNumber(document.PHMCIntOnlyCalc.YR.value) || document.PHMCIntOnlyCalc.YR.value.length ==0)
		document.PHMCIntOnlyCalc.YR.value='30';
}

function PHMCIsMoney(val)
{
	var number="0123456789$,.";

	for (var i=0;i<val.length;i++)
	{
		if (number.indexOf(val.charAt(i)) == -1)
		{
			alert('Value may only contain digits, a leading dollar sign, commas and a decimal point.');
			return false;
		}
	}
	return true;
}

function PHMCIsNumber(val)
{
	var number="0123456789.";

	for (var i=0;i<val.length;i++)
	{
		if (number.indexOf(val.charAt(i)) == -1)
		{
			alert('Value may only contain digits and a decimal point');
			return false;
		}
	}
	return true;
}
	
function PHMCIsPct(val)
{
	var number="0123456789.%,";

	for (var i=0;i<val.length;i++)
	{
		if (number.indexOf(val.charAt(i)) == -1)
		{
			alert('Value may only contain digits, a decimal point, and a percent sign');
			return false;
		}
	}
	return true;
}

function PHMCAddPct(pct)
{
	pct=PHMCReplacePct(pct);
	pct=parseFloat(pct);
	if (PHMCCheckForm(pct));
	{
		return (pct + '%');
	}
}

function PHMCCheckForm(toCheck)
{
	isNum = true;
	for (j = 0; j < toCheck.length; j++)
	{
		if ((toCheck.substring(j,j+1) < "0") && (toCheck.substring(j,j+1) != ".") || (toCheck.substring(j,j+1) > "9"))
		{
			isNum = false;
		}
	}
	if ((isNum == false) || (toCheck.length == 0) || (toCheck == null))
	{
		alert("Please enter only numerical data in all fields.");
		return false;
	}
	else
	{
		return true;
	}
}
