<!--
	// Bath.co.uk Frame Checker v0.2
	//
	// This script checks to see whether or not part of the Bath.co.uk
	// website has been loaded outside of the frameset.  If it has, it 
	// automatically reloads the Bath frameset, then reloads the content
	// the user originally intended to go to.
	//
	// This is a two-stage process.  First, the script detects if the site
	// is not part of the normal frameset - and if it isn't, it reloads the
	// site with the target url stub appended (see array below)
	//
	// When the site loads and detects the frameset is correct, it checks to
	// see if there is a stub, and if there is it replaces the main content
	// with the target.
	//
	var baseURL = "http://www.bath.co.uk";
	
	var refArray = {"_code/":"bcm",
						 "_code/sections/accommodation/":"acc",
	                "_code/sections/foodanddrink/":"fad",
						 "_code/sections/broadband/":"bro",
						 "_code/sections/jobs/":"job",
						 "_code/sections/email/":"eml",
						 "_code/sections/weddings/":"wed",
						 "_code/sections/contactus/":"con",
						 "_code/sections/bestofbath/":"bob",
						 "_code/sections/vtour/":"vto",
						 "_code/sections/whatson/":"won",
						 "_code/sections/basicinfo/":"bin",
						 "ContentWorx/content.asp?category=29":"cw1",
						 "_code/sections/virtualtour/":"vt2",
						 "_code/sections/cametobath/":"ctb",
						 "_code/sections/gettingaround/":"gta",
						 "_code/sections/maps/":"map",
						 "_code/sections/yoursay/":"ysy",
						 "_code/sections/foodanddrink/":"fnd",
						 "_code/sections/shopmobility/accessguide/":"spm",
						 "_code/sections/community/":"com",
						 "_code/sections/whattoseeanddo/":"wsd",
						 "_code/sections/shopsandservices/":"sas",
						 "_code/sections/baththrutheages/":"bta",
						 "_code/sections/cityTrail/":"ctt",
						 "_code/sections/sport/":"spo",
						 "_code/sections/bathspa/":"bsp",
						 "ContentWorx/content.asp?category=41":"cw2",
						 "ContentWorx/content.asp?category=38":"cw3",
						 "_code/sections/toursinbath/":"tib",
						 "_code/sections/gettingaround/":"gor",
						 "ContentWorx/content.asp?category=42":"cw4"
						 }
	
	function setStatus(msg) {
     if (msg)
       window.status = msg;
   }
	
	function fsetIsValid() { 
     var i;
     if (parent.frames.length == 3)
	     return (parent.frames[0].name == 'header' || parent.frames[1].name == 'sidebar' || parent.frames[2].name == 'main');
	  else  
	     return false;
	}
	
	function getContentFromURL(urlStr,cutPage) {
		// This function returns the portion of the supplied url
		// that does not include the http:// or https:// part, or 
		// the domain itself.
		if (!urlStr) return null;
		// Cut out the protocol (http, https, ftp, etc)
		if (urlStr.indexOf("://") > -1) urlStr = urlStr.substring(urlStr.indexOf("://")+3);
		// Cut out the domain
		if (urlStr.indexOf("/") > -1) urlStr = urlStr.substring(urlStr.indexOf("/")+1);
		// Cut the actual page off, if its specified (and is default[.asp|.htm|.html])
		if (cutPage)
		   urlStr = urlStr.substring(0,urlStr.lastIndexOf("/")+1)
		return urlStr;
	}
	
	function frameChecker() {
	   var i, found = false;
		var topf = top.location.href.toLowerCase();
		var ref = document.referrer.toLowerCase();
		//alert("topf = "+getContentFromURL(topf)+"\nref = "+getContentFromURL(ref));
	   if (!fsetIsValid()) {
			// Find out what page it is, and redirect them to the homepage+stub
			// If there is no match, just redirect to homepage to be on the safe side
			for (i in refArray) {
				// Assuming this is a single page, we work out which page of page we are.
				
				// As a "niceity", if the referrer is Bath itself (e.g. if the user opens a new
				// page in new window) we allow it.
				if (ref.indexOf(baseURL) > -1) return true;
				
				// Try and match document location with either directory (dloc)
				// or directory+script (dloc_wpg) (required for ContentWorx variations)
			   dloc = getContentFromURL(document.location.href,true);
				dloc_wpg = getContentFromURL(document.location.href,false);
				//alert("i="+i+", dloc="+dloc+", dloc2="+dloc2);
				if (i == dloc || i == dloc_wpg) {
					found = true;
					// If page is not "default.asp", we direct to the specific page
					if (i == dloc) {
					   // User may need to be redirected to a subpage which was matched
					   // to a directory (e.g. a page under _code/sections/jobs - e.g. bathjobs2.asp)
					   // instead of the default page.
					   upg = dloc_wpg.substring(dloc_wpg.lastIndexOf("/")+1);
					   if (upg.length > 0) {
					      top.location.replace(baseURL+"?redir="+refArray[i]+"&page="+upg);
							break;
					   }
					}
				   top.location.replace(baseURL+"?redir="+refArray[i]);
				}
			}
			// No match
			if (!found) top.location.replace(baseURL);
		} else {
		   // 2nd pass check (or normal running)
			
			// Check for stub, only if we're not already in the frameset.
			// The top frame of Bath records whether or not we're in the Bath frameset
			if (top.haveRedirected) return true;
			
			topf = getContentFromURL(topf,false);
			if (topf.substring(0,7) == "?redir=") {
			   for (i in refArray) {
				   if (refArray[i] == topf.substring(7,10)) {
						//alert("redirecting to "+i);
						// Failsafe - make sure we're not already on this page!
						// (otherwise we'll reload constantly)
						if (parent.frames[2].location.href != (baseURL+"/"+i)) {
						   // Check to see if we are getting a specific page
							// If we are, redirect them to that instead of the default page
							if (topf.indexOf("&page=") > -1)
								parent.frames[2].location.replace(baseURL+"/"+i+topf.substring(topf.indexOf("&page=")+6));
							else
						      parent.frames[2].location.replace(baseURL+"/"+i);
							top.haveRedirected = true;
						}
					}
			   }
			}
		}
	}
//-->
