//Messages

function checkEnter(e){ //e is event object passed from function invocation
   var characterCode;
   
   if(e && e.which){ 
      e = e;
      characterCode = e.which; 
   }
   else{
      e = event;
      characterCode = e.keyCode; 
   }
   
   if(characterCode == 13){
/*
      document.getElementById("buttonA").click();
      document.getElementById("buttonB").click();
      document.getElementById("buttonR").click();
*/
        calculateA();
        calculateB();
        calculateR();
   }
   
   return;

}

function getmsg(percent)
{
	if(percent <= 36)
		return "Good - for most budgets this is an acceptable debt load."
	if(percent >= 37 && percent <= 42)
		return "Moderate - look for ways to pay down debt or increase income."
	if(percent >= 43 && percent <= 49)
		return "Serious - immediate debt reduction is necessary to avoid potential financial difficulty."
	if(percent >=50)
		return "Critical - this is an unhealthy level of debt compared to income. Immediately reduce debt or seek professional financial guidance."
}		
		
function getmsgcolor(percent)
{
	if(percent <= 36)
		return "green"
	if(percent >= 37 && percent <= 42)
		return "blue"
	if(percent >= 43 && percent <= 49)
		return "orange"
	if(percent >=50)
		return "red"
}

function calculateA() {
	var mortgage = new Number(strToFloat(document.calculator.mortgage.value))
	var card = new Number(strToFloat(document.calculator.card.value))
	var car = new Number(strToFloat(document.calculator.car.value))
	var studentloans = new Number(strToFloat(document.calculator.studentloans.value))
	var alimonypayment = new Number(strToFloat(document.calculator.alimonypayment.value))
	var other = new Number(strToFloat(document.calculator.other.value))
	
	var a = mortgage + card + car + studentloans + alimonypayment + other
	
	if(isNaN(a)) {
		msg("All entries must be numbers","white","orange")
		document.calculator.a.value = ""
	}
	else
	{
		val = strToFloat(a)
		document.calculator.a.value = numToDollars(val)
		msg("","black","white")
	}
	
}

function calculateB() {
	var salary = new Number(strToFloat(document.calculator.salary.value))
	var bonus = new Number(strToFloat(document.calculator.bonus.value))
	var income = new Number(strToFloat(document.calculator.income.value))
	var alimony = new Number(strToFloat(document.calculator.alimony.value))
	
	var b = Math.round((salary + bonus + income + alimony) / 12)
	
	if(isNaN(b)) {
		msg("All entries must be numbers","white","orange")
		document.calculator.b.value = ""
	}
	else
	{
		val = strToFloat(b)
		document.calculator.b.value = numToDollars(val)
		msg("","black","white")
	}
}

function calculateR() {

	var a = new Number(strToFloat(document.calculator.a.value))
	var b = new Number(strToFloat(document.calculator.b.value))

	if(a=="" || b=="")
		msg("You have not completed the form","white","red")
	else
	{
		var r = a / b
		var rpercent = Math.round(100 * r)
		document.calculator.ab.value = rpercent + "%"
		var message = "<b style='font-weight:bold;color:white'>Debt-to-Income Ratio Results:</b><br>"
		message += getmsg(rpercent)
		msg(message, "white", getmsgcolor(rpercent))
	}
}

function msg(message, color, bgcolor)
{
	document.getElementById('messagebox').style.backgroundColor = bgcolor
	document.getElementById('messagebox').style.color = color
	document.getElementById('messagebox').innerHTML = message
}


function strToFloat( s )
{
	var n = parseFloat( removeNonNumerics( s ) )
	if ( isNaN( n ) )
	{
		n = 0
	}

	return (n < 0) ? 0 : n;
}

function removeNonNumerics( s )
{
	if ( s == null ) return null
	s = '' + s
	var tmp = ''
	var isLeadingZero = true
	for ( var i = 0; i < s.length; i++ )
	{
		var c = s.charAt( i )
		if ( isNumeric( c ) )
		{
			if ( isLeadingZero == true && c == '0' )
			{
				continue;
			}
			else
			{
				isLeadingZero = false
			}
			tmp = tmp + c
		}
	}
	return tmp
}

function isNumeric( c )
{
	return ( c == '.' || c == '-' || ( c >= '0' && c <= '9') )
}


function numToDollars( n )
{
	return _numToUnits( n, '$', 0, '' )
}

function _numToUnits( n, units_prefix, decimal_places, units_suffix )
{
	if ( isOverflow( n ) ) return 0
	var isNegative = ( n < 0 ) ? true : false
	var s = ''
	var d = strToInt( decimal_places )

	var x = Math.abs( Math.round ( n * Math.pow(10, decimal_places ) ) )

	if ( x == 0 )
	{
		var defaultStr = '0'
		if ( d > 0 )
		{
			defaultStr += '.'
			for ( var i = 0; i < d; i++ ) defaultStr += '0'
		}
		defaultStr = units_prefix + defaultStr + units_suffix
		return defaultStr
	}

	if ( d == 0 )
	{
		var tmp = '' + x
		s = addCommas( tmp )
	}
	else
	{
		var leftTmp = '' + x
		var rightTmp = '' + x
		var len = leftTmp.length

		for ( var i = 0; i < d + 1 - len; i++ )
		{
			leftTmp = '0' + leftTmp
			rightTmp = '0' + rightTmp
		}

		len = leftTmp.length
		var decimalSplit = len - d

		var leftStr = addCommas( leftTmp.substring( 0, decimalSplit ) )
		var rightStr = rightTmp.substring( decimalSplit, len )

		s = leftStr + '.' + rightStr
	}

	s = units_prefix + s + units_suffix

	if ( isNegative ) s = '-' + s

	return s
}

function isOverflow( n )
{
	return ( isNaN( n ) || n == Number.POSITIVE_INFINITY || n == Number.NEGATIVE_INFINITY )
}

function strToInt( s )
{
	var n = parseInt( removeNonNumerics( s ) )
	if ( isNaN( n ) )
	{
		n = 0
	}
	return (n < 0) ? 0 : n;
}

function addCommas( tmp )
{
	var s = ''
	var len = tmp.length
	var end = len - 1
	for ( var i = 0; i < len; i++ )
	{
		c = tmp.charAt( end - i )
		s = c + ((i > 2 && i % 3 == 0) ? ',' : '') + s
	}
	return s
}