function trim(s) {var t = s; var ch = t.substring(0, 1); while (ch == " ") {t = t.substring(1, t.length); ch = t.substring(0, 1);} ch = t.substring(t.length-1, t.length); while (ch == " ") {t = t.substring(0, t.length-1); ch = t.substring(t.length-1, t.length);} return t;}
function right(s, l) {return s.substring(s.length-l);}
function left(s, l) {return s.substring(0, l);}
function listLen(list, delimiter) {	
	var listSub = list.substring(list.indexOf(delimiter) + 1, list.length);
	var numAttrs = 1;
	while(1)
	{
		listSub = listSub.substring(listSub.indexOf(delimiter) + 1, listSub.length);
		numAttrs += 1;
		if(listSub.indexOf(delimiter) < 0)
			break;
	}
	return numAttrs;
}
function listGetAt(list, listpos, delimiter) {var p, n; var L = list; if (listpos > 0) {p = list.indexOf(delimiter); if (p >=0) {if (listpos > 1) {n = right(list, list.length - p - 1);L = listGetAt(n, listpos - 1, delimiter);} else {L = left(list, p);}}} return L;}
function find0(s, a) {var ret = -1; for (var i=0; i<a.length; i++) {if (a[i].toUpperCase()==s.toUpperCase()) {ret = i; break;}} return ret;}
function colorComp(a, b) {if (a.color < b.color) return -1; if (a.color > b.color) return 1; return 0;}
var currQty; var colorSelect;
var attrLen = listLen(attrs, ',');
var sizes  = new Array("XS (0-6mo)",   "XSmall",    "X-Small",     "Small", "S (1-2yr)",    "Medium", "M (2-3yr)", "M (2-4yr)",     "Large", "L (3-4yr)",   "X-Large",    "XLarge");
var colors = new Array( new Array(), new Array(), new Array(), new Array(), new Array(), new Array(), new Array(), new Array(), new Array(), new Array(), new Array(), new Array());
var allColors = new Array();
for (var i=1; i<attrLen+1; i++) {
        var li = listGetAt(attrs, i, ',');
        var size = trim(listGetAt(li, 1, '/'));
        var colorCount = li.substring(listGetAt(li, 1, '/').length+1, li.length);
        var color = listGetAt(colorCount, 1, '(');
        var count = listGetAt(colorCount, 2, '(');      count = count.substr(0, count.length-1);
        if (count>0) {
                var icolor = color.toLowerCase().replace(/\s/, '_');
                if (find0(icolor, allColors) < 0) {allColors[allColors.length]=icolor; allColors.sort();}
                var sizeIndex = find0(size, sizes);
                if (sizeIndex < 0) {sizes[sizes.length]=size; colors[colors.length]=(new Array()); sizeIndex = sizes.length-1;}
                var colorList = colors[sizeIndex];
                var o = new Object(); o.color = color; o.count = count; colorList[colorList.length]=o; colorList.sort(colorComp);
                colors[sizeIndex] = colorList;
        }
}
function chooseColor(color) {
        var selectSize = color.form.select_size;
        var o = color.options[color.selectedIndex];
        var size = selectSize.options[selectSize.selectedIndex].text;
        var qty = color.form.qty.value;
        currQty = parseInt(o.value);
        color.form.color.value=o.text;
        color.form.size.value=size;
}
function setColors(size) {
        var currColor = (colorSelect.options.length > 0) ? colorSelect.options[colorSelect.selectedIndex].text : '';
        colorSelect.options.length = 0;
        var sizeIndex = find0(size.value, sizes);
        if (sizeIndex < 0) {
                var o = new Option(); o.text = 'None'; o.value = 'None'; colorSelect.options[colorSelect.length] = o;
        } else {
                var colorList = colors[sizeIndex];
                if (colorList.length) {for (var i=0; i<colorList.length; i++) {var o = new Option(); o.text = colorList[i].color; o.value = colorList[i].count; colorSelect.options[colorSelect.length] = o; if(o.text == currColor) {o.selected = true}}} else {var o = new Option(); o.text = '-- None --'; o.value = '-- None --'; colorSelect.options[colorSelect.length] = o;}
        }
        chooseColor(colorSelect);
}
function checkQty() {
        if(parseInt(this.qty.value) > currQty) {
                var url = "/stores/barefootyoga/images/Test/popup.htm?<font face=arial,helvetica,sans-serif size=2>Sorry, we have only "+currQty+" of this item in stock. Please enter another quantity.</font><br><br>";
                var w = window.open(url, "currQty", "height=200,width=400,resizable=no,scrollbars=no,top=200,left=150");
                w.focus();
                return false;
        }
}
function setSizes(form) {
        sizeSelect = form.select_size;
        colorSelect = form.select_color;
        var none = true;
        sizeSelect.options.length = 0;
        for (var i=0; i<sizes.length; i++) {var colorList = colors[i]; if (colorList.length) {none = false; var o = new Option(); o.text = sizes[i]; o.value = sizes[i]; sizeSelect.options[sizeSelect.length] = o;}}
        if (none) {var o = new Option(); o.text = 'None'; o.value = 'None'; sizeSelect.options[sizeSelect.length] = o;}
        setColors(sizeSelect);
        sizeSelect.form.onsubmit = checkQty;
}
function showColors(pre, suf, hspace, vspace) {
        var colorImgs = "";
        if (suf==null) suf = ".gif";
        if (hspace==null) hspace = "0";
        if (vspace==null) vspace = "0";
        for (var i=0; i<allColors.length; i++) {
                colorImgs += '<img src="'+pre+allColors[i]+suf+'" hspace="'+hspace+'" vspace="'+vspace+'">';
        }
        document.write(colorImgs);
}

