/**
 * This file contains JavaScript functions being used on the Guided SKU template page. 
 */ 

/*************************************************************************
 * Given an element name of a radio button group, dropdown box, or hidden input field, this function will
 * return the value of the selected option; or null if no option is selected. 
 *
 * For example, if getControlValue("radioButtonGroupName") is called and "radioButtonGroupName" is a radio
 * button group name, the value of the selected radio button will be returned. 
 *************************************************************************/
function getControlValue(controlName) {
	var controls = document.getElementsByName(controlName);
	if (controls[0].type == "select-one") {
		return getSelectedDropdownValue(controls[0]);
	} else if (controls[0].type == "radio") {
		return getCheckedRadioValue(controls);
	} else if (controls[0].type == "hidden") {
		var hiddenValue = controls[0].value;
		if (hiddenValue.length == 0) {
			return null;
		}
		return hiddenValue;  
	}
	
	// Unsupported control type; return null
	return null;
}

/*************************************************************************
 * Returns the checked radio option in the radio group indicated by the given group name.
 *************************************************************************/
function getCheckedRadioValue(radioButtonsArray) {
	// Scan through each radio button and see if it is 'checked'
	for (var i = 0; i < radioButtonsArray.length; i++) {
		if (radioButtonsArray[i].checked) {
			return radioButtonsArray[i].value;
		}
	}
	// None of the radio buttons were checked; return null
	return null;
}

/*************************************************************************
 * Returns the selected SkuOptionValue in the combobox
 *************************************************************************/
function getSelectedDropdownValue(combobox) {
	var selectedValue = combobox.options[combobox.selectedIndex].value;
	// Value was an empty string, return null
	if (selectedValue.length == 0) {
		return null;
	}
	return selectedValue;
}

/**************************************************************************
 * Sets the hidden input specifying the SKU code used by the AddToCartController to the given skuCode value. 
 *************************************************************************/
function setSkuCode(skuCode) {
	document.getElementById("skuGuid").value = skuCode;
}

/*************************************************************************
 * Validates the form.
 *************************************************************************/
function validateForm(isDomainCaptureRequired, isUpdate, updateCartItemDomainName) {
	var isSkuOptionValueControlsValid = validateSkuOptionValueControls();
	var isQuantityValid = validateQuantity();
	var isDomainNameValid = validateDomainName(isDomainCaptureRequired, isUpdate, updateCartItemDomainName);
	
	return isSkuOptionValueControlsValid && isQuantityValid && isDomainNameValid;
}

/*************************************************************************
 * Submits the skuSelectForm to the AddToCartController.
 *************************************************************************/
function addToCart(isDomainCaptureRequired, isUpdate, updateCartItemDomainName) {
	// The SKU code that is submitted should already be set correctly, but let's
	// refresh it right before submitting just to be sure.
	setSkuCode(lookupSku());
	
	// Submit the form only if validation passes	
	if (validateForm(isDomainCaptureRequired, isUpdate, updateCartItemDomainName)) {
		document.skuSelectForm.submit();
	} else {
		popUpErrors();
	}
}