//the global ajax object
var xmlhttp;

//the init function
function initAjax()
{
  if (window.XMLHttpRequest)
  {
	// code for IE7+, Firefox, Chrome, Opera, Safari
	xmlhttp=new XMLHttpRequest();
  }
  else if (window.ActiveXObject)
  {
	// code for IE6, IE5
	xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
  else
  {
	alert("Your browser does not support XMLHTTP!");
  }
}
//--------------------------------


/**
* given a modulename and where clausule, returns the 
* content of the fieldname.
* it will invoke document.getDBFieldWithAjax_callback(modulename, fieldname, filter, returnvalue)
*/
function getDBFieldWithAjax(modulename, fieldname, filter)
{
	xmlhttp.onreadystatechange=function()
	{
		if(xmlhttp.readyState==4)
  		{
		
			//window.alert("getDBFieldWithAjax: xmlhttp: "+xmlhttp.responseText);
			if(document.getDBFieldWithAjax_callback != undefined)
			{
				document.getDBFieldWithAjax_callback(modulename, fieldname, filter, xmlhttp.responseText);
			}
			else
			{
			   window.alert("myajax.js: getDBFieldWithAjax: document.getDBFieldWithAjax_callback is undefined");
			}
		}
	}
	
	var query = "/geus/srv_getdbfield.php?modulename="+modulename+"&fieldname="+fieldname+"&filter="+filter;
	//window.alert(query);
	xmlhttp.open("GET",query,true);
	
	xmlhttp.send(null);
}
//--------------------------------



function setDivContentAjax(divname, contenturl)
{
	xmlhttp.onreadystatechange=function()
	{
		if(xmlhttp.readyState==4)
  		{		
			div = document.getElementById(divname);
			if(div != null)
			{
			
			//http://www.webdeveloper.com/forum/archive/index.php/t-177998.html
				div.innerHTML = xmlhttp.responseText;
				var search = xmlhttp.responseText;
				var script;
				while( script = search.match(/(<script[^>]+javascript[^>]+>\s*(<!--)?)/i))
				{
					search = search.substr(search.indexOf(RegExp.$1) + RegExp.$1.length);
					if (!(endscript = search.match(/((-->)?\s*<\/script>)/))) break;
					block = search.substr(0, search.indexOf(RegExp.$1));
					search = search.substring(block.length + RegExp.$1.length);
					var oScript = document.createElement('script');
					oScript.text = block;
					document.getElementsByTagName("head").item(0).appendChild(oScript);
				}
			
				
			}
			else window.alert("myajax.js setDivContentAjax not found divname "+divname);
		}
	}
	var query = "/geus/"+ contenturl;
	//window.alert(query);
	xmlhttp.open("GET",query,true);
	
	xmlhttp.send(null);	
};
