home *** CD-ROM | disk | FTP | other *** search
- /* ======================================================================
- OBJECT: com_netobjects_SelectBox
-
- PLATFORMS: Netscape Navigator 4.01 or higher,
- Microsoft Internet Explorer 4.02 or higher
-
- DESC: This object allows developers to create SelectBoxes with common built
- in behaviors.
- ====================================================================== */
- function com_netobjects_SelectBox(params) {
- // Public Properties
- this.name = (params.name != null) ? params.name : "";
- this.selectBoxName = (params.selectBoxName != null) ? params.selectBoxName : "";
- this.formName = (params.formName != null) ? params.formName : "";
- this.size = (params.size != null) ? params.size : "";
- this.multiple = (params.multiple == true) ? params.multiple : false;
-
- this.valuesListStr = params.valuesListStr;
- this.labelsListStr = params.labelsListStr;
- this.selectedStr = params.selectedStr;
-
- this.validate = ((params.validate != null) && (params.validate == true)) ? params.validate : "";
- this.isRequired = (params.isRequired != null) ? params.isRequired : "";
- this.state = (params.state != null) ? params.state : "";
-
- this.testMode = (params.testMode != null) ? params.testMode : "none"
-
- // Private Properties
- this.valuesList = this.valuesListStr.split(";");//unEncodeList(this.valuesListStr);
- this.labelsList = this.labelsListStr.split(";");//unEncodeList(this.labelsListStr);
- this.selectedList = this.selectedStr.split(";");//unEncodeList(this.selectedStr);
-
- this.isValid = true;
- this.errType = null;
- this.selectBoxIndex = 0
-
- // Public Methods
- this.validateValue = _SelectBox_validateValue;
- this.render = _SelectBox_render;
- this.getSelected = _SelectBox_getSelected;
- this.setSelected = _SelectBox_setSelected
-
- // Private Methods
- this.validateProperties = _SelectBox_validateProperties;
- this.isElement = _SelectBox_isElement;
- this.ctlCnt = _SelectBox_CtlCnt;
-
- // Public Events
- this.onBlur = _SelectBox_onBlur;
- this.onChange = _SelectBox_onChange;
- this.onFocus = _SelectBox_onFocus;
- this.onClick = _SelectBox_onClick;
-
- this.onValidate = _SelectBox_onValidate;
- this.onInvalid = _SelectBox_onInvalid;
- this.onValid = _SelectBox_onValid;
- this.onRender = _SelectBox_onRender;
-
- // Private Events (internal event driven functionality)
- this.eOnChange = _SelectBox_eOnChange;
-
- // Validate object properties
- this.validateProperties();
-
- // Public Methods ----------------------------------------
- function _SelectBox_render() {
- // Initialize Variables
- var sSelectBoxName = "";
- var svalue = "";
- var ssize = "";
- var smultiple = "";
- var label, value;
-
- var sonBlur = "";
- var sonChange = "";
- var sonFocus = "";
- var sonSelect = "";
- var sonClick = "";
-
- // Set Variables
- sSelectBoxName = " Name = \"" + this.selectBoxName + "\""
- if (this.size != "") {ssize = " Size = \"" + this.size + "\""}
- if (this.multiple) {smultiple = " MULTIPLE "}
- svalue = " VALUE=\"" + this.selectedStr + "\"";
-
- // Build Nav & IE shared link events
- sonBlur = " onBlur=\"" + this.name + ".onBlur();\""
- sonChange = " onchange=\"return " + this.name + ".eOnChange();\""
- sonClick = " onclick=\"" + this.name + ".onClick();\""
- sonFocus = " onFocus=\"" + this.name + ".onFocus();\""
- eEvents = sonBlur + sonChange + sonFocus + sonClick;
-
- // Write HTML
- if (this.state == "hidden") {
- document.write( "<Input Type=hidden " + sSelectBoxName + svalue + eEvents + ">");
- } else if (this.state == "read-only") {
- document.write( this.selectedStr + "\n");
- } else {
- document.write( "<SELECT" + sSelectBoxName + svalue + ssize + smultiple + eEvents + ">");
- if (this.valuesList != null) {
- var max = Math.max(this.valuesList.length, this.labelsList.length);
- for (var i=0; i < max; i++) {
- label = this.labelsList[i];
- value = this.valuesList[i];
- // If a label is null, set it to the value
- if (this.labelsList[i] == null && this.valuesList[i] != null)
- label = this.valuesList[i];
- // If a value is null, set it to the label
- else if (this.valuesList[i] == null && this.labelsList[i] != null)
- value = this.labelsList[i];
- document.write( "<OPTION VALUE=\"" + value +"\">" + label + "");
- }
- document.write( "</SELECT>\n");
-
- this.setSelected(this.selectedStr);
- }
- }
-
- if (this.state != "read-only") {
- // Set control index if multiple controls exist
- this.selectBoxIndex = this.ctlCnt(this.selectBoxName)-1
- if (isNaN(this.selectBoxIndex)) {this.selectBoxIndex=0}
- }
-
- // Fire public events
- this.onRender();
-
- } // END _SelectBox_render
-
-
- /* ======================================================================
-
- METHOD: _SelectBox_validateProperties. If the test mode is set,
- generate error and/or warning messages if required or recommended properties
- are set with invalid property values or are not set at all.
-
- ====================================================================== */
- function _SelectBox_validateProperties() {
- if ((this.testMode.toLowerCase() == "text") || (this.testMode.toLowerCase() == "alerts")) {
- var errors = new Array()
- iIndex = 0;
-
- // Check for errors
- if (this.name == "")
- errors[iIndex++] = "Name is a required property.";
- if (this.SelectBoxName == "")
- errors[iIndex++] = "SelectBox name is a required property.";
- if (this.SelectBoxName == this.name)
- errors[iIndex++] = "SelectBox name and object name may not be the same.";
- if (this.valuesList == null)
- errors[iIndex++] = "No values have been specified for the select box.";
- if (this.labelsList == null)
- errors[iIndex++] = "No labels have been specified for the select box.";
- if ((this.valuesList != null) && (this.labelsList != null) && (this.valuesList.length != this.labelsList.length))
- errors[iIndex++] = "The values list and label list do not have the same number of elements.";
-
- // Display errors
- for (var i=0; i<errors.length; i++) {
- if (this.testMode.toLowerCase() == "text")
- document.write( "<BR><HR><B><FONT COLOR=RED>TEST MODE, com_netobjects_SelectBox, '" + this.name + "' : </FONT> " + errors[i] + "</B><HR>\n");
- else
- alert("TEST MODE, com_netobjects_SelectBox, '" + this.name + "' : " + errors[i]);
- }
- }
- } // END _SelectBox_validateProperties
-
- /* ======================================================================
-
- METHOD: _SelectBox_validateValue.
-
- ====================================================================== */
- function _SelectBox_validateValue() {
- this.isValid = true;
- this.errType = ""
-
- // Check for required value
- if ((this.isRequired) && ((this.getSelected() == null) || (this.getSelected() == ""))) {
- this.errType = "NoValue"
- this.isValid = false;
- // exit function
- }
-
- // Fire public events
- if (this.isValid) {this.onValid()} else {this.onInvalid()};
- this.onValidate()
-
- return this.isValid;
-
- } // END _SelectBox_validate
-
- /* ======================================================================
-
- METHOD: _SelectBox_getSelected
-
- ====================================================================== */
- function _SelectBox_getSelected() {
- var result = "";
- var isSel;
- var index = "";
-
- if (this.state != "read-only") {
-
- // Only attempt to index them by array if there's more than one with the same name.
- // Otherwise, it will cause an error
- if (this.ctlCnt(this.selectBoxName) > 1) {
- index = "[" + this.selectBoxIndex + "]"
- }
-
- if (this.state == "editable") {
- var itmCnt = eval("window.document." + this.formName + "." + this.selectBoxName + index + ".options.length")
-
- for (var i=0; i < itmCnt; i++) {
- isSel = eval("window.document." + this.formName + "." + this.selectBoxName + index + ".options["+ i +"].selected")
- if (isSel) {
- if (result.length > 0) {
- result = result + ";"
- }
- result = result + eval("window.document." + this.formName + "." + this.selectBoxName + index + ".options["+i+"].value")
- }
- } // end for loop
- } else if (this.state == "hidden")
- result = eval("window.document." + this.formName + "." + this.selectBoxName + index + ".value")
- } else
- result = this.selectedStr;
-
- return result;
-
- } // END _SelectBox_getSelected
-
-
- /* ======================================================================
-
- METHOD: _SelectBox_setSelected
-
- ====================================================================== */
- function _SelectBox_setSelected(newVal) {
- var result = ""
- var isSel
- var index = ""
-
- if (this.state != "read-only") {
- this.selectedStr = newVal
- if (this.ctlCnt(this.selectBoxName) > 1) {
- index = "[" + this.selectBoxIndex + "]"
- }
- if (this.state == "editable") {
- var itmCnt = eval("window.document." + this.formName + "." + this.selectBoxName + index + ".options.length")
- var setVal = false;
- for (var i=0; i < itmCnt; i++) {
- eval("window.document." + this.formName + "." + this.selectBoxName + index + ".options["+i+"].selected = " + this.isElement(this.selectedStr, this.valuesList[i]))
- }
- } else if (this.state == "hidden")
- eval("window.document." + this.formName + "." + this.selectBoxName + index + ".value='" + newVal + "'");
- }
- return result;
-
- } // END _SelectBox_setSelected
-
-
- /* ======================================================================
-
- METHOD: _SelectBox_isElement, looks for string in ; delimited string
-
- ====================================================================== */
- function _SelectBox_isElement(myStr, val) {
- var result = false
- var myArray = myStr.split(";");
-
- for (var i=0; i < myArray.length; i++)
- if (myArray[i] == val) { result = true}
- return result;
-
- } // END _SelectBox_isElement
-
-
- /* ======================================================================
-
- METHOD: _SelectBox_CtlCnt. Counts number of elements with selected name.
-
- ====================================================================== */
- function _SelectBox_CtlCnt(ctlName) {
-
- var result = 0
- var itmCnt = eval("window.document." + this.formName + ".elements.length")
-
- for (var i=0; i < itmCnt; i++) {
- if (ctlName == eval("window.document." + this.formName + ".elements["+ i +"].name")) {
- result = result + 1
- }
- }
-
- return result
-
- } // END _SelectBox_CtlCnt
-
-
- // Private Event Handlers ----------------------------------------
-
- /* ======================================================================
-
- METHOD: _SelectBox_eOnChange. For the selected state, display the
- appropriate SelectBox if available.
-
- ====================================================================== */
- function _SelectBox_eOnChange() {
- this.selectedList = this.getSelected();
- var result = true
- if (this.validate) {
- result = this.validateValue()
- }
- this.onChange()
-
- return result
-
- } // END _SelectBox_eOnChange
-
-
- // Public Event Handlers ----------------------------------------
- function _SelectBox_onBlur() {
- }
- function _SelectBox_onChange() {
- }
- function _SelectBox_onFocus() {
- }
- function _SelectBox_onValidate() {
- }
- function _SelectBox_onInvalid() {
- // Error messages are put here so that they may be overridden
- if (this.errType == "NoValue") {
- alert("An entry in this field is REQUIRED.")
- } else {
- alert("Unknown Error.")
- }
- }
- function _SelectBox_onValid() {
- }
- function _SelectBox_onRender() {
- }
- function _SelectBox_onClick() {
- }
-
- } // END CONSTRUCTOR com_netobjects_SelectBox
-
-
-