// boilerplate functions.  separate these?
	function clearEl(el){
	    while (el.hasChildNodes && el.firstChild != null) el.removeChild(el.firstChild);
	}
	function contains(col, item){
		for (var i in col){
			if (col[i] == item)
				return true;
		}
		return false;
	}
	
	// end boilerplate functions
	
	xAddEventListener(window,'load',function(){
	for (i in features){
		color = false;
		prodfeat = features[i];
		if (prodfeat instanceof String && prodfeat != 'COLOR') {
			setOptions(prodfeat, variants[i]);
			break;
		}
		
		if (! contains(prodfeat, 'COLOR'))
			setOptions(prodfeat[0]+"_"+i, variants[i]);
		else{
			if (prodfeat[0] == 'COLOR'){
				l=0;
				for (c in variants[i]) l++;
				if (l > 0){
					selectOnlyColor(i);
				}
			}
		}
	}
});

	function selectOnlyColor(id){
		for (i in variants[id]){
			selGroup(id, i); break;
		}
	}	
	
	
	var colorSelections = new Object();
	
	function selGroup(id, c){
		var disp = $(id+"colorDisplay");
		if (disp){
			clearEl(disp);
			disp.innerHTML = c;
		}
		var button = $(id+":"+c);
		
		if (colorSelections[id]!= null)
			colorSelections[id].className = "color-box-gtl-notselected";
			
		colorSelections[id] = button;
		button.className="active";
	    sizes = variants[id][c];
	    setOptions("SIZE_"+id, sizes);
	}
	
	function isNumeric(x){
		var RegExp = /^(-)?(\d*)(\.?)(\d*)$/;
		return x.match(RegExp);
	}
	
	var alphaSizes = ['XXS', 'XS', 'S', 'S/M', 'M', 'M/L', 'L', 'XL', 'OSF', 'Small','Medium','Large'];
	
	function ind(arr, val){
		for (v in arr){
			if (arr[v] == val)
				return v; 
		}
	}
	
	
	function getSortedKeySet(items){
		var num = false;
		var out = new Array();
		for (i in items){
			if (isNumeric(i)) num = true;
			out.push(i);
		}
		if (num)
			return out.sort(function(a,b){return a-b; });  // only sort numeric keys
		else
			return out.sort(function(a,b){ return ind(alphaSizes, a) - ind(alphaSizes, b); })
	}
	
	function setOptions(id, items){
		var sel = $(id)
		if (! sel) return;
	    clearEl(sel);
	    var keys = getSortedKeySet(items);
	    for (var i=0;i<keys.length; i++){
	        var s = document.createElement("option");
	        s.appendChild(document.createTextNode(keys[i]))
	        s.setAttribute("value", items[keys[i]])
	        sel.appendChild(s);
    	}
	}
	
	function toCart(skus){
		if (! skus) return;
		document.location = "/cache/control/additems?"+skus.join('&');
	}
	function toWishList(skus){
		if (! skus) return;
		skus.push("productStoreId=${productStoreId}");
		
		document.location = "/cache/control/addToWishListBulk?"+skus.join("&");
	}
	function getItems(){
		var skus = new Array();
		for (id in variants){
			if ($("selected_"+id).checked){
				var sku;
				if (variants[id] == "NONE"){
					sku = id;
				}else{
				 	sku = $("SIZE_"+id).value;
				}
			 	var q = $("quantity_"+id).value;
			 	if (sku == "NULL" || q == "NULL"){ 
					notify(); return null;
			 	}
			 	skus.push("quantity_"+sku+"="+q);
			}
		}
		if (skus.length == 0) {
			alert("You haven't selected any products.");
			return null;
		}
		return skus;
	}

function notify(){
	alert("Please select all of the required options."); 
}
