var Form = function(id) {
	
	this.id = id;
	this.id_prefix = "__FormField_" + this.id + "_";
	
}

Form.instances = new Array();

Form.get = function(id) {
	if (!Form.instances[id]) {
		Form.instances[id] = new Form(id);
	}
	return Form.instances[id];
}

Form.prototype.getFieldValue = function(field) {
	return this.getFieldNode(field).value;
}

Form.prototype.getFieldId = function(field) {
	return this.id_prefix + field;
}

Form.prototype.getFieldNode = function(field) {
	return $(this.getFieldId(field));
}

Form.prototype.getFieldNodeRow = function(field) {
	return $(this.getFieldId(field) + "_Row");
}

Form.prototype.getFieldNodeLabel = function(field) {
	return $(this.getFieldId(field) + "_LabelNode");
}

Form.prototype.setFieldVisible = function(field, visible) {
	this.getFieldNodeRow(field).style.display = visible ? "" : "none";
}

Form.prototype.setFieldRequired = function(field, required) {
	var node = this.getFieldNodeLabel(field);
	var label = node.innerHTML;
	if (label.substr(-1) === '*') {
		label = label.substr(0, label.length - 1);
	}
	node.innerHTML = label + (required ? "*" : ""); 
}

Form.prototype.setFieldHelpVisible = function(field, visible) {
	var node = $("node_form_help");
	if (!node) {
		node = document.body.appendChild(document.createElement("DIV"));		
		node.className = "form_field_help";
		node.id = "node_form_help";
		node.style.maxWidth = "400px";
	}
	node.style.display = "none";
	if (!visible) {
		return false;
	}
	var label = this.getFieldNodeLabel(field);
	var position = label.cumulativeOffset();
	node.innerHTML = label.__help;
	node.style.display = "";
	node.style.top = (position.top + label.getHeight() + 6) + "px";
	node.style.left = position.left + "px";
}

Form.prototype.setFieldHelp = function(field, help) {
	var label = this.getFieldNodeLabel(field);
	label.__parent = this;
	label.__field = field;
	label.__help = help;
	label.onmouseover = function() {
		this.__parent.setFieldHelpVisible(this.__field, true);
	}
	label.onmouseout = function() {
		this.__parent.setFieldHelpVisible(this.__field, false);
	}
}