function add_pick_item(){
	var pItem = get_obj("id_pickitem");
	var pList = get_obj("id_picklist");
	
	var selInd = pList.selectedIndex;
	
	if(Trim(pItem.value) != "") 
	{
		var selInd = pList.selectedIndex;
		var optLng = pList.options.length;
		var val = pItem.value;
		
		var arr = val.split("|");
		for(var i=0;i<arr.length;i++) 
		{
			if(Trim(arr[i]) == "") continue;
			parentID = getParentPickID();
			if(Trim(parentID) == "") parentID = 0;
			parentTxt = Trim(getParentPickText());
			val = "0|" + parentID + "|" + arr[i];
			tt = arr[i] + " (" + parentTxt +")";
			if(!checkForUniqueListText(get_obj('id_picklist'), tt)) {
				alert(tt + " : element already exists.");
				continue;
			}
			
			var opt = new Option(tt, val, false, true);
			opt.setAttribute("parentID", parentID);
			
			pList.options[optLng+i] = opt;
		}
		pItem.value = "";
	}
	pItem.focus();
}

function remove_selected(){
	var pList = get_obj("id_picklist");
	for(var i=0;i<pList.options.length;i++) {
		if(pList.options[i].selected) {
			pList.remove(i);
		}
	}
}

function checkForUniqueListText(listObj, txt) {
	var pList = get_obj("id_picklist");
	for(var i=0;i<pList.options.length;i++) {
		//alert(txt + " : " + pList.options[i].text);
		if(txt == pList.options[i].text) {
			pList.options[i].selected = true;
			return false;
		}
	}
	return true;
}

function pi_down(){
	var pItem = get_obj("id_pickitem");
	var pList = get_obj("id_picklist");
	
	var selInd = pList.selectedIndex;
	var optLng = pList.options.length;

	if(selInd == 0) return ;

	swapElems(selInd, selInd-1);

}

function pi_up(){
	var pItem = get_obj("id_pickitem");
	var pList = get_obj("id_picklist");
	
	var selInd = pList.selectedIndex;
	var optLng = pList.options.length;

	if((selInd+1) == optLng) return ;
	swapElems(selInd, selInd+1);
}

function swapElems(elemIndex1, elemIndex2){
	
	if(elemIndex1 <0 || elemIndex2 < 0) return false;
	var pItem = get_obj("id_pickitem");
	var pList = get_obj("id_picklist");
	
	var selInd = elemIndex1;
	var optLng = pList.options.length;

	var o1 = new Option(pList.options[elemIndex1].text, pList.options[elemIndex1].value, false, true);
	var o2 = new Option(pList.options[elemIndex2].text, pList.options[elemIndex2].value);//pList.options[selInd+1];
	
	pList.options[elemIndex1] = o2;
	pList.options[elemIndex2] = o1;
}

function submitAttrForm(){
	var pList = get_obj("id_picklist");
	
	var optLng = pList.options.length;
	
	for(var i=0;i<optLng;i++){
		pList.options[i].selected = true;
	}
	var k = document.getElementById("id_attr_form");
//	showObj(k);
	try {
		k.submit();
	} catch(e){
		alert(e);
	}
}

function CheckKeyRequestNum(evt)
{
	evt = (evt) ? evt : (window.event) ? window.event : "";
	var theKey ;
	if (evt)
	{
		theKey = (evt.which) ? evt.which : evt.keyCode ;
	}
//	alert(theKey);
	if (theKey == 13)
	{
		add_pick_item();
		return false;
	}
	
}


function showObj(obj){
	var txt = "";
	for(var k in obj) {
		txt += k + " : " + obj[k] + "\n";
	}
}


function getPicklistForParentID()
{
	var p = document.getElementById("id_pick_parent");
	var pList = document.getElementById("id_picklist");
	var selInd = p.selectedIndex;
	var value = p.options[selInd].value;
	for(var i=0;i<pList.options.length;i++) {
		var opt = pList.options[i];
		opt.selected = (opt.getAttribute("parentID") == value);
	}
}

function setParent(){
	var p = document.getElementById("id_picklist");
	var selInd = p.selectedIndex;
	var parentID = p.options[selInd].getAttribute("parentID");
	selectOptionElemByValue(get_obj("id_pick_parent"), parentID);
}

function selectOptionElemByValue(obj, val) {

	for(var i=0;i<obj.options.length;i++){
		var k = obj.options[i].value;
		if(k == val) {
			obj.options[i].selected = true;
			return;
		}
	}
}

function getParentPickID(){
	var p = document.getElementById("id_pick_parent");
	var selInd = p.selectedIndex;
	return p.options[selInd].value;
}

function getParentPickText(){
	var p = document.getElementById("id_pick_parent");
	var selInd = p.selectedIndex;
	return p.options[selInd].text;
}
