// JavaScript Document
// ROI calculator script - © Intact 2004

var MAILING_COST_POUNDS = "1";
var MAILING_COST_PENCE = "00";
var MAILING_CUSTOMER_COUNT = "50000";

var count = new Array(7);

var screeningcost = new Array(7);
/* 
22/02/06 - Tim Chalk - Values updated - TeleAppend and Names removed
Old values:	
screeningcost[1] = 0.06;	 	//TeleAppend cost
screeningcost[2] = 0.005;	 	//MPS cost
screeningcost[3] = 0.005;	 	//TPS cost
screeningcost[4] = 0.006;	 	//GoneAways cost
screeningcost[5] = 0.005;	 	//FPS cost
screeningcost[6] = 0.0042; 	//Deceased cost
screeningcost[7] = 0.025;	 	//NCOA cost
screeningcost[8] = 0.006;	 	//Dedupe cost
screeningcost[9] = 0.01;	 	//Names cost	
*/
screeningcost[1] = 0.005;	 	//MPS cost
screeningcost[2] = 0.005;	 	//TPS cost
screeningcost[3] = 0.006;	 	//GoneAways cost
screeningcost[4] = 0.005;	 	//FPS cost
screeningcost[5] = 0.0042; 	//Deceased cost
screeningcost[6] = 0.025;	 	//NCOA cost
screeningcost[7] = 0.006;	 	//Dedupe cost	
	
var percentage = new Array(7);
/* 
22/02/06 - Tim Chalk - Values updated - TeleAppend and Names removed
Old values:
percentage[1] = 0.6101;		 //TeleAppend
percentage[2] = 0.0286;		 //MPS
percentage[3] = 0.1444;		 //TPS
percentage[4] = 0.0739;		 //GoneAways
percentage[5] = 0.005;		 //FPS
percentage[6] = 0.006;		 //Deceased
percentage[7] = 0.0188;		 //NCOA
percentage[8] = 0.03;			 //Dedupe
percentage[9] = 0.6032;		 //Names
*/	
percentage[1] = 0.0286;		 //MPS
percentage[2] = 0.1444;		 //TPS
percentage[3] = 0.0739;		 //GoneAways
percentage[4] = 0.005;		 //FPS
percentage[5] = 0.006;		 //Deceased
percentage[6] = 0.0188;		 //NCOA
percentage[7] = 0.03;			 //Dedupe

// function to calculate cost savings
function calculate(){
	var totalex = 0;
	var screenexcost = 0;
	var waste = 0;
	var intMailingCost = 0;
	var intNumberMailed = 0; 
	var strPenceValue = document.calculator.mailpackpence.value + "";
	
	if(document.calculator.mailpack.value=='') document.calculator.mailpack.value=0;
	if(document.calculator.mailpackpence.value=='') document.calculator.mailpackpence.value='0';	
	if(document.calculator.mailed.value=='') document.calculator.mailed.value=0;		
	
	intMailingCost = document.calculator.mailpack.value + document.calculator.mailpackpence.value;
	intNumberMailed = document.calculator.mailed.value;

	if(isNaN(intMailingCost)||intMailingCost==''){
		document.calculator.mailpack.value = MAILING_COST_POUNDS;
		document.calculator.mailpackpence.value = MAILING_COST_PENCE;
		calculate();
		return;
	}
	
	if(isNaN(intNumberMailed)||intNumberMailed==''){	
		document.calculator.mailed.value = MAILING_CUSTOMER_COUNT;
		calculate();
		return;
	}	
	
	/* 
	22/02/06 - Tim Chalk - Values updated - TeleAppend and Names removed
	Old values:
	if (document.calculator.teleappend.checked) count[1] = 1 ; else count[1] = 0;
	if (document.calculator.mps.checked) count[2] = 1 ; else count[2] = 0;
	if (document.calculator.tps.checked) count[3] = 1 ; else count[3] = 0;
	if (document.calculator.goneaways.checked) count[4] = 1 ; else count[4] = 0;
	if (document.calculator.fps.checked) count[5] = 1 ; else count[5] = 0;
	if (document.calculator.deceased.checked) count[6] = 1 ; else count[6] = 0;
	if (document.calculator.ncoa.checked) count[7] = 1 ; else count[7] = 0;
	if (document.calculator.dedupe.checked) count[8] = 1 ; else count[8] = 0;
	if (document.calculator.names.checked) count[9] = 1 ; else count[9] = 0;
	*/
	if (document.calculator.mps.checked) count[1] = 1 ; else count[1] = 0;
	if (document.calculator.tps.checked) count[2] = 1 ; else count[2] = 0;
	if (document.calculator.goneaways.checked) count[3] = 1 ; else count[3] = 0;
	if (document.calculator.fps.checked) count[4] = 1 ; else count[4] = 0;
	if (document.calculator.deceased.checked) count[5] = 1 ; else count[5] = 0;
	if (document.calculator.ncoa.checked) count[6] = 1 ; else count[6] = 0;
	if (document.calculator.dedupe.checked) count[7] = 1 ; else count[7] = 0;	

	for(i = 1; i < count.length; i++){
		totalex = (totalex + (count[i] * intNumberMailed * percentage[i]));
		screenexcost = (screenexcost + (count[i] * intNumberMailed * screeningcost[i]));
	}
			
			
	if ((screenexcost < 25) && (screenexcost > 0)) screenexcost = 25;

	//New DIV values
	showInfo('ExcludedResult', formatAmount(Math.round(totalex)));
	showInfo('ContactSaving', formatAmount(Math.round((intMailingCost / 100 * totalex))));
	showInfo('ScreeningCost', formatAmount(Math.round(screenexcost)));
	showInfo('CampaignSaving', formatAmount(Math.round(((intMailingCost / 100 * totalex) - screenexcost))));

}

function formatPenceField() {
	var t = setTimeout('formatPenceFieldTimeout()', 1000);
}

function formatPenceFieldTimeout() {
	var strPenceValue = document.calculator.mailpackpence.value;
	if(strPenceValue!='' && strPenceValue!='00') {
		while(strPenceValue.length < 2) { strPenceValue += "0"; }
		if(document.calculator.mailpackpence.value!=strPenceValue) {
			document.calculator.mailpackpence.value = strPenceValue;
		}
	}
}

function formatAmount(p_intValue){
	var intAmount = 0;
	if(!isNaN(p_intValue) && p_intValue!="" && p_intValue !="undefined" && p_intValue > 0) intAmount = FormatNumber(p_intValue, 0, false, false, true);
	return intAmount;
}

/**********************************************************************
	IN:
		NUM - the number to format
		decimalNum - the number of decimal places to format the number to
		bolLeadingZero - true / false - display a leading zero for
										numbers between -1 and 1
		bolParens - true / false - use parenthesis around negative numbers
		bolCommas - put commas as number separators.
 
	RETVAL:
		The formatted number!
 **********************************************************************/
function FormatNumber(num,decimalNum,bolLeadingZero,bolParens,bolCommas){ 
	if (isNaN(parseInt(num))) return 0;

	var tmpNum = num;
	var iSign = num < 0 ? -1 : 1;		// Get sign of number
	
	// Adjust number so only the specified number of numbers after
	// the decimal point are shown.
	tmpNum *= Math.pow(10,decimalNum);
	tmpNum = Math.round(Math.abs(tmpNum))
	tmpNum /= Math.pow(10,decimalNum);
	tmpNum *= iSign;					// Readjust for sign
	
	
	// Create a string object to do our formatting on
	var tmpNumStr = new String(tmpNum);

	// See if we need to strip out the leading zero or not.
	if (!bolLeadingZero && num < 1 && num > -1 && num != 0)
		if (num > 0)
			tmpNumStr = tmpNumStr.substring(1,tmpNumStr.length);
		else
			tmpNumStr = "-" + tmpNumStr.substring(2,tmpNumStr.length);
		
	// See if we need to put in the commas
	if (bolCommas && (num >= 1000 || num <= -1000)) {
		var iStart = tmpNumStr.indexOf(".");
		if (iStart < 0)
			iStart = tmpNumStr.length;

		iStart -= 3;
		while (iStart >= 1) {
			tmpNumStr = tmpNumStr.substring(0,iStart) + "," + tmpNumStr.substring(iStart,tmpNumStr.length)
			iStart -= 3;
		}		
	}

	// See if we need to use parenthesis
	if (bolParens && num < 0)
		tmpNumStr = "(" + tmpNumStr.substring(1,tmpNumStr.length) + ")";

	return tmpNumStr;		// Return our formatted string!
}


// Has return been pressed?
function keypress(){
	var key = event.keyCode;
	if (key == 13) calculate();
}

// Helper function to trap empty fields
function currencyFields(fld, p_strValue) {
	if(isNaN(fld.value) && p_strValue!='') fld.value = p_strValue;
	calculate();
}