// (c)2008 uShip, Inc.

function setDropdown(dropDown,value){var numOptions=dropDown.options.length;for(i=0;i<numOptions;i++){if(dropDown.options[i].value==value){dropDown.options[i].selected=true;}}}
var CategorySelector=Class.create();CategorySelector.prototype={initialize:function(comArr,conElementList,txtCommodity1,txtCommodity2,conCommodityName,btnContinue,hidCommodityPath){this.commodityList=comArr;this.conElementList=conElementList;this.btnContinue=btnContinue;this.hidCommodity1=txtCommodity1;this.hidCommodity2=txtCommodity2;this.conCommodityName=conCommodityName;this.hidCommodityPath=hidCommodityPath;this.elemCollection=[];this.selectionPath=[];this.elemCollection.push($('dlist1'));this.btnContinue.disabled=true;if(this.hidCommodityPath.value.split(',').length>1){this.prefillPath(this.hidCommodityPath.value);}
else if(this.hidCommodity1.value>0){this.prefillSelection(parseInt(this.hidCommodity1.value,10));}},createListBox:function(level,callingElement){this.clearListBoxes(level);var option=this._getSelectedElement(callingElement);var parentId=option.value;this.selectionPath.push(parentId);this.hidCommodityPath.value=this.selectionPath.join(',');if(option.readAttribute('leaf')==1){var commodity=this.commodityList.find(function(commodity){return(commodity.id==parentId);});this.btnContinue.disabled=false;this.setCommodityName(commodity.n);this.hidCommodity1.value=commodity.id;$A(this.conElementList.getElementsByTagName('select')).each(function(select){var tag=this._getSelectedElement(select).readAttribute('tag');if(tag&&tag.length>0){this.hidCommodity2.value=tag;}}.bind(this));}
else if(option.readAttribute('leaf')==0){var nextLevel=level+1;this.btnContinue.disabled=true;this.setCommodityName(undefined);this.hidCommodity1.value='';this.hidCommodity2.value='';var htmlOptions=this.commodityList.select(function(commodity){return(commodity.p==parentId);}).map(function(commodity){var name=commodity.n;if(name.length>40){name=name.substring(0,40)+'...';}
if(commodity.l==0){name+=' >';}
return['<option value="',commodity.id,'" title="',commodity.n,'" leaf="',commodity.l,'" tag="',commodity.t,'" r="',commodity.r,'">',name,'</option>'].join('');});var newElem=document.createElement('div');newElem.id='dlist'+nextLevel;newElem.className='commodity-select';newElem.innerHTML+='<div class="q"><span class="req">*</span>Please specify:</div><select id="list'+nextLevel+'" size="14" onchange="myCategorySelector.createListBox('+nextLevel+', this)">'+htmlOptions.join('')+'</select>';this.conElementList.appendChild(newElem);this.elemCollection.push($(newElem));}},clearListBoxes:function(level){if(!level||level<1){level=1;}
this.elemCollection=this.elemCollection.select(function(elem,i){if(i>=level){elem.parentNode.removeChild(elem);}
return(i<level);});this.selectionPath.length=(level-1);},setCommodityName:function(name){if(name&&name.length>0){this.conCommodityName.innerHTML='You have selected <strong>'+name+'</strong>';this.conCommodityName.style.visibility='visible';}
else{this.conCommodityName.style.visibility='hidden';this.conCommodityName.innerHTML='';}},prefillSelection:function(commodityId){var ids=this._getParents(commodityId);this.clearListBoxes();for(var i=0,len=ids.length;i<len;i++){if(i>0){this.createListBox(i,$('list'+i));}
var listBox=$('list'+(i+1));var isSelected=false;for(var j=0,len2=listBox.options.length;j<len2&&!isSelected;j++){if(listBox.options[j].getAttribute("r")==ids[i]){listBox.options[j].selected=true;isSelected=true;}}}
this.createListBox(ids.length,$('list'+ids.length));},prefillPath:function(path){this.clearListBoxes();var selectionPath=path.split(',');selectionPath.each(function(value,index){var dd_commodity=$('list'+(index+1));var options=dd_commodity.options;var is_selected=false;for(var i=0,len=options.length;i<len&&!is_selected;i++){if(options[i].value===value){options[i].selected=true;is_selected=true;}}
this.createListBox(index+1,dd_commodity);}.bind(this));},_getParents:function(commodityId,arr){if(!arr){arr=[];}
arr.unshift(commodityId);var parentId=(this.commodityList.find(function(commodity){return(commodity.r==commodityId);})).p;if(parentId==0){return arr;}
else{return this._getParents(parentId,arr);}},_getSelectedElement:function(elemSelect){return $(elemSelect.options[elemSelect.selectedIndex]);}}