/*

blog.js - Copyright 2007 Satu Creative Technology

*/

function addComments(index)
{
	var html;
	html = "<div id='addCommentsDialog' class='dialogBox'>\n";
	html += "<form  class='commentsForm' id='commentsForm" + index + "'>\n";
	html += "<label for='commentName'>Your Name</label><input type='text' name='commentName' id='commentName'><br>\n";
	html += "<label for='commentURL'>URL</label><input type='text' name='commentURL' id='commentURL'><br>\n";
	html += "<label for='commentEmailAddress'>Email</label><input type='text' name='commentEmailAddress' id='commentEmailAddress'><br>\n";
	html += "<label for='commentText'>Comments</label><textarea name='commentText' id='commentText'></textarea><br>\n";
	html += "<input type='hidden' name='blogEntry' value='" + index + "'>\n";
	html += "<label for='butAdd'>&nbsp;</label><input id='butAdd' type='button' type='submit' value='Add' onclick='commentThis(" + index + ");'>";
	html += "</form>";
	html += "</div>";
	dialogSpace = document.getElementById("blogEntryDialogSpace" + index);
	dialogSpace.innerHTML = html;
	pageTracker._trackPageview("/js_addComments_" + index );
}

function viewComments(index)
{
	var url = "/blog/ajaxViewComments.php";
	
	if (window.ActiveXObject) // for IE
	{
	    httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else if (window.XMLHttpRequest) // for other browsers
	{
	    httpRequest = new XMLHttpRequest();
            if (httpRequest.overrideMimeType) 
		httpRequest.overrideMimeType('text/xml');
	   
	}
	
	ajaxBlogEntryId = encodeURI(index);
	
	var txt = "?blogEntryId=" + ajaxBlogEntryId;
	
	httpRequest.onreadystatechange = function() {ajaxRequestHandler(httpRequest, 'viewComments',index);};
	pageTracker._trackPageview("/js_viewComments_"+index );
	httpRequest.open("GET",url+txt, true);
	httpRequest.send(null);
	
}

function sendToFriend(index)
{
	html = "<div id='shareDialog' class='dialogBox'>\n";
	html += "<form class='shareBlogEntryForm' id='shareBlogEntryForm" + index + "' >\n";
	html += "<label for='txtYourName'>Your Name</label><input type='text' name='txtYourName'><br>\n";
	html += "<label for='txtTheirName'>Send to Name</label><input type='text' name='txtTheirName'><br>\n";
	html += "<label for='txtEmailAddress'>Send to Address</label><input type='text' name='emailAddress'><br>\n";
	html += "<input type='hidden' name='blogEntry' value='" + index + "'><br>\n";
	html += "<label for='butAdd'>&nbsp;</label><input id='butSend' type='button' type='submit' value='Send' onclick='sendThis(" + index + ");'><br>\n";
	html += "</form>\n";
	html += "</div>\n";
	pageTracker._trackPageview("/js_sendtoFriend_" + index );
	dialogSpace = document.getElementById("blogEntryDialogSpace" + index);
	dialogSpace.innerHTML = html;
}

function sendThis(index)
{
	shareBlogEntryForm = document.getElementById("shareBlogEntryForm" + index);
	receiverName = shareBlogEntryForm.txtTheirName.value;
	senderName = shareBlogEntryForm.txtYourName.value;
	receiverEmail = shareBlogEntryForm.emailAddress.value;
	pageTracker._trackPageview("/js_sendThis_" + index );
	sendAjaxEmailRequest(senderName, receiverName, receiverEmail, index);
}

function commentThis(index)
{
	commentsForm = document.getElementById("commentsForm" + index);
	commentName = commentsForm.commentName.value;
	commentURL = commentsForm.commentURL.value;
	commentText = commentsForm.commentText.value;
	commentEmail = commentsForm.commentEmailAddress.value;
	pageTracker._trackPageview("/js_commentThis_" + index );
	sendAjaxCommentRequest(commentName, commentURL, commentEmail, commentText, index);
}

function sendAjaxCommentRequest(commentName, commentURL, commentEmail, commentText, blogEntryId)
{
	var url = "/blog/ajaxComments.php";
	
	if (window.ActiveXObject) // for IE
	{
	    httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else if (window.XMLHttpRequest) // for other browsers
	{
	    httpRequest = new XMLHttpRequest();
            if (httpRequest.overrideMimeType) 
		httpRequest.overrideMimeType('text/xml');
	   
	}
	ajaxCommentName = encodeURI(commentName);
	ajaxCommentText = encodeURI(commentText);
	ajaxCommentURL = encodeURI(commentURL);
	ajaxCommentEmail = encodeURI(commentEmail);
	ajaxBlogEntryId = encodeURI(blogEntryId);
	
	var txt = "?commentName=" + ajaxCommentName + "&commentText=" + ajaxCommentText + "&commentURL=" + ajaxCommentURL + "&commentEmail=" +  ajaxCommentEmail + "&blogEntryId=" + ajaxBlogEntryId;
	
	httpRequest.onreadystatechange = function() {ajaxRequestHandler(httpRequest, 'comment',blogEntryId);};
	pageTracker._trackPageview("/js_sendAjaxCommentRequest_" + index );
	httpRequest.open("GET",url+txt, true);
	httpRequest.send(null);
}

function sendAjaxEmailRequest(senderName, receiverName, receiverEmail, blogEntryId)
{
	var url = "/blog/ajaxEmail.php";
	
	if (window.ActiveXObject) // for IE
	{
	    httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
	}
	else if (window.XMLHttpRequest) // for other browsers
	{
	    httpRequest = new XMLHttpRequest();
            if (httpRequest.overrideMimeType) 
		httpRequest.overrideMimeType('text/xml');   
	}
	
	ajaxSenderName = encodeURI(senderName);
	ajaxReceiverName = encodeURI(receiverName);
	ajaxReceiverEmail = encodeURI(receiverEmail);
	ajaxBlogEntryId = encodeURI(blogEntryId);
	
	var txt = "?senderName=" + ajaxSenderName + "&receiverName=" + ajaxReceiverName + "&receiverEmail=" + ajaxReceiverEmail + "&blogEntryId=" + ajaxBlogEntryId;
	
	httpRequest.onreadystatechange = function() {ajaxRequestHandler(httpRequest, 'email', blogEntryId);};
	pageTracker._trackPageview("/js_sendAjaxEmailRequest_" + index );
	httpRequest.open("GET",url+txt, true);
	httpRequest.send(null);
}

function ajaxRequestHandler(httpRequest, type, index)
{
	pageTracker._trackPageview("/js_ajaxRequestHandler_" + index + "_type_" + type);
	if(httpRequest.readyState == 4)
	{
		if(httpRequest.status == 200)
		{
			var xmlDoc = httpRequest.responseXML;
			
			
			if(type=='email')
			{
				var message = xmlDoc.getElementsByTagName("message");
				emailForm = document.getElementById("shareBlogEntryForm" + index);
				emailForm.innerHTML = message[0].firstChild.nodeValue;
			}
			else if(type=='comment')
			{
				var message = xmlDoc.getElementsByTagName("message");
				commentsForm = document.getElementById("commentsForm" + index);
				commentsForm.innerHTML = message[0].firstChild.nodeValue;
			}
			else if(type=='viewComments')
			{
				commentsSpace = document.getElementById("blogEntryDialogSpace" + index);
				var comments = xmlDoc.getElementsByTagName("comments");
				html = "";
				
				for(i=0;i<comments[0].childNodes.length;i++)
				{
					
				comment = comments[0].childNodes[i];
				html += "<hr class='hrBlogComment'/>\n";
				html += "<span class='blogCommentsText'>" + comment.childNodes[1].firstChild.nodeValue + "</span><br><br>\n";
				html += "<span class='blogCommentsAuthorDetails'>Posted by "
				if(comment.childNodes[2] != null)
				html += "<a href='" + comment.childNodes[2].firstChild.nodeValue + "' target='_new'>";
				html += comment.childNodes[0].firstChild.nodeValue 
				if(comment.childNodes[2] != null)
				html += "</a>\n";
				html += " on " + comment.childNodes[4].firstChild.nodeValue + "</span><br>";
				if(comment.childNodes[3] != null)
				html += "<span class='blogCommentEmail'>Email: " + comment.childNodes[3].firstChild.nodeValue + "</span><br>";
				}
				commentsSpace.innerHTML = html;
			}
		}
	}
}

function cancelDialog(index)
{
	pageTracker._trackPageview("/js_cancelDialog_" + index);
	dialog = document.getElementById("blogEntryDialogSpace" + index);
	dialog.innerHTML = "";
}

function switchArchive()
{
	pageTracker._trackPageview("/js_switchArchive" );
	if(window.XMLHttpRequest)
	var archiveSelect = document.getElementById("archiveSelect");
	//alert(archiveSelect);
	archiveNumber = archiveSelect.options[archiveSelect.selectedIndex].value;
	document.location.href = "/blog/index.php?monthYear=" + archiveNumber;
}

