var EditItem= "";
var ToolItem= "";
var EditWritePtr	= "";
var EditReadPtr= "";
var EditTable= "";

function doBold() {
	document.execCommand('bold', false, null);
  }

function doItalic() {
	document.execCommand('italic', false, null);
  }

function doUnderline() {
	document.execCommand('underline', false, null);
  }
  
function doLeft() {
  document.execCommand('justifyleft', false, null);
  }

function doCenter() {
  document.execCommand('justifycenter', false, null);
  }

function doRight() {
  document.execCommand('justifyright', false, null);
  }
  
function doForeground(Foreground) {
  document.execCommand('forecolor', false, Foreground);
  }

function doBackground(Background) {
  document.execCommand('backcolor', false, Background);
  }

function doLink() {
	var URL = prompt("Enter the URL", "");
	
	if (URL && URL.length) 
  	document.execCommand('createlink', false, URL);
  }
   
function BorderOn(Item) {
	if (!EditItem.length &&
		 (EditTable.search(';' + Item.id + ';') >= 0)) {
		Item.style.borderWidth= "1px";
		}
	}
function BorderOff(Item) {
	if (EditTable.search(';' + Item.id + ';') >= 0) {
		Item.style.borderWidth= "0px";
		}
	}
	
// Functions to control read/write operations

function SetEditDirs(WebDir, DataDir) {
	DataDirectory= DataDir + '/';
	WebDirectory= WebDir;
	}
	
function EditReadFile(Name) {		// Read the data from the file
	var	xmlhttp;
	var	message;
	var Element;
	
	if (Element= document.getElementById(Name)) {
		message= DataDirectory + Name;
	
		if (window.XMLHttpRequest) {
			xmlhttp= new XMLHttpRequest();
			}
		else {
			xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
			}
	
		xmlhttp.onreadystatechange = function() {};
		xmlhttp.open("POST", WebDirectory + "cgi/editread.cgi", false);
		xmlhttp.setRequestHeader("Content-Type", "text/xml");
		xmlhttp.setRequestHeader("Content-length", message.length);
		xmlhttp.send(message);
	
		Element.innerHTML= xmlhttp.responseText;
		}
	}

function EditWriteFile(Name) {	// Write the data to the file
	var Element;
	var	xmlhttp;
	var	message;
	
	if ((EditItem.length) &&		// Ensure we really are editing
	   (Element= document.getElementById(Name))) {
		message= DataDirectory + Name + '=' + Element.innerHTML;

		if (window.XMLHttpRequest) {
			xmlhttp= new XMLHttpRequest();
			}
		else {
			xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
			}

		xmlhttp.onreadystatechange = function(){};
		xmlhttp.open("POST", WebDirectory + "cgi/editwrite.cgi", true);
		xmlhttp.setRequestHeader("Content-Type", "text/xml");
		xmlhttp.setRequestHeader("Content-length", message.length);
		xmlhttp.send(message);
		}
	}

// Position function: Turn on the editing of the item specified

function EditOn(Item, Tools) {
	var Element;
	var	X= 0, Y= 0;
		
// Determine the position of the edit item

	if (!EditItem.length && 
		 (EditTable.search(';' + Item.id + ';') >= 0)) {
		if ((Element= Item) &&
			 Element.offsetParent) {
			do {
				X += Element.offsetLeft;
				Y += Element.offsetTop;
				} while (Element= Element.offsetParent);
			}

		if (Element= document.getElementById(Tools)) {
			ToolItem= Tools;
			Element.style.left= X;
			Element.style.top= Y - 35;
			Element.style.visibility= "visible";
			}

		Item.style.borderWidth= "0px";
		Item.contentEditable= true;
		Item.designMode="on";

		EditItem= Item.id;
		}
	}

function EditOff() {	// Turn off editing
	var Element;
	
	if (Element = document.getElementById(EditItem)) {
		Element.contentEditable= false;
		Element.designMode="off";

		if (Element= document.getElementById(ToolItem)) 
			Element.style.visibility= "hidden";

		EditItem= "";
		}
	}
// Functions called by edit areas for file-based access
function EditRead() {
	eval(EditReadPtr);

	EditOff();
	}
function EditWrite() {
	eval(EditWritePtr);
		
	EditOff();
	}

// Define the login-logoff validation (string contains X, as leader)

function Validate(Name1, Name2) {	
	var	xmlhttp;
	var	message;
	var	Info;
	
	message= Name1 + ";" + Name2;

	if (window.XMLHttpRequest) {
		xmlhttp= new XMLHttpRequest();
		}
	else {
		xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
		}

	xmlhttp.onreadystatechange = function() {};
	xmlhttp.open("POST", WebDirectory + "cgi/editorverify.cgi", false);
	xmlhttp.setRequestHeader("Content-Type", "text/xml");
	xmlhttp.setRequestHeader("Content-length", message.length);
	xmlhttp.send(message);

	Info= xmlhttp.responseText;
	if (Info.substr(0, 2) == "M;") { // Valid return string
		EditTable= Editable;
		return 1;
		}
	else if (Info.substr(0, 2) == "U;") { // Valid return string
		EditTable= Info.substr(1);
		return 1;
		}
	else {
		return 0;
		}
	}

// Define the editor login controls
 
function EditorOFF() {
	var Element;

	EditOff();

	if (Element= document.getElementById("floatlogoff")) {
		Element.style.visibility= "hidden";
		}
	EditTable= "";
	}
function EditorON() {	// called from logo
	var Element;

	if (Element= document.getElementById("floatlogin")) {
		Element.style.left= 100;
		Element.style.top= 50;
		Element.style.visibility= "visible";
		}
	}
function EditorState(Activate, Name1, Name2) {
	if (Element= document.getElementById("floatlogin")) {
		Element.style.visibility= "hidden";
		}

	if (Activate) {
		if (Validate(Name1, Name2)) {
			if (Element= document.getElementById("floatlogoff")) {
				Element.style.visibility= "visible";
				}
			}
		else {
			alert("Invalid password!");
			}
		}
	}
