function MsSearch(){
	
	this.dialogWidth = 580;
	this.dialogHeight = 440;
	
}

MsSearch.prototype.returnOption = function( box, type ){
	
	switch( type ){
		case 'reg':
			store = document.forms['fsearch'].reg;
			break;
		case 'tag':
			store = document.forms['fsearch'].tag;
			break;
		case 'mis':
			store = document.forms['fsearch'].mis;
			break;
		case 'aom':
			store = document.forms['fsearch'].aom;
			break;
	}
	
	arr = new Array();
	if( store.value ) arr = store.value.split(',');
	
	if( box.checked )
		arr.push(box.value);
	else
		arr = arr.without(box.value);
	
	arr = arr.uniq().compact();
	store.value = arr.join(',');
	
	this.updateOptionsList(type, box);
	
	Element.extend(box);
	
	if( box.checked ){
		ul = box.up();
		ul.addClassName('checked');
	} else {
		ul = box.up();
		ul.removeClassName('checked');
	}
	
	/*alert(store.value);*/
	
}

MsSearch.prototype.updateOptionsList = function( type, box ){

	Element.extend(box);
	var ul = $(type + 'ul');
	
	descendants = ul.descendants();
	if( descendants.length ){
		
		exists = false;
		descendants.each(function( li ){
		    if( li.value == box.value ){
		    	if( !box.checked ){
		    		li.remove();
		    		throw $break;
		    	} else {
		    		exists = true;
		    		throw $break;	
		    	}
		    }
		});
		if( box.checked && !exists )
			new Insertion.Bottom(ul, '<li value="' + box.value + '">' + box.readAttribute('optname') + '</li>');
			
	} else if( box.checked ){
		
		new Insertion.Top(ul, '<li value="' + box.value + '">' + box.readAttribute('optname') + '</li>');
		
	}
	
	descendants = ul.descendants();
	if( descendants.length > 1 ){
		descendants.each(function( li ){ /*alert(li.innerHTML + ' ' + li.value);*/
		    if( li.value == 0 ){
		    	li.remove();
		    	throw $break;
		    }
		});
	} else if( descendants.length == 0 ) {
		new Insertion.Top(ul, '<li value="0" class="light_gray">No items selected</li>');
	}
	
}

MsSearch.prototype.openDialog = function( type ){
	
	var parent = this;
	
	var div = $('dialog');
		
	switch( type ){
		case 'reg':
			param = document.forms['fsearch'].reg.value;
			break;
		case 'tag':
			param = document.forms['fsearch'].tag.value;
			break;
		case 'mis':
			param = document.forms['fsearch'].mis.value;
			break;
		case 'aom':
			param = document.forms['fsearch'].aom.value;
			break;
	}
	
	var url = '/mssearch/mssearch.prc.php';
	new Ajax.Request(url, {
		method: 'post',
		parameters: {
			'type' : type,
			'param': param
		},
		onSuccess: function( transport ){
			
			new Insertion.Top($('dlgin'), transport.responseText);
			
			div.setStyle({
				'width': parent.dialogWidth,
				'height': parent.dialogHeight,
				/*'height': 'auto',*/
				'left': parseInt((document.viewport.getWidth() - parent.dialogWidth)/2),
				'top': parseInt((document.viewport.getHeight() - parent.dialogHeight)/2),
				'display': 'block'
			});
			
		}
	});
		
}

MsSearch.prototype.closeDialog = function(){
	
	/*$('dlgin').innerHTML = '';*/
	$('dlgin').replace('<div id="dlgin"></div>');
	$('dialog').setStyle({'display': 'none'});
	
}

var MsSearch = new MsSearch();