// JavaScript Document

// Declare vars

// Set default values
// Note values on the form and those for calculations can easily be changed here
var carInterestRate = (5.25+2)/100;
var carValue = 20000;
var carPaymentPeriod = 4; 
var carCost = 0;
var pensionValue = 0;
var natInsRate = 12.9/100;
var defaultComputerCost=1000;
var defaultDeskCost = 300;
var defaultPHICost = 400;

var defaultEmptyOK = false;
var salary = 20000;
var natIns = 0;
var natInsCar = 0;
var natInsPHI = 0;
var natIns1A = 0;
var PHI = 0;
var computerCost = 0;
var deskCost = 0;
var yearOneCost = 0;
var ongoingCost = 0;



// Data validation functions

// Function to check if a variable is empty (null of zero-length)
function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}

// Function to check if a character is a digit
function isDigit (c)
{   return ((c >= "0") && (c <= "9"))
}

// Function to check if a string is a positive integer
function isInteger (s)
{   var i;

    if (isEmpty(s)) 
       if (isInteger.arguments.length == 1) return defaultEmptyOK;
       else return (isInteger.arguments[1] == true);

    // Search through string's characters one by one
    // until we find a non-numeric character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);

        if (!isDigit(c)) return false;
    }

    // All characters are numbers.
    return true;
}

// Function to format number to a currency
function formatCurrency(num) 
{	num = num.toString().replace(/\$|\,/g,'');
	if (isNaN(num))
	{
		num = "0";
	}
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	pence = num%100;
	num = Math.floor(num/100).toString();
	if (pence<10)
	{
		pence = "0" + pence;
	}
	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 (((sign)?'':'-') + '£' + num + '.' + pence);
}

// Calc function to perform all calculations on the form
function calc()
{
	// Perform validation on input fields
	
	// Check that a salary has been entered
	if (isEmpty(document.form2.q1.value))
	{
		alert(' Please enter a salary for the employee');
		document.form2.q1.focus();
		return false;
	}
	
	// Check that the entered salary is a positive Integer
	if (!isInteger(document.form2.q1.value))
	{
		alert(' Please enter a numeric value for the employees salary. e.g. 20000');
		document.form2.q1.focus();
		return false;
	}
	
	// Set the value for next years salary and local salary value for calculations
	document.form2.salary.value=formatCurrency(document.form2.q1.value);
	salary=document.form2.q1.value;
	// Set value for computer cost
	if (document.form2.q2.options[document.form2.q2.selectedIndex].value=='yes')
	{
		document.form2.computer.value=formatCurrency(defaultComputerCost);
		computerCost=defaultComputerCost;
	}
	else
	{
		document.form2.computer.value=formatCurrency(0);
		computerCost=0;
	}
	
	// Set value for desk cost
	if (document.form2.q3.options[document.form2.q3.selectedIndex].value=='yes')
	{
		document.form2.desk.value=formatCurrency(defaultDeskCost);
		deskCost=defaultDeskCost;
	}
	else
	{
		document.form2.desk.value=formatCurrency(0);
		deskCost=0;
	}
	
	// Calculate value for car cost
	if (document.form2.q4.options[document.form2.q4.selectedIndex].value=='yes')
	{
		carCost=(carValue * carInterestRate * carPaymentPeriod + carValue) / carPaymentPeriod;
	}
	else
	{
		carCost=0;
	}
	
	// Set values for car cost
	document.form2.car1.value=formatCurrency(carCost);
	document.form2.car2.value=formatCurrency(carCost);
	
	// Calculate pension values
	if (document.form2.q5.options[document.form2.q5.selectedIndex].value=='yes')
	{
		pensionValue=salary*document.form2.q5a.options[document.form2.q5a.selectedIndex].value/100;
	}
	else
	{
		pensionValue=0;
	}

	// Set values for pension
	document.form2.p1.value=formatCurrency(pensionValue);
	document.form2.p2.value=formatCurrency(pensionValue);
	
	// Set values for PHI
	if (document.form2.q6.options[document.form2.q6.selectedIndex].value=='yes')
	{
		document.form2.phi1.value=formatCurrency(defaultPHICost);
		document.form2.phi2.value=formatCurrency(defaultPHICost);
		PHI=defaultPHICost;
	}
	else
	{
		document.form2.phi1.value=formatCurrency(0);
		document.form2.phi2.value=formatCurrency(0);
		PHI=0;
	}
	
	// Set values for Employers National Insurance
	natIns=(salary-52*87)*natInsRate;
	document.form2.natins1.value=formatCurrency(natIns);
	document.form2.natins2.value=formatCurrency(natIns);
	
	// Calculate National Insurance Class 1A
	if (document.form2.q4.options[document.form2.q4.selectedIndex].value=='yes')
	{
		natInsCar=carValue*0.35*natInsRate;
	}
	else
	{
		natInsCar=0;
	}
	if (document.form2.q6.options[document.form2.q6.selectedIndex].value=='yes')
	{
		natInsPHI=PHI*natInsRate;
	}
	else
	{
		natInsPHI=0;
	}
	natIns1A=natInsCar+natInsPHI;
	
	// Set National Insurance Class 1A
	document.form2.natinsa1.value=formatCurrency(natIns1A);
	document.form2.natinsa2.value=formatCurrency(natIns1A);
	
	// Calculate Year 1 Cost
	yearOneCost=(parseFloat(salary) + parseFloat(computerCost) + parseFloat(deskCost) + parseFloat(carCost) + parseFloat(pensionValue) + parseFloat(PHI) + parseFloat(natIns) + parseFloat(natIns1A));

	// Set Year 1 Cost
	document.form2.cost.value=formatCurrency(yearOneCost);
	
	// Calculate Ongoing Cost
	ongoingCost=(parseFloat(salary) + parseFloat(carCost) + parseFloat(pensionValue) + parseFloat(PHI) + parseFloat(natIns) + parseFloat(natIns1A))
	
	// Set Ongoing Cost
	document.form2.cost2.value=formatCurrency(ongoingCost);
}
