<!--
/*
    Xml, Xslt functions
    © Adam Lenkow 2007
*/

//--------Class XmlServices-------------
function XmlService()
{
    this.XmlHttp = this.CreateXmlHttpRequestObject();
}

// Returns false if browser doesn't serv required technologies
XmlService.prototype.TestServices = function ()
{
    if( !(window.XMLHttpRequest && window.XSLTProcessor && window.DOMParser ))
        if (!(window.ActiveXObject && CreateMsxml2DOMDocumentObject()))
            return false;
            
    return true;
}

// Creates XmlHttpRequest object
XmlService.prototype.CreateXmlHttpRequestObject = function () 
{
  var xmlHttp;
  try
  {
    // Create XMLHttpRequest object for browsers != IE<=6
    xmlHttp = new XMLHttpRequest();
  }
  catch(e)
  {
    // Check ActiveX objects for IE<=6 
    var xmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
                                     "MSXML2.XMLHTTP.5.0",
                                     "MSXML2.XMLHTTP.4.0",
                                     "MSXML2.XMLHTTP.3.0",
                                     "MSXML2.XMLHTTP",
                                     "Microsoft.XMLHTTP");
    for (var i=0; i<xmlHttpVersions.length && !xmlHttp; i++) 
    {
        try 
        { 
            xmlHttp = new ActiveXObject(xmlHttpVersions[i]);
        } 
        catch (e) {}
        }
    }
  
    if (!xmlHttp)
        throw 'Nie można utworzyć obiektu XMLHttpReqest';
        
    return xmlHttp;
}

// Only for IE
// Creates XSLT transformation object
// Mosilla like browsers use their native objects
XmlService.prototype.CreateMsxml2DOMDocumentObject = function()
{
  var msxml2DOM;
  var msxml2DOMDocumentVersions = new Array("Msxml2.DOMDocument.6.0",
    "Msxml2.DOMDocument.5.0",
    "Msxml2.DOMDocument.4.0");
  // Try to find the best object
  for (var i=0; i<msxml2DOMDocumentVersions.length && !msxml2DOM; i++) 
  {
    try 
    { 
      msxml2DOM = new ActiveXObject(msxml2DOMDocumentVersions[i]);
    } 
    catch (e) {}
  }
  if (!msxml2DOM)
    return false;
    
  return msxml2DOM;
}

// XSLT tranformation
// Writes html result into target.innerHTML
XmlService.prototype.Xml2Html = function( xslt, xml, target )
{
    try
    {    
        if (window.XMLHttpRequest && window.XSLTProcessor && window.DOMParser)
        {   
            // Start XSLT
            var xsltProcessor = new XSLTProcessor();
            xsltProcessor.importStylesheet(xslt);
            var result = xsltProcessor.transformToFragment(xml, document);
            target.innerHTML = "";
            target.appendChild(result);
          } 
          // For IE
        else if (window.ActiveXObject) 
        {
            // Start XSLT
            var theDocument = this.CreateMsxml2DOMDocumentObject();
            theDocument.async = false;
            theDocument.load(xml);
            target.innerHTML = theDocument.transformNode(xslt);
        }
        else
        {          
            throw "Nie można utworzyć obiektu transformacji XSL";  
        }
    }
    catch(e)
    {
	alert(e);
        throw "Błąd transfomacji XSLT lub brak pliku XSL ";
    }
}

// Tests serwer's response is valid
// If not - throws an exception
XmlService.prototype.TestXmlResponse = function ()
{
    response = this.XmlHttp.responseText;
    if (response.indexOf("ERRNO") >= 0 || response.indexOf("error") >= 0 || response.length == 0)
        throw response.length == 0 ? "Brak odpowiedzi serwera" : response;
 
    if ( !this.XmlHttp.responseXML )
        throw 'Brak odpowiedzi XML';

}

// Tests XMLHttpRequest object state before calling
// If XMLHttpRequest is ready returns true
XmlService.prototype.Ready = function()
{
    if (this.XmlHttp && (this.XmlHttp.readyState == 4 || this.XmlHttp.readyState == 0))
        return true;
    else
        return false;
}

// Tests answer of async calling with XMLHttpRequest
// If ok returns true
// Not ready returns false
// Error - throws exception
XmlService.prototype.Answer = function()
{
    if (this.XmlHttp.readyState != 4)
        return false;
        
    if (this.XmlHttp.status != 200)
        throw "Błąd podczas odczytu odpowiedzi z serwera";
    
    return true;
}
-->
