function commaFormat(num , sep) 
{ 
	var n = '' + num;
	var numl  = n.length;
	var i = n.indexOf('.');
	var fract = '';

	if (i > -1){
		numl -= n.length-i;
		fract = n.substring(i);
	}
	if (numl > 3){
		var s, dl = numl%3;
		var str = (dl > 0) ? n.substring(0,dl):'';
		var max = Math.floor(numl/3);
		for (i=0 ; i < max ; i++){
			s = n.substring(dl+i*3,dl+i*3+3);
			str += (dl==0 && i==0) ? s : sep+s;
 		}
		return str+fract;
	}else return n;
}

function strRound( number, places ) {
    if ( number < 1 ) {
        result = "0" + Math.round( number * Math.pow(10, places) );
    }
    else {
        result = "" + Math.round( number * Math.pow(10, places) );
    }

    var dp = result.length - places;
    result = result.substring(0, dp) + "." + 
             result.substring(dp, result.length);
        
    if ( number < 0 ) {
        result = "-" + result;
    }

    return result;
}

function calcValue() {
    if (document.forms.value_calc.borrowed.value == "" || document.forms.value_calc.loftcost.value == ""){
      alert('Please enter a property and conversion value.')
    }else{

    var principal = parseFloat(Number(document.forms.value_calc.borrowed.value.replace(/(,|\.)/g, "")));
    var loftCost = parseFloat(Number(document.forms.value_calc.loftcost.value.replace(/(,|\.)/g, "")));
    var apr = parseFloat(4) / 100;
    var maturity = parseFloat(document.forms.value_calc.years.value);
    var withLoftValue = Math.pow(apr + 1, maturity)*(principal+loftCost);
    var withoutLoftValue = Math.pow(apr + 1, maturity)*principal;
    
    if (document.getElementById){
      document.getElementById('withoutConversion').innerHTML = '&pound;' +  commaFormat(strRound(withoutLoftValue, 2), ",");
      document.getElementById('withConversion').innerHTML = '&pound;' + commaFormat(strRound(withLoftValue, 2), ",");
    }
    
    }
    
}
