
function jsTitleCalc2()
{
	var _county;
	var _liabilityAmount;
	var _type;
	var _types;
	
	var _addCommas = function(nStr) {
		// From http://www.mredkj.com/javascript/numberFormat.html
		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;
	};
	
	var _calcDavidson = function(result) {
		// Base policy charge
		var basePolicyCharge = 200;
		
		var perThousandValue1k = 6;
		var perThousandValue100k = 4.5;
		var perThousandValue500k = 3;
		
		// Clamp to [1000, 1000000]
		_liabilityAmount = Math.min(Math.max(1000, _liabilityAmount), 1000000);
		
		var policyCharge = basePolicyCharge;
		if (_liabilityAmount > 500000) {
			policyCharge += (_liabilityAmount/1000 - 500) * perThousandValue500k;
			policyCharge += (400) * perThousandValue100k;
			policyCharge += (100-1) * perThousandValue1k;
		} else if (_liabilityAmount > 100000) {
			policyCharge += (_liabilityAmount/1000 - 100) * perThousandValue100k;
			policyCharge += (100-1) * perThousandValue1k;
		} else {
			policyCharge += (_liabilityAmount/1000 - 1) * perThousandValue1k;
		}
		
		if (_type == "eagle") {
			policyCharge *= 1.2;
		}
		
		result.policyCharge = _displayAsMoney(policyCharge);
		
		result.error = false;
	};
	
	var _calcHamilton = function(result) {
		// Base policy charge
		var basePolicyCharge = 200;
		
		var perThousandValue1k = 6;
		var perThousandValue100k = 3;
		
		// Clamp to [1000, 1000000]
		_liabilityAmount = Math.min(Math.max(1000, _liabilityAmount), 1000000);
		
		var policyCharge = basePolicyCharge;
		if (_liabilityAmount > 100000) {
			policyCharge += (_liabilityAmount/1000 - 100) * perThousandValue100k;
			policyCharge += (100-1) * perThousandValue1k;
		} else {
			policyCharge += (_liabilityAmount/1000 - 1) * perThousandValue1k;
		}
		
		if (_type == "eagle") {
			policyCharge *= 1.2;
		}
		
		result.policyCharge = _displayAsMoney(policyCharge);
		
		result.error = false;
	};
	
	var _calcKnox = _calcHamilton;
	
	var _calcShelby = function(result) {
		// Base policy charge
		var basePolicyCharge = 200;
		
		var perThousandValue1k = 4;
		var perThousandValue100k = 3;
		
		// Clamp to [1000, 1000000]
		_liabilityAmount = Math.min(Math.max(1000, _liabilityAmount), 1000000);
		
		var policyCharge = basePolicyCharge;
		if (_liabilityAmount > 100000) {
			policyCharge += (_liabilityAmount/1000 - 100) * perThousandValue100k;
			policyCharge += (100-1) * perThousandValue1k;
		} else {
			policyCharge += (_liabilityAmount/1000 - 1) * perThousandValue1k;
		}
		
		if (_type == "eagle") {
			policyCharge *= 1.2;
		}
		
		result.policyCharge = _displayAsMoney(policyCharge);
		
		result.error = false;
	};
	
	var _calcDefault = function(result) {
		// Base policy charge
		var basePolicyCharge = 150;
		
		var perThousandValue1k = 4;
		var perThousandValue38k = 4;
		var perThousandValue50k = 3.5;
		var perThousandValue100k = 2.5;
		
		// Clamp to [1000, 1000000]
		_liabilityAmount = Math.min(Math.max(1000, _liabilityAmount), 1000000);
		
		var policyCharge = basePolicyCharge;
		if (_liabilityAmount > 100000) {
			policyCharge += (_liabilityAmount/1000 - 100) * perThousandValue100k;
			policyCharge += (50) * perThousandValue50k;
			policyCharge += (12) * perThousandValue38k;
			policyCharge += 2;
		} else if (_liabilityAmount > 50000) {
			policyCharge += (_liabilityAmount/1000 - 50) * perThousandValue50k;
			policyCharge += (12) * perThousandValue38k;
			policyCharge += 2;
		} else if (_liabilityAmount > 38000) {
			policyCharge += (_liabilityAmount/1000 - 38) * perThousandValue38k;
			policyCharge += 2;
		} else if (_liabilityAmount > 37000) {
			policyCharge += 2;
		} else {
			//policyCharge += (_liabilityAmount/1000 - 1) * perThousandValue1k;
		}
		
		if (_type == "eagle") {
			policyCharge *= 1.2;
		}
		
		result.policyCharge = _displayAsMoney(policyCharge);
		
		result.error = false;
	};
	
	var _displayAsMoney = function(number) {
		return "$" + _addCommas(number.toFixed(2));
	};
	
	/*
	var _roundToMoney = function(number) {
		return Math.round(100 * number) / 100;
	};
	*/
	
	this.calculate = function() {
		var result = {
			error: true,
			message: "Unknown error"
		};
		
		if (_type) {
			switch (_county) {
				case "davidson":
					_calcDavidson(result);
					break;
					
				case "hamilton":
					_calcHamilton(result);
					break;
					
				case "knox":
					_calcKnox(result);
					break;
					
				case "shelby":
					_calcShelby(result);
					break;
					
				default:
					_calcDefault(result);
			}
			
			//result.error = false;
			//result.message = "Done";
		} else {
			result.message = "No policy/issue type selected.";
		}
		
		return result;
	};
	
	this.setCounty = function(newValue) {
		_county = newValue ? newValue : "";
		//alert("_county: " + _county);
	};
	
	this.setLiabilityAmount = function(newValue) {
		_liabilityAmount = newValue ? 1000 * (Math.round(newValue / 1000)) : 0;
		//alert("_liabilityAmount: " + _liabilityAmount);
	};
	
	this.setType = function(newValue) {
		_type = (_types.indexOf(newValue) >= 0) ? newValue : "";
		//alert("_type: " + _type);
	};
	
	// Init
	_county = "";
	_liabilityAmount = 0;
	_type = "";
	_types = $([
		"standard",
		"eagle"
	]);
	
}

if (typeof Prototype != "undefined") {
	document.observe("dom:loaded", function() {
		function getFieldValue(objForm, strFieldName)
		{
			var ret = "";
			
			if (objForm && strFieldName) {
				if (objForm.elements[strFieldName]) {
					var objField = objForm.elements[strFieldName];
					//alert("strFieldName = " + strFieldName + ", objForm.elements[strFieldName] = " + objField);
					
					// Radio buttons, apparently?
					if (objField.length && !objField.options) {
						//alert("objField.length = " + objField.length);
						for (var i = 0; i < objField.length; i++) {
							//alert("objField[i] = " + objField[i]);
							if (objField[i].checked) {
								ret = objField[i].value;
								break;
							}
						}
					} else {
						// Select
						if (objField.options) {
							ret = objField.options[objField.selectedIndex].value;
						} else {
							// Input/Textarea
							//alert(objField.value);
							ret = objField.value;
						}
					}
				}
			}
			
			return ret;
		}
		
		function getFieldNumber(objForm, strFieldName)
		{
			var ret = 0;
			
			var val = getFieldValue(objForm, strFieldName);
			if (val) {
				// Remove commas (and by way of this, digit grouping)
				val = val.replace(",", "");
				
				// Pull a dollars-and-cents-format floating point number out of the value
				var matches = val.match(/\d+(.\d{1,2})?/);
				//alert("matches: " + matches);
				if (matches) {
					return (matches[0] * 1.0);
				}
			}
			
			return ret;
		}
		
		$$("form.jsTitleCalc2").each(function(item) {
			var calcButton = item.select("input.calculate").first();
			if (calcButton) {
				item.calc = new jsTitleCalc2();
				
				calcButton.observe("click", function() {
					item.calc.setCounty(getFieldValue(item, "county"));
					//alert("County = " + getFieldValue(item, "county"));
					
					item.calc.setLiabilityAmount(getFieldNumber(item, "liability_amount"));
					//alert("Loan Amount = " + getFieldNumber(item, "loan_amount"));
					
					item.calc.setType(getFieldValue(item, "policy_type"));
					//alert("Policy/Issue Type = " + getFieldValue(item, "policy_type"));
					
					var result = item.calc.calculate();
					if (result.error) {
						alert("Error: " + result.message);
					} else {
						//alert("Success: " + result.message);
						var resultContainer = $("calc_result");
						if (resultContainer) {
							while (resultContainer.firstChild) {
								resultContainer.removeChild(resultContainer.firstChild);
							}
							//resultContainer.appendChild(document.createTextNode(result.message));
							
							// Display the results
							resultContainer.appendChild(document.createElement("br"));
							resultContainer.appendChild(document.createTextNode("Policy Charge: " + result.policyCharge));
							
						}
					}
				});
			}
		});
	});
}

