﻿// JScript File
var firstApp, secondApp, firstBonus, secondBonus, firstTotal, secondTotal, total

function CalculateBorrow()
{

    firstApp = document.getElementById('txtFirst').value;
    secondApp = document.getElementById('txtSecond').value;
    firstBonus = document.getElementById('txtFirstBonus').value;
    secondBonus = document.getElementById('txtSecondBonus').value;
    total = document.getElementById('txtResult');
    firstTotal = 0;
    secondTotal = 0;

	if(isNaN(firstApp) || isNaN(secondApp) || isNaN(firstBonus) || isNaN(secondBonus))
	{
		alert("Please enter numbers only");
		return false;
	}
    

    if(firstApp == null || firstApp == '')
    {
        firstApp = 0;
    }
    else
    {
        firstApp = Math.round(firstApp);
    }
    if(secondApp == null || secondApp == '')
    {
        secondApp = 0;
    }
    else
    {
        secondApp = Math.round(secondApp);
    }
    if(firstBonus == null || firstBonus == '')
    {
        firstBonus = 0;
    }
    else
    {
        firstBonus = Math.round(firstBonus);
    }
    if(secondBonus == null || secondBonus == '')
    {
        secondBonus = 0;
    }
    else
    {
        secondBonus = Math.round(secondBonus);
    }
    
    //first app calcs
        if((firstApp != 0 && secondApp != 0) || (firstApp != 0 && secondApp == 0 && secondBonus != 0))
        {
            firstTotal = ((parseFloat(firstBonus / 2) + parseFloat(firstApp)) * 3);
        }
        else
        {
            firstTotal = ((parseFloat(firstBonus / 2) + parseFloat(firstApp)) * 4);
        }
    
    //second app calc
        secondTotal = ((parseFloat(secondBonus / 2) + parseFloat(secondApp))* 3);
        
    total.value = Math.round(parseFloat(firstTotal) + parseFloat(secondTotal));
}

