function writeSelectBox(matrix, id, size, onchange, css, disabled, buttononly, buttontext, multiple, popselect, sortorder, combo, classname) {
	var ie4 = (document.all != null);

	if (ie4) {
		document.write(createIEString(matrix, id, size, onchange, css, disabled, buttononly, buttontext, multiple, popselect, sortorder, combo, classname));
	}
	else {
		document.write(createXString(matrix, id, size, onchange, css, disabled, buttononly, buttontext, multiple, popselect, sortorder, combo, classname));
	}
}

function writeSelectBoxNormal(matrix, id, size, onchange, css, disabled, buttononly, buttontext, multiple, popselect, sortorder, combo, classname) {
	document.write(createXString(matrix, id, size, onchange, css, disabled, buttononly, buttontext, multiple, popselect, sortorder, combo, classname));
}

function createIEString(matrix, id, size, onchange, css, disabled, buttononly, buttontext, multiple, popselect, sortorder, combo, classname) {
	var str = "";
	var showinput, showtable, tableclass, buttonclass;

	// Set disabled, if not passed in
	if (disabled == null)
		disabled = "";
		
	// Set buttononly, if not passed in
	if (buttononly == null)
		buttononly = false;
		
	// Set buttontext, if not passed in
	if (buttontext == null)
		buttontext = "";
		
	// Set multiple, if not passed in
	if (multiple == null)
		multiple = false;

	// Set popselect, if not passed in
	if (popselect == null)
		popselect = false;
		
	// Set sortorder, if not passed in (can be: asc, desc or empty)
	if (sortorder == null)
		sortorder = "";
		
	// Set combo, if not passed in
	if (combo == null)
		combo = false;
		
	// if popselect, create a display div
	if (popselect) {
		if (multiple) {
			str += '<div ' + disabled + ' style="position: relative; visibility: visible; cursor: hand; background: white; border: 1 solid black;" onclick="displayClick(\'' + id + '\');" onselectstart="return false;"';
		}
		else {
			str += '<div ' + disabled + ' style="position: relative; visibility: visible; cursor: hand; text-align: center; background: buttonface; border: 2 outset buttonhighlight; padding: 1;" onclick="displayClick(\'' + id + '\');" onselectstart="return false;"';
		}
		if (id != null)
			str += ' id="DisplayBoxDD' + id + '"';
		else
			id = '';
		if (css != null)
			str += ' style="' + css + '"';
		else
			css = '';
		str += '>\n';
		if (!multiple) {
			str += "<div nowrap style='cursor: hand; overflow-x: hidden; width: 100%;'>" + buttontext + "</div>";
		}
		str += '</div>\n';
		if ((multiple) && (buttontext != "")) {
			str += '<div ' + disabled + ' style="cursor: hand; text-align: center; background: buttonface; border: 2 outset buttonhighlight;" onclick="displayClick(\'' + id + '\');"';
			if (css != null)
				str += ' style="' + css + '"';
			else
				css = '';
			str += '>\n';
			str += '<font size="1">' + buttontext + '</font>\n';
			str += '</div>\n';
		}
	}

	// Span startTag	
		str += '<span class="select" onpropertychange="updateSelectBox(this);"';
		if (size == null)
			size = 1;
		str += ' size="' + size + '"';
		if ((buttononly) && (size == 1))
			str += ' buttononly';
		if (multiple)
			str += ' multiple';
		readonly = "readonly";
		onclick = "";
		if (combo) {
			str += ' combo';
			readonly = "";
			onclick = "this.select();";
		}
		if (css == null)
			css = '';
		if (popselect) {
			str += ' popselect';
			str += ' style="display: none;' + css + '"';
		}
		else {
			str += ' style="'+ css + '"';
		}
		if (id != null)
			str += ' id="DD' + id + '"';
		else
			id = '';
		if (onchange != null)
			str += ' onchange="' + onchange + '"';
		str += ' sortorder="' + sortorder + '"';
		str += '>\n';
	
	// Table Tag
		showtable = "";
		if (buttononly) {
			showinput = "none";
			tableclass = "selectTableButton";
			buttonclass = "buttononly";
		}
		else {
			if ((multiple) || (popselect)) {
				showtable = "none";
			}
			showinput = "";
			tableclass = "selectTable";
			buttonclass = "button";
		}
		str += '<table ' + disabled + ' style="display: ' + showtable + ';" class="' + tableclass + '" cellspacing="0" cellpadding="0" onclick="toggleDropDown(this.parentElement);">\n';
		str += '<tr>\n';
		str += '<td><input type="text" class="selected" style="display: ' + showinput + '; ' + css + '" ' + readonly + ' value="" onfocus="MyFocus2(this);" onblur="MyBlur2(this);" onkeydown="navHandler();" onkeyup="keyHandler(); this.focus();" onclick="' + onclick + '">\n';
		str += '<input type="hidden" name="' + id + '" value=""></td>\n';
		if (size == 1) {
			var btndisabled = "";
			if ((matrix != null) && (matrix.length == 0)) {
				btndisabled = "disabled";
			}
			str += '<td><button class="' + buttonclass + '" ' + btndisabled + ' style="width: 100%;" tabindex="-1">' + buttontext + '<label style="font-family: webdings; font-size: 5pt;">6</label></button></td>\n';
		}
		str += '</tr>\n';
		str += '</table>\n';
		
	// DropDown startTag
		if (popselect) {
			str += '<div class="dropDown" onclick="optionClick();" onkeydown="navHandler();" onkeyup="keyHandler();" onselectstart="return false;">\n';
		}
		else if (multiple) {
			str += '<div ' + disabled + ' style="position: relative; visibility: visible; display: none; background: white; border: 2 inset buttonhighlight;" class="dropDown" onclick="optionClick();" onkeydown="navHandler();" onkeyup="keyHandler();" onselectstart="return false;">\n';
		}
		else {
			str += '<div style="position: absolute; visibility: hidden; display: none;" class="dropDown" onclick="optionClick();" onmouseover="optionOver();" onmouseout="optionOut();" onkeydown="navHandler();" onkeyup="keyHandler();" onselectstart="return false;">\n';
		}
		
		// We may start with an empty dropdown and add options later
		if (matrix != null) {
			for (var i=0; i<matrix.length; i++) {
				html     = matrix[i].html;
				value    = matrix[i].value;
				css      = matrix[i].css;
				selected = matrix[i].selected;
				
			// Write option starttag
				str += '<div nowrap width="100%" class="option" onpropertychange="updateOption(this);"';
				if (value != null)
					str += ' value="' + value + '"';
				if (css != null)
					str += ' style="' + css + '"';
				if (selected != null)
					str += ' selected';
				str += '>\n';
				
			// Write HTML contents
				str += html;
			// Write end tag
				str += '</div>\n';
			}
		}
	
	// DropDown endtag
		str += '</div>\n';
		
	// If multi-select, output "select all" button
		if ((multiple) || (popselect)) {
			str += '<table width="100%" ' + disabled + ' style="background: buttonface; border: 2 outset buttonhighlight; padding-right: 1" cellspacing="0" cellpadding="0">\n';
			str += '<tr>\n';
			if (multiple) {
				str += '<td width="80%" align="left"><input type="checkbox" style="border: 0px; background: transparent;" value="" onclick="selectAll(this.checked);"><font size="1">All</font></td>\n';
			}
			if (popselect) {
				str += '<td width="20%" align="right"><label title="Accept and Close" style="cursor: hand;" onclick="closeSelectPopup();"><font face="Webdings" size="3" color="green">4</font></label></td>\n';
			}
			str += '</tr></table>\n';
		}
		
	// Span endTag
		str += '</span>';
	return str;
}

function createXString(matrix, id, size, onchange, css, disabled, buttononly, buttontext, multiple, popselect, sortorder, combo, classname) {
	// Set disabled, if not passed in
	if (disabled == null)
		disabled = "";

	// Select startTag
	str = '<select class="' + classname + '" ' + disabled + ' ';
	if (size == null)
		size = 1;
	str += ' size="' + size + '"';
	if (multiple) {
		str += ' multiple';
	}
	if (id != null) {
		str += ' id="' + id + '"';
		str += ' name="' + id + '"';
	}
	if (onchange != null)
		str += ' onchange="' + onchange + '"';
	//str += ' onfocus="this.className=\'dropDown\';"';
	//str += ' onblur="this.className=\'\';"';
	if (css != null)
		str += ' style="' + css + '"';
	str += '>\n';

	// write options
	for (var i=0; i<matrix.length; i++) {
		html     = matrix[i].html;
		value    = matrix[i].value;
		css      = matrix[i].css;
		selected = matrix[i].selected;
		
	// Write option starttag
		str += '\n<option';
		if (value != null)
			str += ' value="' + value + '"';
//		if (css != null)
//			str += ' style="' + css + '"';
		if (selected != null)
			str += ' selected';
		str += '>';
		
	// Write HTML contents
		str += stripTags(html);
	// Write end tag
		str += '</option>\n';
	}
	str += '\n</select>\n';
	return str;
}

function stripTags(str) {
	var s = 0;
	var e = -1;
	var r = "";

	s = str.indexOf("<",e);	

	do {
		r += str.substring(e + 1,s);
		e = str.indexOf(">",s);
		s = str.indexOf("<",e);
	}
	while ((s != -1) && (e != -1))

	r += str.substring(e + 1,str.length);

	return r;
}

function Option(html, value, css, selected) {
	if (css == "")
		css = null;
	if (selected == "")
		selected = null;
	this.html = html;
	this.value = value;
	this.css = css;
	this.selected = selected;
	// Create a 'Text' property to be compatible with the original Select's Option object
	this.text = html;
}
