var menuSeparatorIndex = 0;

function addMenuItem(objList,itemText,itemValue,emptyText,emptyValue){
    itemText = (typeof itemText == "string") ? document.getElementById(itemText) : itemText;
    if (itemText.type.toLowerCase()!= 'text' || itemText.value.length < 1) return false;
	
    itemValue = (typeof itemValue == "string") ? document.getElementById(itemValue) : itemValue;
    if (itemValue.type.toLowerCase()!= 'text'  || itemValue.value.length < 1) return false;
	
    objList.addItem(new item(itemText.value,itemValue.value,1,1,-1));
    itemText.value = emptyText;
    itemValue.value = emptyValue;
}

function addMenuSeparatorSeparator(objList){
	
	objList.addItem(new item('--------------------', 'MENUSEPARATOR_'+menuSeparatorIndex,1,1,-1));
        menuSeparatorIndex++;
}

function initList(view){
        var i = 0;
        viewObj = document.getElementById(view);
        if (viewObj.tagName.toLowerCase()!= 'select') return null;
	
        optionList = new Array();
        for(i = 0; i < viewObj.options.length; i++){
                optionList[i] = viewObj.options[i];
        }
	
        var objList = new itemList(view);
	
        for(i = 0; i < optionList.length; i++){
                arrData = optionList[i].value.split('|');
                value = arrData[0];
                if (arrData.length > 2){
                        removable = parseInt(arrData[1]);
                        sortable = parseInt(arrData[2]);
                }else{
                        removable = 1;
                        sortable  = 1;
                }
		if (value.search('MENUSEPARATOR') > -1) menuSeparatorIndex++;
                objList.addItem(new item(optionList[i].text,value,removable,sortable,i));
        }
	
        return objList;
}

function submitItemList(itemList){
	
        var i;
        for (i=0; i<itemList.view.length;i++){
                item = itemList.getItemById(itemList.view[i].value);
                itemList.view[i].value = item.value+'|'+item.caption;
                itemList.view[i].selected = true;
        }
	
}

function moveOptions(sourceList, destinationList,sortDestination){
	
	var selection = new Array();
	
	for (i = 0; i <  sourceList.view.length ; i++) {
		if (sourceList.view[i].selected == true) {
			selection[selection.length] = sourceList.view[i].value;
		}
	}
	
	for (i in selection) {
		item = sourceList.getItemById(selection[i]);
		if (item.removable){
			destinationList.addItem(item);
			sourceList.removeItem(item);
		}
	}
	
	if (sortDestination) {
		destinationList.sortList();
		destinationList.listRender();
	}
	
}

function moveAllOptions(sourceList,destinationList,sortDestination){
	
	for (i = 0; i <  sourceList.view.length ; i++) {
		item = sourceList.getItemById(sourceList.view[i].value);
		if (item.removable){
			sourceList.view[i].selected = true;
		}
	}
	moveOptions(sourceList, destinationList,sortDestination);
}

function item(caption,value,removable,sortable,initialOrder){
	this.id = getUniqueId();
	this.caption = caption;
	this.value = value;
	this.removable = removable;
	this.sortable = sortable;
	this.initialOrder = initialOrder;
}

function itemList(view){
	
	this.list = new Array();
	this.slectionList = new Array();
	this.view = (typeof view == "string") ? document.getElementById(view) : view;
	
	// Method Definition
	this.setView = itemList_setView;
	this.addItem = itemList_addItem;
	this.removeItem = itemList_removeItem;
	this.removeAllItem = itemList_removeAllItem;
	this.itemDown = itemList_itemDown;
	this.itemUp = itemList_itemUp;
	this.getItemById = itemList_getItemById;
	this.getItemIndexById = itemList_getItemIndexById;
	this.getSelectedItem  = itemList_getSelectedItem;
	this.contains  = itemList_contains;
	
	this.listRender = itemList_listRender;
	this.refreshDisplay = itemList_refreshDisplay;
	this.sortList = itemList_sortList;
}

function itemList_setView(view){
	
	this.view = (typeof view == "string") ? document.getElementById(view) : view;
	this.listRender();
}

function itemList_addItem(item){
	
	this.list[this.list.length] = item;
	this.listRender();
}

function itemList_removeItem(item){
	
	if (item == null){
		for (i=this.view.length-1 ; i > -1 ; i--) {
			if ((this.view[i].selected == true) && (this.list[i].removable)) this.list.splice(i,1);
		}
	}else{
		if (typeof  item == 'object'){
			if (item.removable){
				for (i = 0; i < this.list.length; i++){
					if (this.list[i] == item) this.list.splice(i,1);
				}
			}
		}else{
			if (!isNaN(item) && (item < this.list.length) && (this.list[item].removable)) this.list.splice(item,1);
		}
	}
	this.listRender();
}

function itemList_removeAllItem(){
	
	for (i = this.list.length-1; i > -1; i--){
		this.removeItem(i);
	}
	
	this.listRender();
}

function itemList_itemDown(){
	
	var i;
	var selection = new Array();
	
	for (i=this.view.length-1; i>-1; i--) {
		if (this.view[i].selected == true) {
			selection[selection.length] = this.view[i].value;
		}
	}
	
	for (i in selection) {
		itemObj      = this.getItemById(selection[i]);
		itemIndex = this.getItemIndexById(selection[i]);
		
		if ((itemIndex != this.list.length-1) && (itemObj.sortable) && (this.list[itemIndex+1].sortable)){
			tmp = this.list[itemIndex+1];
			this.list[itemIndex+1] = itemObj;
			this.list[itemIndex] = tmp;
		}
	}
	
	this.listRender();

	var thisObj = this;
    setTimeout(function(){thisObj.refreshDisplay(selection)},0);
}

function itemList_itemUp(){
	
	var i;
	var selection = new Array();
	
	for (i=0 ; i < this.view.length; i++) {
		if (this.view[i].selected == true) {
			selection[selection.length] = this.view[i].value;
		}
	}
	
	for (i in selection) {
		itemObj      = this.getItemById(selection[i]);
		itemIndex = this.getItemIndexById(selection[i]);
		
		if ((itemIndex != 0) && (itemObj.sortable) && (this.list[itemIndex-1].sortable)){
			tmp = this.list[itemIndex-1];
			this.list[itemIndex-1] = itemObj;
			this.list[itemIndex] = tmp;
		}
	}
	
	this.listRender();
	
	var thisObj = this;
    setTimeout(function(){thisObj.refreshDisplay(selection)},0);
}

function itemList_refreshDisplay(selection){
	for (i in selection) {
		itemIndex = this.getItemIndexById(selection[i]);
		this.view.options[itemIndex].selected = true;
	}
}

function itemList_getItemById(id){
	
	var i;
	
	for (i = 0; i < this.list.length; i++){
		if (this.list[i].id == id) return this.list[i];
	}
	
	return null;
}

function itemList_getItemIndexById(id){
	
	var i;
	
	for (i = 0; i < this.list.length; i++){
		if (this.list[i].id == id) return i;
	}
	
	return -1;
}

function itemList_getSelectedItem(){

	for (i=0 ; i < this.view.length; i++) {
		if (this.view[i].selected == true) return this.list[i];
	}
	
	return null;
}

function itemList_contains(it){
	
	index = this.getItemIndexById(it.id);
	
	if (index == -1) return false;
	
	return true;
}

function itemList_listRender(){
	
	var i;
	this.view.options.length = 0;
	
	for (i = 0; i < this.list.length; i++){
		
		newItem = new Option(this.list[i].caption,this.list[i].id);
		if ((!this.list[i].removable)&&(!this.list[i].sortable)){
			//newItem.style.backgroundColor = "#F2F2ED";
			//newItem.style.color = "#CCCCCC";
			newItem.text = newItem.text+'*';
		}else{
			if (!this.list[i].removable){
				//newItem.style.backgroundColor = "#D3E0DF";
				//newItem.style.color = "#666666";
				newItem.text = newItem.text+'**';
			}
		}
		this.view.options[this.view.options.length] = newItem;
	}
}

function itemList_sortList(){
	this.list.sort(itemsCompare);
}

function itemsCompare(item1,item2){
	
	return (item1.initialOrder - item2.initialOrder);
}

function getUniqueId(){
	
	timeStamp = (new Date()).getTime();
	rnd = Math.floor(Math.random()*10001);
	return hex_md5(timeStamp+''+(rnd*timeStamp));
}

// Array Function Emulating
function Array_pop() {
	var response = this[this.length - 1]
	this.length--
	return response
}

if (typeof(Array.prototype.pop) == "undefined") {
	Array.prototype.pop = Array_pop
}

function Array_push() {
	var A_p = 0
	for (A_p = 0; A_p < arguments.length; A_p++) {
		this[this.length] = arguments[A_p]
	}
	return this.length
}

if (typeof Array.prototype.push == "undefined") {
	Array.prototype.push = Array_push
}

function Array_shift() {
	var A_s = 0
	var response = this[0]
	for (A_s = 0; A_s < this.length-1; A_s++) {
		this[A_s] = this[A_s + 1]
	}
	this.length--
	return response
}

if (typeof Array.prototype.shift == "undefined") {
	Array_prototype.shift = Array_shift
}


function Array_splice(index, delTotal) {
	var temp = new Array()
	var response = new Array()
	var A_s = 0
	for (A_s = 0; A_s < index; A_s++) {
		temp[temp.length] = this[A_s]
	}
	for (A_s = 2; A_s < arguments.length; A_s++) {
		temp[temp.length] = arguments[A_s]
	}
	for (A_s = index + delTotal; A_s < this.length; A_s++) {
		temp[temp.length] = this[A_s]
	}
	for (A_s = 0; A_s < delTotal; A_s++) {
		response[A_s] = this[index + A_s]
	}
	this.length = 0
	for (A_s = 0; A_s < temp.length; A_s++) {
		this[this.length] = temp[A_s]
	}
	return response
}

if (typeof Array.prototype.splice == "undefined") {
	Array.prototype.splice = Array_splice
}

function Array_unshift() {
	var A_u = 0
	for (A_u = this.length-1; A_u >= 0; A_u--) {
		this[A_u + arguments.length] = this[A_u]
	}
	for (A_u = 0; A_u < arguments.length; A_u++) {
		this[A_u] = arguments[A_u]
	}
	return this.length
}

if (typeof Array.prototype.unshift == "undefined") {
	Array.prototype.unshift = Array_unshift
}
