function checkBrowser()
{
var av=navigator.appVersion;
// Check if IE and if so, check version
var iMSIE=parseInt(av.indexOf("MSIE"));
if (iMSIE>=1)
{
// Get major version and write appropriate style
var iVer=parseInt(av.charAt(iMSIE+5));
if ( iVer >= 6 ) {
return 6;
}
if (iVer>=5)
{
return 5;
}
}
return 0;
}
function error( sErrorMessage )
{
alert( sErrorMessage );
}
function getSortOrder()
{
return cSortOrder;
}
function sortList( cNewSortOrder )
{
if ( cSortOrder == cNewSortOrder ) {
return;
}
cSortOrder = cNewSortOrder;
if ( cSortOrder == "" ) {
return;
}
var bAscending = ( cSortOrder == "a" ); // "a" for accending, "d" for decsending
var iLen = oSelectBox.options.length;
var iInd;
var asOptions = new Array( iLen );
var asSortedOptions = new Array( iLen );
var asValues = new Array( iLen );
var sText;
var iSelectedIndex = oSelectBox.selectedIndex;
var sSelectedText = "";
//============================================================
//deleteValue() -- deletes the value from the combo box; if the
// argument is not present or is empty, deletes the current combo
// box value.
//------------------------------------------------------------
function deleteOption( sText )
{
if ( (sText == null) || (sText == "") ) {
if ( oTextBox.value == "" ) {
return false;
}
sText = oTextBox.value;
}
var iLen = oSelectBox.options.length;
var iIndex;
var iFound = -1;
var sOptionText;
if (case_sensitive == false) {
sText = sText.toUpperCase();
}
for (iIndex=0; iIndex<iLen; iIndex++)
{
sOptionText = oSelectBox.options(iIndex).text;
// warn user if only list entries are allowed
if ( !bFound ) {
if ( options_only == true ) {
error("'" + oTextBox.value + "' is not allowed");
oTextBox.focus();
oTextBox.select();
return;
}
oSelectBox.selectedIndex = -1;
}
//============================================================
//addValue() -- adds a value to the combo box; if the argument
// is not present or is empty, adds the current combo box value.
// iInsertIndex - insertion index; if none is given:
// if list is not sorted, places the option to the end of options array.
// if list is sorted, places into appropriate position
//------------------------------------------------------------
function addOption( sText, sValue, iInsertIndex )
{
if ( (sText == null) || (sText == "") ) {
if ( oTextBox.value == "" ) {
return -1;
}
sText = oTextBox.value;
}
// Ignore insertion index if list is sorted
if ( cSortOrder != "" ) {
iInsertIndex = null;
bSort = true;
}
var iIndex;
var bExists = false;
var sOptionText, sOptionTextUpper;
var sTextUpper = sText;
if (case_sensitive == false) {
sTextUpper = sTextUpper.toUpperCase();
}
//check for duplicates and return if a duplicate exists
for (iIndex=0; iIndex<iLen; iIndex++)
{
sOptionTextUpper = sOptionText = oSelectBox.options(iIndex).text;
if (case_sensitive == false) {
sOptionTextUpper = sOptionTextUpper.toUpperCase();
}
// if list is sorted, figure out insertion point
if ( bSort ) {
if ( iInsertIndex != null ) {
continue; // insertion index already found
}
if ( cSortOrder == "a" ) {
if ( sText < sOptionText ) {
iInsertIndex = iIndex;
}
}
else {
if ( sText > sOptionText ) {
iInsertIndex = iIndex;
}
}
}
}
if ( !bExists ) {
var oOption = document.createElement("OPTION");
oOption.text = sText;
oOption.value = sValue;
if ( iInsertIndex == null ) {
oSelectBox.add(oOption ); // place at the end
oSelectBox.selectedIndex = iLen;
}
else {
oSelectBox.add(oOption, iInsertIndex);
oSelectBox.selectedIndex = iInsertIndex;
}
}
oTextBox.value = sText;
oTextBox.select();
oTextBox.focus();
// If an option is selected, select text in text control
if ( oSelectBox.selectedIndex > -1 ) {
oTextBox.focus();
oTextBox.select();
}
else if ( options_only == true ) {
oTextBox.value = sSavedText; // if only options are allowed, don't change text control
oTextBox.focus();
oTextBox.select();
}
}
function getOptionText( iIndex )
{
if ( !isInteger( iIndex ) ) {
return "";
}
yCodeComboBox.js
----------------------------------
/*
* Copyright Notice.
* This code was originally created by yCode: http://www.yCode.com
* Any modifications to this code can only be made upon purchase from
* yCode provided that this notice remains in the source code.
*
*/
document.write("<PUBLIC:EVENT NAME = 'cb_onselectionchanged' ID='event_selectionchanged'/>");
document.write("<PUBLIC:EVENT NAME = 'cb_ontextchanged' ID='event_textchanged'/>");
document.write("<PUBLIC:EVENT NAME = 'cb_onkeyup' ID='event_textkeyup'/>");
document.write("<PUBLIC:EVENT NAME = 'cb_onkeydown' ID='event_textkeydown'/>");
document.write("<PUBLIC:EVENT NAME = 'cb_onkeypress' ID='event_textkeypress'/>");
function initComboBox() {
var iBrowserVersion = checkBrowser();
if ( iBrowserVersion == 0 ) {
alert("This page may not be displayed properly:\n\
This product requires Microsoft Internet Explorer 5 or later browser only.");
}
if ( iBrowserVersion == 6 ) {
oSelectBox.style.setExpression("clip","'rect(auto auto auto '+(offsetWidth - 18)+')'");
}
else {
oSelectBox.style.setExpression("clip","'rect(auto auto auto '+(offsetWidth - 20)+')'");
}
/*
* Do not sort list for performance reasons; options should be provided in appropriate order
*
if ( element.sort_order != "" ) {
sortList();
}
*/
// If element is intialized with options, insert appropriate text into edit box
var iSelected = oSelectBox.selectedIndex;
if ( iSelected > -1 ) {
oTextBox.value = oSelectBox.options[ iSelected ].text;
fireTextChange();
}
if ( ( currentStyle.visibility == "visible" ) && ( currentSTyle.display != "none" ) ) { // if control is visible
oTextBox.select();
oTextBox.focus();
}
combobox.htc
----------------------------------
/*
* Copyright Notice.
* This code was originally created by yCode: http://www.yCode.com
* Any modifications to this code can only be made upon purchase from
* yCode provided that this notice remains in the source code.
*
*/
<META NAME="description" CONTENT="yCode Combo Box v2.0 acts and
feels like a regular windows combo box where you can enter a value directly
or select one from a drop down list. The Combo Box is implemented as an HTC component
in JavaScript and can be easily included in any html page.">
<font face="Helvetica">
<p>
<b>yCode Combo Box</b> v2.0 provides functionality similar to a Windows combo-box.
Combo-box consists of a SELECT box combined with INPUT TEXT control. The SELECT box portion
of the control drops down when the user clicks the drop-down arrow next to the control.
Currently selected item (if any) in the SELECT box is displayed in the TEXT control.
In addition, the user can enter text directly into the TEXT control.
<br><br>
The Combo Box is implemented as an HTC component in JavaScript and can be easily
included in any html page.
</p>
<p>
There are many situations where a combo box is useful. One example is
entering an email address: you may select one from the SELECT box
or just type one in.
</p>
<p>
So, give it a try! (<b>Note:</b> it can run on <b>IE5.5, 6.0,</b> and later browsers only.)
</p>
<p>
Please enter email address:
<script language = JScript.Encode>
document.write("<yCode:COMBOBOX ID='my_combobox' name='my_combobox'>");
document.write("<option value='info@mycompany.com'>info@mycompany.com</option>");
document.write("<option value='support@mycompany.com'>support@mycompany.com</option>");
document.write("<option value='sales@mycompany.com'>sales@mycompany.com</option>");
document.write("</yCode:COMBOBOX>");
</script>
<input ID="btn_retreive" type="button" name="retrieve" value="Retrieve Selection"
onclick="alert('Combo Box value is \'' + my_combobox.getText() + '\'');">
</p>
Programmer controls:
<ul>
<li>Whether entries in the SELECT box are sorted</li>
<li>Whether values not existing in the SELECT box are allowed to be typed in</li>
<li>Whether values that already exist in the SELECT box are looked up as the user types data in</li>
<li>Whether above options are case-sensitive</li>
<li>Length of user's input</li>
<li>Styles, such as text color, backround color, font, and more</li>
<li>Values can be added to or deleted from the Combo Box</li>
<li>Both the selected text string and a corresponding SELECT box value can be retrieved and/or posted by a form</li>
</ul>
Please follow the link to view a more <a href="combobox_test.htm"><b>Advanced Example</b><a>. It demonstrates all
combo-box functions and properties in action.
</p>
<p>
Other functionality can be added upon request. Please contact
<a href="mailto:sales@yCode.com">sales@yCode.com</a> for more information.
</p>
<p>
<b>Note:</b> the source code for this page is <a href="http:\\www.yCode.com/yCode/copyright.html">copyrighted<a>.
</p>
<p>
Please follow the link to
<a href="http:\\www.yCode.com\ComboBox\buy_combo_frameset.htm" target="_top"><b>BUY</b></a>
the source code and intranet license for $25 (USD)<br>
(Purchase securely through <a href="http:\\www.ibill.com" target="_blank">ibill</a>, our authorized sales agent).
</p>
</font>