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 });
	}
}










// JavaScript Document

// DropDownMenu by Miha Hribar
// http://hribar.info

function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            oldonload();
            func();
        }
    }
}

function prepareMenu() {
    // first lets make sure the browser understands the DOM methods we will be using
  	if (!document.getElementsByTagName) return false;
  	if (!document.getElementById) return false;
  	
  	// lets make sure the element exists
  	if (!document.getElementById("menu")) return false;
  	var menu = document.getElementById("menu");
  	
  	// for each of the li on the root level check if the element has any children
  	// if so append a function that makes the element appear when hovered over
  	var root_li = menu.getElementsByTagName("li");
  	for (var i = 0; i < root_li.length; i++) {
  	    var li = root_li[i];
  	    // search for children
  	    var child_ul = li.getElementsByTagName("ul");
  	    if (child_ul.length >= 1) {
  	        // we have children - append hover function to the parent
  	        li.onmouseover = function () {
  	            if (!this.getElementsByTagName("ul")) return false;
  	            var ul = this.getElementsByTagName("ul");
  	            ul[0].style.display = "block";
  	            return true;
  	        }
  	        li.onmouseout = function () {
  	            if (!this.getElementsByTagName("ul")) return false;
  	            var ul = this.getElementsByTagName("ul");
  	            ul[0].style.display = "none";
  	            return true;
  	        }
  	    }
  	}
  	
  	return true;
}

addLoadEvent(prepareMenu);