﻿Type.registerNamespace('RIV.Web');

RIV.Web.USCustoms = function(oArgs) {
    _service = oArgs.service;
    _root = oArgs.root;
    _ctrlState = oArgs.ctrlState;
    _ctrlProvince = oArgs.ctrlProvince;
    _ctrlBorderCross = oArgs.ctrlBorderCross;
    _defaultContent = oArgs.defaultContent;
    _defaultContentBorder = oArgs.defaultContenBorder;
    _divId = oArgs.divId;
    _language = oArgs.language;
    _notFoundMsg = oArgs.notFoundMsg;
    _timeoutMsg = oArgs.timeoutMsg;
    _waitMsg = oArgs.waitMsg;
    _stateDefaultItem = oArgs.stateDefaultItem;
    _provinceDefaultItem = oArgs.provinceDefaultItem;
    _borderDefaultItem = oArgs.borderDefaultItem;
    _ctrlState.USCustoms = this;
    _ctrlProvince.USCustoms = this;
    _ctrlBorderCross.USCustoms = this;

    _ctrlState.onchange = function() {
    debugger
        var selectedIndex = _ctrlState.selectedIndex;

        if (selectedIndex == 0) {
            //this.USCustoms.getProvinces();
            this.USCustoms.getBorderCrossings(true);
            this.USCustoms.showDefaultContent();
        } else {
            _ctrlProvince.selectedIndex = 0;
            var state = _ctrlState[selectedIndex].value;
            //this.USCustoms.getProvincesByState(state);
            this.USCustoms.getBorderCrossingsByState(state);
            this.USCustoms.showDefaultContentBorder();
        }
    }

    _ctrlProvince.onchange = function() {
    debugger
        var selectedIndex = _ctrlProvince.selectedIndex;

        if (selectedIndex == 0) {
            //this.USCustoms.getStates();
            this.USCustoms.getBorderCrossings(true);
            this.USCustoms.showDefaultContent();
        } else {
            _ctrlState.selectedIndex = 0;
            var prov = _ctrlProvince[selectedIndex].value;
            //this.USCustoms.getStatesByProvince(prov);
            this.USCustoms.getBorderCrossingsByProvince(prov);
            this.USCustoms.showDefaultContentBorder();
        }
    }

    _ctrlBorderCross.onchange = function() {
    debugger
        var selectedIndex = _ctrlBorderCross.selectedIndex;

        if (selectedIndex == 0) {
            this.USCustoms.getBorderCrossings(true);
            if (_ctrlState.selectedIndex == 0 && _ctrlProvince.selectedIndex == 0)
                this.USCustoms.showDefaultContent();
            else
                this.USCustoms.showDefaultContentBorder();
        } else {
            var officeID = parseInt(_ctrlBorderCross[selectedIndex].value);
            this.USCustoms.showContactInfo(officeID);
        }
    }
}

RIV.Web.USCustoms.prototype = {
    init: function() {
        this.getStates();
        this.getProvinces();
        this.getBorderCrossings(false);
        this.showDefaultContent();
    },

    getStates: function() {
        _ctrlState.options[0] = new Option(_waitMsg, '-1');
        _ctrlState.disabled = true;
        _service.GetStates(
            function(data) {
                RIV.Web.USCustoms.locLoadStates(data);
            },
            function() { }
        );
    },

    getStatesByProvince: function(province) {
        _ctrlState.options[0] = new Option(_waitMsg, '-1');
        _ctrlState.disabled = true;
        _service.GetStatesByProvince(
            province,
            function(data) {
                RIV.Web.USCustoms.locLoadStates(data);
            },
            function() { }
        );
    },

    getProvinces: function() {
        _ctrlProvince.options[0] = new Option(_waitMsg, '-1');
        _ctrlProvince.disabled = true;
        _service.GetProvinces(
            function(data) {
                RIV.Web.USCustoms.locLoadProvinces(data);
            },
            function() { }
        );
    },

    getProvincesByState: function(state) {
        _ctrlProvince.options[0] = new Option(_waitMsg, '-1');
        _ctrlProvince.disabled = true;
        _service.GetProvincesByState(
            state,
            function(data) {
                RIV.Web.USCustoms.locLoadProvinces(data);
            },
            function() { }
        );
    },

    getBorderCrossings: function(reset) {
        RIV.Web.USCustoms.locResetBorderCrossings();
        
        _service.GetBorderCrossings(
            reset,
            function(data) {
                RIV.Web.USCustoms.locLoadBorderCrossings(data);
            },
            function() { }
        );
    },

    getBorderCrossingsByState: function(state) {
        RIV.Web.USCustoms.locResetBorderCrossings();

        _service.GetBorderCrossingsByState(
            state,
            function(data) {
                RIV.Web.USCustoms.locLoadBorderCrossings(data);
            },
            function() { }
        );
    },

    getBorderCrossingsByProvince: function(province) {
        RIV.Web.USCustoms.locResetBorderCrossings();

        _service.GetBorderCrossingsByProvince(
            province,
            function(data) {
                RIV.Web.USCustoms.locLoadBorderCrossings(data);
            },
            function() { }
        );
    },

    showContactInfo: function(officeID) {
        _service.GetContactInfo(
            officeID,
            _language,
            function(data) {
                if (data == '')
                    RIV.Web.USCustoms.locShowContent(_notFoundMsg);
                else {
                    RIV.Web.USCustoms.locShowContent(data);
                }
            },
            function() { });
    },

    openOffWindow: function() {    
        window.open(_root + _language + '/USCustomsOff.aspx', '', 'toolbar=yes,scrollbars=yes,width=650,height=710,left=75,top=75');
    },

    showDefaultContent: function() {
        RIV.Web.USCustoms.locShowContent(_defaultContent)
    },

    showDefaultContentBorder: function() {
        RIV.Web.USCustoms.locShowContent(_defaultContentBorder)
    }
}

RIV.Web.USCustoms.locLoadStates = function(data) {
    _ctrlState.disabled = false;
    _ctrlState.remove(0);
    _ctrlState.options[0] = new Option(_stateDefaultItem, '0');

    var selectedIndex = 0;

    while (_ctrlState.length > 1)
        _ctrlState.remove(_ctrlState.length - 1);

    for (var i = 0; i < data.length; ++i) {
        if (data[i].IsSelected)
            selectedIndex = i + 1;

        _ctrlState.options[_ctrlState.length] = new Option(data[i].Name, data[i].Value);
    }

    if (selectedIndex > 0) {
        _ctrlState.USCustoms.showDefaultContentBorder();
    }
    
    _ctrlState.selectedIndex = selectedIndex;
}

RIV.Web.USCustoms.locLoadProvinces = function(data) {
    _ctrlProvince.disabled = false;
    _ctrlProvince.remove(0);
    _ctrlProvince.options[0] = new Option(_provinceDefaultItem, '0');

    var selectedIndex = 0;

    while (_ctrlProvince.length > 1)
        _ctrlProvince.remove(_ctrlProvince.length-1);

    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) {
        _ctrlState.USCustoms.showDefaultContentBorder();
    }

    _ctrlProvince.selectedIndex = selectedIndex;
}

RIV.Web.USCustoms.locLoadBorderCrossings = function(data) {
    _ctrlBorderCross.disabled = false;
    _ctrlBorderCross.remove(0);
    _ctrlBorderCross.options[0] = new Option(_borderDefaultItem, '0');

    var selectedIndex = 0;

    for (var i = 0; i < data.length; ++i) {
        if (data[i].IsSelected)
            selectedIndex = i + 1;

        _ctrlBorderCross.options[_ctrlBorderCross.length] = new Option(data[i].Name, data[i].Value);
    }

    if (selectedIndex > 0) {
        var officeID = parseInt(_ctrlBorderCross[selectedIndex].value);
        _ctrlProvince.USCustoms.showContactInfo(officeID);
    }

    _ctrlBorderCross.selectedIndex = selectedIndex;
}

RIV.Web.USCustoms.locResetBorderCrossings = function() {
    while (_ctrlBorderCross.length > 0)
        _ctrlBorderCross.remove(0);

    _ctrlBorderCross.options[0] = new Option(_waitMsg, '-1');
    _ctrlBorderCross.selectedIndex = 0;
    _ctrlBorderCross.disabled = true;
}

RIV.Web.USCustoms.locShowContent = function(html) {
    if (!(_divId && html))
        return;

    var div = $get(_divId);

    if (div != null) {
        div.innerHTML = html;
    }
}

RIV.Web.USCustoms.registerClass('RIV.Web.USCustoms');

if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();