// 
// 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][, _fldDisabled][, _style] }
//
// _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.
// _fldDisabled: set to true if the field should be disabled. 
//
// _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.
//
// _style is an anonymous object containing style information, similar to that of
// a style sheet ({_color, _backgroundColor, _font, _fontSize, _fontStyle}).
//
// 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 = " ";
	var title = "";
	if (field._title != null && field._title != "") {
		title = field._title;
	} // if
	this.name = field._fldName;
	if (field._action != null) {
		for (var i = 0; i < field._action.length; i++) {
			actions += field._action[i] + " ";
		} // for
	} // if
	var disabled = (field._fldDisabled != null && field._fldDisabled ) ? " DISABLED " : "";
	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
			if (field._fldType == FieldFactory.k_multiSelect_type) {
				multi = "multiple";
			} // if
			this.stringToWrite += "<SELECT name='"+field._fldName+"' value='"+field._fldValue+"'"+actions+" "+multi+" "+size+" border='0'"+disabled+">";

			if (field._fldExtra != null) {
				for (var j = 0; j < field._fldExtra.length; j++) {
					var selected = "";
					if (field._fldType == "multiSelect") {
						for (var k = 0; k < field._fldValue.length; k++) {
							if (field._fldValue[k] == field._fldExtra[j]._text) {
								selected = "SELECTED";
								break;
							} // if
						} // for
					} // if
					else {
						selected = (field._fldExtra[j]._text == field._fldValue ? "SELECTED" : "");
					} // else
					var option = "<OPTION value='"+field._fldExtra[j]._id+"' "+selected+">"+field._fldExtra[j]._text;
					this.stringToWrite += option;
				} // for
			} // if
			this.stringToWrite += "</SELECT>";
			break;
		case "listDefinition":
			// special list definition control
			var listManager = new ListManager(field._fldName, field._fldExtra, actions, field._fldValue);
			listManager.setNotifyChange(field._notifyChange);
			this.stringToWrite += listManager.getHTML();
			break;
		case "none":
			this.stringToWrite += "<div name='"+field._fldName+"' title='"+title+"' style='color:"+field._fldExtra+"'>"+field._fldValue+"</div><nobr>";
			break;
		case "area":
		case "thinarea":
			var rows = field._fldExtra;
			var size = "";
			if (rows == null) {
				rows = 5;
			}  // if
			if (field._fldType == FieldFactory.k_thinarea_type) {
				rows = 2;
			} // if
			if (field._size != null) {
				size = "cols='"+field._size+"'";
			} // if
			this.stringToWrite += "<TEXTAREA name='"+field._fldName+"' title='"+title+"' rows='"+rows+"' "+size+" "+actions+disabled+">"+field._fldValue+"</TEXTAREA>";
			break;
		case "checkbox":
		case "singleRadio":
			// for this case, fldExtra contains the value you want to send to the server when someone selects the option.
			
			var checked = (field._fldValue == field._fldExtra) ? "CHECKED" : "";
			if (field._fldType == "checkbox") {
				checked = (field._fldValue == "X" || field._fldValue == "true") ? "CHECKED" : "";
			} // if
			var fieldType = (field._fldType == "checkbox"?"checkbox":"radio");
			var fieldValue = (field._fldType == "singleRadio"?field._fldExtra:"X");
			this.stringToWrite += "<INPUT name='"+field._fldName+"' id='"+field._fldName+"' type='"+fieldType+"' value='"+fieldValue+"' "+checked+" title='"+title+"' "+ actions+disabled+">";
			break;
		case "radio":
			if (field._fldExtra != null) {				
				for (var j = 0; j < field._fldExtra.length; j++) {
					var checked = (field._fldExtra[j]._text == field._fldValue ? "CHECKED" : "");
					this.stringToWrite += "<input name='"+field._fldName+"' type='radio' value='"+field._fldExtra[j]._id+"' title='"+title+"' "+checked+" " + actions + disabled + "><font size='2'>"+field._fldExtra[j]._text+"&nbsp;&nbsp;</font>";
				} // for
			} // if
			break;
		case "button":
			this.stringToWrite += "<button name='"+field._fldName+"' title='"+title+"' class='clsActionButton'"+actions+disabled+">"+field._fldValue+"</button>";
			break;
		case "dateEditor":
			this.stringToWrite += field._fldExtra;
			break;
		default:
			var disabled = "";
			var size = "";
			var maxlength = "";
			//this.dump("try",field);

			if (field._fldType == FieldFactory.k_readOnly_type) {
				disabled = " disabled";
			} // if
			if (field._size != null && field._size != "") {
				size = "size='"+field._size+"'";
			} // if
			if (field._maxLength != null ) {
				maxlength = "maxlength='" +field._maxLength+"'";
			} // if
			if (field._fldValue == null) {
				alert("fldValue is null "+field._fldName+" "+field._fldType);
			} // if
			var newValue;
			try {
				newValue = field._fldValue.replace(/"/g, "&quot;");
			}
			catch (e) {
				this.dump("replace failed",field);
			}
			try {
				this.stringToWrite += "<INPUT id='"+field._fldName+"' name='"+field._fldName+"' TYPE="+field._fldType+" value=\"" +newValue+ "\" title='"+title+"' "+size+" "+actions+" "+disabled+" "+maxlength+" border='0'>";
			}
			catch (e) {
				this.dump("render failed",field);
			}
			if (field._fldType == FieldFactory.k_pictureButton_type) {
				this.stringToWrite += field._pictureButton.getHTML();
			} // if
			break;
	} // switch
} // FieldFactory()


FieldFactory.prototype.getHTML = function() {
	return this.stringToWrite;
} // getHTML()


FieldFactory.prototype.disable = function() {
   
} // disable()


FieldFactory.prototype.enable = function() {
} // enable()


FieldFactory.prototype.dump = function(str, field) {
	var content = str+": \n\n";
	
	content += "Type:  "+field._fldType+"\n";
	content += "Name:  "+field._fldName+"\n";
	content += "Max Length:  "+field._maxLength+"\n";
	content += "Size:  "+field._size+"\n";
	if (field._action != null) {
		content += "Actions:  "+field._action[0]+"\n";
	} // if
	else {
		content += "Actions:  no actions\n";
	} // else
	content += "Value: "+field._fldValue;
	
	alert(content);
} // dump()


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;
				} // if
			} // for
		} // if
		else {
			selected = (pickedElementName == elemList[i]) ? SELECTED_ELEMENT : "";
		} // else
		optionList[i] = {_label: elemList[i], _value: elemList[i], _selected: selected};
	} // for
	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";
		} // if
		buttonList[i] = {_label: elemList[i], _value: elemList[i], _checked: checked};
	} // for
	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 = "area";
FieldFactory.k_check_type = "checkbox";
FieldFactory.k_radio_type = "radio";
FieldFactory.k_singleRadio_type = "singleRadio";
FieldFactory.k_thinarea_type = "thinarea";
FieldFactory.k_button_type = "button";
FieldFactory.k_pictureButton_type = "pictureButton";
FieldFactory.k_dateEditor_type = "dateEditor";
FieldFactory.k_readOnly_type = "readOnly";
FieldFactory.k_textWithChooser_type = "textWithChooser";
FieldFactory.k_none = "none";

FieldFactory.k_listDefinition_type = "listDefinition";

// choice list-specific HTML constant
var SELECTED_ELEMENT = "SELECTED";

 


