// Script for both the BNC main page and the left and right content frames

// Used to check if this file loaded
var bncjsloaded = 1;

var textIdx = '/cgi/t/text/text-idx'
var imageDir = '/b/bnc/images/'

// Code to Alias
var bookCodeAliases = new Array();

// Oude testament
bookCodeAliases.gen = 'genesis'
bookCodeAliases.exo = 'exodus' 
bookCodeAliases.lev = 'leviticus'
bookCodeAliases.num = 'numeri'
bookCodeAliases.deu = 'deuteronomium'
bookCodeAliases.joz = 'jozua'
bookCodeAliases.ric = 'rechters'
bookCodeAliases.rut = 'ruth'
bookCodeAliases.sa1 = 'samuel_1'
bookCodeAliases.sa2 = 'samuel_2'
bookCodeAliases.kn1 = 'koningen_1'
bookCodeAliases.kn2 = 'koningen_2'
bookCodeAliases.kr1 = 'kronieken_1'
bookCodeAliases.kr2 = 'kronieken_2'
bookCodeAliases.ezr = 'ezra'
bookCodeAliases.neh = 'nehemia'
bookCodeAliases.est = 'ester'
bookCodeAliases.job = 'job'
bookCodeAliases.psa = 'psalmen' 
bookCodeAliases.spr = 'spreuken'
bookCodeAliases.ecc = 'prediker'
bookCodeAliases.hoo = 'hooglied'
bookCodeAliases.jes = 'jesaja'
bookCodeAliases.jer = 'jeremia' 
bookCodeAliases.kla = 'klaagliederen'
bookCodeAliases.eze = 'ezechiel'
bookCodeAliases.dan = 'daniel'
bookCodeAliases.hos = 'hosea' 
bookCodeAliases.joe = 'joel'
bookCodeAliases.amo = 'amos'
bookCodeAliases.oba = 'obadja'
bookCodeAliases.jon = 'jona'
bookCodeAliases.mic = 'micha'
bookCodeAliases.nah = 'nahum'
bookCodeAliases.hab = 'habakuk'
bookCodeAliases.sef = 'sefanja'
bookCodeAliases.hag = 'haggai'
bookCodeAliases.zac = 'zacharia'
bookCodeAliases.mal = 'maleachi'

// Deuterocanonieke boeken
bookCodeAliases.tob = 'tobit'
bookCodeAliases.jdt = 'judit'
bookCodeAliases.egr = 'ester-grieks'
bookCodeAliases.ma1 = 'makkabeeen_1'
bookCodeAliases.ma2 = 'makkabeeen_2'
bookCodeAliases.wis = 'wijsheid'
bookCodeAliases.sir = 'wijsheid-van-jezus-sirach'
bookCodeAliases.bar = 'baruch'
bookCodeAliases.brj = 'brief-van-jeremia'
bookCodeAliases.dgr = 'toevoegingen-aan-daniel'
bookCodeAliases.geb = 'het-gebed-van-manasse'

// Nieuwe Testament
bookCodeAliases.mat = 'matteus'
bookCodeAliases.mar = 'marcus'
bookCodeAliases.luc = 'lucas'
bookCodeAliases.joh = 'johannes'
bookCodeAliases.han = 'handelingen'
bookCodeAliases.rom = 'romeinen'
bookCodeAliases.ko1 = 'korintiers_1'
bookCodeAliases.ko2 = 'korintiers_2'
bookCodeAliases.gal = 'galaten'
bookCodeAliases.efe = 'efeziers'
bookCodeAliases.fil = 'filippenzen'
bookCodeAliases.kol = 'kolossenzen'
bookCodeAliases.te1 = 'tessalonicenzen_1'
bookCodeAliases.te2 = 'tessalonicenzen_2'
bookCodeAliases.ti1 = 'timoteus_1'
bookCodeAliases.ti2 = 'timoteus_2'
bookCodeAliases.tts = 'titus'
bookCodeAliases.phi = 'filemon'
bookCodeAliases.heb = 'hebreeen'
bookCodeAliases.jam = 'jakobus'
bookCodeAliases.pe1 = 'petrus_1'
bookCodeAliases.pe2 = 'petrus_2'
bookCodeAliases.jo1 = 'johannes_1'
bookCodeAliases.jo2 = 'johannes_2'
bookCodeAliases.jo3 = 'johannes_3'
bookCodeAliases.jud = 'judas' 
bookCodeAliases.ope = 'openbaring'

// Ester Grieks chapters
var egrChapters = [
    // Chapter chunk 1
    'A', '1', '2', '3a', 'B',
    // Chapter chunk 2
    '3b', '4', 'C', 'D', '5',
    // Chapter chunk 3
    '6', '7', '8a', 'E', '8b',
    // Chapter chunk 4
    '9', '10', 'F', 'F' // F is the last chapter. Added one additional F to address BK-EGR:4:4
    					// which is the DIV2 TYPE="NOTES" after the last chapter F
]


function alertLinkNotFound() {
	alert('The link you selected does not exist. Please mail this error message to bijbel@aup.nl with a short description where you found the link.');
}


/*
Correctly handle PNG transparency in Win IE 5.5 & 6.
http://homepage.ntlworld.com/bobosola. Updated 18-Jan-2006.
*/

var arVersion = navigator.appVersion.split("MSIE")
var version = parseFloat(arVersion[1])

function fixPNG(img) {
	if ((version >= 5.5) && (version < 7) && (document.body.filters)) {
		var imgName = img.src.toUpperCase()
		if (imgName.substring(imgName.length - 3, imgName.length) == "PNG") {
			var imgID = (img.id) ? "id='" + img.id + "' " : ""
			var imgClass = (img.className) ? "class='" + img.className + "' " : ""
			var imgTitle = (img.title) ? "title='" + img.title  + "' " : "title='" + img.alt + "' "
			var imgStyle = "display:inline-block;" + img.style.cssText
			var strNewHTML = "<span " + imgID + imgClass + imgTitle
				+ " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;"
				+ imgStyle + ";filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
				+ "(src=\'" + img.src + "\', sizingMethod='scale');\"/></span>"
			img.outerHTML = strNewHTML
		}
    }
}

function getAliasForBookCode(bookCode) {
	var alias = ''
	if ((typeof(bookCode) != 'undefined') && (bookCode != '')
	&& (typeof(bookCodeAliases[bookCode]) != 'undefined')) {
		alias = bookCodeAliases[bookCode];
	}
	return alias
}

function getAnchorForBibRef(bibRef) {
	// GEN:1.1
	var anchor = '';
	var bookCode = getBookCodeFromBibRef(bibRef)
	if (bookCode) {
		var chapter = getChapterFromBibRef(bibRef)
		var verse = getVerseFromBibRef(bibRef)
		anchor = bookCode.toUpperCase() + '-' + chapter
		
		// No verse and first verse
		if (((verse == '') || (verse == '1')) && isFirstChapterOfChapterChunk(bookCode, chapter)) {
			// Jumps made with chapter chunk already loaded need anchor to return to top of frame, others need ''
			if (getChapterChunkFromBibRef(bibRef) == chapterChunk) {
				return anchor
			}
			return ''
		}
		
		if ((verse != '') && (verse != '1')) {
			if (verse.indexOf('pt') >= 0) {
				anchor += verse
			} else {
				anchor += '.' + verse
			}
		}
	}
	return anchor
}


// Helper for getArg. Return the named arg from query
function getA(query, name) {
	if (query) {
		query = query.substr(1) // Remove question mark
		var args = query.split(/[;&]/)
		name += '=';
		for (var i = 0; i < args.length; i++) {
			var arg = args[i];
			if (arg.substr(0, name.length) == name) {
				return decodeURIComponent(arg.substr(name.length))
			}
		}
	}
	return null
}
	
// Return the value of argument name
function getArg(name, defaultValue) {
	var arg = getA(window.location.search, name)
	if (arg == null) {
		// Support cached pages that lost their query string and pages
		// like /terzijde/alias that don't have a fully qualified query string
		if (typeof(savedQuery) != 'undefined') {
			arg = getA('?' + savedQuery, name)
		}
	}
	if (arg == null) {
		if (typeof(defaultValue) == 'undefined') {
			defaultValue = ''
		}
		arg = defaultValue
	}
	return arg
}

function getAside(id, bibRef, frameName, options) {

	if ((typeof(id) == 'undefined') || (id == '')) {
		alertLinkNotFound()
		return
	}

	// Dispatch to index frame
	if ((window.name == 'leftFrame') || (window.name == 'rightFrame')) {
		if (parent.parent.name == 'index') {
			parent.parent.getAside(id, bibRef, window.name, options)
		} else {
			getAsideTop(id, bibRef, window.name, options)
		}
		return
	}
	
	// Now running in index.html
	// pageName defined in head index.html
	if ((typeof(bottomFrame) == 'undefined')
	|| (typeof(bottomFrame.leftFrame) == 'undefined')
	|| (typeof(bottomFrame.rightFrame) == 'undefined')) {
		// Currently not in Bijbelboeken portal  or lost frameset
		getAsideTop(id, bibRef, frameName, options)
		return
	}
	
	// Set left frame when a bibRef is passed
	if ((typeof(bibRef) != 'undefined') && (bibRef != '')) {
		var newBookCode = getBookCodeFromBibRef(bibRef)
		if (newBookCode != bookCode) {
			getAsideTop(id, bibRef, frameName, options)
			return
		}
		
		var newChapterChunk = getChapterChunkFromBibRef(bibRef)
		if (newChapterChunk != chapterChunk) {
			getAsideTop(id, bibRef, frameName, options)
			return
		}
		
		if ((typeof(frameName) == 'undefined') || (frameName != 'leftFrame')) {		
			bottomFrame.leftFrame.location.hash = getAnchorForBibRef(bibRef)
		}
	}
		
	// Set right frame
	var url = textIdx
		+ '?c=bnc;cc=bnc;view=text;rgn=div1;frame=right;parentpage=bijbelboeken'
		+ ';aside=' + encodeURIComponent(id)
		+ getHighLightParams(bottomFrame.rightFrame, frameName, 'right', 1, 1, options)
	
	bottomFrame.rightFrame.location = url;
} // getAside

function getAsideTop(id, bibRef, frameName, options) {
	if ((typeof(bibRef) == 'undefined') || (bibRef == '')) {
		bibRef = 'GEN-1.1'
	}
	var url = getBibRefURL(bibRef) + '/' + id
		
	// Add highlight and set location
	if (window.name == 'leftFrame') {
		parent.parent.location = url
	} else if (window.name == 'rightFrame') {
		// use window.name here
		url += getHighLightQuery(parent.rightFrame, window.name, 'right', 0, 0, options); // 0, 0 no cash=no, no hash
		parent.parent.location = url
	} else {
		// use frameName here
		if (typeof(bottomFrame) != 'undefined') {
			url += getHighLightQuery(bottomFrame.rightFrame, frameName, 'right', 0, 0, options) // 0 no cash=no, no hash
		}
		window.location = url
	}
}

function getBibRef(bibRef, frameName, options) {
	// No bibref
	if ((typeof(bibRef) == 'undefined') || (bibRef == '')) {
		alertLinkNotFound()
		return
	}
	
	// Comin from left or right frame 
	if ((window.name == 'leftFrame') || (window.name == 'rightFrame')) {
		if (parent.parent.name == 'index') {
			parent.parent.getBibRef(bibRef, window.name, options)
		} else {
			getBibRefTop(bibRef, window.name, options)
		}
		return
	}
	
	// Now running in index.html
	var newBookCode = getBookCodeFromBibRef(bibRef)
	var newChapterChunk = getChapterChunkFromBibRef(bibRef)
	
	// pageName defined in head index.html
	if ((typeof(pageName) == 'undefined')
	|| (pageName != 'bijbelboeken')
	|| (typeof(bottomFrame) == 'undefined')
	|| (typeof(bottomFrame.leftFrame) == 'undefined')
	|| (newBookCode != bookCode)) {
		// Set URL main window
		getBibRefTop(bibRef, frameName, options)
		return
	}
	
	var highLight = '';
	if ((typeof(options) == 'undefined') || (options != 'noquery')) {
		 // 1: Add cache=no, 0 don't add anchor
		highLight = getHighLightParams(bottomFrame.rightFrame, frameName, 'left', 1, 0)
	}
	
	if (newChapterChunk != chapterChunk) {
		// Same book, chapter chunk mismatch
		var url = textIdx
				+ '?c=bnc;cc=bnc;view=text;rgn=div1;frame=left;parentpage=bijbelboeken;bibref='
				+ bibRef
				+ highLight
				+ '#' + getAnchorForBibRef(bibRef)
		chapterChunk = newChapterChunk
		bottomFrame.leftFrame.location = url
	} else {
		// Same book, same chapter chunk, set hash, unless highlighting needed
		if ((highLight != '') && (bottomFrame.leftFrame.getArg('q1', '') == '')) {
			getBibRefTop(bibRef, frameName, options)
		} else {
			bottomFrame.leftFrame.location.hash = getAnchorForBibRef(bibRef)
		}
	}
} // getBibRef

// Load bibref into topframe
// window.name used for creating url
// frameName used for highlight
function getBibRefTop(bibRef, frameName, options) {
	var url = getBibRefURL(bibRef)
	var aliasAside = '';
	if (window.name == 'rightFrame') {
		if (typeof(currentAlias) != 'undefined') {
			aliasAside = currentAlias
		}
	} else {
		if (window.name == 'leftFrame') {
			if ((typeof(parent.rightFrame) != 'undefined')
			&& (typeof(parent.rightFrame.currentAlias) != 'undefined'))			{
				aliasAside = parent.rightFrame.currentAlias
			}
		} else {
			if ((typeof(bottomFrame) != 'undefined')
			&& (typeof(bottomFrame.rightFrame) != 'undefined')
			&& (typeof(bottomFrame.rightFrame.currentAlias) != 'undefined')) {
				aliasAside = bottomFrame.rightFrame.currentAlias
			}
		}
	}
	
	if (aliasAside != '') {
		url += '/' + aliasAside
		// Add anchor. Should not add it when coming from overlib rollover
		// if ((typeof(options) == 'undefined') || ((options & 1) = 0)) {
		 	url += '/' + bibRef;
		//}		
	}

	// Add highlight and set location
	if (window.name == 'leftFrame') {
		parent.parent.location = url
	} else if (window.name == 'rightFrame') {
		if ((typeof(options) == 'undefined') || (options != 'noquery')) {
			// use window.name here
			url += getHighLightQuery(parent.rightFrame, window.name, 'left', 0, 0)
		}
		parent.parent.location = url
	} else {
		if ((typeof(bottomFrame) != 'undefined')
		&& ((typeof(options) == 'undefined') || (options != 'noquery'))) {
			// use frameName here
			url += getHighLightQuery(bottomFrame.rightFrame, frameName, 'left', 0, 0)
		}
		window.location = url
	}
} // getBibRefTop

function getBibRefURL(bibRef) {
	var url = '/bijbelboeken'
	var alias = getAliasForBookCode(getBookCodeFromBibRef(bibRef))
	if (alias) {
		url += '/' + alias
		var chapter = getChapterFromBibRef(bibRef)
		var verse = getVerseFromBibRef(bibRef);
		if (chapter) {
			if ((chapter != '1') || ((verse != '') && (verse != '1'))) {
				url += '/' + chapter;
				if (verse && (verse != '1')) {
					url += '/' + verse
				}
			}
		}
	}
	return url
}

function getBookCodeFromBibRef(bibRef) {
	var bookCode = '';
	if (typeof(bibRef) != 'undefined') {
		bookCode = bibRef.substr(0, 3).toLowerCase();
	}
	return bookCode
}

// Return chapter chunk number for chapter
function getChapterChunkForChapter(bookCode, chapter) {
	bookCode = bookCode.toLowerCase()
    if (bookCode == 'dgr') {
        // DGR-A, DGR-B, DGR-C
        return 1;
    }
    if (bookCode == 'egr') {
        chapter = chapter.toLowerCase();
        var newChapter = 0;
        for(var i = 0; i < egrChapters.length; i++) {
            newChapter++;
            if (egrChapters[i].toLowerCase() == chapter) {
                break;
            }
        }
        if (newChapter) {
        	chapter = newChapter
        } else {
        	return 1
        }
    } else {
    	if ((typeof(chapter) == 'undefined') || (chapter == '') || (chapter == 0)) {
    		return 1
    	}
    }
    
    return Math.floor((chapter - 1) / 5) + 1;
}

function getChapterChunkFromBibRef(bibRef) {
    var bookCode = getBookCodeFromBibRef(bibRef);
    var chapter = getChapterFromBibRef(bibRef);
    return getChapterChunkForChapter(bookCode, chapter);
}

function getChapterFromBibRef(bibRef) {
	var chapter = '';
	if ((typeof(bibRef) != 'undefined') && (bibRef != '')) {
		chapter = bibRef.substr(4)
		var i = chapter.indexOf('.')
		if (i >= 0) {
			chapter = chapter.substr(0, i);
		} else {
			i = chapter.indexOf('pt')
			if (i >= 0) {
				chapter = chapter.substr(0, i);
			}
		}
	}
	return chapter
}

function getCurrentPortal(defaultPortal) {
	// pageName defined in head of index.html
	if ((typeof(defaultPortal) == 'undefined') || (defaultPortal == '')) {
		defaultPortal = 'home'
	}
	var currentPortal = defaultPortal;
	if (window.name == 'index') {
		if ((typeof(pageName) != 'undefined') && (pageName != '')) {
			currentPortal = pageName
		}
	} else {
		if (window.name == 'topFrame') {
			if ((typeof(parent.pageName) != 'undefined') 
				&& (parent.pageName != '')) {
				currentPortal = parent.pageName
			}
		} else {
			if ((typeof(parent.parent.pageName) != 'undefined') 
				&& (parent.parent.pageName != '')) {
				currentPortal = parent.parent.pageName
			}
		}
	}
	return currentPortal
}

function getFirstChapterForBookCode(bookCode) {
    var chapter
    bookCode = bookCode.toLowerCase();
    if ((bookCode == 'egr') || ($bookCode == 'dgr')) {
        return 'A'
    }
    return '1';
}

function getEssay(id, alias) {
	if (alias == '') {
		alias = id
	}
	getAside(alias, id.substr(0,3) + '-1')
}

// Add highlighting when coming from the detail view
function getHighLightParams(searchDetailFrame, frameName, highLightTarget, addNoCache, addFirstMatchAnchor, options) {
	if (typeof(addNoCache) == 'undefined') {
		addNoCache = 1;
	}
	if (typeof(addFirstMatchAnchor) == 'undefined') {
		addFirstMatchAnchor = 1;
	}
	if (typeof(options) == 'undefined') {
		options = '';
	}
	
	var params = '';
	if ((options != 'noquery')
	&& (typeof(searchDetailFrame) != 'undefined')
	&& (typeof(frameName) != 'undefined')
	&& (frameName == 'rightFrame')
	&& (searchDetailFrame.getArg('view') == 'reslist')
	&& (searchDetailFrame.getArg('subview') == 'detail')) {
		// There is a detail view
		if (addNoCache) {
			params = ';cache=no';
		}
		params += ';q1=' + encodeURIComponent(searchDetailFrame.getArg('q1'))
				+ ';op2=' + encodeURIComponent(searchDetailFrame.getArg('op2'))
				+ ';q2=' + encodeURIComponent(searchDetailFrame.getArg('q2'))
				+ ';op3=' + encodeURIComponent(searchDetailFrame.getArg('op3'))
				+ ';q3=' + encodeURIComponent(searchDetailFrame.getArg('q3'))
				+ ';srgn=' + encodeURIComponent(searchDetailFrame.getArg('rgn'))
				+ ';start=' + encodeURIComponent(searchDetailFrame.getArg('start'))
				+ ';hitarget=' + encodeURIComponent(highLightTarget)
		if (searchDetailFrame.getArg('alt') != '') {
			params += ';alt=' + encodeURIComponent(searchDetailFrame.getArg('alt'))
		}
		// Anchor first match
		if (addFirstMatchAnchor) {
			params += '#hl1' 
		}
	}
	
	return params
}

function getHighLightQuery(searchDetailFrame, frameName, highLightTarget, addNoCache, addFirstMatchAnchor, options) {
	var highLight = getHighLightParams(searchDetailFrame, frameName, highLightTarget, addNoCache, addFirstMatchAnchor, options);
	if (highLight != '') {
		return '?' + highLight.substr(1) // Add ? and remove leading ;
	}
	return ''
}

function getRD(id) {
	getRDWQ('rodedraden', id)
}

function getRDWQ(portal, id) {
	var url = '/' + portal
	if ((typeof(id) != 'undefined') && (id != '')) {
		url += '/' + id
	}
	if (window.name == 'leftFrame') {
		parent.parent.location = url
	} else if (window.name == 'rightFrame') {
		if ((typeof(parent.parent.pageName) == 'undefined')
		|| (parent.parent.pageName != portal)) {
			parent.parent.location = url + getHighLightQuery(parent.rightFrame, window.name, 'left', 0)
		} else {
			parent.parent.setTitle(portal, id)
			url = textIdx + '?c=bnc;cc=bnc;frame=left;parentpage='
				+ encodeURIComponent(portal) + ';view=text;rgn=div1;rdraad='
				+ encodeURIComponent(id)
				+ getHighLightParams(parent.rightFrame, window.name, 'left', 0)
			parent.leftFrame.location = url
		}
	} else {
		window.location = url
	}
}

// Return propertyName of element
// Still need to fix IE uses propertyName FF property-name
// Currently only used for FF
function getStyleProperty(elementID, propertyName) {
	var element = document.getElementById(elementID);
	var propertyValue;
	if (element != null) {
		if (typeof(element.currentStyle) != 'undefined') {
			// IE
			propertyValue = element.currentStyle[propertyName]
		} else {
			// FireFox, Safari etc.
			if (document.defaultView && document.defaultView.getComputedStyle) {
        		var compStyle = document.defaultView.getComputedStyle(element, null);
        		propertyValue = compStyle.getPropertyValue(propertyName)
			}
		}
	}
	return propertyValue;
}

// Currently only used for FF
function getStylePropertyPix(elementID, propertyName, defaultValue) {
	return styleToPix(getStyleProperty(elementID, propertyName))
}

function getToc(bookCode) {
	var url = textIdx
		+ '?c=bnc;cc=bnc;frame=left;parentpage=bijbelboeken;view=toc;rgn=div1;idno=bk-'
		+ bookCode + ';node=bk-' + bookCode + '%3A1'
	
	if (window.name == 'leftFrame') {
		window.location = url
	} else {
		if ((window.name == 'index')
		&& (typeof(bottomFrame) != 'undefined') && (typeof(bottomFrame.leftFrame) != 'undefined')) {
			bottomFrame.leftFrame.location = url
		} else {
			if ((window.name == 'rightFrame') && (typeof(parent.leftFrame) != 'undefined')) {
				parent.leftFrame.location = url
			} else {
				window.location = url
			}
		}
	}
}

function getVerseFromBibRef(bibRef) {
	var verse = '';
	if (typeof(bibRef) != 'undefined') {
		var i = bibRef.indexOf('.')
		if (i >= 0) {
			verse = bibRef.substr(i+1);
		} else {
			i = bibRef.indexOf('pt')
			if (i >= 0) {
				verse = bibRef.substr(i)
			}
		}
	}
	return verse
}

// Return a webdoc
function getWebDoc(portal, leftDoc, rightDoc) {
	if (typeof(leftDoc) == 'undefined') leftDoc = ''
	if (typeof(rightDoc) == 'undefined') rightDoc = ''

	var currentPortal = getCurrentPortal();
	if ((typeof(portal) == 'undefined') || (portal == '')) {
		portal = currentPortal;
	}
	
	if (portal != currentPortal) {
		// Need to switch portal
		var url = '/' + encodeURIComponent(portal)
		if (leftDoc == '') {
			if (rightDoc != '') {
				url += '/home/' + rightDoc
				// Now /home/home/rightdoc
			}
		} else {
			url += '/' + leftDoc
			if (rightDoc != '') {
				url += '/' + rightDoc;
				// Now /home/leftdoc/rightdoc
			}
		}
		if (window.name == 'topFrame') {
			parent.location = url
		} else {
			if ((window.name == 'leftFrame') || (window.name == 'rightFrame')) {
				parent.parent.location = url
			} else {
				window.location = url
			}
		}
		// End portal != currentPortal
	} else {
		// Portal is current portal. Need to keep args in this order, to prevent unesc reloads
		var url = textIdx + '?c=bnc;cc=bnc'
		
		// Handle update left frame
		if ((leftDoc != '') && (leftDoc != 'current')) {
			// Update Left frame
			var leftUrl = url + ';frame=left;parentpage=' + encodeURIComponent(currentPortal)
						+ ';page=webdoc;webdoc=' + leftDoc
			if (window.name == 'index') {
				if ( (typeof(bottomFrame) != 'undefined')
				&& (typeof(bottomFrame.leftFrame) != 'undefined')) {
					var lFrame = bottomFrame.leftFrame
					if ((typeof(lFrame.currentWebDocAlias) == 'undefined')
					|| (lFrame.currentWebDocAlias != leftDoc)) {
						bottomFrame.leftFrame.location = leftUrl
					}
				}
			} else {
				if (window.name == 'topFrame') {
					// Top frame
					if ((typeof(parent.bottomFrame) != 'undefined')
					&& (typeof(parent.bottomFrame.leftFrame) != 'undefined')) {
						parent.bottomFrame.leftFrame.location = leftUrl
					}
				} else {
					if (window.name == 'rightFrame') {
						// Right frame
						if ((typeof(parent.parent.bottomFrame) != 'undefined')
						&& (typeof(parent.parent.bottomFrame.leftFrame) != 'undefined')) {
							parent.parent.bottomFrame.leftFrame.location = leftUrl
						}
					} else {
						// Left or other frame
						if ((typeof(currentWebDocAlias) == 'undefined')
						|| (currentWebDocAlias != leftDoc)
						// Next to reload wisselgalerij
						|| (rightDoc == '')) {
							window.location = leftUrl
						}
					}
				}
			}
		}

		// Handle update of right frame
		if ((rightDoc != '') && (rightDoc != 'current')) {
			var rightUrl = url + ';frame=right;parentpage=' + encodeURIComponent(currentPortal)
						+ ';page=webdoc;webdoc=' + rightDoc
			if (window.name == 'index') {
				if ((typeof(bottomFrame) != 'undefined')
				&& (typeof(bottomFrame.rightFrame) != 'undefined')) {
					bottomFrame.rightFrame.location = rightUrl
				}
			} else {
				if (window.name == 'topFrame') {
					if ((typeof(parent.bottomFrame) != 'undefined')
					&& (typeof(parent.bottomFrame.rightFrame) != 'undefined')) {
						parent.bottomFrame.rightFrame.location = rightUrl
					}
				} else {
					if (window.name == 'leftFrame') {
						if ((typeof(parent.parent.bottomFrame) != 'undefined')
						&& (typeof(parent.parent.bottomFrame.rightFrame) != 'undefined')) {
							parent.parent.bottomFrame.rightFrame.location = rightUrl
						}
					} else {
						// Right or other frame
						window.location = rightUrl
					}
				}
			}
		}
	}
}

function getWQ(id) {
	getRDWQ('webquests', id)
}


// Return the height of the browser window client area
function getWindowHeight() {
	if (window.innerHeight) {
		// "All" but IE before IE 9
		return window.innerHeight
	}
	// We are running in strict mode so use document.documentElement
	if (document.documentElement) {
		return document.documentElement.clientHeight
	}
	// Oldies
	if (document.body) {
		return document.body.clientHeight
	}
	// Don't resize 
	return 0
}

// Return the width of the browser window client area
function getWindowWidth() {
	if (window.innerWidth) {
		// "All" but IE before IE 9
		return window.innerWidth
	}
	// We are running in strict mode so use document.documentElement
	if (document.documentElement) {
		return document.documentElement.clientWidth
	}
	// Oldies
	if (document.body) {
		return document.body.clientWidth
	}
	// Don't resize 
	return 0
}

// Return whether this is the first chapter of a chapter chunk
function isFirstChapterOfChapterChunk(book, chapter) {
	if ((typeof(book) == 'undefined') || (book == '') || (typeof(chapter) == 'undefined')) {
		alertLinkNotFound()
		return 1
	}
	if ((typeof(chapter) == 'undefined') || (chapter == '') || (chapter == '1')) {
		return 1
	}
	
    book = book.toLowerCase()
    chapter = chapter.toLowerCase()
    if (book == 'egr') {
        if ((chapter == 'a') || (chapter == '3b') || (chapter == '6') || (chapter == '9')) {
            return 1;
        }
        return 0;
    }
    if (book == 'dgr') {
        return chapter == 'a' ? 1 : 0;
    }
    
    if (chapter <= 1) {
    	return 1
    }
    
    if (((chapter - 1) % 5) > 0) {
    	return 0
    } 
    
    return 1
}

// rollOn event handler
function rollOn(name, subdir) {
	if (document.images) {
		var img = document.images[name]
		if (typeof(img) != 'undefined') {
			if (typeof(subdir) != 'undefined') name = subdir + '/' + name;
			img.src = imageDir + name + "_over.gif";
		}
	}
	return true;
}

// rollOff event handler
function rollOff(name, subdir) {
	if (document.images) {
		var img = document.images[name]
		if (typeof(img) != 'undefined') {
			if (typeof(subdir) != 'undefined') name = subdir + '/' + name;
			img.src = imageDir + name + "_out.gif";
		}
	}
	return true;
}

function runningInFrameSet(expectedFrameSet) {
	return parent.name == expectedFrameSet
}

// Ugly way to get from a style to pix
function styleToPix(style, defaultValue) {
	style = parseInt(style)
	if ((typeof(defaultValue) != 'undefined') && isNaN(style)) {
		style = defaultValue
	}
	return style		
}

function trim(s) {
	return s.replace(/^\s+|\s+$/g, '')
}

