
/**
 * This script lets you embed a comment form directly in your post page.
 * The user will see a comment form ready to be filled, while reading your 
 * original post, in your page, without having to go to Blogger.com website
 * or click on a sily link to popup the form.
 *
 * Visitors will feel more invited to comment, and your posts will be
 * more dynamic.
 *
 * It is based on a bloggerhacks.blogspot.com old script, but much improved
 * and way more compatible with nowadys Blogger.com forms and methods.
 *
 * This hack homepage: http://avi.alkalay.net/techdumps/
 * (go ahead and post a comment there!) 
 *
 * 
 * USAGE
 *
 * - Download this script and put it somewhere on your website
 * - Include it in your page in the HTML <head> section like this:
 *
 *   <head>
 *   ... 
 *   <script type="text/javascript" language="JavaScript1.2" src="http://your.page.com/bloggerCommentForm.js">
 *      // a </script> is really needed bellow.
 *   </script>
 *   ...
 *   </head>
 *
 * - Then call it from your template, in the point you want the form to appear:
 *
 *   <script type="text/javascript" language="JavaScript1.2">
 *     commentForm('<$BloggerCommentLink$>');
 *   </script>
 *
 * - You can change text or language of the labels by redefining them before
 *   calling the method, like this example in portuguese:
 *
 *   <script type="text/javascript" language="JavaScript1.2">
 *     var bloggerUserLabel = "Usuário do Blogger.com";
 *     var customUserLabel = "Nome ou apelido";
 *     var anonLabel = "Anônimo";
 *     var nameLabel = "Nome";
 *     // ...
 *
 *     commentForm('<$BloggerCommentLink$>');
 *   </script>
 *
 * Licensed under LGPL
 *
 * (c) Avi Alkalay <avi AT unix DOT sh>
 * Made in Brazil
 * Aug 2006
 *
 */


/** CHANGELOG

   * Avi Alkalay <avi at unix dot sh> Aug 7 2006
   - First transformation from original bloggerhacks.blogspot.com script
   - Updated to new Blogger.com form
   - No need to post always as anonymous
   - Authorship etc compatible and integrated to Blogger.com's tags
   - Source cleaned and beautified
   - Transformed into a includable JS file with callable methods
   - Documentation
   - Much better cookie handling
   - Improved screen layout

*/




// General defaults
var labelWidth = 80;
var bloggerFormActionURL="http://www.blogger.com/login-comment.do"
var confirmBeforePost = true;

// Message defaults
var bloggerUserLabel = "Blogger.com user";
var otherUserLabel = "Other";
var customUserLabel = "Name or nickname: ";
var urlLabel = "URL: ";
var anonLabel = "Anonymous";
var rememberLabel = "Remember Me";
var postedByText = "Posted by";
var commentButtonText = "Post Comment";
var previewButtonText = "Preview";
var previewWindowTitle = "Comment Preview";
var confirmText = "Post this comment?";
var boldButtonText = "B";
var italicsButtonText = "I";
var linkButtonText = "Link";
var linkPrompt = "Link Text:";
var urlPrompt = "Link URL:";
var quoteButtonText = "Quote";
var quotePrompt = "Use your mouse to select the text"+
" in the comment you want to quote.\n"+
"Then press the quote button.";




/*
function getAuthorInfo(frm) {
	var text="";
	var author=frm.author.value;
	var url=frm.anonURL.value;

	if (author=="") author="Anonymous";
	if(url) {
		if (url)text+='"'+url+'"';
		else text+='"'+document.location+'"';

		text+='>'+author+'</a>';
	} else text+=postedByText+'<a><b> </b></a>'+author;
	return text;
}



function encodeAuthorInfo(frm) {
	var text="";
	var author=frm.iden.value;
	var url=frm.url.value;

	if (author=="") author="Anonymous";

	text+='<b id="author">'+author+"</b>\n";

	if (anonURL) text+='<b id="url">'+url+"</b>\n";

	return text;
}

*/

function addTags(ltag,rtag) {
	if (document.all){
		var txt=document.selection.createRange().text;
		if (txt!="") {
			document.selection.createRange().text=ltag+
				txt+rtag;
		}
	} else if (document.getElementById) {
		ta=document.CommentForm.postBody;
		var sLen=ta.textLength;
		var sStart=ta.selectionStart;
		var sEnd=ta.selectionEnd;
		if (rtag != '' && sStart==sEnd) return;
		if (sEnd==1 || sEnd==2) sEnd=sLen;
		var s1=(ta.value).substring(0,sStart);
		var s2=(ta.value).substring(sStart,sEnd);
		var s3=(ta.value).substring(sEnd,sLen);
		ta.value=s1+ltag+s2+rtag+s3;
	}
}





function makeLink() {
	var ta = document.CommentForm.postBody;
	if (document.all && document.selection.createRange().text) {
		linkurl=prompt(urlPrompt,'http://');
		if (linkurl==null || linkurl=='' || linkurl=='http://') return;
		addTags('<a href="'+linkurl+'">','</a>');
	} else if (document.getElementById && ta.selectionStart < ta.selectionEnd) {
		linkurl=prompt(urlPrompt,'http://');
		if (linkurl==null || linkurl=='' || linkurl=='http://') return;
		addTags('<a href="'+linkurl+'">','</a>');
	} else {
		var txt=prompt(linkPrompt,'');
		if (txt==null || txt=='')return;
		var linkurl=prompt(urlPrompt,'http://');
		if (linkurl==null || linkurl=='' || linkurl=='http://') return;
		insertText('<a href="'+linkurl+'">'+ txt+'</a>');
	}
}





function storeCaret(ta) {
	if (ta.createTextRange) ta.caretPos=document.selection.createRange().duplicate();
}





function insertText(txt) {
	var ta=document.CommentForm.postBody;
	if (document.all) {
		if (ta.createTextRange && ta.caretPos) {
			var caretPos = ta.caretPos;
			caretPos.text=caretPos.text.charAt(caretPos.text.length-1)==' '?txt+caretPos.text+' ':txt+caretPos.text;
		} else ta.value = ta.value + txt;
	} else if (document.getElementById) addTags(txt,'');
}




function boldText() {
	addTags('<b>','</b>');
}



function italicText() {
	addTags('<i>','</i>');
}



function quoteText() {
	var txt=(document.all)?document.selection.createRange().text:window.getSelection();
	if(txt==null || txt=='') {
		alert(quotePrompt);
		return;
	}
	insertText('"<i>'+txt+'</i>"');
}




function press(lnk) {
	lnk.className="toolbarPressed";
}



function unpress(lnk) {
	lnk.className="toolbarButton";
}



function PreviewComment(frm) {
	if (frm.postBody.value=='') return;

	var preview=window.open('','Preview','width=400,height=300,resizable=yes,toolbar=no,'+'location=no,directories=no,status=yes,scrollbars=yes,'+'menubar=no');

	preview.document.open();

	var text=frm.postBody.value+"<br/><br/>"+encodeAuthorInfo(frm);

	while(text.indexOf("\n") > -1) text=text.replace("\n","<br/>");

	preview.document.write('<html><head><title>'+previewWindowTitle+'</title></head><bo'+'dy>'+text+'</body></html>');
	preview.document.close();
}




function PostComment(frm) {
	if (frm.postBody.value=="" || (confirmBeforePost && confirm(confirmText)==false)) return;

	var radioButtonIterator;
	for (radioButtonIterator = 0; radioButtonIterator <= 2; radioButtonIterator++)
		if (frm.iden[radioButtonIterator].checked) break;

	if (radioButtonIterator <= 2) setCookie('authorType',radioButtonIterator,'/');
	if (radioButtonIterator == 2) {
		frm.anonName.value="";
		frm.anonURL.value="";
	}

	setCookie('authorName',frm.anonName.value,'/');
	setCookie('anonURL',frm.anonURL.value,'/');
	setCookie('remember','true','/');

	frm.submit();
}



function toggleRemember(frm) {
	if (frm.rememberMe.checked) setCookie('remember','true');
	else setCookie('remember','false');
}



function getCookie(name) {
	var rexp = new RegExp(name + "=([^;]+)");
	var val=rexp.exec(document.cookie);
	return (val!=null)?unescape(val[1]):"";
}



function setCookie(name, value, path, domain, expires, secure) {
	var today=new Date();
//	var expiry=new Date(today.getTime()+90*24*60*60*1000);

	document.cookie= name + "=" + escape(value) +
		((expires) ? "; expires=" + expires.toGMTString() : "") +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		((secure) ? "; secure" : "");

//	document.cookie=name+"="+escape(value)+";expires="+expiry.toGMTString();
}








function commentForm(clink) {
	var blogId;
	var postId;
	var remember=getCookie('remember');
	var bpos=clink.indexOf('blogID=');
	var ppos;
	var endpos=clink.indexOf('&'+'amp;postID=');
	
	if (bpos > -1) blogId=clink.substring(bpos+7,endpos);

	ppos=clink.indexOf('postID=');
	if (ppos > -1) {
		endpos=clink.indexOf('&'+'amp;isPopup=');
		if (endpos < 0) endpos=1000;
		postId=clink.substring(ppos+7,endpos);
	}

	
	document.write('<form class="commentForm" name="CommentForm" action="'+bloggerFormActionURL+'" method="post">');
	
	
	
	/* Render the Remember me checkbox
	document.write('<div style="vertical-align: middle;">');
	document.write('<div style="width:'+labelWidth+'px;float:left;"></div>');
	document.write('<input type="checkbox" name="rememberMe" class="urlBox" tabindex="4" onclick="toggleRemember(document.CommentForm);">');
	document.write(rememberLabel);
	document.write('</input>');
	document.write('</div>');
	*/
	
	
	/* Render the toolbar */
	document.write('<div class="toolbarBox" style="text-align:right">');
	/* bold button */
	document.write('<a class="toolbarButton" onmousedown="press(this);" onmouseup="unpress(this)" href="javascript:boldText();"><b>'+boldButtonText+'</b></a>');
		
	/* italic button */
	document.write('<a class="toolbarButton" onmousedown="press(this);" onmouseup="unpress(this)" href="javascript:italicText();"><i>'+italicsButtonText+'</i></a>');
		
	/* link button */
	document.write('<a class="toolbarButton" onmousedown="press(this);" onmouseup="unpress(this)" href="javascript:makeLink();">'+linkButtonText+'</a>');
		
	/* quote button */
	document.write('<a class="toolbarButton" onmousedown="press(this);" onmouseup="unpress(this)" href="javascript:quoteText();">'+quoteButtonText+'</a>');
		
	document.write('</div>'); /* toolbar end */
	
	
	/* Include post ID */
	document.write('<input type="hidden" name="blogID" value="'+ blogId+'"/>');
	document.write('<input type="hidden" name="postID" value="'+ postId+'"/>');
	
	
	/* Render comment input area */
	document.write('<textarea id="comment-body" name="postBody" rows="10" class="commentBox" tabindex="5" onselect="storeCaret(this);" onclick="storeCaret(this);" onkeyup="storeCaret(this);"></textarea>');
	
	
	/* Render the Name label and input line */
	document.write('<input type="radio" name="iden" value="Blogger" id="iden-bname"><label for="iden-bname">'+ bloggerUserLabel +'</label><br/>');
	document.write('<input type="radio" name="iden" value="Other" id="iden-other"><label id="iden-other-label" for="iden-other">'+otherUserLabel+'</label><br/>');


	/* Render custom name input area */
	document.write('<table class="otherData"><tbody><tr><td>');
	document.write(customUserLabel+'</td><td width="60%"><input type="text" name="anonName" class="nameBox" tabindex="1"/>');

	document.write('</td></tr><tr><td>');

	/* Render the URL input area */
	document.write(urlLabel+'</td><td><input type="text" name="anonURL" class="urlBox" tabindex="2"/>');
	document.write('</td></tr></tbody></table>');
	
	document.write('<input type="radio" name="iden" value="Other" id="iden-other"><label id="iden-other-label" for="iden-other">'+anonLabel+'</label><br/>');
	
	
	/* Some more hidden form args */
	// document.write('<input type="hidden" name="postBody"/>');
	// document.write('<input type="hidden" name="anonymous" value="y"/>');
	
	
	/* The post and preview buttons */
	document.write('<div class="buttonsDiv">');
	document.write('<input type="button"'+ ' class="commentButton" tabindex="6" name="post"'+ ' onclick="PostComment(document.CommentForm);" value="'+ commentButtonText+'"/>');
	document.write('<input type="button" class="previewButton"'+' tabindex="7" name="preview"'+' onclick="PreviewComment(document.CommentForm);" value="'+ previewButtonText+'"/>');
	document.write('<br/>');
		
	/* Render the link to the product */
	document.write('<span style="font-size:7pt;text-align:center;"><a title="Blogger.com integrated comment form by Avi Alkalay" href="http://avi.alkalay.net/techdumps/">Blogger.com integrated comment form by Avi Alkalay</a></span>');
	document.write('</div>');
	
	/* End form rendering */
	document.write('</form>');



	/* Fill with remembered authorship values */
	var authorCookie=getCookie('authorType');
	if (authorCookie != "") document.CommentForm.iden[authorCookie].checked=true;
	else document.CommentForm.iden[0].checked=true;
	document.CommentForm.anonName.value=getCookie('authorName');
	document.CommentForm.anonURL.value=getCookie('anonURL');
}



function commentFormStyle() {
	document.write('<style type="text/css"> \
	.commentForm { \
`		margin-left: 10px; \
		width: 400px; \
	} \
	.otherData { \
		padding-left: 30px; \
		width: 100%; \
		border: 1; \
	} \
	\
	.caption { \
		font-size:10pt; \
		text-align:center; \
		width: 400px; \
	} \
	\
	.nameBox { \
		width: 100%; \
		font-size:8pt; \
	} \
	\
	.urlBox { \
		width: 100%; \
		font-size:8pt; \
	} \
	\
.toolbarBox \
{width: 397px; \
font-size:8pt; \
padding:2px; \
} \
\
.commentBox \
{width: 400px; \
font-size:8pt;} \
 \
.buttonsDiv \
{width: 400px; \
text-align:center;} \
 \
.commentButton \
{font-size:8pt;} \
 \
.previewButton \
{font-size:8pt;} \
 \
a.toolbarButton \
{text-decoration:none; \
color: rgb(0,0,0); \
border-width: 1px; \
padding: 0px 3px 0px 3px; \
border-style: outset;} \
 \
a.toolbarButton:hover \
{text-decoration:none; \
cursor: default;} \
 \
a.toolbarButton:visited \
{color: rgb(0,0,0); \
text-decoration:none;} \
 \
a.toolbarPressed \
{text-decoration:none; \
color: rgb(0,0,0); \
border-width: 1px; \
padding: 0px 3px 0px 3px; \
cursor: default; \
border-style: inset;}\
</style>');
}

