/******************************************************************************
 *
 * File:	xmlhttp.js
 *
 * Purpose:	Contains logic to dynamically retrieve content from other
 *		HTTP locations.
 *
 * _Implemented routines / objects______Purpose________________________________
 *
 *  object HttpRequest			Wrapper class around XmlHttpRequest.
 *
 *  createSelectBoxFromUri( )		Create a SELECT, with data from web.
 *
 *  fillSelectBoxFromUri( ) 		Update SELECT by object.
 *  fillSelectBoxFromUriById( ) 	Update SELECT by ID.
 *  fillSelectBoxFromXmlDocument( ) 	Update SELECT with XML data from web.
 *
 * Author:	Navaho Gunleg
 *
 * History:
 *   $Log$
 *
 ******************************************************************************
 $Id$
 */
 
/* HttpRequest( method, uri, func, async )
 *
 *	Set up a HTTP object (XmlHttpRequest is used to do this), which can
 * communicatie with a remote site (GET, POST).
 *
 * Input:  method	GET / POST
 *	   uri		The location to GET or POST.
 *	   func		A routine function( a, b ) which will be called
 *			every time the xmlhttp.readyState changes.
 *	   async	Asynchronous or synchronous.
 *
 * Output: (none)
 *
 * Returns:	NULL	if object cannot be created.
 */
function HttpRequest( method, uri, func, async )
{
	this.state = -1;
	this.text = "";

	this.method = method;
	this.uri = uri;

	this.update = func;

	this.http = false;
	this.execute = function( ) { };

	xmlhttp = false;

/*@cc_on @*/
/*@if (@_jscript_version >= 5)
	try 
	{
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} 
	catch (e) 
	{
		try 
		{
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	  	} 
		catch (E) 
		{
			xmlhttp = false;
		} // try
	} // try
@end @*/

	if (!xmlhttp && typeof XMLHttpRequest!="undefined") 
	{
  		xmlhttp = new XMLHttpRequest();
	}

	// Test if all succeeded:
	if ( (!xmlhttp) )
	{
		document.write( "Error: could not initialize XMLHttpRequest object!" );
		return NULL;
	}

	this.http = xmlhttp;

	this.http.open( method, uri, async );
	this.http.onreadystatechange = function( ) { func( xmlhttp.readyState, xmlhttp.responseText ) };

	this.execute = function( ) { this.http.send( null ); };

} /* HttpRequest( .. ) */





function fillSelectBoxFromUri( pObject, sUri, iZap )
{
	var Requestor = new HttpRequest( "GET", sUri, null, false );
	Requestor.execute( );

	if (iZap > 0) pObject.options.length = 0;

	// We will return when the request has finished; now split
	// up the values and populate the SELECT-box.
	var raw_lines = Requestor.http.responseText.split( ';' );
	for (i=0; i < raw_lines.length; i++)
	{
		var line = raw_lines[i];
		var opts = line.split( ',' );
		if (opts[1])
		{
			pObject.options[ pObject.options.length ] = new Option( opts[1], opts[0] );
		}
	}

} /* fillSelectBoxFromUri.. */
 
function fillSelectBoxFromUriById( sId, sUri, iZap )
{
	var Object = document.getElementById( sId );
	if (Object)
	{
		fillSelectBoxFromUri( Object, sUri, iZap );

	}

} /* fillSelectBoxFromUriById */

		
function fillSelectBoxFromXmlDocument( pObject, sUri )
{
        var Http = new HttpRequest( 'GET', sUri, null, false );
        var xml = 0;
        if (Http)
        {
		// If no object has been passed, create a new
 		// SELECT object:
                if (pObject) 
		{
			pRet = pObject;

			// Reset the passed options:
			pRet.options.length = 0;
		}
		else 
		{
			pRet = documenet.createElement( 'SELECT' );

		} // if...

                // Do the HTTP request and get the result as an XML
		// document:
                Http.execute( );
                xml = Http.http.responseXML;

		// Parse the result; we're interested in the 'selectbox'
		// tree:
                x = xml.getElementsByTagName( 'selectbox' );
                for (j = 0; j < x[0].childNodes.length; j++ )
                {
                        if (x[0].childNodes[j].nodeType != 1) continue;

			// Append new option:
                        pObject.options[ pObject.options.length ] = 
				new Option( 
                        		 x[0].childNodes[j].textContent,
                        		 x[0].childNodes[j].attributes.getNamedItem( 'value' ).nodeValue 
					);

			if ((c = x[0].childNodes[j].attributes.getNamedItem( 'class' )))
				pObject.options[ pObject.options.length - 1 ].setAttribute( 'class', c.nodeValue );

			if ((s = x[0].childNodes[j].attributes.getNamedItem( 'style' )))
				pObject.options[ pObject.options.length - 1 ].setAttribute( 'style', s.nodeValue );


                } // for...

        } // Http?

	if (!pObject) return pRet;

} /* fillSelextBoxFromXmlDocument */



/* createSelectBoxFromUri( .. )
 *
 *	Creates a new select box; optionally adding it to 'pObject' -- if passed.
 * Otherwise, this routine will return the newly created SELECT-object.
 *
 */
function createSelectBoxFromUri( pObject, sUri, sName, sClass, sStyle )
{
	var selectBox = document.createElement( "SELECT" );
	if (selectBox)
	{
		selectBox.setAttribute( 'NAME', sName );
		selectBox.setAttribute( 'ID',   sName );

		selectBox.setAttribute( 'CLASS', sClass );
		selectBox.setAttribute( 'STYLE', sStyle );

		// Populate the selectbox by calling the applicable 
		// routine:
		fillSelectBoxFromUri( selectBox, sUri );

		// Now add this to the requested object; if non-null
		if (pObject)
			pObject.appendChild( selectBox );
		else
			return selectBox;

	} // if...

} /* createSelectBoxFromUri */

