
setInputValue = function(inputElement, inputValue) {
    logDebug('Entering setInputValue. setting', inputElement.id, 'to', inputValue);
    if (inputElement.tagName == 'INPUT' || inputElement.tagName == 'TEXTAREA') {
        inputElement.value = inputValue; 
    }
    else if (inputElement.tagName == 'SELECT') {
        for (var i=0; i<inputElement.options.length; i++) {
            if (inputElement.options[i].value == inputValue) {
                inputElement.options[i].selected = true;
            };
        };
    }
    else {
        logError('Unknown input tag: ' + inputElement.tagName);
    };
};

getInputValue = function(inputElement) {
    var inputValue = null;
    if (inputElement.tagName == 'INPUT' || inputElement.tagName == 'TEXTAREA') {
        inputValue = inputElement.value;
    }
    else if (inputElement.tagName == 'SELECT') {
        if (inputElement.selectedIndex != -1) {
            inputValue = inputElement.options[inputElement.selectedIndex].value;
        };
    }
    else {
        logError('Unknown input tag: ' + inputElement.tagName);
    };
    return inputValue;
};


optionBuilder = function(option) {
    logDebug("Entering optionBuilder:", option.value, option.label);
    return OPTION({value: option.value}, option.label);
};


buildOptionsFor = function(selectedAttr, relatedAttr) {

    logDebug("Entering buildOptionsFor:", 
        "selectedAttr", selectedAttr, 
        "relatedAttr", relatedAttr);

    var result = [];

    var selectedValue = getInputValue(getSelectElementFor(selectedAttr));
    var relatedValues = productAttributes[relatedAttr].values;

    for (var i=0; i<relatedValues.length; i++) {
        logDebug(i, "selectedValue", selectedValue, 
                    "relatedValue", relatedValues[i].value);
        var optionValue = selectedValue+","+relatedValues[i].value;
        optionValue = optionValue.split(",").sort()
        if(optionValue in availableOptionValues) {
            result.push(relatedValues[i]);
        };
    };
    return result;
};


getSelectElementFor = function(productAttr) {
    return getElement("attribute_"+productAttr);
};


updateOptionsRelatedTo = function(selectedAttr) {
    logDebug("Entering updateOptionsRelatedTo",
        "selectedAttr", selectedAttr);
    var relatedAttrs = [];
    for (var productAttr in productAttributes) {
        if (productAttr != selectedAttr) {
            relatedAttrs.push(productAttr);
        };
    };
    logDebug("relatedAttrs", repr(relatedAttrs));

    for (var i=0; i<relatedAttrs.length; i++) {
        var oldSelectElement = getSelectElementFor(relatedAttrs[i]);
        logDebug("building newSelectElement for", relatedAttrs[i]);
        var newSelectElement = 
            SELECT({
                id: oldSelectElement.id, 
                name: oldSelectElement.name, 
                onchange: oldSelectElement.onchange 
                }, 
                map(optionBuilder, buildOptionsFor(selectedAttr, relatedAttrs[i])));
        if (getInputValue(oldSelectElement)) {
            setInputValue(newSelectElement, getInputValue(oldSelectElement));
        };
        swapDOM(oldSelectElement, newSelectElement);
    };
};

/**
 * It's not necessary to verify if an item is in stock because we are already filtering it when the page loads
 */

/*
function isAvailable(selected_value_ids)
{
	var available = false;
	var elements = document.valueIdList.elements;
	for (var i = 0; i < elements.length; i++)
	{
		var element = elements[i];
		var available_value_ids = element.value.split(',');
		selected_value_ids.sort();
		available_value_ids.sort();

		if (selected_value_ids.toString() ==
	        available_value_ids.toString())
		{
			available = true;
			break;
		};
	};
	return available;
};


function verifyStockStatus()
{
	var selected_value_ids = [];
	var elements = document.productForm.elements;
	for (var i = 0; i < elements.length; i++) {
		var element = elements[i];
		if (element.name.indexOf('attribute_') != -1) {
			var value_id = element.value;
			var options = element.options;
			for (var j = 0; j < options.length; j++) {
				if (options[j].selected) {
					selected_value_ids.push(value_id);
				};
								}
		};
		
				};
	
	var isInStock = isAvailable(selected_value_ids);
	if (!isInStock) {
		alert("Unfortunately, this style is currently unavailable in the color/size combination you have chosen.  Please choose another combination for this style, or email us for future availability of this combination or other similar style suggestions.  We appreciate your business and look forward to helping you find the outfit you desire.");
	};
	return isInStock; // returning 'false' cancels productForm submission
};*/


function verifyStockStatus(){
	var selectValues = [],
		selects = $('.dependentSelect'),
		numOfSelects = $(selects).length;
				
	selects.each(function(){
		if(this.value != 'notpicked'){
			selectValues.push(this.value);	
		}
	});
	
	if (selectValues.length < numOfSelects) {
		alert("Unfortunately, this style is currently unavailable in the color/size combination you have chosen.  Please choose another combination for this style, or email us for future availability of this combination or other similar style suggestions.  We appreciate your business and look forward to helping you find the outfit you desire.");
		return false;
	}
	return true;
}


/** 
 * 
 *--fbuenano: this code has been replace with code at file /site/_js/dressdetail.js
jQuery(function() {
		jQuery("select.dependentSelect").change(function(event) {
			var currentAttributeID = jQuery(event.currentTarget).attr("id").replace("attribute_", "");
			var currentValue       = jQuery(event.currentTarget).val();

			/*
			 *  Go through all the other attributes and filter the options
			 /
			for (var productAttr in productAttributes) {
				if (productAttr != currentAttributeID) {
					var availableOptions = getAvailableOptions(productAttr, currentValue);

					setOptions("attribute_" + productAttr, availableOptions);
				}
			}

			showWin();
		});
})
*/


	/**
	 *   The outerHTML property returns the HTML that composes the whole element,
	 * unlike innerHTML which returns the HTML that composes of what is inside the element.
	 */
	jQuery.fn.outerHTML = function() {
	    return jQuery('<div>').append( this.eq(0).clone() ).html();
	}


	/**
	 *  For the given attribute return only those options that have the filter_value available
	 *
	 * @param {Object} attribute_to_filter_id ID of the attribute that we're filtering
	 * @param {Object} filter_value
	 */
	
	/*** This function has been replaced by function:  getAvailableOptions on file dressdetail.js */
	/*  
	function getSelectedAvailableOptions(attribute_to_filter_id, filter_value) {
		var allOptions = productAttributes[attribute_to_filter_id].values;
		var result     = [];

		
		if( filter_value == "notpicked") {
			return allOptions;
		}

		for(var i=0; i < allOptions.length; i++) {
			
			var optionKey = (filter_value + "," + allOptions[i].value).split(",").sort();
			if( availableOptionValues[ optionKey ] !== undefined ) {
				result.push( allOptions[i] );
			}
		}
		return result;
	}
	*/


	/**
	 *  Replace the options in the select element identified by id with the passed options
	 *
	 * @param {String} id
	 * @param {Array} options Array containing objects, each object needs to have value and label
	 */
	function setOptions(id, options) {
		var selectElem    = jQuery( "#" + id );

		if( selectElem.get(0).nodeName != "SELECT" ) {
			alert("This function can only be applied to SELECT elements");
		}

		var previousValue = selectElem.val();
		/*  The first option will always be the 'Please select' but we want to avoid writing it in 2 places  */
		var optionsHTML   = jQuery( "#" + id + " option:eq(0)" ).outerHTML();

		jQuery(options).each(function(index, item) {
			optionsHTML += '<option value="' + item.value + '">' + item.label + '</option>';
		});

		selectElem.html( optionsHTML )
			.val( previousValue );
	}

