﻿var Rates = {
	T73T74FixedCharge: 241.874,
	T73NightPerUnit: 10.095,
	T74DayPerUnit: 22.975,
	
	T22FixedCharge: 73.847,
	T22Step1PerUnit: 26.285,
	T22Step2PerUnit: 19.296,
	T22Threshold: 500,
	
	T82FixedCharge: 201.088,
	T82AllPerUnit: 10.331,
	T82Step1PerAnnum: 143.998,
	T82Step2PerAnnum: 143.998,
	T82Threshold: 250
	
};

function BillRow () {
	this.DaysInBill = 0;
	this.NightkWh = 0;
	this.DaykWh = 0;
	
	this.TotalkWh = null;
	this.PercentAtNight = null;
	this.T82DayDemand = null;
	
	this.T22 = null;
	this.T82 = null;
	this.T73 = null;
	this.T74 = null;
	this.TI = null;
	
	this.init = function () {
		var self = this;
	};
	
	this.TotalkWh = function () {
		var self = this;
		var output = 0;
		if (self.DaysInBill > 0) {
			output = Number(self.NightkWh) + Number(self.DaykWh);
		}
		return output;
	};
	
	this.PercentAtNight = function () {
		var self = this;
		var output = 0;
		if (self.DaysInBill > 0) {
			output = self.NightkWh / self.TotalkWh();
		}
		return output;
	};
	
	this.T82DayDemand = function () {
		var self = this;
		var output = 0;
		if (self.DaysInBill > 0) {
			output = self.DaykWh / self.DaysInBill * 365 / 8760 / 0.25 / 0.75;
		}
		return output;
	};
	
	this.T22 = function () {
		var self = this;
		return Rates.T22FixedCharge / 100 * self.DaysInBill + (self.TotalkWh() <= Rates.T22Threshold ? self.TotalkWh() * Rates.T22Step1PerUnit / 100 : Rates.T22Threshold * Rates.T22Step1PerUnit / 100 + (self.TotalkWh() - Rates.T22Threshold) * Rates.T22Step2PerUnit / 100);
	};
	
	this.T82 = function () {
		var self = this;
		return self.DaysInBill * Rates.T82FixedCharge / 100 + self.TotalkWh() * Rates.T82AllPerUnit / 100 + (self.T82DayDemand() <= Rates.T82Threshold ? self.T82DayDemand() * Rates.T82Step1PerAnnum * self.DaysInBill / 365 : Rates.T82Threshold * Rates.T82Step1PerAnnum * self.DaysInBill / 365 + (self.T82DayDemand() - Rates.T82Threshold) * Rates.T82Step2PerAnnum * self.DaysInBill / 365);
	};
	
	this.T73 = function () {
		var self = this;
		return self.NightkWh * Rates.T73NightPerUnit / 100;
	};
	
	this.T74 = function () {
		var self = this;
		return Rates.T73T74FixedCharge / 100 * self.DaysInBill + self.DaykWh * Rates.T74DayPerUnit / 100;
	};
	
	this.TI = function () {
		var self = this;
		return Number(self.T73()) + Number(self.T74());
	};
}

var IrrigationCalc = new function () {
	this.CalcDiv = null;
	this.CalcForm = null;
	this.CalcTable = null;
	this.ResultTable = null;
	this.NumRows = null;
	this.Bills = null;
	this.Message = null;
	
	this.init = function () {
		var self = IrrigationCalc;
		self.NumRows = 6;

		self.CalcDiv = document.getElementById('IrrigationCalc');
		self.CalcForm = document.createElement('form');
		self.CalcForm.id = 'CalcId';
		self.CalcForm.action = '';
		self.CalcForm.method = 'POST';
		self.CalcForm.onsubmit = null;
		self.CalcDiv.appendChild(self.CalcForm);

		self.Bills = new Array();
		
		for (var i = 1; i <= self.NumRows; i++) {
			self.Bills[i] = new BillRow();
		}
		
		self.createCalcTable();
		self.createResultTable();
		
		self.CalcForm.appendChild(self.CalcTable);
		self.CalcForm.appendChild(document.createElement('hr'));
		self.CalcForm.appendChild(self.ResultTable);
		self.CalcForm.appendChild(document.createElement('hr'));

/*
		var calcButton = document.createElement('input');
		calcButton.id = 'btnCalc';
		calcButton.name = 'btnCalc';
		calcButton.tabIndex = self.NumRows;
		calcButton.type = 'button';
		calcButton.value = 'Calculate';
		calcButton.onclick = self.doCalculations;
		self.CalcDiv.appendChild(calcButton);
*/
		self.Message = document.createElement('div');
		self.Message.id = 'divMessage';
		self.CalcDiv.appendChild(self.Message);
		
		enableTooltips("IrrigationCalc");
	};
	
	this.createCalcTable = function () {
		var self = IrrigationCalc;
		self.CalcTable = document.createElement('table');
		self.CalcTable.width = '100%';
		self.CalcTable.style.border = '#000000 0px solid';
		for (var i = 0; i <= self.NumRows; i++) {
			self.CalcTable.insertRow(i);
			self.CalcTable.rows[i].insertCell(0);
			self.CalcTable.rows[i].cells[0].style.width = '10%';
			self.CalcTable.rows[i].insertCell(1);
			self.CalcTable.rows[i].cells[1].style.width = '23%';
			self.CalcTable.rows[i].insertCell(2);
			self.CalcTable.rows[i].cells[2].style.width = '23%';
			self.CalcTable.rows[i].insertCell(3);
			self.CalcTable.rows[i].cells[3].style.width = '23%';
			if (i == 0) {
				self.CalcTable.rows[i].cells[0].innerHTML = '&nbsp;';
				self.CalcTable.rows[i].cells[1].style.fontWeight = 'bold';
				self.CalcTable.rows[i].cells[1].appendChild(document.createTextNode('Days in Bill '));
					var ToolTip1 = document.createElement('a');
					ToolTip1.href = '';
						var ToolTip1Img = document.createElement('img');
						ToolTip1Img.src = '../../images/tooltip.jpg';
						ToolTip1Img.style.verticalAlign = 'middle';
						ToolTip1Img.border = '0';
						ToolTip1Img.style.cursor = 'help';
					ToolTip1.appendChild(ToolTip1Img);
					ToolTip1.title = 'Number of days in this billing period';
					self.CalcTable.rows[i].cells[1].appendChild(ToolTip1);
				self.CalcTable.rows[i].cells[2].style.fontWeight = 'bold';
				self.CalcTable.rows[i].cells[2].appendChild(document.createTextNode('Night kWh '));
					var ToolTip2 = document.createElement('a');
					ToolTip2.href = '';
						var ToolTip2Img = document.createElement('img');
						ToolTip2Img.src = '../../images/tooltip.jpg';
						ToolTip2Img.style.verticalAlign = 'middle';
						ToolTip2Img.border = '0';
						ToolTip2Img.style.cursor = 'help';
					ToolTip2.appendChild(ToolTip2Img);
					ToolTip2.title = 'Is available for a period of at least 10 hours between 8pm EST and 7am EST.';
					self.CalcTable.rows[i].cells[2].appendChild(ToolTip2);
				self.CalcTable.rows[i].cells[3].style.fontWeight = 'bold';
				self.CalcTable.rows[i].cells[3].appendChild(document.createTextNode('Day kWh '));
					var ToolTip3 = document.createElement('a');
					ToolTip3.href = '';
						var ToolTip3Img = document.createElement('img');
						ToolTip3Img.src = '../../images/tooltip.jpg';
						ToolTip3Img.style.verticalAlign = 'middle';
						ToolTip3Img.border = '0';
						ToolTip3Img.style.cursor = 'help';
					ToolTip3.appendChild(ToolTip3Img);
					ToolTip3.title = 'Peak Rate available all times';
					self.CalcTable.rows[i].cells[3].appendChild(ToolTip3);
			}
			else {
				self.CalcTable.rows[i].cells[0].style.fontWeight = 'bold';
				self.CalcTable.rows[i].cells[0].appendChild(document.createTextNode('Bill ' + i));
				
				var inputDaysInBill = document.createElement('input');
				inputDaysInBill.id = 'dib_' + i;
				inputDaysInBill.name = 'dib_' + i;
				inputDaysInBill.tabIndex = i;
				inputDaysInBill.type = 'text';
				inputDaysInBill.maxLength = '13';
				inputDaysInBill.value = self.Bills[i].DaysInBill;
				inputDaysInBill.onkeyup = self.handleKeyUp;
				self.CalcTable.rows[i].cells[1].appendChild(inputDaysInBill);
				
				var inputNightkWh = document.createElement('input');
				inputNightkWh.id = 'nightkwh_' + i;
				inputNightkWh.name = 'nightkwh_' + i;
				inputNightkWh.tabIndex = i;
				inputNightkWh.type = 'text';
				inputNightkWh.maxLength = '13';
				inputNightkWh.value = self.Bills[i].NightkWh;
				inputNightkWh.onkeyup = self.handleKeyUp;
				self.CalcTable.rows[i].cells[2].appendChild(inputNightkWh);

				var inputDaykWh = document.createElement('input');
				inputDaykWh.id = 'daykwh_' + i;
				inputDaykWh.name = 'daykwh_' + i;
				inputDaykWh.tabIndex = i;
				inputDaykWh.type = 'text';
				inputDaykWh.maxLength = '13';
				inputDaykWh.value = self.Bills[i].DaykWh;
				inputDaykWh.onkeyup = self.handleKeyUp;
				self.CalcTable.rows[i].cells[3].appendChild(inputDaykWh);
			}
		}
	};

	this.createResultTable = function () {
		var self = IrrigationCalc;
		self.ResultTable = document.createElement('table');
		self.ResultTable.width = '100%';
		self.ResultTable.style.border = '#000000 0px solid';
		for (var i = 0; i <= self.NumRows; i++) {
			self.ResultTable.insertRow(i);
			self.ResultTable.rows[i].insertCell(0);
			self.ResultTable.rows[i].cells[0].style.width = '10%';
			self.ResultTable.rows[i].insertCell(1);
			self.ResultTable.rows[i].cells[1].style.width = '30%';
			self.ResultTable.rows[i].insertCell(2);
			self.ResultTable.rows[i].cells[2].style.width = '30%';
			self.ResultTable.rows[i].insertCell(3);
//			self.ResultTable.rows[i].cells[3].style.width = '18%';
			self.ResultTable.rows[i].insertCell(4);
//			self.ResultTable.rows[i].cells[4].style.width = '18%';
			self.ResultTable.rows[i].insertCell(5);
			self.ResultTable.rows[i].cells[5].style.width = '30%';
			if (i == 0) {
				self.ResultTable.rows[i].cells[0].innerHTML = '&nbsp;';
				self.ResultTable.rows[i].cells[1].style.fontWeight = 'bold';
				self.ResultTable.rows[i].cells[1].appendChild(document.createTextNode('General'));
				self.ResultTable.rows[i].cells[1].appendChild(document.createElement('br'));
				self.ResultTable.rows[i].cells[1].appendChild(document.createTextNode('(Tariff 22) '));
					var ToolTip4 = document.createElement('a');
					ToolTip4.href = '';
						var ToolTip4Img = document.createElement('img');
						ToolTip4Img.src = '../../images/tooltip.jpg';
						ToolTip4Img.style.verticalAlign = 'middle';
						ToolTip4Img.border = '0';
						ToolTip4Img.style.cursor = 'help';
					ToolTip4.appendChild(ToolTip4Img);
					ToolTip4.title = 'Tariff 22 does not offer an off peak rate. Its energy charge is lower than the day but higher than the night rate of the irrigation tariff. It also has a lower daily charge.';
					self.ResultTable.rows[i].cells[1].appendChild(ToolTip4);
				self.ResultTable.rows[i].cells[3].style.fontWeight = 'bold';
				self.ResultTable.rows[i].cells[3].appendChild(document.createTextNode('Irrigation Night'));
				self.ResultTable.rows[i].cells[3].appendChild(document.createElement('br'));
				self.ResultTable.rows[i].cells[3].appendChild(document.createTextNode('(Tariff 73) '));
					var ToolTip6 = document.createElement('a');
					ToolTip6.href = '';
						var ToolTip6Img = document.createElement('img');
						ToolTip6Img.src = '../../images/tooltip.jpg';
						ToolTip6Img.style.verticalAlign = 'middle';
						ToolTip6Img.border = '0';
						ToolTip6Img.style.cursor = 'help';
					ToolTip6.appendChild(ToolTip6Img);
					ToolTip6.title = '';
					self.ResultTable.rows[i].cells[3].appendChild(ToolTip6);
				self.ResultTable.rows[i].cells[3].style.display = 'none';
				self.ResultTable.rows[i].cells[4].style.fontWeight = 'bold';
				self.ResultTable.rows[i].cells[4].appendChild(document.createTextNode('Irrigation Day'));
				self.ResultTable.rows[i].cells[4].appendChild(document.createElement('br'));
				self.ResultTable.rows[i].cells[4].appendChild(document.createTextNode('(Tariff 74) '));
					var ToolTip7 = document.createElement('a');
					ToolTip7.href = '';
						var ToolTip7Img = document.createElement('img');
						ToolTip7Img.src = '../../images/tooltip.jpg';
						ToolTip7Img.style.verticalAlign = 'middle';
						ToolTip7Img.border = '0';
						ToolTip7Img.style.cursor = 'help';
					ToolTip7.appendChild(ToolTip7Img);
					ToolTip7.title = '';
					self.ResultTable.rows[i].cells[4].appendChild(ToolTip7);
				self.ResultTable.rows[i].cells[4].style.display = 'none';
				self.ResultTable.rows[i].cells[5].style.fontWeight = 'bold';
				self.ResultTable.rows[i].cells[5].appendChild(document.createTextNode('Total Irrigation'));
				self.ResultTable.rows[i].cells[5].appendChild(document.createElement('br'));
				self.ResultTable.rows[i].cells[5].appendChild(document.createTextNode('(Tariffs 73 / 74) '));
					var ToolTip8 = document.createElement('a');
					ToolTip8.href = '';
						var ToolTip8Img = document.createElement('img');
						ToolTip8Img.src = '../../images/tooltip.jpg';
						ToolTip8Img.style.verticalAlign = 'middle';
						ToolTip8Img.border = '0';
						ToolTip8Img.style.cursor = 'help';
					ToolTip8.appendChild(ToolTip8Img);
					ToolTip8.title = 'Irrigation pumps may be supplied under Irrigation Tariff 73/74. This tariff is best suited if you can time your pump to irrigate overnight to take advantage of the low overnight rate.';
					self.ResultTable.rows[i].cells[5].appendChild(ToolTip8);
			}
			else {
				self.ResultTable.rows[i].cells[0].style.fontWeight = 'bold';
				self.ResultTable.rows[i].cells[0].appendChild(document.createTextNode('Bill ' + i));
				
				var outputT22 = document.createElement('input');
				outputT22.id = 't22_' + i;
				outputT22.name = 't22_' + i;
				outputT22.tabIndex = i + 10;
				outputT22.type = 'text';
				outputT22.value = formatDollars(self.Bills[i].T22());
				outputT22.className = 'readOnly';
				outputT22.readOnly = 'readonly';
				self.ResultTable.rows[i].cells[1].appendChild(outputT22);
				
				var outputT82 = document.createElement('input');
				outputT82.id = 't82_' + i;
				outputT82.name = 't82_' + i;
				outputT82.tabIndex = i + 10;
				outputT82.type = 'text';
				outputT82.value = '';
				outputT82.className = 'readOnly';
				outputT82.readOnly = 'readonly';
				self.ResultTable.rows[i].cells[2].appendChild(outputT82);
				
				var outputT73 = document.createElement('input');
				outputT73.id = 't73_' + i;
				outputT73.name = 't73_' + i;
				outputT73.tabIndex = i + 10;
				outputT73.type = 'text';
				outputT73.value = formatDollars(self.Bills[i].T73());
				outputT73.className = 'readOnly';
				outputT73.readOnly = 'readonly';
				self.ResultTable.rows[i].cells[3].appendChild(outputT73);
				self.ResultTable.rows[i].cells[3].style.display = 'none';
				
				var outputT74 = document.createElement('input');
				outputT74.id = 't74_' + i;
				outputT74.name = 't74_' + i;
				outputT74.tabIndex = i + 10;
				outputT74.type = 'text';
				outputT74.value = formatDollars(self.Bills[i].T74());
				outputT74.className = 'readOnly';
				outputT74.readOnly = 'readonly';
				self.ResultTable.rows[i].cells[4].appendChild(outputT74);
				self.ResultTable.rows[i].cells[4].style.display = 'none';
				
				var outputTI = document.createElement('input');
				outputTI.id = 'ti_' + i;
				outputTI.name = 'ti_' + i;
				outputTI.tabIndex = i + 10;
				outputTI.type = 'text';
				outputTI.value = formatDollars(self.Bills[i].TI());
				outputTI.className = 'readOnly';
				outputTI.readOnly = 'readonly';
				self.ResultTable.rows[i].cells[5].appendChild(outputTI);
			}
		}
		
		var totalRow = self.NumRows + 1;
		self.ResultTable.insertRow(totalRow);
		self.ResultTable.rows[totalRow].insertCell(0);
		self.ResultTable.rows[totalRow].cells[0].style.width = '10%';
		self.ResultTable.rows[totalRow].insertCell(1);
		self.ResultTable.rows[totalRow].cells[1].style.width = '30%';
		self.ResultTable.rows[totalRow].insertCell(2);
		self.ResultTable.rows[totalRow].cells[2].style.width = '30%';
		self.ResultTable.rows[totalRow].insertCell(3);
//		self.ResultTable.rows[totalRow].cells[3].style.width = '18%';
		self.ResultTable.rows[totalRow].insertCell(4);
//		self.ResultTable.rows[totalRow].cells[4].style.width = '18%';
		self.ResultTable.rows[totalRow].insertCell(5);
		self.ResultTable.rows[totalRow].cells[5].style.width = '30%';
		
		self.ResultTable.rows[totalRow].cells[0].style.fontWeight = 'bold';
		self.ResultTable.rows[totalRow].cells[0].appendChild(document.createTextNode('Total'));
		
		var outputTotalT22 = document.createElement('input');
		outputTotalT22.id = 'total_t22';
		outputTotalT22.name = 'total_t22';
		outputTotalT22.type = 'text';
		outputTotalT22.value = formatDollars(0);
		outputTotalT22.className = 'readOnly';
		outputTotalT22.readonly = true;
		self.ResultTable.rows[totalRow].cells[1].appendChild(outputTotalT22);
		
		var outputTotalT82 = document.createElement('input');
		outputTotalT82.id = 'total_t82';
		outputTotalT82.name = 'total_t82';
		outputTotalT82.type = 'text';
		outputTotalT82.value = '';
		outputTotalT82.className = 'readOnly';
		outputTotalT82.readonly = true;
		self.ResultTable.rows[totalRow].cells[2].appendChild(outputTotalT82);
		
		var outputTotalT73 = document.createElement('input');
		outputTotalT73.id = 'total_t73';
		outputTotalT73.name = 'total_t73';
		outputTotalT73.type = 'text';
		outputTotalT73.value = formatDollars(0);
		outputTotalT73.className = 'readOnly';
		outputTotalT73.readonly = true;
		self.ResultTable.rows[totalRow].cells[3].appendChild(outputTotalT73);
		self.ResultTable.rows[totalRow].cells[3].style.display = 'none';
		
		var outputTotalT74 = document.createElement('input');
		outputTotalT74.id = 'total_t74';
		outputTotalT74.name = 'total_t74';
		outputTotalT74.type = 'text';
		outputTotalT74.value = formatDollars(0);
		outputTotalT74.className = 'readOnly';
		outputTotalT74.readonly = true;
		self.ResultTable.rows[totalRow].cells[4].appendChild(outputTotalT74);
		self.ResultTable.rows[totalRow].cells[4].style.display = 'none';
		
		var outputTotalTI = document.createElement('input');
		outputTotalTI.id = 'total_ti';
		outputTotalTI.name = 'total_ti';
		outputTotalTI.type = 'text';
		outputTotalTI.value = formatDollars(0);
		outputTotalTI.className = 'readOnly';
		outputTotalTI.readonly = true;
		self.ResultTable.rows[totalRow].cells[5].appendChild(outputTotalTI);
	};
	
	this.handleKeyUp = function (e) {
		var self = IrrigationCalc;
		
		var KeyCode = (window.event ? event.keyCode : e.keyCode);
		var SrcElement = (window.event ? event.srcElement.id : this.id);
		var CurrentElement = document.getElementById(SrcElement);
		
		if (isNaN(CurrentElement.value))
		{
			self.showMessage('Some of the information entered is incorrectly formatted.  Please ensure all information is entered in numeric form only.', 'e');
		}
		else if (((KeyCode >= 48) && (KeyCode <= 57)) || ((KeyCode >= 96) && (KeyCode <= 105)) || (KeyCode == 8) || (KeyCode == 46))
		{
			self.showMessage('');
			self.doCalculations();
		}
	};
	
	this.doCalculations = function () {
		var self = IrrigationCalc;
		var TotalT22 = 0;
		var TotalT82 = 0;
		var TotalT73 = 0;
		var TotalT74 = 0;
		var TotalTI = 0;
		
		for (var i = 1; i <= self.NumRows; i++) {
			var inputDaysInBill = document.getElementById('dib_' + i);
			var inputNightkWh = document.getElementById('nightkwh_' + i);
			var inputDaykWh = document.getElementById('daykwh_' + i);

			inputDaysInBill.value = formatNumber(inputDaysInBill.value);
			inputNightkWh.value = formatNumber(inputNightkWh.value);
			inputDaykWh.value = formatNumber(inputDaykWh.value);

			self.Bills[i].DaysInBill = inputDaysInBill.value;
			self.Bills[i].NightkWh = inputNightkWh.value;
			self.Bills[i].DaykWh = inputDaykWh.value;
		}

		var NumberOfDays = 0;
		for (var i = 1; i <= self.NumRows; i++) {
			NumberOfDays += Number(self.Bills[i].DaysInBill);
		}
		
		var NightkWhTotal = 0;
		for (var i = 1; i <= self.NumRows; i++) {
			NightkWhTotal += Number(self.Bills[i].NightkWh);
		}
		
		var DaykWhTotal = 0;
		for (var i = 1; i <= self.NumRows; i++) {
			DaykWhTotal += Number(self.Bills[i].DaykWh);
		}
		
		if (NumberOfDays > 365) {
			self.showMessage('Your total number of days in bill adds up to more than 1 year.', 'e');

			for (var i = 1; i <= self.NumRows; i++) {
				var outputT22 = document.getElementById('t22_' + i);
				var outputT82 = document.getElementById('t82_' + i);
				var outputT73 = document.getElementById('t73_' + i);
				var outputT74 = document.getElementById('t74_' + i);
				var outputTI = document.getElementById('ti_' + i);
				
				outputT22.value = formatDollars(0);
				outputT82.value = formatDollars(0);
				outputT73.value = formatDollars(0);
				outputT74.value = formatDollars(0);
				outputTI.value = formatDollars(0);
			}

			TotalT22 = 0;
			TotalT82 = 0;
			TotalT73 = 0;
			TotalT74 = 0;
			TotalTI = 0;
			
			document.getElementById('total_t22').value = formatDollars(TotalT22);
			//document.getElementById('total_t82').value = formatDollars(TotalT82);
			document.getElementById('total_t73').value = formatDollars(TotalT73);
			document.getElementById('total_t74').value = formatDollars(TotalT74);
			document.getElementById('total_ti').value = formatDollars(TotalTI);
		}
		else {
			for (var i = 1; i <= self.NumRows; i++) {
				var outputT22 = document.getElementById('t22_' + i);
				//var outputT82 = document.getElementById('t82_' + i);
				var outputT73 = document.getElementById('t73_' + i);
				var outputT74 = document.getElementById('t74_' + i);
				var outputTI = document.getElementById('ti_' + i);
				
				outputT22.value = formatDollars(self.Bills[i].T22());
				//outputT82.value = formatDollars(self.Bills[i].T82());
				outputT73.value = formatDollars(self.Bills[i].T73());
				outputT74.value = formatDollars(self.Bills[i].T74());
				outputTI.value = formatDollars(self.Bills[i].TI());
				
				TotalT22 += Number(self.Bills[i].T22());
				//TotalT82 += Number(self.Bills[i].T82());
				TotalT73 += Number(self.Bills[i].T73());
				TotalT74 += Number(self.Bills[i].T74());
				TotalTI += Number(self.Bills[i].TI());
			}
			
			document.getElementById('total_t22').value = formatDollars(TotalT22);
			//document.getElementById('total_t82').value = formatDollars(TotalT82);
			document.getElementById('total_t73').value = formatDollars(TotalT73);
			document.getElementById('total_t74').value = formatDollars(TotalT74);
			document.getElementById('total_ti').value = formatDollars(TotalTI);
			
			if (NumberOfDays > (365 / 2))
			{
				var bestOption = new Array();
				if ((TotalT22 < TotalT82) && (TotalT22 < TotalTI))
				{
					bestOption[0] = 'T22';
					bestOption[1] = 'General (Tariff 22)';
				}
				else if ((TotalTI < TotalT22) && (TotalTI < TotalT82))
				{
					bestOption[0] = 'TI';
					bestOption[1] = 'Irrigation Night / Day (Tariff 73 / 74)';
				}
				
				var NightPercentUsage = Math.round((NightkWhTotal / (NightkWhTotal + DaykWhTotal)) * 100);
				var DayPercentUsage = Math.round((DaykWhTotal / (NightkWhTotal + DaykWhTotal)) * 100);

				var Msg = '';
				Msg += 'Based on the information you have provided, your best option may be ' + bestOption[1] + '.';
				if (NightPercentUsage < 75)
				{
					if ((bestOption[0] != 'T22') && (NightkWhTotal != 0))
					{
						Msg += '<br />You may be able to save even more if you change your usage to use more at night!';
					}
					if (bestOption[0] == 'T22')
					{
						Msg += '<br />Did you know that if you use your electricity at night, you can potentially save money?';
					}
				}
//				Msg += 'Night %: ' + NightPercentUsage + '<br />Day %: ' + DayPercentUsage + '';
				
				self.showMessage(Msg, '');
			}
		}
	};
	
	this.showMessage = function(messageText, messageType) {
		var self = IrrigationCalc;
		
		if (messageType == 'e')
		{
			self.Message.style.color = '#990000';
			self.Message.style.fontWeight = 'bold';
			self.Message.style.textAlign = 'center';
		}
		else
		{
			self.Message.style.color = '#009900';
			self.Message.style.fontWeight = 'bold';
			self.Message.style.textAlign = 'center';
		}
		
		self.Message.innerHTML = messageText;
	};
};

function formatDollars(num)
{
	num = num.toString().replace(/\$|\,/g, '');
	if (isNaN(num))
		num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num * 100 + 0.50000000001);
	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 formatDollars(num) {
	num = num.toString();
	var output = '';
	var numParts = num.split('.');
	
	if (numParts[1])
	{
		numParts[1] = Math.round(numParts[1].substr(0,3) / 10);
	}

	if (numParts[0].length > 3)
	{
		var tmpParts = new Array();
		for (var j = 0; j < numParts[0].length; j = j + 3)
		{
		  tmpParts.push(numParts[0].substr(j, 3));
		}
		tmpParts.reverse();
		numParts[0] = tmpParts.join(',');
	}
	
	output = '$' + numParts.join('.');
	
//	output += ' (' + num.substr(num.indexOf('.')+1).length + ')';
	
	return output;
}
*/

function formatNumber(num) {
	if (num.indexOf('$') != -1) {
		num = num.split('$');
		num = num.join();
	}
	if (num.indexOf(',') != -1) {
		num = num.split(',');
		num = num.join('');
	}
	return num;
}

if (window.addEventListener)
{
	window.addEventListener('load', IrrigationCalc.init, false);
}
else
{
	window.attachEvent('onload', IrrigationCalc.init);
}

//window.onload = IrrigationCalc.init;
