window.addEvent('domready', function(){
	if($('CU1')){
		// check if the radio button is pre-chosen on page reload
		if($('CU1').checked){
			$('yesidouseacorporatesocialnetworkingservice').removeClass('hide');
		}else{
			$('yesidouseacorporatesocialnetworkingservice').addClass('hide');
		}

		$('CU1').onclick = function(){
			if(this.checked){
				$('yesidouseacorporatesocialnetworkingservice').removeClass('hide');
			}else{
				$('yesidouseacorporatesocialnetworkingservice').addClass('hide');
			}
		}
		$('CU2').onclick = function(){
			if(this.checked){
				$('yesidouseacorporatesocialnetworkingservice').addClass('hide');
			}else{
				$('yesidouseacorporatesocialnetworkingservice').removeClass('hide');
			}
		}
		$('iglooroicalculatorform').onsubmit = function(){return validate();}
	}
	
	// if this is the result page, 
	if(gup('NE')!="" && $('roiresultsdiv')){
		calculate();
	} 
});

function validate(){

	var errors = new Array();
	
	if($('NE').value.length < 1){errors.push('Number of Employees cannot be empty');}
	if(!IsNumeric($('NE').value)){errors.push('Number of Employees must be a number');}
	if($('AR').value.length < 1){errors.push('Annual Revenue cannot be empty');}
	if(!IsNumeric($('AR').value)){errors.push('Annual Revenue must be a number');}
	if($('EP').value.length < 1){errors.push('% of employees cannot be empty');}
	if($('EP').value > 100){errors.push('% of employees must be less than 100%');}
	if(!IsNumeric($('EP').value)){errors.push('% of employees must be a number');}
	if($('CU1').checked){
		if($('HC').value.length < 1){errors.push('Supporting software and hardware cost cannot be empty');}
		if(!IsNumeric($('HC').value)){errors.push('Supporting software and hardware cost must be a number');}
		if($('MC').value.length < 1){errors.push('Annual maintenance cost cannot be empty');}
		if(!IsNumeric($('MC').value)){errors.push('Annual maintenance cost must be a number');}
		if($('IT').value.length < 1){errors.push('Staff dedicated to IT cannot be empty');}
		if(!IsNumeric($('IT').value)){errors.push('Staff dedicated to IT must be a number');}
	}
	for(var i=0;i<errors.length;i++){
		alert(errors[i]);
	}
	return (errors.length == 0);
	
}

function gup( name ){
  // this function grabs values from the querystring
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null ){
    return "";
  }else{
    return results[1];
  }
}


function calculate(){


		var output = ""; 

		var NE = parseInt(gup('NE'));
		var AR = parseInt(gup('AR'));
		var EP = parseInt(gup('EP'))/100;
		var HC = parseInt(gup('HC'));
		var MC = parseInt(gup('MC'));
		var IT = parseInt(gup('IT'));
		
		var grandtotal1 = 0;
		var grandtotal3 = 0;
		
		
		output += "<div class='results'>";
		
		

		output += "<table cellpadding='10'>";

		output += "<tr><td colspan='3'><h2>Human Capital Costs</h2></td></tr>";


			output += "<tr>";
				output += "<th></th>";
				output += "<th>First Year</th>";
				output += "<th>3 Years</th>";
			output += "</tr>";


		/*
		FTE Savings = $240,000 FTE cost over 3 years saved with SaaS (based on $80k per year)
		*/

		var fte = 80000;

		grandtotal1 = grandtotal1 + fte;
		grandtotal3 = grandtotal3 + fte*3;
		
				
				
		/*
		Time wasted searching/indexing = (# of employees* percentage access to email) * ($2496*3years)
		-  $1.2 million per year, $3.7 million over 3 yrs
		*/			

		var tws = NE * EP * 2496;

		output += "<tr>";
			output += "<td class='cell-grey'>";
				output += "<h3>Productivity Loss</h3>";
				output += "<p>On average, each knowledge worker wastes $2,500 per year<br/>searching for information.</p>";
			output += "</td>";
			output += "<td class='numbercell cell-grey'>";
				output += formatCurrency(tws);
			output += "</td>";
			output += "<td class='numbercell cell-grey'>";
				output += formatCurrency(tws*3);
			output += "</td>";
		output += "</tr>";

		grandtotal1 = grandtotal1 + tws;
		grandtotal3 = grandtotal3 + (tws*3);

		/*
		Information not Accessible = (# of employees* percentage access to email) * ($5251*3years)
		                      - $2.6 million per year, 7.88 million over 3 yrs
		*/
		var ina = NE * EP * 5251;

		output += "<tr>";
			output += "<td>";
				output += "<h3>Information Silo Cost</h3>";
				output += "<p>On average, each knowledge worker wastes $5,200 per year<br/>by not having access to the right information.</p>";
			output += "</td>";
			output += "<td class='numbercell'>";
				output += formatCurrency(ina);
			output += "</td>";
			output += "<td class='numbercell'>";
				output += formatCurrency(ina*3);
			output += "</td>";
		output += "</tr>";

		grandtotal1 = grandtotal1 + ina;
		grandtotal3 = grandtotal3 + (ina*3);
				
				
		/*
		Cost of Re-working Information = (# of employees* percentage access to email) * ($5850 *3years)
		-	$2.9 million per year, $ 8.7 million over 3 yrs
		*/

		var cri = NE * EP * 5850;

		output += "<tr>";
			output += "<td class='cell-grey'>";
				output += "<h3>Re-working Information Cost</h3>";
				output += "<p>On average, each knowledge worker wastes $5,850 per year re-working or re-creating existing corporate information and knowledge.</p>";
			output += "</td>";
			output += "<td class='numbercell cell-grey'>";
				output += formatCurrency(cri);
			output += "</td>";
			output += "<td class='numbercell cell-grey'>";
				output += formatCurrency(cri*3);
			output += "</td>";
		output += "</tr>";

		grandtotal1 = grandtotal1 + cri;
		grandtotal3 = grandtotal3 + (cri*3);

		/*
		Opportunity Cost = ([Revenue per Employee] 250 million /500 employees = 500k
		-	500k / 2080 hours worked = $240 per hour
		*/
		var rpe = NE==0?0:(AR / NE);
		var pph = rpe / 2080;

		
		
		/*
		Knowledge worker email waste (3.5 hours per week) – 3.5*52 weeks * 240 per hour * 500 employees
		 = $22 million per year - $66 million over 3 years
		 */
		 
		 // 182 is # hours wasted, per employee, per year
		 
		 
		var kew = (3.5 * 52) * NE * EP * (80000/2080);

		output += "<tr>";
			output += "<td>";
				output += "<h3>Email Management Cost</h3>";
				output += "<p>On average, each knowledge worker wastes 3.5 hours per week<br/>sending, responding and managing their email inbox.</p>";
			output += "</td>";
			output += "<td class='numbercell'>";
				output += formatCurrency(kew);
			output += "</td>";
			output += "<td class='numbercell'>";
				output += formatCurrency(kew * 3);
			output += "</td>";
		output += "</tr>";

		grandtotal1 = grandtotal1 + kew;
		grandtotal3 = grandtotal3 + (kew*3);
	 
		 /*
		 Duplicated effort – (1.25 hours per week wasted per employee) – 1.25*52*240 per hour *500 employees
		 = $7.8 million per year - $23 million over 3 years
		*/


		var due = (1.25 * 52) * NE * (80000/2080);

		output += "<tr>";
			output += "<td class='cell-grey'>";
				output += "<h3>Duplicated Efforts</h3>";
				output += "<p>On average, each knowledge worker wastes 1.5 hours per week<br/>duplicating work done by others.</p>";
			output += "</td>";
			output += "<td class='numbercell cell-grey'>";
				output += formatCurrency(due);
			output += "</td>";
			output += "<td class='numbercell cell-grey'>";
				output += formatCurrency(due * 3);
			output += "</td>";
		output += "</tr>";

		grandtotal1 = grandtotal1 + due;
		grandtotal3 = grandtotal3 + (due*3);


		
		
		



		/*do this only if the user clicked "yes"*/
		if(gup('CU')==1 || gup('CU')=='Yes'){
            
            
            output += "<tr><td colspan='3'><h2>IT Infrastructure Costs</h2></td></tr>";
            output += "<tr>";
                output += "<th></th>";
                output += "<th>First Year</th>";
                output += "<th>3 Years</th>";
            output += "</tr>";

		
			/* hardware savings*/


			var itsavings1 = HC + MC;
			var itsavings3 = HC + (MC*3);
			
			output += "<tr>";
				output += "<td class='cell-grey'>";
					output += "<h3>Initial Hardware and Software Savings</h3>";
					output += "<p>Savings based on moving to a SaaS model. Renting rather than <br/>purchasing expensive hardware and software applications upfront.</p>";

				output += "</td>";
				output += "<td class='numbercell cell-grey'>";
					output += formatCurrency(itsavings1);
				output += "</td>";
				output += "<td class='numbercell cell-grey'>";
					output += formatCurrency(itsavings3);
				output += "</td>";
			output += "</tr>";

			grandtotal1 = grandtotal1 + itsavings1;
			grandtotal3 = grandtotal3 + itsavings3;


			/* staffing savings*/

			var avgsalaryetc = 80000;
			var hrsavings = IT * avgsalaryetc;

			output += "<tr>";
				output += "<td class=''>";
					output += "<h3>Potential IT Staff Savings</h3>";
					output += "<p>On average, a mid-level IT Administrator's fully burdened salary is $80,000 per year.</p>";
				output += "</td>";
				output += "<td class='numbercell'>";
					output += formatCurrency(hrsavings);
				output += "</td>";
				output += "<td class='numbercell'>";
					output += formatCurrency(hrsavings * 3);
				output += "</td>";
			output += "</tr>";

			grandtotal1 = grandtotal1 + hrsavings;
			grandtotal3 = grandtotal3 + (hrsavings*3);

		} // end if Yes for CU

				
				
				
		output += "</table>";
		output += "</div>";
			
		$('roiresultsdiv').innerHTML = output;

}


function formatCurrency(num) {
	var num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num)){num = "0";}
	var sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	var cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10){cents = "0" + cents;}
	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 + '.' + cents);
}



function IsNumeric(sText){
	var ValidChars = "0123456789.";
	var IsNumber=true;
	var Char;
	for (i = 0; i < sText.length && IsNumber == true; i++){ 
		Char = sText.charAt(i); 
		if (ValidChars.indexOf(Char) == -1){IsNumber = false;}
	}
	return IsNumber;
}

