// BCBonload() is called whenever a Main page is loaded
// If the site is being used correctly within frames it sets the navigation option
// If a single page has been loaded, it reloads the entire site

function BCBOnload(BCBOname) {
	if (checkForFrames()) {
		setNavigation(BCBOname);
		removeStuff();
	} else {
		window.location = "index.html";
	}
}

// function to remove any gumf that freeserve has added.

function removeStuff() {
	removeFrames();
	//removeBanners();
	//removePopups();
}

function removeFrames() {
	// If freeserve has added a new frame with a banner, get rid of it pronto!
	// Because of the way freeserve does this, check the frame names in 
	// top.document not parent.document
	
	var fsFrame = false;
	var f = 0;
	var frs = top.document.getElementsByTagName("frame");
	var fn = "";
	while (f < frs.length) {
		fn = frs[f].id;
		// alert(fn);
		if ((fn != "frameMain")
		&&  (fn != "frameNav")
		&&  (fn != "frameLogo")
		&&  (fn != "frameHead")) {
			// alert(fn + " - hide because of this one");
			fsFrame = true;
		}
		f += 1;
	}	
	// alert(top.document.body.rows);
	if (fsFrame) {
		// assume the new frame is added above the site proper
		// alert(top.document.body.rows);
		top.document.body.rows = "0px,*";
		// alert(top.document.body.rows);
	}	
}

function removeBanners() {
	// Hide any nasty stuff that freeserve has added within a document

	var awsBuster = document.getElementsByTagName("div");
	var i = 0;
	while (i < awsBuster.length) {
		if ((awsBuster[i].id.length > 2) 
		&&  (awsBuster[i].id.substr(0,3) == "aws")) {
			// alert("found one: " + awsBuster[i].id);
			document.getElementById(awsBuster[i].id).className = "divBeGone";
			
			// this will not have fully worked - the text in the division
			// has been set invisible, but images still display.
			// I'm leaving it for now, as the site uses frames so
			// removeFrames() does all that is necessary
		}
		i += 1;
	}
}

function removePopups() {
	// not yet worked out how to do this
	// take care not to close legitimate children such as restricted
	// areas or external sites.
}

// changePage() refreshes the main and title frames

function changePage(CPname) {
	parent.frameMain.location = (CPname + "Main.html");
	parent.frameHead.location = (CPname + "Head.html");
}

// setNavigation() highlights the current page name in the navigation frame

function setNavigation(SNoption) {

	var SNelts = parent.frameNav.navForm.getElementsByTagName("input");
	var i = 0;

	while (i < SNelts.length) {
		if (SNelts[i].name == SNoption) {
			SNelts[i].setAttribute("className","navOptCurr");
		} else {
			SNelts[i].setAttribute("className","navOpt");
		}	
		i += 1;
	}
}

// checkForFrames() returns true if the navigation panel exists
// This function is useful if the site has been reached from a search engine.
// The search engine may have catalogued an individual frame, but we want the
// whole site to display in a frameset.

function checkForFrames() {
	var CFFbool = false;
	if (parent.frames.length > 0) {
		CFFbool = true;
	}
	return CFFbool;
}

// This addrecode() reverses a hidden email address.  Encoding email addresses this
// way prevents them being harvested by spambots.
// The addresses themselves are declared in this .js file to avoid hardcoding

var AUTHOR_ADDRESS = "ku.oc.oohay*egazzat";
var BAND_ADDRESS = "ku.oc.oohay*dnabtrecnocsenrab";
var CONDUCTOR_ADDRESS = "moc.xepip.lsd*htysrof.nareik";

function mailtoDecode(MDinput) {
	return ("mailto:" + addrdecode(MDinput));
}

function addrdecode(InString) {
	var i, s, OutString;
	// InString += ':otliam';
	OutString = new String();
	for (i=InString.length-1; i>= 0; i--) {
		s = InString.charAt(i);
		if ( s == '*' ) { 
			s= '@';
		}
		OutString += s;
	}
	return OutString;
}


function membersOnly()
{
	var password = prompt("Enter password","");
	var newPage = password + ".html";
	//alert(newPage);
	var newWindow = window.open(newPage);
	
	var checkNot404 = newWindow.document.getElementsByTagName("input");
	var bool = false;
	if (checkNot404.length > 0) {
		var i = 0;
		while (i < checkNot404.length) {
			if (checkNot404[i].id == "passwordValid") {
				// the password is correct, and a page has opened
				// containing an element with this id.
				bool = true;
			}
			i += 1;
		}
	}

	if (!bool) {
	//	newWindow.close();
	//	alert("access denied");
	}
}
