function dataLoading(id, msg) {
	if (msg == '') {
		var msg = 'Loading Data';
	}
	document.getElementById(id).innerHTML = '<table align="center" style="height: 100%; width: 100%;"><tr><td align="center" style=\"padding-bottom: 9px;\">' + msg + '</td></tr></table>';
}

//Browser Display Dimensions
	function browserDimensions() {
		width = window.innerWidth || document.body.clientWidth;
		height = window.inneHeight || document.body.clientHeight;
		return {height:height, width:width};
	}

//Get absolute co-ords if object is relative
	function pos(obj) { 
		var cY = obj.offsetTop;
		var cX = obj.offsetLeft;
		while((obj = obj.offsetParent) != null) { 
			cY += obj.offsetTop;
			cX += obj.offsetLeft;
		}
		return {x:cX, y:cY};
	}

//Warn Change Functions
	function warnChange() {

		//Display Panel
		obj = document.getElementById('orgChangeWarn');
		obj.style.display = "";
		var x = obj.offsetWidth;
		var y = obj.offsetHeight;

		//If < IE7, hide select boxes
		if (/MSIE (5\.5|6\.)/.test(navigator.userAgent)) {
			document.getElementById('ext_users_hearaboutus').style.display = "none";
		}

		//Scroll Position
		/*var scrollTop = document.body.scrollTop | window.pageYOffset | 0;
		var scrollLeft = document.body.scrollLeft | window.pageXOffset | 0;

		//Center Panel on Screen
		//x = width of panel
		//y = height of panel
			var s = browserDimensions();
			//alert("Scroll: "+scrollLeft+", bWidth: "+s.width+", objWidth: "+x);

			obj.style.left = posH+"px";
			obj.style.top = posV+"px";*/

			objRegForm = document.getElementById('userRegForm');
			objPos = pos(objRegForm);
			var posH = Math.floor(((objRegForm.offsetWidth)/2) - (x/2))+objPos.x;
			var posV = Math.floor(((objRegForm.offsetHeight)/2) - (y/2))+objPos.y;
			obj.style.left = posH+"px";
			obj.style.top = posV+"px";

	}

	function warnChangeContinue() {
		document.getElementById('inbound_charity').value = 0;
		document.getElementById('orgChangeWarn').style.display = "none";
		document.getElementById('ext_users_hearaboutus').style.display = "";
		orgChange();
	}
	
	function warnChangeCancel() {
		document.getElementById('orgChangeWarn').style.display = "none";
		document.getElementById('ext_users_hearaboutus').style.display = "";
	}

//Capture Enter Key for searhc input box.
function enterSearch(evt) {

	var charCode = (evt.which) ? evt.which : event.keyCode;

	if (charCode == 13) {
		// cancel the default submit
		//event.returnValue    = false;
		//event.cancel        = true;
		doSearch();
	}

}

//Search from input box, need to reset the filter for 0-9 or abc searches.
function doSearch() {
	if (document.getElementById('orgList')) {
		document.getElementById('orgSearchFilter').value = '';
		filterOrgSearch(document.getElementById('orgSearchInput').value, 1);
	}
}

//Initiate the Search Request
function filterOrgSearch(s, pg) {

	//Disabled
	//alert("This feature has been disabled during the testing stage.");
	//return false;

	if (document.getElementById('orgList')) {
		var f = (!s ? document.getElementById('orgSearchFilter').value : '');
		var pstr = "/rpc/org-search.php"; // pstr = '/rpc/org-search.php'; 
		var runScript = "makePOSTRequest('"+pstr+"', \"action=filterOrgSearch&s=" + encodeURIComponent(s) + "&f=" + f + "&pg=" + pg+"\");";
		eval(runScript);
	}
}

//Initiate Resize of panel
//This is called from /rpc/org-search.php and multiplies the number of results to display by the height of each result
//which equals the height of the box
function initResize(numResults) {
	var emptyHeight = 65;
	var resultHeight = 20;
	var totalHeight = (numResults>0 ? resultHeight*numResults : resultHeight)+emptyHeight;

	//Show Search Results area
	document.getElementById('searchResults').style.display = '';

	//Run this after resizing
	runScript = "document.getElementById('orgList').innerHTML = document.getElementById('orgListBuffer').innerHTML;"; //

	//Resize
	elem = document.getElementById('searchResultsInner');
	if (parseInt(elem.offsetHeight) < totalHeight) {
		resizeBox('searchResultsInner', 10, totalHeight, 30, 'grow', 40, escape(runScript)); 
	} else {
		resizeBox('searchResultsInner', elem.offsetHeight, totalHeight, 30, 'shrink', 40, escape(runScript));
	}
	
}

//Change Page
function turnPage(d) {
	l = (document.getElementById('orgSearchFilter').value ? document.getElementById('orgSearchFilter').value : document.getElementById('orgSearchInput').value);
	if (d=="prev") {filterOrgSearch(l, document.getElementById('prev_page').value);}
	if (d=="next") {filterOrgSearch(l, document.getElementById('next_page').value);}
}

//Set 0-9 abc filter then initiate search
function setFilter(filter) {
	document.getElementById('orgSearchFilter').value = filter;
	filterOrgSearch(filter, 1);
}

//Resize the Box
function resizeBox(id, start, target, increment, type, timing, runScript) {

	elem = document.getElementById(id);

	if (type == 'shrink' && start > target) {

		if (document.getElementById('orgList')) {
			document.getElementById('orgList').innerHTML = 'Loading...';
		}

		newHeight = start - increment;
		if (newHeight < target) newHeight = target;
		elem.style.height = newHeight + 'px';
		if (newHeight > target) { 
			setTimeout("resizeBox('"+id+"', "+newHeight+", "+target+", "+increment+", '"+type+"', '"+timing+"', '"+runScript+"');", timing);
		} else { 
			document.getElementById('searchResultsArea').style.display = '';
			if (runScript) eval(unescape(runScript));
		}
	} else if (type == 'grow' && parseInt(elem.offsetHeight) <= target) {
		newHeight = start + increment;
		if (newHeight > target) {newHeight = target;}
		elem.style.height = newHeight + 'px';
		if (parseInt(elem.offsetHeight) < target) { 
			setTimeout("resizeBox('"+id+"', "+parseInt(elem.offsetHeight)+", "+target+", "+increment+", '"+type+"', '"+timing+"', '"+runScript+"');", timing);
		} else { 
			document.getElementById('searchResultsArea').style.display = '';
			if (runScript) eval(unescape(runScript));
		}
	}
}

//Select Organisation
function orgSelect(orgId, orgName, orgSubdomain) {
	
	//Toggle Input Boxes
	document.getElementById('orgSelect'+orgId).checked = 'checked';
	document.getElementById('searchResultsArea').innerHTML = "";
	document.getElementById('orgSearchInput').disabled = "disabled";
	document.getElementById('orgSearchInput').value = orgName;

	//Easysearch URL
	var orgUrl = "http://"+orgSubdomain+"/";

	//Resize Box
	runScript = "dataLoading(\"searchResultsArea\", \"Loading easysearch home page for...<br><b><a href='"+orgUrl+"'>"+orgName+"'s easysearch Home Page</a></b>\"); setTimeout(\"document.location.href = '"+orgUrl+"'\", 1500);";
	resizeBox('searchResultsInner', document.getElementById('searchResultsInner').offsetHeight, 50, 30, 'shrink', 40, escape(runScript));

}

function expandMoreInfo(linkTxt) {

	var obj = document.getElementById('more_info');

	if (obj.style.display == 'none') {
		obj.style.display = '';
		linkTxt.innerHTML = 'less info...';
	} else {
		obj.style.display = 'none';
		linkTxt.innerHTML = 'more info...';
	}

}
