/*
 * ajax.js
 */

//
// Classes in Javascript
//

Array.from = function( arr ) {
  if ( !arr ) return [];
  if ( arr.toArray ) 
  {
    return arr.toArray();
  }
  else 
  {
    var results = [];
    for ( var i = 0; i < arr.length; i++ )
    {
      results.push( arr[ i ] );
    }
    return results;
  }
}

Function.prototype.bind = function() {
  var __method = this, args = Array.from(arguments), object = args.shift();
  return function() 
  {
    return __method.apply(object, args.concat(arguments));
  }
}


//
// Class XMLHttpRequester.
//
// Parameters:
//   url - the url to retrieve
//   xslurl - the url of an xslt file for transforming the contents of url
//   domid - the id of a DOM node to receive the result.
//
// The xslurl may be null. In that case no transformation is made.
// If xslurl is not null the xslt file is retrieved first.
// 
// Call execute to execute the request.
//
// Usage:
//   aURL = 'http://www.somewhere.com/data.xml';
//   aXSL = 'http://www.somewhere.com/data2html.xsl';
//   req = new XMLHttpRequester( aURL, aXSL, 'datadiv' );
//   req.execute();
//

XMLHttpRequester.prototype = new Object();

function XMLHttpRequester( url, xslurl, domid )
{
  //
  // Setup the class (prototype is like a vtable)
  //

  // Create a new request-object. Hides browser differences
  XMLHttpRequester.prototype.getRequest = XMLHttpRequesterGetRequest;
  XMLHttpRequester.prototype.getXSLTProcessor = XMLHttpRequesterGetXSLTProcessor;

  // Executes the XMLHttpRequester object.
  XMLHttpRequester.prototype.setMethod = XMLHttpRequesterSetMethod;
  XMLHttpRequester.prototype.execute = XMLHttpRequesterExecute;

  // Initiates the URL request after the XSL request has completed
  XMLHttpRequester.prototype.doURLRequest = XMLHttpRequesterURLReq;
  XMLHttpRequester.prototype.doXSLTransform = XMLHttpRequesterXSLTransform;

  //XMLHttpRequester.prototype.requestDone = XMLHttpRequesterRequestDone;
  //XMLHttpRequester.prototype.XslRequestDone = XMLHttpRequesterXslRequestDone;

  //
  // members
  //
  var theURL = url;
  var theXSLURL = xslurl;
  var theDOMId = domid;
  var theRequest;
  var theXSLDoc = null;
  var xsltProcessor = null;
  var theMethod = "GET";

  //
  // Implementation
  //
  function XMLHttpRequesterGetRequest()
  {
    if ( window.XMLHttpRequest ) // In Mozilla, Safari, and IE7+...
    { 
      return new XMLHttpRequest();
    }
    else if ( window.ActiveXObject ) // IE6...
    {
      return new ActiveXObject( "Microsoft.XMLHTTP" );
    }
  }

 function XMLHttpRequesterGetXSLTProcessor( xsltdoc )
  {
    if ( window.ActiveXObject ) // IE6+...
    {
      /*
      doc = new ActiveXObject( "Microsoft.XMLDOM" );
      doc.async = false;
      doc.loadXML( xsltdoc );
      */
      return xsltdoc;
    }
    else if ( window.XMLHttpRequest ) // In Mozilla, Safari, ...
    { 
      processor = new XSLTProcessor();
      processor.importStylesheet( xsltdoc );
      return processor;
    }
  }

  function XMLHttpRequesterXSLTransform()
  {
    var elem = document.getElementById( theDOMId );

    if ( elem )
    {
      elem.innerHTML = "";

      if ( window.ActiveXObject )
      {
        theDoc = theRequest.responseXML;
        /*
        alert ( theDoc );
        alert ( xsltProcessor );
        alert ( theDoc.getDocument );
        alert ( xsltProcessor.document );
	*/
        elem.appendChild( theDoc.transformNode( xsltProcessor ) );
      }
      else /* if ( window.XMLHttpRequest ) */
      { 
        var result = xsltProcessor.transformToFragment( theRequest.responseXML, document );
        elem.appendChild( result );
      }

    }
  }

  var resText = null;

  function XMLHttpRequesterRequestDone()
  {
    if ( theRequest.readyState == 4 ) 
    {
      if ( theRequest.status == 200 ) 
      {
        if ( theXSLURL )
        {
          this.doXSLTransform(); 
        }
        else if ( theDOMId )
        {
	  e = document.getElementById( theDOMId );
          if ( e != null )
	  {
            e.innerHTML = theRequest.responseText;
	  }
	  else
	  {
            // assume it's a function
            resText = theRequest.responseText;
            eval( theDOMId+ "( resText )" );
	  }
        }
        theRequest = null;
      } 
      else 
      {
      }
    } 
    else 
    {
    }
  }

  function XMLHttpRequesterURLReq()
  {
    theRequest = this.getRequest();
    // Override the header sent by the server, just in case it's not text/xml.
    // theRequest.overrideMimeType( 'text/xml' );

    theRequest.onreadystatechange = XMLHttpRequesterRequestDone.bind( this );

    //theRequest.open( 'GET', theURL, true );
    theRequest.open( theMethod, theURL, true );
    theRequest.send( null );
  }

  function onXslLoad()
  {
    xsltProcessor = this.getXSLTProcessor( theXSLDoc ); // new XSLTProcessor();
    // theXSLDoc = null;

    this.doURLRequest();
  }

  function XMLHttpRequesterSetMethod( mstr )
  {
    theMethod = mstr;
  }

  function XMLHttpRequesterExecute()
  {
    if ( theXSLURL )
    {
      if ( window.XSLTProcessor )
      {
        // support Mozilla/Gecko based browsers
        theXSLDoc = document.implementation.createDocument("", "", null);
        theXSLDoc.addEventListener( "load", onXslLoad.bind( this ), false );
        theXSLDoc.load( theXSLURL );
      }
      else
      {
	//theXSLDoc = new ActiveXObject( "Microsoft.XMLDOM" );
        theXSLDoc = new ActiveXObject( "MSXML2.DOMDocument" );

        theXSLDoc.async = true;
        theXSLDoc.onreadystatechange = onXslLoad.bind( this );
        theXSLDoc.loadXML( theXSLURL );
      }
    }
    else
    {
      this.doURLRequest();
    }
  }
}

RepeatingXMLHttpRequester.prototype = new XMLHttpRequester();
function RepeatingXMLHttpRequester( url, xslurl, domid, interval )
{
  RepeatingXMLHttpRequester.prototype.start = RepeatingXMLHttpRequesterStart;

  XMLHttpRequester( url, xslurl, domid );
  var theInterval = interval;
  var theTimer;

  function RepeatingXMLHttpRequesterStart()
  {
    this.execute();
    theTimer = setTimeout( RepeatingXMLHttpRequesterTimeout.bind( this ), theInterval*1000 );
  }

  function RepeatingXMLHttpRequesterTimeout()
  {
    clearTimeout( theTimer );
    this.start();
  }
}

/*****************************************************************************/

//var updreq;
function update()
{
  url = 'http://www.hitmand.dk/update-request.php';
  updreq = new RepeatingXMLHttpRequester( url, null, 'tempdiv', 5 );
  updreq.start();
}


function update_xml()
{
   url = 'http://www.hitmand.dk/xml-request.xml';
   xsl = 'http://www.hitmand.dk/xml.xsl';
   updreq = new XMLHttpRequester( url, xsl, 'xmldiv' );
   updreq.execute();
}
