
function site_root(){
	/* http:// not required */
	/* return "localhost/~jeroen/www.happymakinggames.com/"; */
	return "http://www.happymakinggames.com/";
}


function checkRadioItem(elname, idx)
{
	// find way of catching click on button, then just return
	
	
	group = document.getElementsByName(elname);
	if(group[idx].checked == true)
		group[idx].checked = false;
	else
		group[idx].checked = true;
}

function getX( el )
{
	var iReturnValue = 0;
	while( el != null ) 
	{
		iReturnValue += el.offsetLeft;
		el = el.offsetParent;
	}
	return iReturnValue;
}

/*

for each element
	get any style tag (mark these, they need to be removed and replaced with css)
	get any class
	get any id
	
in the css find each unused element and "strip" using commenting


// traverses through each child node from the passed rootNode and checks some CSS
// so in the body tag you could do this: <body id="someid" class="someclass" onload="checkCSS(this);"> and it would traverse all the 
// nodes in the doc.

//perhaps we can use this in a ajax style to check a webpage returned.
// in this case we will need some other function to let people submit an address.
// we need to take the references  css files from the submitted file (just look for the 
	<link rel='stylesheet' href='SOME_CSS_FILE' type='text/css' media='screen' />)
*/

var global_styles = 0;



function parseHTMLAsText()
{
//	var headString = getFront(loadedPage, "<body>");
	
}

function replaceDocument()
{
	global_styles = 0;
	
//	var myDoc = new document("test");
//	alert(myDoc.innerText);

//	var loadedPage = document.getElementById("loadedPage");
//	loadedPage.innerHTML = loadWebPage("http://localhost/jeroen/www.happymakinggames.com/index.php");

	var loadedDocument = loadWebPage("http://localhost/jeroen/www.happymakinggames.com/index.php");
	//alert( getFront(loadedDocument, "<body") );

	// get the <head> part of the page (ie: everything before the <body> tag starts).
	var headOfPage = getFront(loadedDocument, "<body");






	var stylesArray = new Array();
	var linkTagStart = 0;
	var linkTagEnd = 0;
	var linkTag;
	var linkTags = 0;
	
	while(linkTagStart > -1)
	{
		linkTagStart = headOfPage.indexOf("<link");
		// next line gets the text from the "<link" until the end of the source
		headOfPage = headOfPage.substring(headOfPage.indexOf("<link"), headOfPage.length);
		// now find the index of the "/>" to get he full link tag only
		linkTagEnd = headOfPage.indexOf("\>") + 2;
		linkTag = headOfPage.substring(0, linkTagEnd);
		headOfPage = headOfPage.substring(linkTagEnd, headOfPage.length);
		if(linkTag.indexOf("stylesheet") > -1)//( linkTagStart > -1 )//
		{
			var cssStart = linkTag.indexOf("href=") + 6; 
			linkTag = linkTag.substring(cssStart, linkTag.length);
			var cssEnd = linkTag.indexOf('"');
			if(cssEnd < 0)
				cssEnd = linkTag.indexOf("'");
			alert(cssEnd);
			linkTag = linkTag.substring(0,cssEnd);
			alert(linkTag);
			stylesArray.push(linkTag);
			alert(stylesArray[linkTags]);
			linkTags ++;
		}	
	}





//	alert(headOfPage);

	// now we need to find the end for this "<link" tag
	//var linkedFilesStart = headOfPage.indexOf("< />");
	
	// now find each "<link" with "stylesheet" inside

//<link rel="stylesheet" href="standard-tags.css" type="text/css" media="screen" />


//document.getElementById("loadedPage").innerHTML = loadWebPage("http://localhost/jeroen/www.happymakinggames.com/index.php");

//	alert(loadedPage.innerHTML);

	// var stylesheets = "ALL LINKED STYLESHEETS:" + loadedPage.styleSheets.length + "\n\n";
	// for(i = 0; i < loadedPage.styleSheets.length; i++)
	// 	stylesheets += "<style rel='stylesheet' type='text/css' media='screen'>" + loadStylesheet(loadedPage.styleSheets[i].href) + "</style>\n";
	// 
	// var newtext = "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"><html><head>\n" +stylesheets + "</head><body>" + 
	//  					checkCSS(document.body, 0) + "\n\n\nSTYLES=" + global_styles + "\n</body></html>";
	// document.getElementById('doc').innerText = newtext;

//	document.getElementById('parsedPage').innerText = checkCSS(loadedPage, 0);
	
//	document.getElementById('parsedPage').style.color = "black";
//	document.getElementById('parsedPage').style.backgroundColor = "white";
	
	
	// now the document contains the styles and styled tags only.
	
}




// if the url requires parameters then they should be enetered as part of the string passed to this function
function loadWebPage(url)
{
//alert("getting webpage");
	var req = new XMLHttpRequest();
	req.open("GET", url, false);
	req.send(null);
//alert(req.responseText);
	return req.responseText;
}





// if the url requires parameters then they should be enetered as part of the string passed to this function
function loadStylesheet(url)
{
	var req = new XMLHttpRequest();
	req.open("GET", url, false);
	req.send(null);
	return req.responseText;
}




function checkCSS(rootNode, n)
{
// alert("start checkCSS");
	var output = "";
	var indent = "";
	var i, childNodes;
	n = 0;
	for(i = 0; i < n; i++)
	{
		indent = indent + "  ";	// 2 spaces for each indent level
	}
	
	childNodes = rootNode.children;
	
	for (i = 0; i < childNodes.length; i++) 
	{
		if(childNodes[i].id || childNodes[i].className || childNodes[i].getAttribute('style'))
		{
			output += indent + "<" + childNodes[i].tagName;

			if(childNodes[i].id)
				output += " id=\"" + childNodes[i].id + "\" ";
			if(childNodes[i].className)
				output += " class=\"" + childNodes[i].className + "\" ";

// if this is null then the style is provided by class or id
// if this attribute is set then the returned string contains the INLINE style attributes that were set.

			if(childNodes[i].getAttribute('style'))
			{
				output += " style=\"" + childNodes[i].getAttribute('style') + "\" ";
				// global_styles is defined globally.
				global_styles ++;
			}
		
			output += ">";
		}
		
		if (childNodes[i].children.length > 0) {
			output += checkCSS(childNodes[i], n + 1);
		}

		if(childNodes[i].id || childNodes[i].className || childNodes[i].getAttribute('style'))
		{
			output += "</" + childNodes[i].tagName + ">\n";
		}
	}

	return output;
}



//// if the url requires parameters then they should be enetered as part of the string passed to this function
//function loadSomething(url)
//{
	//var req = new XMLHttpRequest();
	//req.open("GET", url, true); // for synchronous set to false, asynchronous set to true
	//// the following function is only required if asynchronous is used
	// req.onreadystatechange = function() 
	// { 
	// 	if(req.readyState == 4) 
	// 	{ 
	// 		if(req.status == 200)
	// 		{
	// 			//return req.responseText;
	// 		}
	// 	} 
	// };
	//req.send(null);
	////return req.responseText;
//}


// extract front part of string prior to searchString 
function getFront(mainStr,searchStr)
{ 
	foundOffset = mainStr.indexOf(searchStr); 
	if (foundOffset == -1) 
		return null;
	return mainStr.substring(0,foundOffset); 
} 


// extract front part of string prior to searchString 
function getToEnd(mainStr,index)
{ 
	// foundOffset = mainStr.indexOf(searchStr); 
	// if (foundOffset == -1) 
	// 	return null;
	return mainStr.substring(index,mainStr.length); 
} 
