function getFormParams(form){
  // ripped this code from http://be.twixt.us/jquery/formSubmission.php
  var params = {};
  jQuery(form).find("input[@checked], input[@type='text'], input[@type='hidden'], input[@type='password'], input[@type='submit'], option[@selected], textarea")
         .filter(":enabled")
         .each(function(){
            var key = this.name || this.id || this.parentNode.name || this.parentNode.id;
            var values = params[key];
            if (!values) {
              values = [];
              params[key] = values;
            }
            values.push(this.value);
            });
  return params;
}
 
function parseResourceId(url) {
  var m = url.match(/\/(\d+)$/);
  if (m) {
    return m[1];
  }
} 

function removeResourceId(url) {
  var m = url.match(/(\/\d+)$/);
  if (m) {
    return url.replace(m[1], '');
  } else {
    return url;
  }
}

function editComment(comment_id, redirect) {
  if ( ! redirect ) { redirect = window.location }
  overlay.load({ name : 'dialog', title : 'Edit comment', width : 610, 
    shadow : true, version: 'v4',
    initFunc: function(overlayContentDiv) {
      comment_url = '/comments/' + comment_id + '/edit?partial=1&redirect=' + encodeURIComponent(redirect);
      jQuery.ajax({
            type: "GET",
            url: comment_url,
            success: function(comment_form){
              // TODO: investigate why this pageTracker line is
              // making firebug choke
              //pageTracker._trackPageview(comment_url);
              var div = document.createElement('div');
              div.innerHTML = comment_form;
              overlayContentDiv.appendChild(div);
              jQuery('#cancel-link', overlayContentDiv).click(
                function(){ overlay.close();return false; }
                );
            }
        });        
      } 
    });
}

function selectFilter(selectEl, qs_param_name) {
  var selectedVal = jQuery('option:selected', selectEl).val();
  if (window.location.href.match((new RegExp(qs_param_name)))) {
    window.location.href = window.
                            location.
                            href.
                            replace(new RegExp(qs_param_name + '=\\w*'), 
                                    qs_param_name + '=' + selectedVal);
  } else {
    var href = window.location.href;
    if (href.match(/\?/)) {
      if (!href.match(/\?$/)) {
        href += '&'
      }
    } else {
      href += '?'
    }
    href += qs_param_name + '=' + selectedVal;
    window.location.href = href;
  }   
}  
