//ajax.jx
//latest version at http://vista.donavon.com/ajax.js
/*global window, ActiveXObject, XMLHttpRequest*/

function Ajax() {
	var ajaxVersion = "1.0.2";

	var that = this;

	//public properties
	this.method = "GET";
	this.url = null;
	this.req = null;
	this.onload = null;
	this.onerror = null;
	this.postData = null;
	this.postContentType = "application/x-www-form-urlencoded";


	//constructor
    if (window.XMLHttpRequest) {
	    // branch for Firefox, Safari, etc
        this.req = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
	    // branch for IE/Windows ActiveX version
        this.req = new ActiveXObject("Microsoft.XMLHTTP");
    }
    
    if(!this.req) {
		//throw error
		throw("XMLHttpRequest unsupported");
    }

	this.processReqChange = function() {
		if (this.req.readyState == 4) { //4=complete
			if ((this.req.status == 200 || this.req.status == 304) && this.req.responseXML.documentElement) { //OK (and valid XML document)?
				if (this.onload) {this.onload(this.req, this);}
			} else {
				if (this.onerror) {this.onerror(this.req, this);}
			}
		}
	};
	
	//public
	this.load = function(url) {
		
		if (url !== undefined) {
			this.url = url;
		}

		this.req.onreadystatechange = function() {that.processReqChange();};
		this.req.open(this.method, this.url, true);
		this.req.setRequestHeader("x-ajax-version",ajaxVersion);
		if (this.method=="POST") {
			this.req.setRequestHeader("Content-Type",this.postContentType);
		}
		this.req.send(this.postData);
	};
	
	this.buildFromForm = function(f) {
		if (f.action) {this.url = f.action;}
		if (f.method) {this.method = f.method;}
		
		var enc="";
		
		for (var i=0; i<f.elements.length; i++) {
			if (f.elements[i].name) {
				enc += "&" + encodeURIComponent(f.elements[i].name) + "=" + encodeURIComponent(f.elements[i].value);
			}
		}
		
		if (this.method=="GET") {
			this.url += "?" + enc.substr(1);
			this.postData = null;
		} else {
			this.postData = enc.substr(1);
		}
  
  	};



}



//global helper functions

function buildUri(name, value) {
	return "&" + encodeURIComponent(name) + "=" + encodeURIComponent(value);
}

function AjaxError(req) {
	alert("Error loading "+this.url+"\n\nstatus: "+this.req.status+" "+this.req.statusText);
}

function getChildNode(node, name) {
	for (var i=0;i<node.childNodes.length;i++) {
		if (node.childNodes[i].nodeName == name) {
			return node.childNodes[i];
		}
	}
	throw "Node "+name+" is not a child node of "+node.nodeName;
}
function getNodeText(node, name) {
	return getInnerText(getChildNode(node,name));
}
function getInnerText (node) {
	if (node.textContent) {
		return node.textContent;
	} else if (node.text) {
		return node.text;
	} else if (node.innerText) {
		return stripcdata(node.innerText);
	} else {
		switch (node.nodeType) {
		case 3:
		case 4:
			return node.nodeValue;
			//break;
		case 1:
		case 11:
			var innerText = '';
			for (var i = 0; i < node.childNodes.length; i++) {
				innerText += getInnerText(node.childNodes[i]);
			}
			return innerText;
			//break;
		default:
			return '';
		}
	}
}

function stripcdata(str) {
	if (str.substr(0,9) == "<![CDATA[") {
		return str.substr(9,str.length-12);
	} else {
		return str;
	}
}
