// JavaScript Document

function save() {
	edtForm.inpContent.value = oEdt.getHTMLBody();
	edtForm.submit();
}

function cancel(sloc) {
	if (confirm("Discard your changes, are you sure?")) location.href=sloc;
	
}

function prevverValid() {
	if (updated()) if (!confirm("You have not saved the updates you've made to the page content." + String.fromCharCode(13) + "You should do this before adding loading an old Version or you will lose" + String.fromCharCode(13) + "your changes - do you really want to continue?")) return true;
	if (loadver.prevver.value=="") {alert("Please select a previous version before proceeding."); return false;}
	loadver.submit();
}

function sectionValid() {
	if (newsection.sectname.value=="") {alert("Please enter a name for the section first."); return false;}
	return true;
}

function deleteVersion() {
	if (updated()) if (!confirm("You have not saved the updates you've made to the page content." + String.fromCharCode(13) + "You should do this before adding an old Version or you will lose" + String.fromCharCode(13) + "your changes - do you really want to continue?")) return true;
	if (prevverValid()) {
		if (confirm("Are you sure you want to delete this previous version?" + String.fromCharCode(13) + "There is no undo and the version will be permanently deleted.")) {
			newsection.delsect.value = "yes";
			newsection.submit();
		}
	}
}

function deleteSection() {
	if (sectionValid()) {
		if (confirm("Are you sure you want to delete '" + newsection.sectname.value + "'?" + String.fromCharCode(13) + "There is no undo and the section will be permanently deleted.")) {
			if (confirm("This will also delete all the pages and Sub-Sections in '" + newsection.sectname.value + "'" + String.fromCharCode(13) + String.fromCharCode(13) + "ARE YOU REALLY SURE?")) {
				newsection.delsect.value = "yes";
				newsection.submit();
			}
		}
	}
}

function orderSection() {
	var fld = document.getElementById("saveorder");
	var frm = document.getElementById("newsection");
	setSectOrder();
	fld.value = "yes";
	frm.submit();
}

function setSectName() {
	var lst = document.getElementById("sectname");
	var fld = document.getElementById("newsectname");
	fld.value = lst.value;
}

function renameSection() {
	if (sectionValid()) {
		var oNewCat = document.getElementById("newsectname");
		newCat = oNewCat.value;
		while (newCat != null) {
			var sCheck = new String(newCat);
			aInvalid = [String.fromCharCode(92), "/", ":", "*", "?", String.fromCharCode(34), "'", "<", ">", "|"];
			valid=true;
			for (i=0;i<aInvalid.length;i++) {
				if (sCheck.indexOf(aInvalid[i]) >= 0) {
					alert("A section name cannot contain any of the following characters:" + String.fromCharCode(13) + String.fromCharCode(92) + "/ : * ? " + String.fromCharCode(34) + " ' < > |");
					valid=false;
					break;
				}
			}
			if (valid) {
				var sNew = new String(newCat);
				var sExist;
				for (i=0;i<newsection.sectname.length;i++) {
					sExist = new String(newsection.sectname.options(i).value);
					if (sExist.toLowerCase()==sNew.toLowerCase()) {
						alert("That section name already exists, please choose another name or delete the exiting section first.");
						valid=false;
						break;
					}
				}
				if (valid) {
					newsection.newsectname.value = newCat; 
					newsection.rensect.value = "yes"; newsection.submit();
					newCat=null;}
				else {
					newCat = window.prompt("Rename '" + newsection.sectname.value + "' to:", newCat);
				}
			}
			else {
				newCat = window.prompt("Rename '" + newsection.sectname.value + "' to:", newCat);
			}
		}
	}
}

function addSection() {
	var newCat;
	var oNewCat = document.getElementById("newsectname");
	newCat = oNewCat.value;
	while (newCat != null) {
		var sCheck = new String(newCat);
		aInvalid = [String.fromCharCode(92), "/", ":", "*", "?", String.fromCharCode(34), "'", "<", ">", "|"];
		valid=true;
		for (i=0;i<aInvalid.length;i++) {
			if (sCheck.indexOf(aInvalid[i]) >= 0) {
				alert("A section name cannot contain any of the following characters:" + String.fromCharCode(13) + String.fromCharCode(92) + "/ : * ? " + String.fromCharCode(34) + " ' < > |");
				valid=false;
				break;
			}
		}
		if (valid) {
			newEle = document.createElement("OPTION"); newEle.text = newCat; newEle.value = newCat;
			newsection.sectname.options.add(newEle); newsection.sectname.value = newCat; newsection.submit();
			newCat=null;
		}
		else {
			newCat = window.prompt("New Section Name", newCat);
		}
	}
}

function updated() {
	if (oEdt.getHTMLBody() != edtForm.inpContent.value) return true; else return false;
}

function moveitem(sMove) {
	var oList = document.getElementById("sectname");
	if ((sMove=="up" || sMove=="top") && oList.selectedIndex == 0) return false;
	if ((sMove=="down" || sMove=="bottom") && oList.selectedIndex == oList.length) return false;
	
	var oEle = oList.options[oList.selectedIndex];
	var iIndex = oList.selectedIndex;
	oList.options.remove(oList.selectedIndex);
	
	switch (sMove) {
		case "top": oList.options.add(oEle, 0); break;
		case "up": oList.options.add(oEle, iIndex-1); break;
		case "down": oList.options.add(oEle, iIndex+1); break;
		case "bottom": oList.options.add(oEle, oList.length); break;
		default: return false;
	}
	return true;
}


function setSectOrder() {
	var oList = document.getElementById("sectname");
	var oField = document.getElementById("setorder");
	var sOrder = "", i;
	for (i=0;i<oList.length;i++) {
		if (sOrder=="") {
			sOrder = oList.options(i).value;
		}
		else {
			sOrder = sOrder + "," + oList.options(i).value;
		}
	}
	oField.value = sOrder;
	return true;
}

