function getCookie(name) {
    name = name + "=";
    var cookies = document.cookie.split(';');
    for (var i = 0; i < cookies.length; i++) {
        var c = cookies[i];
        while (c.charAt(0) == ' ') {
            c = c.substring(1, c.length);
        }
        if (c.indexOf(name) === 0) {
            return c.substring(name.length, c.length);
        }
    }
    return null;
}
function activateSearch(elid) {
    $(elid).removeClassName("inactive");
    $(elid).value = "";
}
function deactivateSearch(elid, defaultValue) {
    if($(elid).value == "" || $(elid).value == defaultValue) {
        $(elid).addClassName("inactive");
        $(elid).value = defaultValue;
    }
}

function openAttachmentWindow(url, h, w) {
    if ( h === undefined ) {
        h = 0.25;
    }
    if ( w === undefined ) {
        w = 0.25;
    }
    if ( w > 1 ) {
      var options = "height=" + h + ",width=" + w + ",resizable=no,scrollbars=no";
    } else {
      var width = document.viewport.getWidth();
      var height = document.viewport.getHeight();
      var options = "height=" + (height - h * height) + ",width=" + (width - w * width) + ",resizable=yes,scrollbars=yes";
    }
    var win = this.window.open(url, 'win', options);
    return false;
}
function highlightStory(id) {
    $('news_'+id).addClassName("highlighted");
}
function unhighlightStory(id) {
    $('news_'+id).removeClassName("highlighted");
}
function selectStory(id) {
   window.location = $('link_' + id).href;
}

// Registers a callback which copies the csrf token into the X-CSRF-Token 
// header with each ajax request.  Necessary to work with rails applications 
// which have fixed CVE-2011-0447
// See: http://weblog.rubyonrails.org/2011/2/8/csrf-protection-bypass-in-ruby-on-rails
Ajax.Responders.register({
  onCreate: function(request) {
    var csrf_meta_tag = $$('meta[name=csrf-token]')[0];

    if (csrf_meta_tag) {
      var header = 'X-CSRF-Token',
          token = csrf_meta_tag.readAttribute('content');

      if (!request.options.requestHeaders) {
        request.options.requestHeaders = {};
      }
      request.options.requestHeaders[header] = token;
    }
  }
});
// See: http://weblog.rubyonrails.org/2011/2/8/csrf-protection-bypass-in-ruby-on-rails
(function($){
  $(document).ajaxSend(function(e, xhr, options) {
    var token = $("meta[name='csrf-token']").attr("content");
    xhr.setRequestHeader("X-CSRF-Token", token);
  });
})(jQuery);

