
FieldObject.prototype.makeTableLine = FieldObject_MakeTableLine;
FieldObject.prototype.makeLegendTD = FieldObject_MakeLegendTD;
FieldObject.prototype.makeFormElement = FieldObject_MakeFormElement;
FieldObject.prototype.setValue = FieldObject_SetValue;
FieldObject.prototype.getValue = FieldObject_GetValue;

function FieldObject()
{
	var arr;
	if (arguments.length==1)
		arr = arguments[0];
	else
		arr = arguments;

	var reType = /^([*]?)(.*)$/;

	this.bRequired = false;
	this.sType = String(arr[0]);
	this.sName = null;
	this.sValue = null;
	this.sLegend = null;
	this.nLen1 = null;
	this.nLen2 = null;
	this.arrOptions = null;

	this.eInput = null;

	var bSelect = this.sType == "select";

	switch(arr.length)
	{
		case 6:
			this.nLen2 = arr[5]?Number(arr[5]):null;
		case 5:
			if (bSelect && arr[4])
				this.arrOptions = arr[4];
			else
				this.nLen1 = arr[4]?Number(arr[4]):null;
		case 4:
			this.sLegend = arr[3]?String(arr[3]):null;
		case 3:
			this.sValue = arr[2]?String(arr[2]):null;
		case 2:
			this.sName = arr[1]?String(arr[1]):null;
		case 1:
			this.sType = arr[0]?String(arr[0]):null;
	}
}

function FieldObject_MakeFormElement(eContainer)
{
	var el = null;
	var sTagName;
	switch(this.sType)
	{
		case "text":
		case "hidden":
		case "password":
			sTagName = "INPUT";
			break;
		case "textarea":
			sTagName = "TEXTAREA";
			break;
		case "select":
			sTagName = "SELECT";
			break;
		case "button":
			sTagName = "BUTTON";
			break;
	}

	if (sTagName)
	{
		el = document.createElement(sTagName);
		el.name = this.sName;

		switch(sTagName)
		{
			case "INPUT":
				el.type = this.sType;
				if (this.nLen1) el.size = this.nLen1;
				if (this.nLen2) el.maxlength = this.nLen2;
				if (this.sValue) el.value = this.sValue;
				break;

			case "TEXTAREA":
				if (this.nLen1) el.cols = this.nLen1;
				if (this.nLen2) el.rows = this.nLen2;
				if (this.sValue) el.appendChild(document.createTextNode(this.sValue));
				break;

			case "SELECT":
				if (eContainer)
					eContainer.appendChild(el);

				var nStop = this.arrOptions.length;
				var nValue = Number(this.sValue);
				var aOpt, bSelected;
				el.options.length = nStop;
				for (var i=0; i<nStop; i++)
				{
					aOpt = this.arrOptions[i];
					bSelected = aOpt[0] == nValue;
					el.options[i] = new Option(aOpt[1], aOpt[0], false, aOpt[0]==nValue);
				}
				break;

			case "BUTTON":
				SetupButton(el);
				el.oFieldObj = this;
				el.appendChild(document.createTextNode(this.sValue));
				break;
		}
	}

	this.eInput = el;
	eContainer.appendChild(el);

	return el;
}

function FieldObject_MakeLegendTD()
{
	var eTD = document.createElement("TD");
	if (this.sLegend)
	{
		var arrLeg = this.sLegend.split("\n");
		for (var i=0; i<arrLeg.length; i++)
		{
			if (i)
				eTD.appendChild(document.createElement("BR"));
			eTD.appendChild(document.createTextNode(arrLeg[i]));
		}
	}
	else
		eTD.appendChild(document.createTextNode(""));

	return eTD;
}

function FieldObject_MakeTableLine(oForm)
{
	var bMessage = (this.sType=="message");

	var eTR, eTD;

	this.oForm = oForm;
	eTR = this.trRow = document.createElement("TR");
	eTR.bInputType = !bMessage;

//	var eTN = document.createTextNode(bMessage?this.sValue:(this.sLegend?this.sLegend:""));
//	var eTD = document.createElement("TD");
//	eTD.appendChild(eTN);

	eTD = this.makeLegendTD();
	eTD.style.paddingBottom = "4px";

	eTR.appendChild(eTD);

	if (this.sType=="message")
	{
		eTD.colSpan = "2";
		eTR.style.display = "none"; // hide before sizing
	}
	else
	{
		eTD = document.createElement("TD");
		this.makeFormElement(eTD);
		eTR.appendChild(eTD);
	}

	return eTR;
}

function FieldObject_SetValue(val)
{
	this.sValue = val;
	var sVal = (val)?String(val):"";
	var nVal = (val)?Number(val):-1;
	if (isNaN(nVal))
		nVal = -1;

	if (this.eInput)
	{
		switch(this.sType)
		{
			case "text":
			case "hidden":
			case "password":
				this.eInput.value = sVal;
				break;
			case "textarea":
				this.eInput.firstChild.data = sVal;
				break;
			case "select":
			{
				var aO = this.eInput.options;
				var n = aO.length;
				for (var i=0; i<n; i++)
					if (aO[i].value == sVal || aO[i].text == sVal)
					{
						nVal = i;
						break;
					}

				this.eInput.selectedIndex = nVal;
				break;
			}
		}
	}
}

function FieldObject_GetValue()
{
	if (this.eInput)
	{
		switch(this.sType)
		{
			case "text":
			case "hidden":
			case "password":
				return this.eInput.value;

			case "textarea":
				if (this.eInput.hasChildNodes())
					return this.eInput.firstChild.data;
				else
					return "";

			case "select":
				return this.eInput.value;
		}
	}
	else
		return this.sValue;
}

FormObject.prototype.findField = FormObject_FindField;
FormObject.prototype.setFieldValue = FormObject_SetFieldValue;
FormObject.prototype.getFieldValue = FormObject_GetFieldValue;
FormObject.prototype.getButtonTR = FormObject_GetButtonTR;
FormObject.prototype.makeForm = FormObject_MakeForm;
FormObject.prototype.getPostData = FormObject_GetPostData;

FormObject.prototype.setSubmit = FormObject_SetSubmit;


function FormObject(arrForm, sAction, sMethod, sTarget, fCancelFunc)
{
	this.arrForm = arrForm;
	this.sAction = sAction;
	this.sMethod = "post";
	this.sTarget = null;
	this.sSubmitLabel = "Do it";
	this.fCancelFunc = null;
	this.sButtonClass = "Button";

	if (arguments.length>2 && sMethod)
		this.sMethod = sMethod;
	if (arguments.length>3 && sTarget)
		this.sTarget = sTarget;
	if (arguments.length>4 && fCancelFunc)
		this.fCancelFunc = fCancelFunc;

	this.nFields = arrForm.length;

}

function FormObject_FindField(sFieldName)
{
	for (var i=0; i<this.nFields; i++)
		if (this.arrForm[i].sName == sFieldName)
			return this.arrForm[i];

	return null;
}

function FormObject_SetFieldValue(sFieldName, vValue)
{
	var oField = this.findField(sFieldName);
	if (oField)
	{
		oField.setValue(vValue);
		return true;
	}
	else
		return false;
}

function FormObject_GetFieldValue(sFieldName)
{
	var sValue = null;
	var oField = this.findField(sFieldName);
	if (oField)
		sValue = oField.getValue();

	return sValue;
}

function FormObject_SetSubmit(sLabel, fFunc)
{
	if (sLabel)
		this.sSubmitLabel = sLabel;

	if (fFunc)
		this.fSubmitFunc = fFunc;
}

function FormObject_GetButtonTR(eForm)
{
	var eTR = document.createElement("TR");
	var eTD = document.createElement("TD");
	eTR.appendChild(eTD);

	eTD = document.createElement("TD");
	eTD.style.whiteSpace = "nowrap";

	var eButton = document.createElement("INPUT");
	SetupButton(eButton);
	eButton.type = "submit";
	eButton.value = this.sSubmitLabel;
	eTD.appendChild(eButton);

	eButton = document.createElement("INPUT");
	SetupButton(eButton);
	eButton.type = "reset";
	eButton.value = "Reset";
	eTD.appendChild(eButton);

	if (this.fCancelFunc)
	{
		eButton = document.createElement("BUTTON");
		SetupButton(eButton);
		eButton.eForm = eForm;
		eButton.appendChild(document.createTextNode("Cancel"));
		eButton.onclick = this.fCancelFunc;
		eTD.appendChild(eButton);
	}
	eTR.appendChild(eTD);

	return eTR;
}

function FormObject_MakeForm(sStartingField)
{
	var eForm = document.createElement("FORM");
	eForm.object = this;
	eForm.method = this.sMethod;
	eForm.action = this.sAction;

	if (this.sTarget)
		eForm.target = this.sTarget;

	if (this.fSubmitFunc)
		eForm.onsubmit = this.fSubmitFunc;

	var eFF = null;

	var eTBody = document.createElement("TBODY");

	var oField;
	for (var i=0; i<this.nFields; i++)
	{
		oField = this.arrForm[i];

		if (oField.sType == "hidden")
			oField.makeFormElement(eForm);
		else
		{
			var eTR = eTBody.appendChild(oField.makeTableLine(this));
			if (eFF==null)
			{
				if (sStartingField)
				{
					if (sStartingField==oField.sName)
						eFF = eTR.childNodes[1].firstChild;
				}
				else if (eTR.bInputType)
					eFF = eTR.childNodes[1].firstChild;
			}
		}
	}

	if (eTBody.hasChildNodes())
	{
		var eButtons = this.getButtonTR(eForm);
		eTBody.appendChild(eButtons);

		var eTable = document.createElement("TABLE");
		eTable.appendChild(eTBody);
		eForm.appendChild(eTable);
		eForm.eTable = eTable;

		eForm.eFirstField = eFF;
	}

	return eForm;
}

function FormObject_GetPostData()
{
	var oField;
	var sPost = "";

	for (var i=0; i<this.nFields; i++)
	{
		oField = this.arrForm[i];

		if (oField.sType != "message")
			sPost += (sPost.length>0?"&":"") + oField.sName + "=" + encodeURIComponent(oField.getValue());
	}
	return sPost;
}

