// JavaScript Document

function save() {
	edtForm.inpContent.value = oEdt.getHTMLBody();
	edtForm.submit();
}

function cancel(sloc) {
	if (updated()) if (!confirm("Discard your changes, are you sure?")) return true;
	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 renameSection() {
	if (sectionValid()) {
		var newCat
		newCat = window.prompt("Rename '" + newsection.sectname.value + "' to:", newsection.sectname.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) + aInvalid.join(" "));
					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
	newCat = window.prompt("New Section Name", "");
	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) + aInvalid.join(" "));
				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) {
//		alert("HTML Body has changed");
		return true;
	}
	if (edtForm.pagetitle.value!=edtForm.origtitle.value) {
//		alert("Page Title has changed" + String.fromCharCode(13) + "Orig Title='" + edtForm.origtitle.value + "'" + String.fromCharCode(13) + "New Title='" + edtForm.pagetitle.value + "'");
		return true;
	}
	if (edtForm.pagedesc.value!=edtForm.origdesc.value) {
//		alert("Page Desccription has changed" + String.fromCharCode(13) + "Orig Desc='" + edtForm.origdesc.value + "'" + String.fromCharCode(13) + "New Desc='" + edtForm.pagedesc.value + "'");
		return true;
	}
	if (edtForm.keywords.value!=edtForm.origkeyw.value) {
//		alert("Keywords have changed" + String.fromCharCode(13) + "Orig Keyw='" + edtForm.origkeyw.value + "'" + String.fromCharCode(13) + "New Keyw='" + edtForm.keywords.value + "'");
		return true;
	}
	return false;
}


