		var per_page, page_num, num_pages;
		
		$(document).ready(function(){
			
			//initially, hide comment window and pager bar:
			$('div#comment_and_pager').hide();
									
			//show hide comment form
			$(".add_comm").toggle(function(){
     			$("form.commentform").show('slow');
  			 },function(){
    			 $("form.commentform").hide('slow');
   			});
   			
   			//validate comment form
   			$("#submit").click(function(){
   				$(".error").hide();
   				var email_reg =/^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
   				var no_error = true;
   				
   				var name_val = $("#name").val();
   				if(name_val.length < 3) {
					$("#name").after('<span class="error">&nbsp; Your name should be at least 3 characters</span>');
					no_error = false;
				}
				
				var email_val = $("#email").val();
				if(!email_reg.test(email_val)  || (email_val.length<10) ){
					$("#email").after('<span class="error">&nbsp; Enter a valid email address (10+ chars).</span>');
					no_error = false;
				}
				
				var title_val = $("#title").val();
				if( title_val.length<10 ){
					$("#title").after('<span class="error">&nbsp; Enter comment title (10+ chars).</span>');
					no_error = false;
				}
				
				var comment_val = $("#comment").val();
				if( comment_val.length<10 ){
					$("#comment").after('<span class="error">&nbsp; Enter comment text (10+ chars).</span>');
					no_error = false;
				}
   				return no_error;
			});
			
			
		
			//prepare to fetch (and then fetch) coments:
			per_page = 4; //comments per page - customize it
			page_num = $("#page_num").val();
			content_id = $("#content_id").val();
						
			fetch_comments(per_page, page_num, content_id);
						
			//submitting new comment data
			$("form#commentform").submit(function(){
				$.post("/cgi-bin/comments.cgi",{
					name:$("#name").val(),
					email:$("#email").val(),
					url:$("#url").val(),
       				comment:$("#comment").val(),
       				location:$("#location").val(),
       				title:$("#title").val(),
       				ts:$("#ts").val(),
       				content_id:$("#content_id").val(),
       				action:"add"
     			}, function(xml) {
					var response = $("response",xml).text();
					$("form.commentform").hide('slow');
					var msg;
					if(response == 'OK'){
						msg='Your comment was added';
					}else if(response == 'TIMEOUT'){
						msg='Comment was not added because of timeout';
					}else{
						msg='Comment was not added because of error';
					}
					$(".add_comm").replaceWith("<p class='add_comm'>"+msg+"</p>");
					
					fetch_comments(per_page, num_pages, content_id);
				});
				return false; 	
			});
			
			//add_security check
			$.get("/cgi-bin/comments.cgi?action=token", function(resp){
				$("#ts").val(resp);
			});
			
		}); //ready(function())
		
		
	
		function fetch_comments(per_page, page_num){
			var xhr;
			try { xhr = new XMLHttpRequest(); }                 
    		catch(e) {    
				xhr = new ActiveXObject("Microsoft.XMLHTTP");
			} xhr.onreadystatechange  = function()
    		{ 
         		if(xhr.readyState  == 4){
					if(xhr.status  == 200) {
						display_comments(xhr.responseXML);
					}else{
					}
				}
			}; 
			
			if(page_num <1){
						page_num = 1;
			}
					
			xhr.open("GET", "/cgi-bin/comments.cgi?action=fetch&per_page="+per_page+"&page_num="+page_num+"&content_id="+content_id,  true); 
			xhr.send(null); 
		}

		function display_comments(xml){
			num_pages = $("num_pages",xml).text();
			$("#commentwindow").empty();
			$("#pager").empty();
			if(num_pages == 0){
				$("#commentwindow").prepend("No comments for that item yet.");
				return;
			}
			//show comment and pager if there are any comment pages:
			$('div#comment_and_pager').show();
			
			
			page_num = $("page_num",xml).text();
			
			$("#pager").html("Page "+page_num+" of "+num_pages+"&nbsp;|&nbsp;");

			
			if(page_num > 1){
				var prev_pg = page_num -1;
				$("#pager").append("<a href='#' onClick='fetch_comments("+per_page+" ,"+prev_pg+")'>Prev. </a>&nbsp;");
			}
			
			for(i=1;i<=num_pages; i++){
				if(i == page_num){
					$("#pager").append(i+"&nbsp;");
				}else{
					$("#pager").append("<a href='#' onClick='fetch_comments("+per_page+" ,"+i+")'>"+i+" </a>&nbsp;");
				}
			}
						
			if(page_num < num_pages){
				next_pg = ++page_num; --page_num;
				$("#pager").append("<a href='#' onClick='fetch_comments("+per_page+" ,"+next_pg+")'>Next </a>");
			}
			
			//hide the pager, if there is only one page:
			if(num_pages == 1){
				$("#pager").hide();
			}
	
			$("item",xml).each(function(id) {
				it = $("item",xml).get(id);
				i = $("id",it).text();
				tit = $("title", it).text();
				nam = $("name",it).text();
				loc = $("location",it).text();
				url = $("url",it).text();
				
				date = $("date",it).text();
				comment = return2br($("comment",it).text());
				txt = "<div class='comment_title' title='"+i+"'>"+tit+"</div><div class='comment_author'><b>"+date+"</b><br>Published by: "+nam;
				if(loc.length){
					txt += " - "+loc;
				}
				
				if(url.length){
					if(url.indexOf('http://') != 0){
						url = 'http://'+url;
					}
					txt += "&nbsp;&nbsp;&nbsp;<a href='"+url+"' target='_blank' text-decoration:none><img src='http://www.venturechoice.com/comments_images/visitWebIcon.gif' width='20' height='20' class='webIcon'/></a>";
				}
				
				txt += "</div>";
				txt += "<div class='comment_content'>"+comment+"</div>";
				
				$("#commentwindow").append(txt);
			});			
		}//display_comments
		
		function return2br(dataStr) {
     	   return dataStr.replace(/(\r\n|[\r\n])/g, "<br />");
    	}
    	
    	function insert_comment(content_id, show_table){
    		
    		if(show_table){
    			document.write("<table width=500 border=0 align='center' cellpadding=0 cellspacing=0><tr><td width=7 background=http://www.venturechoice.com/comments_images/lb000001.gif><img src=http://www.venturechoice.com/comments_images/lb000001.gif width=7 height=5></td><td valign=top bgcolor=white align='center'><table width=100% cellspacing=0 cellpadding=0 border=0 height=11><tr><td><img src=http://www.venturechoice.com/comments_images/cgd00001.gif width=100% height=11></td></tr></table>");
    		}
			document.write("<div id='wrapper'  ><div id='comment_and_pager'><p  id='commentwindow'><span id='loading'>Loading Comments...</span></p> <p id='pager' class='pg_nav'>	<form id='pagerform'><input type='hidden' id='page_num' /><input type='hidden' id='content_id' name='content_id' value='"+content_id+"'/></form>	</p></div><a href='#' class='add_comm'>Add Your Comment</a><form class='commentform' id='commentform'><fieldset class='commentFieldset'><legend class='commentFieldsetLegend'>Please, enter your comment (red-bordered inputs are required)</legend><table class='commentTable'><tr><td class='labelsBack'><label for='name'>Name:</label></td><td><input id='name' name='name' class='requir' maxlength='40'></td></tr><tr><td class='labelsBack'><label for='email'>Email:&nbsp;(not published)</label></td>       <td><input id='email' name='email' class='requir' maxlength='80'></td></tr><tr><td width='130' class='labelsBack'><label for='url'>URL:</label></td><td><input id='url' name='url' maxlength='40'></td></tr><tr><td width='130' class='labelsBack'><label for='location'>Location:</label></td><td><input id='location' name='location' maxlength='40'></td></tr><tr><td width='130' class='labelsBack'><label for='title'>Your Comment Title:</label></td><td><input id='title' name='title' class='requir' maxlength='40'></td></tr><tr><td width='130' class='labelsBack'><label for='comment'>Your Comment:</label></td><td><textarea rows='5' cols='64' id='comment' name='comment' class='requir'></textarea></td></tr><tr><td colspan='2' class='submitBack'><input type='submit' class='submitButton' id='submit' value='Submit'></td></tr></table><input type='hidden' name='ts' id='ts' /><input type='hidden' id='content_id' name='content_id' value='"+content_id+"'/></fieldset></form></div>");
			if(show_table){
				document.write("</td><td width=7 background=http://www.venturechoice.com/comments_images/rb000001.gif valign=top style='background-repeat:repeat-y'><img src=http://www.venturechoice.com/comments_images/rb000001.gif width=7 height=5></td></tr><tr><td><img src=http://www.venturechoice.com/comments_images/la000001.gif width=7 height=7></td><td height=7 background=http://www.venturechoice.com/comments_images/bl.gif><img src=http://www.venturechoice.com/comments_images/bl000001.gif width=100% height=7></td><td><img src=http://www.venturechoice.com/comments_images/ra000001.gif width=7 height=7></td></tr> </table>");
			}
		}
