//Unit price calculator for EventU
//Calculate unit total and display best package
//Brian Robison - ServiceU Corp - All rights reserved  2.11.09

//Highlight row and calculate the total cost
var highLightRowColor = '#FFFF99';
var initRowColor = '#F0F6FE';
var initAltRowColor = '#FFFFFF';

//package levels 
var package10 = 54.95;
var package20 = 99.95;
var package40 = 189.95;
var package60 = 269.95;
var package80 = 339.95;
var package100 = 399.95;
	
//additional unit cost
var additionalUnits10 = 5.50;	
var additionalUnits20 = 5.00;
var additionalUnits40 = 4.75;
var additionalUnits60 = 4.50;
var additionalUnits80 = 4.25;
var additionalUnits100 = 4.00;
	
//number of units in each package
var package10units = 10;
var package20units = 20;
var package40units = 40;
var package60units = 60;
var package80units = 80;
var package100units = 100;
	
//best maximum value for number of units in each package
var bestMaxUnitValue10 = 18;
var bestMaxUnitValue20 = 37;
var bestMaxUnitValue40 = 56;
var bestMaxUnitValue60 = 75;
var bestMaxUnitValue80 = 94;
var bestMaxUnitValue100 = 100;

//Unit value of cost items - Users, Resources, View only users, and Subscribers 
var userUnitValue = 1.00;
var resourceUnitValue = .25;
var viewonlyUnitValue = .25;
var subscriberUnitValue = .01;

//start the calculator
var interval = 1;
function startCalc(){
	var interval = setInterval("unitCalc()",100);
	initTableColor();
}

//stop the calculator
function stopCalc(){
	clearInterval(interval);
}

//Initialize table row color
function initTableColor() {
		var table = document.getElementById('table2');
		table.rows[1].style.backgroundColor=initRowColor;
		table.rows[2].style.backgroundColor=initAltRowColor;
		table.rows[3].style.backgroundColor=initRowColor;
		table.rows[4].style.backgroundColor=initAltRowColor;
		table.rows[5].style.backgroundColor=initRowColor;
		table.rows[6].style.backgroundColor=initAltRowColor;
}

onload=initTableColor
		
//calculate the units
function unitCalc() {
	var costTotal = 0;
	var users = document.autoSumForm.users.value;
	var resources = document.autoSumForm.resources.value; 
	var viewonlyusers = document.autoSumForm.viewonlyusers.value;
	//var subscribers = document.autoSumForm.subscribers.value;
	//Subscribers should not show, requested by Tim.  2/18/09 - BR
	var subscribers = 0;
	var unitResult = ((users * userUnitValue) * 1) + ((resources * resourceUnitValue) * 1) + 
		((viewonlyusers * viewonlyUnitValue) * 1) + ((subscribers * subscriberUnitValue) * 1);
		unitTotal = unitResult.toFixed(2); 
		document.autoSumForm.unitTotal.value = unitTotal;
		
		initTableColor();
		
		//Highlight best package row and calculate the total for the best package
		var table = document.getElementById('table2');
		if (unitTotal != 0) {
			if (unitTotal <= bestMaxUnitValue10) {
				table.rows[1].style.backgroundColor=highLightRowColor;
				if (unitTotal >= package10units) {
					costTotal = package10 + (unitTotal - package10units) * additionalUnits10;
					}
				else {costTotal = package10}
				}
			else if (unitTotal > bestMaxUnitValue10 && unitTotal <= bestMaxUnitValue20) {
				table.rows[2].style.backgroundColor=highLightRowColor;
				if (unitTotal >= package20units) {
					costTotal = package20 + (unitTotal - package20units) * additionalUnits20;
					}
				else {costTotal = package20}
				}
			else if (unitTotal > bestMaxUnitValue20 && unitTotal <= bestMaxUnitValue40) {
				table.rows[3].style.backgroundColor=highLightRowColor;
				if (unitTotal >= package40units) {
					costTotal = package40 + (unitTotal - package40units) * additionalUnits40;
					}
				else {costTotal = package40}
				}
			else if (unitTotal > bestMaxUnitValue40 && unitTotal <= bestMaxUnitValue60) {
				table.rows[4].style.backgroundColor=highLightRowColor;
				if (unitTotal >= package60units) {
					costTotal = package60 + (unitTotal - package60units) * additionalUnits60;
					}
				else {costTotal = package60}
				}
			else if (unitTotal > bestMaxUnitValue60 && unitTotal <= bestMaxUnitValue80) {
				table.rows[5].style.backgroundColor=highLightRowColor;
				if (unitTotal >= package80units) {
					costTotal = package80 + (unitTotal - package80units) * additionalUnits80;
					}
				else {costTotal = package80}
				}
			else if (unitTotal > bestMaxUnitValue80) {
				table.rows[6].style.backgroundColor=highLightRowColor;
				if (unitTotal >= bestMaxUnitValue100) {
					costTotal = package100 + (unitTotal - package100units) * additionalUnits100;
					}
				else {costTotal = package100}
				}
			}
			
				
			costTotal = costTotal.toFixed(2);
			document.autoSumForm.costTotal.value = costTotal;
}


