/**
 * prokineticsproducts.js
 * 
 * JavaScript library for prokineticsproducts.html.
 */

/**
 * Define constant for redirect URL and base query string
 */
var baseUrl = "http://www.1shoppingcart.com/SecureCart/SecureCart.aspx?mid=8D8C8380-1E77-418E-A8C4-2519B5A8AD04&pid=";

/**
 * Submits the user's order request to the shoppingcart.com order site.
 * 
 * @param theSelectElement
 *            The HTML SELECT element activated by the user
 */
function submitForm(theSelectElement)
{

	var thisSelectedIndex = theSelectElement.selectedIndex;
	if (thisSelectedIndex > 0)
	{
		var redirectUrl = baseUrl
				+ theSelectElement.options[thisSelectedIndex].value;
		location = redirectUrl;
	}
}

/**
 * Populates a second ("child") select element based on the option chosen from
 * the first ("parent") select element.
 * 
 * @param theParentSelectElement
 *            The first ("parent") select element.
 * @param theChildSelectElement
 *            The child element to be populated.
 */
function populateChildSelect(theParentSelectElement, theChildSelectElement)
{

	// Fetch selected option
	var parentSelectedID = theParentSelectElement.id;
	var parentSelectedIndex = theParentSelectElement.selectedIndex;
	var parentSelectedValue = theParentSelectElement.options[parentSelectedIndex].value;

	// Clear previous child options (but never remove the first option)
	// Use a descending iterator because ascending does not work properly
	var optionsLength = theChildSelectElement.options.length - 1;
	for ( var i = optionsLength; i > 0; i--)
	{
		theChildSelectElement.remove(i);
	}

	// Repopulate child options based on parent selection
	if (parentSelectedID == "perfectFitGenderSizeSelectField")
	{
		switch (parentSelectedValue)
		{
			case "women":
				addOption(theChildSelectElement, "No",
						"dc31a143af63bdbd90a1a494b031a964");
				addOption(theChildSelectElement, "Yes #1",
						"12c8b909c1ca22127213670f67744662");
				addOption(theChildSelectElement, "Yes #2",
						"6067796cf6f345200567f97bfc7b70c2");
				break;

			case "men":
				addOption(theChildSelectElement, "No",
						"37b0893ffea32132e698abdfc4b3a299");
				addOption(theChildSelectElement, "Yes #1",
						"5c81c9e82d6b71a42303539d7c8eae86");
				addOption(theChildSelectElement, "Yes #2",
						"9f927e1cc7b41831684e4eee517d864d");
				break;

			case "menXL":
				addOption(theChildSelectElement, "No",
						"25be2842fb76371b8decb22eeb91db43");
				addOption(theChildSelectElement, "Yes #1",
						"e8c0d2c5981c317d93c04fbc8b2d48c2");
				addOption(theChildSelectElement, "Yes #2",
						"ff7fe175b68cfb59935ebd043dddc95e");
				break;

			case "youth":
				addOption(theChildSelectElement, "No",
						"ab4fc5a167c0799b8b85a735609743b2");
				addOption(theChildSelectElement, "Yes #1",
						"f3a17bfc23d8ee9de857dbe6828a59d9");
				addOption(theChildSelectElement, "Yes #2",
						"877a79b77a528f8229e4511d4a5d09a3");
				break;
		}
	}
	else if (parentSelectedID == "postureControlGenderSizeSelectField")
	{
		switch (parentSelectedValue)
		{
			case "women3.5":
				addOption(theChildSelectElement, "No",
						"6056e3b0ccab8b981c813a71d458a9d4");
				addOption(theChildSelectElement, "Yes #1",
						"daa76302f07f4e841819e46a3576d8e1");
				addOption(theChildSelectElement, "Yes #2",
						"ab09446119730bc357f23f93b2a191f1");
				break;

			case "women6.0":
				addOption(theChildSelectElement, "No",
						"ddabd6d27ed9da0bb3a1534f1e7df67e");
				addOption(theChildSelectElement, "Yes #1",
						"44415c838589cafdd1533d4fdf58ec33");
				addOption(theChildSelectElement, "Yes #2",
						"9653ba0a24f50690083b4d2bd1e246f4");
				break;

			case "men3.5":
				addOption(theChildSelectElement, "No",
						"f1e985acfb572ab37e97bd7920070146");
				addOption(theChildSelectElement, "Yes #1",
						"c8ad129db802738be47cbf5dd00d5448");
				addOption(theChildSelectElement, "Yes #2",
						"13b22c639446839f3ed886b4cffaeb71");
				break;

			case "men6.0":
				addOption(theChildSelectElement, "No",
						"11236d902aa52640a22220097fd085ae");
				addOption(theChildSelectElement, "Yes #1",
						"58777dbee82c17d66e1e858394d64d54");
				addOption(theChildSelectElement, "Yes #2",
						"5e4fe1973c3bf2a485c81023ab910c95");
				break;

			case "menXL3.5":
				addOption(theChildSelectElement, "No",
						"eff2cdf2f5f9449ddc19948a92dbbdf9");
				addOption(theChildSelectElement, "Yes #1",
						"be098dfe7f1132baeb3860f9fce600fe");
				addOption(theChildSelectElement, "Yes #2",
						"b7ed321dfc50406d7b61489d1f7a97db");
				break;

			case "menXL6.0":
				addOption(theChildSelectElement, "No",
						"41ced65a98936ea62353c301541e1598");
				addOption(theChildSelectElement, "Yes #1",
						"b09d5d45b2462954f2317c43c39e6b25");
				addOption(theChildSelectElement, "Yes #2",
						"89d70f34f700d774fa1c44fd22d11e83");
				break;

			case "youth3.5":
				addOption(theChildSelectElement, "No",
						"e6b3aef5c0f1bf04c0ec8da28aff610f");
				addOption(theChildSelectElement, "Yes #1",
						"dbf08953f47ef0c03c952af1e332ab25");
				addOption(theChildSelectElement, "Yes #2",
						"587bc7403ddafdfed9eba3eea2ac30b8");
				break;

			case "youth6.0":
				addOption(theChildSelectElement, "No",
						"ae2b902616108770f97380f6d5d69b43");
				addOption(theChildSelectElement, "Yes #1",
						"4d75f59c84323573db9fa41c269b3e91");
				addOption(theChildSelectElement, "Yes #2",
						"4f1507fb031c2b1574460536350f343e");
				break;
		}
	}

	// Select the first option and focus on the element
	theChildSelectElement.selectedIndex = 0;
	theChildSelectElement.focus();
}

/**
 * Adds an option element to a select element.
 * 
 * @param theSelectElement
 *            The select element to add an option to.
 * @param theText
 *            The option text to display.
 * @param theValue
 *            The option value.
 */
function addOption(theSelectElement, theText, theValue)
{

	var option = document.createElement("option");
	option.text = theText;
	option.value = theValue;
	theSelectElement.add(option, null);
}

