regionListing = function(__constructor) {
    //data contained at /appjs/local_region_array.js
    var elId = __constructor
    var uData;
    var el = document.getElementById(__constructor);
    var me = this;

    this.setTargetDropDown = function(tField) {
        elId = tField;
        el = document.getElementById(tField);
        return this;
    }

    this.defaultTo = function(regionId) {
        regionId ? el.value = regionId.toLowerCase() : el.selectedIndex = 0;
        return;
    }
    
    this.displayListing = function(defaultregion) {
        if (el) { 
            for(i=1; i < globalRegionList.length; i++) {
                locationary = globalRegionList[i].val;
                if (globalRegionList[i].regType=='subregion') {
                    locationary = '   ' + globalRegionList[i].val;
                }
                if (jQuery.trim(locationary)!='All Regions')
                    el.options[el.length++] = new Option(locationary, jQuery.trim(locationary.toLowerCase()));
            }

        if (defaultregion) {
            this.defaultTo(defaultregion)
        } else {
            if(userData) {
                if (userData.weddingLocation) {
                        this.defaultTo(userData.weddingLocation);
                }
                clearInterval(uData);
            } else {
                //retries until the userData obj is loaded.
                uData = setInterval(function() {
                    me.defaultTo(userData.weddingLocation);
                }, 2500);
            }
        }
        }
        return this;
    }
    
    this.targetUrl = function(url, queryParam) {
        if(queryParam == null) {
            queryParam = 'location';
        }
        if(url) {
            jQuery('#'+elId).bind('change', function(e) {
                if (document.getElementById(elId).selectedIndex!=0) {
                    q = url.indexOf('?')>=0 ? '&' : '?';
                    document.location = url + q + queryParam + '=' + el.value.replace(/&/g, '%26').replace(/,/g, '%2C');
                } else {
                   //view all - deletes the region parameter from url
                   r = new RegExp('(&*)' + queryParam + '=((\\w*)(%*\\d)*)*', 'gi');
                   document.location = document.location.href.replace(r, '');
                }
            });
        }
        return this;
    }
}