/** Searches through HTML page, extracts and return the wanted content */

function getposts(content, dfirst, dlast) 
{
   test = content.toLowerCase();    // to eliminate case sensitivity
   var x = test.indexOf(dfirst);
   if(x == -1) return "";

   x = test.indexOf(">", x);
   if(x == -1) return "";

   var y = test.lastIndexOf(dlast);
   if(y == -1) y = test.lastIndexOf("</html>");
   if(y == -1) y = content.length;    // If no HTML then just grab everything till end

   return content.slice(x + 1, y);   
} 

/**
	Function Loads a HTML page and places the content of the body into the current page.

	Arguments:
		url of the other HTML page to load
		id of the tag that has to hold the content
*/		

function loadHTMLposts(url, fun, storage1, param1, storage2, param2)
{
	var xhr = createXHR();
	xhr.onreadystatechange=function()
	{ 
		if(xhr.readyState == 4)
		{
			/** Get the posts and store */
			//if(xhr.status == 200)
			{
				storage1.innerHTML = getposts(xhr.responseText, "<dmrecent", "</dmrecent>");
				fun(storage1, param1);
			}

			/** Get the comments and store */
			//if(xhr.status == 200)
			{
				storage2.innerHTML = getposts(xhr.responseText, "<dmcomment", "</dmcomment>");
				fun(storage2, param2);
			}  
		} 
	}; 

	xhr.open("GET", url , true);
	xhr.send(null); 

} 


/** Callback:	Assign directly a tag */		
	function processHTML(temp, target)
	{
		target.innerHTML = temp.innerHTML;
	}

/** This function calls the other functions <scripts></scripts> */
function loadRecentPosts(url)
	{
		var y1 = document.getElementById("ddmposts");
		var x1 = document.getElementById("ddmdisplayedp");

		var y2 = document.getElementById("ddmcomments");
		var x2 = document.getElementById("ddmdisplayedc"); 

		loadHTMLposts(url, processHTML, x1, y1, x2, y2);
	}	




