// Make a popup window to fill in multi-select items.
//
// Copyright 2008 Michael Chaney Consulting Corporation - All Rights Reserved

function selection_popup(field, header, items_per_col) {
	if (!items_per_col) items_per_col=15;
	var ie_5_or_6_window_int_timer = null;

	var body = document.getElementsByTagName('body')[0];

	field = $(field);

	var div;
	div = document.getElementById('multi-select-popup');

	if (div) {
		// clean it out
		while (div.lastChild) {
			div.removeChild(div.lastChild);
		}
	} else {
		div = document.createElement('div');
		div.className = 'multi-select-popup';
		div.id = 'multi-select-popup';
	}

	var h3 = document.createElement('h3');
	h3.appendChild(document.createTextNode(header));
	div.appendChild(h3);

	// find the elements in the select box
	if (field.type!='select-multiple') {
		throw new Error("selection_popup only works with multi-select controls");
		return false;
	}

	var portal_div = document.createElement('div');
	portal_div.className = 'portal';
	div.appendChild(portal_div);
	var scroller_div = document.createElement('div');
	scroller_div.className = 'scroller';
	portal_div.appendChild(scroller_div);

	var row=999999;
	var ul=false;

	var all_uls=[];

	for (var i=0 ; i<field.options.length ; i++) {
		if (row>=items_per_col) {
			if (ul) scroller_div.appendChild(ul);
			if (ul) all_uls.push(ul);
			ul = document.createElement('ul');
			row = 0;
		}
		var cb = document.createElement('input');
		cb.type = 'checkbox';
		cb.value = i;
		cb.className = 'multi-select-popup-checkbox';
		if (field.options[i].selected) {
			cb.checked = true;
			cb.defaultChecked = true;
		}
		var li = document.createElement('li');
		li.appendChild(cb);
		li.appendChild(document.createTextNode(field.options[i].text));
		if (row&1) li.className='odd';
		ul.appendChild(li);
		row++;
	}

	if (ul) scroller_div.appendChild(ul);
	if (ul) all_uls.push(ul);

	var br = document.createElement('br');
	br.setAttribute('clear', 'both');
	div.appendChild(br);

	function properly_size_elements() {
		var scroller_width=0;
		for (var i=0 ; i<all_uls.length ; i++) scroller_width += all_uls[i].offsetWidth;
		scroller_div.style.width = (scroller_width+5) + 'px';
		portal_div.style.height = (all_uls[0].offsetHeight+20) + 'px';
		if (div.getHeight() > document.viewport.getHeight()) {
			// resize the viewport
			div.style.top = '0px';
			div.style.height = document.viewport.getHeight().toString()+'px';
		} else if (div.getHeight() > document.viewport.getHeight()-100) {
			// relocate the viewport
			div.style.top = ((document.viewport.getHeight() - div.getHeight())/2).toString() + 'px';
		}
	}

	function apply_selection_popup() {
		var cbs = div.getElementsByTagName('input');
		for (var i=0 ; i<cbs.length ; i++) {
			if (cbs[i].type == 'checkbox') {
				if (cbs[i].checked && !field.options[i].selected) {
					field.options[i].selected=true;
				} else if (!cbs[i].checked && field.options[i].selected) {
					field.options[i].selected=false;
				}
			}
		}
		body.removeChild(div);
		if (ie_version>0 && ie_version<7) {
			field.scrollIntoView();
		}
	}

	function cancel_selection_popup() {
		body.removeChild(div);
		if (ie_version>0 && ie_version<7) {
			field.scrollIntoView();
		}
	}

	var apply_button = document.createElement('input');
	apply_button.type = 'button';
	apply_button.value = 'Save';
	apply_button.onclick = apply_selection_popup;
	div.appendChild(apply_button);

	var cancel_button = document.createElement('input');
	cancel_button.type = 'button';
	cancel_button.value = 'Cancel';
	cancel_button.onclick = cancel_selection_popup;
	div.appendChild(cancel_button);

	// For ie 5/6, we have to position it above the form.  There's an
	// interesting bug where select boxes always render at the front.
	if (ie_version>0 && ie_version<7) {
		body.insertBefore(div, body.firstChild);
		div.scrollIntoView();
	} else {
		body.appendChild(div);
		properly_size_elements();
	}
}
