/**
 * @author 	Andreas Goetz 	<cpuidle@gmx.de>
 * $Id: dynamic.js,v 1.5 2005/08/18 17:08:58 andig2 Exp $
 *
 * Orginial Code Care Of:
 * Copyright (c) 2004 Bitflux GmbH
 * http://blog.bitflux.ch/p1735.html
 *
 * Part of work based on:
 * Simon Willison
 * http://simon.incutio.com/archive/2004/05/26/addLoadEvent
 */

var processURI		= './index.php';
var liveReq 		= false;

var string_loading	= "Loading...";

// on !IE we only have to initialize it once
if (window.XMLHttpRequest) {
	liveReq = new XMLHttpRequest();
}

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

/**
 * Build the XMLHttpRequest and send it
 */
function liveReqDoReq() {
	if (liveReq && liveReq.readyState < 4) {
		liveReq.abort();
	}

	if (window.XMLHttpRequest) {
		// branch for IE/Windows ActiveX version
	} else if (window.ActiveXObject) {
		liveReq = new ActiveXObject("Microsoft.XMLHTTP");
	}

	name = this.id;
	liveReq.onreadystatechange = function(){ liveReqProcessReqChange(name); };

//	liveReq.open("GET", processURI + "?docpart=" + this.id );
	liveReq.open("GET", this.href + "&docpart=" + this.id);

	// show "Loading..." while waiting
	target = getTarget(this.id);
	if (el = document.getElementById(target+"_loading")) {
		el.style.display = "block";
	} else {
		document.getElementById().innerHTML = string_loading;
	}

	liveReq.send(null);

	return false;
}

/**
 * Processes the results of the XMLHttpRequest
 */
function liveReqProcessReqChange(name) {
	if (liveReq.readyState == 4) {
		document.getElementById(getTarget(name)).innerHTML = liveReq.responseText;
		doAction(name)
//		replaceWithContent(name);
	}
}

/**
 * Execute action if associated action for id exists
 */
function doAction(id)
{
	action = id+"_action";
	if (window[action])
	{
		window[action]();
	}
}

/**
 * Translate action target if translated target for id exists
 */
function getTarget(id)
{
	if (window[id]) {
		target = window[id]();
	} else {
		target = id + '_target';
	}

	return(target);
}

function addEvent(id)
{
	target = document.getElementById(id);
	if (!target) return false;
	target.onclick = liveReqDoReq;
}

/**
 * Initialise the filter links
 */
function init() {
	// keep browsers that don't support DOM or
	// XMLHttpRequest from getting in trouble
	if (document.getElementById && (window.XMLHttpRequest || window.ActiveXObject))
	{
		addEvent("docpart_more");
		addEvent("docpart_less");
/*
		el = document.getElementById("filtersalphabet");
		for (var i=0; i<el.childNodes.length; i++)
		{
			child = el.childNodes[i];
			child.onclick = function(){alert(this.childNodes[0].value)};
		}
*/
	}
}

addLoadEvent(init);
