


function Product(){

	
	this.weight=function() {
		var c=this.SelectedCombo();
		if (this.c)
			return this.baseweight*(1+c.weightadd_percent()/100)+c.weightadd_fixed();
		else
			return this.baseweight;
	}

	this.price=function() {
		var c=this.SelectedCombo();
		if (c)
			return this.baseprice*(1+c.priceadd_percent()/100)+c.priceadd_fixed();
		else
			return this.baseprice;
	}

		
	this.GetAvailableOptions=function(parameterid) {
		var tree=this.OptionTree;
		for (var p=0;p<this.ProductType().optionlist.length;p++) {
			
			var pp=this.ProductType().optionlist[p];
			if (pp==parameterid) return tree.a;
			if (tree.a[this.inputOptions[pp]]!=undefined)
				tree=tree.t[this.inputOptions[pp]];
			else if (tree.a[this.defaultOptions[pp]]!=undefined)
				tree=tree.t[this.defaultOptions[pp]];
			else {
				var first;
				for (var x in tree.a) {
					first=x;
					break;
				}
				tree=tree.t[first];
			}
		}
	}
	
	this.chosenvalue=function(parameterid) {
		return this.currentOptions[parameterid];
	}
	

	this.ProductType=function() {
		return GetProductType(this.producttypeid);
	}

	this.setSelected=function(comboid) {
		if (comboid) {
			this.selectedcomboid=comboid;
			for (var p in this.CurrentCombo().getOptionValues()) {
				this.currentOptions[p]=this.CurrentCombo().getOptionValues()[p];
			}
		}
	}
	
	this.GetComboMatchingSelection = function(selection) {
		var t=this.OptionTree;
		Optlist: for (var p=0;p<this.ProductType().optionlist.length;p++) {
			var parameterid=this.ProductType().optionlist[p];
			for (var s in selection) {
				s=selection[s];
				if (s[parameterid]!=undefined && t.a[s[parameterid]]!=undefined) {
					t=t.t[s[parameterid]];
					continue Optlist;
				}
			}
			var first;
			for (var x in t.a) {
				first=x;
				break;
			}
			t=t.t[first];
		}
		return AllOptionCombos[t.o];
	}
	
	this.SelectedCombo = function() {
		return this.GetComboMatchingSelection([this.inputOptions,this.defaultOptions]);
	}
	
	this.CurrentCombo = function() {
		return AllOptionCombos[this.selectedcomboid];
	}

		
	
}


function OptionCombo() {
	
	this.refcode=function() {return this.r;}
	this.priceadd_percent=function() {return this.pp;}
	this.priceadd_fixed=function() {return this.pf;}
	this.weightadd_percent=function() {return this.wp;}
	this.weightadd_fixed=function() {return this.wf;}
	this.stock=function() {return this.s;}
	this.disabled=function() {return this.d;}
	
}



function GetOptionG(optionid) {
	return dicAllOptions[optionid];
}

function ProductType() {
	this.ParameterNamed=function(name) {
		for (var p in this.parameterlist) {
			p=this.parameterlist[p];
			p=GetParameter(p);
			if (p.parametername.toLowerCase()==name.toLowerCase())
				return p;
		}
	}
}


function AllProductTypes() {
	return dicAllProductTypes;
}

function GetProductType(typeid) {
	return AllProductTypes()[typeid];
}

function parameterdef() {
}

var dicAllParameters;

function AllParameters() {
	return dicAllParameters;
}

function GetParameter(parameterid) {
	return AllParameters()[parameterid];
}

var dicOptionParameters;
function GetOptionParameters() {
	if (!dicOptionParameters) {
		dicOptionParameters=[];
		for (var p in AllParameters()) {
			var param=AllParameters()[p];
			if (param.parametertype=='opt' || param.parametertype=='bool') dicOptionParameters.push(p);
		}
	}
	return dicOptionParameters;
}

var dicFreeParameters;
function GetFreeParameters() {
	if (!dicFreeParameters) {
		dicFreeParameters=[];
		for (var p in AllParameters()) {
			var param=AllParameters()[p];
			if (param.parametertype=='text' || param.parametertype=='float') dicFreeParameters.push(p);
		}
	}
	return dicFreeParameters;
}

for (var p in productdata) Object.extend(productdata[p],new Product());
for (var t in AllProductTypes()) Object.extend(GetProductType(t),new ProductType());
for (var p in AllParameters()) Object.extend(GetParameter(p),new parameterdef());
for (var c in AllOptionCombos) Object.extend(AllOptionCombos[c],new OptionCombo());

