/**
 * geo form updater
 */
var geoform = 
{
    ajaxurl: "/ajax/geo/update.php",
    
    update: function(geoDiv)
    {
        this.prepare(geoDiv, 3);
    },
    
    /**
     * prepare the geo form update
     * @param geoDiv string id of the geo div to update
     * @param level  int    0: reinit the geo form
     *                      1: update with the country
     *                      2: update with (1) + zipcode or area
     *                      3: update with (2) + town
     */
    prepare: function(geoDiv, level)
    {
        var ajaxData = {};
        var geoContext = $j('#' + geoDiv);
        if(level > 0)
        {
            ajaxData['my_country']  = $j('#my_country', geoContext).val();
            
            if(level > 1)
            {
                var area = $j('#my_area');
                if(area.length > 0)
                {
                    ajaxData['my_area']  = area.val();
                }
                var zipcode = $j('#my_zipcode');
                if(zipcode.length > 0)
                {
                    ajaxData['my_zipcode']  = zipcode.val();
                }
            
                if(level > 2)
                {
                    var town = $j('#my_town');
                    if(town.length)
                    {
                        ajaxData['my_town']  = town.val();
                    }
                }
            }
        }
        
        this._updateRequest(geoContext, ajaxData);
    },
    
    _updateRequest: function(geoDiv, data)
    {
        $j.ajax({
            type: "POST",
            url:  this.ajaxurl,
            data: data,
            dataType: "text",
            complete: function(xhr)
            {
                if(xhr.status == 200)
                {                
                    geoDiv.replaceWith(xhr.responseText);
                    var geoErrorDiv = $j('#err_my_geo');
                    geoErrorDiv.hide();
                }
            }
        });
    }
}