var submitSearchBusy = false;
function submitSearch() {
	var exists = false;
	document.AdvancedSearchForm.searchCriteria.value = document.AdvancedSearchForm.searchCriteria.value.replace(/s+$/g, '');
		//below line trims and drops toLowerCase
	var searchCriteria = $.trim(document.AdvancedSearchForm.searchCriteria.value.toLowerCase());
	if (searchCriteria == 'treadmills') {
			searchCriteria = 'treadmill';
		} else if (searchCriteria == 'ellipticals') {
			searchCriteria = 'elliptical';
		} else if (searchCriteria == 'bikes') {
			searchCriteria = 'bike';
		} else if (searchCriteria == 'exercise bikes') {
			searchCriteria = 'exercise bike';
		} else if (searchCriteria == 'skiers') {
			searchCriteria = 'skier';
		} else if (searchCriteria == 'systems') {
			searchCriteria = 'system';
		} else if (searchCriteria == 'strength systems') {
			searchCriteria = 'strength system';
		} else if (searchCriteria == 'stair steppers') {
			searchCriteria = 'stair stepper';
		} else if (searchCriteria == 'steppers') {
			searchCriteria = 'stepper';
		} else if (searchCriteria == 'strollers') {
			searchCriteria = 'stroller';
		} 
		//regex for standard sku such as abc12345 and a possible trailing alpha such as Z, like: NTEL4255Z
		else if (searchCriteria.match(/^([a-z]{3,5})?[0-9]+([a-z]{0,})$/)) {
			exists = true;
		}
		//regex for standard sku plus alpha and number such as WGG5600L06 WGG5600X06 WGG5600S06 WGG5600M06
		else if (searchCriteria.match(/^([a-z]{3,7})([0-9]{1,})(([l||m||s||x]{1}))([0-9]{2})$/)) {
			exists = true;
		}
		//regex for pilates or yoga kits such as WDN8PK08 and WDN8YK08
		else if (searchCriteria.match(/^([a-z]{3,7})([0-9]{1})(([p||y]{1})([k]{1}))([0-9]{2})$/)) {
			exists = true;
		}
		//regex for Jillian cross bar JKIT09.1 and ultimate powerbell JAKB2009.1 with a little room to grow
		else if (searchCriteria.match(/^([a-z]{4,5})([0-9]{1,5})([.]{1})([0-9]{1,2})$/)) {
			exists = true;
		}
		//regex for Golds Gym 7-foot bar WGGOCB7P09 and WGGOCB7P06 or ther similarly formatted sku
		else if (searchCriteria.match(/^([a-z]{5,7})([0-9]{1})([p]{1})([0-9]{1,2})$/)) {
			exists = true;
		}
		//regex for Weight lifting belt WGGBLBLX06 and WGGBLBSM06 or other similarly formatted skus
		else if (searchCriteria.match(/^([a-z]{8})([0-9]{1,2})$/)) {
			exists = true;
		}
		//regex for Weight lifting belt WGGBLD09 or GGMPSLS002
		else if (searchCriteria.match(/^(([w]{1,})||([g]{1,}))([a-z]{6,7})([0-9]{1,5})$/)) {
			exists = true;
		}
		if(exists) { // if one of the above regex is matched submit the sku to the search
			document.AdvancedSearchForm.sku.value = searchCriteria;
			document.AdvancedSearchForm.searchCriteria.value = '';
		}else{ 
		document.AdvancedSearchForm.searchTerm.value = searchCriteria;
	}
	document.AdvancedSearchForm.submit();
}

var submitCompareBusy = false;
function submitCompare(form) {
	if (!submitCompareBusy) {
		submitCompareBusy = true;
		var count = 0;
		if (form.compare.length == null) {
			if (form.compare.checked) {
				count++;
			}
		} else {
			for (var i=0, length=form.compare.length; i<length; i++) {
				if (form.compare[i].checked) {
					count++;
				}
			}
		}
		if (count == 0) {
			alert('Please select up to 3 products to compare!');
			submitCompareBusy = false;
		} else if (count > 3) {
			alert('You may only select a maximum of 3 products to compare!');
			submitCompareBusy = false;
		} else {
			form.submit();
		}
	}
}

