﻿if (!Utils) throw ('Missing object: Utils');

var InputFieldManager = {
	formName: document.URL.substring(document.URL.lastIndexOf("\/") + 1, document.URL.lastIndexOf("\.")),
	inited: false,
	changed: false,
	prefix: '',

	init: function() {
		var fields = this.getInputs();
		var className, isNumericClass, isWellFormatted, type, id, prefix, index;
		for (var i = 0; i < fields.length; i++) {
			className = this.getFirstClassName(fields[i]);
			isNumericClass = (this.getClassName(fields[i]).indexOf('numeric') != -1);
			isWellFormatted = (this.getClassName(fields[i]).indexOf('well_formatted') != -1);
			type = this.getTypeOfControl(fields[i]);
			id = fields[i].getAttribute('id');

			if (isNumericClass && !isWellFormatted) {
				fields[i].value = fields[i].value.replace(',', '');
				Utils.addListener(fields[i], 'change', function() { this.value = (this.value) ? this.value.replace(',', '') : '' });
			}

			if (className && ((className == 'RequiredField') || (className == 'OptionalField'))) {
				switch (type) {
					case 'select':
					case 'select-one':
						if (fields[i].selectedIndex > 0) InputFieldManager.unmark(fields[i]);
						Utils.addListener(fields[i], 'change', this.onBlur);
						break;
					case 'text':
						if (fields[i].value) InputFieldManager.unmark(fields[i]);
						Utils.addListener(fields[i], 'blur', this.onBlur);
						break;
				}
			} else if (type != 'hidden') Utils.addListener(fields[i], 'change', this.registerChange);
			if (type == 'text') Utils.addListener(fields[i], 'focus', this.onFocus);

			if (id && (id.indexOf('hdn_Mode') != -1) && this.prefix == '') this.prefix = id.substring(0, id.indexOf('hdn_Mode')) + 'cph_Form_';
			if (id && (id.indexOf('hdn_EnabledJS') != -1) && this.prefix != '') $gE(this.prefix.replace('cph_Form_', '') + 'hdn_EnabledJS').value = 'true';
			InputFieldManager.manageSpecialInputs(fields[i]);
		}
		this.inited = true;
	},

	onBlur: function(e) {
		var ed = Utils.getEventDetails(e);
		var type = InputFieldManager.getTypeOfControl(ed.target);
		var className = InputFieldManager.getFirstClassName(ed.target);
		var optional = (className == 'OptionalField');
		var manageIsReady = InputFieldManager.manageSpecialInputs(ed.target); ;

		if (!manageIsReady) {
			switch (type) {
				case 'text':

					if (ed.target.value) InputFieldManager.unmark(ed.target);
					else InputFieldManager.mark(ed.target);
					break;
				case 'select':
				case 'select-one':
					if (ed.target.selectedIndex > 0) InputFieldManager.unmark(ed.target);
					else InputFieldManager.mark(ed.target);
					break;
			}
		}
		InputFieldManager.registerChange(e);
	},

	onFocus: function(e) {
		var ed = Utils.getEventDetails(e);
		if (ed.target.value.indexOf('$') == 0) ed.target.value = ed.target.value.substring(1);
	},

	mark: function(e) {
		var bgHex = Utils.RGBtoHex(e.style.backgroundColor).replace('#', '');
		if ((bgHex != 'f0d6d6') && (bgHex != 'd6e4f5') && (bgHex != 'd8e5f5'))
			Utils.setStyle(e, { 'background': '#fff' });     /* c4e150 */
		InputFieldManager.emptyRequired++;
	},

	unmark: function(e) {
		var bgHex = Utils.RGBtoHex(e.style.backgroundColor).replace('#', '');
		var className = InputFieldManager.getFirstClassName(e);
		var optional = (className == 'OptionalField');

		if ((bgHex != 'f0d6d6') && (bgHex != 'd6e4f5') && (bgHex != 'd8e5f5')) {
			if (optional) Utils.setStyle(e, { 'background': '#fff' });
			else Utils.setStyle(e, { 'background': '#FFFFFF' });
		}
		InputFieldManager.emptyRequired--;
	},

	registerChange: function(e) {
		InputFieldManager.changed = true;
	},

	confirmIfChanged: function(e) {
		if (InputFieldManager.changed && ($gE(InputFieldManager.prefix.replace('cph_Form_', '') + 'hdn_Mode').value != 'View')) {
			return confirm('Click OK to abandon your updates.\n\nOtherwise click Cancel and then click on the Save button.');
		} else return true;
	},

	manageSpecialInputs: function(ctrl) {
		var reply = false;

		switch (InputFieldManager.formName) {
			case 'GeneralInformation': reply = InputFieldManager.manageGeneralInfo(ctrl); break;
			case 'SiteInformation': reply = InputFieldManager.manageSiteInfo(ctrl); break;
			case 'Measures': reply = InputFieldManager.manageMeasures(ctrl); break;
			case 'Summary': reply = InputFieldManager.manageSummary(ctrl); break;
			default: break;
		}

		return reply;
	},

	manageGeneralInfo: function(ctrl) {
		var reply = false;
		var id = ctrl.getAttribute('id');

		if (!id) return false;

		if ((id.indexOf('ApplicantContact') != -1) && this.prefix && this.inited) $gE(this.prefix + 'cb_SameAsTheBuildingOwner').checked = false;

		return reply;
	},

	manageSiteInfo: function(ctrl) {
		var id = ctrl.getAttribute('id');
		var prefix;
		var reply = false;

		if (!id) return false;

		if ((id.indexOf('ddl_Characteristics_BuildingType') != -1)) {
			prefix = id.substring(0, id.indexOf('ddl_Characteristics_BuildingType'));

			if ($gE(prefix + 'txt_Characteristics_Other')) {
				if (ctrl.selectedIndex == 5) {
					$gE(prefix + 'txt_Characteristics_Other').disabled = false;
					InputFieldManager.mark($gE(prefix + 'txt_Characteristics_Other'));
					Utils.addListener($gE(prefix + 'txt_Characteristics_Other'), 'blur', InputFieldManager.onBlur);
					if ($gE(prefix + 'txt_Characteristics_Other').value != '') InputFieldManager.unmark($gE(prefix + 'txt_Characteristics_Other'));
				} else {
					$gE(prefix + 'txt_Characteristics_Other').value = '';
					$gE(prefix + 'txt_Characteristics_Other').disabled = true;
					InputFieldManager.unmark($gE(prefix + 'txt_Characteristics_Other'));
					Utils.removeListener($gE(prefix + 'txt_Characteristics_Other'), 'blur', InputFieldManager.onBlur);
				}
			}

			//reply = true;
		}

		if ((id.indexOf('ddl_Characteristics_ProjectType') != -1)) {
			prefix = id.substring(0, id.indexOf('ddl_Characteristics_ProjectType'));

			Utils.setStyle($gE('projectType_portfolio'), { 'display': ((ctrl.selectedIndex == 1) ? 'inline-block' : 'none') });

			var isPortfolio = (ctrl.options[ctrl.selectedIndex].value == 'portfolio');
			Utils.setStyle($gE(prefix + 'txt_Characteristics_GrossFloorArea'), { 'background': ((isPortfolio) ? '#d8e5f5' : '#fff') });
			Utils.setStyle($gE(prefix + 'txt_Characteristics_EAC'), { 'background': ((isPortfolio) ? '#d8e5f5' : '#fff') });
			Utils.setStyle($gE(prefix + 'txt_Characteristics_ESPD'), { 'background': ((isPortfolio) ? '#d8e5f5' : '#fff') });
			Utils.setStyle($gE(prefix + 'txt_GGPAPPT_ProjectStart'), { 'background': ((isPortfolio) ? '#d8e5f5' : '#fff') });
			Utils.setStyle($gE(prefix + 'txt_GGPAPPT_ProjectFinish'), { 'background': ((isPortfolio) ? '#d8e5f5' : '#fff') });

			$gE(prefix + 'ddl_Characteristics_ExistingCoolingSystem').disabled = isPortfolio;
			$gE(prefix + 'ddl_Characteristics_ExistingHeatingSystem').disabled = isPortfolio;
			if (isPortfolio) {
				$gE(prefix + 'ddl_Characteristics_ExistingCoolingSystem').selectedIndex = 0;
				$gE(prefix + 'ddl_Characteristics_ExistingHeatingSystem').selectedIndex = 0;
			}
		}

		if ((id.indexOf('lbl_GGPAPPT_GGPAT') != -1)) {
			Utils.setStyle($gE('tr_GGPAPPT_GGPAT'), { 'display': ((ctrl.value != '') ? 'table-row' : 'none') });
		}

		return reply;
	},

	checkOtherType: function(sender, args) {
		var ddlBuildingType = $gE(InputFieldManager.prefix + 'ddl_Characteristics_BuildingType');
		var txtOtherType = $gE(InputFieldManager.prefix + 'txt_Characteristics_Other');

		args.IsValid = !((ddlBuildingType.selectedIndex == 5) && (txtOtherType.value == ''));
	},

	manageMeasures: function(ctrl) {
		var id = ctrl.getAttribute('id');
		var prefix, index, currentCtrl, controlID;
		var reply = false;
		var controls = [
			{
				'name': 'Other',
				'cast': '',
				'type': 'txt',
				'enabledMeasureIndex': ['>0'],
				//'enabledMeasureIndex': ['>1', '!=3', '!=5', '!=6', '!=7', '!=8', '!=9', '<11'],
				'enabledAccess': ['Edit'],
				'enabledMetric': null,
				'autoCalculating': false,
				'calculateEstimated': false
			}, {
				'name': 'IncentiveMetric',
				'cast': '', 'type': 'ddl',
				'enabledMeasureIndex': ['>0'],
				//'enabledMeasureIndex': ['>0', '!=6'],
				'enabledAccess': ['Edit', 'Aeir'],
				'enabledMetric': null,
				'autoCalculating': false,
				'calculateEstimated': false
			}, {
				'name': 'PeakDemand',
				'cast': 'Existing',
				'type': 'txt',
				'enabledMeasureIndex': ['>0'],
				//'enabledMeasureIndex': ['>0', '!=6'],
				'enabledAccess': ['Edit', 'Aeir', 'Feir'],
				'enabledMetric': 'kW',
				'autoCalculating': true,
				'calculateEstimated': true
			}, {
				'name': 'AnnualConsumption',
				'cast': 'Existing',
				'type': 'txt',
				'enabledMeasureIndex': ['>0'],
				//'enabledMeasureIndex': ['>0', '!=6'],
				'enabledAccess': ['Edit', 'Aeir', 'Feir'],
				'enabledMetric': 'kWh',
				'autoCalculating': true,
				'calculateEstimated': true
			}, {
				'name': 'PeakDemand',
				'cast': 'Proposed',
				'type': 'txt',
				'enabledMeasureIndex': ['>0'],
				//'enabledMeasureIndex': ['>0', '!=6'],
				'enabledAccess': ['Edit', 'Aeir', 'Feir'],
				'enabledMetric': 'kW',
				'autoCalculating': true,
				'calculateEstimated': true
			}, {
				'name': 'AnnualConsumption',
				'cast': 'Proposed',
				'type': 'txt',
				'enabledMeasureIndex': ['>0'],
				//'enabledMeasureIndex': ['>0', '!=6'],
				'enabledAccess': ['Edit', 'Aeir', 'Feir'],
				'enabledMetric': 'kWh',
				'autoCalculating': true,
				'calculateEstimated': true
			}, {
				'name': 'TonsOfCooling',
				'cast': '',
				'type': 'txt',
				'enabledMeasureIndex': ['==6'],
				'enabledAccess': ['Edit', 'Aeir', 'Feir'],
				'enabledMetric': 'Ton',
				'autoCalculating': true,
				'calculateEstimated': false
			}, {
				'name': 'PrescriptiveIncentive',
				'cast': '', 'type': 'txt',
				'enabledMeasureIndex': ['>16', '<19'],
				'enabledAccess': ['Edit'],
				'enabledMetric': null,
				'autoCalculating': false,
				'calculateEstimated': false
			}
		];

		if (!id) return false;

		if ((id.indexOf('ddl_Measures_Type') != -1)) {
			prefix = id.substring(0, id.indexOf('ddl_Measures_Type'));
			index = id.substring(id.indexOf('ddl_Measures_Type') + 'ddl_Measures_Type'.length);

			var isEnabled = false;
			var expression, enabledAccess;
			var access = $gE(prefix.replace('cph_Form_', '') + 'hdn_Mode').value;
			for (var j = 0; j < controls.length; j++) {
				currentCtrl = $gE(prefix + controls[j].type + '_Measures_' + controls[j].cast + controls[j].name + index);
				if (!currentCtrl) alert(prefix + controls[j].type + '_Measures_' + controls[j].cast + controls[j].name + index);

				enabledAccess = false;
				for (var k = 0; k < controls[j].enabledAccess.length; k++)
					if (controls[j].enabledAccess[k] == access)
					enabledAccess = true;

				expression = new Array;
				for (var k = 0; k < controls[j].enabledMeasureIndex.length; k++)
					expression[expression.length] = ctrl.selectedIndex + controls[j].enabledMeasureIndex[k];

				eval('isEnabled=(' + expression.join(' && ') + ')');

				if (isEnabled) {
					if ((((controls[j].type == 'ddl') && (currentCtrl.selectedIndex == 0)) || (currentCtrl.value == '')) && !ctrl.disabled) InputFieldManager.mark(currentCtrl);
					else InputFieldManager.unmark(currentCtrl);
					currentCtrl.disabled = !enabledAccess;
					if ((controls[j].type == 'ddl') && (ctrl.selectedIndex != 6) && (currentCtrl.selectedIndex == 3)) currentCtrl.selectedIndex = 0;
				} else {
					if (controls[j].type == 'ddl') currentCtrl.selectedIndex = (ctrl.selectedIndex == 6) ? 3 : 0;
					else currentCtrl.value = '';

					InputFieldManager.unmark(currentCtrl);
					currentCtrl.disabled = true;
				}

				if (controls[j].name == 'PrescriptiveIncentive') {
					var ie7 = (document.all && !window.opera && window.XMLHttpRequest) ? true : false;
					var show_value = (ie7) ? 'inline' : 'table-row';
					Utils.setStyle($gE('prescriptive' + index), { 'display': (isEnabled) ? show_value : 'none' });

					var color, text;
					switch (ctrl.value) {
						case '17':
							color = '#f0d6d6';
							text = 'controls';
							break;
						case '18':
							color = '#d6e4f5';
							text = 'retrofit';
							break;
						default:
							color = '#fff';
							text = '';
							break;
					}

					Utils.setStyle($gE('td_ProposedPeakDemand' + index).firstChild, { 'backgroundColor': color });
					Utils.setStyle($gE('td_ProposedAnnualConsumption' + index).firstChild, { 'backgroundColor': color });
					Utils.setStyle($gE('td_PrescriptiveIncentive' + index).firstChild, { 'backgroundColor': color });
					$gE('prescriptive_type' + index).innerHTML = text;

				}
			}

			InputFieldManager.updateMVProcedure(prefix, index);
			InputFieldManager.updateAllTotal(prefix);
			if (ctrl.selectedIndex == 0) InputFieldManager.updateAllEstimated(prefix);
			reply = true;
		}

		if ((id.indexOf('ddl_Measures_IncentiveMetric') != -1)) {
			prefix = id.substring(0, id.indexOf('ddl_Measures_IncentiveMetric'));
			index = id.substring(id.indexOf('ddl_Measures_IncentiveMetric') + 'ddl_Measures_IncentiveMetric'.length);
			ctrlType = $gE(prefix + 'ddl_Measures_Type' + index);

			if ((ctrlType.selectedIndex != 6) && (ctrl.selectedIndex == 3)) {
				alert(ctrl.options[ctrl.selectedIndex].text + ' is invalid incentive metric for ' + ctrlType.options[ctrlType.selectedIndex].text);
				ctrl.selectedIndex = 0;
			} else {
				if ((ctrl.selectedIndex == 0) && (ctrlType.selectedIndex > 0)) InputFieldManager.mark(ctrl);
				else InputFieldManager.unmark(ctrl);

				InputFieldManager.updateAllTotal(prefix);
				InputFieldManager.updateMVProcedure(prefix, index);
			}

			reply = true;
		}

		// Calculate the estimated and total values
		for (var j = 0; j < controls.length; j++) {
			controlID = controls[j].type + '_Measures_' + controls[j].cast + controls[j].name;
			if ((controls[j].autoCalculating) && (id.indexOf(controlID) != -1)) {
				prefix = id.substring(0, id.indexOf(controlID));
				index = id.substring(id.indexOf(controlID) + controlID.length);

				if (controls[j].calculateEstimated) InputFieldManager.updateEstimated(prefix, controls[j].name, index, (InputFieldManager.inited || (index == 9)));
				if (InputFieldManager.inited || (index == 9)) InputFieldManager.updateTotal(prefix, controls[j].cast + controls[j].name, controls[j].enabledMetric);
			}
		}

		return reply;
	},

	updateAllEstimated: function(prefix) {
		for (var i = 0; i < parseInt($gE(prefix + 'hdn_nRows').value); i++) {
			InputFieldManager.updateEstimated(prefix, 'PeakDemand', i, (i == 9));
			InputFieldManager.updateEstimated(prefix, 'AnnualConsumption', i, (i == 9));
		}
	},

	updateEstimated: function(prefix, name, index, updateTotal) {
		var Existing = parseInt($gE(prefix + 'txt_Measures_Existing' + name + index).value);
		var Proposed = parseInt($gE(prefix + 'txt_Measures_Proposed' + name + index).value);
		if (updateTotal == null) updateTotal = true;

		$gE(prefix + 'txt_Measures_Estimated' + name + index).value = ((!isNaN(Existing) && !isNaN(Proposed)) && (Existing >= Proposed)) ? Existing - Proposed : '';

		this.updateMVProcedure(prefix, index);
		//if (updateTotal) 
		this.updateTotal(prefix, 'Estimated' + name);
	},

	updateMVProcedure: function(prefix, index) {
		var ctrlMVprocedure = $gE(prefix + 'txt_Measures_MVProcedure' + index);
		if (InputFieldManager.inited && ctrlMVprocedure.value) {
			ctrlMVprocedure.value = 'modified';
		} else return true;
		/*
		var MVProcedureCodes = {
			'Building Automation Systems (BAS)': { 'basic': 'BAS-B', 'enhanced': 'BAS-E' },
			'Building Envelope': { 'basic': 'BE-B', 'enhanced': 'BE-E' },
			'Chiller Replacement': { 'basic': '', 'enhanced': 'CR-E' },
			'Deep Lake Water Cooling': { 'basic': '', 'enhanced': 'DLWC-E' },
			'Equipment Replacement': { 'basic': 'ER-B', 'enhanced': 'ER-E' },
			'Fuel Substitution': { 'basic': 'FS-B', 'enhanced': 'FS-E' },
			'Ground Source Heat Pumps': { 'basic': '', 'enhanced': 'GSHP-E' },
			'HVAC Re-Design': { 'basic': '', 'enhanced': 'HVAC-E' },
			'Lighting Controls': { 'basic': 'LC-B', 'enhanced': 'LC-E' },
			'Lighting Retrofit': { 'basic': 'LR-B', 'enhanced': 'LR-E' },
			'Other Custom Measures': { 'basic': '', 'enhanced': 'OCM-E' },
			'Sub Metering': { 'basic': 'SM-B', 'enhanced': 'SM-E' },
			'Variable Speed Drives (VSDs)': { 'basic': 'VSD-B', 'enhanced': 'VSD-E' },
			'Lighting Controls - Prescriptive': { 'basic': 'portf', 'enhanced': 'portf' },
			'Lighting Retrofit - Prescriptive': { 'basic': 'portf', 'enhanced': 'portf' }
		};

		var ctrlMeasureType = $gE(prefix + 'ddl_Measures_Type' + index);
		var ctrlMetric = $gE(prefix + 'ddl_Measures_IncentiveMetric' + index);
		var ctrlEstimatedPeakDemand = $gE(prefix + 'txt_Measures_EstimatedPeakDemand' + index);
		var ctrlEstimatedAnnualConsumption = $gE(prefix + 'txt_Measures_EstimatedAnnualConsumption' + index);

		if (ctrlMetric.selectedIndex > 0) {
			var type = ctrlMeasureType.options[ctrlMeasureType.selectedIndex].text;
			var metric = ctrlMetric.options[ctrlMetric.selectedIndex].value;
			var hasMVProcedure = (typeof (MVProcedureCodes[type]) != 'undefined');

			if (hasMVProcedure && (ctrlEstimatedPeakDemand.value != "") && (ctrlEstimatedAnnualConsumption.value != "")) {
				var amount = (metric == "kW") ? (parseInt(ctrlEstimatedPeakDemand.value) * 800) : (parseInt(ctrlEstimatedAnnualConsumption.value) * 0.10);
				var code = ((amount < 20000) && (MVProcedureCodes[type]['basic'] != '')) ? 'basic' : 'enhanced';
			} else code = 'enhanced';

			$gE(prefix + 'txt_Measures_MVProcedure' + index).value = (hasMVProcedure) ? MVProcedureCodes[type][code] : '';
		}
		*/
	},

	updateAllTotal: function(prefix) {
		this.updateTotal(prefix, 'ExistingPeakDemand', 'kW');
		this.updateTotal(prefix, 'ExistingAnnualConsumption', 'kWh');
		this.updateTotal(prefix, 'ProposedPeakDemand', 'kW');
		this.updateTotal(prefix, 'ProposedAnnualConsumption', 'kWh');
		this.updateTotal(prefix, 'TonsOfCooling', 'Ton');
	},

	updateTotal: function(prefix, name, eligibleMetric) {
		var sum = 0;
		var eligibleSum = 0;
		var value;
		var ddlMetric;

		for (var i = 0; i < parseInt($gE(prefix + 'hdn_nRows').value); i++) {
			ddlMetric = $gE(prefix + 'ddl_Measures_IncentiveMetric' + i);
			value = parseInt($gE(prefix + 'txt_Measures_' + name + i).value);
			if (!isNaN(value)) {
				sum += value;
				if ((ddlMetric.selectedIndex > 0) && (eligibleMetric != null) && (ddlMetric.options[ddlMetric.selectedIndex].value == eligibleMetric)) eligibleSum += value;
			}
		}

		$gE(prefix + 'txt_Measures_Total' + name).value = sum;
		if (eligibleMetric != null) $gE(prefix + 'txt_Measures_TotalEligible' + name).value = eligibleSum;

		//TotalEligibleEstimatedPeakDemand
		value = parseInt($gE(prefix + 'txt_Measures_TotalEligibleExistingPeakDemand').value) - parseInt($gE(prefix + 'txt_Measures_TotalEligibleProposedPeakDemand').value);
		$gE(prefix + 'txt_Measures_TotalEligibleEstimatedPeakDemand').value = (value >= 0) ? value : '';

		//TotalEligibleEstimatedAnnualConsumption
		value = parseInt($gE(prefix + 'txt_Measures_TotalEligibleExistingAnnualConsumption').value) - parseInt($gE(prefix + 'txt_Measures_TotalEligibleProposedAnnualConsumption').value);
		$gE(prefix + 'txt_Measures_TotalEligibleEstimatedAnnualConsumption').value = (value >= 0) ? value : '';
	},

	validateIncentiveMetric: function(source, arguments) {
		var id = source.getAttribute('id');
		var prefix = id.substring(0, id.indexOf('ctv_'));
		var idx = id.substring(id.indexOf('ctv_') + 28);

		ctrl = $gE(prefix + 'ddl_Measures_Type' + idx);
		if (ctrl && (ctrl.options[ctrl.selectedIndex].value != -1)) {
			ctrl = $gE(prefix + 'ddl_Measures_IncentiveMetric' + idx);
			if (ctrl && (ctrl.options[ctrl.selectedIndex].value == -1)) {
				arguments.IsValid = false;
				return;
			}
		}
		arguments.IsValid = true;
		return;
	},

	validateMeasureRow: function(source, arguments) {
		var id = source.getAttribute('id');
		var prefix = id.substring(0, id.indexOf('ctv_'));
		var idx = id.substring(id.indexOf('ctv_') + 16);

		ctrl = $gE(prefix + 'ddl_Measures_Type' + idx);
		if (ctrl && (ctrl.options[ctrl.selectedIndex].value != -1)) {
			var Conditions = ['ExistingPeakDemand', 'ExistingAnnualConsumption', 'ProposedPeakDemand', 'ProposedAnnualConsumption', 'TonsOfCooling'];
			var ctrl;
			var sum = 0;
			for (var i = 0; i < Conditions.length; i++) {
				ctrl = $gE(prefix + 'txt_Measures_' + Conditions[i] + idx);
				sum += parseInt((ctrl && ctrl.value) ? ctrl.value : 0);
			}
			arguments.IsValid = (sum > 0);
			return;
		}
		arguments.IsValid = true;
		return;
	},

	manageSummary: function(ctrl) {
		var id = ctrl.getAttribute('id');
		if (!id) return false;
		var prefix = id.substring(0, id.indexOf('txt_'));
		if (!prefix) return false;
		var reply = false;
		var ctrlEligibleCosts = ['GoGreenAssessmentTool', 'EnergyAudits', 'Engineering', 'Equipment', 'InstallationLabour', 'Commissioning', 'MeterRentalOrPurchase', 'PurchasedMeteringServices'];
		var TotalEligibleCosts = 0, ctrlCost, needMark = true;

		for (var i = 0; i < ctrlEligibleCosts.length; i++) {
			ctrlCost = $gE(prefix + 'txt_EligibleCosts_' + ctrlEligibleCosts[i]);
			if (ctrlCost.value != '') {
				TotalEligibleCosts += parseInt((ctrlCost.value.indexOf('$') == 0) ? ctrlCost.value.substring(1) : ctrlCost.value);
				needMark = false;
				if (ctrlCost.value.indexOf('$') == -1) ctrlCost.value = '$' + ctrlCost.value;
			}
		}

		for (var i = 0; i < ctrlEligibleCosts.length; i++) {
			if (needMark) this.mark($gE(prefix + 'txt_EligibleCosts_' + ctrlEligibleCosts[i]));
			else this.unmark($gE(prefix + 'txt_EligibleCosts_' + ctrlEligibleCosts[i]));
		}

		$gE(prefix + 'txt_EligibleCosts_TotalEligibleCosts').value = "$" + TotalEligibleCosts;

		var TotalProjectEstimatedIncentive = parseInt($gE(prefix + 'txt_EstimatedIncentive_TotalProjectEstimatedIncentive').value.substring(1));
		var TotalEligibleTonsOfCooling = parseInt($gE(prefix + 'txt_EstimatedIncentive_TotalEligibleTonsOfCooling').value.substring(1));
		var EligibleIncentive = Math.min((TotalEligibleCosts < TotalProjectEstimatedIncentive) ? TotalEligibleCosts : TotalProjectEstimatedIncentive, (TotalEligibleCosts * 0.4)) + TotalEligibleTonsOfCooling;

		$gE(prefix + 'txt_EstimatedIncentive_EligibleIncentive').value = "$" + EligibleIncentive;

		reply = true;

		return reply;
	},

	getTypeOfControl: function(ctrl) {
		return (ctrl.getAttribute('type')) ? ctrl.getAttribute('type') : ctrl.tagName.toLowerCase()
	},

	getFirstClassName: function(ctrl) {
		var className = this.getClassName(ctrl);
		if (className && className.indexOf(' ') != -1) className = className.substring(0, className.indexOf(' '));
		return className;
	},

	getClassName: function(ctrl) {
		var className = (ctrl.className) ? ctrl.className : ctrl.getAttribute('class');
		return (className) ? className : '';
	},

	copyInputDatas: function(arFrom, arTo) {
		var objFrom, objTo, typFrom, typTo;
		if ((arFrom instanceof Array) && (arTo instanceof Array) && (arFrom.length == arTo.length)) {
			for (var i = 0; i < arFrom.length; i++) {
				objFrom = $gE(arFrom[i]); objTo = $gE(arTo[i]);
				typFrom = this.getTypeOfControl(objFrom);
				typTo = this.getTypeOfControl(objTo);

				if (typFrom == typTo) {
					switch (typFrom) {
						case 'text':
							objTo.value = objFrom.value;
							break;

						case 'select':
						case 'select-one':
							for (var j = 0; j < objFrom.options.length; j++) objTo.options[j].selected = objFrom.options[j].selected;
							break;
					}
				}
			}
		}
	},

	getInputs: function() {
		var fields = new Array;
		var input = $T('input');
		var select = $T('select');
		var textarea = $T('textarea');
		var chkbox = $T('checkbox');
		var radio = $T('radio');

		for (var i = 0; i < input.length; i++) fields[fields.length] = input[i];
		for (var i = 0; i < select.length; i++) fields[fields.length] = select[i];
		for (var i = 0; i < textarea.length; i++) fields[fields.length] = textarea[i];
		for (var i = 0; i < chkbox.length; i++) fields[fields.length] = chkbox[i];
		for (var i = 0; i < radio.length; i++) fields[fields.length] = radio[i];

		return fields;
	}
}
Utils.addInit('InputFieldManager.init');