/* ============================================
ajaxLE - the ajax Lightweight Engine 
Version: 1.0
Last update: 20.04.2006

By Tomislav Ocvirek
www.ocvirek.com 
Copyright (C)2006

This script is freeware. 
DISCLAIMER: 
Use it at your own risk. Technical support is not provided.
============================================ */

var ajax = "";
var ajax_divID = "";
var ajax_url = "";
var ajax_callBack = "";
var ajax_method = "";

function ajaxTS() {
    var d = new Date();
    return d.getTime();
}

function ajaxExecute(url, callBack)
{
	//wrapper
	ajax_method = "execute";
	ajax_Engine(url, "", callBack);
}

function ajaxRender(url, divID, callBack)
{
	//wrapper
	ajax_method = "render";
	ajax_Engine(url, divID, callBack);
}

        
function ajaxRequest() 
{
 var a;
 var browser = navigator.appName;
 try
 {
	// use native xmlHttp - (mozilla, opera, IE7.x etc)
	a = new XMLHttpRequest(); 
 }
 catch(e)
 {
	try
	{
		// use Active X - IE 5.x, 6.x
		a = new ActiveXObject("Microsoft.XMLHTTP");
 	}
	catch(e)
	{
		var errStr = "<h2><font color=red>AjaxLE Application Error:</font> Your browser does not support Ajax</h2>"; 
		document.write(errStr);
	}
 }
 return a;
}


function ajax_Engine(url, divID, callBack)
{
	// initialize:
	ajax = ajaxRequest();
	ajax_divID = divID;
	ajax_url = url;
	ajax_callBack = callBack;
	// execute:
	ajax.onreadystatechange = function ajaxHandler(){ ajaxResponse(); };
	ajax.open("GET", (ajax_url + "&ts=" + ajaxTS()) , true);
	ajax.send("");
}

function ajaxResponse() 
{
  try
  {
	if(ajax.readyState==4)
	{

	 if (ajax.status == 200) {
			// fetch HTML:
			var a=ajax.responseText;
	
			if(ajax_method=="render")
			{
				try
				{
					// render method
					document.getElementById(ajax_divID).innerHTML=a;
				} 
				catch(e)
				{
					var errCode = a.replace(/\n/g, "<br>") + "";
					errCode = errCode.replace(/\t/g, "&nbsp;&nbsp;&nbsp;");
					var errStr = "<h2><font color=red>AjaxLE Application Error:</font> Could not perform ajaxRender() method</h2>" + 
					             "<h4><b>Error description: " + e.description + "</b></h4>" +
					             "<table bgcolor=#FFFFBB width=100% cellpadding=10><tr><td><font face=Courier size=2><h3>HTML:</h3>" +
					              errCode + "</font></td></tr></table>";
					document.write(errStr);
				}
			}	
			else
			{
				try
				{
					// execute method
					eval(a);
				} 
				catch(e)
				{
					var errCode = a.replace(/\n/g, "<br>") + "";
					errCode = errCode.replace(/\t/g, "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
					var errStr = "<h2><font color=red>AjaxLE Application Error:</font> Could not perform ajaxExecute() method</h2>" + 
					             "<h4><b>Error name: " + e.name + "<br>Error description: " + e.description + "</b></h4>" +
					             "<table bgcolor=#FFFFBB width=100% cellpadding=10><tr><td><font face=Courier size=2><h3>Javascript code:</h3>" +
					              errCode + "</font></td></tr></table>";
					document.write(errStr);
				}
			}
			// callback function:
			try
			{
			    eval(ajax_callBack);	
			}
			catch(e){}
	  } 
	  else
	  {
			var errStr = "<h2><font color=red>AjaxLE Application Error:</font> Internal Server Error 500</h2>" +
						 "<iframe src='" + ajax_url + "' width=100% height=100%></iframe>";
			document.write(errStr);
	  }
	 
	  // clean up:
	  ajax = null;
	  ajax_divID = "";
	  ajax_url = "";
	  ajax_callBack = "";
   
     }
  } catch(e) {}
}
