
var lastClickedItem;
document.onclick = function() {
	for (var i=0; i<selectAlternates.length; i++) {
		if (selectAlternates[i] != lastClickedItem) {
			document.getElementById(selectAlternates[i]).setAttribute('open', 'false');
			document.getElementById(selectAlternates[i]).style.display = 'none';
		}
	}

	lastClickedItem = null;
	
}

			
var selectAlternates = [];
function selectAlternate() {
	alert('boe');
	var candidates = document.getElementsByTagName("select");
	for (var i=0; i<candidates.length; i++) {
		// Retrieve the required information from the original select box
		var origSelect	= candidates[i];
		var origClass	= origSelect.className.substring(1);

		// Create replacement select box
		selectAlternates[selectAlternates.length]	= origSelect.getAttribute('id') + '_alt_dropdown';
		
		var selectReplace		= document.createElement('div');
		selectReplace.setAttribute('id', origSelect.getAttribute('id') + '_alt');
		selectReplace.setAttribute('orig_id', origSelect.getAttribute('id'));
		selectReplace.className		= origClass;
		selectReplace.onclick		= function() { 
								var targetSelect	= document.getElementById(this.getAttribute('orig_id') + '_alt');
								var targetSelectDD 	= document.getElementById(this.getAttribute('orig_id') + '_alt_dropdown');
								lastClickedItem		= this.getAttribute('orig_id') + '_alt_dropdown';

								if (targetSelectDD.getAttribute('open') == 'true') {
									targetSelectDD.setAttribute('open', 'false');
									targetSelectDD.style.display = 'none';
								} else {
									targetSelectDD.setAttribute('open', 'true');
									targetSelectDD.style.display = 'block';
								}
							}
		
		var selectReplaceList		= document.createElement('dl');
		var optionReplace		= document.createElement('dt');
		selectReplaceText 		= document.createTextNode(origSelect.options[0].text);
		optionReplace.appendChild(selectReplaceText);
		selectReplaceList.appendChild(optionReplace);
		selectReplace.appendChild(selectReplaceList);

		// Create replacement select box dropdown
		var selectReplaceOptions		= document.createElement('div');
		selectReplaceOptions.setAttribute('id', origSelect.getAttribute('id') + '_alt_dropdown');
		selectReplaceOptions.className		= origClass + '_options';
		var selectReplaceList			= document.createElement('dl');
		
		for (var j= 0; j<origSelect.options.length; j++) {
			var optionReplace		= document.createElement('dt');
			optionReplaceLink		= document.createElement('a')
			
			optionReplaceLink.setAttribute('position', j);
			optionReplaceLink.setAttribute('select_alt', origSelect.getAttribute('id') + '_alt');
			optionReplaceLink.setAttribute('select', origSelect.getAttribute('id'));
			optionReplaceLink.onclick	= function() {
									document.getElementById(this.getAttribute('select')).options[this.getAttribute('position')].selected = 'true';
									
									var newOption			= document.createElement('dt');
									if (!this.text) {
										var newOptionText		= document.createTextNode(this.childNodes[0].toString());
									} else {
										var newOptionText		= document.createTextNode(this.text);
									}
									newOption.appendChild(newOptionText);
									document.getElementById(this.getAttribute('select_alt')).childNodes[0].replaceChild(newOption, document.getElementById(this.getAttribute('select_alt')).childNodes[0].childNodes[0]);
									
									eval(document.getElementById(this.getAttribute('select')).getAttribute('onchangealt') + '()');
								}
			
			optionReplaceText 		= document.createTextNode(origSelect.options[j].text);
			
			optionReplaceLink.appendChild(optionReplaceText);
			optionReplace.appendChild(optionReplaceLink);
			selectReplaceList.appendChild(optionReplace);
		}

		// Append the replacement selectbox to the form
		selectReplaceOptions.appendChild(selectReplaceList);
		selectReplace.appendChild(selectReplaceOptions);
		origSelect.parentNode.insertBefore(selectReplace, origSelect);

		// Hide the original selectbox
		origSelect.style.display		= 'none';
		origSelect.style.position		= 'absolute';
		origSelect.style.left			= '-999em';
	}
}
