// JavaScript Document

function calculate()
{
	// Set Errors to False.
	errors = false;
	
	// Get the values of each selection.
	var numProperties = document.getElementById('numProperties').value;
	var propertyCost = document.getElementById('propertyCost').value;
	var netReturn = document.getElementById('netReturn').value;
		
	// Check if variable is not a number or blank.
	if(isNaN(numProperties) || numProperties == "")
	{
		// Set the text for the error label.
		document.getElementById('errorNumProperties').innerHTML = "Please enter a valid number.";
		
		// Set Errors to true.
		errors = true;
	}										
			
	if(isNaN(propertyCost) || propertyCost == "")
	{
		document.getElementById('errorPropertyCost').innerHTML = "Please enter a valid number.";
		errors = true;
	}
	
	if(isNaN(netReturn) || netReturn == "")
	{
		document.getElementById('errorNetReturn').innerHTML = "Please enter a valid number.";
		errors = true;
	}
	
	// If no errors are present.
	if (!errors)
	{
		// Work out the neccessary figures.
		var annualCashFlow = cashFlow(numProperties, netReturn);			
		var fiveROI = five(annualCashFlow);
		var portfolioValue = portfolio(numProperties, propertyCost);
		var estPortfolioValue = estPortfolio(numProperties, propertyCost);
		// Set the result labels.
		document.getElementById('txtCashFlow').innerHTML = "£" + addCommas(annualCashFlow.toFixed(2));
		document.getElementById('txt5YearROI').innerHTML = "£" + addCommas(fiveROI.toFixed(2));
		document.getElementById('txtPortfolio').innerHTML = "£" + addCommas(portfolioValue.toFixed(2));
		document.getElementById('txt2019Portfolio').innerHTML = "£" + addCommas(estPortfolioValue.toFixed(2));
		// Show the results div.
		showDiv('results');
	}		
}

function cashFlow(numProperties, netReturn)
{
	return parseFloat((numProperties * netReturn) * 12);
}

function five(annualCashFlow)
{
	return parseFloat((annualCashFlow * 5) - 4125);
}

function portfolio(numProperties, propertyCost)
{
	return parseFloat(numProperties * propertyCost);
}

function estPortfolio(numProperties, propertyCost)
{
	return parseFloat((numProperties * propertyCost) * 2);
}

function getRefToDiv(divID,oDoc) 
{
  if (document.getElementById) 
  {
    return document.getElementById(divID); 
  }
  
  if (document.all) 
  {
    return document.all[divID]; 
  }
  
  if (!oDoc) 
  { 
  	oDoc = document; 
  }
  
  if (document.layers)
  {
    if (oDoc.layers[divID]) 
	{ 
		return oDoc.layers[divID]; 
	} 
	else 
	{
      //repeatedly run through all child layers
      for(var x = 0, y; !y && x < oDoc.layers.length; x++) 
	  {
        //on success, return that layer, else return nothing
        y = getRefToDiv(divID,oDoc.layers[x].document); 
	  }
    	return y; 
	} 
  }
  return false;
}

function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}


function showDiv(divID) {
  //get a reference as above ...
  myReference = getRefToDiv(divID);
  
  if( !myReference ) 
  {
    return; //don't go any further
  }
  //now we have a reference to it
  if( myReference.style ) 
  {
    //DOM & proprietary DOM
    myReference.style.display = 'block';
  } 
  else 
  {
    //layers syntax
    myReference.display = 'block';
  }
}

