﻿Type.registerNamespace('RIV.Web');

RIV.Web.RIVInspection = function(oArgs) {
    _service = oArgs.service;
    _root = oArgs.root;
    _ctrlProvince = oArgs.ctrlProvince;
    _ctrlCity = oArgs.ctrlCity;
    _ctrlLocation = oArgs.ctrlLocation;
    _defaultContent = oArgs.defaultContent;
    _defaultContentCity = oArgs.defaultContentCity;
    _defaultContentLoc = oArgs.defaultContentLoc;
    _divId = oArgs.divId;
    _language = oArgs.language;
    _notFoundMsg = oArgs.notFoundMsg;
    _timeoutMsg = oArgs.timeoutMsg;
    _waitMsg = oArgs.waitMsg;
    _provinceDefaultItem = oArgs.provinceDefaultItem;
    _cityDefaultItem = oArgs.cityDefaultItem;
    _locDefaultItem = oArgs.locDefaultItem;
    _ctrlProvince.Inspect = this;
    _ctrlCity.Inspect = this;
    _ctrlLocation.Inspect = this;
    _selectedProvince = '';
    _selectedCity = '';

    _ctrlProvince.onchange = function() {
    
        var selectedIndex = _ctrlProvince.selectedIndex;
        _ctrlLocation.selectedIndex = 0;

        if (selectedIndex == 0) {
            // Nothign has been selected.  Show default content for each item and disable the rest.
            this.Inspect.showDefaultContentLoc();
            this.Inspect.showDefaultContentCity();
            //this.Inspect.showDefaultContent();
            _service.ResetProvince();
            _ctrlCity.disabled = true;
            _ctrlLocation.disabled = true;
        } else {
            // Province selected.            
            var prov = _ctrlProvince[selectedIndex].value;
            this.Inspect.showDefaultContentLoc();
            this.Inspect.showDefaultContentCity();
            //_ctrlCity.selectedIndex = 0;
            _ctrlCity.disabled = false;
            _ctrlLocation.disabled = true;
            _selectedProvince = prov;
            this.Inspect.getCities(_selectedProvince);
        }
    }

    _ctrlCity.onchange = function() {
    
        var selectedIndex = _ctrlCity.selectedIndex;

        if (selectedIndex == 0) {
            _ctrlLocation.selectedIndex = 0;
            _ctrlLocation.disabled = true;
            this.Inspect.showDefaultContentLoc();
            this.Inspect.showDefaultContentCity();
        } else {
            var city = _ctrlCity[selectedIndex].value;
            this.Inspect.showDefaultContentLoc();

            if (city == _selectedCity) {
                _ctrlLocation.disabled = false;
            } else {
                _selectedCity = city;
                this.Inspect.getLocations(_selectedProvince, _selectedCity);
            }
        }
    }

    _ctrlLocation.onchange = function() {
    
        var selectedIndex = _ctrlLocation.selectedIndex;

        if (selectedIndex == 0) {
            this.Inspect.showDefaultContentLoc();
        } else {
            var loc = parseInt(_ctrlLocation[selectedIndex].value);
            this.Inspect.showContactInfo(loc);
        }
    }
}

RIV.Web.RIVInspection.prototype = {
    init: function() {
        this.getProvinces();
        this.showDefaultContent();
    },

    getProvinces: function() {
        _ctrlProvince.options[0] = new Option(_waitMsg, '-1');
        _ctrlProvince.disabled = true;
        _ctrlCity.options[0] = new Option(_cityDefaultItem, '0');
        _ctrlCity.disabled = true;
        _ctrlLocation.options[0] = new Option(_locDefaultItem, '0');
        _ctrlLocation.disabled = true;

        _service.GetProvinces(
            function(data) {
                RIV.Web.RIVInspection.locLoadProvinces(data);
            },
            function() { }
        );
    },

    getCities: function(province) {
        _service.GetCities(province,
            function(data) {
                RIV.Web.RIVInspection.locLoadCities(data);
            },
            function() { }
        );
    },

    getLocations: function(province, city) {
        _ctrlLocation.remove(0);
        _ctrlLocation.options[0] = new Option(_waitMsg, '-1');

        _service.GetLocations(
            province,
            city,
            function(data) {
                RIV.Web.RIVInspection.locLoadLocations(data);
            },
            function() { }
        );
    },

    showContactInfo: function(inspectID) {
        _service.GetContactInfo(
            inspectID,
            _language,
            function(data) {
                if (data == '')
                    RIV.Web.RIVInspection.locShowContent(_notFoundMsg);
                else {
                    RIV.Web.RIVInspection.locShowContent(data);
                }
            },
            function() { });
    },

    openLocWindow: function() {
        window.open(_root + _language + '/RIVInspectionLoc.aspx', '', 'toolbar=yes,scrollbars=yes,width=650,height=710,left=75,top=75');
    },

    showDefaultContent: function() {
        _service.ResetProvince();
        RIV.Web.RIVInspection.locShowContent(_defaultContent)
    },

    showDefaultContentCity: function() {
        _service.ResetCity();
        RIV.Web.RIVInspection.locShowContent(_defaultContentCity)
    },

    showDefaultContentLoc: function() {
        _service.ResetLocation();
        RIV.Web.RIVInspection.locShowContent(_defaultContentLoc)
    }
}

RIV.Web.RIVInspection.locLoadProvinces = function(data) {
    _ctrlProvince.disabled = false;
    _ctrlProvince.remove(0);
    _ctrlProvince.options[0] = new Option(_provinceDefaultItem, '0');

    var selectedIndex = 0;

    for (var i = 0; i < data.length; ++i) {
        if (data[i].IsSelected)
            selectedIndex = i + 1;

        _ctrlProvince.options[_ctrlProvince.length] = new Option(data[i].Name, data[i].Value);
    }

    if (selectedIndex > 0) {
        _selectedProvince = _ctrlProvince[selectedIndex].value;
        _ctrlProvince.Inspect.getCities(_selectedProvince);
    }

    _ctrlProvince.selectedIndex = selectedIndex;
}

RIV.Web.RIVInspection.locLoadCities = function(data) {
    _ctrlCity.disabled = false;
    _ctrlCity.length = 0;

    // add default
    var selectedIndex = 0;
    _ctrlCity.options.add(new Option(_cityDefaultItem, '0'));

    for (var i = 0; i < data.length; ++i) {

        if (data[i].IsSelected)
            selectedIndex = i + 1;
            
        _ctrlCity.options.add(new Option(data[i].Name, data[i].Value));
    }
    
    if (selectedIndex > 0) {
        _selectedCity = _ctrlCity[selectedIndex].value;
        _ctrlCity.Inspect.getLocations(_selectedProvince, _selectedCity);
    }
    
    _ctrlCity.selectedIndex = selectedIndex;
}

RIV.Web.RIVInspection.locLoadLocations = function(data) {
    _ctrlLocation.disabled = false;
    _ctrlLocation.remove(0);
    _ctrlLocation.options[0] = new Option(_locDefaultItem, '0');

    var selectedIndex = 0;

    for (var i = 0; i < data.length; ++i) {
        if (data[i].IsSelected)
            selectedIndex = i + 1;

        _ctrlLocation.options[_ctrlLocation.length] = new Option(data[i].Name, data[i].Value);
    }

    if (selectedIndex > 0) {
        var loc = parseInt(_ctrlLocation[selectedIndex].value);
        _ctrlLocation.Inspect.showContactInfo(loc);
    }

    _ctrlLocation.selectedIndex = selectedIndex;
}

RIV.Web.RIVInspection.locShowContent = function(html) {
    if (!(_divId && html))
        return;

    var div = $get(_divId);

    if (div != null) {
        div.innerHTML = html;
    }
}

RIV.Web.RIVInspection.registerClass('RIV.Web.RIVInspection');

if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();