// Tridesign Multimedia 2006
// This is a merging of several javascript efforts.
// Takes into account <select> elements without ids
// and ensuring updates of original form values on submit
// *** Does require the existance of a <form> on which the target <selects> must sit. ***** 
// The script replaces the original <select> element with a clone <object> called "comp_...." which can support a zIndex of "0"
// It then creates a hidden element named as per the original <select> which holds and passes the object's selected value 
//==============================================================================================================================

function Property(name, value) {
	this.name = name;
	this.value = value;
}

function changeMe(){
	if ((document.getElementById) && (document.all)) { 
   for(i=0; i<document.forms[0].elements.length; i++){
     var thisElem = document.forms[0].elements[i];
     var myName = thisElem.name;
     var pos = myName.indexOf("comp_");
	 if(pos == 0) {
	    orig_name = myName.substr(5,myName.length);
        value = thisElem.options[thisElem.value];
	    var orig_elem = document.all[orig_name];
		orig_elem.value = value;
		if(orig_elem.onchange != null) goSub = true; else goSub = false; 
	 }
   }
 }
 // if there is an Onchange event attached to the original <select>, perform that function (eg. submit form)
 if(goSub) {  
	 theFunc = orig_elem.onchange.toString();
	 var mystart = theFunc.indexOf("{");
	 var stuff = theFunc.substr(mystart+1,theFunc.length);
	 stuff = stuff.replace(/^\s*|\s*$/g, "");
	 var myend = stuff.lastIndexOf("}");
	 stuff = stuff.substr(0,myend-1);
	 stuff = stuff.replace(/^\s*|\s*$/g, "");
	 eval(stuff+";");
 }// 
}

var defaultSelectProps = new Array();
var theId;	
defaultSelectProps[defaultSelectProps.length] = new Property("VariousPropertyBits", "726624571");
defaultSelectProps[defaultSelectProps.length] = new Property("DisplayStyle", "7");
defaultSelectProps[defaultSelectProps.length] = new Property("ShowDropButtonWhen", "2");
defaultSelectProps[defaultSelectProps.length] = new Property("ScrollBars", "0");
defaultSelectProps[defaultSelectProps.length] = new Property("FontName", "Arial");
defaultSelectProps[defaultSelectProps.length] = new Property("FontHeight", "200");

function replaceIESelect(id, selectProps) {
	if (selectProps==null) selectProps = defaultSelectProps;
	if (document.all&&document.getElementById) {
		var sel = document.all[id];
		sel.attachEvent("change", changeMe); 
		var parent = sel.parentNode;
		var obj = document.createElement("object");
		var paramSuccess = true;
		try {
			for (var j in selectProps) {
				var param = document.createElement("param");
				param.setAttribute("name", selectProps[j].name);
				param.setAttribute("value", selectProps[j].value);
				obj.appendChild(param);
			}
		} catch(er) {
			paramSuccess = false;
		}
		with (obj) {
			setAttribute("classid","clsid:8BD21D30-EC42-11CE-9E0D-00AA006002F3");
			setAttribute("id", "comp_" + sel.name);
			setAttribute("name", "comp_" + sel.name);
			setAttribute("width", sel.offsetWidth);
			setAttribute("height", sel.offsetHeight);
//			setAttribute("align", "left");
	  		attachEvent("change", changeMe);
			if (!paramSuccess) Style=2;
		}
		var input = document.createElement("input");
		with (input) {
			setAttribute("id", sel.name);
			setAttribute("name", sel.name);
			setAttribute("type", "hidden");
			if(sel.onchange != '') setAttribute("onchange", sel.onchange); 
			else setAttribute("onchange", changeMe); 
		}
		obj.options = new Array();
		for (var j=0; j<sel.options.length; j++) {
			obj.options[sel.options[j].text] = sel.options[j].value;
		}
		obj.input = input;
		obj.input.value = obj.options[obj.value];
		for (var j in obj.options) {
			obj.additem(j);
		}
		selText = sel.options[sel.selectedIndex].text;
		selVal = sel.options[sel.selectedIndex].value;
		input.setAttribute("value", selVal);
		parent.replaceChild(obj, sel);
		parent.appendChild(input);
		obj.value = selText;
//		obj.style.position = "relative";
		obj.style.zIndex = "0";
	}
}

function replaceAllIESelects() {
	var arrSels = new Array();
	var arrNames = new Array();
	var frms = document.forms.length;
	if(frms > 0) { 
		if ((document.getElementById) && (document.all)) { 
			arrSels = document.getElementsByTagName("SELECT");
			for (var x=0;x<arrSels.length;x++)  { arrNames[x]= arrSels[x].name; }   
			for (var d=0;d<arrNames.length;d++) { replaceIESelect(arrNames[d]); }
		}  
	} 
}
