
function addLoadEvent(func) 
{
	var oldonload = window.onload;
	if (typeof window.onload != 'function') 
	{
		window.onload = func;
	} else {
		window.onload = function() 
		{
			oldonload();
			func();
		}
	}
}

function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function breakout_of_frame() {
  if (top.location != location) {
    top.location.href = document.location.href ;
  }
}

function showTT(e, id, src, atImage)
{
	obj = document.getElementById(src);
	tt = document.getElementById(id);
	
	var curleft = curtop = 0;
	if(atImage)
	{
		if (obj.offsetParent) {
			curleft = obj.offsetLeft
			curtop = obj.offsetTop
			while (obj = obj.offsetParent) {
				curleft += obj.offsetLeft
				curtop += obj.offsetTop
			}
		}
	}
	else
	{
		var posx = 0;
		var posy = 0;
		if (!e) var e = window.event;
		if (e.pageX || e.pageY) 	{
			posx = e.pageX;
			posy = e.pageY;
		}
		else if (e.clientX || e.clientY) 	{
			posx = e.clientX + document.body.scrollLeft
				+ document.documentElement.scrollLeft;
			posy = e.clientY + document.body.scrollTop
				+ document.documentElement.scrollTop;
		}
		
		curleft = posx - 45;
		curtop = posy - 640;	
	}

/*	obj = document.getElementById(src);
	curtop += obj.height;*/
	
	tt.style.left = curleft + "px";
	tt.style.top = curtop + "px";
	tt.style.zIndex = "100";
	tt.style.display = "block";	
}

// Disables the link of an anchor.
// An anchor is the html tag which starts with <a href="http://www.blabla.com/etc"
//
// obj       is the anchor, which can be obtained at the HTML page
//           with document.getElementById('this_anchor_id')
//
// disable   true  ==> disables the anchor by disablig the href attribute
//                     and stores the original value of href in href_bak
//           false ==> enables back the anchor by setting its href attribute with
//                     the value previously stored in href_bak
function disableAnchor_ORIGINAL(obj, disable)
{
	if(disable)
	{
		var href = obj.getAttribute("href");
		if(href && href != "" && href != null)
		{
			obj.setAttribute('href_bak', href);
		}
		obj.removeAttribute('href');
		obj.style.color="gray";
	}
	else
	{
		obj.setAttribute('href', obj.attributes['href_bak'].nodeValue);
		obj.style.color="blue";
	}
}

/* Disables/Enables an HTML anchor object (i.e. a link)
 * The usual HTML disabled property cannot be used with anchors, and
 * therefore this function needs to be used.
 * Internally the value of the href attribute of the anchor object
 * is saved in a new href_bak, and the href attribute is removed.
 * When the link needs to be enabled again, the href attribute is
 * restored with the value contained in href_bak.
 * If the function is called in "enable" mode for the first time,
 * without having disabled it before, nothing is done.
 *
 * Parameters:
 *	obj		HTML object representing the anchor.
 *			It can be obtained in a HTML page by using:
 *				document.getElementById(objectID)
 *	disable	= true  ==> will disable the anchor
 *	enable  = false ==> will enable the anchor with the old href value
 */
function disableAnchor(obj, disable)
{
	if(disable)
	{
		var href = obj.getAttribute("href");
		if(href && href != "" && href != null)
		{
			obj.setAttribute('href_bak', href);
		}
		obj.removeAttribute('href');
		obj.style.color="gray";
	}
	else
	{
		var href_bak = obj.getAttribute("href_bak");
		if(href_bak && href_bak != "" && href_bak != null)
		{
			obj.setAttribute('href', href_bak);
			obj.style.color="blue";
		}
		else
		{
			// The function has been called with a false parameter (to enable the original
			// link) but there was no href_bak attribute (the original link), because the
			// function had not previously been called with a true parameter (to disable
			// the link).
			// Conclusion: Do nothing and leave the link as if nothing had happened
		}
	}
}

/**
 *  Disables a HTML anchor for a period of time and enables it back afterwards
 *  Parameters:
 *		objectID		object id of the anchor =>   <a id="anchor_id" href="http://www.etc..."
 *		milliseconds	duration of the disabled state
 *							n > 0	disables the anchor. After n milliseconds it will be enabled.
 *							n = 0	disables the anchor indefinitely. It is never enabled again.<b> 
 *							n < 0	enables the anchor to the old href value
 *		millisecondsBeforeExecution		delay before disabling the anchor
 *
 */
function disableAnchorTemporarily(objectID, milliseconds, millisecondsBeforeExecution)
{
	// If a delay is set, execute this function later
	if (millisecondsBeforeExecution > 0)
	{
		window.setTimeout("disableAnchorTemporarily('"+objectID+"','"+milliseconds+"','0');",millisecondsBeforeExecution);
		return;
	}

	// If no duration is set, simply disable the anchor and never enable it again
	if (milliseconds == 0)
	{
		disableAnchor(document.getElementById(objectID), true);
		return;
	}
	// If a duration is set, disable the anchor, and set a timer to call this
	// function later in order to enable it again, by specifying a negative duration
	else if (milliseconds > 0)
	{
		disableAnchor(document.getElementById(objectID), true);
		window.setTimeout("disableAnchorTemporarily('"+objectID+"','-1','0');",milliseconds);
		return;
	}
	// If a negative duration was specified, enable the anchor again.
	// This will normally happen in the call to this procedure made by the
	// timer, after having disabled the anchor. See the elseif clause above
	// If the first call to this function has a negative duration, there will be
	// no problem, because the disableAnchor function won't do anything, since the
	// anchor wasn't previously disabled.
	else
	{
		disableAnchor(document.getElementById(objectID), false);
		return;
	}
}


var http_request = false;

function makeRequest(url, obj, executeJS) 
{
	var parameters = '';

	if(obj)
	{
		obj = obj.childNodes[0]; //form contains a DIV which contains the INPUTs
		
		for (i=0; i<obj.childNodes.length; i++) 
		{
			if (obj.childNodes[i].tagName == "INPUT") 
			{
				if (obj.childNodes[i].type == "text") 
				{
					parameters += "&" + obj.childNodes[i].name + "=" + encodeURIComponent(obj.childNodes[i].value);
				}
			}
		}
	}
	
	http_request = false;
	if (window.XMLHttpRequest) // Mozilla, Safari,... 
	{ 
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) 
		{
			// set type accordingly to anticipated content type
			//http_request.overrideMimeType('text/xml');
			http_request.overrideMimeType('text/html');
		}
	} 
	else if (window.ActiveXObject) // IE
	{ 
		try 
		{
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (e) 
		{
			try 
			{
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} 
			catch (e) {}
		}
		
		//http_request.overrideMimeType('text/html');		
	}
	if (!http_request) 
	{
		alert('Cannot create XMLHTTP instance');
		return false;
	}
	
	http_request.onreadystatechange = alertContents;// new Function('alertContents("'+executeJS+'")');
	http_request.open('GET', url + parameters, true);
	http_request.setRequestHeader("Content-Type", "text/html; charset=UTF-8");
	http_request.send(null);
}

function alertContents() 
{
	if (http_request.readyState == 4) 
	{
		if (http_request.status == 200) 
		{
			result = http_request.responseText;
			document.getElementById('sb_content').innerHTML = result;

			executeJSinHTML(result);
		}
		else
		{
			document.getElementById('sb_content').innerHTML = 'There was a problem with the request.';
		}
	}
}

function str_replace (search, replace, subject)
{
  var result = "";
  var  oldi = 0;
  for (i = subject.indexOf (search)
     ; i > -1
     ; i = subject.indexOf (search, i))
  {
    result += subject.substring (oldi, i);
    result += replace;
    i += search.length;
    oldi = i;
  }
  return result + subject.substring (oldi, subject.length);
}

function executeJSinHTML(input)
{		
	input=str_replace ("\'", "'", input);
	var re = /<script.*?>(.*?)<\//igm;
	var match;
	while ( (match = re.exec(input)) )		
	{
		eval(match[1]);
	}		
}

function addRecipient(name)
{
	if(document.messageForm.recipient.value.length < 1)
	{
		document.messageForm.recipient.value = name;
	}
	else
	{
		document.messageForm.recipient.value += ',' + name;
	}

}

function bc_sort(listid, bytag)
{
	var taglink = document.getElementById('taglink_' + listid);
	var levellink = document.getElementById('levellink_' + listid);	
	var theList = document.getElementById(listid);
	var theListElements = theList.getElementsByTagName('li'); 
	
	if(bytag)
	{
		if(theListElements.length < 2)
		{
			return true;
		}
		
		for(var n = 1; n < theListElements.length; ++n)
		{
			/* clantag stored in the classname */
			var clantag = theListElements[n].className;

			/* insertionsort */
			for(var i = 0; i < n; ++i)
			{
				var comparetag = theListElements[i].className;
				
				if(clantag < comparetag)
				{
					theList.insertBefore(theListElements[n], theListElements[i]);
					break;
				}
			}
		}
		
		taglink.style.display = 'none';
		levellink.style.display = 'inline';		
	}
	else /* by level */
	{
		if(theListElements.length < 2)
		{
			return true;
		}
		
		for(var n = 1; n < theListElements.length; ++n)
		{
			/* original sorting (by level) stored in the id */			
			var thelevel = theListElements[n].id;
			
			for(var i = 0; i < n; ++i)
			{
				var comparelevel = theListElements[i].id;
				
				if(thelevel < comparelevel)
				{
					theList.insertBefore(theListElements[n], theListElements[i]);
					break;
				}
			}
		}		
		
		levellink.style.display = 'none';
		taglink.style.display = 'inline';		
	}
}
