if(![].indexOf)Array.prototype.indexOf=function(needle){for(var i=0;i<this.length;i++)if(this[i]===needle)return i;return-1;}
var skuMaster,skuVariantAttribute,skuVariant;skuMaster=function(ID){this.ID=ID;this.Attributes=new Array();this.Variants=new Array();this.SelectedAttributes=new skuSelectedAttributes();this.VariantValueDivider='|';this.SelectedVariantIndex=-1;this.SortDropDowns=false;this.CompleteFunction;this.ResetFunction;}
skuMaster.prototype={addAttribute:function(ID,Label,Class){this.Attributes.push(new skuVariantAttribute(ID,Label,Class));},getAttributeLabel:function(ID,Label,Class){this.Attributes.push(new skuVariantAttribute(ID,Label,Class));},getAttributeClass:function(index){return this.Attributes[index].Class;},getAttributeID:function(index){return this.Attributes[index].ID;},getAttributeCount:function(){return this.Attributes.length;},addAttributeOption:function(index,value){this.Attributes[index].addOption(value);},writeAttributeOptionsToSelectList:function(index,definedArray){definedArray=definedArray||this.Attributes[index].Options;this.Attributes[index].writeOptionsToSelectList(definedArray,this.SortDropDowns);},writeAttributesOptionsToSelectList:function(index){for(var i=0;i<this.Attributes.length;i++){this.writeAttributeOptionsToSelectList(i);}},clearAttributeOptionsFromSelectList:function(index){this.Attributes[index].clearSelectList();},addVariant:function(ID,variant,extra){var newObject=new skuVariant();var a="ID";newObject[a]=ID;if(this.Attributes.length==1){this.addAttributeOption(0,variant)
a=this.Attributes[0].Label;newObject[a]=variant;}else{var variantValues=variant.split(this.VariantValueDivider);for(var i=0;i<this.Attributes.length;i++){this.addAttributeOption(i,variantValues[i])
a=this.Attributes[i].Label;newObject[a]=variantValues[i];}}
if(extra!=''){if(extra.indexOf(',')!=-1){extras=extra.split(',');for(var i=0;i<extras.length;i++){var pair=extras[i].split(':');newObject[pair[0]]=pair[1];}}else{var pair=extra.split(':');newObject[pair[0]]=pair[1];}}
this.Variants.push(newObject);},initialize:function(){if(typeof this.ResetFunction=='function')this.ResetFunction();var selectList=document.getElementById(this.Attributes[0].ID);for(var i=1;i<this.Attributes.length;i++){this.Attributes[i].disableSelectList();this.clearAttributeOptionsFromSelectList(i);}
this.writeAttributeOptionsToSelectList(0);},selectListChange:function(selectListID){for(var i=0;i<this.Attributes.length;i++){if(this.Attributes[i].ID!=selectListID)continue;var selectList=document.getElementById(this.Attributes[i].ID);this.SelectedAttributes[this.Attributes[i].Label]=selectList.value;for(var j=i+1;j<this.Attributes.length;j++){this.Attributes[j].disableSelectList();this.clearAttributeOptionsFromSelectList(j);}
if((i+1)!=this.Attributes.length){if(typeof this.ResetFunction=='function')this.ResetFunction();this.writeAttributeOptionsToSelectList(i+1,this.filterAttributeOptionsbySelection(i));if(this.SelectedAttributes[this.Attributes[i].Label]!='')this.Attributes[i+1].enableSelectList();}else{this.setSelectedVariantIndex();if(this.SelectedVariantIndex==-1){if(typeof this.ResetFunction=='function')this.ResetFunction();}else{if(typeof this.CompleteFunction=='function')this.CompleteFunction();}}
break;}},filterAttributeOptionsbySelection:function(index){var newArray=new Array();newArray[0]='';for(var i=index;i<index+1;i++){for(var j=0;j<this.Variants.length;j++){if(this.Variants[j][this.Attributes[i].Label]!=this.SelectedAttributes[this.Attributes[i].Label])continue;if(!this.Attributes[i+1])continue
if(newArray.indexOf(this.Variants[j][this.Attributes[i+1].Label])!=-1)continue;if(this.Attributes[i+1].Options.indexOf(this.Variants[j][this.Attributes[i+1].Label])==-1)continue;newArray.push(this.Variants[j][this.Attributes[i+1].Label]);}}
return newArray;},setSelectedVariantIndex:function(){this.SelectedVariantIndex=-1;for(var j=0;j<this.Variants.length;j++){for(var i=0;i<this.Attributes.length;i++){if(this.Variants[j][this.Attributes[i].Label]!=this.SelectedAttributes[this.Attributes[i].Label])break;if(i==this.Attributes.length-1)this.SelectedVariantIndex=j;if(this.SelectedVariantIndex!=-1)break;}
if(this.SelectedVariantIndex!=-1)break;}}}
skuVariantAttribute=function(ID,Label,Class){this.ID=ID;this.Label=Label;this.Class=Class;this.Options=new Array();this.selectList=document.getElementById(this.ID);this.sortFunction;this.formatFunction;}
skuVariantAttribute.prototype={addOption:function(value){if(this.Options.indexOf(value)==-1)this.Options.push(value);},delOption:function(value){},disableSelectList:function(){this.selectList.disabled=true;},enableSelectList:function(){this.selectList.disabled=false;},clearSelectList:function(){this.selectList.length=0;},writeOptionsToSelectList:function(definedArray,sort){definedArray=definedArray||this.Options;if(sort)definedArray.sort(this.getSortFunction());for(var i=0;i<definedArray.length;i++){var option=document.createElement("option");var myText=definedArray[i];if(typeof this.formatFunction!='undefined')this.FormatFunction(myText);option.text=definedArray[i];option.value=definedArray[i];try{this.selectList.options[this.selectList.length]=option;}
catch(exception){this.selectList.options[0]=option;}}},getSortFunction:function(){if(typeof this.sortFunction=='undefined')return sortLexAsc;if(typeof this.sortFunction=='function')return this.sortFunction;},getFormatFunction:function(){if(typeof this.formatFunction=='undefined')return;if(typeof this.formatFunction=='function')return this.formatFunction;}}
skuVariant=function(){}
skuSelectedAttributes=function(){}
function sortLexAsc(a1, b1){a=a1.toLowerCase();b=b1.toLowerCase();return ((a < b)?-1:((a>b)?1:0));}
function sortLexDesc(a1, b1){a=a1.toLowerCase();b=b1.toLowerCase();return ((a > b)?-1:((a<b)?1:0));}
function sortNumAsc(a,b){ var check = parseFloat(a.replace(/[^0-9.]+/g, '')); if (isNaN(check)) a ='0'; check = parseFloat(b.replace(/[^0-9.]+/g, '')); if (isNaN(check)) b ='0'; return parseFloat(a.replace(/[^0-9.]+/g, ''))-parseFloat(b.replace(/[^0-9.]+/g, '')); }
function sortNumDesc(a,b){ var check = parseFloat(a.replace(/[^0-9.]+/g, '')); if (isNaN(check)) a ='0'; check = parseFloat(b.replace(/[^0-9.]+/g, '')); if (isNaN(check)) b ='0'; return parseFloat(b.replace(/[^0-9.]+/g, ''))-parseFloat(a.replace(/[^0-9.]+/g, '')); }
function sortNone(a,b){return 0;}

