function CPACalculate(e) {
	var target = getTrigger(e);
	var table = new Array();
	table ['F'] = new Array();
	table ['G'] = new Array();
	table ['H'] = new Array();
	table ['I'] = new Array();

	table['G'][3] = $('in_1').value;
	table['H'][3] = $('in_1').value;
	table['I'][3] = $('in_1').value;
	
	table['F'][7] = $('in_2').value;
	table['F'][8] = $('in_3').value;
	table['F'][9] = $('in_4').value;
	
	$('in_5').value = 0;
	if($('in_2').value != '') $('in_5').value = parseFloat($('in_5').value) + parseFloat($('in_2').value);
	if($('in_3').value != '') $('in_5').value = parseFloat($('in_5').value) + parseFloat($('in_3').value);
	if($('in_4').value != '') $('in_5').value = parseFloat($('in_5').value) + parseFloat($('in_4').value);
	
	if($('in_2').value != 0 && $('in_3').value != 0 && $('in_4').value == 0) {
		var rest = Math.round((parseFloat(100)-parseFloat($('in_2').value)-parseFloat($('in_3').value))*100)/100;
		$('in_4').value = rest;
		$('in_5').value = 100;
	}
	
	table['H'][7] = (table['H'][3]/100) * table['F'][7];
	table['G'][7] = table['H'][7];
	table['I'][7] = table['H'][7];
	
	table['H'][8] = (table['H'][3]/100) * table['F'][8];
	table['G'][8] = table['H'][8];
	table['I'][8] = table['H'][8];
	
	table['H'][9] = (table['H'][3]/100) * table['F'][9];
	table['G'][9] = table['H'][9];
	table['I'][9] = table['H'][9];

	table['G'][11] = 0;	table['H'][11] = $('in_7').value; table['I'][11] = 98;
	
	table['G'][13] = (1-(table['G'][11]/100))*table['G'][8];
	table['H'][13] = (1-(table['H'][11]/100))*table['H'][8];
	table['I'][13] = (1-(table['I'][11]/100))*table['I'][8];

	table['H'][15] = $('in_8').value;
	table['G'][15] = table['H'][15];
	table['I'][15] = table['H'][15];

	table['G'][19] = table['G'][13]*table['G'][15];
	table['H'][19] = table['H'][13]*table['H'][15];
	table['I'][19] = table['I'][13]*table['I'][15];

	table['G'][20] = table['G'][19]/60;
	table['H'][20] = table['H'][19]/60;
	table['I'][20] = table['I'][19]/60;
	
	table['G'][21] = table['G'][20]/60;
	table['H'][21] = table['H'][20]/60;
	table['I'][21] = table['I'][20]/60;	
	
	table['H'][23] = $('in_9').value;
	table['G'][23] = table['H'][23];
	table['I'][23] = table['H'][23];
	
	table['G'][25] = table['G'][21]*table['G'][23];
	table['H'][25] = table['H'][21]*table['H'][23];	
	table['I'][25] = table['I'][21]*table['I'][23];	

	table['H'][27] = $('in_10').value;
	table['G'][27] = table['H'][27];
	table['I'][27] = table['H'][27];
	
	table['G'][29] = table['G'][25]*table['G'][27];
	table['H'][29] = table['H'][25]*table['H'][27];	
	table['I'][29] = table['I'][25]*table['I'][27];
	

	table['H'][37] = $('in_11').value;
	table['G'][37] = table['H'][37];
	table['I'][37] = table['H'][37];
	
	table['H'][36] = $('in_12').value;
	table['G'][36] = table['H'][36];
	table['I'][36] = table['H'][36];	

	table['G'][38] = (table['G'][7]*table['G'][36])/(table['G'][37]*60*60);
	table['H'][38] = (table['H'][7]*table['H'][36])/(table['H'][37]*60*60);
	table['I'][38] = (table['I'][7]*table['I'][36])/(table['I'][37]*60*60);	
	
	table['G'][39] = (table['G'][13]*table['G'][15])/(8*60*60);
	table['H'][39] = (table['H'][13]*table['H'][15])/(8*60*60);	
	table['I'][39] = (table['I'][13]*table['I'][15])/(8*60*60);	
	
	table['G'][40] = Math.ceil(table['G'][38]+table['G'][39]);
	table['H'][40] = Math.ceil(table['H'][38]+table['H'][39]);	
	table['I'][40] = Math.ceil(table['I'][38]+table['I'][39]);
	
	table['I'][44] = parseFloat(15)+parseFloat(table['I'][36]);	
	table['I'][45] = parseFloat(30);
	table['I'][46] = parseFloat(15)+parseFloat(table['I'][15]);		
	table['I'][47] = parseFloat(15)+parseFloat(table['I'][40]);			
	table['I'][49] = Math.ceil(parseFloat((table['F'][7]/100)*table['I'][44]) + parseFloat(table['I'][45]*(table['I'][11]/100)*(table['F'][8]/100)) + parseFloat(table['I'][46]*(parseFloat(1)-(table['I'][11]/100))*(table['F'][8]/100)) + parseFloat(table['I'][47]*(table['F'][8]/100)));
	table['I'][51] = table['I'][3]/table['I'][37];
	table['I'][53] = table['I'][49]*table['I'][51];	
	table['I'][55] = parseFloat(3600);		
	table['I'][57] = Math.round(table['I'][53]/table['I'][55]);
	table['I'][59] = Math.round(table['I'][57]/table['I'][40]);
	
	var buttonGroup = document['forms']['calculate']['in_6']
	
	var radio = '';
	for (var i=0; i<buttonGroup.length; i++) {
		if (buttonGroup[i].checked == true) {
			radio = buttonGroup[i].value;
		}
	}

	if(radio == 'Y') {
		table['I'][31] = Math.round((table['H'][29]-table['I'][29])*100)/100;
	} else {
		table['I'][31] = Math.round((table['G'][29]-table['I'][29])*100)/100;
	}

	table['I'][61] = Math.round((table['I'][31]/table['I'][57])*100)/100;
	
	if(target.id == "calculate") {
		var error = 0;
		if($('in_1').value == 0) {error = 1; $('in_1').className = "calc_error";}
		if($('in_2').value == 0) {error = 1; $('in_2').className = "calc_error";}
		if($('in_3').value == 0) {error = 1; $('in_3').className = "calc_error";}
		if($('in_4').value == 0) {error = 1; $('in_4').className = "calc_error";}
		if(radio == '') {error = 1; $('in_6_holder').style.backgroundColor = '#FDE4E3';}
		if($('in_7').value == 0 && radio == 'Y') {error = 1; $('in_7').className = "calc_error";}	
		if($('in_8').value == 0) {error = 1; $('in_8').className = "calc_error";}
		if($('in_9').value == 0) {error = 1; $('in_9').className = "calc_error";}
		if($('in_10').value == 0) {error = 1; $('in_10').className = "calc_error";}
		if($('in_11').value == 0) {error = 1; $('in_11').className = "calc_error";}
		if($('in_12').value == 0) {error = 1; $('in_12').className = "calc_error";}		
	
		if(error == 1) {
			alert('Please input all required fields!');
			return false;
		}
		
		if(parseInt($('in_5').value) != 100 && error == 0) {
			alert('Percent must total 100%');
			$('in_5').style.backgroundColor = '#FDE4E3';
			return false;
		}

		$('a').innerHTML = table['G'][3];
		$('b').innerHTML = table['G'][3];		
		$('1a').innerHTML = table['F'][7]+'%';
		$('1b').innerHTML = table['F'][7]+'%'; 
		$('2a').innerHTML = table['F'][8]+'%';
		$('2b').innerHTML = table['F'][8]+'%'; 		
		$('3a').innerHTML = table['F'][9]+'%';
		$('3b').innerHTML = table['F'][9]+'%';
		if(table['H'][11] != 0) {
			$('4a').innerHTML = table['H'][11]+'%'; 
		} else {
			$('4a').innerHTML = 'n/a';
		}
		
		if(radio == 'Y') {
			$('5a').innerHTML = Math.round(table['H'][13]); 
		} else {
			$('5a').innerHTML = Math.round(table['G'][13]);
		}
		
		$('5b').innerHTML = Math.round(table['I'][13]);
		$('6a').innerHTML = table['H'][15];
		$('6b').innerHTML = table['H'][15];

		if(radio == 'Y') {
			$('7a').innerHTML = Math.round(table['H'][19]);
			$('8a').innerHTML = Math.round(table['H'][20]);
			$('9a').innerHTML = Math.round(table['H'][21]); 
		} else {
			$('7a').innerHTML = Math.round(table['G'][19]);
			$('8a').innerHTML = Math.round(table['G'][20]);
			$('9a').innerHTML = Math.round(table['G'][21]); 
		}
		
		$('7b').innerHTML = Math.round(table['I'][19]);
		$('8b').innerHTML = Math.round(table['I'][20]);
		$('9b').innerHTML = Math.round(table['I'][21]);
		
		$('10a').innerHTML = Math.round(table['H'][23]);
		$('10b').innerHTML = Math.round(table['H'][23]);

		if(radio == 'Y') {
			$('11a').innerHTML = Math.round(table['H'][25]);
		} else {
			$('11a').innerHTML = Math.round(table['G'][25]);
		}
		$('11b').innerHTML = Math.round(table['I'][25]);
		
		$('12a').innerHTML = Math.round(table['H'][27]);
		$('12b').innerHTML = Math.round(table['H'][27]);
		
		if(radio == 'Y') {				
			$('13a').innerHTML = Math.round(table['H'][29]);
		} else {
			$('13a').innerHTML = Math.round(table['G'][29]);
		}
		$('13b').innerHTML = Math.round(table['I'][29]);

		$('15a').innerHTML = table['G'][36];
		$('15b').innerHTML = table['I'][36];
		$('16a').innerHTML = table['G'][37];
		$('16b').innerHTML = table['I'][37];
		$('17a').innerHTML = Math.round(table['G'][38]*100)/100;
		$('17b').innerHTML = Math.round(table['I'][38]*100)/100;		
		
		if(radio == 'Y') {				
			$('18a').innerHTML = Math.round(table['H'][39]*100)/100;
		} else {
			$('18a').innerHTML = Math.round(table['G'][39]*100)/100;
		}
		$('18b').innerHTML = Math.round(table['I'][39]*100)/100;		


		if(radio == 'Y') {				
			$('19a').innerHTML = Math.round(table['H'][40]*100)/100;
		} else {
			$('19a').innerHTML = Math.round(table['G'][40]*100)/100;
		}
		$('19b').innerHTML = Math.round(table['I'][40]*100)/100;


		$('14b').innerHTML = '$'+numberFormat(table['I'][31]);
		$('total_cost').innerHTML = '$'+numberFormat(table['I'][31]) + ' annually';
		
		if(radio == 'Y') {				
			$('total_resource1').innerHTML = Math.abs(Math.round(table['I'][40]-table['H'][40]))+' fewer agents per day';
			$('total_resource2').innerHTML = Math.abs(Math.round(table['I'][40]-table['H'][40]))+' Agents';
		} else {
			$('total_resource1').innerHTML = Math.abs(Math.round(table['I'][40]-table['G'][40]))+' fewer agents per day';
			$('total_resource2').innerHTML = Math.abs(Math.round(table['I'][40]-table['G'][40]))+' Agents';
		}		

		$('calc_form').style.display = 'none';
		$('calc_summary').style.display = ''; 
	
		//alert('$ ' + numberFormat(table['I'][31]) + '$ ' + numberFormat(table['I'][61]));
	}
	
	if(target.value == '' && target.id != 'id_6' && target.id != 'calculate') target.value = 0;
		
}

function CPACheckEmpty(e) {
	var target = getTrigger(e);
	if(target.value == 0 && target.id != 'id_6') target.value = '';
}

function CPACheckAscii(e) {
	var target = getTrigger(e);
	var asciiCode = (e.which || e.charCode || e.keyCode);

	if((asciiCode < 48 || asciiCode > 57) && asciiCode != 8 && asciiCode != 46 && asciiCode != 1 && asciiCode != 190) {
		target.value = target.value.replace(/[^\d\.]/g,"");
		return false;
	}
}

function CPAtoggle(value) {
	if(value == 'Y') {
		$('accurate').style.display='';
	} else {
		$('accurate').style.display='none';
		$('in_7').value=0;
	}
}

function CPAreset() {
	$('calc_form').style.display = '';
	$('calc_summary').style.display = 'none';
	for(var i = 1; i < 13; i++) {
		if(i != 6) {
			$('in_'+i).value=0;
			$('in_'+i).className='calc';
		}
	}

	$('in_6_holder').style.backgroundColor = '#fff';
	$('in_5').style.backgroundColor = '#fff';
	$('in_9').value = 312;
	
	var buttonGroup = document['forms']['calculate']['in_6'];
	
	for (var i=0; i<buttonGroup.length; i++) {
		buttonGroup[i].checked = false;
	}
	
	setTabs('tabs2', 1, 'tabDisplay2');
}

function CPAattach() {
	for(var i = 1; i < 13; i++) {
		if(i != 6) {
			addListener($('in_'+i), 'keyup', CPACheckAscii, true);
			addListener($('in_'+i), 'focus', CPACheckEmpty, true);
			addListener($('in_'+i), 'blur', CPACalculate, true);
		}
	}	
}

function CPArecalculate() {
	modalCancelSKU();
	CPAreset();
	modal_summary(660,500,26,false,20);
}

function numberFormat(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;
}