
loadScript = function(url) {
	//var e = document.createElement("script");
	//e.src = url;
	//e.type="text/javascript";
	//document.getElementsByTagName("head")[0].appendChild(e);
	document.write('<script src="' + url + '" type="text/javascript"></script>');
}

loadScript('/BOSI/js/BOSI3.js');
loadScript('/BOSI/js/BOSI3_Forms.js');

var w3c=(document.getElementById)? true: false;
var ns4=(document.layers)?true:false;
var ie5=(w3c && document.all)? true : false;
var ns6=(w3c && !document.all)? true: false;

function formsSubmit(formsAction,formsButton,formsFocus) {
	document.mainform.formsAction.value = formsAction;
	if(typeof(formsButton) != 'undefined') { document.mainform.formsButton.value = formsButton; }
	if(typeof(formsFocus) != 'undefined') { document.mainform.formsFocus.value = formsFocus; }
	document.mainform.submit();
	return false;
}

function cbAttachEvent(e,eventname,handler) {
	if(ie5) {
		return e.attachEvent(eventname,handler);
	} else {
		return e.addEventListener(eventname.substring(2,eventname.length),handler,false);
	}
}

function cbDetachEvent(e,eventname,handler) {
	if(ie5) {
		return e.detachEvent(eventname,handler);
	} else {
		return e.removeEventListener(eventname.substring(2,eventname.length),handler,false);
	}
}

/******************************************************************************************************************************************
	Handle all the javascript actions for BOSIFORMS tables in their various modes
******************************************************************************************************************************************/
function BOSIDatatable(uid,currentSortString,scrollMode,selectMode,submitOnClick,submitOnDblClick) {
	
	// initialize the member function references 
	// for the class prototype
	if (typeof(_BOSIDatatable_prototype_called) == 'undefined')
	{
		_BOSIDatatable_called = true;
		BOSIDatatable.prototype.init = init;
		BOSIDatatable.prototype.checkboxes = checkboxes;
		BOSIDatatable.prototype.pagedGo = pagedGo;
		BOSIDatatable.prototype.toggle = toggle;
		BOSIDatatable.prototype.select = select;
		BOSIDatatable.prototype.selectOne = selectOne;
		BOSIDatatable.prototype.deselect = deselect;
		BOSIDatatable.prototype.selectAll = selectAll;
		BOSIDatatable.prototype.deselectAll = deselectAll;
		BOSIDatatable.prototype.handleClick = handleClick;
		BOSIDatatable.prototype.handleDblClick = handleDblClick;
		BOSIDatatable.prototype.handleSort = handleSort;
		BOSIDatatable.prototype.handleQBEChange = handleQBEChange;
	}

	this.uid = uid;
	this.scrollMode = scrollMode;	// 'paged' or 'scrolled'
	this.selectMode = selectMode;	// 'none' (click causes onclick action) or 'single' or 'multi'
	this.currentSortString = currentSortString;    // [a|d]<columnIdent>
	this.submitOnClick = submitOnClick;
	this.submitOnDblClick = submitOnDblClick;
	this.init();

	function init() {
	}

	// Handle pagination requests
	function pagedGo(startIndex) {
		var mainform = document.mainform;
		mainform['startIndex_'+this.uid].value=startIndex;
		mainform['actionVerb_'+this.uid].value='refresh';
		bMainForm.doAction('controlAction',{
			controlActionTarget:this.uid,
			focus:'CONTROL_'+this.uid
		});
		return false;
	}
	
	function checkboxes() {
		var mainform = document.mainform;
		var l = mainform['selection_'+this.uid+':list'];
		if(l.length) {
			return l;
		} else {
			var nl = [l];
			return nl;
		}
	}
	
	function select(index) {
		this.checkboxes()[index].checked = true;
		document.getElementById('TABLE_'+this.uid).rows[index+2].className = 'datarow selected';
	}

	function toggle(index) {
		if(this.checkboxes()[index].checked)
			this.deselect(index);
		else
			this.select(index);
	}

	function deselect(index) {
		this.checkboxes()[index].checked = false;
		document.getElementById('TABLE_'+this.uid).rows[index+2].className = 'datarow';
	}
	
	function selectOne(index) {
		this.deselectAll();
		this.select(index);
	}

	function selectAll() {
		for(i=0;i<this.checkboxes().length;i++) {
			this.select(i);
		}
	}

	function deselectAll() {
		for(i=0;i<this.checkboxes().length;i++) {
			this.deselect(i);
		}
	}

	function handleClick(index,event,itemUID) {
		switch(this.selectMode) {
			case 'none':
				break;
			case 'single':
				this.deselectAll();
				this.select(index);				
				break;
			case 'multi':
				this.toggle(index);	
				break;
		}
		if(!this.submitOnClick) return;
		var mainform = document.mainform;
		mainform['actionItem_'+this.uid].value = itemUID;
		mainform['actionVerb_'+this.uid].value = 'click';
		bMainForm.doAction('controlAction',{
			controlActionTarget:this.uid,
			focus:'CONTROL_'+this.uid
		});
		return false;
	}

	function handleDblClick(index,event,itemUID) {
		if(!this.submitOnDblClick) return;
		var mainform = document.mainform;
		mainform['actionItem_'+this.uid].value = itemUID;
		mainform['actionVerb_'+this.uid].value = 'dblclick';
		bMainForm.doAction('controlAction',{
			controlActionTarget:this.uid,
			focus:'CONTROL_'+this.uid
		});
		return false;
	}

	function handleSort(columnIdent) {
		var newSortString = null;
		if(this.currentSortString == 'a'+columnIdent) {
			newSortString = 'd'+columnIdent;
		} else if (this.currentSortString == 'd'+columnIdent) {
			newSortString = 'a'+columnIdent;
		} else {
			newSortString = 'a'+columnIdent;
		}
	
		var mainform = document.mainform;
		mainform['sortString_'+this.uid].value=newSortString;
		mainform['actionVerb_'+this.uid].value='refresh';
		mainform['startIndex_'+this.uid].value=0;
		bMainForm.doAction('controlAction',{
			controlActionTarget:this.uid,
			focus:'CONTROL_'+this.uid
		});
		return false;
	}

	function handleQBEChange(fieldId) {
		var mainform = document.mainform;
		mainform['actionVerb_'+this.uid].value='refresh';
		mainform['startIndex_'+this.uid].value=0;
		bMainForm.doAction('controlAction',{
			controlActionTarget:this.uid,
			focus:'CONTROL_'+this.uid
		});
		return false;
	}
}
	

function BOSIPageBar(uid,first,prev,next,last) {

	// initialize the member function references 
	// for the class prototype
	if (typeof(_BOSIPageBar_prototype_called) == 'undefined')
	{
		_BOSIPageBar_called = true;
		BOSIPageBar.prototype.init = init;
		BOSIPageBar.prototype.submitIndex = submitIndex;
		BOSIPageBar.prototype.handleKey = handleKey;
	}

	this.init(uid,first,prev,next,last);

	function init() {
		this.uid = uid;
		this.firstPageIndex = first;
		this.prevPageIndex = prev;
		this.nextPageIndex = next;
		this.lastPageIndex = last;
	}

	function submitIndex(startIndex) {
		var mainform = document.mainform;
		mainform['startIndex_'+this.uid].value=startIndex;
		mainform['actionVerb_'+this.uid].value='refresh';
		bMainForm.doAction('controlAction',{
			controlActionTarget:this.uid,
			focus:'CONTROL_'+this.uid
		});
		return false;
	}

	function handleKey(event) {
		switch(event.keyCode) {
			case 33:	// PgUp
				if(event.ctrlKey)
					this.submitIndex(this.firstPageIndex);
				else
					this.submitIndex(this.prevPageIndex);
				break;
			case 34:	//PgDn
				if(event.ctrlKey)
					this.submitIndex(this.lastPageIndex);
				else
					this.submitIndex(this.nextPageIndex);
				break;
		}
	}
}
/*********************************************************************
* TWO-LIST MULTI-SELECT FIELDS (BOSI FORMS)
*********************************************************************/
function twinListControl(list1,list2,uid,sort1,sort2) {

	if (typeof(_twinListControl_prototype_called) == 'undefined') {
		_twinListControl_prototype_called = true;
		twinListControl.prototype.init = init;
		twinListControl.prototype.hasOptions = hasOptions;
		twinListControl.prototype.moveSelection = moveSelection;
		twinListControl.prototype.moveSelectionUp = moveSelectionUp;
		twinListControl.prototype.moveSelectionDown = moveSelectionDown;
		twinListControl.prototype.swapOptions = swapOptions;
		twinListControl.prototype.sort = sort
		twinListControl.prototype.save = save;
	}
	
	this.init(list1,list2,uid,sort1,sort2);

	function init(list1,list2,uid,sort1,sort2) {
		if(typeof(list1)=="string") this.list1 = document.getElementById(list1);
		else this.list1 = list1;
		if(typeof(list2)=="string") this.list2 = document.getElementById(list2);
		else this.list2 = list2;
		this.uid = uid;
		this.sort1 = sort1;
		this.sort2 = sort2;
	}

	function hasOptions(obj) {
		if (obj!=null && obj.options!=null) { return true; }
		return false;
	}

	function moveSelection(from,to) {
		if(typeof(from)=="string") from = document.getElementById(from);
		if(typeof(to)=="string") to = document.getElementById(to);
		// Move them over
		if (!this.hasOptions(from)) { return; }
		for (var i=0; i<from.options.length; i++) {
			var o = from.options[i];
			if (o.selected) {
				if (!this.hasOptions(to)) { var index = 0; } else { var index=to.options.length; }
				to.options[index] = new Option( o.text, o.value, false, false);
			}
		}
		// Delete them from original
		for (var i=(from.options.length-1); i>=0; i--) {
			var o = from.options[i];
			if (o.selected) {
				from.options[i] = null;
			}
		}
		if((from == this.list1 && this.sort1) || (from == this.list2 && this.sort2)) this.sort(from);
		if((to == this.list1 && this.sort1) || (to == this.list2 && this.sort2)) this.sort(to);

		from.selectedIndex = -1;
		to.selectedIndex = -1;
		this.save();
	}


	function moveSelectionUp(obj) {
		if(typeof(obj)=="string") obj = document.getElementById(obj);
		if (!this.hasOptions(obj)) { return; }
		for (i=0; i<obj.options.length; i++) {
			if (obj.options[i].selected) {
				if (i != 0 && !obj.options[i-1].selected) {
					this.swapOptions(obj,i,i-1);
					obj.options[i-1].selected = true;
				}
			}
		}
		this.save();
	}

	function moveSelectionDown(obj) {
		if(typeof(obj)=="string") obj = document.getElementById(obj);
		if (!this.hasOptions(obj)) { return; }
		for (i=obj.options.length-1; i>=0; i--) {
			if (obj.options[i].selected) {
				if (i != (obj.options.length-1) && ! obj.options[i+1].selected) {
					this.swapOptions(obj,i,i+1);
					obj.options[i+1].selected = true;
				}
			}
		}
		this.save();
	}

	function swapOptions(obj,i,j) {
		var o = obj.options;
		var i_selected = o[i].selected;
		var j_selected = o[j].selected;
		var temp = new Option(o[i].text, o[i].value, o[i].defaultSelected, o[i].selected);
		var temp2= new Option(o[j].text, o[j].value, o[j].defaultSelected, o[j].selected);
		o[i] = temp2;
		o[j] = temp;
		o[i].selected = j_selected;
		o[j].selected = i_selected;
		}

	function sort(obj) {
		var o = new Array();
		if (!hasOptions(obj)) { return; }
		for (var i=0; i<obj.options.length; i++) {
			o[o.length] = new Option( obj.options[i].text, obj.options[i].value, obj.options[i].defaultSelected, obj.options[i].selected) ;
		}
		if (o.length==0) { return; }
		o = o.sort( 
			function(a,b) { 
				if ((a.text+"") < (b.text+"")) { return -1; }
				if ((a.text+"") > (b.text+"")) { return 1; }
				return 0;
			} 
		);
		
		for (var i=0; i<o.length; i++) {
			obj.options[i] = new Option(o[i].text, o[i].value, o[i].defaultSelected, o[i].selected);
		}
	}
	
	function save() {
		var out = "";
		for(j=0;j<this.list2.options.length;j++) {
			var option = this.list2.options[j];
			if(out) out += '|' + option.value;
			else out += option.value;
		}
		document.getElementById(this.uid).value = out;
	}
}

