$(document).ready(function() {

  $("#contents h2, #contents h3").each(function(index){
    var new_id = index;
    var new_section = "toc_section_" + index
    index+=1;

    if($(this).is("h3")){
      cls="subheading";
    }else{
      cls="heading";
    }

    $("#toc ul").append("<li class=\"" +
			cls +
			"\"><a href=\"#" + 
			new_section +  
			"\">"+ 
			$(this).text() + 
			"</a></li>");

    $(this).attr("id", new_section );
    $(this).append("<a class=\"top\" href=\"#toc\">[top]</a>");
  });

});