// dmb0_lib.js
// -----------------------------------------------------------------------------
// The 'Brand0' javascript library: Functions used to enhance the interactivity
// of B0 pages, open popups, show infotext, and power DHTML
//
// Release    > 1.49.8
// Project    > brand0_aug05:S01_C03
// Changedate > 11-10-07
// Copyright  > (C) 2001 Thomas Brattli 2001 & Digital Mail (2005)
// Author     > Thomas Brattli 2001, RAK for DML
// -----------------------------------------------------------------------------
// This code is includes part of the DHTML library from DHTMLCentral.com
// (updated by RAK), is opensource, and may be used and changed freely as long
// as this messsage is intact.
//
// Netscape 4 support dropped in Feb 2007 - RAK
// -----------------------------------------------------------------------------

// Add entry to library registration object, creating it if not present
if (typeof dm_library != 'object') var dm_library = new Object(); // Library registration object
if (typeof dm_pg_init != 'object') var dm_pg_init = new Object(); // Has page initialisation run?
dm_library.dmb0_lib = true;
dm_pg_init.dmb0_lib = false;

// ------------------------------------------------------------

// Use DHTML menus? Boolean setting whether to display DHTML menus or not
var dmb0_use_dhtml_menus = 1;

// Use DHTML infotext? Boolean. Can be overridden by variable set by dmx_options.php
var dmb0_use_infotext = typeof dmb0_use_infotext_seed != 'undefined' ? dmb0_use_infotext_seed : 1;

// Pre ECMAScript 3 compatability fixes (mainly for IE 5)
// Prevent 'undefined is undefined' error in IE by defining undefined (in Moz this is defined as the string 'undefined')
var undefined;

// IE Fix: For backwards compatibility, make sure dmb0_show_infotext and dmb0_hide_infotext are defined as empty functions until the page loads
dmb0_show_infotext = dmb0_hide_infotext = function() { return; }

// ------------------------------------------------------------

// Set up page initialisation event handler
if (window.addEventListener) { // W3C
	window.addEventListener('load', dmb0_page_init, true);
} else if (window.attachEvent) { // IE  - Quirks Mode
	window.attachEvent('onload', dmb0_page_init);
} else {
	window.onload = dmb0_page_init;
}

function dmb0_page_init() {
	if (dm_pg_init.dmb0_lib) return;
	dmb0_initialize_DHTML_objects();
	dmb0_show_infotext = dmb0_show_infotext_DEFINITION; // Reassign function to active version
	dmb0_hide_infotext = dmb0_hide_infotext_DEFINITION;
	dm_pg_init.dmb0_lib = true;
}

// ------------------------------------------------------------
// ------------------------------------------------------------
// 'PUBLIC' FUNCTIONS
// ------------------------------------------------------------
// ------------------------------------------------------------

function dmb0_popup() {
	// Flexible popup window function, taking 1-5 arguments
	// of which only the first is mandatory
	//
	// 1st: URL
	// 2nd: target / window name ... use 'popup_window' if not present
	// 3rd: width ....... if 0 or not present use a default (700 pixels)
	// 4th: height ...... if 0 or not present use a default (550 pixels)
	// 5th: scrollbars .. if 0 set to no, if 1 or not present set to yes
	
	if (arguments.length < 1) { alert('Please specify a URL for this popup!') };
	var win_name = arguments[1] ? arguments[1] : 'popup_window';
	var width = arguments[2] ? arguments[2] : 700;
	width = win_name == 'dmNotes' ? 800 : width; // TEMP WIDTH OVERIDE TO ACCOMODATE LEGACY dmNotes PAGES
	var height = arguments[3] ? arguments[3] : 550;
	var scrollbars = arguments[4] == 0 ? 'no' : 'yes';
	var control_string = 'width=' + width + ',height=' + height + ',scrollbars=' + scrollbars;
	window.open(arguments[0], win_name, control_string);
	return false;
}

// ------------------------------------------------------------
// INFOTEXT BUBBLES
// ------------------------------------------------------------

function dmb0_show_infotext_DEFINITION(infotext_copy, event) {
	// Show and, if necessary, initialise infotext bubble
	// Note: this function definition is assigned to dmb0_show_infotext by dmb0_page_init
	if (!dmb0_use_infotext || dmb0_browser.mac && dmb0_browser.ie5) {
		return;
	}
	
	// Delta x and y to cope with IE 6 <SELECT> menu appearing over ANY floating layer
	// Move infotext out of the way if this could happen
	var dx = arguments[2] && dmb0_browser.ie6 ? arguments[2] : 0;
	var dy = arguments[3] && dmb0_browser.ie6 ? arguments[3] : 0;
	
	// Position infotext bubble intelligently - use a standard 15px offset, but make sure it still appears onscreen
	var x, y;
	var mpos = dmb0_find_mouse_position(event);
	x = mpos[0] + 10 + dx; y = mpos[1] + 15 + dy;
	
	var w, h;
	var wdim = dmb0_find_window_dimensions();
	w = wdim[0]; h = wdim[1];
	
	// Quick and dirty menthod of repositioning infotext bubble if it is likely to go offscreen
	if (x > w - 150) { x -= 150; }
	// if (y > h - 35) { y -= 35; }

	// RAK addition: Lazy initialisation for infotext 'dmb0_info_bubble' object, in a DIV called 'dmb0_infotext_bubble'
	if (typeof dmb0_info_bubble != 'object') dmb0_info_bubble = dmb0_create_and_initialize_div('dmb0_infotext_bubble');
	
	//dmb0_info_bubble.write(infotext_copy + " x:" + x + " y: " + y);
	dmb0_info_bubble.write(infotext_copy);
	dmb0_info_bubble.move_to(x, y);
	dmb0_info_bubble.display_block();
	dmb0_handle_ie_bugs('show_infotext');
}

// ------------------------------------------------------------

function dmb0_hide_infotext_DEFINITION() {
	// Hide infotext bubble
	// Note: this function definition is assigned to dmb0_hide_infotext by dmb0_page_init
	if (!dmb0_use_infotext || dmb0_browser.mac && dmb0_browser.ie5) {
		return;
	}
	if (typeof dmb0_info_bubble != 'undefined') {
		dmb0_info_bubble.display_none();
		dmb0_handle_ie_bugs('hide_infotext');
	}
}

// ------------------------------------------------------------

function dmb0_create_and_initialize_div(DOM_name) {
	// create an empty DIV with ID DOM_name
	var d = document.createElement('div');
	d.setAttribute('id', DOM_name);
	window.document.body.appendChild(d);
	// Now initialize it for layer manipulation functions
	js_obj = new dmb0_DHTML_obj(DOM_name);
	dmb0_handle_ie_bugs('create_div');
	return js_obj;
}

// ------------------------------------------------------------

function dmb0_handle_ie_bugs(bug_action) {
	// THIS WAS DESIGNED TO HIDE SELECT ELEMENTS (ie <select> ... </select>) BEHIND INFOTEXT IN IE, BUT DOESN'T WORK YET!
	return;
	
	if (!dmb0_browser.ie) {
		return;
	}
	
	switch (bug_action) { // THIS IS DESIGNED TO HIDE SELECT ELEMENTS BEHIND INFOTEXT IN IE, BUT DOESN'T WORK YET
		
		case 'create_div':
			var info = dmb0_get_DOM_ref('dmb0_infotext_bubble');
			info.insertAdjacentHTML('BeforeEnd', '<iframe style="DISPLAY: none; LEFT: 0px; POSITION: absolute; TOP: 0px" src="javascript:false;" frameBorder="0" scrolling="no" name="dmb0_bug_iframe"></iframe>');
			break;

		case 'show_infotext':
			var iframe = dmb0_get_DOM_ref('dmb0_bug_iframe');
			var info = getComputedStyle(dmb0_get_DOM_ref('dmb0_infotext_bubble'));
			alert('WIDTH: ' + info.width);
			iframe.style.top = info.top;
			iframe.style.left = info.left;
			iframe.style.width = info.width;
			iframe.style.height = info.height;
			iframe.style.zIndex = 15284;
			iframe.style.display = 'block';
			break;
			
		case 'hide_infotext':
			var iframe = dmb0_get_DOM_ref('dmb0_bug_iframe');
			iframe.style.display = 'none';
			break;

	}
}

// ------------------------------------------------------------

// Browsercheck (needed for DHTML code-forks)
function lib_dmb0_browsercheck() { 
	this.ver =   navigator.appVersion;
 	this.agent = navigator.userAgent;
	this.dom = document.getElementById ? 1 : 0;
	this.ie5 = (this.ver.indexOf("MSIE 5") > -1 && this.dom) ? 1 : 0; 
	this.ie6 = (this.ver.indexOf("MSIE 6") > -1 && this.dom) ? 1 : 0;
	this.ie7 = (this.ver.indexOf("MSIE 7") > -1 && this.dom) ? 1 : 0;
	this.ie = this.ie5 || this.ie6 || this.ie7;
	this.mac = this.agent.indexOf("Mac") > -1;
	this.ns6 = (this.dom && parseInt(this.ver) >= 5) ? 1 : 0; 
	this.dmb0_browser = (this.ie5 || this.ie6 || this.ie7 || this.ns6);
	return this;
}
dmb0_browser = new lib_dmb0_browsercheck(); // Browsercheck object

// ------------------------------------------------------------
// Form scripting utilities
// ------------------------------------------------------------

function dmb0_set_s_action_param(val) {
	// Set the s_action (server action) parameter of 'mainform'
	document.forms['mainform'].elements['s_action'].value = val;
}

// ------------------------------------------------------------

function dmb0_set_form_field(form_id, field_id, val) {
	// Set field in form to value
	document.forms[form_id].elements[field_id].value = val;
}

// ------------------------------------------------------------

function dmb0_form_submit_handler(ref) {
	// Use get_post_util, if use_get_post_util flag is set
	if (use_get_post_util) {
		ref.setAttribute('action', dmb1_root + dmb1_get_post_util);
		ref.target = 'blank';
	} else {
		// Append the Coldfusion Session Link (sesslink) to the form action
		ref.setAttribute('action', ref.getAttribute('action') + '?' + sesslink);
	}
}

// ------------------------------------------------------------

function dmb0_preserve_selected(ref, val) {
	// Make sure that the selected element in the drop down menu is preserved, from page hit to page hit
	// ref is a DOM reference to the <SELECT>, and val is the selected value
	for (i=0; i<ref.options.length; i++) {
		if (ref.options[i].value == val) {
			ref.selectedIndex = i;
		}
	}
}

// ------------------------------------------------------------

function dmb0_generate_date_range_select() {
	// Generate options for months from August 2004 to current
	var d = new Date();
	var m = d.getUTCMonth(); // 0 (Jan) - 11 (Dec)
	var y  = d.getUTCFullYear();

	var month = new Array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
	// var days_in_month = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); Not needed by protocol

	var sel_ref = document.forms['mainform'].elements['date_range']; // DOM ref to select

	var dr;
        var yearadjust;
	while (y > 2004 || (y == 2004 && m > 6)) { // Go back to August 2004
                if (m==11) yearadjust = 1; else yearadjust = 0;
                dr = y + '0'.concat(m + 1).slice(-2) + '01' + '-' + (y + yearadjust) + '0'.concat(((m + 1) * (m != 11) + 1)).slice(-2) + '01';
		append_option(sel_ref, y + ' ' + month[m], dr);
		m--;
		if (m == -1) { y--; m = 11; }
	}

}

// ------------------------------------------------------------

function dmb0_append_option(ref, txt, val) {
	ref.options[ref.options.length] = new Option(txt, val);
}

// ------------------------------------------------------------
// ------------------------------------------------------------
// POSITIONING UTILITY FUNCTIONS
// ------------------------------------------------------------
// ------------------------------------------------------------

function dmb0_find_div_position(id) {
	// Find the position of a div (if one argument is supplied - the div id)
	// Supply DIV id (compulsorary)

	x = 0;
	y = 0;
	var el, temp;
	el = document.getElementById(id);
	if (el.offsetParent) {
		temp = el;
		while (temp.offsetParent) { // Looping parent elements to get the offset from them as well
			temp = temp.offsetParent; 
			x += temp.offsetLeft;
			y += temp.offsetTop;
		}
	}
	x += el.offsetLeft;
	y += el.offsetTop;
	return [x,y];
}

// ------------------------------------------------------------

function dmb0_find_mouse_position(event) {
	// Get current mouse position, and return it as an array
	if (typeof event.pageX == "number" ) { // Moz
		x = event.pageX;
		y = event.pageY;
	} else if (event.clientX || event.clientY) {
		x = event.clientX;
		y = event.clientY;
		if (dmb0_browser.ie) {
			if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {
				// IE is in DOM compliant mode
				x += document.body.scrollLeft;
				y += document.body.scrollTop;
			} else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
				//IE6 standards compliant mode
				x += document.documentElement.scrollLeft;
				y += document.documentElement.scrollTop;
			}
		}
	}
	return [x,y];
}

// ------------------------------------------------------------

function dmb0_find_window_dimensions() {
	// Return the dimensions of the area used to display content in the browser
	x = document.body.innerWidth ? document.body.innerwidth : document.body.clientWidth ? document.body.clientWidth : 'cant find width';
	y = document.body.innerHeight ? document.body.innerHeight : document.body.clientHeight ? document.body.clientHeight : 'cant find height';
	return [x,y];	
}

// ------------------------------------------------------------

dmb0_DHTML_obj.prototype.show_tl_window = function() {
	// for showing hidden DIV objects at the top left of the browser window (inset by 20 pixels)
	// no matter what the scroll position
	x = dmb0_browser.ns6 ? window.pageXOffset : document.body.scrollLeft;
	y = dmb0_browser.ns6 ? window.pageYOffset : document.body.scrollTop;
	this.move_to(x+20, y+20);
	this.show();
	this.dragdrop();
	return false;
}

// ------------------------------------------------------------
// Format info on LHS navbar
// ------------------------------------------------------------

function dmb0_format_lhs_navbar() {
	bar = '';
	accType != '' ? bar = accType + ' : ' : '';
	accRef != '' ? bar += accRef + ' : ' : '';
	userID != '' ? bar += userID + ' : ' : '';
	additional != '' ? bar += additional + ' : ' : '';
	mode != '' ? bar += '<span class="internal">&nbsp;' + mode + '&nbsp;</span>' + ' : ' : '';
	bar = bar.substring(0, bar.length - 3);
	return bar;
}

// ------------------------------------------------------------

function dmb0_initialize_DHTML_objects() {
	page   = new dmb0_lib_doc_size();
}

/*******************************
* DHTML LAYER UTILITY ROUTINES *
*******************************/

function dmb0_get_DOM_ref(obj) {
	// cross-browser function to get an object's DOM reference given its id
	if(document.getElementById && document.getElementById(obj)) {
		return document.getElementById(obj); // W3C DOM
	} else if (document.all && document.all(obj)) {
		return document.all(obj); // MSIE 4 DOM
	} else if (document.layers && document.layers[obj]) {
		return document.layers[obj]; // NN 4 DOM.. note: this won't find nested layers
	}
	return false;
}

// START OF OPENSOURCE CODE LIBABY

/****************************************************************************   
*   DHTML library from DHTMLCentral.com
*   Copyright (C) 2001 Thomas Brattli 2001
*   This script was released at DHTMLCentral.com
*   Visit for more great scripts!
*   This may be used and changed freely as long as this msg is intact!
*   We will also appreciate any links you could give us.
*
*   Made by Thomas Brattli 2001
*   Modified by Richard Knight (RAK) 2003 / 2005 / 2006
***************************************************************************/

// Lib objects
function dmb0_DHTML_obj(obj, nest) { 
	// alert(obj);
	if (!dmb0_browser.dmb0_browser) return lib_message('Old browser');
  
	// RAK ADDED CODE ... CHECK OBJECT EXISTS BEFORE TRYING TO MODEL IT
	// to prevent errors in IE5 when a call is made for a non-existant DIV
	if ( !( (document.getElementById && document.getElementById(obj)) || (document.all && document.all[obj]) ) ) {
		return;
	}
	// END RAK ADDED CODE
  
	nest=(!nest) ? '' : 'document.' + nest + '.';
	this.evnt = dmb0_browser.dom ? document.getElementById(obj) : 0;	
	if (!this.evnt) {
		return lib_message('The layer does not exist (' + obj + ')' + '- \nIf your using Netscape please check the nesting of your tags!');
	}
  
	this.css = dmb0_browser.dom ? this.evnt.style : this.evnt; 
	this.ref = dmb0_browser.dom ? document : this.css.document;
	this.x = parseInt(this.css.left) || this.css.pixelLeft || this.evnt.offsetLeft || 0;
	this.y = parseInt(this.css.top) || this.css.pixelTop || this.evnt.offsetTop || 0
	this.w = this.evnt.offsetWidth || this.css.clip.width || this.ref.width || this.css.pixelWidth || 0; 
	this.h = this.evnt.offsetHeight || this.css.clip.height || this.ref.height || this.css.pixelHeight || 0;
	this.c = 0; //Clip values
	if (dmb0_browser.dom && this.css.clip) {
		this.c = this.css.clip; this.c = this.c.slice(5,this.c.length-1); 
		this.c = this.c.split(' ');
		for (var i=0; i<4; i++) { this.c[i] = parseInt(this.c[i]) }
	}
	this.ct = this.css.clip.top || this.c[0] || 0; 
	this.cr = this.css.clip.right || this.c[1] || this.w || 0;
	this.cb = this.css.clip.bottom || this.c[2] || this.h || 0; 
	this.cl = this.css.clip.left || this.c[3] || 0;
	this.obj = obj + "Object"; eval(this.obj + "=this");
	this.className = this.evnt.className;			
	return this;
}

// Moving object to
dmb0_DHTML_obj.prototype.move_to = function(x, y) {
	this.x = x;
	this.y = y;
	this.css.left = x +'px';
	this.css.top  = y + 'px';
}

// Moving object by
dmb0_DHTML_obj.prototype.move_by = function(x, y) {
	this.x = this.x + x;
	this.y = this.y + y;
	this.css.left = this.x + 'px';
	this.css.top = this.y + 'px';
}

// Showing object
dmb0_DHTML_obj.prototype.show = function() {
	this.css.visibility = 'visible';
}

// Hiding object
dmb0_DHTML_obj.prototype.hide = function() {
	this.css.visibility = 'hidden';
}

// ------------------------------------------------------------
// RAK ADDED CODE

// Changing the display of an object
dmb0_DHTML_obj.prototype.display_block = function() {
	this.css.display = 'block';
}

// Changing the display of an object
dmb0_DHTML_obj.prototype.display_none = function() {
	this.css.display = 'none';
}

// Setting the class
dmb0_DHTML_obj.prototype.set_class = function(classname) {
	this.className = classname;
}

// END RAK ADDED CODE
// ------------------------------------------------------------

// Changing backgroundcolor
dmb0_DHTML_obj.prototype.bg = function(color) { 
	if (dmb0_browser.opera) this.css.background=color;
	else if (dmb0_browser.dom) this.css.backgroundColor=color;
}

// Writing content to object
dmb0_DHTML_obj.prototype.write = function(text, startHTML, endHTML){
	this.evnt.innerHTML = text;
}

// Slide animation
dmb0_DHTML_obj.prototype.slide = function(endx, endy, inc, speed, fn, wh) {
	if(!this.slideactive) {
		var distx = endx - this.x; var disty = endy - this.y;
		var num = Math.sqrt(Math.pow(distx, 2) + Math.pow(disty, 2)) / inc;
		var dx = distx/num; var dy = disty/num;
		this.slideactive = 1; 
		if (!wh) wh=0; if (!fn) fn=0;
		this.slide_execute(dx, dy, endx, endy, speed, fn, wh);
    }
}

dmb0_DHTML_obj.prototype.slide_execute = function(dx, dy, endx, endy, speed, fn, wh) {
	if (this.slideactive && (Math.floor(Math.abs(dx)) < Math.floor(Math.abs(endx-this.x)) || Math.floor(Math.abs(dy)) < Math.floor(Math.abs(endy-this.y)))) {
		this.move_by(dx, dy); 
		if (wh) eval(wh);
		setTimeout(this.obj+".slide_execute(" + dx + ", " + dy + ", " + endx + ", " + endy + ", " + speed + ", '" + fn + "', '" + wh + "')", speed);
	} else {
		this.slideactive = 0; 
		this.move_to(endx, endy);
		if (fn) eval(fn);
	}
}

// Document size object
function dmb0_lib_doc_size() { 
	this.x = 0;
	this.x2 = dmb0_browser.ie && document.body.offsetWidth-20 || innerWidth || 0;
	this.y = 0;
	this.y2 = dmb0_browser.ie && document.body.offsetHeight-5 || innerHeight || 0;
	if (!this.x2 || !this.y2) return message('Document has no width or height'); 
	this.x50 = this.x2/2;
	this.y50 = this.y2/2;
	return this;
}

// ------------------------------------------------------------
// Drag drop functions start
// ------------------------------------------------------------

dmb0_dd_is_active = 0;
dmb0_dd_obj = 0;
dmb0_dd_mobj = 0;

function dmb0_lib_dd() {
	dmb0_dd_is_active=1;
	document.onmousemove = dmb0_lib_dd_move;
	document.onmousedown = dmb0_lib_dd_down;
	document.onmouseup   = dmb0_lib_dd_up;
}

dmb0_DHTML_obj.prototype.dragdrop = function(obj) {
	if (!dmb0_dd_is_active) dmb0_lib_dd()
	this.evnt.onmouseover = new Function("dmb0_lib_dd_over("+this.obj+")")
	this.evnt.onmouseout  = new Function("dmb0_dd_mobj=0")
	if (obj) this.ddobj=obj;
}

dmb0_DHTML_obj.prototype.nodragdrop = function() {
	this.evnt.onmouseover = '';
	this.evnt.onmouseout  = '';
	dmb0_dd_obj=0;
	dmb0_dd_mobj=0;
}

//Drag drop event functions
function dmb0_lib_dd_over(obj) { dmb0_dd_mobj = obj }

function dmb0_lib_dd_up(e) { dmb0_dd_obj = 0 }

function dmb0_lib_dd_down(e) { // Mousedown
  if (dmb0_dd_mobj) {
    x = dmb0_browser.ns6 ? e.pageX : event.x || event.clientX;
    y = dmb0_browser.ns6 ? e.pageY : event.y || event.clientY;
    dmb0_dd_obj = dmb0_dd_mobj;
    dmb0_dd_obj.clX = x - dmb0_dd_obj.x; 
    dmb0_dd_obj.clY = y - dmb0_dd_obj.y;
  }
}

function dmb0_lib_dd_move(e, y, rresize) { // Mousemove
	x = dmb0_browser.ns6 ? e.pageX : event.x || event.clientX;
	y = dmb0_browser.ns6 ? e.pageY : event.y || event.clientY;
	if (dmb0_dd_obj) {
		nx = x-dmb0_dd_obj.clX;
		ny = y-dmb0_dd_obj.clY;
		if (dmb0_dd_obj.ddobj) dmb0_dd_obj.ddobj.move_to(nx, ny);
		else dmb0_dd_obj.move_to(nx, ny);
	}
	return false;      
}
// Drag drop functions end