/**
 * This file contains JavaScript functions being used on the Product template and Guided SKU template pages. 
 */

/*************************************************************************
 * Returns true if the given value is a valid positive number; returns false otherwise
 *************************************************************************/
function isPositiveWholeNumber(value) {
   	var regex = /^\d+$/;
   	value = value.toString();
   	return value.match(regex);
}
	
/*************************************************************************
 * Returns true if the given value is empty; returns false otherwise
 *************************************************************************/
function isEmpty(value) {
   	var regex = /.+/;
	value = value.toString();
	return !value.match(regex);
}


/*************************************************************************
 * Validates the domain name field.
 * Returns true if it is a valid URL, false otherwise.
 * If invalid, an error is rendered in the domain name's error section. 
 *************************************************************************/
function validateDomainName(isDomainCaptureRequired, isUpdate, updateCartItemDomainName) {
	// No need to validate if the product does not require a domain name.
	if (!isDomainCaptureRequired) {
		return true;
	}

	var validationErrorMessage;

	// AJAX call to validate the domain name.
	// This AJAX call is synchronous because the result is used by the caller of the validateDomainName() function.
	var domainName = document.getElementById("domainName").value;
	
	// Skip the domain name in-use check if this is an update and the user has not changed the domain name. 
	var skipInUseCheck = isUpdate && (updateCartItemDomainName == domainName);
	addToCartController.validateDomainName(domainName, skipInUseCheck, {
		async: false,
		callback: function(errMsg) {
			validationErrorMessage = errMsg;
			document.getElementById("domainName-error").innerHTML="<br/><span class='red' id='domainName-error-inner'>" + errMsg + "</span>";
		}
	});
	
	// Validation PASSED if there is no validation error message.
	return (validationErrorMessage === "");
}

/*************************************************************************
 * Pops up a window detailing validation errors that have occured on the page. 
 *************************************************************************/
function popUpErrors() {
	var languageError = document.getElementById('language-error-inner');
	var quantityError = document.getElementById('quantity-error-inner');
	var domainNameError = document.getElementById('domainName-error-inner');
	var errorMsg;
	
	if (null != languageError) {
		errorMsg = languageError.innerHTML;				
	}
	if (null != quantityError) {
		if (errorMsg == null) {
			errorMsg = quantityError.innerHTML;
		} else {
			errorMsg = errorMsg + '\n' + quantityError.innerHTML;
		}
	}
	if (null != domainNameError) {
		if (errorMsg == null) {
			errorMsg = domainNameError.innerHTML;
		} else {
			errorMsg = errorMsg + '\n' + domainNameError.innerHTML;
		}
	}
	
	if (errorMsg != null) {
		window.alert(errorMsg);
	}
}