/* AJAX */

/*
Run an Ajax request.
dstdiv: the div-id to place result in. if it starts with "js_"-prefix, only the given JavaScript is processed
url: the request url.
script: the script to run after result, default: none
method: GET or POST, default: GET
*/ 
function runRequest( dstdiv, url, script, method )
{
  if( typeof(xmlHttp[dstdiv]) != "undefined" && xmlHttp[dstdiv] !== null )
    return;
  if( requestScheduled[dstdiv] === true )
  {
    requestScheduled[dstdiv] = false;
    script = requestScheduledScript[dstdiv];
  }

  xmlHttp[dstdiv] = GetXmlHttpObject();
  if( xmlHttp[dstdiv] == null )
    return;
  xmlHttpScript[dstdiv] = script;
  
  if( url.indexOf('service=') == -1 )
	url = url + (url.indexOf('?') == -1 ? "?" : "&") + "service=Ajax";

  var params = null;
  if( method && method.search(/post/i) != -1 )
  {
    params = url.split('?',2);
    if( params )
    {
      url = params[0];
      params = params.length > 1 ? params[1] : "";
    }
  }
	
  xmlHttp[dstdiv].onreadystatechange = function(){showRequest(dstdiv)};
  xmlHttp[dstdiv].open( (method ? method : "GET"), url, true );

  if( params !== null )
  {
    xmlHttp[dstdiv].setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp[dstdiv].setRequestHeader("Content-length", params.length);
    xmlHttp[dstdiv].setRequestHeader("Connection", "close");
  }
  
  xmlHttp[dstdiv].send( params !== null ? params : null );
} // runRequest



/*
Run an Ajax request after a duration and automatically start again
dstdiv: see runRequest()
url: see runRequest()
duration: time in millsec
*/
function runRefresh( dstdiv, url, duration )
{
	scheduleRequest( dstdiv, url, duration, "runRefresh('"+dstdiv+"','"+url+"',"+duration+")" );
} // runRefresh



/*
Run an Ajax request after a duration
dstdiv: see runRequest()
url: see runRequest()
duration: time in millsec
script: see runRequest()
*/
function scheduleRequest( dstdiv, url, duration, script )
{
  if( requestScheduled[dstdiv] === true )
    return;
  requestScheduled[dstdiv] = true;
  requestScheduledScript[dstdiv] = script;
  requestScheduledId[dstdiv] = setTimeout( "runRequest('"+dstdiv+"','"+url+"')", duration );
} // scheduleRequest



/*
Stop a scheduled Ajax request
dstdiv: see runRequest()
script: set the script to run if a request has already been sent, default: no script is run
*/
function stopRequest( dstdiv, script )
{
  if( requestScheduled[dstdiv] === true )
    requestScheduledScript[dstdiv] = script;
  if( xmlHttp[dstdiv] )
    xmlHttpScript[dstdiv] = script;
} // scheduleRequest



/*
Post a form via Ajax
dstdiv: see runRequest()
formEl: the element of the form
script: see runRequest()
*/
function runFormRequest( dstdiv, formEl, script )
{
	var urlAttr = formEl.attributes.getNamedItem('action');
	var methodAttr = formEl.attributes.getNamedItem('method');
	var url = (urlAttr ? urlAttr.value : "");
	var method = (methodAttr ? methodAttr.value : "");
	var params = "";
	for( var childElNo = 0; childElNo < formEl.elements.length; childElNo++ )
	{
		childEl = formEl.elements[childElNo];
		if( childEl.name )
		{
			var value = "";
			if( childEl.type == 'radio' )
			{
				if( childEl.checked )
					value = childEl.value;
				else
					value = null;
			}
			else
			if( childEl.type == 'checkbox' )
			{
				if( childEl.checked )
					value = childEl.value;
			}
			else
			{
				value = childEl.value;
			}
			
			if( value !== null )
			{
				params += (params.length == 0 ? "?" : "&");
				params += encodeURIComponent( childEl.name ) + "=" + encodeURIComponent( value );
			}
		}
	}
	url += params;
	runRequest( dstdiv, url, script, method );
} // runFormRequest



var xmlHttp = new Array();
var xmlHttpScript = new Array();
var requestScheduledId = new Array();
var requestScheduledScript = new Array();
var requestScheduled = new Array();

/* processes the result of an Ajax request */
function showRequest( dstdiv )
{
  if( xmlHttp[dstdiv] == null )
    return;
    
  if( xmlHttp[dstdiv].readyState==4 || xmlHttp[dstdiv].readyState=="complete" )
  {
	if( xmlHttp[dstdiv].status == 200 )
  	{
	  	var responseText = xmlHttp[dstdiv].responseText;
	  	if( responseText !== null && dstdiv.indexOf("js_") !== 0 && !xmlHttp[dstdiv].responseIsFailure )
	    	document.getElementById( dstdiv ).innerHTML = xmlHttp[dstdiv].responseText;
	    script = xmlHttpScript[dstdiv];
	    if( typeof(script) != "undefined" && script != null )
	      eval( script );
	}
    xmlHttp[dstdiv] = null;
  }
} // showRequest



/* creates an Ajax request object */
function GetXmlHttpObject()
{
  var objXMLHttp=null
  
  if (window.XMLHttpRequest)
  {
    objXMLHttp=new XMLHttpRequest()
  }
  else if (window.ActiveXObject)
  {
    objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
  }
  return objXMLHttp
}




if (!ajaxPrototype) {
    var ajaxPrototype = new Object();

    ajaxPrototype.Ajax = function() {
        this.http = null;
        this.targetElementId = null;
        this.onLoadFunction = null;
    }

    ajaxPrototype.Ajax.prototype = {
        setOnLoadFunction: function(onLoadFunction) {
            this.onLoadFunction = onLoadFunction;
        },
        load: function(url, targetElementId, method, data) {
            var requestMethod = "GET";
            if (method != null) requestMethod = method;

            this.targetElementId = targetElementId;
            this.postHTTP(url, this.delegate(this, this.onAjaxLoad), requestMethod, data);
        },
        onAjaxLoad: function() {
            if (this.http.readyState == 4) {
                if (this.http.status == 200) {
                    if (this.targetElementId != null) {
                        var element = document.getElementById(this.targetElementId);
                        if (element != null) {
                            element.innerHTML = this.http.responseText;
                        }
                    }
                    if (this.onLoadFunction != null) {
                        this.onLoadFunction();
                    }
                }
            }
        },
        postHTTP: function(url, callbackFunction, method, data) {
            this.http = this.getHTTPObject();
            this.http.open(method, url, true);
            this.http.onreadystatechange = callbackFunction;
            if (method == "POST") {
                this.http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            }
            this.http.send(data);
        },
        getHTTPObject: function() {
            var http = false;
            if (window.ActiveXObject) {
                var names = ["Msxml2.XMLHTTP.6.0", "Msxml2.XMLHTTP.5.0", "Msxml2.XMLHTTP.4.0", "Msxml2.XMLHTTP.3.0", "Msxml2.XMLHTTP", "Microsoft.XMLHTTP"];
                for (var key in names) {
                    try {
                        return new ActiveXObject(names[key]);
                    } catch (e) {}
                }
            } else if (XMLHttpRequest) {
                try {
                    http = new XMLHttpRequest();
                } catch (e) {}
            }
            return http;
        },
        delegate: function(obj, objMethod) {
            return function() {
                return objMethod.call(obj, arguments);
            }
        }
    }
}

var Ajax = ajaxPrototype.Ajax;

function ajaxLoad(url, targetElementId, method, data, onLoadFunction) {
    var ajaxObj = new Ajax();
    if (onLoadFunction != null) ajaxObj.setOnLoadFunction(onLoadFunction);
    ajaxObj.load(url, targetElementId, method, data);
}
