/* Expand Comments version 0.6
 *  Copyright 2006 Andrew Rader
 *
 * This file is part of Expand Comments
 *
 * Expand Comments is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.

 * Expand Comments is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Expand Comments; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

// makeAjaxRequest is taken from www.sitepoint.com/article/take-command-ajax
// which is taken from other various tuts. Sharing is caring.
// For documentation's sake, callback is a function that takes
// a single argument. This argument holds what is returned from
// the server, as XML of retXML is true, or as Text if not

function makeAjaxRequest(url, callback, retXML)
{
	var http_request = false;

	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/xml');
		}
	}
	else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}

	if (!http_request) {
		//alert('Unfortunatelly you browser doesn\'t support this feature.');
		return false;
	}

	http_request.onreadystatechange = function() {
		if (http_request.readyState == 4) {
			if (http_request.status == 200) {
				if (retXML) {
					eval(callback + '(http_request.responseXML)');
				}
				else {
					eval(callback + '(http_request.responseText)');
				}
			}
		}
	}

	/* This RANDOM query is so the page isn't cached */
	url += "&RANDOM=" + (Math.random() * Date.parse(new Date()))
	http_request.open('GET', url, true);
	http_request.send(null);
}

/*
 * handleAsyncRed: Handle the return call of the call to 'makeAjaxRequest'
 */
function handleAsyncRet( retVal ) {
	/* The XML Doc */
	xmlDoc = retVal.documentElement;

	/* Use the global theID to find the div target */
	if( theID >= 0 ) {
		appendDiv = document.getElementById( "expComDiv-" + theID );
		theID = -1;

		/* Loop through the entries from the XML */
		entries = xmlDoc.getElementsByTagName( "comment" );
		for( i = 0; i < entries.length; i++ ) {
			/* Create the container for this single comment */
			comment = document.createElement( "div" );
			if( i % 2 == 0 ) {
				comment.className = "";
			}
			else {
				comment.className = "alt";
			}

			/* Create the Author & Link */
			authLinkNode = entries[i].getElementsByTagName("comment_author_url")[0];
			/* Can we get a link out of it? */
			if( authLinkNode.firstChild && authLinkNode.firstChild.data != "http://" ) {
				authCite = document.createElement( "cite" );

				authLink = authLinkNode.firstChild.data;
				auth = document.createElement("a");
				auth.href = authLink;
				auth.innerHTML = entries[i].getElementsByTagName("comment_author")[0].firstChild.data;

				authCite.appendChild( auth );	
			}
			else {
				authCite = document.createElement("cite");
				authCite.innerHTML = entries[i].getElementsByTagName("comment_author")[0].firstChild.data;
			}

			comment.appendChild( authCite );

			says = document.createTextNode( " says: " );
			comment.appendChild( says );

			approved = entries[i].getElementsByTagName("comment_approved")[0].firstChild.data;
			/* Is this approved? */
			if( approved == "0" ) {
				appr = document.createElement( "em" );
				appr.innerHTML = "Your comment is awaiting moderation.";
				comment.appendChild( appr );
			}

			br = document.createElement( "br" );
			comment.appendChild(br);

			/* Now the Date */
			small = document.createElement( "small" );
			small.className = "commentmetadata";

			link = document.createElement( "a" );
			link.href = entries[i].getElementsByTagName("comment_link")[0].firstChild.data;
			link.innerHTML = entries[i].getElementsByTagName( "comment_date" )[0].firstChild.data + " at " + entries[i].getElementsByTagName( "comment_time" )[0].firstChild.data;
			link.title = "";
			small.appendChild( link );

			/* Can we edit this comment? */
			canEdit = entries[i].getElementsByTagName("comment_edit")[0];
			if( canEdit.firstChild ) {
				space = document.createTextNode( " " );
				small.appendChild( space );

				edit = document.createElement("a");
				edit.href = canEdit.firstChild.data;
				edit.innerHTML = "edit";
				small.appendChild( edit );
			}

			comment.appendChild( small );

			/* Now the Comment Body */
			text = document.createElement( "div" );
			entryNode = entries[i].getElementsByTagName( "comment_content" );

			text.innerHTML = entryNode[0].firstChild.data;
			comment.appendChild( text );

			/* Append the comment block */
			appendDiv.appendChild( comment );
		}
	}
	appendLink = document.getElementById(appendDiv.id.replace("expComDiv-","expComLink-"));
	appendLink.innerHTML = expcom_col;
}
