			var openModal = false;
			var low_line_num = -1; // line where highlighting starts, defaults to number outside of range
			var high_line_num = -1; // line where highlighting ends
			
			var hs_index = 0;
			var current_numlines = 0;
			var overOtherSnippet = false;
			var loadingPreviousPage = false;
			var loadingNextPage = false;
			
			var mousedown = 0; // mouse button is down
			document.onselectstart=new Function ("return false")

			var hs_colors = ["#FFFF00","#80FF00","#FF8000","#00FFFF","#FF0000","#7A7AFF","#3D3DFF","#FF0080","#FF00FF","#8000FF","#0000FF","#C9C9C9"];
			var hs_trans_colors = ["#ffffc0", "#e0ffc0", "#ffe0c0", "#c0ffff", "#ffc0c0", "#dedeff", "#cfcfff", "#ffc0e0", "#ffc0ff", "#e0c0ff"];
			hs_colors[99] = "#C9C9C9"; // background color
			hs_trans_colors[99] = "#f2f2f2";
			var view_page = true;
			var embedded = false;
			var embedded_link = '';

			$(document).ready(function() {
      	if (highlight == true)
      	{
      		lineNums(); // load line nums for document
      		setupForm();
      		
      		if (loadSnippets != false)
   						getSnippets();
   			}
   			
   			// create previous page element
    		var prev_div = '<div id="previousPage">LOADING</div>';
   			var div = jQuery(prev_div);
   			div.css('display', 'none');
				div.appendTo('.page-body');
				
				// create next page element
    		var next_div = '<div id="nextPage">LOADING</div>';
   			var div2 = jQuery(next_div);
   			div2.css('display', 'none');
				div2.appendTo('.page-body');
				
				
				// create loading div
				var loading_html = '<div><br />Loading...<br /><img style="margin-top: 10px;" src="/img/ajax-loader.gif" /><br></div>';
				var loading_div = jQuery(loading_html);
				
				loading_div.attr("id","loadingInfo");
				
				loading_div.css('position', 'absolute');
				loading_div.css('width', '120px');
				loading_div.css('height', '120px');
				loading_div.css('font-size', '20px');
				loading_div.css('background-color', '#FFF');
				loading_div.css('font-weight', 'bold');
				loading_div.css('border', '1px solid #FCB700');
				loading_div.css('padding', '30px');
				loading_div.css('text-align', 'center');
				loading_div.hide();
				
				var pos = $("#form_text").offset();  
			  var width = ($("#form_text").width() / 2) - 60;
				
   			loading_div.css('top', pos.top + 50 + "px");
   			loading_div.css('left', (pos.left + width) + "px");
				loading_div.prependTo('#form_text');
				
				loadBuffers();
   			
				$("#page-left").mouseover(function() {
					$('#page-left').css('background-image', 'url(/i/page-left-bg-over.gif)');
				});
				
				$("#page-left").mouseleave(function() {
					$('#page-left').css('background-image', 'url(/i/page-left-bg.gif)');
				});
				
				
				$("#page-right").mouseover(function() {
					$('#page-right').css('background-image', 'url(/i/page-right-bg-over.gif)');
				});
				
				$("#page-right").mouseleave(function() {
					$('#page-right').css('background-image', 'url(/i/page-right-bg.gif)');
				});
				
				
   			$(window).resize(function(){
				  var pos = $("#form_text").offset();  
				  
				  // reposition loading info
				  var width = ($("#form_text").width() / 2) - 60;
	   			$("#loadingInfo").css('top', pos.top + 50 + "px");
	   			$("#loadingInfo").css('left', (pos.left + width) + "px");
				});

				$(".clear_class").css('width', $("#form_text").width()+'px');
				
				checkLargePage();
				
				if (embedded)
					setupEmbeddedVersion();
      });

			function checkLargePage()
			{
				var currentHtml = $("#currentPage").html();
				if (currentHtml != null && currentHtml.length < 200 && currentHtml.indexOf('GET SECWATCH.COM') >= 0) 
				{
					var temp = new Array();
					temp = currentHtml.split(':');
					loadAWS(temp[1]);
					
					$("#loadingInfo").show();
				}
			}

			function loadAWS(anum) 
			{
				$.ajax({
				   type: "GET",
				   url: "/filings/s3.jsp",
				   data: "anum="+anum+"&p="+page,
				   dataType: "text",
				   success: function(msg){
				   	$("#currentPage").replaceWith('<div id="currentPage">'+msg+'</div>');
				    
				    if ($("#loadingInfo").css('display') == 'block')
							$("#loadingInfo").hide();
							
						lineNums(); // load line nums for document
      			setupForm();
   					
						if (loadSnippets != false)
   						getSnippets();
				   }
				 });
			}

			function disableSelection(target){
			if (typeof target.onselectstart!="undefined") //IE route
				target.onselectstart=function(){return false}
			else if (typeof target.style.MozUserSelect!="undefined") //Firefox route
				target.style.MozUserSelect="none"
			else //All other route (ie: Opera)
				target.onmousedown=function(){return false}
			target.style.cursor = "default"
			}

			function lineNums()
			{
				var fH = document.getElementById("form_text").offsetHeight;
				document.getElementById("lnum").style.height = fH;
			}

			function loadPage(new_page_num)
			{
				//alert(new_page_num);
				$("#thumbs_frame").contents().find("#thumb"+page).css('border', '1px solid #C9C9C9');
				page = new_page_num;
				$("#thumbs_frame").contents().find("#thumb"+page).css('border', '2px solid #000');
				
				$.ajax({
				   type: "GET",
				   url: "/filings/getpage.jsp",
				   data: "formid="+formid+"&page="+page,
				   dataType: "text",
				   success: function(msg){
				   	$("#currentPage").replaceWith('<div id="currentPage">'+msg+'</div>');
				    loadBuffers();
				
						if (highlight == true)
						{
							if (loadSnippets != false)
   							getSnippets();
   							
							adjustHeight();
						}
						
						checkLargePage();
				   }
				 });
				 
				$("#page-num").html("Page "+new_page_num);
			}
			
			function getNextPage() 
			{
				$.ajax({
				   type: "POST",
				   url: "/filings/getpage.jsp",
				   dataType: "text",
				   data: "formid="+formid+"&page="+(page+1),
				   success: function(msg){
				   	$("#nextPage").replaceWith('<div id="nextPage" style="display:none;">'+msg+'</div>');
				    
				    if (loadingNextPage == true)
				    	clickNext();
				   }
				 });
			}
			
			function getPreviousPage() 
			{
				$.ajax({
				   type: "POST",
				   url: "/filings/getpage.jsp",
				   dataType: "text",
				   data: "formid="+formid+"&page="+(page-1),
				   success: function(msg){
				   	$("#previousPage").replaceWith('<div id="previousPage" style="display:none;">'+msg+'</div>');
				    
				    if (loadingPreviousPage == true)
				    	clickPrevious();
				   }
				 });
			}
			
			
			
			function loadBuffers()
			{
				$("#previousPage").replaceWith('<div id="previousPage" style="display:none;">LOADING</div>');
				$("#nextPage").replaceWith('<div id="nextPage" style="display:none;">LOADING</div>');
				
				if (page > 1)
					getPreviousPage();
				
				getNextPage();	
			}

			function clickPrevious()
			{
				if (page > 1)
				{
					var nextHtml = $("#previousPage").html();
					
					// still waiting for response
					if (nextHtml.indexOf('LOADING') >= 0 && nextHtml.length < 25)
					{
						$("#loadingInfo").show();
						loadingPreviousPage=true;
						getPreviousPage();
					}
					else
					{
						loadingPreviousPage=false;
						
						if ($("#loadingInfo").css('display') == 'block')
							$("#loadingInfo").hide();
					
						$("#currentPage").replaceWith('<div id="currentPage">'+nextHtml+'</div>');
						
						$("#thumbs_frame").contents().find("#thumb"+(page-1)).css('border', '2px solid #000');
						$("#thumbs_frame").contents().find("#thumb"+page).css('border', '1px solid #C9C9C9');
				    
						page = page - 1;
						$("#page-num").html("Page "+page);
						
						if (highlight == true)
						{
							adjustHeight();
							
							if (loadSnippets != false)
								getSnippets();
						}
						
						loadBuffers();
						
						//$("#thumbs_frame").contents().scrollTo("#thumb"+(page), 600, { offset:{ top:0,left:0 } });
						document.getElementById('thumbs_frame').contentWindow.gotoPage(page);
						
						checkLargePage();
					}
				}
			}
			
			function clickNext()
			{
				var nextHtml = $("#nextPage").html();
				
				// still waiting for response
				if (nextHtml.length < 25 && nextHtml.indexOf('LOADING') >= 0)
				{
					$("#loadingInfo").show();
					loadingNextPage=true;
					getNextPage();
				}
				else if (nextHtml.length < 25 && nextHtml.indexOf('NO_PAGE') >= 0)
				{
					// no next page, do something here
				}
				else
				{
					loadingNextPage=false;
					if ($("#loadingInfo").css('display') == 'block')
						$("#loadingInfo").hide();
					
					$("#currentPage").replaceWith('<div id="currentPage">'+nextHtml+'</div>');
					
					$("#thumbs_frame").contents().find("#thumb"+(page+1)).css('border', '2px solid #000');
					$("#thumbs_frame").contents().find("#thumb"+page).css('border', '1px solid #C9C9C9');
			    
					
					page = page + 1;
					$("#page-num").html("Page "+page);
					
					if (highlight == true)
					{
						adjustHeight();
						
						if (loadSnippets != false)
							getSnippets();
					}
					
					loadBuffers();
					
					// scroll to thumbnail
					document.getElementById('thumbs_frame').contentWindow.gotoPage(page);
					//$("#thumbs_frame").contents().scrollTo("#thumb"+(page), 1, { offset:{ top:0,left:0 } });
					
					checkLargePage();
				}
			}
			
			function getSnippets()
			{
				resetAllLines();
				var currentDate = new Date()

				$.getJSON("/s/snippets?t="+currentDate.toString()+"&formid="+formid+"&page="+page,
	        function(data){
	          $.each(data.snippets, function(i,item){
	            
	            var snippetid = item.sid;
	            var start = parseInt(item.start);
	            var end = parseInt(item.end);
	            var num_comments = item.num_comments;
	            
	            loadSnippet(start, end, snippetid);
	 						loadSnippetTools(start, end, snippetid, num_comments, snippetid);
	 						hs_index++;
	 						
	            //alert('snippetid:'+item.sid+',start:'+item.start+',end:'+item.end+',num_comments:'+item.num_comments);
	            //if ( i == 3 ) return false;
	          });
	        });
	      
			}

			// removes highlighted lines, reset values
			function resetLines()
			{
				if (low_line_num != -1 && high_line_num != -1)
				{
					for (j=low_line_num; j<=high_line_num; j++)
					{
						//alert(rgbToHex($("#"+j).css("background-color"))+'='+ hs_colors[hs_index]);
						
							$('#'+j).css("background-color","");
							$('#lnum-row'+j).css("background-color","#FFF");
						
					}
					
					low_line_num = -1;
					high_line_num = -1;
					overOtherSnippet = false;
				}
			}
			
			// removes ALL highlighted lines, reset values
			function resetAllLines()
			{
					for (j=0; j<=current_numlines; j++)
					{
						//alert(rgbToHex($("#"+j).css("background-color"))+'='+ hs_colors[hs_index]);
						
							$('#'+j).css("background-color","");
							$('#lnum-row'+j).css("background-color","#FFF");
							$("#"+j).unbind('click', null);
							$("#"+j).css("cursor", "url('/img/highlighter.ico'), default");
					}

					for (i=0; i<=hs_index; i++)
					 $("#tools"+i).remove();

					hs_index = 0;
					
			}
		
			function rgbToHex(rgb) 
			{
			  var rgbvals = /rgb\((.+),(.+),(.+)\)/i.exec(rgb);
			  var rval = parseInt(rgbvals[1]);
			  var gval = parseInt(rgbvals[2]);
			  var bval = parseInt(rgbvals[3]);
			  return '#' + (
			    rval.toString(16) +
			    gval.toString(16) +
			    bval.toString(16)
			  ).toUpperCase();
			
			} 
		
			function adjustHeight()
			{
				var line_height = 14;
				var fH = document.getElementById("currentPage").offsetHeight;
				
				//alert(fH);
				//document.getElementById("lnum").style.height = fH;
				
				
				var num_lines = parseInt(fH / line_height);
				
				// there are more lines in the new page than in the previous page
				
				var form_width = $('#form_text').width() + 21;
				
				if (num_lines < current_numlines)
					current_numlines = 0;
				
				
				var line_nums = '';
				for (i=(current_numlines+1); i<=num_lines; i++)
				{
					line_nums += '<div class="lnum-row" id="lnum-row'+i+'"><span id="'+i+'" name="line'+i+'" class="highlight_line" style="width:'+form_width+'px;" ><div id="clear_class" class="clear_class" style="color: #FFF;"><img src="/img/shim.gif" style="height:11px;width:1px;" />SECWATCHSECWATCHSECWATCHSECWATCHSECWATCHSECWATCHSECWATCHSECWATCHSECWATCHSECWATCHSECWATCHSECWATCH</div></span></div>';
					
				}
				
				if (current_numlines == 0)
					$(".lnum").replaceWith('<div class="lnum" id="lnum" style="display: block;">'+line_nums+'</div>');
				else
					$(".lnum").append(line_nums);
			
				for (j=0; j<=num_lines; j++)
				{
					$("#"+j).unbind('mouseover', null);
					
					$("#"+j).mouseover(function()
					{
						if (overOtherSnippet != true)
							highlightline(this.getAttribute('id'), false);
					});
				}
				
				current_numlines = num_lines;
				
				if (fH > 800)
					document.getElementById("thumbs_frame").height = fH;
				else
					document.getElementById("thumbs_frame").height = 800;
				
				
			}
		
			function setupForm()
			{
				adjustHeight();
				
				$("body").mouseup(function(){
		      mousedown=0;
		      
		      if (low_line_num != -1 && high_line_num != -1)
					{
						if (openModal != true)
						{
							jQuery.facebox({ ajax: '/filings/snippet/highlight.jsp?formid='+formid+'&page='+page+'&start='+low_line_num+'&end='+high_line_num });
							openModal=true;
						}
					}
							      
		      
		    }).mousedown(function(){
		      
		      	if (openModal != true)
		      	{
		      		resetLines();
		      		mousedown=1;
		      	}
		    });
			}
			
			function checkLinesByRange()
			{
				for (i=low_line_num; i<=high_line_num; i++)
				{
					if ($("#"+i).css("background-color") != hs_colors[hs_index])
					{
						highlighlineStyle(i);
						
					}
				}
			}
			
			function highlighlineStyle(line)
			{
				$('#'+line).css("background-color",hs_colors[hs_index]);
	      $('#'+line).css("filter","alpha(opacity=25)");
	      $('#'+line).css("-moz-opacity","0.25");
	      $('#'+line).css("opacity","0.25");
	      $('#'+line).css("-khtml-opacity","0.25");
			}
			
			function highlightline(line, check)
			{
				$('#overdiv').val(line);
				if (mousedown == 1 || check)
				{
					
					
					//if ($('#lnum-row'+line).css("background-color") != 'rgb(255, 255, 255)')
					//	alert($('#lnum-row'+line).css("background-color"));
						
		      $('#'+line).css("background-color",hs_colors[hs_index]);
		      $('#'+line).css("filter","alpha(opacity=25)");
		      $('#'+line).css("-moz-opacity","0.25");
		      $('#'+line).css("opacity","0.25");
		      $('#'+line).css("-khtml-opacity","0.25");
		      
		      //$('#lnum-row'+line).css("background-color",hs_colors[hs_index]);
		     
		     	if (parseInt(line) < low_line_num || low_line_num == -1)
		     	{
		     		low_line_num = parseInt(line);
		     		//$('#'+low_line_num).css("border-top","3px solid #000;");
		     	}
		     	
		     	if (parseInt(line) > high_line_num || high_line_num == -1)
		     	{
		     		high_line_num = parseInt(line);
		     		//$('#'+high_line_num).css("border-bottom","3px solid #000;");
		     	}
		     	
		     	$('#overdiv').val(line);
		     	$('#highlinenum').val(high_line_num);
		     	$('#lowlinenum').val(low_line_num);
		     	
		     	
		     	$('#'+line).css("background-color",hs_colors[hs_index]);
		     	
		     	checkLinesByRange();	 
		    }
			}

			function loadSnippet(start, end, snippetid)
			{
				//alert(start+','+end);
				for (i=start; i<=end; i++)
				{
					//alert(hs_index);
					//alert('#lnum-row'+i+', '+hs_colors[hs_index]);
						highlighlineStyle(i);
						//$('#lnum-row'+i).css("background-color", hs_colors[hs_index]);
						
					$("#"+i).css('cursor', 'pointer');
					$("#"+i).unbind('mouseover', null);
					$("#"+i).click(function(){commentedSnippet(snippetid)});
					
					$("#"+i).mouseover(function(){
			      mousedown=0;
			      
			      if (low_line_num != -1 && high_line_num != -1)
							overOtherSnippet = true;
					});
					
				}
				//alert('done');
			}
			
			// removes ALL highlighted lines, reset values
			function setupEmbeddedVersion()
			{
					for (j=0; j<=current_numlines; j++)
					{
						$("#"+j).unbind('click', null);
						$("#"+j).css("cursor", "pointer");
						$("#"+j).click(function(){top.location.href=embedded_link;});
					}
					
			}
			
			function loadSnippetTools(start, end, commentid, comments, snippetid)
			{
				  var pos = $("#lnum-row"+start).offset();  
				  var width = $("#lnum-row"+start).width() + $("#"+start).width() - 156;
	   			
	   			var div_html = '<div class="snippettools"><a onclick="commentedSnippet('+snippetid+');" style="cursor:pointer;text-decoration:underline;color: #000;">comments('+comments+')</a>&nbsp;|&nbsp;<a onclick="linkSnippet('+snippetid+');" style="cursor:pointer;text-decoration:underline; color:#000;">link to</a>&nbsp;|&nbsp;<a onclick="emailSnippet('+snippetid+');" style="cursor:pointer;text-decoration:underline; color: #000;">email</a></div>'
	   			
	   			if (comments == -1)
	   				div_html = '<div><a onclick="uncommentedSnippet('+start+', '+end+');" style="cursor:pointer;text-decoration:underline;color:#000;"><b>comment</b></a>&nbsp;|&nbsp;<a onclick="linkSnippet('+snippetid+');" style="cursor:pointer;text-decoration:underline;color:#000;">link to</a>&nbsp;|&nbsp;<a onclick="emailSnippet('+snippetid+');" style="cursor:pointer;text-decoration:underline;color:#000;">email</a></div>'	
	   			
	   			var div = jQuery(div_html);
	   			
	   			
	   			div.attr("id","tools"+hs_index);
	   			div.css('background-color', hs_trans_colors[hs_index]);
	   			//div.css('background-color', hs_colors[hs_index]);
	   			//div.css('border-left', '1px solid #C9C9C9');
	   			//div.css('border-right', '1px solid #C9C9C9');
	   			//div.css('border-top', '1px solid #C9C9C9');
	   			
	   			div.css('text-align', 'right');
	   			div.css('position', 'absolute');
	   			div.css('height', '15px');
	   			div.css('width', '140px');
	   			div.css('padding', '5px 10px 5px 5px');
	   			div.css('font-size', '10px');
	   			div.css('left', (pos.left + width) + "px");
	   			div.css('top', (pos.top - 25) +"px");
	   			div.css('z-index', '100');
					div.appendTo('#view_filing');
			}
			
			function uncommentedSnippet(start, end)
			{
				jQuery.facebox({ ajax: '/filings/snippet/highlight.jsp?formid='+formid+'&page='+page+'&start='+start+'&end='+end });
			}
			
			function commentedSnippet(snippetid)
			{
				jQuery.facebox({ ajax: '/filings/snippet/existing_comments_outer.jsp?formid='+formid+'&page='+page+'&snippetid='+snippetid });
			}
			
			function linkSnippet(snippetid)
			{
				jQuery.facebox({ ajax: '/filings/snippet/link_outer.jsp?formid='+formid+'&page='+page+'&snippetid='+snippetid });
			}
			
			function emailSnippet(snippetid)
			{
				jQuery.facebox({ ajax: '/filings/snippet/email_outer.jsp?formid='+formid+'&page='+page+'&snippetid='+snippetid });
			}
			
			function printPage()
			{
				window.location.href='/filings/print.jsp?formid='+formid+'&page='+page;
			}
			
			function linkToEmbedPage()
			{
				jQuery.facebox({ ajax: '/filings/snippet/page_linkembed.jsp?formid='+formid+'&page='+page });
			}
			
			function noteContent()
			{
				jQuery.facebox({ ajax: '/filings/snippet/highlight.jsp?formid='+formid+'&page='+page });
			}
