// define calculate function to complete results box

function calculate()	{

	//move form values into more easilly managed variables
	var turnover = document.forms[0].turnover.value
	var gm = document.forms[0].gm.value / 100
	var cancel = document.forms[0].cancel.value / 100
	var orders = document.forms[0].orders.value
	var variance = document.forms[0].variance.value /100
	var admin = document.forms[0].admin.value / 100
	var headcount = document.forms[0].headcount.value
	var pricing = document.forms[0].pricing.value

	//initiate running totals
	var sales_up = 0
	var gm_up = 0
	var cost_down = 0

	//validate input
	check = turnover * gm
	check = check * cancel
	check = check * orders  //used to decide what to print - see end of function definition

	//start building up the sentence called "text" to state the result
	var text = "Using the data you provided, we estimate that it currently costs your business £"

	//calculate the present cost of placing an order
	var now = headcount
	now = now * 15000

	//estimate and add telephone bill
	phone = 0.66 * orders
	now += phone

	//work out cost per order
	now = now / orders
	
	//round to 2 decimal places
	now = Math.round(now * 100)
	now = now / 100

	//add to "text"
	text += now
	text += " to process each purchase order. (We have assumed a cost of £15,000 per full time employee including NIC and benefits. We have estimated your annual cost of phoning, faxing and postage to your suppliers as £" + phone + ").\n\n"

	//calculate the future cost of placing orders
	then = now * 2
	then = then / 3

	//round to 2 decimal places
	then = Math.round(then * 100)
	then = then / 100

	//calculate saving
	var total = 0
	saving = now - then
	saving = saving * orders
	saving = Math.round(saving)

	//add to total
	total += saving

	//add to cost_down
	cost_down += saving

	//add to "text"
	text += "This could be reduced to £"
	text += then
	text += " by using Corridor - a saving of 33%. That's an annual saving of £"
	text += saving
	text += ".\n\n"
	text += "What's more, Corridor should reduce your cancellations - and allowances too. Putting aside cancellations due to buyer's remorse and defective goods, this should save you another £"

	//calculate cancellation and allowance savings
	var saving_scm = Math.min(cancel,0.0025)
	cancel -= saving_scm	//seperate out the 0.25% cancellations due to scm
	saving_scm *= 0.8

	saving = cancel * 0.07
	saving += saving_scm
	saving = saving * turnover	//this is the revenue lost due to cancellations

	//add to sales_up
	sales_up += saving

	saving = saving * gm		//this is the potential profit on lost sales
	var cancellations = saving
	var allowances = saving / 4	//straight to the bottom line

	//add to sales_up
	sales_up += allowances

	//add to gm_up
	gm_up += allowances

	//add to saving
	saving = cancellations + allowances

	//remove decimals
	saving = Math.round(saving)
	cancellations = Math.round(cancellations)
	allowances = Math.round(allowances)

	//add to total
	total += saving

	//add to "text"
	text += saving
	text += " a year. (£"
	text += cancellations
	text += " a year from reduced cancellations and £"
	text += allowances
	text += " a year from allowances).\n\nPerhaps more important still is the time that will be saved as a result of fewer customer complaints. After all senior managers often spend much of their working day solving problems they would rather not have.\n\nIn the long term this will lead to an increase in repeat business and referal sales. (We have not included this in the benefits as we don't have enough information).\n\n"

	//calculate savings from correct pricing and add to text

	statement = ""
	if (saving == 0) 	{
		statement += "You did not enter a figure for invoice price variance. Perhaps you simply accept a 'small' variance of, say, 1-2% as a fact of life (The 'industry average' is about 1.5%, so you would be doing well!).\n\nIf this is the case, try putting in a small number - you might be surprised at how much this 'small' variance is costing you!\n\n"
		}
	if (saving < 0) 	{
		statement += "You entered a negative invoice price variance, which means that your suppliers are under-charging you. If this is correct, then well done!\n\nIf (as is more likely) you meant to say that invoices are higher than expected, just change the number and recalculate the true savings.\n\n"
		}
	if (saving > 0)		{
		if (pricing == "1")	{
			saving = variance * turnover
			saving = Math.round(saving)	
			statement += "You told us that your suppliers charge more than you expect. This suggests that your 'price bible' is sometimes out of date or inaccurate.\n\nDid you realise that this means your retail prices are probably marked up too low? They should be " + document.forms[0].variance.value +"% higher.\n\nThat's costing your business £" + saving + " a year in lost profit. Ouch!\n\n"
			}
		if (pricing == "0")	{
			saving = variance * turnover
			saving *= (1 - gm)
			saving = Math.round(saving)	
			statement += "You told us that your suppliers charge more than you expect. Assuming your 'price bible' is up to date and accurate, this means your gross margin is being eaten away. It should be " + document.forms[0].variance.value +"% higher.\n\nThat's costing your business £" + saving + " a year in lost profit. Ouch!\n\n"
			}
		}
	text += statement

	//add to totals
	if (saving > 0)	{

		//add to sales_up
		if (pricing == "1")	{
			sales_up += saving
			}

		//add to gm_up
		gm_up += saving

		//add to total
		total += saving
		}


	//calculate extra sales resource
	if (admin < 0) {admin = 0; document.forms[0].admin.value = 0}	//a negative value is meaningless!
	sales = turnover * admin
	sales /= 2		//let's assume 2:1 ratio of sales resource to sales increase

	//add to sales_up
	sales_up += sales

	//work out extra profit
	saving = sales * gm
	saving = Math.round(saving)

	//add to gm_up
	gm_up += saving

	//add to total
	if (saving > 0)
		{total += saving}

	//work out increased % sales
	half = document.forms[0].admin.value / 2

	//add to text
	if (admin == 0)
		{text += "Finally... If your sales staff really spend ALL of their time selling you will be the envy of the industry! We have found that most retail sales people spend between 5-10% of their time checking order progress.\n\nyou need to be sure that your staff are really out there selling if you're going to maximise your profits. Feel free to enter a small percentage and recalculate - you might be surprised how costly it can be to use sales staff in this way.\n\n"}
	if (admin > 0)
		{text += "And finally, you told us that your sales staff spend " + document.forms[0].admin.value + "% of their time on admin. Wouldn't you prefer them to spend more of their time selling?\n\nIncreasing your sales resource by " + document.forms[0].admin.value + "% ought to lead directly to an increase in sales. Let's assume sales go up by just " + half + "%.\n\nThat's an increase of £" + sales + " a year.\n\nAND THAT'S AN EXTRA £" + saving + " PROFIT!\n\n"}

	//explain why
	text += "Corridor is able to deliver these improvements by streamlining the movement of information (e.g., catalogues and price lists) and documents (e.g., orders, acknowledgements and invoices) between buyers and sellers in the furniture industry.\n\nCorridor is the e-business marketplace for the whole industry. You could say it's business as usual - ONLY BETTER!\n\nThank you for using the Corridor calculator.\n\nNB. Please be sure to read the notes at the foot of this page and contact Corridor for further information."	

	//round up/down sales_up, gm_up, cost_down
	sales_up = Math.round(sales_up)
	gm_up = Math.round(gm_up)
	cost_down = Math.round(cost_down)

	//validate input and display "text" in results window
	document.forms[0].result.value = "HOW CORRIDOR WILL ADD £" + total + " PER YEAR TO YOUR PROFITS...\n\n   ***********************\n     Sales up by £" + sales_up + "\n     Gross margin up by £" + gm_up + "\n     Costs down by £" + cost_down + "\n   ***********************\n\nThis is how we calculated your benefits...\n\n" + text
	if (check <= 0)
		{document.forms[0].result.value = "Sorry! You have given us inadequate information.\n\nWe need to know your turnover, margin, cancellation rate and order volume details to calculate the savings.\n\nPlease answer as many questions as you can."}

	//end of function definition
	}

function isaPosNum(s) {
	return (parseInt(s) > 0)
	}

function number_check(number, min, max)	{
	var returnVal = false
//remove "£,%" characters
	var string = new String(number.value)
	for (var i = 1; i < string.length; i++)	{
		if (string.charAt(i) == ",")	{
			string = string.substring(0, i) + string.substring(i+1, string.length)
			}	//end of if statement
		}		//end of for loop
	if (string.indexOf("£") > -1)	{
		string = string.substring(1, string.length)
		}
	if (string.indexOf("%") > -1)	{
		string = string.substring(0, string.indexOf("%"))
		}
	number.value = string
//check the number makes sense
	var real = number.value * 1000
	if (!isaPosNum(real))	{
		number.value = "<void>"
		alert(number.name + " should be a positive number")
		}
	var test = number.value
	var reset_max = max
	var reset_min = min
	if (parseInt(test) < min)	{
		number.value = reset_min
		alert(number.name + " should be greater than " + min)   
		}
	if (parseInt(test) > max)       {
		number.value = reset_max
		alert(number.name + " should be less than " + max)   
		}
	}

function change()	{
	var oldval = document.forms[0].pricing.value
	var newval = "1"
	if (oldval == "1")
		{newval = "0"}
	document.forms[0].pricing.value = newval
	}