// JavaScript used by all deepsea pages

// THESE SCRIPTS ARE THE COPYRIGHT OF JOHN LIDDIARD (C) 2003 AND MAY NOT BE
// COPIED OR USED WITHOUT HIS EXPLICIT CONSENT

// Style info - global with defaults
var Look = {
	Sheet:	"dsstyle.css",
	Line:	"hline1.gif",
	Button:	"btn1.gif"
	}

// Prefix for web path resolution - global
var prefix = "";
var cgibin = prefix+"cgi-bin/";

// write to either this doc window or a named window
function write_on_wdow(text,wdow){
	if (wdow) {
		wdow.document.write (text);
		}
	else {
		document.write (text);
		}
	}


// ==================== Start of all docs ===============================

// Work out what the appropriate style sheet is called
function resolve_style () {
	var styleinfo = getCookie('style_cookie')||"";
	if (styleinfo){
		var styleitems = new Array();
		var styleitems = styleinfo.split ('||');
		var i;
		var itemname;
		var itemval;
		for (i in styleitems){
			itemname = styleitems[i].split('::')[0];
			itemval = styleitems[i].split('::')[1];
			Look[itemname]=itemval;
			}
		}
	}

function ds_set_path(path) {
	if (path) {
		prefix = path;
		cgibin = prefix+"cgi-bin/";
		}
	}

// Reference the appropriate style sheet
function dsinclude_style(path,wdow) {
	if (path) {
		prefix = path;
		cgibin = prefix+"cgi-bin/";
		}
	resolve_style();
	var text='<link rel=stylesheet type="text/css" href="'+prefix+Look['Sheet']+'">';
	write_on_wdow (text,wdow);
	}

// Display a line/bar graphic about the page heading
function dsheader_line(wdow) {
	if (Look['Line']){
		var text= '<p class=dsc><img class=btn src="'+prefix+'graphics/'+Look['Line']+'" align=center></p>';
		write_on_wdow (text,wdow);
		}
	}

// Display a button for a list
function dslist_btn(wdow) {
	var text= '<img class=btn src="'+prefix+'graphics/'+Look['Button']+'" align=left>';
	write_on_wdow (text,wdow);
	}


// ================== End of all docs ====================================

// Display all the end of page stuff
function dspage_end(ctr_name,wdow,ticker) {
	dspage_end_ticker(wdow, ticker);
	dspage_end_menu(wdow);
	dspage_end_credit(wdow);
	dspage_end_ctr(ctr_name,wdow);
	dspage_end_diag(wdow);
	}

// Display abbreviated end of page stuff - no menu or counter
function ds_short_page_end(ctr_name,wdow,ticker) {
	dspage_end_ticker(wdow, ticker);
	dspage_end_credit(wdow);
	dspage_end_diag(wdow);
	}

// called via: dspage_end('','','boards/tiger_lily_faq+boards/tiger_lily_notes')
function dspage_end_ticker(wdow,tipclass) {

	// on members pages, defaults to webmaster_faq
	var docloc = document.location.href;
	if (docloc.indexOf('cgi-bin')>=0){
		docloc = window.location.href;
		}
	if (docloc.indexOf('cgi-bin')>=0){
		docloc = parent.location.href;
		}

	if ((docloc.indexOf('member')>=0)||
		(docloc.indexOf('webmaster')>=0)||
		(docloc.indexOf('admin/help')>=0)
		){
		tipc='tipf=boards/webmaster_faq'; // force to webmaster
		}
	else if (! tipclass){
		// else defaults to manager_faq
		tipc='tipc=boards/manager_faq'; // default to manager
		}
	else if (tipclass){
		// multiples - select one at random
		if (tipclass.indexOf('+')) {
			// Multiple choice
			var b_arr = new Array();
			b_arr = tipclass.split('+');
			var ix = Math.floor(Math.random()*b_arr.length);
			ix=ix>b_arr.length?b_arr.length:ix; // make sure we don't overflow
			tipc='tipf='+b_arr[ix]; //force to one at random
			}
		else {
			tipc='tipf='+tipclass; //force to one at random
			}
		}
	var tip_link = cgilink ('jlusrnb_qsection.cgi', tipc);
	//debug (tip_link);
	write_on_wdow ('<br><br><br>',wdow);
	write_on_wdow ('<p class=dsc><table class=cldr width=80% border=1 cellpadding=1 align=center><tr><td>',wdow);
	write_on_wdow ('<IFRAME src="', wdow);
	write_on_wdow (tip_link, wdow);
	write_on_wdow ('" width=100% height=185 border=0 frameborder=0 name="qnote" id="qnote" marginheight=0 marginwidth=0 align=middle standby="loading...">',wdow);
	write_on_wdow ('</IFRAME></td></tr></table></p>',wdow);
	write_on_wdow ('<br>',wdow);

	// initialise the tool tip stuff
	dstip_prepare(wdow);


	return;
	}


// Display the end of page menu -----
function dspage_end_menu(wdow) {
	document.write ('<br><br><br><p class="dsmc">');

	// only link to webmaster board for members
	if (getCookie('user_cookie') && getCookie('user_cookie').split('||')[1]) {

		var uname = getCookie('user_cookie').split('||')[1]||'';
		var pw = getCookie('user_cookie').split('||')[2]||'';
		var upwparams = '';
		if (uname && pw) {
			// will enable auto-login
			//upwparams ='?u='+escape(uname)+'&p='+escape(pw);
			}

		// Member login
		var lilink = '<a href="'+prefix+'admin/member_li.htm'+upwparams+'" target="_top">';
		write_on_wdow (lilink+'Member Login</a><br>',wdow);
		}
	write_on_wdow ('<a href=http://www.deepsea.co.uk target=_top>www.deepsea.co.uk</a><br>',wdow);
	write_on_wdow ('<a href='+prefix+'index.htm target=_top>Home</a> - ',wdow);
	write_on_wdow ('<a href='+prefix+'admin/howto.htm target=_top>User Guide</a><br>',wdow);
	write_on_wdow ('<a href='+prefix+'admin/links.htm target=_top>Links</a> - ',wdow);
	write_on_wdow ('<a href='+prefix+'admin/howto.htm target=_top>Booking advice</a><br>',wdow);
	write_on_wdow ('<a href='+prefix+'forum/index.htm target=_top>Message board</a><br>',wdow);
	write_on_wdow ('<a href='+prefix+'boats/angling.htm target=_top>Angling</a> - ',wdow);
	write_on_wdow ('<a href='+prefix+'boats/diving.htm target=_top>Diving</a><br>',wdow);
	write_on_wdow ('<a href='+prefix+'boats/leisure.htm target=_top>Leisure charters</a> - ',wdow);
	write_on_wdow ('<a href='+prefix+'boats/commercial.htm target=_top>Commercial boats</a><br>',wdow);
	write_on_wdow ('<a href='+prefix+'ports/ports.htm target=_top>Ports</a> - ',wdow);
	write_on_wdow ('<a href='+prefix+'boats/skippers.htm target=_top>Skippers</a><br>',wdow);
	write_on_wdow ('<a href='+prefix+'shops/tackleshops.htm target=_top>Angling Shops</a> - ',wdow);
	write_on_wdow ('<a href='+prefix+'shops/diveshops.htm target=_top>Dive Shops</a><br>',wdow);
	write_on_wdow ('<a href='+prefix+'pubfoodaccom/pubfoodaccom.htm target=_top>Food, pubs & accommodation</a><br>',wdow);
	write_on_wdow ('<a href='+prefix+'admin/join.htm target=_top>Join the Deep Sea directory</a><br>',wdow);
	write_on_wdow ('<a href='+prefix+'boatyard/index.htm target=_top>Boatyard</a><br>',wdow);
	write_on_wdow ('<a href='+prefix+'affiliate/index.htm target=_top>Affiliate Scheme</a><br>',wdow);
	write_on_wdow ('<a href='+prefix+'admin/report.htm target=_top>Report a problem with this site</a><br>',wdow);
	write_on_wdow ('</p><br>',wdow);
	}

// Display a credit to me! -----
function dspage_end_credit(wdow) {
	write_on_wdow ('<br><br><br>',wdow);
	write_on_wdow ('<p class="dsfn">Web engineering by<br> <a href=http://www.liddiard.demon.co.uk/index.htm target=_top>',wdow);
	write_on_wdow ('<img align=middle src='+prefix+'graphics/uwlinkbtn.jpg border=0 alt="John Liddiard Underwater" width=115 height=74></a></p>',wdow);
	write_on_wdow ('<br><br><br>',wdow);
	}

// Put a counter at the end of the page -----
function dspage_end_ctr(ctr_name,wdow) {
	var docloc = ""+document.location; // force to string type
	var docurl = docloc.split("?")[0];
	var ctrurl;
	var cn;


	// specified counter
	if (ctr_name) {
		cn = ctr_name;
		}
	else if (docurl.indexOf('southboats/')>=0) {
		cn = 'southboats';
		}
	else if (docurl.indexOf('boats/')>=0) {
		cn = docurl.split('boats/')[1];
		cn = cn.split('/')[0];
		}
	else if (docurl.indexOf('boatyard/')>=0) {
		cn = docurl.split('boatyard/')[1];
		cn = cn.split('/')[0];
		}
	else if (docurl.indexOf('shops/')>=0) {
		cn = docurl.split('shops/')[1];
		cn = cn.split('/')[0];
		}
	else if (docurl.indexOf('accom/')>=0) {
		cn = docurl.split('accom/')[1];
		cn = cn.split('/')[0];
		}
	else if (docurl.indexOf('advertising/')>=0) {
		cn = docurl.split('advertising/')[1];
		cn = cn.split('/')[0];
		}
	else {
		// no counter specified or assumed, so don't render it.
		return;
		}

	if (cn==null || cn=='' || cn==' ' || cn.indexOf('.htm')>=0){
		return;
		}

	cn = correct_name(cn);

	var ctr_link_params = 'ctr='+cn;

	if (docurl.indexOf('advertising/')>0) {
		ctrurl = cgilink('ad_counter.cgi', ctr_link_params);
		}
	else {
		ctrurl = cgilink('jlcounter.cgi', ctr_link_params);
		}

	var ctr_link = '<img src='+ctrurl+' width=1 height=1 border=0>';
	write_on_wdow (ctr_link, wdow);
	//debug (ctr_link);
	}


//NEEDS WORK FOR PREVIEW STUFF
// Work out host/target doc base from the referencing docs url
function docs_base (){
		return "../";
	}

// ================================== Manage USR Tab =======================
// (most in dsmenu.js)
function reset_usr_tab (){
	setCookie ('buttons_cookie', '');
	top.location.href=top.location.href;
	return false;
	}


// ====================== Popup Preview Windows =========================
// for previews of pics, schedule rows etc...
function show_preview_window_of_ds_pic (pic){
	if ((pic=='none')||(!pic)){
		return false;
		}
	var body_text = '<p class=dsc><IMG src='+ docs_base() + pic +'><br clear=all>' + pic +'</p>';
	show_preview_window ("Preview of file on deepsea", "File on deepsea", body_text);
	}

function show_preview_window_of_local_pic (pic){
	if ((pic=='none')||(!pic)){
		return false;
		}
	var body_text = '<p class=dsc><IMG src=file:///'+ pic +'><br clear=all>' + pic +'</p>';
	show_preview_window ("File on your PC - ready for upload", "File on your PC", body_text);
	}


function show_preview_window (title, head, body_text){
	var PreviewWindow = window.open('','preview','scrollbars=yes,resizable=yes,width=900,height=700');
	write_on_wdow('<HTML><HEAD><TITLE>'+title+'</TITLE>', PreviewWindow);
	dsinclude_style (docs_base(), PreviewWindow);
	dsheader_line(PreviewWindow) ;
	write_on_wdow('<h1>'+head+'</h1>',PreviewWindow);
	dsheader_line(PreviewWindow);
	write_on_wdow(body_text,PreviewWindow);
	dspage_end_credit(PreviewWindow);
	write_on_wdow('</BODY></html>',PreviewWindow);
	PreviewWindow.document.close();
	PreviewWindow.focus();
	}


// =================== Preview tips - on status bar and in popup =======================
// (Nothing to do with page level tips)

var currentdescription;
var tid;
var i=0;

// Clear the tip
function cPrev() {
	window.clearTimeout(tid);
	currentdescription=' ';
	// link to code for popup tool tips
	clear_tool_tip();
	window.status=' ';
	i = 0;
	return true;
	}

// Show the tip
function dPrev(description,evt,frameflag) {
	evt=(evt)?evt:event;
	window.clearTimeout(tid);
	currentdescription=' '+description;
	i = 0;

	// link to code for popup tool tips
	// frame flag used my menu to tell tip to pop in main window
	if (frameflag) {
		show_frame_tool_tip(currentdescription,evt);
		}
	else {
		show_tool_tip(currentdescription,evt);
		}

	if (currentdescription.length>100) {
		currentdescription='          '+description+'          ';
		window.status=currentdescription;
		scrollStatus();
		return true;
		}
	else {
		window.status=currentdescription;
		return true;
		}

	return true;
	}

// Scroll long tips
function scrollStatus() {
	window.status = currentdescription.substring(i,currentdescription.length) + currentdescription.substring(0,i-1);
	if (i < currentdescription.length) {
		i++;
		}
	else {
		i = 0;
		}
	tid = setTimeout('scrollStatus()',150);
	return true;
	}

// filter click action to only link for real events.
function cFilter(description) {
	window.clearTimeout(tid);
	window.status=' ';
	currentdescription=' '+description;
	i = 0;
	return true;
	}

//===========================tool tips in a pseudo window========================

// Init tip system for later content
var tipon=0;
var tipid;
function dstip_prepare(wdow) {
	// may need to quit early for old browsers...
	if (tipon) return;
	if (!document.getElementById)return;
	tipon ++;

	var tip_style1="position:absolute;top:0px;left:0px;background-color:#ffffe8;";
	var tip_style2="color:black;font-size:10pt;text-align:left;font-family:Arial;border-width:1;border-color:black;border-style:solid;padding:3px 3px;";

	write_on_wdow ('<div id="dstipdiv" style="'+tip_style1+tip_style2+'" visible:false>',wdow);
	write_on_wdow ('tip', wdow);
	write_on_wdow ('</div>',wdow);

	tipid = document.getElementById("dstipdiv");

	tipid.style.visibility="hidden";
	return;
	}

function getPageEventCoords(evt) {
    var coords = {left:0, top:0};
    if (evt.pageX) {
        coords.left = evt.pageX;
        coords.top = evt.pageY;
    	}
    else if (evt.clientX) {
        coords.left =
            evt.clientX + document.body.scrollLeft - document.body.clientLeft;
        coords.top =
            evt.clientY + document.body.scrollTop - document.body.clientTop;
        // include html element space, if applicable
        if (document.body.parentElement && document.body.parentElement.clientLeft) {
            var bodParent = document.body.parentElement;
            coords.left += bodParent.scrollLeft - bodParent.clientLeft;
            coords.top += bodParent.scrollTop - bodParent.clientTop;
        	}
    	}
    return coords;
	}

function show_tool_tip(tip,evt){
	if (!tipid) return;
	if (!tip) return;
	if (!document.createTextNode) return;

	// kill any leading date stuff, as most likely hovering on a date anyway
	var ntip=tip.replace(/^\s*[0-9]{1,2}-[a-z]{3}\s*/i,'');
	var ntip=ntip.replace(/^\s*is\s*/,'');

	var coords = getPageEventCoords(evt);
	var tipmsg = document.createTextNode(ntip);
	var oldmsg = tipid.replaceChild(tipmsg, tipid.firstChild);

	var units = (typeof tipid.style.left == "string")?"px":0;
	tipid.style.left=(coords.left>width_trip(tip))?(width_trip(tip)+units):(coords.left+20+units);
	tipid.style.top=coords.top+15+units;

	tipid.style.visibility="visible";
	}

// Tip code for displaying tip in other frame
var frtipid;
function show_frame_tool_tip(tip,evt){
	if (!tip) return;
	// confirm support for functionality
	if (!parent) return;
	if (!parent.frames[1]) return;
	if (!parent.frames[1].document) return;
	if (!parent.frames[1].document.getElementById) return;
	if (!document.createTextNode) return;
	// Get the id of the tip popup
	frtipid=parent.frames[1].document.getElementById("dstipdiv");
	if (!frtipid) return;

	// kill any leading date stuff, as most likely hovering on a date anyway
	var ntip=tip.replace(/^\s*[0-9]{1,2}-[a-z]{3}\s*/i,'');
	var ntip=ntip.replace(/^\s*is\s*/,'');

	var coords = getPageEventCoords(evt);

	var units = (typeof frtipid.style.left == "string")?"px":0;
	var left_offset=3;
	var top_offset=5;

	// allow for scrolling of other window
	if (parent.frames[1].document.body.scrollTop){
		top_offset+= parent.frames[1].document.body.scrollTop;
		}

	var tipmsg = parent.frames[1].document.createTextNode(ntip);
	var oldmsg = frtipid.replaceChild(tipmsg, frtipid.firstChild);

	frtipid.style.left = left_offset+units;
	frtipid.style.top=coords.top+top_offset+units;
	frtipid.style.visibility="visible";
	}

// Erase any tips displayed in this or other frame - no need to be clever and track it,
// just clear everything
function clear_tool_tip(){
	if (tipid) {
		if (tipid.style.visibility){
			tipid.style.visibility="hidden";
			}
		}
	if (frtipid){
		if (frtipid.style.visibility){
			frtipid.style.visibility="hidden";
			}
		}
	return;
	}

// work out what number to trip on when approaching right margin
function width_trip(tip) {
	var room = (tipid.style.width)?(tipid.style.width):'300px';
	room = parseInt(room.replace(/[^0-9]+/g,''))+30;
	// trip for short text...
	room = (tip.length<30)?150:room;

	if (window.innerWidth) {
		return (window.innerWidth-room);
		}
	if (document.body && document.body.clientWidth){
		return (document.body.clientWidth-room);
		}
	var dwidth=900;
	return (dwidth - room);
	}


// ================================== Cookies ==============================

// retrieve cookie by name
function getCookie(name) {
    var lookfor = name + "=";
    var cookiestext = document.cookie;
    var i = 0;
    while (i < cookiestext.length) {
        var j = i + lookfor.length;
        if (cookiestext.substring(i, j) == lookfor) {
    		var k = cookiestext.indexOf (";", j);
    		if (k < 0) {
        		k = cookiestext.length;
    		}
    	cookietext = cookiestext.substring(j, k);
   	   	return unescape(conditional_unenc_it(cookietext));
        }
        i = cookiestext.indexOf(" ", i) + 1;
        if (i == 0) return null;
    }
    return null;
}

// store cookie value for 1 yr, site wide.
function setCookie(name, value) {
    var expDate = new Date();
    expDate.setDate(expDate.getDate() + parseInt(365));
    var expires = expDate.toGMTString();
    document.cookie = name + "=" + escape (value) +
    				"; expires=" + expires +
     			  	"; path=" + "/" ;
    return true;
	}


function setStyleCookie(value){
	// debug ('Set style_cookie=' + value);
	setCookie('style_cookie', value);
	return true;
	}

// turn decimal and dot back into text
// - nothing that secure, just designed to stop idle curiosity
function unenc_it (hinp){
	var inp="";
	var hch1;
	var hch2;
	var hchix;
	var hinp_chars =new Array();
	hinp_chars = hinp.split('.');
	var hkey = parseInt(hinp_chars.pop())%256; // last item is the key
	for (hchix in hinp_chars) {
		hch1 = parseInt(hinp_chars[hchix])%256;
		hch2=(hch1^hkey)%256;
		inp = inp + String.fromCharCode(hch2);
		}
	return inp;
	}

function conditional_unenc_it (hinp){
	if (!hinp){  //empty str
		return "";
		}
	if (	(hinp.match(/[^0-9\.]{1}/)) || // anything other than 0-9 or dot
			(hinp.indexOf('.')==-1)		 // or no dots in it
		){
		// not crypted
		return hinp;
		}
	return unenc_it (hinp);
	}


// ======================== CGI linking & frame building ==============================

// Attach any relevant params to a cgi/uri - from parent, from doc, or from param string passed.
function cgilink (cgi, params){
	var baselink = cgibin+cgi;

	// used to have top.location.search in here, but problems with security if framed from outside
	if (window.location.search){
		baselink = add_srch_params (baselink, window.location.search);
		}
	if (document.location.search){
		baselink = add_srch_params (baselink, document.location.search);
		}
	if (params){
		baselink = add_srch_params (baselink, params);
		}return baselink;
	}

// Adds more ?& params nicely to the end of a path
function add_srch_params (srchstr, params) {
	var webstr=srchstr;
	if (params) {
		// cut anything unwanted inc '?' of the start of the params
		if (params.indexOf('?')>=0){
			params = params.substring(params.indexOf('?')+1);
			}
		if (srchstr.indexOf('?')>0){
			// extend params
			webstr= srchstr+'&'+params;
			}
		else {
			webstr= srchstr+'?'+params;
			}
		}
	webstr = remove_duplicate_params (webstr);
	return webstr;
	}

// Remove duplicate params from uri - the methods used above to
// make sure all params are collected can catch some twice!
function remove_duplicate_params (webstr){
	if (webstr.indexOf('?')<1) {
		return webstr;
		}
	var uri = webstr.split('?')[0];
	var paramlist = webstr.split('?')[1];

	if (! (paramlist.indexOf('&')>=0)){
		// only 1 parameter
		return uri + '?' + paramlist;
		}

	var paramparts = new Array();
	paramparts = paramlist.split ('&');
	paramparts = paramparts.sort();
	var outparams="?";
	var currentparam;
	var lastparam="";
	for (iparam in paramparts) {
		currentparam = paramparts[iparam];
		if (!(currentparam == lastparam)){
			if (outparams=="?"){
				outparams = outparams+currentparam;
				}
			else {
				outparams = outparams+"&"+currentparam;
				}
			}
		lastparam=currentparam;
		}
	return uri + outparams;
	}


// Outputs a framed page with dsmenu and specified cgi in frame
function make_ds_framed_cgi (cgi, params){
	var page_link = cgilink (cgi, params);
	var deepsea_menu_set = '<frameset cols = "155,*" border=0 frameborder=0><frame name="menu" src="'+prefix+'dsmenu.htm" marginwidth=0 marginheight=0 scrolling="auto" noresize>';

	var frame_intro = ' <frameset border=0 frameborder=0><frame name="indiv" src="';
	var frame_end = '" marginwidth=10 marginheight=0 scrolling="auto"></frameset>';

	write_on_wdow (deepsea_menu_set);
	write_on_wdow (frame_intro+page_link+frame_end);
	}

// Outputs a framed page with dsmenu and specified doc in frame
function make_ds_framed_htm (htm, params){
	var page_link = htm;
	if (top.location.search){
		page_link = add_srch_params (page_link, top.location.search);
		}
	if (document.location.search){
		page_link = add_srch_params (page_link, document.location.search);
		}
	if (params){
		page_link = add_srch_params (page_link, params);
		}

	var deepsea_menu_set = '<frameset cols = "155,*" border=0 frameborder=0><frame name="menu" src="'+prefix+'dsmenu.htm" marginwidth=0 marginheight=0 scrolling="auto" noresize>';

	var frame_intro = ' <frameset border=0 frameborder=0><frame name="indiv" src="';
	var frame_end = '" marginwidth=10 marginheight=0 scrolling="auto"></frameset>';

	write_on_wdow (deepsea_menu_set);
	write_on_wdow (frame_intro+page_link+frame_end);
	}


// Outputs an un-framed page with specified cgi in frame
function make_ds_unframed_cgi (cgi, params){
	var page_link = cgilink (cgi, params);

	var frame_intro = ' <frameset border=0 frameborder=0><frame name="indiv" src="';
	var frame_end = '" marginwidth=10 marginheight=0 scrolling="auto"></frameset>';

	write_on_wdow (frame_intro+page_link+frame_end);
	}

// Outputs a simple page with specified cgi in frame
function make_ds_simple_framed_cgi (cgi, params){
	var page_link = cgibin+cgi;
	page_link = add_srch_params (page_link, params);

	var frame_intro = ' <frameset border=0 frameborder=0><frame name="indiv" src="';
	var frame_end = '" marginwidth=10 marginheight=0 scrolling="auto"></frameset>';

	write_on_wdow (frame_intro+page_link+frame_end);
	}

// Derives a board name from a frame's location
function derive_board_name (){
	var pathstuff = get_nice_path();

	// path of form ...../boat/moreinfo/baordname_board.htm
	var pathparts = new Array();
	pathparts = pathstuff.split('/');
	var pplast = pathparts.length-1;

	var board = pathparts[pplast];
	board = board.replace(/_gallery.*/, '');
	board = board.replace(/_board.*/, '');
	board = board.replace(/_spaces.*/, '');

	var boat = pathparts[pplast-2];; // boat is next
	var boat = correct_name(boat);
	return (boat+'_'+board);
	}

// Shows a board title based on board name derived from a frame's location
function show_board_title (title){
	if (title) {
		write_on_wdow ('<title>'+title+'</title>');
		}
	else {
		var pathstuff = get_nice_path();

		// path of form ...../boat/moreinfo/baordname_board.htm
		var pathparts = new Array();
		pathparts = pathstuff.split('/');
		var pplast = pathparts.length-1;

		var board = pathparts[pplast];
		board = board.replace(/_gallery.*/, '');
		board = board.replace(/_board.*/, '');
		board = board.replace(/_spaces.*/, '');

		var boat = pathparts[pplast-2];; // boat is next
		var boat = correct_name(boat);
		write_on_wdow ('<title>'+boat+' '+board+'</title>');
		}
	}

// Derives a schedule name from a frame's location
function derive_schedule_name (){
	var pathstuff = get_nice_path();

	// path of form ...../boat/moreinfo/angling_spaces.htm
	var pathparts = new Array();
	pathparts = pathstuff.split('/');
	var pplast = pathparts.length-1;
	var schedule = pathparts[pplast];
	schedule = schedule.replace(/_gallery.*/, '');
	schedule = schedule.replace(/_board.*/, '');
	schedule = schedule.replace(/_spaces.*/, '');

	var boat = pathparts[pplast-2]; // boat is next
	var boat = correct_name(boat);

	return (boat+'_'+schedule);
	}

// Shows a schedule title based on board name derived from a frame's location
function show_schedule_title (title){
	if (title) {
		write_on_wdow ('<title>'+title+'</title>');
		}
	else {
		var pathstuff = get_nice_path();

		// path of form ...../boat/moreinfo/angling_spaces.htm
		var pathparts = new Array();
		pathparts = pathstuff.split('/');
		var pplast = pathparts.length-1;

		var boat = pathparts[pplast-2]; // boat is next
		var boat = correct_name(boat);
		write_on_wdow ('<title>'+boat+' schedule</title>');
		}
	}

function get_nice_path(){
	if (window.location.pathname) {
		return window.location.pathname;
		}
	if (window.location.href) {
		var pathstuff = window.location.href;
		pathstuff = pathstuff.replace (/\?.*/, ''); // kill all after ?....
		return pathstuff;
		}
	if (document.location.pathname) {
		return  document.location.pathname;
		}
	if (document.location.href) {
		var pathstuff = document.location.href;
		pathstuff = pathstuff.replace (/\?.*/, ''); // kill all after ?....
		return  pathstuff;
		}
	if (document.url.pathname) {
		return  document.location.pathname;
		}
	if (document.url.href) {
		var pathstuff = document.location.href;
		pathstuff = pathstuff.replace (/\?.*/, ''); // kill all after ?....
		return  pathstuff;
		}
	return "";
	}


// General kludge function to correct boat names
function correct_name(boat){
	if (boat=='weymouth_angling_centre') {return 'wac';}
	if (boat=='duchessii') {return 'duchess2';}
	if (boat=='sundance') {return 'sundance2';}
	if (boat=='trio') {return 'trio2';}
	return boat;
	}








// === Debug/diagnostic utilities ======================================
function confirm_debug() {
	var cookie_prefix='deepsea';
	var usrcook=getCookie(cookie_prefix+'uc');
	if (usrcook) {
		if (usrcook.indexOf('Webmaster')>=1){
			return 1;
			}
		}
	return 0;
	}

function debug (msg){
	if (confirm_debug()) {
		alert ('DEBUG\n\n'+msg);
		}
	// else - ignore
	}

// text of cookie values
function cookieDump(){
	return 	'user_cookie='+getCookie('user_cookie')+
			'\nmenu_cookie='+getCookie('menu_cookie')+
			'\nbuttons_cookie='+getCookie('buttons_cookie')+
			'\nstyle_cookie='+getCookie('style_cookie');
	}

// Only write on page if debug enabled
function write_debug_on_wdow(text,wdow){
	if (confirm_debug()) {
		write_on_wdow(text,wdow);
		}
	// else - ignore
	}

function diplay_rendered_html(wdow){
	if (confirm_debug()){
		var diagw = window.open("","","");

		// doc uri, formatted to breal on each parameter
		var d_title = unescape(document.location.href);
		d_title = d_title.replace (/\&/ig, "<br>&");
		d_title = d_title.replace (/\?/ig, "<br>?");

		// doc body, formatted to break on tags, textareas 'escaped'
		var d_body = document.body.parentNode.innerHTML+"\n\n\n\n";
		d_body = d_body.replace ( /textarea/ig, "XTEXTAREA");
		d_body = d_body.replace ( /<br>/ig, "<br>\n");
		d_body = d_body.replace ( /<p/ig, "\n<p");
		d_body = d_body.replace ( /<td/ig, "\n<td");
		d_body = d_body.replace ( /<tr/ig, "\n<tr");
		d_body = d_body.replace ( /<table/ig, "\n<table");
		d_body = d_body.replace ( /<h1/ig, "\n<h1");
		d_body = d_body.replace ( /<h2/ig, "\n<h2");
		d_body = d_body.replace ( /<h3/ig, "\n<h3");
		d_body = d_body.replace ( /<h4/ig, "\n<h4");
		d_body = d_body.replace ( /<option/ig, "\n<option");
		d_body = d_body.replace ( /<input/ig, "\n<input");
		d_body = d_body.replace ( /<script/ig, "\n<script");
		d_body = d_body.replace ( /<noscript/ig, "\n<noscript");
		d_body = d_body.replace ( /<a/ig, "\n<a");
		d_body = d_body.replace ( /<img/ig, "\n<img");
		d_body = d_body.replace ( /<!/ig, "\n<!");

		diagw.document.write('<html><body><p><font face="arial">'+d_title+"</font></p>");
		diagw.document.write("<textarea cols=120 rows=40>"+d_body+"</textarea><br><br></body></html>");
		}
	return false;
	// else - ignore
	}

function dspage_end_diag(wdow) {
	write_debug_on_wdow ('<p class=dsfn>', wdow);
	write_debug_on_wdow ('<a href="error.htm" onClick="return diplay_rendered_html()" target=_top>', wdow);
	write_debug_on_wdow ('View Rendered HTML</a></p>', wdow);
	}


//======= end disgnostic stuff ===========================================