// calculates color concentrate cost comparison
function calculateColor() {
	var option = 'color';
	
	resetFields(option);
	
	// initialize color concentrate 1 variables
	var usage_rate_1 = document.getElementById('usage_rate_1').value;
	var letdown_ratio_1 = document.getElementById('letdown_ratio_1').value;
	var color_concentrate_1 = document.getElementById('color_concentrate_1').value;
	
	// initialize color concentrate 1 variables
	var usage_rate_2 = document.getElementById('usage_rate_2').value;
	var letdown_ratio_2 = document.getElementById('letdown_ratio_2').value;
	var color_concentrate_2 = document.getElementById('color_concentrate_2').value;
	
	// initialize polymer cost
	var base_polymer_cost = document.getElementById('base_polymer_cost').value;
	
	// check for empty variables and correct data types
	var errors = false;
	
	if(usage_rate_1 == '' && letdown_ratio_1 == '') {
		errors = true;
		
		alert('Please input either Usage Rate 1 or Letdown Ratio 1');
	}
	
	if(usage_rate_1 != '' && letdown_ratio_1 != '') {
		errors = true;
		
		alert('Please input either Usage Rate 1 or Letdown Ratio 1, not both');
	}
	
	if(usage_rate_1 != '') {
		if(isNaN(usage_rate_1) == true) {
			errors = true;
			
			alert('Usage Rate 1 must be a number');
		}
		if(usage_rate_1 < 0) {
			errors = true;
			
			alert('Usage Rate 1 must be positive');
		}
		if (String(usage_rate_1).indexOf('.') < String(usage_rate_1).length - 3) {
			errors = true;
			
			alert('Usage Rate 1 can be at most 2 decimal places');
		}
	}
	
	if(letdown_ratio_1 != '') {
		if(isNaN(letdown_ratio_1) == true) {
			errors = true;
			
			alert('Letdown Ratio 1 must be a number');
		}
		if(letdown_ratio_1 < 0) {
			errors = true;
			
			alert('Letdown Ratio 1 must be positive');
		}
		if((typeof(letdown_ratio_1) != 'number') && (letdown_ratio_1.toString().indexOf('.') != -1)) {
			errors = true;
			
			alert('Letdown Ratio 1 must be an integer');
		}
	}
	
	if(color_concentrate_1 == '') {
		errors = true;
		
		alert('Color Concentrate 1 Cost is a required field');
	}
	
	if(color_concentrate_1 != '') {
		if(isNaN(color_concentrate_1) == true) {
			errors = true;
			
			alert('Color Concentrate 1 Cost must be a number');
		}
		if(color_concentrate_1 < 0) {
			errors = true;
			
			alert('Color Concentrate 1 Cost must be positive');
		}
		if (String(color_concentrate_1).indexOf('.') < String(color_concentrate_1).length - 3) {
			errors = true;
			
			alert('Color Concentrate 1 Cost can be at most 2 decimal places');
		}
	}
	
	if(usage_rate_2 == '' && letdown_ratio_2 == '') {
		errors = true;
		
		alert('Please input either Usage Rate 2 or Letdown Ratio 2');
	}
	
	if(usage_rate_2 != '' && letdown_ratio_2 != '') {
		errors = true;
		
		alert('Please input either Usage Rate 2 or Letdown Ratio 2, not both');
	}
	
	if(usage_rate_2 != '') {
		if(isNaN(usage_rate_2) == true) {
			errors = true;
			
			alert('Usage Rate 2 must be a number');
		}
		if(usage_rate_2 < 0) {
			errors = true;
			
			alert('Usage Rate 2 must be positive');
		}
		if (String(usage_rate_2).indexOf('.') < String(usage_rate_2).length - 3) {
			errors = true;
			
			alert('Usage Rate 2 can be at most 2 decimal places');
		}
	}
	
	if(letdown_ratio_2 != '') {
		if(isNaN(letdown_ratio_2) == true) {
			errors = true;
			
			alert('Letdown Ratio 2 must be a number');
		}
		if(letdown_ratio_2 < 0) {
			errors = true;
			
			alert('Letdown Ratio 2 must be positive');
		}
		if((typeof(letdown_ratio_2) != 'number') && (letdown_ratio_2.toString().indexOf('.') != -1)) {
			errors = true;
			
			alert('Letdown Ratio 2 must be an integer');
		}
	}
	
	if(color_concentrate_2 == '') {
		errors = true;
		
		alert('Color Concentrate 2 Cost is a required field');
	}
	
	if(color_concentrate_2 != '') {
		if(isNaN(color_concentrate_2) == true) {
			errors = true;
			
			alert('Color Concentrate 2 Cost must be a number');
		}
		if(color_concentrate_2 < 0) {
			errors = true;
			
			alert('Color Concentrate 2 Cost must be positive');
		}
		if (String(color_concentrate_2).indexOf('.') < String(color_concentrate_2).length - 3) {
			errors = true;
			
			alert('Color Concentrate 2 Cost can be at most 2 decimal places');
		}
	}
	
	if(base_polymer_cost == '') {
		errors = true;
		
		alert('Base Polymer Cost is a required field');
	}
	
	if(base_polymer_cost != '') {
		if(isNaN(base_polymer_cost) == true) {
			errors = true;
			
			alert('Base Polymer Cost must be a number');
		}
		if(base_polymer_cost < 0) {
			errors = true;
			
			alert('Base Polymer Cost must be positive');
		}
		if (String(base_polymer_cost).indexOf('.') < String(base_polymer_cost).length - 3) {
			errors = true;
			
			alert('Base Polymer Cost can be at most 2 decimal places');
		}
	}
	
	// calculate results if no errors
	if(errors == false) {
		var color_cost_1 = 0;
		var color_cost_2 = 0;
		var rate_1 = 0;
		var rate_2 = 0;
		
		if(usage_rate_1 != '' && letdown_ratio_1 == '') {
			rate_1 = usage_rate_1 / 100;
			color_cost_1 = (rate_1 * color_concentrate_1) + (1 - rate_1) * base_polymer_cost;
			color_cost_1 = Math.round(color_cost_1 * 100) / 100;
		}
		else if(usage_rate_1 == '' && letdown_ratio_1 != '') {
			rate_1 = 1 / letdown_ratio_1;
			color_cost_1 = (rate_1 * color_concentrate_1) + (1 - rate_1) * base_polymer_cost;
			color_cost_1 = Math.round(color_cost_1 * 100) / 100;
		}
		
		if(usage_rate_2 != '' && letdown_ratio_2 == '') {
			rate_2 = usage_rate_2 / 100;
			color_cost_2 = (rate_2 * color_concentrate_2) + (1 - rate_2) * base_polymer_cost;
			color_cost_2 = Math.round(color_cost_2 * 100) / 100;
		}
		else if(usage_rate_2 == '' && letdown_ratio_2 != '') {
			rate_2 = 1 / letdown_ratio_2;
			color_cost_2 = (rate_2 * color_concentrate_2) + (1 - rate_2) * base_polymer_cost;
			color_cost_2 = Math.round(color_cost_2 * 100) / 100;
		}
		
		// print solutions with corresponding classes
		if(color_cost_1 > color_cost_2) {
			document.getElementById('solution-1').className = 'red';
			document.getElementById('solution-1-text').innerHTML += ' &#36;' + color_cost_1 + ' / weight unit';
			document.getElementById('solution-2').className = 'green';
			document.getElementById('solution-2-text').innerHTML += ' &#36;' + color_cost_2 + ' / weight unit';
		}
		else if(color_cost_1 < color_cost_2) {
			document.getElementById('solution-1').className = 'green';
			document.getElementById('solution-1-text').innerHTML += ' &#36;' + color_cost_1 + ' / weight unit';
			document.getElementById('solution-2').className = 'red';
			document.getElementById('solution-2-text').innerHTML += ' &#36;' + color_cost_2 + ' / weight unit';
		}
		else {
			document.getElementById('solution-1').className = 'green';
			document.getElementById('solution-1-text').innerHTML += ' &#36;' + color_cost_1 + ' / weight unit';
			document.getElementById('solution-2').className = 'green';
			document.getElementById('solution-2-text').innerHTML += ' &#36;' + color_cost_2 + ' / weight unit';
		}
	}
}

// calculates pre-color conversion
function calculatePreColor() {
	var option = 'pre-color';
	
	resetFields(option);
	
	// initilialize color concentrate variables
	var pre_usage_rate = document.getElementById('pre_usage_rate').value;
	var pre_letdown_ratio = document.getElementById('pre_letdown_ratio').value;
	var pre_color_concentrate = document.getElementById('pre_color_concentrate').value;
	
	// initialize costs
	var pre_base_polymer_cost = document.getElementById('pre_base_polymer_cost').value;
	var current_pre_cost = document.getElementById('current_pre_cost').value;
	
	// check for empty variables and correct data types
	var errors = false;
	
	if(pre_usage_rate == '' && pre_letdown_ratio == '') {
		errors = true;
		
		alert('Please input either Usage Rate or Letdown Ratio');
	}
	
	if(pre_usage_rate != '' && pre_letdown_ratio != '') {
		errors = true;
		
		alert('Please input either Usage Rate or Letdown Ratio, not both');
	}
	
	if(pre_usage_rate != '') {
		if(isNaN(pre_usage_rate) == true) {
			errors = true;
			
			alert('Usage Rate must be a number');
		}
		if(pre_usage_rate < 0) {
			errors = true;
			
			alert('Usage Rate must be positive');
		}
		if (String(pre_usage_rate).indexOf('.') < String(pre_usage_rate).length - 3) {
			errors = true;
			
			alert('Usage Rate can be at most 2 decimal places');
		}
	}
	
	if(pre_letdown_ratio != '') {
		if(isNaN(pre_letdown_ratio) == true) {
			errors = true;
			
			alert('Letdown Ratio must be a number');
		}
		if(pre_letdown_ratio < 0) {
			errors = true;
			
			alert('Letdown Ratio must be positive');
		}
		if((typeof(pre_letdown_ratio) != 'number') && (pre_letdown_ratio.toString().indexOf('.') != -1)) {
			errors = true;
			
			alert('Letdown Ratio must be an integer');
		}
	}
	
	if(pre_color_concentrate == '') {
		errors = true;
		
		alert('Color Concentrate Cost is a required field');
	}
	
	if(pre_color_concentrate != '') {
		if(isNaN(pre_color_concentrate) == true) {
			errors = true;
			
			alert('Color Concentrate Cost must be a number');
		}
		if(pre_color_concentrate < 0) {
			errors = true;
			
			alert('Color Concentrate Cost must be positive');
		}
		if (String(pre_color_concentrate).indexOf('.') < String(pre_color_concentrate).length - 3) {
			errors = true;
			
			alert('Color Concentrate Cost can be at most 2 decimal places');
		}
	}
	
	if(pre_base_polymer_cost == '') {
		errors = true;
		
		alert('Base Polymer Cost is a required field');
	}
	
	if(pre_base_polymer_cost != '') {
		if(isNaN(pre_base_polymer_cost) == true) {
			errors = true;
			
			alert('Base Polymer Cost must be a number');
		}
		if(pre_base_polymer_cost < 0) {
			errors = true;
			
			alert('Base Polymer Cost must be positive');
		}
		if (String(pre_base_polymer_cost).indexOf('.') < String(pre_base_polymer_cost).length - 3) {
			errors = true;
			
			alert('Base Polymer Cost can be at most 2 decimal places');
		}
	}
	if(current_pre_cost == '') {
		errors = true;
		
		alert('Current Pre-Color Cost is a required field');
	}
	
	if(current_pre_cost != '') {
		if(isNaN(current_pre_cost) == true) {
			errors = true;
			
			alert('Current Pre-Color Cost must be a number');
		}
		if(current_pre_cost < 0) {
			errors = true;
			
			alert('Current Pre-Color Cost must be positive');
		}
		if (String(current_pre_cost).indexOf('.') < String(current_pre_cost).length - 3) {
			errors = true;
			
			alert('Current Pre-Color Cost can be at most 2 decimal places');
		}
	}
	
	// calculate results if no errors
	if(errors == false) {
		var pre_color_cost = 0;
		var savings = 0;
		var rate = 0;
		
		if(pre_usage_rate != '' && pre_letdown_ratio == '') {
			rate = pre_usage_rate / 100;
		}
		
		if(pre_usage_rate == '' && pre_letdown_ratio != '') {
			rate = 1 / pre_letdown_ratio;
		}
		
		pre_color_cost = (rate * pre_color_concentrate)  + (1 - rate) * pre_base_polymer_cost;
		pre_color_cost = Math.round(pre_color_cost * 100) / 100;
		
		savings = current_pre_cost - pre_color_cost;
		savings = Math.round(savings * 100) / 100;
		
		document.getElementById('pre-solution').className = 'yellow';
		document.getElementById('pre-solution-text').innerHTML += ' &#36;' + pre_color_cost + ' / weight unit';
		
		if(savings > 0) {
			document.getElementById('pre-savings').className = 'green';
			document.getElementById('pre-savings-text').innerHTML += ' &#36;' + savings + ' / weight unit';
		}
		else {
			document.getElementById('pre-savings').className = 'red';
			document.getElementById('pre-savings-text').innerHTML += ' &#36;' + savings + ' / weight unit';
		}
	}
}

// function that resets fields
function resetFields(option) {
	if(option == 'color') {
		document.getElementById('solution-1').className = 'solution';
		document.getElementById('solution-2').className = 'solution';
		document.getElementById('solution-1-text').innerHTML = 'Color Concentrate 1:';
		document.getElementById('solution-2-text').innerHTML = 'Color Concentrate 2:';
	}
	else if(option == 'pre-color') {
		document.getElementById('pre-solution').className = 'solution';
		document.getElementById('pre-savings').className = 'solution';
		document.getElementById('pre-solution-text').innerHTML = 'Color Concentrate:';
		document.getElementById('pre-savings-text').innerHTML = 'Savings Compared to Pre-Color:';
	}
}

// function that resets fields on calculator one
function resetColor() {
	document.getElementById('usage_rate_1').value = '';
	document.getElementById('letdown_ratio_1').value = '';
	document.getElementById('color_concentrate_1').value = '';
	document.getElementById('usage_rate_2').value = '';
	document.getElementById('letdown_ratio_2').value = '';
	document.getElementById('color_concentrate_2').value = '';
	document.getElementById('base_polymer_cost').value = '';
	
	// reset solution fields
	var option = 'color';
	
	resetFields(option);
}

// function that resets fields on calculator two
function resetPreColor() {
	document.getElementById('pre_usage_rate').value = '';
	document.getElementById('pre_letdown_ratio').value = '';
	document.getElementById('pre_color_concentrate').value = '';
	document.getElementById('pre_base_polymer_cost').value = '';
	document.getElementById('current_pre_cost').value = '';
	
	// reset solution fields
	var option = 'pre-color';
	
	resetFields(option);
}
