/*
 usage:
 var states = new States(id [, initial_country, initial_state] )

 creates a dynamic state select list.

 id is the DOM element to bind
 initial_country is a valid country code to prepopulate the states list
 initial_state is a valid state code for that country and will pre-select
   that state in the list
*/
function States(id, initial_country, initial_state) {
    var dom_id;
    dom_id = document.getElementById(id);
    
    function populate(o) {
        var obj = eval( '(' + o.responseText + ')' );
        populateSelect(dom_id, obj.Result);
        if ( initial_state != null ) {
            set(initial_state);
            initial_state = null;
        }
        return;
    }
    
    function notify_failure(o) {
        window.alert('Error: ' + o.ResponseText, 'Petfinder Warning', YAHOO.widget.SimpleDialog.ICON_WARN);
    }
            
    function set(s) {
        setSelectedValue(dom_id, s);
    }
    
    this.list = function(c, preset) {
        // This is ends up being the first argument when called from onDOMReady
        if ( c == 'DOMReady' ) {
            c = null;
        }
        
        // need to clear initial_country in case the country list changes
        if ( initial_country ) {
            c = initial_country;
            initial_country = null;
        }

        if ( preset != '' ) {
            initial_state = preset;
        }

        var query = c ? '/svcs/states.pl?output=json&country_code=' + c : '/svcs/states.pl?output=json';
        YAHOO.util.Connect.asyncRequest('GET',
            query, {
            success : populate,
            failure : notify_failure,
            scope   : this
        }, null);
    };

    YAHOO.util.Event.onDOMReady(this.list,null,false);

    return this;
}

/*
 usage:
 var countries = new Countries( id [, initial_country, change_object ] )

 id is the DOM element to bind
 initial_country enables pre-selecting the country in the list
 change_object is a States() object that will get refreshed on new country
 selection
*/
function Countries(id, initial_country, change_obj) {
    var dom_id;
    dom_id = document.getElementById(id);

    function populate(o) {
        var obj = eval( '(' + o.responseText + ')' );
        populateSelect(dom_id, obj.Result);
        if ( initial_country != null ) {
            this.set(initial_country);
            initial_country = null;
        }
        return;
    }

    function notify_failure(o) {
        window.alert(o.ResponseText, 'Petfinder Warning', YAHOO.widget.SimpleDialog.ICON_WARN);
    }
    
    /*
    function set(c) {
        setSelectedValue(dom_id, c);
    }
    */

    function changeEvent() {
        // this should be a States() object so we can update the states
        // when the country selection changes
        change_obj.list(dom_id.options[dom_id.selectedIndex].value);
    }

    this.list = function() {
        YAHOO.util.Connect.asyncRequest('GET',
            '/svcs/countries.pl?output=json', {
            success : populate,
            failure : notify_failure,
            scope   : this
        }, null);
    };
    
    this.set = function(c) {
        setSelectedValue(dom_id, c);
    };

    YAHOO.util.Event.onDOMReady(this.list, this, true);
    YAHOO.util.Event.addListener(dom_id, 'change', changeEvent);

    return;
}
