function ClubSelector(data, regionSel, clubSel, regionSelOnComplete, hidRegionId, hidClubFacId) { 
    this.SelClub = $(clubSel);
    this.SelRegion = $(regionSel);
    this.DataDivId = data;
    this.Data = $(data).innerHTML.evalJSON(true);
    this.RegionSelectedOnComplete = regionSelOnComplete;
    if (hidClubFacId != null && hidClubFacId != '')
        this.HiddenClubFacId = $(hidClubFacId);
    if (hidRegionId != null && hidRegionId != '')
        this.HiddenRegionId = $(hidRegionId);   
    this.init();

}
ClubSelector.prototype.init = function() {
    var This = this;
    if (this.SelRegion == null)
        return;
    
    this.SelRegion.onchange = function() { This.RegionSelected(); }
    if (this.HiddenClubFacId != null)
        this.SelClub.onchange = function() { This.ClubSelected(); }
 
    this.FillRegionDropdown();
    this.FillClubDropdown(); 
    
    if (this.SelRegion.selectedIndex != -1)
    {
        This.RegionSelected();
    }
    return;
};
ClubSelector.prototype.Show = function(show) {
    
    var obj = this.Div;
    
    if (show) {
        this.DlgBox.Show();
        obj.show();
    }
    else {
        this.DlgBox.Hide();
        obj.hide();
    }
    return;
};
ClubSelector.prototype.FillDropdown = function(ddl, arr, selVal, opt0) {
    //alert('Filling DropDown');
    ddl.options.length = 0;
    var offset = 0;
    
    if (opt0 != null) {
        ddl.options[0] = opt0;
        offset = 1;
    }
    for (var i=0; i<arr.length; i++) {
        var name = arr[i].name;
        var id = arr[i].id;
        ddl.options[i + offset] = new Option(name, id);
        if (id == selVal)
            ddl.options[i + offset].selected = true;
    }
};
ClubSelector.prototype.FillClubDropdown = function(doNotShowDefault) {
    //alert('Filling Club DropDown');
    var d = this.Data;
    var opt0 = this.GetClubNoneSelected();
    
    var selVal;
    
    if(d.SelectedClub == null){
        if(this.HiddenClubFacId != null &&
            this.HiddenClubFacId.value != ''){
            selVal = this.HiddenClubFacId.value;
        }else{
            selVal = '-1';
        }
    }else{
        selVal = d.SelectedClub;
    }
    
    this.FillDropdown(this.SelClub, d.RegionClubs, selVal, doNotShowDefault==true?null:opt0);
};
ClubSelector.prototype.SetRegion = function(rgnId) {
    //assumes regions already populated
    if (this.SelRegion == null)
        return;
    
    if (this.SelRegion.options.length > 0) {
        var opt;
        for (var i=0;i<this.SelRegion.options.length;i++) {
            opt = this.SelRegion.options[i];
            if (opt.value == rgnId) { opt.selected = true; }
        }
    }
    this.RegionSelected();
}
ClubSelector.prototype.GetRegionNoneSelected = function() {
    return new Option('Select Region...', -1);
};
ClubSelector.prototype.GetClubNoneSelected = function() {
    return new Option('Select Club...', -1);
};
ClubSelector.prototype.FillRegionDropdown = function() {
    var d = this.Data;
    var opt0 = this.GetRegionNoneSelected();
        
    var selVal = d.SelectedRegion == null ? '-1' : d.SelectedRegion;
      
    selVal = selVal == '-1' && this.HiddenRegionId != null &&   this.HiddenRegionId.value != '' ? this.HiddenRegionId.value : '-1';
    this.FillDropdown(this.SelRegion, d.Regions, selVal, opt0);

    //this.RegionSelected();
};
ClubSelector.prototype.RegionSelected = function() {
    var ddl = this.SelRegion;
    var regionId = ddl.options[ddl.selectedIndex].value;
    var region = ddl.options[ddl.selectedIndex].text;
    
    //alert('Region Select:' + regionId );
    if (this.HiddenRegionId != null)
        this.HiddenRegionId.value = regionId;
            
    var This = this;

    var ajaxUrl = location.href;
    if (location.href.lastIndexOf('/') == location.href.length - 1)
        ajaxUrl = ajaxUrl + 'default.aspx';
    
    if (location.href.lastIndexOf('?') < 0){
        ajaxUrl = ajaxUrl + '?';
    }
    else
    {
        ajaxUrl = ajaxUrl + '&';
     }
     
    ajaxUrl = ajaxUrl + 'ajax=true&method=UpdateClubList&params=' + regionId;
    //alert('Making Ajax Call: ' + ajaxUrl);
   /* new Ajax.Updater(This.DataDivId, ajaxUrl,
        {asynchronous:true,method:'post',onComplete:This.RegionSelectedPreComplete.bind(this),
        postBody:'ajax=true&method=UpdateClubList&params=' + regionId}); 
   */
   new Ajax.Updater(This.DataDivId, ajaxUrl,{method: 'get', onComplete:This.RegionSelectedPreComplete.bind(this)});
};
ClubSelector.prototype.ClubSelected = function() {
    var ddl = this.SelClub;
    this.HiddenClubFacId.value = ddl.options[ddl.selectedIndex].value;
    //alert(this.HiddenClubFacId.value);
};
ClubSelector.prototype.RegionSelectedPreComplete = function()
{
    
    //If the clubFacId field is populated, make it selected in the dropdown list
    var ddl = this.SelClub;
    //alert('RegionSelected PreComplete')
    for(var i; i < ddl.options.length; i++){
        if(ddl.options[i].value == this.HiddenClubFacId.value)
        {
            ddl.selectedIndex = i;
        }
    }
    
    //If there was no value selected, reset the field
    if(ddl.selectedValue == '-1'){
        this.HiddenClubFacId.value = '';
    }
    
    this.RegionSelectedOnComplete();
};
ClubSelector.prototype.SelectClub = function(clubId) {
    for (var i=0;i<this.SelClub.options.length;i++) {
        opt = this.SelClub.options[i];
        if (opt.value == clubId) { opt.selected = true; }
    }
};
ClubSelector.prototype.RemoveClub = function(clubId)
{
    for (var i=0;i<this.SelClub.options.length;i++) {
        opt = this.SelClub.options[i];
        if (opt.value == clubId) { this.SelClub.remove(i); }
    }
}