// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
function link_selects(src, dest, url, callback)
{

  
  var optionMatch = /<option value="([^"]*)"[^>]*>([^<]+)<\/option>/g;
  var nvMatch = /<option value="([^"]*)"[^>]*>([^<]+)<\/option>/;
  observer = function (e)
  {
      $('#' + dest).disabled = true;
      var selectedText = $('#' + src)[0].options[$('#' + src)[0].selectedIndex].text;
      var data = {};
      data[src] = $('#' + src)[0].value;

      var rec = $('#' + dest)[0];

      $.get(url, data, function(value){
          selectedIndex = 0;
          var arr = value.match(optionMatch);
          rec.innerHTML = '';

          for (var i = 0; i < arr.length; i ++)
          {
              var opts = arr[i].match(nvMatch);
              rec.options.add(new Option(opts[2], opts[1]));
              if (opts[2] == selectedText) selectedIndex = i;
          }

          rec.selectedIndex = selectedIndex;
          $(rec).disabled = false;
          if (callback) callback();
      });
  };

  $('#' + src).change(observer);
  return observer;
}

$(function(){
  $('#login-anchor').click(function(){
    var el = $('#login-form :text:first');
    if (el.length)
    {
      el.focus();
      return false;
    }
  });
  $('#signup-anchor').click(function(){
    var el = $('#signup :text:first');
    if (el.length)
    {
      el.focus();
      return false;
    }
  });
});
