/* version 2 */
var ajaxIdents = {};

function ajaxEncodeJSON(value) {
	var a,i,k,l,v;
	var r = /["\\\x00-\x1f\x7f-\x9f]/g;
        var m = {
            '\b': '\\b',
            '\t': '\\t',
            '\n': '\\n',
            '\f': '\\f',
            '\r': '\\r',
            '"' : '\\"',
            '\\': '\\\\'
        };

	switch (typeof value) {
	case 'string':
		return r.test(value) ? '"' + value.replace(r, function(a) {
				var c = m[a];
				if (c) return c;
				c = a.charCodeAt();
				return '\\u00' + Math.floor(c / 16).toString(16) + (c % 16).toString(16);
                    		}) + '"' :
                    '"' + value + '"';

	case 'number':
		return isFinite(value) ? String(value) : 'null';

	case 'boolean':
	case 'null':
		return String(value);

	case 'object':
		if (!value)
			return 'null';

		if (typeof value.toJSON === 'function') {
			return ajaxEncodeJSON(value.toJSON());
		}
		a = [];
		if (typeof value.length === 'number' && !(value.propertyIsEnumerable('length'))) {
			l = value.length;
			for (i = 0; i < l; i += 1)
				a.push(ajaxEncodeJSON(value[i]) || 'null');

			return '[' + a.join(',') + ']';
		}
		
		for (k in value)
			a.push(ajaxEncodeJSON(k) + ':' + ajaxEncodeJSON(value[k]));

		return '{' + a.join(',') + '}';
	}
}


function ajaxGetXmlHttp() {
	var xmlhttp = null;
	if (window.XMLHttpRequest) {
		xmlhttp = new XMLHttpRequest();
	}
	else if (window.ActiveXObject) { // IE
		try {
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e) {
			try {
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e2) {
				xmlhttp = null;
			}
		}
	}
	return xmlhttp;
}

function ajaxURLEncode(str) {
	str = str.replace(/%/g,'%25');
	str = str.replace(/\+/g,'%2B');
	str = str.replace(/=/g,'%3D');
	str = str.replace(/ /g,'+');
	str = str.replace(/\?/g,'%3F');
	str = str.replace(/&/g,'%26');
	return str;
}

/* subsequent calls with ident != null will cancel the previous requests with the same ident */
function ajaxCall(url,post,callback,ident,localParam) {
	var xmlhttp = ajaxGetXmlHttp();
	var mynum = null;
	
	if (ident != null) {
		if (ajaxIdents[ident] != null)
			mynum = ajaxIdents[ident] + 1;
		else
			mynum = 1;
		ajaxIdents[ident] = mynum;
	}
	
	if (callback != null) {
		xmlhttp.onreadystatechange = function () {
			if (xmlhttp.readyState == 4 && xmlhttp.status == 200 && mynum == ajaxIdents[ident])
				callback(eval('('+xmlhttp.responseText+')'),localParam);
		};
	}
	
	if (post == null) {
		xmlhttp.open("GET",url,true);
		xmlhttp.send(null);
	}
	else {
		xmlhttp.open("POST",url,true);
		xmlhttp.setRequestHeader('Content-Type','text/x-json');
		xmlhttp.send(ajaxEncodeJSON(post));
  	}
}

function ajaxDebug(url,post) {
	var xmlhttp = ajaxGetXmlHttp();
	xmlhttp.onreadystatechange = function () {
			if (xmlhttp.readyState == 4)
				alert("Status: "+xmlhttp.status+"\n"+xmlhttp.responseText);
		};
	if (post == null) {
		xmlhttp.open("GET",url,true);
		xmlhttp.send(null);
	}
	else {
		xmlhttp.open("POST",url,true);
		xmlhttp.setRequestHeader('Content-Type','text/x-json');
		xmlhttp.send(ajaxEncodeJSON(post));
  	}
}



function ajaxSetInnerHTML(url,post,obj) {
	var xmlhttp = ajaxGetXmlHttp();
	if(typeof(obj) != 'object') obj = document.getElementById(obj);
	xmlhttp.onreadystatechange = function() {
				if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
					obj.innerHTML = xmlhttp.responseText;
			};
	if (post == null) {
		xmlhttp.open("GET",url,true);
		xmlhttp.send(null);
	}
	else {
		xmlhttp.open("POST",url,true);
		xmlhttp.setRequestHeader('Content-Type','text/x-json');
		xmlhttp.send(ajaxEncodeJSON(post));
  	}
}




