//************************************************************
//** Script for handling interactive profile maintenance
//***********************************************************/
//**************************************************************
//* outputButton_class can be set to "submit" for older sites
//************************************************************
var outputButton_class = "submit_short";

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

var catalogCodeArray=new Array();
var catalogDescrArray=new Array();
var catalogCommodityArray=new Array();
var catalogClassArray=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=\""+outputButton_class+"\" 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;
   var selectedProduct = document.UIservletForm["profile_lst"].options[selIndex].value;
   selectedProduct = trim(selectedProduct);
   if(selectedProduct == ""){
   		alert("You may move profile headings, but not delete them.");
   		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, searchArray, isExactMatchRequired){

	if(catalogDescrArray.length==0){
		buildCatalogArrays();
	}
	if(!criteria){
		criteria = document.UIservletForm["searchText"].value;
	}
	if(bShowAll=="false" &&  criteria==""){
		return;
	}
	if(!criteria){
		criteria = document.UIservletForm["searchText"].value;
	}
	if(searchArray == null || searchArray ==""){
		searchArray = catalogDescrArray;
	}
	if(isExactMatchRequired == null || isExactMatchRequired == ""){
		isExactMatchRequired = "false";
	}
	//empty the target
	document.UIservletForm["catalog_lst"].options.length=0;

	if (bShowAll == "true") {
		populateList(catalogCodeArray, catalogDescrArray, document.UIservletForm["catalog_lst"]);
		document.UIservletForm["searchText"].value = "";
		try { document.UIservletForm["profile_comm_values"].selectedIndex = 0; } catch (e1) { }
		try { document.UIservletForm["CLASSSEARCH"].selectedIndex = 0; } catch (e2) { }
	} else{
		resultIndex=0;
		
		for(x=0;x<searchArray.length;x++){
			if(isExactMatchRequired == "false" && searchArray[x].indexOf(criteria.toUpperCase())>=0){
				var o=new Option(catalogDescrArray[x],catalogCodeArray[x]);
				document.UIservletForm["catalog_lst"].options[resultIndex]=o;
				resultIndex++
			}else if(searchArray[x] == criteria) {
				var 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("|");
	catalogCommodityArray=document.UIservletForm["CATALOG_COMMODITY"].value.split("|");
	catalogClassArray=document.UIservletForm["CATALOG_CLASS"].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, criteria) {
	var code = 0;
	if (NS4){
		code = event.which;
	}else{
		code = event.keyCode;
	}
	if (code==13){
		event.keyCode=38;
		document.UIservletForm["profile_comm_values"].selectedIndex=0;
		profMaintSearch("false", criteria);
	}
}

function profileMaintSearch() {
	try{ document.UIservletForm.profile_comm_values.selectedIndex=0; }catch(e1){} 
	try{ document.UIservletForm["CLASSSEARCH"].selectedIndex=0; }catch(e2){} 
	profMaintSearch('false');               
}

function pmCommSearch(commodityCodeCriteria){
	profMaintSearch("false", commodityCodeCriteria, catalogCommodityArray, true);
	document.UIservletForm["searchText"].value="";
}

function pmClassSearch(classCodeCriteria){
	profMaintSearch("false", classCodeCriteria, catalogClassArray, true);
	document.UIservletForm["searchText"].value="";
}

