//REQUIRES Sarissa, MakeRequestSarissa, CreateMethodReference

function setElementFromXmlAndXslData(obj, xmlData, xslData)
{
	if(window.ActiveXObject)
	{
		obj.innerHTML = xmlData.transformNode(xslData);
	}
	else
	{
		var xsltProcessor = new XSLTProcessor();
		xsltProcessor.importStylesheet(xslData);

		var fragment = xsltProcessor.transformToFragment(xmlData, document);
		xsltProcessor.reset();

		obj.innerHTML = "";
		obj.appendChild(fragment);
	}
}



//function getDocumentFromXmlAndXslData(xmlData, xslData)
function getHTMLDocument(xmlData, xslData)
{
	var returnText = '';
	if(window.ActiveXObject)
	{
		returnText = xmlData.transformNode(xslData);
	}
	else
	{
		var xsltProcessor = new XSLTProcessor();
		xsltProcessor.importStylesheet(xslData);

		var fragment = xsltProcessor.transformToFragment(xmlData, document);
		xsltProcessor.reset();

		var temp = document.createElement('div');
		temp.appendChild(fragment);
		returnText = temp.innerHTML;
	}

	return returnText;
}






/*
function xsltUpdater(object, xmlFile, xslFile, onCompleteCustomFunction)
{
	this.object = object;
	this.xmlFile = xmlFile;
	this.xslFile = xslFile;
	this.xmlData = null;
	this.xslData = null;
	this.onCompleteCustomFunction = onCompleteCustomFunction;



	this.onXMLLoad = function(response_xml)
	{
		this.xmlData = response_xml;
		this.updateObject();
	}

	this.onXSLLoad = function(response_xml)
	{
		this.xslData = response_xml;
		this.updateObject();
	}

	this.updateObject = function()
	{
		if(this.xmlData==null || this.xslData==null)
		{ return; }

		this.object.innerHTML = getHTMLDocument(this.xmlData, this.xslData);
		//	setElementFromXmlAndXslData(this.object, xmlData, xslData);  //ALTERNATIVE
		//document.write(this.object.innerHTML);
		//runScriptsInSegment(this.object);
		onCompleteCustomFunction();
	}

	var onXML = this.onXMLLoad;
	var onXSL = this.onXSLLoad;

	makeHttpRequest2(xmlFile,
					'GET',
					'',
					onXML,
					true
	);

	makeHttpRequest2(xslFile,
					'GET',
					'',
					onXSL,
					true
	);
}
*/







function xsltUpdater(object, xmlFile, xslFile, onCompleteCustomFunction)
{
	this.object = object;
	this.xyz='hello';
	this.xmlFile = xmlFile;
	this.xslFile = xslFile;
	this.xmlData = null;
	this.xslData = null;
	this.onCompleteCustomFunction = onCompleteCustomFunction;


	makeHttpRequest2(xmlFile,
					'GET',
					'',
					this.onXMLLoad.bind(this),
					true
	);

	makeHttpRequest2(xslFile,
					'GET',
					'',
					this.onXSLLoad.bind(this),
					true
	);
}


xsltUpdater.prototype.onXMLLoad = function(response_xml)
{
//test('xml');
	this.xmlData = response_xml;
	this.updateObject();
}

xsltUpdater.prototype.onXSLLoad = function(response_xml)
{
//test('xsl');
	this.xslData = response_xml;
	this.updateObject();
}

xsltUpdater.prototype.updateObject = function()
{
	if(this.xmlData==null || this.xslData==null)
	{ return; }

	this.object.innerHTML = getHTMLDocument(this.xmlData, this.xslData);
	//	setElementFromXmlAndXslData(this.object, xmlData, xslData);  //ALTERNATIVE
	//document.write(this.object.innerHTML);
	//runScriptsInSegment(this.object);

//	alert(this.xyz);
	this.onCompleteCustomFunction();
}
