
function determineTypeAndSetPercentage() {
	
	var workerType = document.calculatorFrm.workerType.value;
	var recruitmentArrangement = document.calculatorFrm.recruitmentArrangement.value;
	var percentage;
	
	switch(workerType) {		
		case "office": 
			switch(recruitmentArrangement) {
				case "multipleSuppliers":
				percentage = 25;
				break;
				case "soleSupplier":
				percentage = 20;
				break;
				case "preferredSupplier":
				percentage = 23;
				break;
				case "managedSolution":
				percentage = 18;
				break;
			}
			break;
			
		case "industrial": 
			switch(recruitmentArrangement) {
				case "multipleSuppliers":
				percentage = 25;
				break;
				case "soleSupplier":
				percentage = 20;
				break;
				case "preferredSupplier":
				percentage = 23;
				break;
				case "managedSolution":
				percentage = 18;
				break;
			}
			break;
			
		case "care":
			switch(recruitmentArrangement) {
				case "multipleSuppliers":
				percentage = 32;
				break;
				case "soleSupplier":
				percentage = 24;
				break;
				case "preferredSupplier":
				percentage = 28;
				break;
				case "managedSolution":
				percentage = 20;
				break;
			}
			break;
			
		case "professional":
			switch(recruitmentArrangement) {
				case "multipleSuppliers":
				percentage = 25;
				break;
				case "soleSupplier":
				percentage = 17;
				break;
				case "preferredSupplier":
				percentage = 19;
				break;
				case "managedSolution":
				percentage = 16;
				break;
			}
			break;
			
		case "it":
			switch(recruitmentArrangement) {
				case "multipleSuppliers":
				percentage = 20;
				break;
				case "soleSupplier":
				percentage = 18;
				break;
				case "preferredSupplier":
				percentage = 19;
				break;
				case "managedSolution":
				percentage = 17;
				break;
			}
			break;
	}
	return percentage;
}


function validateNumber(num) {
     if(isNaN(num) || num=="") {
       return false;
     }
     return true;
  }
  
 function displayResult(id,amount) {
	if (validateNumber(amount)) {
		//document.calculatorFrm[id].value = parseInt(amount);
		document.calculatorFrm[id].value = amount;
	} else {
			document.calculatorFrm[id].value = "";
	}
}

function checkEnter(e){
	var characterCode
	if(e && e.which){
		e = e
	 	characterCode = e.which
	} else {
		e = event
		characterCode = e.keyCode
	}
	if(characterCode == 13){
 		calculate();
	 }
}

function calculate() {
	var CMSmargin = 14; // percentage
	var percentage = determineTypeAndSetPercentage();
	var annualSpend = document.calculatorFrm.annualSpend.value;
	
	if (validateNumber(annualSpend)) {
		
		// DO THE CALCULATION --------------------------------------------------------------------
		// ---------------------------------------------------------------------------------------
		//alert("annual spend = "+annualSpend);
		//alert("do the calculation with percentage = "+percentage);
		
		// Cost of work --------------------------------------------------------------------------
		var cost = Math.round(annualSpend-(annualSpend*(percentage/100)));
		//alert("cost="+cost);
		displayResult('cost',cost);
		
		// Cost with CMS margin applied ----------------------------------------------------------
		var costWithCMS = Math.round(cost/(1-(CMSmargin/100))-cost);
		//alert("costWithCMS="+costWithCMS);
		displayResult('costWithCMS',costWithCMS);
		
		// Potential savings ---------------------------------------------------------------------
		var potentialSavings = Math.round(annualSpend-(cost+costWithCMS));
		//alert("potentialSavings="+potentialSavings);
		displayResult('potentialSavings',potentialSavings);
		
		// % savings -----------------------------------------------------------------------------
		var percentageSavings = Math.round(((potentialSavings/annualSpend)*100)*10)/10;
		//alert("percentageSavings="+percentageSavings);
		displayResult('percentageSavings',percentageSavings);
	
	} else {
		
		if (annualSpend != "") {
			alert("Please enter an integer \n [without punctuation]");
			document.calculatorFrm.annualSpend.focus;
			document.calculatorFrm.annualSpend.select();
			displayResult('cost','');
			displayResult('costWithCMS','');
			displayResult('potentialSavings','');
			displayResult('percentageSavings','');
		}
	}
}