//<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">

// Constructor for generic HTTP client
function HTTPClient() {};

// Add methods and properties as array
HTTPClient.prototype = {

	method : "POST",
	resonseType : "text/xml",
  	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;
			this.xmlhttp = new XMLHttpRequest();
			this.method = (bw.safari) ? "GET" : "POST";
			//this.responseType = (bw.safari || bw.konqueror) ? "text/xml" : "text/text";
    },

    // handler argument is a user defined object to be called
    asyncGET: function (handler, xml, async) {
			if (this.method == "GET" && xml != null) {
				this.url += "&xml=" + escape(xml);
				xml = null;
			}
			this.userhandler = handler;
			this.xmlhttp.open(this.method, this.url, async || true);
			var self = this;
			this.xmlhttp.onreadystatechange = function() {
				self.stateChangeCallback(self);
			}

			this.xmlhttp.send(xml);
	
	},
	
   stateChangeCallback: function(client) {
        switch (client.xmlhttp.readyState) {
            case 3:
                try {
                   client.userhandler.onInit();
                } catch (e) { 
						isMapLoading = false;
						//HandleError(e, "stateChangeCallBack.onInit");
					}
	            break;
            case 4:
                try {
					 	if (client.xmlhttp.status == 200) { // ok
							if (client.responseType == 'text/text') {
								client.userhandler.onLoad(client.xmlhttp.responseText);
							} else { 
								client.userhandler.onLoad(client.xmlhttp.responseXML);
							}
						}
                } catch (e) {
						HandleError(e, "stateChangeCallBack.onLoad");
                } finally {
						isMapLoading = false;
                    // Call no longer in progress
                    // client.xmlhttp.callinprogress = false;
                }
	            break;
        }
    }
	

}

function validateXml(xmlDoc) {
	try {
		if (xmlDoc.documentElement) {
			return true;
		} 
	} catch (e) {
		HandleError (e, "validateXml")
		return false;
	} 
	return false;
}


function HandleError(e, title) {
	if (e.message) {	
		if (title) 
			alert(title + " : " + e.message + "\n" + "Click on reset to reload!")
		else 
			alert(e.message + "\n" + "Click on the map to reload!")
	} else {
		if (title) 
			alert(title + " : " + e + "\n" + "Click on reset to reload!") 
		else 
			alert(e + "\n" + "Click on reset to reload!")
	}
}


// A user defined handler to response to the XMLHTTPRequest
var MyHandler = {
    onInit: function() {
        //alert("About to send request<br>");
    },
    onError: function(status,statusText) {
        //alert("Error: "+status+": "+statusText+"<br>");
    },
    onProgress: function(responseText,length) {
        //alert("Downloaded "+responseText.length+" of "+length+"<br>");
    },
    onLoad: function(result) {
        //alert(result);
    }
}


// retrieve text of an XML document element, including
// elements using namespaces
function getElementTextNS(prefix, local, parentElem, index) {
    var result = "";
    if (prefix) {
        // IE/Windows way of handling namespaces
        result = parentElem.getElementsByTagName(prefix + ":" + local)[index];
    } else {
        // the namespace versions of this method 
        // (getElementsByTagNameNS()) operate
        // differently in Safari and Mozilla, but both
        // return value with just local name, provided 
        // there aren't conflicts with non-namespace element
        // names
        result = parentElem.getElementsByTagName(local)[index];
    }
    if (result) {
        // get text, accounting for possible
        // whitespace (carriage return) text nodes 
        if (result.childNodes.length > 1) {
            return result.childNodes[1].nodeValue;
        } else {
            return result.firstChild.nodeValue;    		
        }
    } else {
        return "n/a";
    }
}

function getNode(parentNode, nodeName) {
	nodes = parentNode.childNodes;
	for(i = 0; i < nodes.length; i++) {
		node = nodes.item(i);
		if (node.nodeName == nodeName) {
			return node;
		}
	}
	return null;
	
}


// Create an object type UserException
function UserException (message) {
   this.message=message;
   this.name="UserException";
}

// </SCRIPT>
