//
// Dynamic inventory selector used by OneShoeTwoShoe.net
//
// Each shoe product page has an array that lists the size/width/color 
// combinations that are in stock.  We update the order button to be active
// or inactive based on whether or not the selection is in stock.
//

var distincts = new Array();

//-----------------------------------------------------------------------------
// Fill in the dropdown selectors with values from the array
//-----------------------------------------------------------------------------
function populate() {
    // run a loop for each criterion
    for (var i = 0; i < productOptions[0].length; i++) {
        distincts[i] = new Array();
        // for all of the product options
        for (var j in productOptions) {
            // if we haven't pushed it into the distinct array, push it good
            if (!in_array(productOptions[j][i], distincts[i])) {
                distincts[i].push(productOptions[j][i]);
            }
        }

		// Selectors might be numeric or alphabetic -- sort appropriately
        if (!isNaN(distincts[i][0] * 1)) {
            distincts[i].sort(function(a, b) { return a - b });
        } else {
            distincts[i].sort();
        }
    }

    // Update each selector widget with its options
    for (i in ids) {
        sel = document.getElementById(ids[i]);
        if (sel) {
            var selectedValue = sel.value;
            // empty it
            sel.options.length = 0;
            // start with a blank
            //sel.options[sel.options.length] = new Option('', '', ('' == selectedValue));
            for (j in distincts[i]) {
                sel.options[sel.options.length] = new Option(distincts[i][j], distincts[i][j], (distincts[i][j] == selectedValue));
            }
        }
    }
}

//-----------------------------------------------------------------------------
// Is the selected shoe available?
//-----------------------------------------------------------------------------
function in_array(needle, haystack) {
    for (key in haystack) {
        if (haystack[key] == needle) {
            return true;
        }
    }
    return false;
}

//-----------------------------------------------------------------------------
// Check the values of the selectors against the array and see whether the
// selected combination is in stock.
//-----------------------------------------------------------------------------
function available() {
    var goodctr = 0;
    for (var i = 0; i < productOptions.length; i++) {
        for (j in ids) {
            sel = document.getElementById(ids[j]);
            if (sel) {
                if (productOptions[i][j] == sel.options[sel.selectedIndex].value) {
                    goodctr++;
                }
            }
        }
        if (goodctr >= 4) {
            break;
        } else {
            goodctr = 0;
        }
    }
    if (goodctr >= 4) {
        return true;
    } else {
        return false;
    }
}

//-----------------------------------------------------------------------------
// Make the order button active or inactive
//-----------------------------------------------------------------------------
function disableOrderButton() {
    document.getElementById('orderbutton').disabled = true;	
}

function enableOrderButton() {
    document.getElementById('orderbutton').disabled = false;	
}

//-----------------------------------------------------------------------------
// Enable or disable the left order buttun
//-----------------------------------------------------------------------------
function checkLeft() {
	sel = document.getElementById("selectLeft");
	if (sel.selectedIndex == 0) {
	    document.getElementById('leftorderbtn').disabled = true;
		document.leftForm.size.value = "ERROR";
	} else {
	    document.getElementById('leftorderbtn').disabled = false;		
		document.leftForm.size.value = document.leftForm.selectLeft.value;
	}
}

//-----------------------------------------------------------------------------
// Enable or disable the left order buttun
//-----------------------------------------------------------------------------
function checkRight() {
	sel = document.getElementById("selectRight");
	if (sel.selectedIndex == 0) {
	    document.getElementById('rightorderbtn').disabled = true;	
		document.rightForm.size.value = "ERROR";
	} else {
	    document.getElementById('rightorderbtn').disabled = false;		
		document.rightForm.size.value = document.rightForm.selectRight.value;
	}
	
}

//-----------------------------------------------------------------------------
// Set the customer message
//-----------------------------------------------------------------------------
function setMessage(txt) {
    document.getElementById('ordermsg').innerHTML = txt;	
}

//-----------------------------------------------------------------------------
// The order button should only be active when the selected combination of
// size, width, and foot is in stock
//-----------------------------------------------------------------------------
function updateButton() {
    if (available()) {
		enableOrderButton();
        setMessage("Order this shoe");
    } else {
        var blank = false;
        for (j in ids) {
            sel = document.getElementById(ids[j]);
            if (sel) {
                if (sel.options[sel.selectedIndex].value == '') {
                    blank = true;
                    break;
                }
            }
        }

        if (!blank) {
            document.getElementById('ordermsg').innerHTML = "This shoe is not available";
        }
        disableOrderButton();
    }
}

