/***************************************************************************
*
*	AJAX Object - version 1.0
*	Description: Create AJAX objects and write response to page
*	Author: Michael Turnwall
*
*	Copyright 2007 Michael Turnwall - Some Rights Reserved
*	http://creativecommons.org/licenses/GPL/2.0/
*
*	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 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(){}
/*----------------------*/

// 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;

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

// 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);
    }
}
/*-------------------------*/
