// Copyright Ian Grant 2003/2004, unless otherwise specified 

// return value dictates whether to follow a link or not on the basis of the success of this function
function toggleSection(id) {
	UserAgent = navigator.userAgent;
	if (UserAgent.indexOf("Opera") >= 0) {
		return true
	} else {
	    if (document.getElementById(id)) {
			if (document.getElementById(id).style.display == 'block') {
				document.getElementById(id).style.display = 'none';
			} else {
				document.getElementById(id).style.display = 'block';
			}
			return false;
		} else {
			return true;
		}
	}
}

var newWindow;

function openURLInNewWindow(title,name,url,width, height, additionalParams) {
	params = '';
	
	if (width > 0) {
		if (width < screen.width) {
			// if we're specifying the width and height, then centre it on the screen
			dialogLeft = (screen.width / 2) - (width / 2);
		} else {
			dialogLeft = 0;
			width = screen.width;
		}
	} else {
		// if no width and height are specified, then fill the screen
		dialogLeft = 0;
		width = screen.width;
	}
	
	if (height > 0) {
		if (height < screen.height) {
			// if we're specifying the width and height, then centre it on the screen
			dialogTop = (screen.height / 2) - (height / 2);
		} else {
			dialogTop = 0;
			height = screen.height - 100;
		}
	} else {
		// if no width and height are specified, then fill the screen (allowing space for taskbar)
		dialogTop = 0;
		height = screen.height - 100;
	}

	if ((additionalParams.indexOf('menubar') != -1) && (is.ie4up)) {
		// ie mucks up the height if we include a menu bar, so do some adjustment
		params = 'width=' + (width - 5) + ',height=' + (height - 23) + ',dependent,left=' + dialogLeft + ',top=' + dialogTop;
	} else {
		params = 'width=' + width + ',height=' + height + ',dependent,left=' + dialogLeft + ',top=' + dialogTop;
	}	

	if (additionalParams != '') {
		params = params + ',' + additionalParams
	}
	
	newWindow = window.open (url, '', params);
	newWindow.document.close();
	newWindow.focus();
}

function openImageInNewWindow(title,url,width, height, additionalParams) {
	dialogLeft = (screen.width / 2) - (width / 2);
	dialogTop = (screen.height / 2) - (height / 2);

	if ((additionalParams.indexOf('menubar') != -1) && (is.ie4up)) {
		// ie mucks up the height if we include a menu bar, so do some adjustment
		params = 'width=' + width + ',height=' + (height - 20) + ',dependent,left=' + dialogLeft + ',top=' + dialogTop;
	} else {
		params = 'width=' + width + ',height=' + height + ',dependent,left=' + dialogLeft + ',top=' + dialogTop;
	}	

	if (additionalParams != '') {
		params = params + ',' + additionalParams
	}
	
	newWindow = window.open ('', '', params);
	
	theContent = "<html>\n<head>\n<title>" + title + "</title>\n</head>\n";
	theContent += "<body style=\"margin:0;padding:0;text-align:center;\">\n";
	theContent += "<img src='" + url + "' style=\"padding:0;margin:0;border:0;\"/>\n"
	theContent += "</body>\n</html>"
	
	newWindow.document.write(theContent);
	newWindow.document.close();
	newWindow.focus();
}

function closeOpenedWindow() {
	if (newWindow) {
		newWindow.close();
	}
}

function closeThisWindow() {
	if (window.opener) {
		window.opener.closeOpenedWindow();
	} else {
		window.close();
	}
}

// returns a random number between 1 and the value of ceiling
function getRandomNumber(ceiling) {
	random_num = (Math.round((Math.random()*(ceiling-1))+1))

	return random_num;
}

// selects everything in a form list, except null values and the specified exception
function selectAllFromList(id, exception) {
	if (document.getElementById(id)) {
		if (document.getElementById(id).type=="select-multiple") {
			// run through the complete set of options
			for (var i=0;i<document.getElementById(id).options.length;i++) {
				if ((document.getElementById(id).options[i].value != 0) && (document.getElementById(id).options[i].value != "")) {
					// if the exception isn't met, then select this option
					if ((exception == "") || (document.getElementById(id).options[i].text != exception)) {
						document.getElementById(id).options[i].selected=true;
					} else {
						document.getElementById(id).options[i].selected=false;
					}
				}
			}
		}
	}
}

