			var types = [];

			var matrix = [];

			function addOption(selectbox,text,value ) {
				var optn = document.createElement("OPTION");
				optn.text = text;
				optn.value = value;
				selectbox.options.add(optn);
			}
			
			function setTypes(json) {
				types = json;
			}
			
			function setMatrix(json) {
				matrix = json;
			}
			
			//Get a descriptive text for a pulldown menu.
			function getDescription(type,isFirst) {
				if (isFirst=='undefined') {
					isFirst = false;
				}

				if (isFirst!==false) {
					description = "First select";
				} else {
					description = "Then select";
				}
				
				switch(type) {
					case "Color":
						description += " color";
						break;
					case "FlavorName":
						description += " flavor";
						break;
					case "HardwarePlatform":
						description += " platform";
						break;
					case "OperatingSystem":
						description += " operating system";
						break;
					case "PackageQuantity":
						description += " package quantity";
						break;
					case "Weight":
						description += " weight";
						break;
					case "Height":
						description += " height";
						break;
					case "Width":
						description += " width";
						break;
					case "RingSize":
					case "ClothingSize":
					case "Size":
						description += " size";
						break;
					case "Style":
						description += " style";
						break;
					default:
						var result = description.match(/[A-Z][a-z]*/g);	//Convert CamelCase type to words
						for( var i=0;i<result.length;i++) {
							description += ' ' + result[i].toLowerCase();;
						}
						break;
				}
				
				return description;
			}
			
			//Update the form after making a selection in one of the selection boxes.
			function selectItem() {
				var offerListingId = getSelectedOfferListingId();
				var dp = document.getElementById('dependentsubmit');
				var x = document.getElementById('CartOfferListingId');
				var y = document.getElementById('availability');
				var z = document.getElementById('VariationPrice');

				x.value = offerListingId;
				if (offerListingId==null) {
					//No offerListing was selected. Disable cart button.
					//dp.disable();
					dp.disabled = true;
					
					dp.value = 'Select options before adding to shopping cart';
					
					if (y!==null) {
						y.innerHTML = "";
					}
					
					if (z!==null) {
						z.innerHTML = "";
					}
				} else {
					//An offerListing was selected. Enable cart button.
					//dp.enable();
					dp.disabled = false;
					
					dp.value = 'Add to shopping cart';
					
					if (y!==null) {
						var availability = getAvailability(offerListingId);
						
						if (availability!=false) {
							y.innerHTML = availability;
						} else {
							y.innerHTML = "";
						}
					}

					if (z!==null) {
						price = getPrice(offerListingId);
						
						if (price!=false) {
							z.innerHTML = price;
						} else {
						alert('z!');
							z.innerHTML = "";
						}
					}
				}
			}
			
			function getPrice(offerListingId) {
				for(var i=0;i<matrix.length;i++) {
					if (matrix[i].offerListingId == offerListingId) {
						return matrix[i].price;
					}
				}
				
				return false;
			}
			
			function getAvailability(offerListingId) {
				for(var i=0;i<matrix.length;i++) {
					if (matrix[i].offerListingId == offerListingId) {
						return matrix[i].availability;
					}
				}
				
				return false;
			}
			
			function getSelectedOfferListingId() {
				var values = [];
				var offerListingId = null;
				
				if (types!='undefined'&&matrix!='undefined'
					&&types.length>0&&matrix.length>0) {	//Find selected path in matrix
					
					for( var i=0;i<types.length;i++ ) {
						var p = types[i];
						var z = document.getElementById(p);
												
						//Not all boxes were filled out.
						if (!(z.selectedIndex>-1)) {
							return null;
						}
						
						var value = z.options[z.selectedIndex].value;

						eval('values[i] = {"' + p + '":"' + value + '"}');
					}
					
					var tmp = matrix;
					var b = tmp.length-1;
					while(b>=0&&offerListingId==null) {	//Walk through the matrix and selected values, and determine selected path.
						for( var j=0;j<types.length;j++) {
							eval('var x = tmp[b].' + types[j]);
							eval('var y = values[j].' + types[j]);

							if (x==y) {
								if (j==types.length-1) {
									offerListingId = tmp[b].offerListingId;
								}
							} else {
								break;	//nodes weren't equal, so find new path.
							}
						}
						
						b--;
					}
				}
				
				return offerListingId;
			}

			//Generate a pulldown menu for a certain type (e.g. ClothingSize, Color)
			function generateForm(type) {
				var x = document.getElementById(type);	//Find the corresponding element in the HTML.

				if (x.options!='undefined') {	//If select box already contains options, clear them.
					x.options.length = 0;
				}

				var i = 0;
				var j = 0;
				var bad = false;
				while( i<types.length && types[i]!=type && !bad ) {	//Check if a selection was made in preceding fields
					var y = document.getElementById(types[i]);

					if (y.selectedIndex<=0) {
						bad = true;
					}

					i++;
				}

				j = i;
				
				var isFirst = (i==0);	//Is this the first selectbox?
				var u = document.getElementById(type);

				//If previous fields weren't filled out, don't generate the new pulldown menu.
				if (!bad) {					
					var values = [];
					var q = 0;
					var i = 0;	//Save preceding selections to values matrix.
					for( i=0;i<j;i++ ) {
						var z = document.getElementById(types[i]);
						var p = types[i];
						var value = z.options[z.selectedIndex].value;

						eval('values[i] = {"' + p + '":"' + value + '"}');
					}
				
					//Add description value.
					var text = getDescription(type,isFirst);
					addOption(u,text,'0');

					//Add options to the selection box.
					for( i=0;i<matrix.length;i++ ) {
						var add = true;	
						for( var t=0;t<j;t++ ) {	//Check what possible options exist in the current path. And add them to the selection box if they do. (walk through every type on the current line of matrix and values)
							var tmp = types[t];

							eval('mVal = matrix[i].' + tmp);
							eval('vVal = values[t].' + tmp);
	
							if (mVal!=vVal) {	//Does this path exist in the variation matrix?
								add = false;
							}
						}

						if (add) {							
							var escape = false;
							for (z=0;z<u.options.length;z++) {	//Does the value already exist? Don't add.
								if (u.options[z].value==eval('matrix[i].' + type)) {
									escape = true;
									break;
								}
							}
							
							if (!escape) {
								eval('var a = matrix[i].' + type);
								
								addOption(u,a,a);
							}
							
							q++;
						}
					}
					
					//u.show();
					u.style.display = "block";
				} else {
					//u.hide();
					u.style.display = "none";
				}
				
				x.enabled = !bad;
			}
