function searchType(type) {
	if (type == 'products') {
		$('searchField').value = 'Nourifusion, ShapeWorks, etc...';
	} else {
		$('searchField').value = 'Acne, Pregnancy, Dry Skin, etc...';
	}
}

function runSearch() {
	// lower-case
	term = $('searchField').value.toLowerCase();
	// normalize space
	term = term.replace(/^\s*|\s(?=\s)|\s*$/g, "");
	// alphanumericise
	term = term.replace(/\W/g, '-');
	// trim
	term = term.replace(/^-+|-+$/, '');
	// todo: run checks here
	document.location.href = "/search/"+term;
	return false;
}

// set className on each li tag in given ul
function classNameOnListItems (newClassName, listElement) {
	for (i=0;i<listElement.childNodes.length; i++) {
		var listChild = listElement.childNodes[i];
		if (listChild.tagName == 'LI') {
			listChild.className = newClassName;
		}
	}
}

function zipcode() {
	var zip = prompt('Please enter your Australian postcode:');
	if (zip!=null)
		window.location.href='/.zipcode.php?zip='+zip;
}

// execute javascript on page load... really
// usage: addLoadEvent(funcName); / addLoadEvent(function() { code_here });
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			if (oldonload) oldonload();
			func();
		}
	}
}

function hideDivByIdPrefix (idPrefix) {
	var tag = 'div';
	var liElements = document.getElementsByTagName(tag);
	for (i=0;i<liElements.length;i++) {
		if (liElements[i].id.substr(0,idPrefix.length)==idPrefix) {
			document.getElementById(liElements[i].id).style.display='none';
		}
	}
}

function hideOtherSubCategories (exceptionId) {
	var tag = 'div';
	var idPrefix = 'category_children';
	var liElements = document.getElementsByTagName(tag);
	for (i=0;i<liElements.length;i++) {
		if (liElements[i].id.substr(0,idPrefix.length)==idPrefix && liElements[i].id != exceptionId) {
				hideSubCategories(liElements[i].id);
		}
	}
}

function toggleSubCategories (parentId) {
	hideOtherSubCategories(parentId);
	Effect.toggle($(parentId), 'slide', {duration:0.5});
}

function hideSubCategories (parentId) {
	// (a) wait until any sliding is done (b) ensure that we're on display (c) toggle slide now that we know it'll go up
	setTimeout("if ($('"+parentId+"').style.display!='none') Effect.toggle($('"+parentId+"'), 'slide', {duration:0.5});", 600);
}

function fade (id, currentState) {
	if (currentState == 1) {
		new Effect.Fade(id, { duration: 1.0 });
	} else {
		new Effect.Appear(id, { duration: 1.0 });
	}
}