// general constants

var hrefbase = document.location.href.match("^https?://[^/]*(:[0-9]*)?/([^_][^/]*/)*")[0];
document.location.hrefbase = hrefbase;


//////////////////////////////////////////////////////////
// Some utility functions
//////////////////////////////////////////////////////////

function getAllTextNodes(root) {
	var children;
	var	textNodes = new Array();
	children = root.childNodes;
	for(var i=0; i<children.length; i++) {
		var node = children[i];
		if(node.nodeType == 3)
			textNodes.push(node);
		if(node.canHaveHTML) {
			var tmp = textNodes.concat(getAllTextNodes(node));
			textNodes = tmp;
		}
	}
	return textNodes;
}

function getContextNode(textNode) {
	var container;
	for(container=textNode; container && ! container.nodeName.match(/^(BODY|DIV|P|TD|H1|H2|H3|LI|TH)$/); container=container.parentNode);
	return container;
}

function openDialog(url, name, properties, data) {
	var newwindow = window.open(url, name, properties);
	if(newwindow) {
		newwindow.dialogData = data;
	}
}

function addOption(control, value, text) {
	if(!control) return;
	if(!text) text = value;
	var opt = document.createElement('OPTION');
	opt.value = value;
	opt.innerText = text;
	control.appendChild(opt);
	return;
}

function disableElement(node) {
	if(!node) return;
	node.disabled = true;
	if(node.nodeName == 'INPUT')
		node.style.backgroundColor = 'gray';
}

function enableElement(node) {
	if(!node) return;
	node.disabled = false;
	if(node.nodeName == 'INPUT')
		node.style.backgroundColor = 'white';
}

function disableElements(elementType) {
	var nodes = document.getElementsByTagName(elementType);
	for(i=0; i<nodes.length; i++)
		disableElement(nodes[i]);
}

function enableElements(elementType) {
	var nodes = document.getElementsByTagName(elementType);
	for(i=0; i<nodes.length; i++)
		enableElement(nodes[i]);
}

function setStyleRecursive(node, attribute, value) {
	var allnodes = node.getElementsByTagName('*');
	for(var i=0; i < allnodes.length; i++)
		if(allnodes[i].style)
			allnodes[i].style[attribute] = value;
}

//////////////////////////////////////////////////////////
// objects
//////////////////////////////////////////////////////////

function hierarchy_obj(id, name)
{
	this.id = id;
	this.name = name;
	this.children = new Array();
	this.find   = _hierarchyFind;

	function _hierarchyFind(needle){
		var obj;
		if(this.id == needle) return this;
		for(var child in this.children) {
			if(this.debug) {
				alert(child);
				alert(this.children[child]);
				alert(this.children[child].find);
			}
			if (obj = this.children[child].find(needle))
				return obj;
		}
		return null;
	}
}

function page_obj(page_id, parent_page_id, name, name_width, type, active, access, meta_description, meta_keywords, place, banner_jpeg, banner_flash, nav_image_id, nav_active_image_id, nav_rollover_image_id, banner_image_id, title, blurb, html_title, owner)
{
	this.page_id = page_id;
	this.parent_page_id = parent_page_id;
	this.name = name;
	this.name_width = name_width;
	this.type = type;
	this.active = active;
	this.access = access;
	this.meta_description = meta_description;
	this.meta_keywords = meta_keywords;
	this.html_title = html_title;
	this.title = title;
	this.blurb = blurb;
	this.place = place;
	this.banner_jpeg = banner_jpeg;
	this.banner_flash = banner_flash;
	this.nav_image_id = nav_image_id;
	this.nav_active_image_id = nav_active_image_id;
	this.nav_rollover_image_id = nav_rollover_image_id;
	this.banner_image_id = banner_image_id;
	this.owner = owner;
}

function content_obj(content_id, pagev_id, section, type, place, display_date, title, text, text_colour, text_bgcolour, content_html, content_plain, url, image_id, date_create, date_modified, date_start, date_end)
{
	this.content_id = content_id;
	this.pagev_id = pagev_id;
	this.section = section;
	this.type = type;
	this.place = place;
	this.display_date = display_date;
	this.title = title;
	this.text = text;
	this.text_colour = text_colour;
	this.text_bgcolour = text_bgcolour;
	this.content_html = content_html;
	this.content_plain = content_plain;
	this.url = url;
	this.image_id = image_id;
	this.date_create = date_create;
	this.date_modified = date_modified;
	this.date_start = date_start;
	this.date_end = date_end;
}

function image_obj(image_id, directory, file, size, date_modified, width, height)
{
	this.image_id = image_id;
	this.directory = directory;
	this.file = file;
	this.size = size;
	this.date_modified = date_modified;
	this.width = width;
	this.height = height;
}

function user_obj(user_id, name, nicename, email, password, administrator)
{
	this.user_id = user_id;
	this.name = name;
	this.nicename = nicename;
	this.email = email;
	this.password = password;
	this.administrator = administrator;
}

function workflow_obj(workflow_id, workflow_name) {
	this.workflow_id = workflow_id;
	this.workflow_name = workflow_name;
}

function role_obj(role_id, role_name, permissions) {
	this.role_id = role_id;
	this.role_name = role_name;
	this.permissions = permissions;
}

function banner_obj(banner_id, banner_image_id, url)
{
	this.banner_id = banner_id;
	this.banner_image_id = banner_image_id;
	this.url = url;
}

function file_obj(directory, file, size, date_modified)
{
	this.directory = directory;
	this.file = file;
	this.size = size;
	this.date_modified = date_modified;
}


// dialog functions

function dialog_areyousure(message)
{
//*** This function is currently on the back burner. Maybe we'll re-instate it if we re-skin the interface
//*** For now it's just a wrapper around the javascript "confirm" function 

	return confirm(message);
/*

	var xin=new Array(), xout;

	xin['message'] = message;

	xout = window.showModalDialog(hrefbase + '_common/dialog/areyousure.asp', xin, 'dialogWidth: 382px; dialogHeight: 137px; status: no;');

	return xout['ok'] == 'yes';
*/
}

function dialog_cellproperties(xin)
{
	var xout;

	xout = window.showModalDialog(hrefbase + '_common/dialog/cellproperties.asp', xin, 'dialogWidth: 510px; dialogHeight: 200px; status: no;');

	if (xout['ok'] == 'yes')
	{
		xin['width'] = xout['width'];
		xin['height'] = xout['height'];
		xin['rowspan'] = xout['rowspan'];
		xin['colspan'] = xout['colspan'];
		xin['align'] = xout['align'];
		xin['valign'] = xout['valign'];
		xin['bgcolor'] = xout['bgcolor'];
	}

	return xout['ok'] == 'yes';
}

function dialog_colourchooser(heading, colour)
{
	var xin=new Array(), xout;

	xin['heading'] = heading;
	xin['colour'] = colour;

	xout = window.showModalDialog(hrefbase + '_common/dialog/colourchooser.asp', xin, 'dialogWidth: 250px; dialogHeight: 275px; status: no;');

	if (xout['ok'] == 'yes')
		return xout['colour'];
	else
		return colour;
}

function dialog_getline(heading, text)
{
	var xin=new Array(), xout;

	xin['heading'] = heading;
	xin['text'] = text;

	xout = window.showModalDialog(hrefbase + '_common/dialog/getline.asp', xin, 'dialogWidth: 406px; dialogHeight: 143px; status: no;');

	if (xout['ok'] == 'yes')
		return xout['text'];
	else
		return text;
}

function dialog_htmlimagechooser(xin)
{
	var xout;

	xout = window.showModalDialog(hrefbase + '_common/dialog/htmlimagechooser.asp', xin, 'dialogWidth: 452px; dialogHeight: 550px; status: no;');

	if (xout['ok'] == 'yes')
	{
		xin['url'] = xout['url'];
		xin['align'] = xout['align'];
		xin['alt'] = xout['alt'];
		xin['caption'] = xout['caption'];
	}

	return xout['ok'] == 'yes';
}

function dialog_imagechooser(heading, image_id)
{
	var xin=new Array(), xout;

	xin['heading'] = heading;
	xin['image_id'] = image_id;

	xout = window.showModalDialog(hrefbase + '_common/dialog/imagechooser.asp', xin, 'dialogWidth: 432px; dialogHeight: 381px; status: no;');

	if (xout['ok'] == 'yes')
		return xout['image_id'];
	else
		return image_id;
}

function dialog_linkproperties(xin)
{
	var xout;

	xout = window.showModalDialog(hrefbase + '_common/dialog/linkproperties.asp', xin, 'dialogWidth: 483px; dialogHeight: 311px; status: no;');

	if (xout['ok'] == 'yes')
	{
		xin['href'] = xout['href'];
	}

	return xout['ok'] == 'yes';
}

function dialog_rowproperties(xin)
{
	var xout;

	xout = window.showModalDialog(hrefbase + '_common/dialog/rowproperties.asp', xin, 'dialogWidth: 275px; dialogHeight: 180px; status: no;');

	if (xout['ok'] == 'yes')
	{
		xin['height'] = xout['height'];
		xin['bgcolor'] = xout['bgcolor'];
	}

	return xout['ok'] == 'yes';
}

function dialog_showmessage(message)
{
//*** This function is currently on the back burner. Maybe we'll re-instate it if we re-skin the interface
//*** For now it's just a wrapper around the javascript "alert" function 
	return alert(message);
/*

	var xin=new Array(), xout;

	xin['message'] = message;

	window.showModalDialog(hrefbase + '_common/dialog/showmessage.asp', xin, 'dialogWidth: 382px; dialogHeight: 137px; status: no;');
*/
}

function dialog_tableproperties(xin)
{
	var xout;

	xout = window.showModalDialog(hrefbase + '_common/dialog/tableproperties.asp', xin, 'dialogWidth: 460px; dialogHeight: 240px; status: no;');

	if (xout['ok'] == 'yes')
	{
		xin['width'] = xout['width'];
		xin['cellpadding'] = xout['cellpadding'];
		xin['rows'] = xout['rows'];
		xin['columns'] = xout['columns'];
		xin['type'] = xout['type'];
		xin['caption'] = xout['caption'];
	}

	return xout['ok'] == 'yes';
}

function dialog_textedit(heading, text, required)
{
	var xin=new Array(), xout;

	// Correct any undefined values
	if(!required) required = false;

	xin['heading'] = heading;
	xin['text'] = text;

	xout = window.showModalDialog(hrefbase + '_common/dialog/textedit.asp?required=' + required.toString(), xin, 'dialogWidth: 606px; dialogHeight: 425px; status: no;');

	if (xout['ok'] == 'yes')
		return xout['text'];
	else
		return text;
}


// dialog helper
function dialog_colourchooser_helper(heading, id)
{
	var xin=new Array(), xout;

	xin['heading'] = heading;
	xin['colour'] = document.getElementById(id).value;

	xout = window.showModalDialog(hrefbase + '_common/dialog/colourchooser.asp', xin, 'dialogWidth: 226px; dialogHeight: 265px; status: no;');

	if (xout['ok'] == 'yes')
		document.getElementById(id).value = xout['colour'];
}

function dialog_imagechooser_helper(heading, base)
{
	var xin=new Array(), xout;

	//alert('general.js/dialog_imagechooser_helper: heading=[' + heading + '], base=[' + base + ']');

	xin['heading'] = heading;
	xin['image_id'] = document.getElementById(base + '_image_id').value;

	xout = window.showModalDialog(hrefbase + '_common/dialog/imagechooser.asp', xin, 'dialogWidth: 550px; dialogHeight: 381px; status: no;');

	if (xout['ok'] == 'yes')
	{
		document.getElementById(base + '_image_id').value = xout['image_id'];
		document.getElementById(base + '_image_name').value = xout['name'];
	}
}

function dialog_linkproperties_helper(heading, base)
{
	var xin=new Array(), xout;

	xin['heading'] = heading;
	xin['href'] = document.getElementById(base + '_url').value;
	xin['refract'] = refract;
	xin['anchors'] = new Array();

	xout = window.showModalDialog(hrefbase + '_common/dialog/linkproperties.asp', xin, 'dialogWidth: 483px; dialogHeight: 311px; status: no;');

	if (xout['ok'] == 'yes')
		document.getElementById(base + '_url').value = xout['href'];
}


function dialog_filelinkproperties_helper(heading, base, start_dir)
{
	var xin=new Array(), xout;

	xin['heading'] = heading;
	xin['href'] = document.getElementById(base).value;
	xin['refract'] = refract;
	xin['anchors'] = new Array();
    xin['start_dir'] = start_dir;


	xout = window.showModalDialog(hrefbase + '_common/dialog/filelinkproperties.asp', xin, 'dialogWidth: 483px; dialogHeight: 311px; status: no;');

	if (xout['ok'] == 'yes')
		document.getElementById(base).value = xout['href'];
}

// general functions

function isdigit()
{
	return ((event.keyCode >= 48) && (event.keyCode <= 57))
}

function unselectable(root)
{
	var i, node, all;
	//alert('general.js/unselectable: root.tagName=[' + root.tagName + ']');

	if (!root.all) return;

	root.unselectable = true;
	all = root.all;

	for (i=0; i<all.length; i++)
	{
		node = all(i);
		if (node.className == 'alwaysSelectable' || node.tagName == 'INPUT' || node.tagName == 'TEXTAREA' || (node.nodeType == 1 && node.tagName == 'DIV' && node.contentEditable))
			node.unselectable = false;
		else
			node.unselectable = true;
	}
}

// move up/down elements
function shuffle(dir, node)
{
	if(!node) return;
	if (dir == 'up')
	{
		if (node.previousSibling)
			node.previousSibling.insertAdjacentElement('beforeBegin', node);
		else
			dialog_showmessage('This is already at the top.');
	}
	else if (dir == 'down')
	{
		if (node.nextSibling)
			node.nextSibling.insertAdjacentElement('afterEnd', node);
		else
			dialog_showmessage('This is already at the bottom.');
	}
	else
	{
		dialog_showmessage('Direction is unknown.');
	}
}

function locate_page(root, page_id) {
	var i, page;

	page = null;

	for(i in root.children) {
		if(i == page_id) {
			return(root.children[i]);
		}
		else
			if(page = locate_page(root.children[i], page_id))
				return(page);
	}
	return(page);
}

function getRefractHrefButton(label, href) {
		return '<span class="refractButtonContainer"><a class="refractButton" href="' + href + '">' + label + '</a><span class="refractButtonEnd">&nbsp;</span></span>';
}

function getRefractOnclickButton(label, onclick) {
		return '<span class="refractButtonContainer" ><span class="refractButton" onclick="' + onclick + '">' + label + '</span><span class="refractButtonEnd">&nbsp;</span></span>';
}
