﻿// AJAX stuff
function getAjaxRequestType() {
	var xmlhttp;
	var activeXType;
	/*@cc_on
	@if (@_jscript_version >= 5)
		try {
			xmlhttp = new ActiveXObject(activeXType = "Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xmlhttp = new ActiveXObject(activeXType = "Microsoft.XMLHTTP");
			} catch (e) {
				xmlhttp = false;
			}
		}
	@else
		xmlhttp = false;
	@end @*/
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
		try {
			xmlhttp = new XMLHttpRequest();
		} catch (e) {
			xmlhttp = false;
		}
	}
	return activeXType;
}

var ieActiveXType = getAjaxRequestType();

function createAjaxRequest() {
	var xmlhttp;

	if (ieActiveXType) {
		xmlhttp = new ActiveXObject(ieActiveXType);
	} else {
		xmlhttp = new XMLHttpRequest();
	}
	return xmlhttp;
}


// if you do a GET, set data, mime to be null or don't pass them
// callback should have the following prototype
//
//      function aCallBack(req, success, url, originalData)
// url, originalData is to make it easy to properly identify to which request this response belongs to.
function sendAjaxRequest(url, callback, opt_data, opt_mime) {

	// use a new private instance for every request
	var req = createAjaxRequest();
	var method = 'GET';
	if (opt_data) {
		method = 'POST';
		if (!opt_mime) {
			opt_mime = 'application/x-www-form-urlencoded';
		}
	}
	req.open(method, url, true);
	if (opt_data && opt_mime) {
		req.setRequestHeader('Content-Type', opt_mime);    // must be called after open
	}
	// stick the instance data into the scope of the function literal 
	req.onreadystatechange = function() {
		if (req.readyState == 4) {
			callback(req, req.status && req.status == 200, url, opt_data);
		}
	};
	req.send(opt_data);
}


// other stuff
function ele(eleName) {
	if(document.getElementById && document.getElementById(eleName)) {
		return document.getElementById(eleName);
	}
	else if (document.all && document.all(eleName)) {
		return document.all(eleName);
	}
	else if (document.layers && document.layers[eleName]) {
		return document.layers[eleName];
	} else {
		return false;
	}
}

// method for creating XML Document
// ActiveX is for IE, all others should follow DOM rules
// For IE, find the best ActiveX object available
function getXmlDocument() {
	var ret;
	if (document.implementation && document.implementation.createDocument) {
		ret = document.implementation.createDocument("", "", null);
	} else if (window.ActiveXObject) {
		var err;
		try {
			ret = new ActiveXObject("Msxml2.DOMDocument.3.0");
		} catch (e) {
			err = e.message;
			try { 
				ret = new ActiveXObject("MSXML2.DOMDocument"); 
			} catch(e) { 
				err = e.message;
				try { 
					ret = new ActiveXObject("MSXML.DOMDocument"); 
				} catch(e) {
					err = e.message;
				}
			}
		}
		var pi = ret.createProcessingInstruction("xml",	"version='1.0' encoding='UTF-8'");
		ret.appendChild(pi);
	} else {
		throw "Browser is unable to create XML document: not supported by this browser";
	}
	return ret;
}

// XML Element creation
// set parent=doc to append to doc
// set null value if sub elements
function appendElement(doc, parent, tag, value) {
	var elem = doc.createElement(tag);
	if (value) {
		elem.appendChild(doc.createTextNode(value));
	}
	parent.appendChild(elem);
	return elem;
}

// Reading XML Element value 
function getElementValue(parentElement, tag) {
	var list = parentElement.getElementsByTagName(tag);
	if (!list || list.length == 0) {
		return null;
	}
	list = list[0].childNodes;
	if (!list || list.length == 0) {
		return null;
	}
	// supporting single child node only
	return list[0].nodeValue;
}

