function Actions(id) {
	this.id = id;
	this.actionsElem = document.getElementById(id);
	Actions.all[id] = this;
	this.links = [];
	this.hotKeys = [];
	this.inputs = [];
}

Actions.all = [];

Actions.get = function(id) {
	return Actions.all[id];
};

Actions.prototype.clearActions = function() {
	while (this.actionsElem.lastChild) {
		this.clearLastAction();
	}
	this.links = [];
	this.hotKeys = [];
	this.inputs = [];
};

Actions.prototype.clearLastAction = function() {
	try {
		if (!this.actionsElem || !this.actionsElem.lastChild) {
			return;
		}
		if (this.actionsElem.lastChild.firstChild
				&& this.actionsElem.lastChild.firstChild.nodeName
				&& this.actionsElem.lastChild.firstChild.nodeName.toLowerCase() == "a") {
			this.links.length--;
			this.hotKeys.length--;
		} else 	if (this.actionsElem.lastChild.lastChild
				&& this.actionsElem.lastChild.lastChild.nodeName
				&& this.actionsElem.lastChild.lastChild.nodeName.toLowerCase() == "form") {
			this.inputs.length--;
		}
		this.actionsElem.removeChild(this.actionsElem.lastChild);
	} catch (eep) {
		debug(eep);
		throw(eep);
	}
};

Actions.prototype.getQueryString = function() {
	var i;
	var queryString = "";
	for (i=0; i<this.inputs.length; ++i) {
		if (i) queryString += "&";
		queryString += this.inputs[i].name
			+ "=" + encodeURIComponent(this.inputs[i].value);
	}
	return queryString;
};

Actions.prototype.addLabel = function(label, classes) {
	if (!label) return;
	var labelP = document.createElement("p");
	if (classes && classes.labelClass)
		labelP.className = classes.labelClass;
	labelP.innerHTML = label;
	this.actionsElem.appendChild(labelP);
};

Actions.prototype.addLabelNode = function(labelNode, classes) {
	if (!labelNode) return;
	var labelP = document.createElement("p");
	if (classes && classes.labelClass)
		labelP.className = classes.labelClass;
	labelP.appendChild(labelNode);
	this.actionsElem.appendChild(labelP);
};

Actions.prototype.addLink = function(label, link, hotKey, classes) {
	if (!link) return;
	if (!label) label = link;
	var linkPElem = document.createElement("p");
	if (classes && classes.pClass)
		linkPElem.className = classes.pClass;
	var linkElem = document.createElement("a");
	linkElem.setAttribute("href", link);
	if (classes && classes.linkClass)
		linkElem.className = classes.linkClass;

	hotKey = this.getHotKey(label, hotKey);
	this.hotKeys[this.hotKeys.length] = hotKey;
	this.links[this.links.length] = link;
	var labelSpanElem = document.createElement("span");
	if (classes && classes.spanClass)
		labelSpanElem.className = classes.spanClass;
	var labelTextNodes = this.underlineHotKey(label, hotKey);
	var i;
	var numTextNodes = labelTextNodes.length;
	for (i=0; i<numTextNodes; ++i) {
		labelSpanElem.appendChild(labelTextNodes[i]);
	}
	linkElem.appendChild(labelSpanElem);
	linkPElem.appendChild(linkElem);
	this.actionsElem.appendChild(linkPElem);
};

Actions.prototype.addInput = function(id, name, label, type, classes, value) {
	if (!name) name = id;
	if (!name) return;
	if (!label) label = name;
	if (!type) type = "text";
	if (!value) value = "";
	var labelP = document.createElement("p");
	if (classes && classes.pClass)
		labelP.className = classes.pClass;
	var labelSpan = document.createElement("span");
	var labelTextNode = document.createTextNode(label);
	var inputElem = document.createElement("input");
	if (id) inputElem.setAttribute("id", id);
	inputElem.setAttribute("type", type);
	inputElem.setAttribute("name", name);
	inputElem.setAttribute("value", value);
	var formElem = document.createElement("form");
	formElem.onsubmit = function() {return false;};
	if (classes && classes.formClass)
		formElem.className = classes.formClass;

	formElem.appendChild(inputElem);
	labelSpan.appendChild(labelTextNode);
	labelP.appendChild(labelSpan);
	labelP.appendChild(formElem);
	this.actionsElem.appendChild(labelP);
	this.inputs[this.inputs.length] = inputElem;
};

Actions.prototype.addRule = function(classes) {
	var hrElem = document.createElement("hr");
	if (classes && classes.hrClass)
		hrElem.className = classes.hrClass;
	this.actionsElem.appendChild(hrElem);
};

Actions.prototype.getHotKey = function(label, suggestedHotKey) {
	if (suggestedHotKey) {
		suggestedHotKey = suggestedHotKey.toLowerCase();
		if (!this.usesHotKey(suggestedHotKey))
			return suggestedHotKey;
	}
	var i;
	for (i=0; i<label.length; ++i) {
		var c = label.substring(i, i+1).toLowerCase();
		if (!this.usesHotKey(c))
			return c;
	}
	return "";
};

Actions.prototype.usesHotKey = function(proposedHotKey) {
	var numHotKeys = this.hotKeys.length;
	var i;
	for (i=0; i<numHotKeys; ++i) {
		if (this.hotKeys[i] == proposedHotKey) {
			return true;
		}
	}
	return false;
};

Actions.prototype.underlineHotKey = function(label, hotKey) {
	var segments = [];
	if (!hotKey) {
		segments[0] = document.createTextNode(label);
		return segments;
	}
	var lowerLabel = label.toLowerCase();
	var hotKeyPos = lowerLabel.indexOf(hotKey);
	if (hotKeyPos < 0) {
		segments[0] = document.createTextNode(label);
		return segments;
	}
	if (hotKeyPos > 0) {
		segments[0] =
			document.createTextNode(label.substring(0,hotKeyPos));
	}
	var midSpanElem = document.createElement("span");
	midSpanElem.style.textDecoration = "underline";
	var midText = label.substring(hotKeyPos, hotKeyPos + 1);
	var hotKeyTextNode =
		document.createTextNode(midText);
	midSpanElem.appendChild(hotKeyTextNode);
	segments[segments.length] = midSpanElem;
	if (hotKeyPos < label.length - 1) {
		segments[segments.length] =
			document.createTextNode(label.substring(hotKeyPos + 1));
	}
	return segments;
};

Actions.prototype.processKeyPress = function(e) {
	var hotKey;
	var target;
	var altKey;
	var ctrlKey;
	if (window.event != null) {
		hotKey = String.fromCharCode(window.event.keyCode).toLowerCase();
		altKey = window.event.altKey;
		ctrlKey = window.event.ctrlKey;
	}else{
		hotKey = String.fromCharCode(e.charCode).toLowerCase();
		altKey = e.altKey;
		ctrlKey = e.ctrlKey;
	}
	if (window.event != null)
		target = window.event.srcElement;
	else
		target = e.originalTarget;
	if (target.nodeName.toLowerCase() == 'input' || target.nodeName.toLowerCase() == 'text' || altKey || ctrlKey){
	}else{
		var hotLink = this.getLinkForHotKey(hotKey);
		if (hotLink) {
			if (hotLink.toLowerCase().indexOf("javascript:") > -1) {
				var jsHotLink = hotLink.substring("javascript:".length);
				if (jsHotLink) {
					eval(jsHotLink);
				}
			} else {
				window.location = hotLink;
			}
		}
		return false;
	}
};

Actions.prototype.getLinkForHotKey = function(hotKey) {
	if (!hotKey) return false;
	var i;
	var numHotKeys = this.hotKeys.length;
	for (i=0; i<numHotKeys; ++i) {
		if (hotKey == this.hotKeys[i])
			return this.links[i];
	}
	return false;
};

Actions.onKeyPress = function(e){
	for (var id in Actions.all) {
		var actionSet = Actions.get(id);
		if (actionSet.processKeyPress)
			actionSet.processKeyPress(e);
	}
};

document.onkeypress=Actions.onKeyPress;
