﻿// globenotes.com ajax wrapper and dropdown-populate utility
// requirements: mozxpath.js (for Mozilla browsers)
// Copyright (c) Peter Foran 2008

function ASXML() {

	var xmlHttpRequest;
	var callbackCode;
	var handle = -1;
	var self = this;
	var isAsync = true;

	this.getHandle = function() {
		return self.handle;
	}

	this.setHandle = function(newHandle) {
		self.handle = newHandle;
	}

	// public functions
	this.xmlResponse = function() {
		return self.xmlHttpRequest.responseXML;
	}

	this.textResponse = function() {
		return self.xmlHttpRequest.responseText;
	} 

	this.statusCode = function() {
		return self.xmlHttpRequest.status;
	}

	this.statusText = function() {
		return self.xmlHttpRequest.statusText;
	} 

	this.setCallback = function(callback) {
		self.callbackCode = callback;
	}

	this.getCallback = function()	{
		return self.callbackCode;
	}

	this.getXML = function(url)	{
		self.xmlHttpRequest = getBrowserHTTPRequest(); // get request object
		self.xmlHttpRequest.onreadystatechange = readyStateChange;
		self.xmlHttpRequest.open("GET", url, isAsync);
		self.xmlHttpRequest.send(null);
	}

	this.postXML = function(url,postData) {
		self.xmlHttpRequest = getBrowserHTTPRequest(); // get request object
		self.xmlHttpRequest.onreadystatechange = readyStateChange;
		self.xmlHttpRequest.open("POST", url, self.isAsync);
		self.xmlHttpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); 
		self.xmlHttpRequest.send(postData);
	}

	// private functions
	var getBrowserHTTPRequest = function () {
		var httpRequestAccess = null;

		if(window.XMLHttpRequest) // is a moz / IE7 browser
		{
			httpRequestAccess = new XMLHttpRequest();
		}
		else if(window.ActiveXObject) // is a IE4,5,6 browser
		{
			httpRequestAccess = new ActiveXObject("Microsoft.XMLHTTP");
		}

		return httpRequestAccess;
	} 

	var readyStateChange = function () {
		var HTTP_REQUEST_UNINITIALISED = 0;
		var HTTP_REQUEST_LOADING = 1;
		var HTTP_REQUEST_LOADED = 2;
		var HTTP_REQUEST_INTERACTIVE = 3;
		var HTTP_REQUEST_COMPLETE = 4;
		var REQUEST_STATUS_OK = 200;
		var REQUEST_STATUS_NOTFOUND = 404;

		if(self.xmlHttpRequest)	{
			if (self.xmlHttpRequest.readyState == HTTP_REQUEST_COMPLETE) {
				if(self.callbackCode)	{
					self.callbackCode(self.xmlHttpRequest.status == REQUEST_STATUS_OK); //send true back, if the doc loaded ok, false otherwise
				}
			}
		}
	}

	// clears a dropdownlist
	this.clearDDL = function(ddlID, startTxt, startVal) {

		//clear out dropdownlist
		_ddl = document.getElementById(ddlID);
		while (_ddl.childNodes.length >0){
			_ddl.removeChild(_ddl.childNodes[0]);
		}

		//insert initial value (if provided)
		if(startTxt != ""){
		    var o = document.createElement("Option");
		    o.innerHTML = startTxt;
		    o.value = startVal;
		    _ddl.appendChild(o);
        }
	}

	// repopulates a dropdownlist
	this.populateDDL = function(ddlID, nodes, startTxt, startVal) {

	    //clear out dropdownlist
	    this.clearDDL(ddlID, startTxt, startVal);

	    //populate the dropdown
	    for (var i = 0; i < nodes.length; i++) {
	        var op = document.createElement("option");
	        var v, t;

	        v = nodes[i].getElementsByTagName("value")[0].childNodes[0].nodeValue;
	        t = nodes[i].getElementsByTagName("name")[0].childNodes[0].nodeValue;

	        op.value = v;
	        op.innerHTML = t;
	        _ddl.appendChild(op);

	        //if just 'startVal' provided, then assume that we want the dropdown to default to this value (if it exists)
	        if (startTxt == "" && startVal != 0) {
	            if (v == startVal) { _ddl.options[i].selected = true; }
	        }
	    }
	}
}

