// 
// File:	FieldFactory.js
// Author:	Rob Allen
// Created:	January 15, 2002
// 
// Purpose: A factory for creating simple DHTML controls.
//
// Copyright (c) 2002, ThoughtByDesign. All rights reserved.
//
// Usage:
// 	var sc = new FieldFactory(field);
// 	field ::= { _fldName, _fldType, _fldValue[, _title][, _action][, _fldExtra][, _size] }
//
// _action: an array of zero or more arbitrary event handlers to associate with the field.
// _fldName: the programatic name for the field. The name is submitted as the key of the key value pair to the server.
// _fldType: one of the pre-defined field type constants listed within this class.
// _fldValue: a default field value. It is only valid for choice lists and text fields.
// _size: the width of text field.
// _title: a snippet of hover help text.
//
// _fldExtra is context dependent. For a select_type it contains the choices 
// to be placed into the choice list. For a radio_type, it contains the labels
// for each radio button in the group.
//
// This computer program is protected by copyright law and international treaties.
// Unauthorized reproduction or distribution of this program, or any portion of it,
// may result in severe civil and criminal penalties, and will be prosecuted to the
// maximum extent possible under the law.
//
// Modification history:
//


function FieldFactory(field) {
	var selectOptions;
	this.stringToWrite = String("");
	var actions = " ";
	this.name = field._fldName;
	if (field._action != null) {
		for (var i = 0; i < field._action.length; i++) {
			actions += field._action[i] + " ";
		}
	}
	
	if (field._title == null) {
		field._title = "";
	}
	
	switch (field._fldType) {
		case "select":
		case "multiSelect":
			// for choice lists, _fldValue may be an array or a simple variable.
			var multi = "";
			var size = "";
			if (field._size != null) {
				size = "size='"+field._size+"'";
			}
			if (field._fldType == FieldFactory.k_multiSelect_type) {
				multi = "multiple";
			}
			this.stringToWrite += "<SELECT name='"+field._fldName+"' value='"+field._fldValue+"'"+actions+" "+multi+" "+size+" border='0'>";

			if (field._fldExtra != null) {
				selectOptions = FieldFactory.arrayToSelectOptions(field._fldExtra, field._fldValue); 
				for (var j = 0; j < field._fldExtra.length; j++) {
					var option = "<OPTION value='"+selectOptions[j]._value+"' "+selectOptions[j]._selected+">"+selectOptions[j]._label;
					this.stringToWrite += option;
				} 
			}
			this.stringToWrite += "</SELECT>";
			break;
		case "none":
			this.stringToWrite += "<div name='"+field._fldName+"' title='"+field._title+"' style='color:"+field._fldExtra+"'>"+field._fldValue+"</div><nobr>";
			break;
		case "area":
		case "thinarea":
			var rows = field._fldExtra;
			if (rows == null)
				rows = 5;
			if (field._fldType == FieldFactory.k_thinarea_type) {
				rows = 2;
			} 
			this.stringToWrite += "<TEXTAREA name='"+field._fldName+"' class='clsFieldLabel' title='"+field._title+"' rows='"+rows+"' "+actions+">"+field._fldValue+"</TEXTAREA>";
			break;
		case "check":
			var checked = (field._fldValue == "X") ? "CHECKED" : "";
			this.stringToWrite += "<INPUT name='"+field._fldName+"' id='"+field._fldName+"' type='"+field._fldType+"' value='X' "+checked+" title='"+field._title+"' "+ actions+">";
			break;
		case "radio":
			if (field._fldItems != null) {
				var disabled = (field._fldDisabled != null && field._fldDisabled ) ? " DISABLED " : "";
				buttonList = SimpleControl.arrayToCheckButtons(field._fldItems, field._fldValue);
				for (var j = 0; j < field._fldItems.length; j++) {
					this.stringToWrite += "<input name='"+field._fldName+"' type='radio' value='"+buttonList[j]._value+"' title='"+field._title+"' "+buttonList[j]._checked+" " + actions + disabled + "><font size='2'>"+buttonList[j]._label+"&nbsp;&nbsp;</font>";
				} 
			}
			break;
		case "button":
			this.stringToWrite += "<button name='"+field._fldName+"' title='"+field._title+"' class='clsSmallButton'"+actions+">"+field._fldValue+"</button>";
			break;
		default:
			var disabled = "";
			var size = "";
			var maxlength = "";

			if (field._fldType == FieldFactory.k_readOnly_type) {
				disabled = " disabled";
			}
			if (field._size != null) {
				size = "size='"+field._size+"'";
			}
			if (field._maxLength != null ) {
				maxlength = "maxlength='" +field._maxLength+"'";
			}
			var newValue = field._fldValue.replace(/"/g, "&quot;")
			this.stringToWrite += "<INPUT name='"+field._fldName+"' TYPE="+field._fldType+" value=\"" +newValue+ "\" title='"+field._title+"' "+size+" "+actions+" "+disabled+" "+maxlength+" border='0'>";			
			break;
	} // switch
} // FieldFactory()


FieldFactory.prototype.getHTML = function() {
	return this.stringToWrite;
} // getHTML()


FieldFactory.prototype.disable = function() {
   
} // disable()


FieldFactory.prototype.enable = function() {
} // enable()


FieldFactory.arrayToSelectOptions = function(elemList, pickedElementName) {
	var optionList = new Array();

	for (var i = 0; i < elemList.length; i++) {
		var selected = "";
		if (typeof pickedElementName == "object") {
			var done = false;
			for (var j = 0; j < pickedElementName.length && done == false; j++) {
				if (pickedElementName[j] == elemList[i]) {
					selected = SELECTED_ELEMENT;
					done = true;
				}
			}
		}
		else {
			selected = (pickedElementName == elemList[i]) ? SELECTED_ELEMENT : "";
		}
		optionList[i] = {_label: elemList[i], _value: elemList[i], _selected: selected};
	}
	return optionList;
} // FieldFactory.arrayToSelectOptions


FieldFactory.arrayToCheckButtons = function(elemList, pickedElementName) {
	var buttonList = new Array();
   
	for (var i = 0; i < elemList.length; i++) {
		var checked = "";
		if (pickedElementName == elemList[i]) {
			checked = "CHECKED";
		}
		buttonList[i] = {_label: elemList[i], _value: elemList[i], _checked: checked};
	}
	return buttonList;
} // FieldFactory.arrayToCheckButtons


// field type constants
FieldFactory.k_text_type = "text";
FieldFactory.k_file_type = "file";
FieldFactory.k_select_type = "select";
FieldFactory.k_multiSelect_type = "multiSelect";
FieldFactory.k_area_type = "textarea";
FieldFactory.k_check_type = "checkbox";
FieldFactory.k_radio_type = "radio";
FieldFactory.k_thinarea_type = "thinarea";
FieldFactory.k_button_type = "button";
FieldFactory.k_readOnly_type = "readOnly";
FieldFactory.k_none = "none";

// choice list-specific HTML constant
var SELECTED_ELEMENT = "SELECTED";



