function CheckGetElementById(suppressAlert) {
	if (document.getElementById) {
		return true;
	} else {
		if(!suppressAlert)
			alert("Your web browser doesn't support this functionality.");
		return false;
	}
}

function GetElement(clientId) {
	if (clientId && CheckGetElementById())
		return document.getElementById(clientId);
	else
		return undefined;
}

function GetElementValue(clientId) {
	var el = GetElement(clientId);
	return (el ? el.value : null);
}

function SetFocus(clientId) {
	if (CheckGetElementById(true)) {
		var el = document.getElementById(clientId);
		if (el && el.focus)
			el.focus();
	}
}

function ClearInput(clientId) {
	var el = GetElement(clientId);
	if (el)
		el.value = '';
}

function setCheckboxes(baseId, boxCount, setValue) {
	var i, thisId, thisCB;

	if (CheckGetElementById()) {
		for (i = 0; i < boxCount; i++) {
			thisId = baseId + '_' + i;
			
			thisCB = GetElement(thisId);
			
			if (thisCB != undefined) {
				thisCB.checked = setValue;
			}
		}
	}
}

function openDefaultPopupWindow(url) {
	openWindow(url, "popup", 400, 300);
}

function openPopupWindow(url, width, height) {
	openWindow(url, "popup", width, height);
}

function openWindow(url, windowName, width, height) {
	openWindowParams(url, windowName, width, height, true, true);
}

function openWindowParams(url, windowName, width, height, scrollbars, resizable) {
	var options = "width="+width+",height="+height+",scrollbars="+(scrollbars?"yes":"no")+",resizable="+(resizable?"yes":"no")+",location=no,menubar=no,status=no,toolbar=no,fullscreen=no";
	openWindowOptions(url, windowName, options);
}

function SanitiseWindowName(origName) {
	var newName = origName;
	var theRegExp = new RegExp("[^a-zA-Z0-9-_:]","g");
	var resArray;
	var found, code, repl, i;
	
	while ((resArray = theRegExp.exec(origName)) != null)
	{
		found = origName.substr(resArray.index,1);
		code = found.charCodeAt(0);
		repl = code.toString();
		
		newName = newName.replace(found, repl);
	}
	
	
	return newName;
}

function openWindowOptions(url, windowName, options) {
	var newWindow = window.open(url, SanitiseWindowName(windowName), options);
	newWindow.focus();
}

function openImageWindow(title, dirCode, imageName, imageExt, imageWidth, imageHeight) {
	var windowWidth = imageWidth + 24;
	var windowHeight = imageHeight + 82;
	var url = "/Popups/image.popup.aspx?d=" + escape(dirCode) + "&src=" + escape(imageName + "." + imageExt) + "&title=" + escape(title);
	var windowName = "Img_" + escape(dirCode) + "_" + escape(imageName);
	
	openWindowParams(url, windowName, windowWidth, windowHeight, false, true);
}

function openMapWindow(mapId, mapWidth, mapHeight) {
	var windowWidth = Math.max(mapWidth + 64, 400);
	var windowHeight = mapHeight + 300;
	var url = "/Popups/map.popup.aspx?id=" + mapId;
	var windowName = "Map_" + mapId;
	
	openWindow(url, windowName, windowWidth, windowHeight);
}

function openIntroCopyWindow(id) {
	var url = "/Popups/article.popup.aspx?type=introcopy&id=" + id;
	var windowName = "IntroCopy_" + id;
	
	openWindow(url, windowName, 400, 400);
}

function openLegislationWindow(id) {
	var url = "/Popups/article.popup.aspx?type=legislation&id=" + id;
	var windowName = "Legislation_" + id;
	
	openWindow(url, windowName, 400, 400);
}

function swapImgDivs(divName1, divName2, buttonName) {
	if (CheckGetElementById(false)) {
		var div1 = document.getElementById(divName1);
		var div2 = document.getElementById(divName2);
		var btn = document.getElementById(buttonName);
		var btnText = btn.firstChild;
		
		if (!div1 || !div2 || !btn) {
			alert("Your web browser doesn't support this functionality.");
		} else {
			if (div1.style.display == "none") {
				div1.style.display = "";
				div2.style.display = "none";
				
				if (btnText) {
					btnText.nodeValue = "Browse Images";
				}
			} else {
				div1.style.display = "none";
				div2.style.display = "";
				
				if (btnText) {
					btnText.nodeValue = "Back";
				}
			}
		}
	}
}

