function validateSKUOrdering(totalFields) {
	var totalSelectedOptions = $("#skuOrdering select[value!='none']").length;
	var errorMessage = "";

	if ( totalSelectedOptions < totalFields ) {
		if ( totalFields == 2 ) {
			errorMessage = "You must make a selection for both product options.";	
		} else {
			errorMessage = "You must make a selection for all "+totalFields+" product options.";
		}
		alert(errorMessage);
		return false;
	} else {
		return true;
	}
}

function resetOptions(chainedTo,optionName,selectedOption) {

	if ( selectedOption == 0 ) {

		// Remove any previous quantity.
		$("#container_quantity").empty();
		
		// Remove any options in the dropdown.
		$("#"+optionName).parent("div").nextAll("div[id^='container_att']").find("select option:gt(0)").remove();
		$("#"+optionName).parent("div").nextAll("div[id^='container_att']").find("select").attr({"disabled":true});
	
	} else {

		$("#container_quantity").empty();
		
		$("#container_att"+chainedTo).nextAll("div[id^='container_att']").find("select option:gt(0)").remove();
		$("#container_att"+chainedTo).nextAll("div[id^='container_att']").find("select").attr({"disabled":true});
		
	}
	
}

function processOptions(optionName,optionValue,chainedTo) {

	var selectedOption = $("#"+optionName)[0].selectedIndex;
	var chainedToNum = chainedTo;

	resetOptions(chainedTo,optionName,selectedOption);

	if ( selectedOption != 0 ) {
	
		// If the current option being selected is not
		// the last option, process it as normal.
		
		if ( chainedTo != "getSKU" ) {
			
			// The dropdown that the current option
			// is chained to.
			var chainedTo = "#att"+chainedTo;
			
			// The default option text for the current
			// option.
			var attributeDefault = $(chainedTo+" option:eq(0)").text();
			
			// When we're getting options, set the option
			// text to "loading" status.
			$(chainedTo+" option:eq(0)").text("loading options...");
			
			// Start building the url parameters.
			var urlParams = "?doGetOptions=1";
			
			// Add the parent SKU.
			urlParams += "&parentSKU="+parentSKU;
			
			// Add the value of the selected option.
			urlParams += "&productAttributes="+optionValue;
			
			// The dropdown that the current option is
			// chained to.
			urlParams += "&chainedTo="+chainedTo;
			
			// The name of the current attribute.
			urlParams += "&attributeName="+optionName;
			
			// The number of the attribute
			urlParams += "&chainedToNum="+chainedToNum;
			
			// POST the data.
			$.post("skuOrdering/getOptions.php", { params : urlParams },
				function(data){
					// Set the option text back to the default option.
					$(chainedTo+" option:eq(0)").text(attributeDefault);
					
					// Remove any options that were already there.
					$(chainedTo+" option:gt(0)").remove();
					
					// Add the new options.
					$(chainedTo).append(data);
					
					// Debug to the console.
					//$("#console").append(data+"<br>");
					
					// Enable the dropdown so a selection can
					// be made.
					$(chainedTo).attr({"disabled":false});
					
					$(chainedTo+" option:first").attr({"selected":"selected"});
				});
			
		} else {
			
			$("#container_quantity").text("Checking Quantity...");
			var chainedTo = "#quantity";
			var urlParams = "?doGetOptions=1";
			urlParams += "&getSKU=1";
			urlParams += "&parentSKU="+parentSKU;
			urlParams += "&productAttributes="+optionValue;
			urlParams += "&attributeName="+optionName;
			$.post("skuOrdering/getOptions.php", { params : urlParams },
				function(data){
				
				// Remove any previous quantity.
				$("#container_quantity").empty();
				
				// Append new quantity information.
				$("#container_quantity").append(data);
				
				// Debug to the console.
				//$("#console").append(data+"<br>");
			});		
			
		}
	}
}
