﻿//==========================================================================
//      Operating Cost Estimate
//      Copyright (c) Wrightsoft Corporation 2006.  All Rights Reserved.
//      $Id: InvestmentBehavior.js 89 2007-01-19 10:13:41Z liup $
//==========================================================================

Type.registerNamespace('Wrightsoft.OCE.Web.AjaxControls');

// Create an alias for the namespace to save some chars each time it's used since
// this is a very long script and will take awhile to download
var $OCE = Wrightsoft.OCE.Web.AjaxControls;

$OCE.InvestmentBehavior = function(element) {
    $OCE.InvestmentBehavior.initializeBase(this, [element]);

    //
    // Variables
    //
    this._promptText = '--- Choose ---';
    this._loadingText = '--- Loading ---';

    // Custom properties
    this._ManF = null;
    this._InvestmentID = null;
    this._OutdoorUnitID = null;
    this._IndoorUnitID = null;
    this._CoolModelID = null;
    this._CoolModelDescID = null;
    this._HeatModelID = null;
    this._HeatModelDescID = null;
    this._CapBalID = null;
    this._EcoBalID = null;
    this._CoolEffID = null;
    this._HeatEffID = null;
    this._CoolCostID = null;
    this._HeatCostID = null;
    this._TotalElecCostID = null;
    this._TotalFuelCostID = null;
    this._TotalCostID = null;
    this._SavingsID = null;
    this._ProgressIndicatorID = null;
    this._ContinuousFanClgID = null;
    this._ContinuousFanHtgID = null;
    
    // WebServides properties
    this._ServicePath = null;
    this._GetOutdoorMethod = null;
    this._GetIndoorMethod = null;
    this._GetInvestmentMethod = null;
    this._GetAllInvestmentMethod = null;
    
    // Member variables
    this._SystemTypeElement = null;
    this._OutdoorUnitElement = null;
    this._IndoorUnitElement = null;
    this._CoolModelElement = null;
    this._CoolModelDescElement = null;
    this._HeatModelElement = null;
    this._HeatModelDescElement = null;
    this._CapBalElement = null;
    this._EcoBalElement = null;
    this._CoolEffElement = null;
    this._HeatEffElement = null;
    this._CoolCostElement = null;
    this._HeatCostElement = null;
    this._TotalElecCostElement = null;
    this._TotalFuelCostElement = null;
    this._TotalCostElement = null;
    this._SavingsElement = null;
    this._ProgressIndicatorElement = null;
    this._ContinuousFanClgElement = null;
    this._ContinuousFanHtgElement = null;
    
    this._supportOutdoorUnit = false;
    this._supportIndoorUnit = false;
    
    this._selectedSystemType = null;
    this._selectedOutdoorUnit = null;
    this._selectedIndoorUnit = null;
    this._investment = null;
    
    // Event Handler
    this._SystemTypeChangeHandler = null;
    this._OutdoorChangeHandler = null;
    this._IndoorChangeHandler = null;
    this._ContinuousFanChangeHandler = null;
}

$OCE.InvestmentBehavior.prototype = {
    //
    // Overrides
    //
    initialize : function() {
        $OCE.InvestmentBehavior.callBaseMethod(this, 'initialize');
        Manager.registerInvestment(this);

        // Validate element
        this._SystemTypeElement = this.get_element();
        Sys.Debug.assert(this._SystemTypeElement != null, "Failed to find SystemType element");
        this._OutdoorUnitElement = $get(this._OutdoorUnitID);
        Sys.Debug.assert(this._OutdoorUnitElement != null, "Failed to find OutdoorUnit element'" + this._OutdoorUnitID + "'");
        this._IndoorUnitElement = $get(this._IndoorUnitID);
        Sys.Debug.assert(this._IndoorUnitElement != null, "Failed to find IndoorUnit element'" + this._IndoorUnitID + "'");
        this._CoolModelElement = $get(this._CoolModelID);
        Sys.Debug.assert(this._CoolModelElement != null, "Failed to find CoolModel element'" + this._CoolModelID + "'");
        this._CoolModelDescElement = $get(this._CoolModelDescID);
        Sys.Debug.assert(this._CoolModelDescElement != null, "Failed to find CoolModelDesc element'" + this._CoolModelDescID + "'");
        this._HeatModelElement = $get(this._HeatModelID);
        Sys.Debug.assert(this._HeatModelElement != null, "Failed to find HeatModel element'" + this._HeatModelID + "'");
        this._HeatModelDescElement = $get(this._HeatModelDescID);
        Sys.Debug.assert(this._HeatModelDescElement != null, "Failed to find HeatModelDesc element'" + this._HeatModelDescID + "'");
        this._CapBalElement = $get(this._CapBalID);
        Sys.Debug.assert(this._CapBalElement != null, "Failed to find CapBal element'" + this._CapBalID + "'");
        this._EcoBalElement = $get(this._EcoBalID);
        Sys.Debug.assert(this._EcoBalElement != null, "Failed to find EcoBal element'" + this._EcoBalID + "'");
        this._CoolEffElement = $get(this._CoolEffID);
        Sys.Debug.assert(this._CoolEffElement != null, "Failed to find CoolEff element'" + this._CoolEffID + "'");
        this._HeatEffElement = $get(this._HeatEffID);
        Sys.Debug.assert(this._HeatEffElement != null, "Failed to find HeatEff element'" + this._HeatEffID + "'");
        this._CoolCostElement = $get(this._CoolCostID);
        Sys.Debug.assert(this._CoolCostElement != null, "Failed to find CoolCost element'" + this._CoolCostID + "'");
        this._HeatCostElement = $get(this._HeatCostID);
        Sys.Debug.assert(this._HeatCostElement != null, "Failed to find HeatCost element'" + this._HeatCostID + "'");
        this._TotalElecCostElement = $get(this._TotalElecCostID);
        Sys.Debug.assert(this._TotalElecCostElement != null, "Failed to find TotalElecCost element'" + this._TotalElecCostID + "'");
        this._TotalFuelCostElement = $get(this._TotalFuelCostID);
        Sys.Debug.assert(this._TotalFuelCostElement != null, "Failed to find TotalFuelCost element'" + this._TotalFuelCostID + "'");
        this._TotalCostElement = $get(this._TotalCostID);
        Sys.Debug.assert(this._TotalCostElement != null, "Failed to find TotalCost element'" + this._TotalCostID + "'");
        this._SavingsElement = $get(this._SavingsID);
        Sys.Debug.assert(this._SavingsElement != null, "Failed to find TotalCost element'" + this._SavingsID + "'");
        this._ProgressIndicatorElement = $get(this._ProgressIndicatorID);
        Sys.Debug.assert(this._ProgressIndicatorElement != null, "Failed to find Progress Indicator element'" + this._ProgressIndicatorID + "'");
        this._ContinuousFanClgElement = $get(this._ContinuousFanClgID);
        Sys.Debug.assert(this._ContinuousFanClgElement != null, "Failed to find Continuous Fan Clg element'" + this._ContinuousFanClgID + "'");
        this._ContinuousFanHtgElement = $get(this._ContinuousFanHtgID);
        Sys.Debug.assert(this._ContinuousFanHtgElement != null, "Failed to find Continuous Fan Htg element'" + this._ContinuousFanHtgID + "'");
        
        // Attach change handler to SystemType
        this._SystemTypeChangeHandler = Function.createDelegate(this, this._onSystemTypeChange);
        $addHandler(this._SystemTypeElement, "change", this._SystemTypeChangeHandler);
        
        // Attach change handler to OutdoorUnit
        this._OutdoorChangeHandler = Function.createDelegate(this, this._onOutdoorUnitChange);
        $addHandler(this._OutdoorUnitElement, "change", this._OutdoorChangeHandler);
        
        // Attach change handler to IndoorUnit
        this._IndoorChangeHandler = Function.createDelegate(this, this._onIndoorUnitChange);
        $addHandler(this._IndoorUnitElement, "change", this._IndoorChangeHandler);
        
        // Attach change handler to ContinuousFanClg and ContinuousFanHtg
        this._ContinuousFanChangeHandler = Function.createDelegate(this, this._onContinuousFanChange);
        $addHandler(this._ContinuousFanClgElement, "click", this._ContinuousFanChangeHandler);
        $addHandler(this._ContinuousFanHtgElement, "click", this._ContinuousFanChangeHandler);
        
        this._setSystemTypeOptions();
    },

    dispose : function() {
        $OCE.InvestmentBehavior.callBaseMethod(this, 'dispose');

        // Detach change handler for SystemType
        if (this._SystemTypeChangeHandler) {
            $removeHandler(this._SystemTypeElement, "change", this._SystemTypeChangeHandler);
            this._SystemTypeChangeHandler = null;
        }

        // Detach change handler for OutdoorUnit
        if (this._OutdoorChangeHandler) {
            $removeHandler(this._OutdoorUnitElement, "change", this._OutdoorChangeHandler);
            this._OutdoorChangeHandler = null;
        }

        // Detach change handler for IndoorUnit
        if (this._IndoorChangeHandler) {
            $removeHandler(this._IndoorUnitElement, "change", this._IndoorChangeHandler);
            this._IndoorChangeHandler = null;
        }

        // Detach change handler for ContinuousFanClg and ContinuousFanHtg
        if (this._ContinuousFanChangeHandler) {
            $removeHandler(this._ContinuousFanClgElement, "click", this._ContinuousFanChangeHandler);
            $removeHandler(this._ContinuousFanHtgElement, "click", this._ContinuousFanChangeHandler);
            this._ContinuousFanChangeHandler = null;
        }
    },

    //
    // Custom methods
    //
    
    _setIndicatorState : function(state) {
        if ($OCE.IndicatorTypes.Error == state) {
            this._ProgressIndicatorElement.style.background = "red none repeat scroll 0%";
        } else if ($OCE.IndicatorTypes.Busy == state) {
            this._ProgressIndicatorElement.style.background = "yellow none repeat scroll 0%";
        } else if ($OCE.IndicatorTypes.Ready == state) {
            this._ProgressIndicatorElement.style.background = "lime none repeat scroll 0%";
        } else {
            this._ProgressIndicatorElement.style.background = "lime none repeat scroll 0%";
        }
    },
    
    _onComplete : function(result, userContext, methodName) {
        // Save parameters to cookie
        Manager.saveParameters();

        if (null == result) {
            this._setIndicatorState($OCE.IndicatorTypes.Error);
        } else {
            this._setIndicatorState($OCE.IndicatorTypes.Ready);
        }
        if (methodName == this._GetOutdoorMethod) {
            // Get Outdoor Units
            if (null == result) {
                this._setIndicatorState($OCE.IndicatorTypes.Error);
                this._setOutdoorUnitError("occur error");
            } else {
                this._setOutdoorUnitOptions(result);
            }            
        } else if (methodName == this._GetIndoorMethod) {
            // Get Indoor Units
            if (null == result) {
                this._setIndicatorState($OCE.IndicatorTypes.Error);
                this._setIndoorUnitError("occur error");
            } else {
                this._setIndoorUnitOptions(result);
            }
        } else if (methodName == this._GetInvestmentMethod) {
            // Get Investment
            if (null == result) {
                this._setIndicatorState($OCE.IndicatorTypes.Error);
                this._setInvestmentError("occur error");
            } else {
                this._setInvestmentValues(result);
            }
        }
    },

    _onError : function(webServiceError, userContext, methodName) {
        // Indicate failure
        this._setIndicatorState($OCE.IndicatorTypes.Error);
        if (methodName == this._GetOutdoorMethod) {
            if (webServiceError.get_timedOut()) {
                this._setOutdoorUnitError("timeout");
            } else {
                this._setOutdoorUnitError("occus error");
            }
        } else if (methodName == this._GetIndoorMethod) {
            if (webServiceError.get_timedOut()) {
                this._setIndoorUnitError("timeout");
            } else {
                this._setIndoorUnitError("occus error");
            }
        } else if (methodName == this._GetInvestmentMethod) {
            if (webServiceError.get_timedOut()) {
                this._setInvestmentError("timeout");
            } else {
                this._setInvestmentError("occus error");
            }
        }        
    },
    
    //=== SystemType's stuff ===================================
    _setSystemTypeOptions : function(value) {
        var e = this._SystemTypeElement;
        
        if (null == value)
            value = this.get_SelectedSystemType();

        for (i = 0 ; i < e.options.length ; i++) {
            e.options[i].selected = (value == e.options[i].value);
        }
        
        if (value && (Sys.Browser.agent !== Sys.Browser.Safari)) {
            // Fire the onchange event for the control to notify any listeners of the change
            if (document.createEvent) {
                var onchangeEvent = document.createEvent('HTMLEvents');
                onchangeEvent.initEvent('change', true, false);
                e.dispatchEvent(onchangeEvent);
            } else if( document.createEventObject ) {
                e.fireEvent('onchange');
            }
        }
    },
    
    _onSystemTypeChange : function(evt) {
        // Get the systemtype for call the web service
        var selectedValue = '';
        var clgTyp, hthTyp, htgFuel;
        var e = this._SystemTypeElement;
        
        // Record the selected value in the client state
        if (e && (-1 != e.selectedIndex)){
            selectedValue = e.options[e.selectedIndex].value;
            this.set_SelectedSystemType(e.options[e.selectedIndex].value);
        } else {
            selectedValue = '000';
            this.set_SelectedSystemType('000');
        }
        
        this._checkSystemType(selectedValue);
        
        // Clear all values
        this._setOutdoorUnitOptions(null, true, true);
        this._setIndoorUnitOptions(null, true, true);
        this._setInvestmentValues(null, true, true);
        
        if (selectedValue == '000') return;

        if (this._supportOutdoorUnit == true) {
            this._getOutdoorUnits();
        } else if (this._supportIndoorUnit == true) {
            this._getIndoorUnits();
        }
    },
    
    _checkSystemType : function(systemtype) {
        var clgTyp, hthTyp, htgFuel;
        
        if (null == systemtype) {
            systemtype = this._SystemTypeElement.options[this._SystemTypeElement.selectedIndex].value;
        }

        if (systemtype == '') {
            clgTyp = htgTyp = htgFuel = 0;
        } else {
            clgTyp = systemtype % 10;
            htgTyp = parseInt(systemtype / 10) % 10;
            htgFuel = parseInt(systemtype / 100) % 10;
        }        
        
        this._supportOutdoorUnit = (0 != clgTyp);
        this._supportIndoorUnit = (0 != htgTyp);
        
        if (5 == clgTyp || 6 == clgTyp)
            this._supportIndoorUnit = false;
        
        if (clgTyp==2 || clgTyp==6) {
	        this._CapBalElement.style.display="inline";
	        this._EcoBalElement.style.display="inline";
        } else {
	        this._CapBalElement.style.display="none";
	        this._EcoBalElement.style.display="none";
        }
        
        if (htgTyp==0 && clgTyp==1) {
            this._supportIndoorUnit = true;
        }
        
        if (htgTyp==0) {
            this._ContinuousFanHtgElement.disabled=true;
            this._ContinuousFanHtgElement.checked=false;
        } else {
            this._ContinuousFanHtgElement.disabled=false;
        }
        
        if (this._supportOutdoorUnit) {
            this._OutdoorUnitElement.style.display="inline";
            this._CoolModelElement.style.display="inline";
            this._CoolModelDescElement.style.display="inline";
            this._CoolCostElement.style.display="inline";
            this._CoolEffElement.style.display="inline";
            this._ContinuousFanClgElement.disabled=false;
         } else {
            this._OutdoorUnitElement.style.display="none";
            this._CoolModelElement.style.display="none";
            this._CoolModelDescElement.style.display="none";
            this._CoolCostElement.style.display="none";
            this._CoolEffElement.style.display="none";
            this._ContinuousFanClgElement.disabled=true;
            this._ContinuousFanClgElement.checked=false;
         }
         
        if (this._supportIndoorUnit) {
            this._HeatModelElement.style.display="inline"; 
	        this._HeatModelDescElement.style.display="inline"; 
	        if (htgTyp==0 && clgTyp==1) {
	            this._HeatCostElement.style.display="none";
                this._HeatEffElement.style.display="none";
            } else {
                this._HeatCostElement.style.display="inline";
                this._HeatEffElement.style.display="inline";
            }
        } else {
            this._HeatModelElement.style.display="none"; 
	        this._HeatModelDescElement.style.display="none";
            if (5 == clgTyp || 6 == clgTyp) {
                this._HeatCostElement.style.display="inline";
                this._HeatEffElement.style.display="inline";
            } else {
                this._HeatCostElement.style.display="none";
                this._HeatEffElement.style.display="none";
            }	        
	    }
	    
	    if (!this._supportOutdoorUnit && this._supportIndoorUnit)
	        this._IndoorUnitElement.style.display="inline";
	    else
	        this._IndoorUnitElement.style.display="none";
    },
    //=== SystemType's stuff ===================================


    //=== OutdoorUnit's stuff ===================================
    _getOutdoorUnits : function() {
        // Validate Parameters
        if (!Manager.checkParameters()) {
            this._setIndicatorState($OCE.IndicatorTypes.Error);
            this._setOutdoorUnitError("Parameters error.");
            return;
        }

        // Call the web service
        this._setIndicatorState($OCE.IndicatorTypes.Busy);
        if (this._ServicePath && this._GetOutdoorMethod) {
            Sys.Net.WebServiceProxy.invoke(
                this._ServicePath, this._GetOutdoorMethod, false,
                {invnum:this._InvestmentID,
                 systemtype:this._selectedSystemType,
                 heatload:Manager.get_HeatLoadValue(),
                 coolload:Manager.get_CoolLoadValue(),
                 manf:this._ManF,
                 gastype:Manager.get_GasType()},
                Function.createDelegate(this, this._onComplete),
                Function.createDelegate(this, this._onError));
        }
    },

    _clearOutdoorUnitItems : function() {
        // Clear any items of OutdoorUnit
        while (0 < this._OutdoorUnitElement.options.length) {
            this._OutdoorUnitElement.remove(0);
        }
    },
    
    _setOutdoorUnitError : function(errmsg) {
        if (!this.get_isInitialized()) {
            return;
        }
        
        var e = this._OutdoorUnitElement;
        // Remove existing contents
        this._clearOutdoorUnitItems();
        this._clearIndoorUnitItems();
        this._setInvestmentValues(null, null, true);

        var optionElement = new Option(errmsg, "");
        optionElement.selected = true;
        e.options[e.options.length] = optionElement;
    },
    
    // Set the contents of the OutdoorUnit to the specified list
    _setOutdoorUnitOptions : function(list, inInit, gettingList) {    
        if (!this.get_isInitialized()) {
            return;
        }
        
        var e = this._OutdoorUnitElement;
        // Remove existing contents
        this._clearOutdoorUnitItems();
        
        // Populate prompt text (if available)
        var headerText;
        if (gettingList && this._loadingText) {
            headerText = this._loadingText;
        } else if (this._promptText) {
            headerText = this._promptText;
        }
        if (headerText) {
            var optionElement = new Option(headerText, "");
            e.options[e.options.length] = optionElement;
            //e.disabled = !list || (0 == list.length);
        }

        // Add each item to the DropDownList, selecting the previously selected item
        var matchedSelectedValue = false;

        if (list) {
            for (i = 0 ; i < list.length ; i++) {
                var listItemName = list[i].Value;
                var listItemValue = list[i].Name + '^' + list[i].Value;

                var isSelectedValue = (listItemValue == this.get_SelectedOutdoorUnit());
                var optionElement = new Option(listItemName, listItemValue, false, isSelectedValue);
                matchedSelectedValue |= isSelectedValue;

                e.options[e.options.length] = optionElement;
            }
        }

        // if we didn't match the selected value, and we found a default
        // item, select that one.
        if (matchedSelectedValue) {
            // Call set_SelectedValue to store the text as well
            this.set_SelectedOutdoorUnit(e.options[e.selectedIndex].value);
        } else if (!inInit && !matchedSelectedValue && !gettingList) {
            this.set_SelectedOutdoorUnit('');
        }

        if (list && (Sys.Browser.agent !== Sys.Browser.Safari)) {
            // Fire the onchange event for the control to notify any listeners of the change
            if (document.createEvent) {
                var onchangeEvent = document.createEvent('HTMLEvents');
                onchangeEvent.initEvent('change', true, false);
                e.dispatchEvent(onchangeEvent);
            } else if( document.createEventObject ) {
                e.fireEvent('onchange');
            }
        }
    },

    _onOutdoorUnitChange : function(inInit) {
        // Get the OutdoorUnit and systemtype for call web service and get indoorunits
        var selectedValue = '';
        var e = this._OutdoorUnitElement;
                
        if (e && (-1 != e.selectedIndex)){
            selectedValue = e.options[e.selectedIndex].value;
            this.set_SelectedOutdoorUnit(e.options[e.selectedIndex].value);
        } else {
            selectedValue = '';
            this.set_SelectedOutdoorUnit('');
        }
        
        // Clear Investment's value
        this._setInvestmentValues(null, inInit, true);
        
        if (selectedValue == '') {
            this._IndoorUnitElement.style.display="none";
            return;
        } else if (this._supportIndoorUnit) {
            // Get Indoor Units
            this._IndoorUnitElement.style.display="inline";
            this._setIndoorUnitOptions(null, inInit, true);
            this._getIndoorUnits();
        } else { 
            // the system type is not supports indoor unit
            // so get Investment directly
            this._IndoorUnitElement.style.display="none";
            this._getInvestment();
        }
    },
    //=== OutdoorUnit's stuff ===================================


    //=== IndoorUnit's stuff ===================================
    _getIndoorUnits : function() {
        // Validate Parameters
        if (!Manager.checkParameters()) {
            this._setIndicatorState($OCE.IndicatorTypes.Error);
            this._setIndoorUnitError("Parameters error.");
            return;
        }
        
        // Call the web service
        this._setIndicatorState($OCE.IndicatorTypes.Busy);
        if (this._ServicePath && this._GetIndoorMethod) {
            // Call the helper web service
            Sys.Net.WebServiceProxy.invoke(
                this._ServicePath, this._GetIndoorMethod, false,
                {invnum:this._InvestmentID,
                 systemtype:this._selectedSystemType,
                 outdoorunit:(this._supportOutdoorUnit ? this._selectedOutdoorUnit : ""),
                 heatload:Manager.get_HeatLoadValue(),
                 coolload:Manager.get_CoolLoadValue(),
                 manf:this._ManF,
                 gastype:Manager.get_GasType()},
                Function.createDelegate(this, this._onComplete),
                Function.createDelegate(this, this._onError));
        }
    },
    
    // Clear any items of OutdoorUnit
    _clearIndoorUnitItems : function() {
        while (0 < this._IndoorUnitElement.options.length) {
            this._IndoorUnitElement.remove(0);
        }
    },

    _setIndoorUnitError : function(errmsg) {
        if (!this.get_isInitialized()) {
            return;
        }
        
        var e = this._IndoorUnitElement;
        // Remove existing contents
        this._clearIndoorUnitItems();
        this._setInvestmentValues(null, null, true);

        var optionElement = new Option(errmsg, "");
        optionElement.selected = true;
        e.options[e.options.length] = optionElement;
    },

    // Set the contents of the OutdoorUnit to the specified list
    _setIndoorUnitOptions : function(list, inInit, gettingList) {
        if (!this.get_isInitialized()) {
            return;
        }
        
        var e = this._IndoorUnitElement;
        // Remove existing contents
        this._clearIndoorUnitItems();
        
        // Populate prompt text (if available)
        var headerText;
        if (gettingList && this._loadingText) {
            headerText = this._loadingText;
        } else if (this._promptText) {
            headerText = this._promptText;
        }
        if (headerText) {
            var optionElement = new Option(headerText, "");
            e.options[e.options.length] = optionElement;
            //e.disabled = !list || (0 == list.length);
        }
        
        // Add each item to the DropDownList, selecting the previously selected item
        var matchedSelectedValue = false;

        if (list) {
            for (i = 0 ; i < list.length ; i++) {
                var listItemName = list[i].Value;
                var listItemValue = list[i].Name + '^' + list[i].Value;

                var isSelectedValue = (listItemValue == this.get_SelectedIndoorUnit());
                var optionElement = new Option(listItemName, listItemValue, false, isSelectedValue);
                matchedSelectedValue |= isSelectedValue;

                e.options[e.options.length] = optionElement;
            }
        }

        // if we didn't match the selected value, and we found a default
        // item, select that one.
        if (matchedSelectedValue) {
            this.set_SelectedIndoorUnit(e.options[e.selectedIndex].value);
        } else if (!inInit && !matchedSelectedValue && !gettingList) {
            this.set_SelectedIndoorUnit('');
        }

        if (list && (Sys.Browser.agent !== Sys.Browser.Safari)) {
            // Fire the onchange event for the control to notify any listeners of the change
            if (document.createEvent) {
                var onchangeEvent = document.createEvent('HTMLEvents');
                onchangeEvent.initEvent('change', true, false);
                e.dispatchEvent(onchangeEvent);
            } else if( document.createEventObject ) {
                e.fireEvent('onchange');
            }
        }
    },
    
    _onIndoorUnitChange : function(inInit) {
        // Get the systemtype, OutdoorUnit, IndoorUnit and Parameters
		// for call web service and get Investment
        var selectedValue = '';
        var e = this._IndoorUnitElement;
        
		// Get IndoorUnit
        if (e && (-1 != e.selectedIndex)){
            selectedValue = e.options[e.selectedIndex].value;
            this.set_SelectedIndoorUnit(e.options[e.selectedIndex].value);
        } else {
            selectedValue = '';
            this.set_SelectedIndoorUnit('');
        }

        // Clear all values
        this._setInvestmentValues(null, inInit, true);
        
        if (selectedValue == '' || selectedValue == '000') {
            return;
        }

        // call web services to get investment
        this._getInvestment();
    },
    //=== IndoorUnit's stuff ===================================
    
    //=== Continuous Fan stuff =================================
    _onContinuousFanChange : function() {
        var selectedValue = '';
        var eI = this._IndoorUnitElement;
        var eO = this._OutdoorUnitElement;
        var bCallWebService = true;
        if (this._supportOutdoorUnit) {
            bCallWebService = (bCallWebService && eO && (eO.selectedIndex > 0));
        }
        if (this._supportIndoorUnit) {
            bCallWebService = (bCallWebService && eI && (eI.selectedIndex > 0));
        }

        // call web services to get investment
        if (bCallWebService) {
            this._getInvestment();
        }
    },
    //=== Continuous Fan stuff =================================

    //=== Investment's stuff ===================================
    _getInvestment : function(inInit, para) {
        // Get the SystemType, OutdoorUnit, IndoorUnit and Parameters
		// for call web service and get Investment
		
		// Check SystemType
        if (!this._selectedSystemType || this._selectedSystemType == '' || this._selectedSystemType == '000') {
		    return;
		}
		
        // Validate Parameters
        if (!Manager.checkParameters()) {
            this._setIndicatorState($OCE.IndicatorTypes.Error);
            this._setInvestmentError("Parameters error.");
            return;
        }

        this._setIndicatorState($OCE.IndicatorTypes.Busy);
        if (this._ServicePath && this._GetInvestmentMethod) {
            // Call the helper web service
            Sys.Net.WebServiceProxy.invoke(
                this._ServicePath, this._GetInvestmentMethod, false,
                { invnum:this._InvestmentID,
                  systemtype:this._selectedSystemType,
                  outdoorunit:(this._supportOutdoorUnit ? this._selectedOutdoorUnit : ""),
                  indoorunit:(this._supportIndoorUnit ? this._selectedIndoorUnit : ""),
                  heatload:Manager.get_HeatLoadValue(),
                  coolload:Manager.get_CoolLoadValue(),
                  zipcode:Manager.get_ZipCodeValue(),
                  natgascost:Manager.get_NatCostValue(),
                  lpgascost:Manager.get_LPCostValue(),
                  oilcost:Manager.get_OilCostValue(),
                  sumelecost:Manager.get_SumEleCostValue(),
                  winelecost:Manager.get_WinEleCostValue(),
                  gastype:Manager.get_GasType(),
                  cfcool:this._ContinuousFanClgElement.checked,
                  cfheat:this._ContinuousFanHtgElement.checked},
                Function.createDelegate(this, this._onComplete),
                Function.createDelegate(this, this._onError));
        }
    },
    
    _setInvestmentError : function(errmsg) {
        this._CoolModelElement.value = errmsg;
        this._CoolModelDescElement.value = errmsg;
        this._HeatModelElement.value = errmsg;
        this._HeatModelDescElement.value = errmsg;
        this._CapBalElement.value = errmsg;
        this._EcoBalElement.value = errmsg;
        this._CoolEffElement.value = errmsg;
        this._HeatEffElement.value = errmsg;
        this._CoolCostElement.value = errmsg;
        this._HeatCostElement.value = errmsg;
        this._TotalElecCostElement.value = errmsg;
        this._TotalFuelCostElement.value = errmsg;
        this._TotalCostElement.value = errmsg;
        this._SavingsElement.value = errmsg;
        
        // Annual Savings
        this._setSavings();
    },
    _setInvestmentValues : function(inv, inInit, gettingList) {
        // Set the contents of the Investment
        this._investment = inv;
        
        if (inv) {
            this._CoolModelElement.value = inv.CoolModel;
            this._CoolModelDescElement.value = inv.CoolModelDesc;
            this._HeatModelElement.value = inv.HeatModel;
            this._HeatModelDescElement.value = inv.HeatModelDesc;
            this._CapBalElement.value = inv.CapBal;
            this._EcoBalElement.value = inv.EcoBal;
            this._CoolEffElement.value = inv.CoolEff;
            this._HeatEffElement.value = inv.HeatEff;
            if (0 == inv.CoolCost)
                this._CoolCostElement.value = '';
            else
                this._CoolCostElement.value = Manager.FormatNumber(inv.CoolCost);
            if (0 == inv.HeatCost)
                this._HeatCostElement.value = '';
            else
                this._HeatCostElement.value = Manager.FormatNumber(inv.HeatCost);
            if (0 == inv.TotalElecCost)
                this._TotalElecCostElement.value = '';
            else
                this._TotalElecCostElement.value = Manager.FormatNumber(inv.TotalElecCost);
            if (0 == inv.TotalFuelCost)
                this._TotalFuelCostElement.value = '';
            else
                this._TotalFuelCostElement.value = Manager.FormatNumber(inv.TotalFuelCost);
            if (0 == inv.TotalCost)
                this._TotalCostElement.value = '';
            else
                this._TotalCostElement.value = Manager.FormatNumber(inv.TotalCost);
        } else {
            this._CoolModelElement.value = '';
            this._CoolModelDescElement.value = '';
            this._HeatModelElement.value = '';
            this._HeatModelDescElement.value = '';
            this._CapBalElement.value = '';
            this._EcoBalElement.value = '';
            this._CoolEffElement.value = '';
            this._HeatEffElement.value = '';
            this._CoolCostElement.value = '';
            this._HeatCostElement.value = '';
            this._TotalElecCostElement.value = '';
            this._TotalFuelCostElement.value = '';
            this._TotalCostElement.value = '';
            this._SavingsElement.value = '';
        }
        this._CoolModelElement.title = this._CoolModelElement.value;
        this._CoolModelDescElement.title = this._CoolModelDescElement.value;
        this._HeatModelElement.title = this._HeatModelElement.value;
        this._HeatModelDescElement.title = this._HeatModelDescElement.value;
        this._CapBalElement.title = this._CapBalElement.value;
        this._EcoBalElement.title = this._EcoBalElement.value;

        // Annual Savings
        this._setSavings();
    },
    
    _setSavings : function() {
        var inv0 = $OCE.Manager.getInvestment(0);
        var inv1 = $OCE.Manager.getInvestment(1);
        var inv2 = $OCE.Manager.getInvestment(2);
        var inv3 = $OCE.Manager.getInvestment(3);

        if (inv0 && inv1 && inv2 && inv3) {
            if (inv0.get_TotalCost() == 0) {
                inv1.set_Savings('');
                inv2.set_Savings('');
                inv3.set_Savings('');
            } else {
                inv1.set_Savings(inv0.get_TotalCost() - inv1.get_TotalCost());
                inv2.set_Savings(inv0.get_TotalCost() - inv2.get_TotalCost());
                inv3.set_Savings(inv0.get_TotalCost() - inv3.get_TotalCost());
            }
        }
    },
    //=== Investment's stuff ===================================

    //
    // Property get/set methods
    //
    get_ManF : function() {
       return this._ManF;
    },

    set_ManF : function(value) {
        if (this._ManF != value) {
            this._ManF = value;
            this.raisePropertyChanged('ManF');
        }
    },

    get_InvestmentID : function() {
       return this._InvestmentID;
    },

    set_InvestmentID : function(value) {
        if (this._InvestmentID != value) {
            this._InvestmentID = value;
            this.raisePropertyChanged('InvestmentID');
        }
    },

    get_OutdoorUnitID : function() {
        return this._OutdoorUnitID;
    },

    set_OutdoorUnitID : function(value) {
        if (this._OutdoorUnitID != value) {
            this._OutdoorUnitID = value;
            this.raisePropertyChanged('OutdoorUnitID');
        }
    },

    get_IndoorUnitID : function() {
        return this._IndoorUnitID;
    },

    set_IndoorUnitID : function(value) {
        if (this._IndoorUnitID != value) {
            this._IndoorUnitID = value;
            this.raisePropertyChanged('IndoorUnitID');
        }
    },

    // Investment properties
    get_CoolModelID : function() {
        return this._CoolModelID;
    },
    set_CoolModelID : function(value) {
        if (this._CoolModelID != value) {
            this._CoolModelID = value;
            this.raisePropertyChanged('CoolModelID');
        }
    },
    
    get_CoolModelDescID : function() {
        return this._CoolModelDescID;
    },
    set_CoolModelDescID : function(value) {
        if (this._CoolModelDescID != value) {
            this._CoolModelDescID = value;
            this.raisePropertyChanged('CoolModelDescID');
        }
    },

    get_HeatModelID : function() {
        return this._HeatModelID;
    },
    set_HeatModelID : function(value) {
        if (this._HeatModelID != value) {
            this._HeatModelID = value;
            this.raisePropertyChanged('HeatModelID');
        }
    },

    get_HeatModelDescID : function() {
        return this._HeatModelDescID;
    },
    set_HeatModelDescID : function(value) {
        if (this._HeatModelDescID != value) {
            this._HeatModelDescID = value;
            this.raisePropertyChanged('HeatModelDescID');
        }
    },

    get_CapBalID : function() {
        return this._CapBalID;
    },
    set_CapBalID : function(value) {
        if (this._CapBalID != value) {
            this._CapBalID = value;
            this.raisePropertyChanged('CapBalID');
        }
    },

    get_EcoBalID : function() {
        return this._EcoBalID;
    },
    set_EcoBalID : function(value) {
        if (this._EcoBalID != value) {
            this._EcoBalID = value;
            this.raisePropertyChanged('EcoBalID');
        }
    },

    get_CoolEffID : function() {
        return this._CoolEffID;
    },
    set_CoolEffID : function(value) {
        if (this._CoolEffID != value) {
            this._CoolEffID = value;
            this.raisePropertyChanged('CoolEffID');
        }
    },

    get_HeatEffID : function() {
        return this._HeatEffID;
    },
    set_HeatEffID : function(value) {
        if (this._HeatEffID != value) {
            this._HeatEffID = value;
            this.raisePropertyChanged('HeatEffID');
        }
    },

    get_CoolCostID : function() {
        return this._CoolCostID;
    },
    set_CoolCostID : function(value) {
        if (this._CoolCostID != value) {
            this._CoolCostID = value;
            this.raisePropertyChanged('CoolCostID');
        }
    },

    get_HeatCostID : function() {
        return this._HeatCostID;
    },
    set_HeatCostID : function(value) {
        if (this._HeatCostID != value) {
            this._HeatCostID = value;
            this.raisePropertyChanged('HeatCostID');
        }
    },

    get_TotalElecCostID : function() {
        return this._TotalElecCostID;
    },
    set_TotalElecCostID : function(value) {
        if (this._TotalElecCostID != value) {
            this._TotalElecCostID = value;
            this.raisePropertyChanged('TotalElecCostID');
        }
    },

    get_TotalFuelCostID : function() {
        return this._TotalFuelCostID;
    },
    set_TotalFuelCostID : function(value) {
        if (this._TotalFuelCostID != value) {
            this._TotalFuelCostID = value;
            this.raisePropertyChanged('TotalFuelCostID');
        }
    },

    get_TotalCostID : function() {
        return this._TotalCostID;
    },
    set_TotalCostID : function(value) {
        if (this._TotalCostID != value) {
            this._TotalCostID = value;
            this.raisePropertyChanged('TotalCostID');
        }
    },

    get_SavingsID : function() {
        return this._SavingsID;
    },
    set_SavingsID : function(value) {
        if (this._SavingsID != value) {
            this._SavingsID = value;
            this.raisePropertyChanged('SavingsID');
        }
    },
    
    get_ProgressIndicatorID : function() {
        return this._ProgressIndicatorID;
    },
    set_ProgressIndicatorID : function(value) {
        if (this._ProgressIndicatorID != value) {
            this._ProgressIndicatorID = value;
            this.raisePropertyChanged('ProgressIndicatorID');
        }
    },
    
    get_ContinuousFanClgID : function() {
        return this._ContinuousFanClgID;
    },
    set_ContinuousFanClgID : function(value) {
        if (this._ContinuousFanClgID != value) {
            this._ContinuousFanClgID = value;
            this.raisePropertyChanged('ContinuousFanClgID');
        }
    },
    
    get_ContinuousFanHtgID : function() {
        return this._ContinuousFanHtgID;
    },
    set_ContinuousFanHtgID : function(value) {
        if (this._ContinuousFanHtgID != value) {
            this._ContinuousFanHtgID = value;
            this.raisePropertyChanged('ContinuousFanHtgID');
        }
    },
    
    // WebServides properties
    get_ServicePath : function() {
        return this._ServicePath;
    },
    set_ServicePath : function(value) {
        if (this._ServicePath != value) {
            this._ServicePath = value;
            this.raisePropertyChanged('ServicePath');
        }
    },

    get_GetOutdoorMethod : function() {
        return this._GetOutdoorMethod;
    },
    set_GetOutdoorMethod : function(value) {
        if (this._GetOutdoorMethod != value) {
            this._GetOutdoorMethod = value;
            this.raisePropertyChanged('GetOutdoorMethod');
        }
    },

    get_GetIndoorMethod : function() {
        return this._GetIndoorMethod;
    },
    set_GetIndoorMethod : function(value) {
        if (this._GetIndoorMethod != value) {
            this._GetIndoorMethod = value;
            this.raisePropertyChanged('GetIndoorMethod');
        }
    },
    
    get_GetInvestmentMethod : function() {
        return this._GetInvestmentMethod;
    },
    set_GetInvestmentMethod : function(value) {
        if (this._GetInvestmentMethod != value) {
            this._GetInvestmentMethod = value;
            this.raisePropertyChanged('GetInvestmentMethod');
        }
    },
    
    get_SelectedSystemType : function() {
        var statevalue = this.get_StateValue(0);
        
        if (null == statevalue || '' == statevalue)
            return this._selectedSystemType;
        else
            return statevalue;
    },
    set_SelectedSystemType : function(value) {
        this._selectedSystemType = value;
        
        // Save State
        if ('' == value || '000' == value) {
            this.set_StateValue(0, "");
            this.set_StateValue(1, "");
            this.set_StateValue(2, "");
        } else {
            this.set_StateValue(0, value);
        }
    },
    
    get_SelectedOutdoorUnit : function(refresh) {
        var statevalue = this.get_StateValue(1);

        if (null == statevalue || '' == statevalue)
            return this._selectedOutdoorUnit;
        else
            return statevalue;
    },
    set_SelectedOutdoorUnit : function(value) {
        this._selectedOutdoorUnit = value;

        // Save State
        if ('' == value || '000' == value) {
            this.set_StateValue(1, "");
            this.set_StateValue(2, "");
        } else {
            this.set_StateValue(1, value);
        }
    },
    
    get_SelectedIndoorUnit : function() {
        var statevalue = this.get_StateValue(2);

        if (null == statevalue || '' == statevalue)
            return this._selectedIndoorUnit;
        else
            return statevalue;
    },
    set_SelectedIndoorUnit : function(value) {
        this._selectedIndoorUnit = value;

        // Save State
        if ('' == value || '000' == value) {
            this.set_StateValue(2, "");
        } else {
            this.set_StateValue(2, value);
        }
    },
    
    get_TotalCost : function() {
        if (this._investment)
            return this._investment.TotalCost;
        else
            return 0;
    },
    
    set_Savings : function(val) {
        if (val == '')
            this._SavingsElement.value = val;
        else if (this.get_TotalCost() == 0)
            this._SavingsElement.value = '';
        else
            this._SavingsElement.value = Manager.FormatNumber(val);
        if (val < 0) {
            this._SavingsElement.style.color = 'red';
        } else {
            this._SavingsElement.style.color = 'black';
        }
    },
    
    get_StateValue : function(index) {
        var state = $OCE.InvestmentBehavior.callBaseMethod(this, 'get_ClientState');                

        if (null == state) {
            return "";
        }
        
        var values = state.split(",");
        if (0 > index || index >= values.length) {
            return "";
        } else {
            return values[index];
        }
    },
    set_StateValue : function(index, value) {
        var values = new Array(3);
        values[0] = this.get_StateValue(0);
        values[1] = this.get_StateValue(1);
        values[2] = this.get_StateValue(2);
        
        if (0 > index || index >= values.length) {
            return;
        } else {
            values[index] = value;
        }

        var statevalue = values[0] + "," + values[1] + "," + values[2];
        $OCE.InvestmentBehavior.callBaseMethod(this, 'set_ClientState', [ statevalue ]);
    }
}

$OCE.InvestmentBehavior.registerClass('Wrightsoft.OCE.Web.AjaxControls.InvestmentBehavior', AjaxControlToolkit.BehaviorBase);

$OCE.IndicatorTypes = function() {
    throw Error.invalidOperation();
}
$OCE.IndicatorTypes.prototype = {
    Error :  0x1,
    Busy  :  0x2,
    Ready :  0x3
}
$OCE.IndicatorTypes.registerEnum('Wrightsoft.OCE.Web.AjaxControls.IndicatorTypes', true);
if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();