﻿function ProgressIndicator(timeout) {
    var _container = null;
    var _timeoutID = null;
    var _timeout = (timeout == null) ? 10 : timeout;    
    $(document).ready(function() {    
    var clientBounds = $common.getClientBounds();    
    _container = $(window.document.createElement('div'));    
        _container.attr('id', 'progress');
        _container.addClass('progress');
        _container.css({
            'display': 'none',
            'width': Math.max(Math.max(document.documentElement.scrollWidth, document.body.scrollWidth), clientBounds.width),
            'height': Math.max(Math.max(document.documentElement.scrollHeight, document.body.scrollHeight), clientBounds.height)
        });        
        $(window.document.body).append(_container);

        var pageRequestManager = Sys.WebForms.PageRequestManager.getInstance();

        // Add initializeRequest and endRequest
        pageRequestManager.add_initializeRequest(onInitializeRequest);
        pageRequestManager.add_beginRequest(onBeginRequest);
        pageRequestManager.add_endRequest(onEndRequest);
    });

    this.show = function() {
        onBeginRequest(null, null);
    }

    this.hide = function() {
        onEndRequest(null, null);
    }

    onInitializeRequest = function(sender, args) {        
        if (Sys.WebForms.PageRequestManager.getInstance().get_isInAsyncPostBack()) {
            args.set_cancel(true);  // Cancel the post back if already in post back.
        }
    }

    // Called when async postback begins
    onBeginRequest = function(sender, args) {        
        $(window).bind('resize', _show);
        $(window).bind('scroll', _show);

        _show(null);
    }

    // Called when async postback ends
    onEndRequest = function(sender, args) {
        if (_timeoutID) {
            clearTimeout(_timeoutID);
            _timeoutID = null;
        }

        _container.hide();        
        $(window).unbind('scroll', _show);
        $(window).unbind('resize', _show);
    }

    _show = function(e) {
        _timeoutID = setTimeout(function() {
            if (_timeoutID) {
                _container.show();
                _timeoutID = null;
            }
        }, _timeout);
    }
}


/**********************************************
        State Territories
**********************************************/
var IE = /*@cc_on!@*/false;
function StatesCollection() {
    this.selectedState = null;
    this.stateToAdd = null;
    this.countiesToAdd = [];
    this.States = [];
    this.ddlID = null;
    //Main function to call when using this object
    this.Select = function(state, counties) {
        this.stateToAdd = state.value;
        this.selectedState = state.value;
        this.ddlID = state.id;
        if (!(state.value in this.ObjectConverter(this.States))) {
            this.addStateToStates();
            if (!counties) {
                this.getCountiesForState(state);
            } else {
                this.addCountiesToState(counties);
            }
        } else {
            this.buildHTML(state);
        }

    }

    //Simple method to add the passed state into States array
    this.addStateToStates = function() {
        this.States.push(this.stateToAdd);
    }

    //Handles the counties string, building an array out for the specific state
    this.addCountiesToState = function(counties) {
        countiesToAddIndex = counties.split("||");
        this.States[this.selectedState] = [];

        for (x = 0; x < countiesToAddIndex.length; x++) {
            pair = countiesToAddIndex[x].split(",");
            this.countiesToAdd[x] = [];
            this.countiesToAdd[x].push(pair[0], pair[1]);
        }

        for (x = 0; x < countiesToAddIndex.length; x++) {
            this.States[this.selectedState][x] = [];
            this.States[this.selectedState][x].push(this.countiesToAdd[x][0], this.countiesToAdd[x][1]);
        }
    }

    this.getCountiesForState = function(state) {
        var AddTerritories = eval('PageMethods.GetCountiesInState');
        if (AddTerritories != null) {
            AddTerritories(state.value, onRequestComplete, onerror, state);
        }
        //PageMethods.GetCountiesInState(state.value, onRequestComplete, onerror, state);
    }

    //Converts array to string and checks against values to see if 'a' exists
    this.ObjectConverter = function oc(a) {
        var o = {};
        for (var i = 0; i < a.length; i++) {
            o[a[i]] = '';
        }
        return o;
    }

    this.buildHTML = function(state) {

        obj = document.getElementById("ddlTerritoryCounties" + state.id.substring(18, 19));
        obj.disabled = false;
        
        //Clear out the previous county options...
        obj.innerHTML = '';
        
        //insert the default "Select..." option
        obj.options[0] = new Option("Select...", "");
        
        //Iterate through counties and add as options to the drop down...
        for (x = 1; x < this.States[this.selectedState].length; x++) {
            var countyOption = new Option(this.States[this.selectedState][x][1], this.States[this.selectedState][x][0]);
            obj.options[x] = countyOption;
        }
    }
}

function onRequestComplete(counties, state) {
    statesCollection.addCountiesToState(counties);
    statesCollection.buildHTML(state);
}

function onerror(Error) {
    alert(Error.get_message());
    ClearCddDdl(statesCollection.ddlID);   
}

function ClearCddDdl(item) {
    el1 = document.getElementById("ddlTerritoryStates" + item);
    el2 = document.getElementById("ddlTerritoryCounties" + item);

    el1.options[0].selected = true;
    el2.disabled = true;
    el2.innerHTML = "<option>Select...</option>";

}

function addBookmark(title, url, doRedirect) {
    var agent = navigator.userAgent.toLowerCase();
    if (agent.indexOf("firefox") != -1) { // firefox
        window.sidebar.addPanel(title, url, "");
    }
    else if (agent.indexOf("msie") != -1) {// ie
        window.external.AddFavorite(url, title);
    }

    if (doRedirect) {
        window.location = url;
    }
}	 
