//************************************************************
//** Script for handling interactive profile maintenance
//***********************************************************/

//***************************************************
//** processCRForm() is called onsubmit() of the form.
//***************************************************/

catalogCodeArray=new Array();
catalogDescrArray=new Array();
function processCRForm(f) {
	for (var i=0;i<f.length;i++) {	
		var el = f[i]
		if (el.name=="profile_lst") {
			var strIDs = ""
			var strDescr = ""
			for (var j=0;j<f[i].options.length;j++){
				strIDs += f[i].options[j].value + "|"
				strDescr += f[i].options[j].text + "|"
			}
			f["PROFILE"].value = strIDs.substring(0,strIDs.length-1)
			f["PROFILE_DESCR"].value = strDescr.substring(0,strDescr.length-1)
			f.submit();
		}
	}
}

function move(f,bDir) {
	var el = f.elements["profile_lst"]
	var idx = el.selectedIndex
	if (idx==-1) 
		alert("You must first select the item to reorder.")
	else {
		var nxidx = idx;
		if(bDir=="true"){
			nxidx--;
		}else{
			nxidx++;
		}
		if (nxidx<0) nxidx=el.length-1
		if (nxidx>=el.length) nxidx=0
		var oldVal = el[idx].value
		var oldText = el[idx].text
		el[idx].value = el[nxidx].value
		el[idx].text = el[nxidx].text
		el[nxidx].value = oldVal
		el[nxidx].text = oldText
		el.selectedIndex = nxidx
	}
}

function outputButton(bDir,val) {
	return "<INPUT class=\"submit\" TYPE=button VALUE=\"" + val + "\" ONCLICK=\"move(this.form,'" + bDir + "')\">"
}

function outputList(sourceFieldName, listname, size) {
	var strIDs = "<SELECT SIZE=\"" + size + "\" ID=\"" + listname + "\" NAME=\"" + listname + "\" class=\"profmaint\">";
	var sel = " SELECTED";
	var item=document.UIservletForm[sourceFieldName].value.split("|");
	var itemDescr=document.UIservletForm[sourceFieldName+"_DESCR"].value.split("|");
	for (var i=0;i<item.length;i++) {
		strIDs += "<OPTION " + sel + " VALUE=\"" + item[i] + "\">" + itemDescr[i];
		sel = "";
	}
	strIDs+="</SELECT>";
	return strIDs;
}

function delItem(){
   var selIndex = document.UIservletForm["profile_lst"].selectedIndex;
   if(selIndex < 0)
      return;
   document.UIservletForm["profile_lst"].removeChild(document.UIservletForm["profile_lst"].options.item(selIndex))
   itemsLeft=document.UIservletForm["profile_lst"].length
   if(selIndex<itemsLeft){
		document.UIservletForm["profile_lst"].selectedIndex=selIndex;
   }else if(itemsLeft>0){
   		document.UIservletForm["profile_lst"].selectedIndex=itemsLeft-1;
   }
}

function addNewItem() {
	var catIndx=document.UIservletForm["catalog_lst"].selectedIndex;
	var optionValue=document.UIservletForm["catalog_lst"].options[catIndx].value;
	var optionText=document.UIservletForm["catalog_lst"].options[catIndx].text;
	
   for(x=0;x<document.UIservletForm["profile_lst"].options.length;x++){
   		//alert(document.UIservletForm["profile_lst"].options[x].value+" " +optionValue);
   		if(optionValue==document.UIservletForm["profile_lst"].options[x].value){
   			alert("This item is already on your profile.");
   			document.UIservletForm["profile_lst"].selectedIndex=x;
   			return;
   		}
   }
	
	
	var optionObject = new Option(optionText,optionValue);
	var newIndex= document.UIservletForm["profile_lst"].options.length;
	var selIndex=document.UIservletForm["profile_lst"].selectedIndex;
	if(selIndex==-1)selIndex=0;
	for(x=newIndex;x>selIndex;x--){
		v=document.UIservletForm["profile_lst"].options[x-1].value;
		t=document.UIservletForm["profile_lst"].options[x-1].text;
		o=new Option(t,v);
		document.UIservletForm["profile_lst"].options[x]=o
	}
	document.UIservletForm["profile_lst"].options[selIndex]=optionObject;
	document.UIservletForm["profile_lst"].selectedIndex=selIndex;
	//document.UIservletForm["profile_lst"].options[newIndex]=optionObject;
}

function profMaintSearch(bShowAll){
	criteria=document.UIservletForm["searchText"].value;
	if(bShowAll=="false" && criteria==""){
		return;
	}

	//empty the target
	document.UIservletForm["catalog_lst"].options.length=0;

	if(catalogDescrArray.length==0){
		buildCatalogArrays();
	}
	if(bShowAll=="true"){
		populateList(catalogCodeArray,catalogDescrArray,document.UIservletForm["catalog_lst"])
	}else{
		resultIndex=0;
		for(x=0;x<catalogDescrArray.length;x++){
			if(catalogDescrArray[x].indexOf(criteria.toUpperCase())>=0){
				o=new Option(catalogDescrArray[x],catalogCodeArray[x]);
				document.UIservletForm["catalog_lst"].options[resultIndex]=o;
				resultIndex++
			}
		}
	}
}

function buildCatalogArrays(){

	catalogCodeArray=document.UIservletForm["CATALOG"].value.split("|");
	catalogDescrArray=document.UIservletForm["CATALOG_DESCR"].value.split("|");

}
function populateList(fromCodeArray, fromDescrArray, toList){

	
	for(i=0;i<fromDescrArray.length;i++){
		o=new Option(fromDescrArray[i],fromCodeArray[i]);
		toList.options[i]=o;
		
	}
	
}
function pmSearchOnEnterKey(event) {
	var code = 0;
	if (NS4){
		code = event.which;
	}else{
		code = event.keyCode;
	}
	if (code==13){
		
		event.keyCode=38;
		profMaintSearch("false");
	}

}

