﻿Type.registerNamespace('OfficialCommunity.Common.WebControls.AddressForm');
Type.registerNamespace('OfficialCommunity.Common.WebControls.AddressForm');

OfficialCommunity.Common.WebControls.AddressForm.CountryRegionBehavior = function(element) 
{

OfficialCommunity.Common.WebControls.AddressForm.CountryRegionBehavior.initializeBase(this, [element]);

    this._CountrySelectorIdValue = null;
    this._RegionLabelIdValue = null;
    this._PostalCodeIdValue = null;
    this._PostalCodeValidatorIdValue = null;
    this._PostalCodeLengthValidatorIdValue = null;
    this._selectedRegion = null;
    this._RegionValidatorIdValue = null;
    this._UpdatePanelIdValue = null;
    this._HasPostalCode = false;
    this._RegionDivIdValue = null;
    this._PostalCodeDivIdValue = null;
    this._RegionNameInputIdValue = null;
    this._RegionCodeInputIdValue = null;
    this._HasPostalCodeInputIdValue = null;
    this._ModeValue = "Full";
}

OfficialCommunity.Common.WebControls.AddressForm.CountryRegionBehavior.prototype =
{

    context: null,
    initialize: function() {
        OfficialCommunity.Common.WebControls.AddressForm.CountryRegionBehavior.callBaseMethod(this, 'initialize');
        $addHandler($get(this._CountrySelectorIdValue), 'change', Function.createDelegate(this, this._onchange));
        $addHandler(this.get_element(), 'change', Function.createDelegate(this, this._onRegionChange));

        this.hideElement($get(this._RegionDivIdValue));

        var countrySelect = $get(this._CountrySelectorIdValue);
        if (countrySelect.selectedIndex > 0) {

            this.fireChangeEvent(countrySelect);

        }

        this._hasPostalCode = $get(this._HasPostalCodeInputIdValue).value;
    },

    dispose: function() {
        OfficialCommunity.Common.WebControls.AddressForm.CountryRegionBehavior.callBaseMethod(this, 'dispose');
    },

    _onchange: function() {
        var sb = $get(this._CountrySelectorIdValue);
        this.callWebService(sb.options[sb.selectedIndex].value);

        if (this._UpdatePanelIdValue != null && this._UpdatePanelIdValue != "") {
            var prm = Sys.WebForms.PageRequestManager.getInstance();
            prm._doPostBack(this._UpdatePanelIdValue, '');
        }
    },

    _onRegionChange: function() {
        var e = this.get_element();
        var regionValue = (e.selectedIndex > -1 && e.options[e.selectedIndex].value != "") ? e.options[e.selectedIndex].value : "";
        var regionText = (e.selectedIndex > -1 && e.options[e.selectedIndex].text != "") ? e.options[e.selectedIndex].text : "";
        this.set_SelectedRegion(regionValue, regionText);
        var validator = null;

        validator = this.getVAMControl(this._RegionValidatorIdValue);

        if (validator != null) {
            if (e.selectedIndex > 0) {
                validator.Enabled = false;
                this.hideElement($get(this._RegionValidatorIdValue));
                validator.CondResult = 1;
            }
            else if (e.options.length > 1) {
                validator.Enabled = true;
                this.showElement($get(this._RegionValidatorIdValue));
            }
        }
    },

    callWebService: function(country) {
        OfficialCommunity.Common.WebControls.AddressForm.CountryRegionBehavior.context = this;
        var webRequest = Sys.Net.WebServiceProxy.invoke(
        "~/OfficialCommunity.Common.WebControls.AddressForm.LocationService.asmx" //path
        , "GetRegions" //methodName
        , false //useHttpGet
        , { "countryCode": country} //parameters 
        , Function.createDelegate(this, this.onRegionReturn)
        , this.failedCallback
        , "User Context"); //userContext 
    },

    onRegionReturn: function(result, eventArgs) {
        var postalCodeLengthValidator = null;
        var postalCodeExpression = null;


        if (result.Regions.length > 0) {
            var regionValue = $get(this._RegionCodeInputIdValue).value;
            var countrySelector = $get(this._CountrySelectorIdValue);
            var selector = this.get_element();
            selector.options.length = 0;
            selector.options[0] = new Option("Select...", "");
            for (var i = 0; i < result.Regions.length; i++) {
                selector.options[i + 1] = new Option(result.Regions[i], result.RegionIds[i]);
                if ((result.RegionIds[i].toUpperCase() == regionValue.toUpperCase()) || (result.Regions[i].toUpperCase() == regionValue.toUpperCase())) {
                    selector.selectedIndex = i + 1;
                }
            }

            this.set_SelectedRegion(selector.options[selector.selectedIndex].value, selector.options[selector.selectedIndex].text);
            $get(this._RegionLabelIdValue).innerHTML = result.SubdivisionLabel + ":";

            this.showElement($get(this._RegionDivIdValue));

            if (result.HasPostalCode && this._ModeValue == "Full") {
                this._HasPostalCode = true;
                this.showElement($get(this._PostalCodeDivIdValue));
                var selectedCountry = countrySelector.options[countrySelector.selectedIndex].value;
                var postalCodeExpressionValidator = this.getVAMControl(this._PostalCodeValidatorIdValue);
                this.setPostalRegex(selectedCountry, postalCodeExpressionValidator.Val[2]);
            }
            else {
                if (this._ModeValue == "Full") {
                    this._HasPostalCode = false;
                    $get(this._PostalCodeIdValue).value = "";
                    this.hideElement($get(this._PostalCodeDivIdValue));
                }
            }
        }
        else {
            this._HasPostalCode = result.HasPostalCode;
            var regionSelect = this.get_element();
            regionSelect.selectedIndex = 0;
            this.hideElement($get(this._RegionDivIdValue));
            while (regionSelect.options.length > 1) {
                regionSelect.options[regionSelect.options.length - 1] = null;
            }

            if (result.HasPostalCode && this._ModeValue == "Full") {
                var postalCodeExpressionValidator = this.getVAMControl(this._PostalCodeValidatorIdValue);
                this.setPostalRegex(selectedCountry, postalCodeExpressionValidator.Val[2]);
            }
            
            if (this._ModeValue == "Full" && !this._HasPostalCode) {
                $get(this._PostalCodeIdValue).value = "";
                this.hideElement($get(this._PostalCodeDivIdValue));
            }
        }

        this.fireChangeEvent(this.get_element());
    },

    failedCallback: function() { alert("There was a problem with your web request. Please try again in a minute or two."); },

    fireChangeEvent: function(el) {
        // 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);
            el.dispatchEvent(onchangeEvent);
        }
        else if (document.createEventObject) {
            el.fireEvent('onchange');
        }
    },

    hideElement: function(el) {
        if (el) {
            el.style.display = "none";
        }
    },

    showElement: function(el) {
        if (el) {
            el.style.display = "";

        }
    },

    getVAMControl: function(id) {
        if (id != null && id != "" && DES_FindAOById) {
            return DES_FindAOById(id);
        }
        return null;
    },

    setPostalRegex: function(selectedCountry, postalCodeExpression) {
        if (postalCodeExpression) {
            if (!postalCodeExpression.AO) {
                postalCodeExpression = this.getVAMControl(postalCodeExpression.FID);
            } else {
                postalCodeExpression = postalCodeExpression.AO;
            }
            switch (selectedCountry) {
                case "CA":
                    postalCodeExpression.Cond.Expr = "^([A-Za-z]\\d[A-Za-z] \\d[A-Za-z]\\d|[A-Za-z]\\d[A-Za-z]\\d[A-Za-z]\\d)$";
                    break;
                case "US":
                    postalCodeExpression.Cond.Expr = "^(\\d{5}-\\d{4}|\\d{5})$";
                    break;
                default:
                    postalCodeExpression.Cond.Expr = ".*";
                    postalCodeExpression.Enabled = false;
            }
        }
    },

    get_CountrySelectorId: function() {
        return this._CountrySelectorIdValue;
    },

    set_CountrySelectorId: function(value) {
        this._CountrySelectorIdValue = value;
    },

    get_RegionLabelId: function() {
        return this._RegionLabelIdValue;
    },

    set_RegionLabelId: function(value) {
        this._RegionLabelIdValue = value;
    },

    get_PostalCodeId: function() {
        return this._PostalCodeIdValue;
    },

    set_PostalCodeId: function(value) {
        this._PostalCodeIdValue = value;
    },

    get_PostalCodeValidatorId: function() {
        return this._PostalCodeValidatorIdValue;
    },

    set_PostalCodeValidatorId: function(value) {
        this._PostalCodeValidatorIdValue = value;
    },

    get_PostalCodeLengthValidatorId: function() {
        return this._PostalCodeLengthValidatorIdValue;
    },

    set_PostalCodeLengthValidatorId: function(value) {
        this._PostalCodeLengthValidatorIdValue = value;
    },

    get_RegionValidatorId: function() {
        return this._RegionValidatorIdValue;
    },

    set_RegionValidatorId: function(value) {
        this._RegionValidatorIdValue = value;
    },

    get_RegionDivId: function() {
        return this._RegionDivIdValue;
    },

    set_RegionDivId: function(value) {
        this._RegionDivIdValue = value;
    },

    get_PostalCodeDivId: function() {
        return this._PostalCodeDivIdValue;
    },

    set_PostalCodeDivId: function(value) {
        this._PostalCodeDivIdValue = value;
    },

    get_RegionNameInputId: function() {
        return this._RegionNameInputIdValue;
    },

    set_RegionNameInputId: function(value) {
        this._RegionNameInputIdValue = value;
    },

    get_RegionCodeInputId: function() {
        return this._RegionCodeInputIdValue;
    },

    set_RegionCodeInputId: function(value) {
        this._RegionCodeInputIdValue = value;
    },

    get_HasPostalCodeInputId: function() {
        return this._HasPostalCodeInputIdValue;
    },

    set_HasPostalCodeInputId: function(value) {
        this._HasPostalCodeInputIdValue = value;
    },

    get_UpdatePanelId: function() {
        return this._UpdatePanelIdValue;
    },

    set_UpdatePanelId: function(value) {
        this._UpdatePanelIdValue = value;
    },

    get_HasPostalCode: function() {
        return this._HasPostalCode;
    },

    set_HasPostalCode: function(value) {
        this._HasPostalCode = value;
    },

    get_Mode: function() {
        return this._ModeValue;
    },

    set_Mode: function(value) {
        this._ModeValue = value;
    },

    get_SelectedRegion: function() {
        return this._selectedRegion;
    },

    set_SelectedRegion: function(value, text) {
        var regionNameInput = $get(this._RegionNameInputIdValue);
        var regionCodeInput = $get(this._RegionCodeInputIdValue);
        var hasPostalCodeInput = $get(this._HasPostalCodeInputIdValue);

        this._selectedRegion = value;
        regionCodeInput.value = value;
        regionNameInput.value = (text == "Select...") ? "" : text;

        hasPostalCodeInput.value = this._HasPostalCode;
        this.raisePropertyChanged('SelectedRegion');

    }
}

OfficialCommunity.Common.WebControls.AddressForm.CountryRegionBehavior.registerClass('OfficialCommunity.Common.WebControls.AddressForm.CountryRegionBehavior', AjaxControlToolkit.BehaviorBase);

if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();