function remove_event(obj, type, callback) {
	var useCapture = false;

	// If a string was passed in, it's an id.
    if (typeof obj == 'string') obj = document.getElementById(obj);
	if (obj == null || callback == null) return false;

	// Mozilla/W3C listeners?
	if (obj.removeEventListener)
		obj.removeEventListener(type, callback, useCapture);
	// IE-style listeners?
	else if (obj.detachEvent)
		obj.detachEvent('on' + type, callback);
}

function add_event(obj, type, callback) {
	var useCapture = false;

	// If a string was passed in, it's an id.
    if (typeof obj == 'string') obj = document.getElementById(obj);
	if (obj == null || callback == null) return false;

	// Mozilla/W3C listeners?
	if (obj.addEventListener)
	{
		obj.addEventListener(type, callback, useCapture);
		return true;
	}
	// IE-style listeners?
	if (obj.attachEvent && obj.attachEvent('on' + type, callback ))
	{
		return true;
	}
}

var iframes_counter = 0;
function ajai_object(skip) {
	var name = 'axif'+(iframes_counter++);
	var o = new Object();
	o.open = function(method, url, flag) { 
		var i = document.createElement('IFRAME'); 
		i.id = name; i.name = name;
		//i.style.display = 'none';
		i.src = url; o.iframe = i;
		o.skip = skip;

		add_event(i, 'load', o.loaded);
		document.body.appendChild(i);
	}
	o.send = function() { }
	o.loaded = function() {
		//alert("skip:"+o.skip);
		if (o.skip) { o.skip--; return; }
		var i = o.iframe;
		if (i.contentDocument) { var d = i.contentDocument;	}
		else if (i.contentWindow) { var d = i.contentWindow.document; }
		else return;//if (d.location.href == 'about:blank') { return; }
//alert(d.body.innerHTML);
		o.readyState = 4;
		o.responseText = d.body.innerHTML;
		o.onreadystatechange(o.note);

		remove_event(i, 'load', o.loaded);
		document.body.removeChild(i);
		delete o;
	}
	return o;
}
function ajax_object() {
      if (window.XMLHttpRequest) { 
         return new XMLHttpRequest();
      } else if (window.ActiveXObject) { 
         try {
            return new ActiveXObject('Msxml2.XMLHTTP');
         } catch (e) {
            try {
               return new ActiveXObject('Microsoft.XMLHTTP');
            } catch (e) {}
         }
      }
      return null;
}
function post_args(args) {
	var str = ''; var sep = '';
	for (key in args) {
		str = str + sep;
		str = str + key+'='+escape(encodeURI(args[key]));
		sep = '&';
	}
	return str;
}
function ajax_run(url, args, post, callback, note) {
	var ajax_req = ( post == 2 ? ajai_object() : ajax_object() );
	ajax_req.onreadystatechange = callback;
	ajax_req.note = note;
	if (post) {
		ajax_req.open('POST',url,true);
		ajax_req.send(post_args(args));
	} else {
		if (args) url = url + '?' + post_args(args)
		ajax_req.open('GET',url,true);
		ajax_req.send(null);
	}
}
function ajax_set(url, div_id) {ajax_run(url, 0, 0, set_div, div_id );}

function set_div(id) {
	if (!id) id = this.note;
	if(this.readyState==4) {
		//alert(this.responseText);
		var obj = document.getElementById(id);
		if (obj) {
			obj.innerHTML = this.responseText;
		}
	}
}

function ajai_submit(obj, callback, note) {
    if (typeof obj == 'string') obj = document.getElementById(obj);
	if (obj == null) return false;
	var ajax_req = ajai_object(0); //hack: skip first!
	ajax_req.onreadystatechange = callback;
	ajax_req.note = note;
	ajax_req.open(obj.method,obj.getAttribute('action'),true);
	//ajax_req.send(null);
	obj.target = ajax_req.iframe.name;
//alert('submiting form...');
	obj.submit();
	return true;
}
function ajax_submit(obj, callback, note) {
    if (typeof obj == 'string') obj = document.getElementById(obj);
	if (obj == null) return false;
	ajax_run(obj.action, ajax_form_args(obj), (obj.method=='post' ? 1 : 0), callback, note);	
	return false;
}
function ajax_form_args(obj) {
    if (typeof obj == 'string') obj = document.getElementById(obj);
	if (obj == null) return false;
	var i = 0; var parts = obj.childNodes;
	var args = new Array();
	for (i = 0; i < parts.length; i++) {
		part = parts[i];
		if (part.nodeName == 'INPUT') {
			if ((parname = part.name) != '') {
				args.push ( {parname : part.value} );
			}
		}
	}
	return args;
}
