//===========================================================================================
function runScriptsInSegment(pagecontent)   //Originally extracted from setSecureFeatures
{					//CALLED BY setSecureFeatures()

//document.write(pagecontent);
		var pageObj = document.createElement('div');
		pageObj.innerHTML = pagecontent.innerHTML;


		var scripts = pageObj.getElementsByTagName('script');
		//document.write(pagecontent.innerHTML);

		//document.write(scripts.length + ', ' + scripts[0].innerHTML);

		//document.write(scripts.length);
		//document.write(scripts[0].innerHTML);

		for(i=0;  i < scripts.length;  i++)
		{
			//document.write(scripts[i].firstChild.nodeValue	);

			//var head = document.getElementsByTagName('head')[0];
			//head.innerHTML = head.innerHTML + scripts[i].firstChild.nodeValue;

			//document.write(scripts[i].firstChild.nodeValue);


			//document.write(scripts[i].firstChild.nodeValue);
			//document.write(scripts[i].innerHTML);
			//			globaleval(scripts[i].firstChild.nodeValue);
						globaleval(scripts[i].innerHTML);
			//document.write("setSecure");
		}

}
//===========================================================================================
function globaleval(s)
{

  if(window.execScript)
  {
//document.write('<pre>'+s+'</pre>');
    window.execScript(s);
    return null;
  }
  if(window.eval)
  {
    window.eval(s);
  }
}

//===========================================================================================
function loadPageContents(pageToLoad)
{
	makeHttpRequest(pageToLoad, 'GET', null, 'setSecureFeatures', true);
}

function loadPageContentsAdv(pageToLoad, method, getstr, parameters)
{
	if(parameters != null)
	{
		if(parameters.secure)
			getstr = getstr + '&secure='+parameters.secure;
		if(parameters.unsecure)
		getstr = getstr + '&unsecure='+parameters.unsecure;
	}
	
//alert(getstr);
	makeHttpRequest(pageToLoad, method, getstr, 'setSecureFeatures', true);
}

//===========================================================================================
function setSecureFeatures(xmlData)
{
	if(xmlData)
	{
		var nodesRoot = xmlData.lastChild;
		var nodes  = nodesRoot.childNodes;
		var pagecontentName = '';

		var pagecontent = document.getElementById('pagecontent');
		if(nodes[0].tagName == 'secure')
		{
			if(nodes[1] && nodes[1].tagName == 'components')
			{
				var components = nodes[1];
				pagecontentName = components.childNodes[0].firstChild.nodeValue;
	//alert(pagecontentName);
				pagecontent = document.getElementById(pagecontentName);
			}
			//var pagecontent = document.getElementById('secureMessage');


	//****PROBLEM - if text gets too big, SimpleXML seems to turn it into 2 or MORE elements ****
	//document.write(nodes[0].childNodes.length);


			if(nodes[0].firstChild)
				var pagetext  =  nodes[0].firstChild.nodeValue;
		/* THIS IS A FIX - A HACK - PERHAPS PROBLEM IS WITH SimpleXML */
			if(nodes[0].childNodes.length > 1)
				for(i=1;  i<nodes[0].childNodes.length;  i++)
					pagetext += nodes[0].childNodes[i].nodeValue;
		/* END OF FIX / HACK ******************************************/


			pagecontent.innerHTML = pagetext;
			runScriptsInSegment(pagecontent);
		}
		else if(nodes[0].tagName == 'unsecure')
		{
			//var pagecontent = document.getElementById('unsecureMessage')


			if(nodes[0].firstChild)
				var pagetext  =  nodes[0].firstChild.nodeValue;
		/* THIS IS A FIX - A HACK - PERHAPS PROBLEM IS WITH SimpleXML */
			if(nodes[0].childNodes.length > 1)
				for(i=1;  i<nodes[0].childNodes.length;  i++)
					pagetext += nodes[0].childNodes[i].nodeValue;
		/* END OF FIX / HACK ******************************************/


			pagecontent.innerHTML = nodes[0].firstChild.nodeValue;
			runScriptsInSegment(pagecontent);

		}
		else
		{
			//document.write("An Error Occored");
		}
	}

	positionFooter();
}
//===========================================================================================
//===========================================================================================



//===========================================================================================
function getTitlePath(title)
{
	newTitle = title.replace(/ /g, '_');
	newTitle = newTitle.replace(/%20/g, '_');
	newTitle = newTitle.replace(/%27/g, '\'');
//HOW DO I GET RID OF ANY WHITE SPACE?
//HOW DO I GET RID OF ALL FUNKY CHARACTERS:  e.g. %20, %27
	return newTitle;
}


function test(str)
{
	//EXAMPLE:  test('&lt;' + nodes[0].tagName	+'>: '+	nodes[0].firstChild.nodeValue);
	var testpit = document.getElementById('testpit')
	var testpitInner = document.getElementById('testpitInner')

	testpit.style.display = 'block';
	testpitInner.innerHTML = str;


	function closeTestPit()
	{
		testpit.style.display = 'none';
	}

	xAddEventListener(testpit, "click", closeTestPit);
}

function testElement(obj)
{
	//EXAMPLE:  test('&lt;' + nodes[0].tagName	+'>: '+	nodes[0].firstChild.nodeValue);
	document.getElementById('testpit').innerHTML = '&lt;' + obj.tagName	+'>: '+	obj.firstChild.nodeValue;
}



//===========================================================================================
function ajaxifyLinks(triplet)
{
	var className, actionName, eventFunction;
	for(var i=0;  i<triplet.length;  i++)
	{
		className = triplet[i][0];
		actionName = triplet[i][1];
		eventFunction = triplet[i][2];

		var buttons = getElementsByClassName(className);
		for(btn in buttons)
			xAddEventListener(buttons[btn], actionName, eventFunction);
	}
}



function jsonConvertObject2Array(obj)
{
	if(!(obj instanceof Array))
	{
		var tmp = obj;
		obj = [];
		obj[0] = tmp;
	}

	return obj;
}

