/**
 * 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=5347DA46-034B-42D7-BE0C-30E9B2F38FA9&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",
						"a1292272b712fac31a51d0136cd099f0");
				addOption(theChildSelectElement, "Yes #1",
						"3f43e3d09d568860a4e7e543cda4bd37");
				addOption(theChildSelectElement, "Yes #2",
						"184485e2962fda8e29c3f516c179a284");
				break;

			case "men":
				addOption(theChildSelectElement, "No",
						"dd2948bff58de40fc22a4bbbc477ef30");
				addOption(theChildSelectElement, "Yes #1",
						"1420c48da3e1f0a240dab4cf42c56ea1");
				addOption(theChildSelectElement, "Yes #2",
						"b8acf8ee548149d78158a002e2c663fc");
				break;

			case "menXL":
				addOption(theChildSelectElement, "No",
						"6e2a982c80bdb32b3e69fa7d75e81cec");
				addOption(theChildSelectElement, "Yes #1",
						"fc89b09ee3bdd270436f7ce99988b764");
				addOption(theChildSelectElement, "Yes #2",
						"73debc3f101f9366dae47414edd13915");
				break;

			case "youth":
				addOption(theChildSelectElement, "No",
						"e5738afba8e695c73e36aba45a0d0b8a");
				addOption(theChildSelectElement, "Yes #1",
						"6fe4d103c34a979e803078c6acac4476");
				addOption(theChildSelectElement, "Yes #2",
						"7692a647e79565d296a10880f58cf993");
				break;
		}
	}
	else if (parentSelectedID == "postureControlGenderSizeSelectField")
	{
		switch (parentSelectedValue)
		{
			case "women3.5":
				addOption(theChildSelectElement, "No",
						"30cb9b66ccaf9c43c92828b2876f806d");
				addOption(theChildSelectElement, "Yes #1",
						"586966eae2c74ca8991d66e48432a3e4");
				addOption(theChildSelectElement, "Yes #2",
						"61825a356815dfbc956907e5e3f44261");
				break;

			case "women6.0":
				addOption(theChildSelectElement, "No",
						"383afc1383d36fe7faf452861053f74e");
				addOption(theChildSelectElement, "Yes #1",
						"3a8baeccbf3ed8cc1143995f057118af");
				addOption(theChildSelectElement, "Yes #2",
						"b41e7b41ae87e65e372d179cfcb44ae9");
				break;

			case "men3.5":
				addOption(theChildSelectElement, "No",
						"5c3ee811175660a68a641d5d1b31b091");
				addOption(theChildSelectElement, "Yes #1",
						"7a23f5833bcf7798d5e5b5e1d11e0324");
				addOption(theChildSelectElement, "Yes #2",
						"c9177186323334f6f0234fb0350fcf5c");
				break;

			case "men6.0":
				addOption(theChildSelectElement, "No",
						"bd5bd58c7a41f4048c853dff992de21e");
				addOption(theChildSelectElement, "Yes #1",
						"9ec82c513c6482aa76e624cc96f6a9ef");
				addOption(theChildSelectElement, "Yes #2",
						"855058b88d8126bb4941d88afa20aaca");
				break;

			case "menXL3.5":
				addOption(theChildSelectElement, "No",
						"dcf37b9148c612275e11ad3c3f0dfba4");
				addOption(theChildSelectElement, "Yes #1",
						"3062b8f1c23765a5387f19f2d19a13a9");
				addOption(theChildSelectElement, "Yes #2",
						"45fe9e7c9cae6d4066ba81f1631835e5");
				break;

			case "menXL6.0":
				addOption(theChildSelectElement, "No",
						"17578035aefbfeedaaaa9f66898e7e5a");
				addOption(theChildSelectElement, "Yes #1",
						"11a50f675a381aa33f7d9cb95a71ebb3");
				addOption(theChildSelectElement, "Yes #2",
						"88acf8857b7fbd4e947774bb1a98ff37");
				break;

			case "youth3.5":
				addOption(theChildSelectElement, "No",
						"cf6be9764dc9bb207212ca2987e7362d");
				addOption(theChildSelectElement, "Yes #1",
						"1879cc5478d9eec6f3ac65f9bf22e2fa");
				addOption(theChildSelectElement, "Yes #2",
						"efa03baa5729c751b22d63302a9f1f0a");
				break;

			case "youth6.0":
				addOption(theChildSelectElement, "No",
						"8d8dac4b33a279d9987ed5c7a217c535");
				addOption(theChildSelectElement, "Yes #1",
						"7afefe387baac11d79669d19f368193e");
				addOption(theChildSelectElement, "Yes #2",
						"26384b48044e45cfcc4af90ee2045872");
				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);
}
