
/*********************************************************************

	REFRAME.JS
	
	Re-loads any frame content page into the appropriate frame
	of the appropriate frameset document.
	
	To both frameset and frame content documents, add the following <head> entry:
	<script src="[path to this file]" type="text/javascript"></script>
	
	Based on sample code by Th. H. Smith; generalized by RNE, 2002-10-02.
	If the browser opens a frame content document, the script
	generates a composed URL like
		http://www.domain.com/index.html?contentFrame=http://www.domain.com/pages/page.html
	sends it to the browser, the browser opens the frameset file
		http://www.domain.com/index.html
	and the script again says to change the content of 'contentFrame'
	to the file
		http://www.domain.com/pages/page.html
	Easy, isn't it?
	
	Feel free to use this code as base for your own JS experiments;
	but I can't give you any warranty... ;-)
	
*********************************************************************/


/*
	reFrame()
	
	Call from the frameset document as follows:
	<frameset ... onload="reFrame(defaultTargetFrame);">
	
	Arguments:
	defaultTargetFrame should be a valid frame object (not name!)
	of the current window.
*/

function reFrame(defaultTargetFrame) {
		
	// Init, & make sure our frameset is the top frameset
	var myURL = self.location.href;
	if (top.location.href != myURL) {
		top.location.href = myURL;
	}
	
	// Examinate the URL
	var p1 = myURL.indexOf("?");
	if (p1 > 0) { // if any '?' was found
		
		// Init var
		var newFrameLoc = "";
		var targetFrame = defaultTargetFrame; // just copy default, change later if possible
	
		// Get target frame
		var p2 = myURL.indexOf("=", p1); // start search at p1 ('?')
		if (p2 <= p1) { // no target information found
			newFrameLoc = myURL.substring(p1+1, myURL.length);
		} else {
			newFrameLoc = myURL.substring(p2+1, myURL.length);
			var targetFrameName = myURL.substring(p1+1, p2);
			for (i=0; i < self.frames.length; i++) {
				if (parent.frames[i].name == targetFrameName) {
					targetFrame = parent.frames[i];
					break;
				} // if no matching frame was found, targetFrame is still = defaultTargetFrame
			}
		}
		
		// Change target frame content, if possible
		if ((targetFrame != null) && (newFrameLoc.length > 0)) {
			// To avoid flicker, we compare URLs first & change only if necessary:
			if (newFrameLoc != targetFrame.location.href) {
				// Change as necessary
				targetFrame.location.href = newFrameLoc;
			}
		}
	} // if (i > 0) -- else: do nothing, URL is simplex (w/o any '?')
}


/*
	loadIntoFrame()
	
	Call from any frame content document as follows:
	<body onLoad="loadIntoFrame(targetFrameName, relPathToFramesetDoc);">
	
	Arguments:
	targetFrameName (as string) should be the name of the corresponding target frame
	as defined in the <frame ...> tag of the frameset document.
	relPathToFramesetDoc (as string) should be the relative path to the frameset document
*/

function loadIntoFrame(targetFrameName, relPathToFramesetDoc) {

	// Are we already framed correctly?
	var frameName = window.name;
	if (frameName == targetFrameName) { // It's fine!
		return;
	}
	
	// Test the URL: avoid loops!
	var myURL = top.location.href;
	var pos   = myURL.indexOf("?");
	if (pos > -1) { // Would result in an URL with multiple occs of '?' -- error!
		return;
	}
	
	// Test frames
	var myParent = window.parent;
	if (myParent != null) {
		if (myParent.frames.length > 0 ) { // If we are multiply framed, lets not get stuck in a loop!
			return;
		}
	}
	
	// Finally, do the change
	pos = myURL.lastIndexOf("/");
	if (pos > -1) {
		var parentDirURL  = myURL.substring(0, pos+1);
		top.location.href = parentDirURL + relPathToFramesetDoc + "?" + targetFrameName + "=" + myURL;
		// Something like 'http://www.x.de/pages/../index.html' is no problem!
		// The HTTP protocol resolves it into the correct absolute adress! 
	} // Else: error, do nothing!
}


/*********************************************************************
	EOF	
*********************************************************************/
