// Template functions

function editTemplate(p, n, q, templateid) {
	launchWindow(uripath + 'edit/template?action=edittemplate&p=' + p + '&n=' + n + '&q=' + q + '&templateid=' + templateid,'templateeditor');
}
function launchTrackbacker(url) {
	launchWindow(url,'trackbacker');
}
function launchImageManager(stylesheetid) {
	launchWindow(uripath + 'edit/stylesheet/' + stylesheetid + '/images', 'imagemanager');
}

// Column functions

function addColumn(p, n, q, templateid) {
  launchWindow(uripath + 'edit/template?action=addcolumn&p=' + p + '&n=' + n + '&q=' + q + '&templateid=' + templateid,'templateedit');
}
function editColumn(p, n, q, columnid) {
  launchWindow(uripath + 'edit/template?action=editcolumn&p=' + p + '&n=' + n + '&q=' + q + '&columnid=' + columnid,'templateedit');
}
function leftColumn(p, n, q, columnid) {
  document.location = uripath + '?action=leftcolumn&p=' + p + '&n=' + n + '&q=' + q + '&columnid=' + columnid;
}
function rightColumn(p, n, q, columnid) {
  document.location = uripath + '?action=rightcolumn&p=' + p + '&n=' + n + '&q=' + q + '&columnid=' + columnid;
}
function deleteColumn(p, n, q, columnid) {
  if (confirm('Are you sure you wish to DELETE this column?')) {
    document.location = uripath + '?action=deletecolumn&p=' + p + '&n=' + n + '&q=' + q + '&columnid=' + columnid;
  }
}

// Module functions

function addModule(p, n, q, columnid) {
  launchWindow(uripath + 'edit/template?action=addmodule&p=' + p + '&n=' + n + '&q=' + q + '&columnid=' + columnid,'templateedit');
}
function editPageHolder(p, n, q, moduleid) {
	launchWindow(uripath + 'edit/template?action=editpageholder&p=' + p + '&n=' + n + '&q=' + q + '&moduleid=' + moduleid,'templateedit');
}
function upModule(p, n, q, columnid, moduleid) {
	document.location = uripath + '?action=upmodule&p=' + p + '&n=' + n + '&q=' + q + '&columnid=' + columnid + '&moduleid=' + moduleid;
}
function downModule(p, n, q, columnid, moduleid) {
	document.location = uripath + '?action=downmodule&p=' + p + '&n=' + n + '&q=' + q + '&columnid=' + columnid + '&moduleid=' + moduleid;
}
function leftModule(p, n, q, columnid, moduleid) {
	document.location = uripath + '?action=leftmodule&p=' + p + '&n=' + n + '&q=' + q + '&columnid=' + columnid + '&moduleid=' + moduleid;
}
function rightModule(p, n, q, columnid, moduleid) {
	document.location = uripath + '?action=rightmodule&p=' + p + '&n=' + n + '&q=' + q + '&columnid=' + columnid + '&moduleid=' + moduleid;
}
function deleteModule(p, n, q, blockid, columnid, moduleid) {
	if (confirm('Are you sure you wish to DELETE this module?')) {
		document.location = uripath + '?action=deletemodule&p=' + p + '&n=' + n + '&q=' + q + '&blockid=' + blockid + '&columnid=' + columnid + '&moduleid=' + moduleid;
	}
}

// Page Functions

function editPage(p, n, q, blockid, moduleid, elementid, inpage) {
	launchWindow(uripath + 'edit/element?action=editpage&p=' + p + '&n=' + n + '&q=' + q + '&blockid=' + blockid + '&moduleid=' + moduleid + '&elementid=' + elementid + '&inpage=' + inpage,'elementedit');
}

function editPageMetadata(p, n, q, blockid, moduleid, elementid, inpage) {
	launchWindow(uripath + 'edit/metadata?action=editpage&p=' + p + '&n=' + n + '&q=' + q + '&blockid=' + blockid + '&moduleid=' + moduleid + '&elementid=' + elementid + '&inpage=' + inpage,'metadataedit');
}

function publishPage (url) {
	if (confirm('Are you sure you wish to PUBLISH this page?')) {
		document.location = url;
	}
}


// pageeditor Functions

function pageeditor(p, n, q, blockid, moduleid) {
	launchWindow(uripath + 'edit/element?action=pageeditor&p=' + p + '&n=' + n + '&q=' + q + '&blockid=' + blockid + '&moduleid=' + moduleid,'pageeditor');
}
function editElement(p, n, q, blockid, moduleid, elementid, inpage) {
	launchWindow(uripath + 'edit/element?action=editelement&p=' + p + '&n=' + n + '&q=' + q + '&blockid=' + blockid + '&moduleid=' + moduleid + '&elementid=' + elementid + '&inpage=' + inpage, 'elementedit');
}
function editElementMetadata(p, n, q, blockid, moduleid, elementid, inpage, mode, pmode) {
	launchWindow(uripath + 'edit/metadata?action=editpage&p=' + p + '&n=' + n + '&q=' + q + '&blockid=' + blockid + '&moduleid=' + moduleid + '&elementid=' + elementid + '&inpage=' + inpage + '&mode=' + mode + '&returnpmode=' + pmode,'metadataedit');
}
function editElementRSS(p, n, q, blockid, moduleid, elementid, inpage) {
	launchWindow(uripath + 'edit/rss?action=editpage&p=' + p + '&n=' + n + '&q=' + q + '&blockid=' + blockid + '&moduleid=' + moduleid + '&elementid=' + elementid + '&inpage=' + inpage,'metadataedit');
}
function addElement(p, n, q, blockid, moduleid, elementtypeid, inpage) {
	launchWindow(uripath + 'edit/element?action=addelement&p=' + p + '&n=' + n + '&q=' + q + '&blockid=' + blockid + '&moduleid=' + moduleid + '&elementtypeid=' + elementtypeid + '&inpage=' + inpage,'elementedit');
}
function upElement(p, n, q, blockid, moduleid, elementid, inpage) {
	if (inpage) {
		document.location = uripath + '?action=upelement&p=' + p + '&n=' + n + '&q=' + q + '&blockid=' + blockid + '&moduleid=' + moduleid + '&elementid=' + elementid + '&inpage=' + inpage;
	}
	else {
		document.location = uripath + 'edit/element?action=upelement&p=' + p + '&n=' + n + '&q=' + q + '&blockid=' + blockid + '&moduleid=' + moduleid + '&elementid=' + elementid + '&inpage=' + inpage;
	}
}
function downElement(p, n, q, blockid, moduleid, elementid, inpage) {
	if (inpage) {
		document.location = uripath + '?action=downelement&p=' + p + '&n=' + n + '&q=' + q + '&blockid=' + blockid + '&moduleid=' + moduleid + '&elementid=' + elementid + '&inpage=' + inpage;
	}
	else {
		document.location = uripath + 'edit/element?action=downelement&p=' + p + '&n=' + n + '&q=' + q + '&blockid=' + blockid + '&moduleid=' + moduleid + '&elementid=' + elementid + '&inpage=' + inpage;
	}
}
function deleteElement(p, n, q, blockid, moduleid, elementid, inpage, elementtypename) {
	if (confirm('Are you sure you wish to DELETE this ' + elementtypename + '?')) {
		if (inpage) {
			document.location = uripath + '?action=deleteelement&p=' + p + '&n=' + n + '&q=' + q + '&blockid=' + blockid + '&moduleid=' + moduleid + '&elementid=' + elementid + '&inpage=' + inpage;
		}
		else {
			document.location = uripath + 'edit/element?action=deleteelement&p=' + p + '&n=' + n + '&q=' + q + '&blockid=' + blockid + '&moduleid=' + moduleid + '&elementid=' + elementid + '&inpage=' + inpage;
		}
	}
}
function undeleteElement(p, n, q, blockid, moduleid, elementid, inpage, elementtypename) {
	if (confirm('Are you sure you wish to RESTORE this ' + elementtypename + '?')) {
		if (inpage) {
			document.location = uripath + '?action=undeleteelement&p=' + p + '&n=' + n + '&q=' + q + '&blockid=' + blockid + '&moduleid=' + moduleid + '&elementid=' + elementid + '&inpage=' + inpage;
		}
		else {
			document.location = uripath + 'edit/element?action=undeleteelement&p=' + p + '&n=' + n + '&q=' + q + '&blockid=' + blockid + '&moduleid=' + moduleid + '&elementid=' + elementid + '&inpage=' + inpage;
		}
	}
}
function visibilityElement(p, n, q, blockid, moduleid, elementid, inpage) {
	if (inpage) {
		document.location = uripath + '?action=visibilityelement&p=' + p + '&n=' + n + '&q=' + q + '&blockid=' + blockid + '&moduleid=' + moduleid + '&elementid=' + elementid + '&inpage=' + inpage;
	}
	else {
		document.location = uripath + 'edit/element?action=visibilityelement&p=' + p + '&n=' + n + '&q=' + q + '&blockid=' + blockid + '&moduleid=' + moduleid + '&elementid=' + elementid + '&inpage=' + inpage;
	}
}

function getEditFormPortal() {return document.forms.editform.p.value;}
function getEditFormNode() {return document.forms.editform.n.value;}

var attachments = 0;
function addAttachment () {
	if (element = document.getElementById('attachments')) {
		if (!document.getElementById('attachments' + (attachments - 1)) || document.getElementById('attachments' + (attachments - 1)).value != '') {
			var tr = document.createElement('tr');
			tr.setAttribute('class','file');
			
			var td = document.createElement('td');
			td.innerHTML = 'File:';
			tr.appendChild(td);
			
			var td = document.createElement('td');
			td.innerHTML = "<p><input type=\"text\" name=\"files[new" + attachments + "][name]\" value=\"\" /></p>";
			td.innerHTML += "<p><input type=\"file\" id=\"attachments" + attachments + "\" name=\"files[new" + attachments + "]\" size=\"20\" /> <a class=\"help-icon\" href=\"help/media/concept/document\" onclick=\"launchHelp('Documents','help/media/concept/document'); return false\" title=\"Help for Documents\"><img style=\"border:none;vertical-align:text-top;\" src=\"furniture/helpicon.gif\" alt=\"Help for Documents\" /></a> <span class=\"help-text\">Max Filesize 12M</span></p>";
			tr.appendChild(td);
			
			element.parentNode.insertBefore(tr, element);
			attachments++;
		}
	}
}

cf.addLoadEvent(function() {
	var anchors = document.getElementsByTagName('a');
	for (i = 0; i < anchors.length; ++i) {
		anchor = anchors[i];
		if (anchor.rel == 'editor') {
			anchor.onclick = function() {launchWindow(this.href,'editor'); return false};
		}
	}
});

function onFileSelect (element, targetElement) {
	if (!targetElement)
		targetElement = 'idtitle';
	
	if (titleElement = document.getElementById(targetElement)) {
		if (titleElement.value == '') {
			var newTitle = new String (element.value.match(/[^\\\/]+$/));
			// Remove Extension
			newTitle = newTitle.replace(/.[a-z0-9]+$/i,'');
			// Remove Non Space Charators
			newTitle = newTitle.replace(/[_.\-]/g,' ');
			// Ucwords
			newTitle = newTitle.replace(/^(.)|\s(.)/g, function ( $1 ) { return $1.toUpperCase ( ); } );
			// Space Out Camalise
			newTitle = newTitle.replace(/([A-Z]{1}[a-z]{1,})/g,'$1 ');
			// Remove Dupe Spaces
			newTitle = newTitle.replace(/[\s]{1,}/g,' ');
			
			titleElement.value = newTitle;
		}
	}
}

function editList (name, action) {
	var availableList = document.getElementById('fieldlist_' + name + '_fields');
	var selectedList = document.getElementById('fieldlist_' + name + '_selected');
	switch (action) {
		case 'add':
			var addIndex = availableList.selectedIndex;
			if(addIndex < 0)
				return;
			selectedList.appendChild(availableList.options.item(addIndex));
			availableList.selectedIndex = addIndex > 0 ? addIndex - 1 : addIndex;
			selectedList.selectedIndex = -1;
		break;
		
		case 'remove':
			var selIndex = selectedList.selectedIndex;
			if(selIndex < 0)
				return;
			availableList.appendChild(selectedList.options.item(selIndex))
			availableList.selectedIndex = -1;
			selectedList.selectedIndex = selIndex > 0 ? selIndex-1 : selIndex;
		break;
		
		case 'up':
			var selIndex = selectedList.selectedIndex;
			if(selIndex < 1)
				return;
			var opt = selectedList.options[selIndex];
			selectedList.removeChild(opt);
			selectedList.insertBefore(opt, selectedList.options[selIndex - 1]);
			availableList.selectedIndex = -1;
		break;
		
		case 'down':
			var selIndex = selectedList.selectedIndex;
			if(selIndex >= (selectedList.options.length - 1))
				return;
			var opt = selectedList.options[selIndex];
			selectedList.removeChild(opt);
			if (selIndex > selectedList.options.length - 2)
				selectedList.appendChild(opt);
			else			
				selectedList.insertBefore(opt, selectedList.options[selIndex+1]);
			availableList.selectedIndex = -1;
		break;
	}
	// Update the hidden field
	var listValue = '';
	for (i=0; i < selectedList.options.length; i++) {
		listValue += (listValue != '' ? ',' : '') + selectedList.options[i].value;
	}
	document.getElementById('fieldlist_' + name).value = listValue;
}

