
/**
	responseHTML
	(c) 2007-2008 xul.fr		
	Licence Mozilla 1.1
*/	


/**
	Searches for body, extracts and return the content
	New version contributed by users
*/


function getBody(content, newurl) 
{
   test = content.toLowerCase();    // to eliminate case sensitivity
   var x = test.indexOf("<tbody");
   if(x == -1) return "";


   x = test.indexOf(">", x);
   if(x == -1) return "";

   var y = test.lastIndexOf("</tbody>");
   if(y == -1) y = test.lastIndexOf("</html>");
   if(y == -1) y = content.length;    // If no HTML then just grab everything till end

	content = content.slice(x + 1, y);

//	content = content.replace(/the/g,newurl);
	content = content.replace(/src=\"images\//g,'src=\"' + newurl + 'images\/');
	content = content.replace(/<a href=\'javascript:Zoom\(\"images/g,'<a href=\'javascript:Zoom\(\"' + newurl + 'images');
	content = content.replace(/.php\"/g,'.php\" target=\"_blank\"');
	content = content.replace(/<a href=\"/g,'<a href=\"' + newurl + '');
//	content = content.replace(/<a href=\'javascript:Zoom\(\"images/g','<a href=\'javascript:Zoom\(\"http:\/\/www.par2pro.com\/images');
//	content = content.replace(/<a href=\"images/g,'<a href=\"http://www.par2pro.com/images\/');
//	if(content != "<a href=\"http:")

   return content
} 

/**
	Loads a HTML page
	Put the content of the body tag into the current page.
	Arguments:
		url of the other HTML page to load
		id of the tag that has to hold the content
*/		

function loadHTML(url, newurl, fun, storage, param)
{
	var xhr = createXHR();
	xhr.onreadystatechange=function()
	{ 
		if(xhr.readyState == 4)
		{
			//if(xhr.status == 200)
			{
					storage.innerHTML = getBody(xhr.responseText, newurl);
				   fun(storage, param);
			}
		} 
	}; 


	xhr.open("GET", url , true);
	xhr.send(null); 

} 

	/**
		Callback
		Assign directly a tag
	*/		


	function processHTML(temp, target)
	{
		target.innerHTML = temp.innerHTML;
	}

	function loadWholePage(url)
	{
		var y = document.getElementById("storage");
		var x = document.getElementById("displayed");

var szURL = url;
var componentList = szURL.split('/');
var szDocument = componentList[componentList.length-1];
var newurl = url.replace(szDocument,"");
//		document.write(newurl); 
//		document.write(szDocument); 
		loadHTML(url, newurl, processHTML, x, y);
	}	


	/**
		Create responseHTML
		for acces by DOM's methods
	*/	
	
	function processByDOM(responseHTML, target)
	{
		target.innerHTML = "Extracted by id:<br />";

		// does not work with Chrome/Safari
		//var message = responseHTML.getElementsByTagName("div").namedItem("two").innerHTML;
		var message = responseHTML.getElementsByTagName("div").item(1).innerHTML;
		
		target.innerHTML += message;

		target.innerHTML += "<br />Extracted by name:<br />";
		
		message = responseHTML.getElementsByTagName("form").item(0);
		target.innerHTML += message.dyn.value;
	}
	
	function accessByDOM(url)
	{
		//var responseHTML = document.createElement("body");	// Bad for opera
		var responseHTML = document.getElementById("storage");
		var y = document.getElementById("displayed");
		loadHTML(url, processByDOM, responseHTML, y);
	}	


