function CreateLinkToPostTableBegin()
{
	var id = null;

	if ((arguments.length >= 0)&&(arguments[0] != null)&&(arguments[0].toString().length > 0)) {
		id = arguments[0].toString();
	}
	
	var sb = new String();
	sb += "<table border=\"0\" cellpadding=\"1\" cellspacing=\"1\"";
	if ((id != null)&&(id.length > 0)) {
		sb += " id=\"LinkToPostTable" + id + "\"";
	}
	sb += "><tr>";
	
	document.write(sb);
}

function CreateLinkToPostTableEnd()
{
	document.write("</tr></table>");
}

function CreateLinkToPostTableCell(postId, imgUrl, urlToken, toolTip)	//onClick, labels
{
	document.write("<td>");
	CreateLinkToPost(postId, imgUrl, urlToken, toolTip, arguments[4], arguments[5]);
	document.write("</td>");
}





function CreateLinkToPost(postId, imgUrl, urlToken, toolTip)	//onClick, labels
{
	var onClick = null;
	var labels = null;

	if ((arguments.length >= 4)&&(arguments[4] != null)&&(arguments[4].toString().length > 0)) {
		onClick = arguments[4].toString();
	}
	if ((arguments.length >= 5)&&(arguments[5] != null)&&(arguments[5].toString().length > 0)) {
		labels = arguments[5].toString();
	}
	
	var info = new BlogItemInfo(postId);
	var sb = new String();
	
	var sUrl = new String();
	var sToolTip = new String();
	var sImgUrl = new String();
	var sOnClick = new String();
	
	sUrl = ReplaceBlogInfoTokens(urlToken, info, true, false);
	sToolTip = ReplaceBlogInfoTokens(toolTip, info, false, false);
	sImgUrl = ReplaceBlogInfoTokens(imgUrl, info, false, false);
	sOnClick = ReplaceBlogInfoTokens(onClick, info, true, true);
	
	sb += "<a";
	if (sUrl.length > 0) {
		sb += " href=\"" + HtmlEncode(sUrl) + "\"";
	}
	if (sToolTip.length > 0) {
		sb += " title=\"" + HtmlEncode(sToolTip) + "\"";
	}
	if (sOnClick.length > 0) {
		sb += " onclick=\"" + sOnClick + "\"";
	}
	sb += ">";
	
	if (sImgUrl.length > 0) {
		sb += "<img";
		sb += " src=\"" + HtmlEncode(sImgUrl) + "\"";
		sb += " border=\"0\"";
		sb += " hspace=\"0\"";
		sb += " vspace=\"0\"";
		if (sToolTip.length > 0) {
			sb += "alt=\"" + HtmlEncode(sToolTip) + "\"";
		} else {
			sb += "alt=\"Link to this post\"";	//Keep XHTML-compliance
		}
		sb += " />";
	} else {
		if (sToolTip.length > 0) {
			sb += sToolTip;
		} else {
			sb += "Link to this post";
		}
	}
	
	sb += "</a>";
	document.write(sb);
}


function BlogItemInfo(postId)
{
	this.PostId = new String();
	this.Url = new String();
	this.Title = new String();
	
	if (postId != null) {
		this.PostId = postId.toString();
		this.Title = GetBlogItemTitle(this.PostId);
		this.Url = GetBlogItemUrl(this.PostId);
	}
	
	return this;
}

function GetBlogItemUrl(postId)
{
	var sUrl = new String();
	var htmlObj = GetElementById("hidBlogItemUrl" + postId);
	if (htmlObj != null) {
		sUrl = htmlObj.value;
	}
	if (sUrl.length == 0) {
		htmlObj = GetElementById("BlogItemUrl" + postId);
		if (htmlObj != null) {
			sUrl = Trim(GetInnerText(htmlObj));
		}
	}
	
	return sUrl;
}


function GetBlogItemTitle(postId)
{
	var sTitle = new String();
	var htmlObj = GetElementById("hidBlogItemTitle" + postId);
	if (htmlObj != null) {
		sTitle = htmlObj.value;
	}
	if (sTitle.length == 0) {
		htmlObj = GetElementById("BlogItemTitle" + postId);
		if (htmlObj != null) {
			sTitle = Trim(GetInnerText(htmlObj));
		}
	}
	
	return sTitle;
}

function ReplaceBlogInfoTokens(value, info, urlEncode, scriptEncode)
{
	var sValue = new String();
	if ((value != null)&&(value.toString().length > 0)) {
		var sTemp = new String();
		sTemp = value.toString();
		
		var parsed = new Array();
		var currentIndex = new Number(0);
		var indexOfLeftBrace = new Number(-1);
		var indexOfRightBrace = new Number(-1);
		var bContinue = new Boolean(true);
		while (bContinue == true) {
			indexOfRightBrace = -1;
			indexOfLeftBrace = sTemp.indexOf("{", currentIndex);
			if (indexOfLeftBrace < 0) {
				parsed[parsed.length] = sTemp.substring(currentIndex, sTemp.length);
				bContinue = false;
			} else {
				indexOfRightBrace = sTemp.indexOf("}", indexOfLeftBrace);
				if (indexOfRightBrace > indexOfLeftBrace) {
					parsed[parsed.length] = sTemp.substring(currentIndex, indexOfLeftBrace);
					parsed[parsed.length] = sTemp.substring(indexOfLeftBrace, indexOfRightBrace+1);
					currentIndex = indexOfRightBrace + 1;
					if ((indexOfRightBrace+1) == sTemp.length) {
						bContinue = false;
					}
				}
			}
		}
		
		for (i=0;i<parsed.length;i++) {
			var transform = new Boolean(true);
			var token = parsed[i].toString();
			var replacedValue = new String();
			
			if (parsed[i].toString().toLowerCase() == "{blogitemurl}") {
				replacedValue = info.Url;
			} else if (parsed[i].toString().toLowerCase() == "{blogitemtitle}") {
				replacedValue = info.Title;
			} else if (parsed[i].toString().toLowerCase() == "{postid}") {
				replacedValue = info.PostId;
			} else {
				replacedValue = parsed[i];
				transform = false;
			}

			if (transform == true) {
				if (urlEncode == true) {
					replacedValue = BlogUrlEncode(replacedValue);
				}
				if (scriptEncode == true) {
					replacedValue = JavaScriptEncode(replacedValue);
				}
			}
			
			sValue += replacedValue;
		}
		
	}
	return sValue;
}


function BlogUrlEncode(value)
{
	if (encodeURIComponent != undefined) {
		return encodeURIComponent(value);
	} else if (encodeURI != undefined) {
		return encodeURI(value);
	} else if (escape != undefined) {
		return escape(value);
	}
}