/*function updateSearch (str) {
//	if (str.length > 0 && str.length < 3) return;
	xmlHttp2 = GetXmlHttpObject();
	if (xmlHttp2 == null) {
		alert ("Your browser does not support AJAX!");
		return;
	} 
	document.getElementById("searchStatus").innerHTML = "Searching...";
	var url = "members/address_book/search? searchString=" + str;
	xmlHttp2.onreadystatechange = searchStateChanged;
	xmlHttp2.open("GET",url,true);
	xmlHttp2.send(null);
}*/

function updateUserSearch (path,name,group,docid,tpl,checkmem) {
	xmlHttp2 = GetXmlHttpObject();
	if (xmlHttp2 == null) {
		alert ("Your browser does not support AJAX!");
		return;
	} 
	document.getElementById("searchStatus").innerHTML = "Searching...";
	var url = path + "? searchString=" + name + "&group=" + group + "&docid=" + docid + "&tpl=" + tpl + "&checkGroupMemberships=" + checkmem;
	xmlHttp2.onreadystatechange = searchStateChanged;
	xmlHttp2.open("GET",url,true);
	xmlHttp2.send(null);
}

function GetXmlHttpObject () {
	var xmlHttp = null;
	try { // Firefox, Opera 8.0+, Safari
		xmlHttp = new XMLHttpRequest();
	} catch (e) { // Internet Explorer
		try {
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}

function searchStateChanged () { 
	if (xmlHttp2.readyState == 4) { 
		document.getElementById("searchResults").innerHTML = xmlHttp2.responseText;
		document.getElementById("searchStatus").innerHTML = "";
	}
}

var Popups = {
    linkId: '',
	showPopup:function(linkId,ajaxPageUrl,parameters) {
        var xhro = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("MSXML2.XMLHTTP.3.0");
        xhro.open('post',ajaxPageUrl,true);
        xhro.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
        xhro.onreadystatechange = function() {
            if (xhro.readyState == 4 && xhro.status == 200) {
				var iDiv = document.getElementById('popup_box_inner');
				iDiv.innerHTML = xhro.responseText;

				Popups.linkId = linkId;

				var img  = document.getElementById('popup_box_image');
				if (img == null || img.src == img.baseURI) {
					Popups.positionPopup();
				}			// else positionPopup will be called by onLoad function of image
			}
		}
        if (xhro) {
            xhro.send(parameters);
        }
        return false;
    },
    hidePopup:function() {
		var div  = document.getElementById('popup_box');
		var speA = document.getElementById('speech_img');
		var speB = document.getElementById('speech_below_img');

		div.style.visibility  = "hidden";
		speA.style.visibility = "hidden";
		speB.style.visibility = "hidden";
    },
    positionPopup:function() {
		var w   = 280;		// width (excl. image)
		var mR  = 12;		// margin to remove if no image
		var oH  = 15;		// max overHang
		var lO  = -60;		// left offset
		var tO  = -60;		// top offset
		var bO  = 10;		// top offset (below)
		var sO  = 60;		// speech offset
		var mBy = 38;		// text margin (with links)
		var mBn = 10;		// text margin (no links)
		
		var div  = document.getElementById('popup_box');
		var speA = document.getElementById('speech_img');
		var speB = document.getElementById('speech_below_img');
				// div.style.left = '10px';
		div.style.visibility = "visible";

		var lnk  = document.getElementById(Popups.linkId);
		var cont = document.getElementById('page');
		var img  = document.getElementById('popup_box_image');
		var info  = document.getElementById('popup_box_info');
		var content  = document.getElementById('popup_box_content');
		var links  = document.getElementById('popup_box_links');
		var text  = document.getElementById('popup_box_text');

		if (img == null) {
			div.style.width = (w - mR) + 'px';
		} else if (img.src == img.baseURI) {
			div.style.width = (w - mR) + 'px';
			img.style.display = "none";
		} else {
			div.style.width = (w + img.width) + 'px';
			img.style.display = "block";
		}

		this.hideIfEmpty(info);
		this.hideIfEmpty(content);
		
		if (this.isEmpty(links.innerHTML)) {
			text.style.marginBottom = mBn + 'px';
		} else {
			text.style.marginBottom = mBy + 'px';
		}

		var contpos = this.getPos(cont);
		var lnkpos  = this.getPos(lnk);
		
		var left = lnkpos[0] - contpos[0] + lO;
		var top  = lnkpos[1] - contpos[1] + tO - div.offsetHeight;

		var speL = left + sO;

		if (left + div.offsetWidth > cont.offsetWidth - oH) {
			left = cont.offsetWidth - div.offsetWidth + oH;
		}

		if (left < - oH) {
			left = - oH;
		}

		if (top >= 0) {
			spe  = speA;
			speN = speB;
		} else {
			top = lnkpos[1] - contpos[1] + bO;
			spe  = speB;
			speN = speA;
		}

		div.style.left = left + 'px';
		div.style.top = top + 'px';
		
		spe.style.left = (speL - left) + 'px';
		spe.style.visibility = "visible";
		speN.style.visibility = "hidden";
    },
    getPos:function(obj){
		var curleft = curtop = 0;
		if (obj.offsetParent) {
			do {
				curleft += obj.offsetLeft;
				curtop += obj.offsetTop;
			} while (obj = obj.offsetParent);
		}
		return [curleft,curtop];
	},
    isEmpty:function(str){
		return str.replace(/^\s+|\s+$/g, '') == '';
	},
    hideIfEmpty:function(obj){
		if (this.isEmpty(obj.innerHTML)) {
			obj.style.display = 'none';
		} else {
			obj.style.display = 'block';
		}
	}
}

