/**
* @ author Greg MacWilliam.
* opens/closes employer DHTML menu and assigns selected values to form
*/

var employer_select_form;
	
function openEmployerSelect() {

	/**
	* opens employer select menu and configres click-off handling
	*
	*/
	
	var dmenu = document.getElementById('employer-select-menu');
	
	dmenu.style.visibility = 'hidden';
	dmenu.style.display = 'block';
	
	//determin vertical space available to menu
	var my = Position.cumulativeOffset(document.getElementById('employer-select'))[1];
	var fy = Position.cumulativeOffset(document.getElementById('footer'))[1];
	var yspan = (fy-my)+40;
	
	dmenu.style.height = 'auto';
	dmenu.style.overflow = 'visible';
	
	if (dmenu.offsetHeight > yspan) {
		//var lh = Menu.getElementsByTagName('a')[0].offsetHeight;
		var lh = 19; //--hard-coded for optimization!!
		dmenu.style.height = lh*Math.floor(yspan/lh)+'px';
		dmenu.style.overflow = 'auto';
	}
	
	dmenu.style.visibility = 'visible';
	
	//configure click-off
	document.onmousedown = clickoffEmployerSelect;
	dmenu.onmousedown = function() {
		//delete document.onmousedown;
		document.onmousedown = null;
	};
}


function clickoffEmployerSelect() {
	
	/**
	* clears document click-off responder then hides employer menu layer
	*
	*/
	
	//delete document.onmouseup;
	document.onmousedown = null;
	closeEmployerSelect();
	//alert('clicked off');
}


function closeEmployerSelect() {

	/**
	* hides employer menu layer
	*
	*/

	document.getElementById('employer-select-menu').style.display = 'none';
}


function setEmployerSelection(setvalue, id) {
	
	/**
	* assigns selected values to form elements then hides menu
	*
	*/
	
	if (setvalue != undefined) document.getElementById('employer-select-field').innerHTML = setvalue;
	if (id != undefined) document.getElementById(employer_select_form).value = id;
	closeEmployerSelect();
}


function configureEmployerSelect() {

	/**
	* copies options from employer select form field and builds them as DHTML
	*
	*/
	
	var form = document.getElementById(employer_select_form);
	var dhtml = document.getElementById('employer-select');
	var dsel = document.getElementById('employer-select-field');
	var dmenu = document.getElementById('employer-select-menu');
	var opts = form.getElementsByTagName('option');
	var maxw = 0;
	
	form.style.display = 'none';
	dhtml.style.display = 'block';
	
	for (var j=0; j<opts.length; j++) {
		
		var li = dmenu.appendChild(document.createElement('li'));
		var a = li.appendChild(document.createElement('a'));
		
		a.name = opts[j].value;
		a.innerHTML = opts[j].innerHTML;
		a.href = 'javascript:void(0);';
		a.onclick = function() { this.className="out"; setEmployerSelection(this.innerHTML, this.name); };
		a.onmouseover = function() { this.className=""; };
		
		a.width='auto';
		if (a.offsetWidth>maxw) maxw = a.offsetWidth;
		a.style.display='block';
	}
	
	maxw += 16; //right-margin allocated for scrollbar
	
	var minw = dhtml.offsetWidth;
	if (maxw<minw) maxw = minw;
	dmenu.style.width = maxw+"px";
	dmenu.style.left = (minw-maxw)+"px";
	
	
	dsel.href = "javascript:void(0);";
	dsel.onclick = openEmployerSelect;
}