var baseelement = document.getElementsByTagName('base')[0];
var uripath = baseelement.href;

function cfStartup() {
	var anchors = document.getElementsByTagName('a');
	for (i = 0; i < anchors.length; ++i) {
		anchor = anchors[i];
		if (anchor.rel == 'offsite') {
			anchor.onclick = function(e) {return clickURI(uripath+'go.php?id='+this.id.replace(/cplink\d+_/, '')+'&url='+escape(this.href), 0);};
		}
		else if (anchor.rel == 'offsite-popup') {
			anchor.onclick = function(e) {return clickURI(uripath+'go.php?id='+this.id.replace(/cplink\d+_/, '')+'&url='+escape(this.href), 1);};
		}
		else if (anchor.rel == 'popup') {
			anchor.onclick = function(e) {return clickURI(this.href, 1);};
		}
		else if (anchor.rel == 'helpfile') {
			anchor.onclick = function(e) {_launchHelp(this.href); return false;};
		}
	}
	var humanoid = document.getElementById('humanoid');
	if (humanoid) {
		var d = new Date();
		humanoid.value = d.getTime();
	}
}

function clickURI(uri, newwin) {
	if (newwin)
		window.open(uri);
	else
		document.location.href = uri;
	return false;
}

function launchPagePicker(portalid, rootnodeid, tdid) {
	newwin = window.open(uripath + 'includes/pagepicker.php?rootnodeid='+rootnodeid+'&n='+rootnodeid+'&p='+portalid+'&tdid='+tdid+'#node'+rootnodeid,'catpicker','scrollbars=yes,location=no,toolbar=no,status=yes,resizable=yes,width=350,height=400,left=100,top=100');
	if (!newwin.opener)
		newwin.opener = window;
}

function launchLibraryPicker(portalid, rootnodeid, tdid, etype) {
	newwin = window.open(uripath + 'includes/librarypicker.php?rootnodeid='+rootnodeid+'&n='+rootnodeid+'&p='+portalid+'&elementtype='+etype+'&tdid='+tdid+'#node'+rootnodeid,'catpicker','scrollbars=yes,location=no,toolbar=no,status=yes,resizable=yes,width=350,height=400,left=100,top=100');
	if (!newwin.opener)
		newwin.opener = window;
}

function onPagePicked(tdid, pageid, pagetitle) {
	td = window.opener.document.getElementById(tdid);
	for (var i = 0; i < td.childNodes.length; ++i) {
		kid = td.childNodes[i];
		if (kid.tagName) {
			if (kid.tagName.toLowerCase() == 'input' && kid.type.toLowerCase() == 'hidden')
				kid.value = pageid;
			else if (kid.tagName.toLowerCase() == 'div')
				kid.innerHTML = pagetitle;
		}
	}
	window.close();
}

function removePickedPage(tdid) {
	td = window.document.getElementById(tdid);
	for (var i = 0; i < td.childNodes.length; ++i) {
		kid = td.childNodes[i];
		if (kid.tagName) {
			if (kid.tagName.toLowerCase() == 'input' && kid.type.toLowerCase() == 'hidden')
				kid.value = 0;
			else if (kid.tagName.toLowerCase() == 'div')
				kid.innerHTML = '';
		}
	}
}

function launchPickedPage(tdid) {
	td = window.document.getElementById(tdid);
	for (var i = 0; i < td.childNodes.length; ++i) {
		kid = td.childNodes[i];
		if (kid.tagName && kid.tagName.toLowerCase() == 'input' && kid.type.toLowerCase() == 'hidden') {
			elementid = kid.value;
			if (elementid > 0)
				window.open(uripath + 'go.php?id='+elementid, 'pickedpageviewer','scrollbars=yes,location=yes,toolbar=yes,status=yes,resizable=yes');
		}
	}
}

function launchCategoryTitleChooser(type, portalid, nodeid, pageid, elementid, eltid, sep) {
	newwin = window.open(uripath + 'includes/categorylistpicker.php?elt='+eltid+'&sep='+escape(sep)+'&text=1&p='+portalid+'&n='+nodeid+'&q='+pageid+'&element='+elementid+'&mdtype='+type,'catpicker','scrollbars=yes,location=no,toolbar=no,status=yes,resizable=yes,width=350,height=400,left=100,top=100');
	if (!newwin.opener)
		newwin.opener = window;
}

function updateCategoryText(eltid, text, sep) {
	x = window.opener.document.getElementById(eltid);
	if (x.value.length > 0)
		x.value += sep;
	x.value += text;
	window.close();
}

function launchCategoryEditor(type, portalid, nodeid, pageid, elementid) {
	newwin = window.open(uripath + 'includes/category-editor.php?p='+portalid+'&n='+nodeid+'&q='+pageid+'&element='+elementid+'&mdtype='+type,'catpicker','scrollbars=yes,location=no,toolbar=no,status=yes,resizable=yes,width=350,height=400,left=100,top=100');
	newwin.opener = window;
}

function _launchHelp(uri) {
	newwin = window.open(uri,'help','scrollbars=yes,location=no,toolbar=no,status=no,resizable=yes,width=450,height=400,left=100,top=100');
	newwin.opener = window;
}

function launchHelp(title,message) {
	_launchHelp(uripath + 'help.php?title='+title+'&show='+message);
}

function updateCategories(doc, categories, fieldname) {
	var cl, i, ul, category, listid, listname, index, itemid, itemname, li, textnode;
	cl = '';
	ul = doc.getElementById(fieldname);
	ul.innerHTML = '';
	for (i in categories) {
		category = categories[i];
		listid = category[0];
		listname = category[1];
		itemid = category[2];
		itemname = category[3];
		cl += listid + ':' + itemid + ';';
		li = doc.createElement('li');
		if (itemname == '')
			textnode = doc.createTextNode(listname)
		else
			textnode = doc.createTextNode(listname + ' - ' + itemname)
		li.appendChild(textnode);
		ul.appendChild(li);
	}
	if (cl != '') cl = cl.substr(0, cl.length - 1);
	doc.getElementById(fieldname+'_list').setAttribute('value', cl);
}

function launchGeocoder() {
	form1 = document.editform;
	if (form1.geocode_e != '')
		easting = form1.geocode_e.value;
	else
		easting = 0;
	if (form1.geocode_n != '')
		northing = form1.geocode_n.value;
	else
		northing = 0;
	newwin = window.open(uripath + 'includes/cp_geocodes.php?coords=' + easting + ',' + northing);
	if (!newwin.opener)
		newwin.opener = window;
}

function openImageMapEditor (imageURL,areas) {
	if (imageURL != '') {
		newwin = window.open(uripath + 'includes/imagemap-editor.php?image='+imageURL+'&areas='+areas,'imagemap','scrollbars=yes,location=no,toolbar=no,status=yes,resizable=yes,width=640,height=480,left=100,top=100');
		if (!newwin.opener)
			newwin.opener = window;
	}
	else {
		alert('No Image Selected!');
	}
	return false;
}

function galleryViewer(galleryid, itemid) {
	newwin = window.open(uripath + 'galleryviewer.php?itemid=' + itemid, 'galleryviewer' + galleryid, 'scrollbars=yes, location=no, toolbar=no, status=no, resizable=yes, width=700, height=500, left=50, top=50');
	if (!newwin.opener)
		newwin.opener = window;
}

function launchEcard(p, n, galleryid, itemid) {
	newwin = window.open(uripath + 'includes/ecard.php?p='+p+'&n='+n+'&galleryid='+galleryid+'&itemid=' + itemid, 'ecard' + galleryid, 'scrollbars=yes, location=no, toolbar=no, status=yes, resizable=yes, width=500, height=400, left=50, top=50');
	if (!newwin.opener)
		newwin.opener = window;
}

function cp_LaunchUrl(url) {
	if (!url.match(/^(\w+:\/\/|[.\/]+)\w+/))
		url = 'http://' + url;
	window.open(url);
}

function openSpellChecker(txt) {
	var speller = new spellChecker(txt);
	speller.openChecker();
}

function showById (idname) {
	document.getElementById(idname).style.display = 'block';
}

function hideById (idname) {
	document.getElementById(idname).style.display = 'none';
}

function toggleTabpanel (id) {
		
	tabButton = document.getElementById('tab-' + id);
	tabPanel = document.getElementById('tab-' + id + '-panel');
	
	buttons = tabButton.parentNode;
	
	for (i=0;i<buttons.childNodes.length;i++) {
		tab = buttons.childNodes[i];
		
		if (typeof(tab.id) != "undefined" && tab.id != '') {
			panel = document.getElementById(tab.id + '-panel')
			if (typeof(panel) != "undefined") {
				tab.className = '';
				panel.className = 'tabpanel';
			}
		}
	}
	
	tabButton.className = 'selected';
	tabPanel.className = 'tabpanel selected';
	
	return false;
}