function HTTPClient() {};

// Add methods and properties as array
HTTPClient.prototype = {
    url: null,

    // Instance of XMLHttpRequest
    xmlhttp: null,

    // Used to make sure multiple calls are not placed
    // with the same client object while another in progress
    callinprogress: false,

    // The user defined handler - see MyHandler below
    userhandler: null,

    init: function(url) {
        this.url = url;
		try{
	        this.xmlhttp = new XMLHttpRequest();
		}
		catch(e){}
		if(!this.xmlhttp){
			this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
			
    },

    // handler argument is a user defined object to be called
    asyncGET: function (handler) {

        // Prevent multiple calls
        if (this.callinprogress) {
            throw "Call in progress";
        };

        this.userhandler = handler;

        // Open an async request - third argument makes it async
        this.xmlhttp.open('GET',this.url,true);

        // Have to assign "this" to a variable - not sure why can't use directly
        var self = this;

        // Assign a closure to the onreadystatechange callback
        this.xmlhttp.onreadystatechange = function() {
            self.stateChangeCallback(self);
        }

        // Send the request
        this.xmlhttp.send(null);
    },

    stateChangeCallback: function(client) {
        switch (client.xmlhttp.readyState) {

            // Request not yet made
            case 1:
                try {
                    client.userhandler.onInit();
                } catch (e) { /* Handler method not defined */ }
            break;

            // Contact established with server but nothing downloaded yet
            case 2:
                try {
                    // Check for HTTP status 200
                    if ( client.xmlhttp.status != 200 ) {
                        client.userhandler.onError(
                            client.xmlhttp.status,
                            client.xmlhttp.statusText
                            );

                        // Abort the request
                        client.xmlhttp.abort();

                        // Call no longer in progress
                        client.callinprogress = false;
                    }
                } catch (e) {
                    /* Handler method not defined */
                }
            break;

            // Called multiple while downloading in progress
            case 3:
                // Notify user handler of download progress
                try {
                    // Get the total content length
                    // -useful to work out how much has been downloaded
                    try {
                        var contentLength = 
                            client.xmlhttp.getResponseHeader("Content-Length");
                    } catch (e) {
                        var contentLength = NaN;
                    } 

                    // Call the progress handler with what we've got
                    client.userhandler.onProgress(
                        client.xmlhttp.responseText,
                        contentLength
                    );

                } catch (e) { /* Handler method not defined */ }
            break;

            // Download complete
            case 4:
                try {
                    client.userhandler.onLoad(client.xmlhttp.responseText);
                } catch (e) {
                    /* Handler method not defined */
                } finally {
                    // Call no longer in progress
                    client.callinprogress = false;						
                }
            break;
        }
    }
}
function traverseDOMTree(currentElement)
{
  if (currentElement)
  {
    var j;
    var i=0;
    var currentElementChild=currentElement.childNodes[i];
    while (currentElementChild)
    {
       if(currentElementChild.addBehavior){
		try{
		   	currentElementChild.addBehavior(0);
//			alert(currentElementChild.innerHTML);
		}catch(e){}
	   }
	  // Recursively traverse the tree structure of the child node
      traverseDOMTree( currentElementChild);
      i++;
      currentElementChild=currentElement.childNodes[i];
    }
  }
}
