// JavaScript Document
function update()
	{
		if (document.layers)  // Other
			{
				alert("Other");
				updateIE();
			}
		else if (document.all) // IE
			{
				// alert("IE");
				updateIE();
			}
		else if (document.getElementById) // Firefox
			{
				// alert("Firefox");
				updateIE();
			}
	}	

function updateIE()
 {
	// Amount of jobs per year
	var jobNo = document.getElementById('jobNumber').value;
	var	staffNumbers = document.getElementById('staffNumber').value;

	var currenyString = getSelectedCheckboxValue(document.valueProposition.currency);	 
	var lastCurrency = document.getElementById('lastCurrency').value;
	var conversion;
	
	if ( jobNo < 1 || staffNumbers < 1 )
		{
			alert('Please supply at least one job vacancy   \r\nand at least one recruiting staff.   ');
			return false;
		}
	
	var AUSRate = 1.0;
	var NZDRate = 1.2;
	var USDRate = 0.8;
	var GBPRate = 0.48;
	var EURRate = 0.6;
	var cost = 125;
	
// set the previous conversion rate
	if (lastCurrency == "AUD")
		lastConversion = AUSRate;
		
	else if (lastCurrency == "NZD")
		lastConversion = NZDRate;
		
	else if (lastCurrency == "USD")
		lastConversion = USDRate;
		
	else if (lastCurrency == "GBP")
		lastConversion = GBPRate;
		
	else if (lastCurrency == "EUR")
		lastConversion = EURRate;
		
	// set the conversion rate
	if (currenyString == "AUD")
		{
			conversion = AUSRate;
			currencySymbol = "$ ";
		}
	else if (currenyString == "NZD")
		{
			conversion = NZDRate;
			currencySymbol = "$ ";
		}
	else if (currenyString == "USD")
		{
			conversion = USDRate;
			currencySymbol = "$ ";
		}
	else if (currenyString == "GBP")
		{
			conversion = GBPRate;
			currencySymbol = "£ ";
		}
		
	else if (currenyString == "EUR")
		{
			conversion = EURRate;
			currencySymbol = "€ ";
		}

	var	yearlyCost =  Math.round((12 * cost * staffNumbers) * conversion);

	// Create a constant reference in Australian dollars
	document.getElementById('evalApplicantsRef').value 		= document.getElementById('evalApplicants').value 	/ lastConversion;
	document.getElementById('setAdvertRef').value 				= document.getElementById('setAdvert').value 				/ lastConversion;
	document.getElementById('manageMailRef').value 				= document.getElementById('manageMail').value 			/ lastConversion;
	document.getElementById('letterProdRef').value 				= document.getElementById('letterProd').value 			/ lastConversion;
	document.getElementById('communicationRef').value 		= document.getElementById('communication').value 		/ lastConversion;
	document.getElementById('stationaryRef').value 				= document.getElementById('stationary').value 			/ lastConversion;
	document.getElementById('mediaAdvertisingRef').value 	= document.getElementById('mediaAdvertising').value / lastConversion;
	document.getElementById('recordManagementRef').value 	= document.getElementById('recordManagement').value / lastConversion;
	document.getElementById('exEvalApplicantsRef').value 	= document.getElementById('exEvalApplicants').value / lastConversion;
	document.getElementById('averageValueRef').value 			= document.getElementById('averageValue').value 		/ lastConversion;

	op1Total = 	document.getElementById('evalApplicants').value 	= Math.round(document.getElementById('evalApplicantsRef').value 	* conversion);
	op1Total += document.getElementById('setAdvert').value 				= Math.round(document.getElementById('setAdvertRef').value 				* conversion);
	op1Total += document.getElementById('manageMail').value 			= Math.round(document.getElementById('manageMailRef').value 			* conversion);
	op1Total += document.getElementById('letterProd').value 			= Math.round(document.getElementById('letterProdRef').value 			* conversion);
	op1Total += document.getElementById('communication').value 		= Math.round(document.getElementById('communicationRef').value 		* conversion);
	op1Total += document.getElementById('stationary').value 			= Math.round(document.getElementById('stationaryRef').value 			* conversion);
	op1Total += document.getElementById('mediaAdvertising').value = Math.round(document.getElementById('mediaAdvertisingRef').value * conversion);
	op1Total += document.getElementById('recordManagement').value = Math.round(document.getElementById('recordManagementRef').value * conversion);
	op2Total = 	document.getElementById('exEvalApplicants').value = Math.round(document.getElementById('exEvalApplicantsRef').value * conversion);
	op2Total += document.getElementById('averageValue').value 		= Math.round(document.getElementById('averageValueRef').value 		* conversion);

	var evaluateApplicants 	= document.getElementById('evalApplicantsRef').value;
	var settingAdvertising 	= document.getElementById('setAdvertRef').value;
	var managingEmail 			= document.getElementById('manageMailRef').value;
	var letterProduction 		= document.getElementById('letterProdRef').value;
	var communication 			= document.getElementById('communicationRef').value;
	var stationary 					= document.getElementById('stationaryRef').value;
	var mediaAdvertising 		= document.getElementById('mediaAdvertisingRef').value;
	var recordManagement 		= document.getElementById('recordManagementRef').value;
	var exEvalApplicants 		= document.getElementById('exEvalApplicantsRef').value;
	var averageValue 				= document.getElementById('averageValueRef').value;
	
	document.getElementById('lastCurrency').value = currenyString;

// ###########################################################################################################



// Option 1 - set Evaluating applicants values
	 paTotal = Math.round(evaluateApplicants * jobNo * conversion);
	 y1Total = Math.round(evaluateApplicants * jobNo * .25 * conversion);
	 y2Total = Math.round(evaluateApplicants * jobNo * .4 * conversion);
	 y3Total = Math.round(evaluateApplicants * jobNo * .6 * conversion);
	 
	 // output formatted number
	 document.getElementById('evalApplicants_pa').value = addCommas(paTotal);
	 document.getElementById('evalApplicants_y1').value = addCommas(y1Total);
	 document.getElementById('evalApplicants_y2').value = addCommas(y2Total);
	 document.getElementById('evalApplicants_y3').value = addCommas(y3Total);
	 
	 // Option 1 - set Setting Advertising values
	 paTotal += temp1 = Math.round(settingAdvertising * jobNo * conversion);
	 y1Total += temp2 = Math.round(settingAdvertising * jobNo * .25 * conversion);
	 y2Total += temp3 = Math.round(settingAdvertising * jobNo * .4 * conversion);
	 y3Total += temp4 = Math.round(settingAdvertising * jobNo * .6 * conversion);
	 
	 document.getElementById('setAdvert_pa').value = addCommas(temp1);
	 document.getElementById('setAdvert_y1').value = addCommas(temp2);
	 document.getElementById('setAdvert_y2').value = addCommas(temp3);
	 document.getElementById('setAdvert_y3').value = addCommas(temp4);
	 
	 //  Option 1 - set Managing Emails values
	 paTotal += temp1 = Math.round(managingEmail * jobNo * conversion);
	 y1Total += temp2 = Math.round(managingEmail * jobNo * .25 * conversion);
	 y2Total += temp3 = Math.round(managingEmail * jobNo * .4 * conversion);
	 y3Total += temp4 = Math.round(managingEmail * jobNo * .6 * conversion);
	 
	 document.getElementById('manageMail_pa').value = addCommas(temp1);
	 document.getElementById('manageMail_y1').value = addCommas(temp2);
	 document.getElementById('manageMail_y2').value = addCommas(temp3);
	 document.getElementById('manageMail_y3').value = addCommas(temp4);

//  Option 1 - set Letter production values
	 paTotal += temp1 = Math.round(letterProduction * jobNo * conversion);
	 y1Total += temp2 = Math.round(letterProduction * jobNo * .25 * conversion);
	 y2Total += temp3 = Math.round(letterProduction * jobNo * .4 * conversion);
	 y3Total += temp4 = Math.round(letterProduction * jobNo * .6 * conversion);
	 
	 document.getElementById('letterProd_pa').value = addCommas(temp1);
	 document.getElementById('letterProd_y1').value = addCommas(temp2);
	 document.getElementById('letterProd_y2').value = addCommas(temp3);
	 document.getElementById('letterProd_y3').value = addCommas(temp4);
	 
	 //  Option 1 - set Communication values
	 paTotal += temp1 = Math.round(communication * jobNo * conversion);
	 y1Total += temp2 = Math.round(communication * jobNo * .25 * conversion);
	 y2Total += temp3 = Math.round(communication * jobNo * .4 * conversion);
	 y3Total += temp4 = Math.round(communication * jobNo * .6 * conversion);
	 
	 document.getElementById('communication_pa').value = addCommas(temp1);
	 document.getElementById('communication_y1').value = addCommas(temp2);
	 document.getElementById('communication_y2').value = addCommas(temp3);
	 document.getElementById('communication_y3').value = addCommas(temp4);
	 
	 //  Option 1 - set Stationary values
	 paTotal += temp1 = Math.round(stationary * jobNo * conversion);
	 y1Total += temp2 = Math.round(stationary * jobNo * .25 * conversion);
	 y2Total += temp3 = Math.round(stationary * jobNo * .4 * conversion);
	 y3Total += temp4 = Math.round(stationary * jobNo * .6 * conversion);
	 
	 document.getElementById('stationary_pa').value = addCommas(temp1);
	 document.getElementById('stationary_y1').value = addCommas(temp2);
	 document.getElementById('stationary_y2').value = addCommas(temp3);
	 document.getElementById('stationary_y3').value = addCommas(temp4);

	 //  Option 1 - set Media advertising values
	 paTotal += temp1 = Math.round(mediaAdvertising * jobNo * conversion);
	 y1Total += temp2 = Math.round(mediaAdvertising * jobNo * .25 * conversion);
	 y2Total += temp3 = Math.round(mediaAdvertising * jobNo * .4 * conversion);
	 y3Total += temp4 = Math.round(mediaAdvertising * jobNo * .6 * conversion);
	 
	 document.getElementById('mediaAdvertising_pa').value = addCommas(temp1);
	 document.getElementById('mediaAdvertising_y1').value = addCommas(temp2);
	 document.getElementById('mediaAdvertising_y2').value = addCommas(temp3);
	 document.getElementById('mediaAdvertising_y3').value = addCommas(temp4);
	 
	 //  Option 1 - set Record management values
	 paTotal += temp1 = Math.round(recordManagement * jobNo * conversion);
	 y1Total += temp2 = Math.round(recordManagement * jobNo * .25 * conversion);
	 y2Total += temp3 = Math.round(recordManagement * jobNo * .4 * conversion);
	 y3Total += temp4 = Math.round(recordManagement * jobNo * .6 * conversion);
	 
	 document.getElementById('recordManagement_pa').value = addCommas(temp1);
	 document.getElementById('recordManagement_y1').value = addCommas(temp2);
	 document.getElementById('recordManagement_y2').value = addCommas(temp3);
	 document.getElementById('recordManagement_y3').value = addCommas(temp4);

	 document.getElementById('totalOption_1').value = currencySymbol + addCommas(op1Total);
																										
	 document.getElementById('totalOption_1_pa').value = currencySymbol + addCommas(paTotal);
	 document.getElementById('totalOption_1_y1').value = currencySymbol + addCommas(y1Total);
	 document.getElementById('totalOption_1_y2').value = currencySymbol + addCommas(y2Total);
	 document.getElementById('totalOption_1_y3').value = currencySymbol + addCommas(y3Total);

// ###########################################################################################################
// Option 2 - set Total values

	 temp1 = Math.round(exEvalApplicants * jobNo * conversion);
	 temp2 = Math.round(averageValue * jobNo * conversion);
	 
	 document.getElementById('exEvalApplicants_pa').value = addCommas(temp1); 
	 document.getElementById('averageValue_pa').value = addCommas(temp2);
	 
	 exPaTotal = temp1 + temp2;

	 document.getElementById('totalOption_2').value = currencySymbol + addCommas(op2Total);
	 document.getElementById('totalOption_2_pa').value = currencySymbol + addCommas(exPaTotal);
	 
	 op2_y1_total = exPaTotal - (paTotal - y1Total);
	 op2_y2_total = exPaTotal - (paTotal - y2Total);
	 op2_y3_total = exPaTotal - (paTotal - y3Total);

	 document.getElementById('totalOption_2_y1').value = currencySymbol + addCommas(op2_y1_total);
	 document.getElementById('totalOption_2_y2').value = currencySymbol + addCommas(op2_y2_total);
	 document.getElementById('totalOption_2_y3').value = currencySymbol + addCommas(op2_y3_total);

// ###########################################################################################################
// Return on Investment 

		document.getElementById('AnnualCostASAP_y1').value = currencySymbol + addCommas(yearlyCost);
		document.getElementById('AnnualCostASAP_y2').value = currencySymbol + addCommas(yearlyCost);
		document.getElementById('AnnualCostASAP_y3').value = currencySymbol + addCommas(yearlyCost);
		
		roi_op1_y1 = Math.round(10 * (y1Total / yearlyCost)) / 10;
		roi_op1_y2 = Math.round(10 * (y2Total / yearlyCost)) / 10;
		roi_op1_y3 = Math.round(10 * (y3Total / yearlyCost)) / 10;

		roi_op2_y1 = Math.round(10 * (op2_y1_total / yearlyCost)) / 10;
		roi_op2_y2 = Math.round(10 * (op2_y2_total / yearlyCost)) / 10;
		roi_op2_y3 = Math.round(10 * (op2_y3_total / yearlyCost)) / 10;
		
		document.getElementById('ROI_Option1_y1').value = "x " + roi_op1_y1;
		document.getElementById('ROI_Option1_y2').value = "x " + roi_op1_y2;
		document.getElementById('ROI_Option1_y3').value = "x " + roi_op1_y3;
		
		document.getElementById('ROI_Option2_y1').value = "x " + roi_op2_y1;
		document.getElementById('ROI_Option2_y2').value = "x " + roi_op2_y2;
		document.getElementById('ROI_Option2_y3').value = "x " + roi_op2_y3;
		
// Payback period		PBP_Option1_y1

		if ( jobNo > 0 )
			{
				pb_op1_y1 = Math.round(10 * (1 / (y1Total / yearlyCost) ) * (365 / 7)) / 10;
				pb_op1_y2 = Math.round(10 * (1 / (y2Total / yearlyCost) ) * (365 / 7)) / 10;
				pb_op1_y3 = Math.round(10 * (1 / (y3Total / yearlyCost) ) * (365 / 7)) / 10;
		
				pb_op2_y1 = Math.round(10 * (1 / (op2_y1_total / yearlyCost) ) * (365 / 7)) / 10;
				pb_op2_y2 = Math.round(10 * (1 / (op2_y2_total / yearlyCost) ) * (365 / 7)) / 10;
				pb_op2_y3 = Math.round(10 * (1 / (op2_y3_total / yearlyCost) ) * (365 / 7)) / 10;
			}
		else
			{
				pb_op1_y1 = 0;
				pb_op1_y2 = 0;
				pb_op1_y3 = 0;
		
				pb_op2_y1 = 0;
				pb_op2_y2 = 0;
				pb_op2_y3 = 0;
			}
			

		document.getElementById('PBP_Option1_y1').value = addCommas(pb_op1_y1);
		document.getElementById('PBP_Option1_y2').value = addCommas(pb_op1_y2);
		document.getElementById('PBP_Option1_y3').value = addCommas(pb_op1_y3);
		
		document.getElementById('PBP_Option2_y1').value = addCommas(pb_op2_y1);
		document.getElementById('PBP_Option2_y2').value = addCommas(pb_op2_y2);
		document.getElementById('PBP_Option2_y3').value = addCommas(pb_op2_y3);



// ###########################################################################################################
// Additional Add value

	 document.getElementById('addedValueOption1_y1').value = currencySymbol + addCommas(Math.round(paTotal * .2));
	 document.getElementById('addedValueOption1_y2').value = currencySymbol + addCommas(Math.round(paTotal * .3));
	 document.getElementById('addedValueOption1_y3').value = currencySymbol + addCommas(Math.round(paTotal * .4));

	 document.getElementById('addedValueOption2_y1').value = currencySymbol + addCommas(Math.round(exPaTotal * .2));
	 document.getElementById('addedValueOption2_y2').value = currencySymbol + addCommas(Math.round(exPaTotal * .3));
	 document.getElementById('addedValueOption2_y3').value = currencySymbol + addCommas(Math.round(exPaTotal * .4));

	return false;
}

function getSelectedRadio(buttonGroup) {
   // returns the array number of the selected radio button or -1 if no button is selected
   if (buttonGroup[0]) { // if the button group is an array (one button is not an array)
      for (var i=0; i<buttonGroup.length; i++) {
         if (buttonGroup[i].checked) {
            return i
         }
      }
   } else {
      if (buttonGroup.checked) { return 0; } // if the one button is checked, return zero
   }
   // if we get to this point, no radio button is selected
   return -1;
} // Ends the "getSelectedRadio" function

function getSelectedRadioValue(buttonGroup) {
   // returns the value of the selected radio button or "" if no button is selected
   var i = getSelectedRadio(buttonGroup);
   if (i == -1) {
      return "";
   } else {
      if (buttonGroup[i]) { // Make sure the button group is an array (not just one button)
         return buttonGroup[i].value;
      } else { // The button group is just the one button, and it is checked
         return buttonGroup.value;
      }
   }
} // Ends the "getSelectedRadioValue" function

function getSelectedCheckbox(buttonGroup) {
   // Go through all the check boxes. return an array of all the ones
   // that are selected (their position numbers). if no boxes were checked,
   // returned array will be empty (length will be zero)
   var retArr = new Array();
   var lastElement = 0;
   if (buttonGroup[0]) { // if the button group is an array (one check box is not an array)
      for (var i=0; i<buttonGroup.length; i++) {
         if (buttonGroup[i].checked) {
            retArr.length = lastElement;
            retArr[lastElement] = i;
            lastElement++;
         }
      }
   } else { // There is only one check box (it's not an array)
      if (buttonGroup.checked) { // if the one check box is checked
         retArr.length = lastElement;
         retArr[lastElement] = 0; // return zero as the only array value
      }
   }
   return retArr;
} // Ends the "getSelectedCheckbox" function

function getSelectedCheckboxValue(buttonGroup) {
   // return an array of values selected in the check box group. if no boxes
   // were checked, returned array will be empty (length will be zero)
   var retArr = new Array(); // set up empty array for the return values
   var selectedItems = getSelectedCheckbox(buttonGroup);
   if (selectedItems.length != 0) { // if there was something selected
      retArr.length = selectedItems.length;
      for (var i=0; i<selectedItems.length; i++) {
         if (buttonGroup[selectedItems[i]]) { // Make sure it's an array
            retArr[i] = buttonGroup[selectedItems[i]].value;
         } else { // It's not an array (there's just one check box and it's selected)
            retArr[i] = buttonGroup.value;// return that value
         }
      }
   }
   return retArr;
} // Ends the "getSelectedCheckBoxValue" function

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;
}
