function changeSection(objSelect) {
	if (!objSelect) return;
	var index = objSelect.selectedIndex;
	var sectionID = objSelect.options[index].value;
	var weaID = "HASH6731e5b341863c8b95d12c";
	if (sectionID.length) sectionID = "." + sectionID;
	if (index > 0)
		gsdl_goto(gsdlURL + '&amp;a=d&amp;d=' + weaID + sectionID);
}

function gsdlTrimFieldValue(objField) {
	if (objField != null && objField.value != null) {
		objField.value = objField.value.replace(/^\\s+/,'');
		objField.value = objField.value.replace(/\\s+$/,'');
	}
}

function openWindow (address) {
	open(fix_url2(address), '_blank', 'width=600,height=400,menubar=yes,location=yes,status=yes,toolbar=yes,resizable=yes,scrollbars=yes');
}

function openWindow2 (address, width, height) {
	open(fix_url2(address), '_blank', 'width='+width+',height='+height+',menubar=no,location=no,status=yes,toolbar=no,resizable=yes,scrollbars=yes');
}

function openExtLink(address) {
	open(address, '_blank', 'width=600,height=400,menubar=yes,location=yes,status=yes,toolbar=yes,resizable=yes,scrollbars=yes');
}

function openCourseWindow(address, width, height, title) {
	var offset = 25, posX, posY;
	if (screen.availHeight > height) height = screen.availHeight - 2*offset;
	if (screen.availWidth > width) width = screen.availWidth - 2*offset;
	posY = (screen.availHeight - height) / 2;
	posX = (screen.availWidth - width) / 2;
	open(address, '_blank', 'top='+posY+',left='+posX+',width='+width+',height='+height+',menubar=no,location=no,status=no,toolbar=no,resizable=yes,scrollbars=yes');
}

function openFixedWindow (address, width, height) {
	var posX, posY, nHeight, nWidth, offset = 10;
	nHeight = (screen.availHeight > height ? height :  screen.availHeight - 2*offset);
	nWidth = (screen.availWidth > width ? width : screen.availWidth - 2*offset);
	posY = (screen.availHeight - nHeight) / 2;
	posX = (screen.availWidth - nWidth) / 2;
	open(address, '_blank', 'top='+posY+',left='+posX+',width='+width+',height='+height+',menubar=no,location=no,status=no,toolbar=no,resizable=yes,scrollbars=no');
}

function resizeFixedWindow (width, height) {
	var posX, posY, nHeight, nWidth, offset = 10;
	nHeight = (screen.availHeight > height ? height :  screen.availHeight - 2*offset);
	nWidth = (screen.availWidth > width ? width : screen.availWidth - 2*offset);
	posY = (screen.availHeight - nHeight) / 2;
	posX = (screen.availWidth - nWidth) / 2;
	moveTo (posX, posY); resizeTo (nWidth, nHeight);
}

function openModalWindow (address, width, height) {
	if (isGecko) {
		window.open(address, '', 'chrome,modal');
	} else {
		var ret = window.showModalDialog(address, '', 'dialogWidth:'+width+'px;dialogHeight:'+height+'px;center:1;status:0;help:0');
		if (ret != null && ret != undefined && ret != "") {
			var idx = ret.indexOf(":");
			if (idx > -1) {
				var strAction = ret.substr(0, idx);
				if (strAction == "reload") {
					window.location.reload(true);
				} else if (strAction == "add") {
					var strURL = gsdlLocationAddParam(window.location, ret.substr(idx+1));
					window.setTimeout("window.location = '" + strURL + "'", 400);
				} else if (strAction == "new") {
					openWindow(ret.substr(idx+1));
				} else if (strAction == "addnew") {
					openWindow(window.location.href + ret.substr(idx+1));
				} else if (strAction == "go") {
					window.setTimeout("window.location = '" + ret.substr(idx+1) + "'", 400);
				} else {
					window.setTimeout("window.location = '" + ret + "'", 400);
				}
			}
		}
	}
}

function openPrintPreviewWindow(address, width, height) {
	if (IE4plus && !IE4) {
		var ampReg = new RegExp('&', 'g');
		address = address.replace(ampReg, "&" + "amp" + ";");
	} else {
		var ampReg = new RegExp('&amp;', 'g');
		address = address.replace(ampReg, "&");
	}
	open(address, '_blank', 'width='+width+',height='+height+',menubar=yes,location=no,status=yes,toolbar=yes,resizable=yes,scrollbars=yes');
}

function openExportWindow(format, encoding, width, height) {
	var strURL = gsdlURL + '&amp;cl=' + cgiarg.cl + '&amp;d=' + cgiarg.d;
	strURL += '&amp;a=p&amp;p=exportmsg&amp;xf=' + format + '&amp;ew=' + encoding;
	openWindow2(fix_url(strURL), width, height);
}

//
// Browser Detection
//
isMac = (navigator.appVersion.indexOf("Mac")!=-1) ? true : false;
NS4 = (document.layers) ? true : false;
IEmac = ((document.all)&&(isMac)) ? true : false;
IE4plus = (document.all) ? true : false;
IE4 = ((document.all)&&(navigator.appVersion.indexOf("MSIE 4.")!=-1)) ? true : false;
IE5 = ((document.all)&&(navigator.appVersion.indexOf("MSIE 5.")!=-1)) ? true : false;
IE55 = ((document.all)&&(navigator.appVersion.indexOf("MSIE 5.5")!=-1)) ? true : false;
IE6 = ((document.all)&&(navigator.appVersion.indexOf("MSIE 6.")!=-1)) ? true : false;
ver4 = (NS4 || IE4plus) ? true : false;
NS6 = (!document.layers) && (navigator.userAgent.indexOf('Netscape')!=-1)?true:false;
isGecko = (navigator.userAgent.indexOf(' Gecko/')!=-1)?true:false;
IE5plus = IE5 || IE6;
IE55plus = IE55 || IE6;
IEMajor = 0;

if (IE4plus)
{
	var start = navigator.appVersion.indexOf("MSIE");
	var end = navigator.appVersion.indexOf(".",start);
	IEMajor = parseInt(navigator.appVersion.substring(start+5,end));
	IE5plus = (IEMajor>=5) ? true : false;
	IE55plus = (IE55 || ((IEMajor>5) ? true : false));
}

function gsdl_encode(parString) {
	if (NS4 || NS6) {
		return escape(parString);
	}
	var str = "";
	for(i=0; i < parString.length; i++) {
		if(parString.charCodeAt(i) < 0x80) {
			str += String.fromCharCode(parString.charCodeAt(i));
		}
		else if (parString.charCodeAt(i) < 0x0800) {
			str += String.fromCharCode(parString.charCodeAt(i)>> 6 | 0xC0);
			str += String.fromCharCode(parString.charCodeAt(i) & 0x3F | 0x80);
		}
		else if (parString.charCodeAt(i) < 0x10000) {
			str += String.fromCharCode(parString.charCodeAt(i)>> 12 | 0xE0);
			str += String.fromCharCode(parString.charCodeAt(i)>> 6 & 0x3F | 0x80);
			str += String.fromCharCode(parString.charCodeAt(i) & 0x3F | 0x80);
		}
		else {
			str += String.fromCharCode(parString.charCodeAt(i)>> 18 | 0xF0);
			str += String.fromCharCode(parString.charCodeAt(i)>> 12 & 0x3F | 0x80);
			str += String.fromCharCode(parString.charCodeAt(i)>> 6 & 0x3F | 0x80);
			str += String.fromCharCode(parString.charCodeAt(i) & 0x3F | 0x80);
		}
	}
	return escape(str);
}

function gsdlLocationAddParam(locationObj, strParams) {
	fix_location_hash(locationObj);
	var strArgs = locationObj.search.substr(1);

	var strNewURL = locationObj.protocol + "//" + locationObj.host;
	strNewURL += locationObj.pathname;
	if (strArgs.length) strArgs += '&';
	strArgs += strParams;

	strNewURL += "?" + strArgs;
	if (locationObj.hash.length) strNewURL += locationObj.hash;
	
 
	if (IE5plus) {
		var ampReg = new RegExp('&', 'g');
		strNewURL = strNewURL.replace(ampReg, "&" + "amp" + ";");
	}
	return strNewURL;
}

function gsdl_goto(strURL) {
	var ampReg = new RegExp('&' + 'amp' + ';', 'g');
	strURL = strURL.replace(ampReg, "&");
	if (IE5plus) {
		var ampReg2 = new RegExp('&', 'g');
		strURL = strURL.replace(ampReg2, "&" + "amp" + ";");
	}
	window.location = strURL;
	return false;
}

function gsdl_goto_ex(obj, strURL) {
	var ampReg = new RegExp('&' + 'amp' + ';', 'g');
	strURL = strURL.replace(ampReg, "&");
	if (IE5plus) {
		var ampReg2 = new RegExp('&', 'g');
		strURL = strURL.replace(ampReg2, "&" + "amp" + ";");
	}
	obj.href = strURL;
	return false;
}

function fix_url(strURL) {
	var ampReg = new RegExp('&' + 'amp' + ';', 'g');
	strURL = strURL.replace(ampReg, "&");
	if (IE5plus) {
		var ampReg2 = new RegExp('&', 'g');
		strURL = strURL.replace(ampReg2, "&" + "amp" + ";");
	}
	return strURL;
}

function fix_url2(strURL) {
	var ampReg = new RegExp('&' + 'amp' + ';', 'g');
	strURL = strURL.replace(ampReg, "&");
	return strURL;
}

function fix_location_hash (oLocationObj) {
	if (IE4 && oLocationObj.search != null) {
		var re = new RegExp ('#.*');
		var arrMatch = oLocationObj.search.match(re);

		if (arrMatch != null && arrMatch.length) {
			oLocationObj.hash = arrMatch[0];
			oLocationObj.search = oLocationObj.search.replace(re, "");
		}
	}
}

var gLocationObj = null;

function clearForm1() {
	var oLocationObj = gLocationObj;
	if (oLocationObj == null)
		oLocationObj = location;
	fix_location_hash(oLocationObj);
	var strArgs = oLocationObj.search.substr(1);
	if (! strArgs.length) return;
	var arr = strArgs.split('&');
	var re = new RegExp ('^(q|fqv|r)=.*', 'i');
	var re2 = new RegExp ('&{2,}', 'g');
	var strNewURL = "";

	strArgs = "";
	for (i = 0; i < arr.length; i++) {
		if (strArgs.length) strArgs += '&';
		arr[i] = arr[i].replace(re, "");
		strArgs += arr[i];
	}

	strNewURL = oLocationObj.protocol + "//" + oLocationObj.host;
	strNewURL += oLocationObj.pathname;
	if (strArgs.length) strArgs += '&';
	strArgs += "q=&fqv=&r=1";

	strArgs = strArgs.replace(re2, "&");
	strNewURL += "?" + strArgs;
	if (oLocationObj.hash.length) strNewURL += oLocationObj.hash;
 
	if (IE5plus) {
		var ampReg = new RegExp('&', 'g');
		strNewURL = strNewURL.replace(ampReg, "&" + "amp" + ";");
	}
	location.href = strNewURL;
}

function gsdlInitValue(field, value) {
	if (field == 'undefined' || field == null)
	{
		return;
	}
	if (field.type == "select-one") {
		var i;
		for (i=0; i < field.options.length;i++) {
			if (value == field.options[i].value) {
				field.options[i].selected = true;
				break;
			}
		}
		} else if (field.type == "checkbox" || field.type == "radio") {
		field.checked = (value == "1"?true:false);
		} else { // "text", "textarea", "password", "hidden"
		field.value = value;
	}
}

function gsdlGetValue(field) {
	if (field.type == "select-one") {
		return field.options[field.selectedIndex].value;
	} else if (field.type == "checkbox" || field.type == "radio"){
		return field.checked ? "1" : "0";
	} else {
		return field.value;
	}
}

function close_detach() {
	close();
}

var loaded = new Array();
function gbutton (image, onimage) {
	if (image && image.src && (image.out == null || typeof(image.out) == typeof(void(0)))) {
		var s = image.src;
		image.out = new Image();
		image.out.src = s;
		image.over = new Image();
		image.over.src = onimage;
		loaded[image.name] = image;
	}
}

function roll (imagename, over) {
	if (document.images) {
		if (over) i = "over";
		else i = "out";
		image = loaded[imagename];
		if (image) image.src = eval("image."+i+".src");
	}
}

function newImage(arg) {
	if (document.images) {
		rslt = new Image();
		rslt.src = arg;
		return rslt;
	}
}

function changeImages() {
	if (document.images && (preloadFlag == true)) {
		for (var i = 0; i < changeImages.arguments.length; i += 2) {
			document[changeImages.arguments[i]].src = changeImages.arguments[i+1];
		}
	}
}

var preloadFlag = false;
function preloadImages() {
	if (document.images) {
		preloadFlag = true;
	}
}

function display_prefs() {
	var win;
	var wndWidth = 375;
	var wndHeight = 80;
	var wndX = (screen.width-wndWidth)/2;
	var wndY = (screen.height-wndHeight)/2;
	var strFeatures = 'location=no,menubar=no,scrollbars=yes,status=no,toolbar=no,width='+wndWidth+',height='+wndHeight+',resizable=yes';
	if (NS4 || NS6) strFeatures += ',screenX='+parseInt(wndX)+',screenY='+parseInt(wndY);
	else if (IE4plus) strFeatures += ',left='+parseInt(wndX)+',top='+parseInt(wndY);

	var strURL = gsdlURL + '&amp;a=p&amp;p=subjectprefs';
	win = open(fix_url(strURL), Math.round(Math.random()*10000), strFeatures);
}

function display_docprefs() {
	var win;
	var wndWidth = 530;
	var wndHeight = 50;
	var wndX = (screen.width-wndWidth)/2;
	var wndY = (screen.height-wndHeight)/2;
	var strFeatures = 'location=no,menubar=no,scrollbars=yes,status=no,toolbar=no,width='+wndWidth+',height='+wndHeight+',resizable=yes';
	if (NS4 || NS6) strFeatures += ',screenX='+parseInt(wndX)+',screenY='+parseInt(wndY);
	else if (IE4plus) strFeatures += ',left='+parseInt(wndX)+',top='+parseInt(wndY);

	var strURL = gsdlURL + '&amp;a=p&amp;p=documentprefs';
	win = open(fix_url(strURL), Math.round(Math.random()*10000), strFeatures);
}

function xInsDoc(parent, text, jn)
{
	var doc = insDoc(parent, gLnk('S', text, 'javascript:display_doc(\\\'' + jn + '\\\')'));
	doc.xID = jn;
}

function xInsFld(parent, text, jn)
{
	var fld = insFld(parent, gFld(text, 'javascript:display_doc(' + String.fromCharCode(34) + jn + String.fromCharCode(34) + ')'));
	fld.xID = jn;
	return fld;
}

function synchPage(linkID)
{
	if (foldersTree == 'undefined' || foldersTree == null)
	{
		window.setTimeout('synchPage("'+ linkID + '")', 400);
	}
	var folderObj;
	docObj = findObj(linkID);
	if (docObj != "undefined")
	{
		docObj.forceOpeningOfAncestorFolders();

		if (typeof docObj.setState != "undefined")
		{ //is folder
			if (!docObj.isOpen)
				clickOnNodeObj(docObj);
		}
		highlightObjLink(docObj);

		//Scroll the tree window to show the selected node
		//Other code in these functions needs to be changed to work with
		//frameless pages, but this code should, I think, simply be removed
		if (typeof document.body != "undefined") //scroll doesn work with NS4, for example
			document.body.scrollTop = docObj.navObj.offsetTop
	}
}

function array_remove_at(arrObject, nIndex, nLength)
{
	if (arrObject.splice != null) {
		arrObject.splice(nIndex, nLength);
	} else {
		var carray = new Array();
		if (nIndex == 0) {
			if (nIndex+nLength > arrObject.length) {
			} else {
				carray = arrObject.slice(nLength);
			}
		} else {
			if (nIndex+nLength > arrObject.length) {
				carray = new Array().concat(arrObject.slice(0,nIndex));
			} else {
				carray = new Array().concat(arrObject.slice(0,nIndex), arrObject.slice(nIndex+nLength));
			}
		}
		arrObject.length = 0;
		var i=0;
		for(i=0; i < carray.length; ++i) {
			arrObject[i] = carray[i];
		}
	}
}

function gsdlGetElement(strName) {
	var elem = null;
	if (document.getElementById) {
		elem = document.getElementById(strName);
	} else if (document.all) {
		elem = document.all[strName];
	} else {
		eval('elem = document.' + strName);
	}
	return elem;
}

function __coverErrorInit__() {
	var strImg = httpCollectionImage + '/blankcover\\.gif';
	var re = new RegExp('\\/', 'g');
	strImg = strImg.replace(re,'\\/');
	g_imgRegExp = new RegExp(strImg + '$', 'i');
}

var g_imgRegExp = 0;

function CoverError() {
	if (g_imgRegExp != 0 && this.src.search(g_imgRegExp) < 0)
		this.src = httpCollectionImage + '/blankcover.gif';
}

function onDocumentLoad() {
	var book = gsdlGetElement('gsdlbookcover');
	if (book != null) book.onError= CoverError;
}