/*************************************************************************************************
 * syntax
 * getParmsFromSearch(<searchString>)
 *
 * parameters
 * <searchString>
 * is made up of name/value pairs with following syntax
 * ?<name>=<value>[&<name>=<value>]...
 *
 * description
 * returns an array of all parameters in which name and corresponding value reside in consecutive indices   
 *
 * global objects/variables used
 * none
 *
 * version
 * 1.0
 *
 * written by
 * Esther Berg
 *
 * contact
 * esther.berg@fernuni-hagen.de
 *
 * date
 * 11/4/02
 *
 **Copyright © 2002 FernUniversit&auml;t – Gesamthochschule in Hagen	Alle Rechte vorbehalten.**********/
 
 function getParmsFromSearch(searchString) {
	var pairs = new Array();
	var singles = new Array();
	var parms = new Array();
	var i = 0;
	
	if(searchString != "" && searchString.substring(0,1) == '?') {
		searchString = searchString.substring(1,searchString.length);
		pairs = searchString.split('&');
		for(i=0; i < pairs.length; i++) {
			singles = pairs[i].split('=');
			if(singles.length == 2) {
				parms = parms.concat(singles);
			} else {
				alert('"' + pairs[i] + '" is not a valid name/value pair.');
			}
		}
	}
	return parms;
}

/*************************************************************************************************
 * syntax
 * frameStuffer(<frameURIPairs>)
 *
 * parameters
 * <frameURIPairs>
 * an array in which framenames and URIs reside in consecutive indices
 *
 * description
 * stuffes URIs into their destination frames
 *
 * global objects/variables used
 * self
 *
 * version
 * 1.1
 *
 * written by
 * Esther Berg
 *
 * contact
 * esther.berg@fernuni-hagen.de
 *
 * date
 * 11/6/02
 *
 **Copyright © 2002 FernUniversit&auml;t – Gesamthochschule in Hagen	Alle Rechte vorbehalten.**********/
  
 function frameStuffer(frameURIPairs) {
	var unknownFrames = new Array();
	var i,j = 0;

	for(i=0; i < self.frames.length; i++) {
		j=0;
		while(j < frameURIPairs.length && frameURIPairs[j] != self.frames[i].name) {
			j +=2;
		}
		if(j < frameURIPairs.length) {
			self.frames[i].location.replace(frameURIPairs[j+1]);
			frameURIPairs.splice(j,2);
		}
	}

	if(frameURIPairs.length > 0) {
		j=0;
		for(i=0; i < frameURIPairs.length; i+=2) {
			unknownFrames[j]=frameURIPairs[i];
			j++;
		}
		alert('Frame(s) "' + unknownFrames.join(', ') + '" is/are unknown in frameset.');
	}
}

/*************************************************************************************************
 * syntax
 * frameBuster(<pathMatch> | <URI> [,<URI>]...,'open' | 'replace')
 *
 * parameters
 * <pathMatch>
 * a positive integer which states in how many path parts a referrer must match its referenced URI to be
 * considered trustable. If an <URI> is given a <pathMatch> of 2 is added. Thus all referrers within the 
 * same domain are made trustable by default.
 * <URI>
 * a relative or absolute URI of a trustable referrer
 * 'open' | 'replace'
 * 'open' opens a new browser window, 'replace' replaces the content of the current browser window
 *
 * description
 * In case of a trustable referrer the referenced URI is shown in the referrer's frame. Otherwise the
 * referenced URI is shown in the current window or a new one.
 *
 * global objects/variables used
 * self
 *
 * version
 * 1.2
 *
 * written by
 * Esther Berg
 *
 * contact
 * esther.berg@fernuni-hagen.de
 *
 * date
 * 11/19/03
 *
 **Copyright © 2002, 2003 FernUniversit&auml;t – Gesamthochschule in Hagen	Alle Rechte vorbehalten.**********/
  
function frameBuster(firstTrustableSpec) {
	var selfPathParts = new Array();
	var trustableSpecs = new Array();
	var referrerPathParts = new Array();
	var trustablePathParts = new Array();
	var newWindow;
	var newBrowser;
	var selfLocationHref = '';
	var pathPartsDelimiter = '/';
	var matchingPathParts = 0;
	var referrerPathPartsLength = 0;
	var trustablePathPartsLength = 0;
	var trustableSpecsLength = 0;
	var i, j = 0;
	var trustable = false;
		
	for(i=0; i < frameBuster.arguments.length-1; i++) {
		trustableSpecs[i] = frameBuster.arguments[i];
	}
	switch(frameBuster.arguments[i]) {
		case 'open':
			newWindow='_blank';
			break;
		case 'replace':
			newWindow='_top';
			break;
		default:
			newWindow='_blank';
			alert('\'' + frameBuster.arguments[i] + '\' is not a valid value.\n' +
				  'Please enter \'open\' to open a new browser or \'replace\' to replace the current browser window.\n' +
				  'A value of \'open\' is assumed.');
		
	}
	selfPathParts = self.location.href.split(pathPartsDelimiter);
	matchingPathParts = parseInt(firstTrustableSpec);
	if(isNaN(matchingPathParts)) {
		matchingPathParts = 2;
	} else {
		matchingPathParts = Math.min(matchingPathParts,selfPathParts.length - 2);
		i=0;
	}
	selfPathParts.splice(matchingPathParts+1,selfPathParts.length - matchingPathParts + 1);
	trustableSpecs[i] = selfPathParts.join(pathPartsDelimiter);

	trustable = (self.document.referrer == '');
	
	referrerPathParts = self.document.referrer.split(pathPartsDelimiter);
	trustableSpecsLength = trustableSpecs.length;
	referrerPathPartsLength = referrerPathParts.length;
	i=0;
	while(!trustable && i < trustableSpecsLength) {
		trustablePathParts = trustableSpecs[i].split(pathPartsDelimiter);
		while(trustablePathParts[trustablePathParts.length - 1] == '') {
			trustablePathParts.pop();
		}
		j=0;
		trustablePathPartsLength = trustablePathParts.length;
		while(j < trustablePathPartsLength && j < referrerPathPartsLength && trustablePathParts[j] == referrerPathParts[j]) {
			j++;
		}
		trustable = (j == trustablePathPartsLength || j == referrerPathPartsLength);

		i++;
	}
	
	if(!trustable) {
		selfLocationHref = self.location.href;
		self.location.href = self.document.referrer;
		newBrowser = window.open(selfLocationHref,newWindow);
		newBrowser.location.replace(selfLocationHref);
		newBrowser.focus();
	}
}

/*************************************************************************************************
 * syntax
 * framesetMaker(<framesetURI>[,<frame>,<URI>]...)
 *
 * parameters
 * <framesetURI>
 * a page defining a frameset
 * <frame>
 * a frame's name defined within <framesetURI>
 * <URI>
 * a relative or absolute URI to be loaded into <frame>
 *
 * description
 * codes [<frame>,<URI>],... into a search string conforming to the syntax given in getParmsFromSearch 
 * loads <framesetURI> with this search string which has to be processed by calling frameStuffer as a
 * frameset's onload event handler.   
 *
 * global objects/variables used
 * parent, self
 *
 * version
 * 1.0
 *
 * written by
 * Esther Berg
 *
 * contact
 * esther.berg@fernuni-hagen.de
 *
 * date
 * 11/11/02
 *
 **Copyright © 2002 FernUniversit&auml;t – Gesamthochschule in Hagen	Alle Rechte vorbehalten.**********/
  
function framesetMaker(framesetURI) {
	var parmsDelimiter = '?';
	var pairDelimiter = '&';
	var singlesDelimiter = '=';
	var newLoc = '';
	var i = 0;
	var navPrinting = false;
	
	if(parent.frames.length == 0) {
 		if ((navigator.appName + navigator.appVersion.substring(0, 1)) == "Netscape4") {
  			navPrinting = (self.innerHeight == 0) && (self.innerWidth == 0);
 		}
		if ((self.location.protocol != "file:") && !navPrinting) {
			newLoc = framesetURI + parmsDelimiter;
			for(i=1; i < framesetMaker.arguments.length; i+=2) {
				newLoc += framesetMaker.arguments[i] + singlesDelimiter + framesetMaker.arguments[i+1] + pairDelimiter;
			}
			newLoc = newLoc.substring(0,newLoc.length-1);
			if(newLoc.indexOf(parmsDelimiter,newLoc.length-1) > -1) {
				newLoc.substring(0,newLoc.length-1);
			}
			newLoc = encodeURI(newLoc);
			if (parseInt(navigator.appVersion) >= 3) {
				self.location.replace(newLoc);
			} else {
				self.location.href = newLoc;
			}
		}
	}
}

