function open_window(url, w, h)
{
	// To center popup window
	var l = parseInt((screen.width - w) / 2);
	var t = parseInt((screen.height - h) / 2);
	// Set up window params
	var parameters = 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=' + w + ',height=' + h + ',left=' + l + ',top=' + t;
	window.open(url, 'PopUpWindow', parameters);
}

function ToggleDDMenu(menu, header)
{
	var DDMenu = document.getElementById(menu);
	var DDTitle = document.getElementById(header);
	DDMenu.style.left = GetLeftOffset(DDTitle) + "px";
	DDMenu.style.top = (GetTopOffset(DDTitle) + DDTitle.offsetHeight) + "px";
	DDMenu.style.position = (DDMenu.style.position == 'visible') ? 'relative' : 'absolute';
	DDMenu.style.visibility = (DDMenu.style.visibility == 'visible') ? 'hidden' : 'visible';
}

function GetLeftOffset(element)
{
	var offsetParent = element.offsetParent;	
	if (offsetParent == null)
		return element.offsetLeft;
	else if(offsetParent == window || offsetParent.tagName == 'BODY')		
		return element.offsetLeft;		
	else
	{
		var offsetLeft = GetLeftOffset(offsetParent) + element.offsetLeft
		return offsetLeft;
	}
}

function GetTopOffset(element)
{
	offsetParent = element.offsetParent;
	if (offsetParent == null)
		return element.offsetTop;
	else if(offsetParent == window || offsetParent.tagName == 'BODY')
		return element.offsetTop;		
	else
		return GetTopOffset(offsetParent) + element.offsetTop;
}


// Image fade functions
var baseopacity=50

function slowhigh(which2){
imgobj=which2
browserdetect=which2.filters? "ie" : typeof which2.style.MozOpacity=="string"? "mozilla" : ""
instantset(baseopacity)
highlighting=setInterval("gradualfade(imgobj)",50)
}

function slowlow(which2){
cleartimer()
instantset(baseopacity)
}

function instantset(degree){
if (browserdetect=="mozilla")
imgobj.style.MozOpacity=degree/100
else if (browserdetect=="ie")
imgobj.filters.alpha.opacity=degree
}

function cleartimer(){
if (window.highlighting) clearInterval(highlighting)
}

function gradualfade(cur2){
if (browserdetect=="mozilla" && cur2.style.MozOpacity<1)
cur2.style.MozOpacity=Math.min(parseFloat(cur2.style.MozOpacity)+0.1, 0.99)
else if (browserdetect=="ie" && cur2.filters.alpha.opacity<100)
cur2.filters.alpha.opacity+=10
else if (window.highlighting)
clearInterval(highlighting)
}

function NewAjaxAgent(display_layer)
{
	var ajaxRequest;  // The variable that makes Ajax possible!
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	return ajaxRequest;
}

function AjaxRequest(page_name, display_layer)
{
	var ajaxRequest = NewAjaxAgent(display_layer);
	
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			var ajaxDisplay = document.getElementById(display_layer);
			ajaxDisplay.innerHTML = ajaxRequest.responseText;
		}
	}	
	ajaxRequest.open("GET", page_name, true);
	ajaxRequest.send(null);
	delete ajaxRequest; // MUST free memory otherwise connection remains active and subsequent refreshes/searches fail
}

function AjaxSearchRequest(query, display_layer, prog_bar, text)
{
	var ajaxRequest = NewAjaxAgent(display_layer);

	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			var x = document.getElementById(prog_bar);
			x.style.display = 'none';
			var ajaxDisplay = document.getElementById('s_results');	
			ajaxDisplay.innerHTML = ajaxRequest.responseText;
		}
	}	
	var url = '/functions/search_results.php?qry=' + query + "&txt=" + text;
	ajaxRequest.open("POST", url, true);
	ajaxRequest.send(null);
	delete ajaxRequest;	// MUST free memory otherwise connection remains active and subsequent refreshes/searches fail
}

function getElementsByClassName(oElm, strTagName, strClassName){
    var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
    var arrReturnElements = new Array();
    strClassName = strClassName.replace(/\-/g, "\\-");
    var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
    var oElement;
    for(var i=0; i<arrElements.length; i++){
        oElement = arrElements[i];      
        if(oRegExp.test(oElement.className)){
            arrReturnElements.push(oElement);
        }   
    }
    return (arrReturnElements)
}

function OptionForm(formname)
{
	var z = getElementsByClassName(document, 'form', 'optionsform');
	for (var i=0; i < z.length; i++)
		if (z[i].id != formname)
			z[i].style.display = 'none';
	
	var w = getElementsByClassName(document, 'td', 'requestheader');
	for (var i=0; i < w.length; i++)
			w[i].style.backgroundColor = '#F0EFEF'; 

	var x = document.getElementById(formname);
	var y = document.getElementById(formname + 'h');
	y.style.backgroundColor = (x.style.display == 'none' || x.style.display == '') ? '#AFD6C0' : '#F0EFEF';
	x.style.display = (x.style.display == 'none' || x.style.display == '') ? 'block' : 'none';
}

function ShowMenu(id, onOrOff)
{
	var lnk = document.getElementById('link' + id);	
	var mnu = document.getElementById('ddmenu' + id);
	
	if (lnk && mnu)
	{
		mnu.style.left = (GetLeftOffset(lnk) + lnk.offsetWidth - 2) + 'px';
		mnu.style.top = (GetTopOffset(lnk) - 1) + 'px';
		if (onOrOff == 'on')
			mnu.style.display = 'block';
		else
			mnu.style.display = 'none';
	}
}

function attachAllEvents()
{
	var links = getElementsByClassName(document, 'a', 'ddlink');
	var menus = getElementsByClassName(document, 'ul', 'dd');
	
	for (var i=0;i<links.length;i++)
	{
		links[i].onmouseover = function() {
			ShowMenu(parseInt(this.id.replace('link', '')), 'on');
		}
		links[i].onmouseout = function() {
			ShowMenu(parseInt(this.id.replace('link', '')), 'off');
		}
	}
	for (var i=0;i<menus.length;i++)
	{
		menus[i].onmouseover = function() {
			ShowMenu(parseInt(this.id.replace('ddmenu', '')), 'on');
		}
		menus[i].onmouseout = function() {
			ShowMenu(parseInt(this.id.replace('ddmenu', '')), 'off');
		}
	}
}
/*
if(window.addEventListener)
		window.addEventListener('load', attachAllEvents, false);
	else
		window.attachEvent('onload', attachAllEvents);
*/