
function createHttpRequest(){

	if(window.ActiveXObject){
		try {
			return new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				return new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e2) {
				return null;
	 		}
	 	}
	} else if(window.XMLHttpRequest){
		return new XMLHttpRequest();
	} else {
		return null;
	}
}

function set_hrefs()
{
	//var oAs = document.getElementsByTagName("a");
	var oAs = document.links;
	var LC;
	for (LC = 0; LC < oAs.length; LC++)
	{
		if (oAs[LC].onclick)
		{
			oAs[LC].href = "javascript:void(0)";
			//oAs[LC].href="javascript:"+oAs[LC].onclick+";";
			//oAs[LC].onclick='';
		}
	}
}

/**
 * activate_css(str)
 *
 * looks for any < link > and sets up a link node in the main document.
 *
 */

function activate_css(targetnode)
{
	var scriptnodes = targetnode.getElementsByTagName('link')
	var LC;
	for (LC = 0; LC < scriptnodes.length; LC++)
	{
		var sn = scriptnodes[LC];
		sn.parentNode.removeChild(sn);

		fileref = document.createElement('link')

		if (sn.href)
		{
			fileref.href = sn.href;
		}
		else
		{
			fileref.text = sn.text;
		}

		fileref.rel = "stylesheet";
		fileref.type = "text/css";

		document.getElementsByTagName("head").item(0).appendChild(fileref);
	}
}

/**
 * activate_javascript(str)
 *
 * looks for any < script > and sets up a script node in the main document.
 *
 */

function activate_javascript(targetnode)
{
	var scriptnodes = targetnode.getElementsByTagName('script')
	var LC;

	var snodes = Array(scriptnodes.length);
	for (LC = scriptnodes.length - 1; LC >= 0; --LC)
	{
		var sn = scriptnodes[LC];
		snodes[LC] = sn.cloneNode(true);
		sn.parentNode.removeChild(sn);
	}

	for (LC = 0; LC < snodes.length; LC++)
	{
		var sn = snodes[LC];

		var fileref = document.createElement('script')

		if (sn.src)
		{
			fileref.setAttribute("src", sn.src);
		}
		else
		{
			fileref.text = sn.text;
		}
		fileref.type = "text/javascript";
		fileref.language = "javascript";
		/*
		fileref.defer = true;
		fileref.onload = function() { return true; };
		*/

		/*
		var debug = document.createElement('pre');
		debug.innerHTML = 'test';
		debug.innerHTML += "href:" + sn.src + " text:" + fileref.text;
		var bodyels = document.getElementsByTagName("body");
		bodyels[bodyels.length-1].appendChild(debug);
		*/

		document.getElementsByTagName("head").item(0).appendChild(fileref);
	}
}

/**
 * ajax_eval(url)
 *
 * @param url	load and activate url
 * @returns		readyState
 */

function ajax_eval(url)
{
	var xhtoj = createHttpRequest()

	xhtoj.open("GET", url , true );

	xhtoj.onreadystatechange = function() 
	{
		if ((xhtoj.readyState==4) && (xhtoj.status == 200) )
		{
			str = xhtoj.responseText;

			eval(str);
		}
	}

	xhtoj.send("")
}

/**
 * ajax_dlink_refresh(oj,url)
 *
 * @param id	id of element for insert
 * @param url	load url
 * @param timeout	refresh timeout period, ms
 * @returns		readyState
 */

/* use these to overrun an existing timeout, so that
   we don't end up with several of them!
 */
var running_timeout = 0;
var timeout_idname;
var timeout_url;
var redo_timeout;

function ajax_dlink_refresh(idname, url, timeout)
{
	timeout_idname = idname;
	timeout_url = url;
	redo_timeout = timeout;
	if (running_timeout)
		return;
	setTimeout("do_ajax_dlink_refresh()", timeout);
	running_timeout = 1;
}

function do_ajax_dlink_refresh()
{
	if (ajax_dlink(timeout_idname, timeout_url) == 0)
	{
		running_timeout = 0;
		return;
	}
	running_timeout = 0;
	ajax_dlink_refresh(timeout_idname, timeout_url, redo_timeout);
}

/**
 * ajax_dlink(oj,url)
 *
 * @param id	id of element for insert
 * @param url	load url
 * @returns		readyState
 */

function ajax_dlink(idname,url)
{
	var body = document.body;
	if (body)
		body.style.cursor = 'wait';

	clearTimeout(); /* really important - get into a mess otherwise */
	var xhtoj = createHttpRequest()

	xhtoj.onreadystatechange = function() 
	{
		if (xhtoj.readyState==4)
		{
			var jsnode = 0;
			if (xhtoj.status == 200)
			{
				str =  xhtoj.responseText;

				jsnode = document.getElementById(idname);

				if (jsnode)
				{
					/*
					var tst = document.createElement('html')
					tst.innerHTML = str;
					*/
					jsnode.innerHTML = str;
					activate_javascript(jsnode);

					/* remove all hrefs from onlick links */
					set_hrefs();

					if (body)
						body.style.cursor = 'auto';
					return 1;
				}
			}
			else
			{
				jsnode = document.getElementById(idname);

				if (jsnode)
				{
					jsnode.innerHTML = xhtoj.status;
				}
			}
		}
	}

	xhtoj.open("GET", url , true );
	xhtoj.send("");

	if (body)
		body.style.cursor = 'auto';

	return 0;
}


function onloaded(id){}

/**
 * chk_browser()
 *
 * @param browserName	browser name
 */

function chk_browser(browserName)
{
	var ua = navigator.userAgent

	switch (browserName)
	{
		case 'konqueror' :
			 return  ua.indexOf("Konqueror") != -1 ; break ;
		case 'safari' :
			 return  ua.indexOf("Safari") != -1 ; break ;

		dafault :
			return null ; break ;
	}
}


/**
 * chk_browser()
 *
 * @param idname	the name of the document id to be replaced on response
 * @param file  	the HTTP page to be POSTED to
 * @param fobj  	the form object.
 */

function ajax_post(idname, file, fobj)
{
	var str = getFormValues(fobj, null);
	var xhtoj = createHttpRequest();

	var jsnode = document.getElementById(idname);
	xhtoj.open( "POST", file, true );
	xhtoj.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xhtoj.setRequestHeader("Content-length", str.length);
	xhtoj.setRequestHeader("Connection", "close");
	xhtoj.send(str);

	xhtoj.onreadystatechange = function() 
	{
		if (xhtoj.readyState==4)
		{
			if (xhtoj.status == 200)
			{
				str =  xhtoj.responseText;

				jsnode = document.getElementById(idname);
				jsnode.innerHTML = str;

				activate_javascript(jsnode);
				activate_css(jsnode);

				/* remove all hrefs from onlick links */
				set_hrefs();
			}
			else
			{
				jsnode = document.getElementById(idname);
				jsnode.innerHTML = file + ": " + xhtoj.status;
			}
		}
	}
}

function getFormValues(fobj)
{
   var str = "";
   var val = "";
   var cmd = "";
   var valFunc = null;

   for (var i = 0;i < fobj.elements.length; i++)
   {
	   switch (fobj.elements[i].type)
	   {
		   case "checkbox":
		   case "radio":
		   {
			if (fobj.elements[i].checked)
			{
				str += fobj.elements[i].name + "=" + fobj.elements[i].value + "&";
			} else {
				str += fobj.elements[i].name + "=&";
			}
			break;
		   }
		   case "text":
		   case "hiddentext":
		   case "hidden":
		   case "password":
		   case "textarea":
		   case "button":
				if(valFunc)
				{
					//use single quotes for argument so that the value of
					//fobj.elements[i].value is treated as a string not a literal
					cmd = valFunc + "(" + 'fobj.elements[i].value' + ")";
					val = eval(cmd)
				}
				str += fobj.elements[i].name + "=" + escape(fobj.elements[i].value) + "&";
				 break;
		   case "select-one":
				str += fobj.elements[i].name + "=" + fobj.elements[i].options[fobj.elements[i].selectedIndex].value + "&";
				break;
	   }
   }
   str = str.substr(0,(str.length - 1));
   return str;
}

