/***************************************************************************
*
*	Ajax Object - v1.3
*	Author: Michael Turnwall
*	Created: 11.07.2006
*	Modified: 01.16.2008
*	Copyright 2006 Michael Turnwall unless noted
*
*	This program is free software; you can redistribute it and/or modify
*	it under the terms of the GNU General Public License as published by
*	the Free Software Foundation (GPLv2). This copyright notice must
*	remain intact. If you use only a portion of this library, such as
*	a single function, you still need to give credit to the original
*	author of the function.
*
*	This program is distributed in the hope that it will be useful,
*	but WITHOUT ANY WARRANTY; without even the implied warranty of
*	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
*	See http://creativecommons.org/licenses/GPL/2.0/ for more information.
*
***************************************************************************/

// creates the XMLHttpRequest object
function Ajax(url,id,callback)
{
	this.url = url;
	this.handler = callback || function() { };
	this.ID = id;
}
/*----------------------*/

// setup the Ajax object transport
Ajax.prototype.init = function()
{
	return this.getMethod(
		function() {return new XMLHttpRequest()},
	    function() {return new ActiveXObject('Msxml2.XMLHTTP')},
	    function() {return new ActiveXObject('Microsoft.XMLHTTP')}
	    ) || false;
}
/*----------------------*/

// find out which method type to use for transport
// adopted from prototype.js - http://www.prototypejs.org/
Ajax.prototype.getMethod = function()
{
	var returnValue;

    for (var i = 0; i < arguments.length; i++) {
      var lambda = arguments[i];
      try {
        returnValue = lambda();
        break;
      } catch (e) {}
    }

    return returnValue;

}
/*----------------------*/

Ajax.prototype.update = function(query,method)
{
	this.ajax = this.init();
	if(!this.ajax)
	{
		return false;
	}
	else
	{
		var self = this;
		self.ajax.onreadystatechange = function()
		{
			if(self.ajax.readyState == 1)
				self.loading();
			if(self.ajax.readyState == 4)
				self.handler(self.ID,self.ajax.responseText,self.ajax.status);
		}
		this.updateTime = new Date();
		if(method != null && method.match(/post/i))
		{
			var url = this.url + "?" + this.updateTime.getTime();
			this.ajax.open("POST",url,true);
			this.ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
			this.ajax.setRequestHeader("Content-Length",query.length);
			this.ajax.send(query);
		}
		else
		{
			var url = this.url + "?" + query + "&timestamp=" + this.updateTime.getTime();
			this.ajax.open("GET",url,true);
			this.ajax.send(null);
		}
	}
}
/*----------------------*/

// used for a loading graphic
Ajax.prototype.loading = function()
{
	//alert(this.ID);
}
/*----------------------*/

// setups the ajax request
// type = POST/GET, url = url to script, sync = true/false, handler = function used to handle responseText
var ajax = "";
function initRequest(type, url, sync, handler,objName)
{
	ajaxObj = new Ajax;
	ajax = ajaxObj.init();

	try
	{
		ajax.onreadystatechange = handler;
		ajax.open(type,url,sync);
		ajax.send("");
	}
	catch(err)
	{
		alert("Sorry, there was a problem contacting the server\nError: " + err.message);
	}
}
/*----------------------*/

// id = id of element where response is going; js is if you want javascript to be executed (true/false - default)
function writeResponseText(id,js)
{
	if(ajax.readyState == 4 && ajax.status == 200)
	{
		var el = document.getElementById(id);
		el.innerHTML = ajax.responseText;
		if(js)
			exeJavascript(ajax.responseText)
	}
}
/*----------------------*/

// execute javascript from the AJAX responset text
function exeJavascript(responseText) {
  // RegExp from prototype.sonio.net
  var ScriptFragment = "(?:<script.*?>)((\n|\r|.)*?)(?:<\/script>)";
           
  var matchExp    = new RegExp(ScriptFragment, "img");
  var scripts  = responseText.match(matchExp);
  
    if(scripts)
	{
        var js = "";
        for(var s = 0; s < scripts.length; s++)
		{
            var matchExp = new RegExp(ScriptFragment, "im");
            js += scripts[s].match(matchExp)[1];
        }
        eval(js);
    }
}
/*-------------------------*/

//process the Ajax
function processData(id,responseText,status)
{
	if(status == 200)
	{
		document.getElementById(id).innerHTML = responseText;
		exeJavascript(responseText);
	}
}