/***************************************************************************
*                            Barracuda Collaborative Directory Software
*                              -----------------
*     begin                : Mon Mar 23 2006
*     copyright            : (C) 2006 BoonEx Group
*     website              : http://www.boonex.com
* This file is part of Barracuda - Collaborative Directory Software
*
* Barracuda is free software; you can redistribute it and/or modify it under 
* the terms of the GNU General Public License as published by the 
* Free Software Foundation; either version 2 of the 
* License, or  any later version.      
*
* Barracuda is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details. 
* You should have received a copy of the GNU General Public License along with Barracuda, 
* see license.txt file; if not, write to marketing@boonex.com
***************************************************************************/
/**
 * xml/xsl transformation
 *
 * methods:
 *
 * properties:
 */

/**
 * constructor
 *		url_xml	- url with xml data to open
 *		url_xsl	- url with xsl data to merge with xml
 *		h		- user handler function
 */
function BxXslTransform(url_xml, url_xsl, url_r, h)
{	
	var r_xsl;
	var r_xml;
	var no_xsl = 0;
	var browserType;


	if (!window.ActiveXObject && !window.XSLTProcessor)
	{
		if (url_xml.indexOf ("?") == -1)
			url_xml += "?trans=1";
		else
			url_xml += "&trans=1";
		no_xsl = 1;
	}

	// xml load handler
	var h_xml = function (r)
	{
		if(browserType == 'opera')
		{
			h(r.responseText);
			return false;
		}

		if (r)  // Mozilla
		{
			r_xml = r;
		}

		if (r_xml.readyState == 4) // IE
		{
			if (200 == r_xml.status || (r_xml.parseError && !r_xml.parseError.errorCode))
			{
//				new BxError("xml load failed", r_xml.parseError.reason);
				if ((r_xsl && r_xsl.readyState == 4) || no_xsl)
					h_res (r_xml, r_xsl);
			}
		}
	}

	// xsl load handler
	var h_xsl = function (r)
	{
		if (r) // Mozilla
		{
			r_xsl = r;
		}
		
		if (r_xsl.readyState == 4) // IE
		{
			if (200 == r_xsl.status || (r_xsl.parseError && !r_xsl.parseError.errorCode))
			{
//				new BxError("xsl load failed", r_xsl.parseError.reason);
				if (r_xml && r_xml.readyState == 4) 
					h_res (r_xml, r_xsl);
			}
		}
	}


	// it fires after both (xml and xsl handlers) functions called
	var h_res = function (r_xml, r_xsl)
	{
	    var f;


		// IE
	    if(window.ActiveXObject)
		{
			try
			{
		        f = r_xml.transformNode (r_xsl);
			}
			catch (e)
			{
				var ee = new BxError(e.message, e.description);
			}
		}
		// Mozilla
	    else if (window.XSLTProcessor)
		{

	        var x = new XSLTProcessor();
		    x.importStylesheet(r_xsl.responseXML);
	        var ff = x.transformToFragment(r_xml.responseXML, window.document);

			if (XMLSerializer)
			{
				f = ((new XMLSerializer()).serializeToString(ff));
			}
			else
				new BxError("xml serialization failed", "please upgrade your browser");
		}
	    else
		{
			//	var e = new BxError("xslt transformation failed", "please upgrade your browser");
			if (XMLSerializer)
			{
				f = ((new XMLSerializer()).serializeToString(r_xml.responseXML));
			}
			else
				new BxError("xml serialization failed", "please upgrade your browser");
		}

		// call user defined handler function
		h (f);		
	}


	// IE
	if(window.ActiveXObject)
	{

		var b = new ActiveXObject("MSXML2.DOMDocument");
		r_xml = b;
		b.async = true;
		b.load (url_xml);
		b.onreadystatechange = h_xml;

		b = new ActiveXObject("MSXML2.DOMDocument");
		r_xsl = b;
		b.async = true;
		b.load (url_xsl);
		b.onreadystatechange = h_xsl;
	}
	// Mozilla
	else if (window.XSLTProcessor)
	{
		new BxXmlRequest (url_xml, h_xml);
		new BxXmlRequest (url_xsl, h_xsl);
	}
	// other browsers
	else
	{
		browserType = 'opera';
		new BxXmlRequest (url_xml, h_xml);
	}


}   
