function getElement(id)
{
	var obj = null;
	if (document.getElementById)
		obj = document.getElementById(id);
	else if (document.all)
		obj = document.all[id];
	// if not yet found, try by name
	if (!obj && document.getElementsByName)
	{
		obj = document.getElementsByName(id);
		obj = (obj.length > 0) ? obj[0] : null;  //return the first element only
	}
	return obj;
}

function displayBlock(id)
{
	var obj = getElement(id);
	if (obj && obj.style)
		if (obj.style.display == 'none')
			obj.style.display = '';  //assume default display
		else
			obj.style.display = 'none';
}

function enableElements(enable, ids)
{
	for (var i = 0; i < ids.length; ++i)
	{
		var el = getElement(ids[i]);
		if (el)
			el.disabled = !enable;
	}
}

function copy_val(from, to)
{
        from = getElement(from);
        to = getElement(to);
        if (from && to)
        {
                if (!to.value || to.value == from.oldvalue)
                        to.value = from.value;
                from.oldvalue = from.value;
        }
}

function copy_val_ddl(from, to)
{
	// Copies value from dropdownlist to textbox on change of the ddl.
        var ddl = getElement(from);
        var txt = getElement(to);
        if (ddl && txt)
        {
                var opts = ddl.options;
                if (opts)
                {
                        var sel = opts[opts.selectedIndex];
                        if (sel)
                        {
                                txt.value = sel.text;
                        }
                }
        }
}

function initfollowddl(sDdl, sTxt)
{
	var ddl = getElement(sDdl);
	var txt = getElement(sTxt);
	if (ddl && txt)
	{
		// The controls exist

		// Create a timer event, so that the txt follows the ddl.
		var evt = function() {
			var top = 0;
			var left = 0;
			for (var ctl=ddl; ctl; ctl=ctl.offsetParent)  //iterate through parents
			{
				top += ctl.offsetTop;
				left += ctl.offsetLeft;
			}
			var width = ddl.offsetWidth - 20 - 2*2;
			var height = ddl.offsetHeight - 2*3;
//alert('top=' + top + ', left=' + left + ', width=' + width + ', height=' + height);
//alert('txt.top=' + txt.offsetTop + ', txt.left=' + txt.offsetLeft + ', txt.width=' + txt.offsetWidth + ', txt.height=' + txt.offsetHeight);
			txt.style.top = top + 'px';
			txt.style.left = left + 'px';
			txt.style.width = width + 'px';
			//txt.style.height = height + 'px';
		};
		// Use a function as event handler, so that some stuff can be
		// pre-calculated and used via a closure. This way, it's a bit
		// more efficient and polling doesn't hurt so much.

		// Add function to onload, to setup the timer
		if (window.onload)
		{
			var prev = window.onload;  //store in closure
			window.onload = function() {
				prev();
				evt();
				window.setInterval(evt, 150);
			};
		}
		else
		{
			window.onload = function() {
				evt();
				window.setInterval(evt, 150);
			};
		}

		// Sets a handler to copy the text from ddl to txt.
		ddl.onchange = function(){
			copy_val_ddl(sDdl, sTxt);
		}
	}
}



