/* **************************************
Sebo JavaScripts
Copyright (c) 2008 by Sebo Marketing, All Rights Reserved
************************************** */






/* **************************************
Protect an email address from spam
************************************** */
function protectEmail(part1, part2) {
	var emailAddress = part1 + '@' + part2;
	document.write('<a href="mailto:' + emailAddress + '">' + emailAddress + '</a>')
}



/* **************************************
Clear default inputs in a form field (usually using onfocus)
************************************** */
function clearFormField(formField, initialValue){
	if (formField.value == initialValue)
	{
		formField.value = "";
	}
}

/* **************************************
Validate required form fields
************************************** */
function validate_required(field, defaultValue, alertText)
{
	with (field)
	{
		//alert("Field: " + value + " " + defaultValue);
        if (value == null || value == defaultValue || value == "")
			{
				alert(alertText);
				return false;
			}
		else {return true}
	}
}

/* **************************************
Validate newsletter signup fields
************************************** */
function validate_newsletter_form(thisform)
{
	with (thisform)
	{
		if (validate_required (newsletter_first_name, "FIRST", "Please enter your first name") == false)
		{
		    if(navigator.appName != "Microsoft Internet Explorer")
		    {
			    newsletter_first_name.focus();
			}
			return false;
		}		
		
		if (validate_required (newsletter_last_name, "LAST", "Please enter your last name") == false)
		{
		    if(navigator.appName != "Microsoft Internet Explorer")
		    {
			    newsletter_last_name.focus();
			}
			return false;
		}
		
		if (validate_required (newsletter_email, "EMAIL", "Please enter your email address") == false)
		{
			if(navigator.appName != "Microsoft Internet Explorer")
		    {
			    newsletter_email.focus();
			}
			return false;
		}
	}
}

/* **************************************
Bookmark the current page
************************************** */
function bookmarkThisPage() {
	if (window.sidebar) {
		window.sidebar.addPanel(document.title, window.location, "");
	} else if (window.external) {
		window.external.AddFavorite(window.location, document.title);
	}
}

/* **************************************
Output a bookmark link based on which browser you have
************************************** */
function bookmarkLinkText(anchor) {
	if (window.sidebar) {
		document.write("<a id=\"footerBook\" " + anchor + ">" + "Bookmark Page</a>");
	} else if (window.external) {
		document.write("<a id=\"footerBook\" " + anchor + ">" + "Add Page to Favorites</a>");
	}
}

/* **************************************
Function to stack up onload scripts
************************************** */

/* This handy addLoadEvent function from Simon Willison allows you to stack up 'window.onload' events 
without them stepping on each other's toes. It's explained here - http://www.sitepoint.com/blog-post-view.php?id=171578 */

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  }
  else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

/* **************************************
Handle external links and popups
************************************** */
function windowLinks() {
    if(!document.getElementsByTagName) {
         return;
    }
	
    var anchors = document.getElementsByTagName("a");
    for (var i = 0; i < anchors.length; i++) {
         var anchor = anchors[i];
         var relIndex = anchor.rel;
		 if (relIndex){
		 var relSplit = relIndex.split("|");    // Split our REL value into parts 
/* XHTML compliant target attribute */
		 if (relSplit[0] == "external") {       // If the REL=external...
            anchor.target = "_blank";           // set it's 'target' attribute to '_blank'
			if (anchor.className) {
				anchor.className += " external"; // add a CSS class to it to allow us to style it
			}
			else {
				anchor.className = "external";	// add a CSS class to it to allow us to style it
			}
			anchor.title = "Load in new window: "+ anchor.href;  // Add a new title attribute to warn the users of a new window
			
/* XHTML compliant popup attribute */
   			} else if (relSplit[0] == "popup") { // If the REL=popup...
			var strOptions="";
   			
   			anchor.className = "popup";          // attach a CSS class to it to allow us to style it
			// anchor.title = "Loads in a Popup Window"; // Add a new title attribute to warn the users of a new window
			anchor.popupWidth = relSplit[1]; 
			anchor.popupHeight = relSplit[2];
	        anchor.onclick = function() {strOptions="resizable=no, height="+this.popupHeight+",width="+this.popupWidth+", scrollbars=yes"; window.open(this.href, '', strOptions); return false;};
			}
		}
	}
}

/* **************************************
Used to open menu sections when clicked.
Works by attaching events to the desired objects. The events add and remove class names as necessary to expand and contract the nav.
************************************** */

function openMenu(link) {
	// First we take care of this class
	listItem = link.parentNode;
	if ((listItem.className == null) || (listItem.className == ""))
	{
		listItem.className = " over";
	}
	else if (listItem.className == "over")
	{
		listItem.className = "";
	}
	else if (listItem.className.indexOf(" over") >= 0)
	{
		listItem.className = listItem.className.replace(" over", "");
	}
	else
	{
		listItem.className += " over";
	}
	// Next we turn off all of the other list items
	var navContainer = document.getElementById("navigation");
	var navRoot;
	if (navContainer.childNodes[0].nodeName == "UL") navRoot = navContainer.childNodes[0];
	else navRoot = navContainer.childNodes[1];
	
	if (navRoot.childNodes.length > 0)
	{
		for (i=0; i < navRoot.childNodes.length; i++)
		{
			node = navRoot.childNodes[i];
			if ((node.nodeName == "LI") && (node != listItem))
			{
				if (node.className == "over")
				{
					node.className = "";
				}
				else if (node.className.indexOf(" over") >= 0)
				{
					node.className = node.className.replace(" over", "");
				}
				
				if (node.className == "current")
				{
					node.className = "";
				}
				else if (node.className.indexOf(" current") >= 0)
				{
					node.className = node.className.replace(" current", "");
				}
			}
		}
	}
}

function navAddBehavior() {
	var navContainer = document.getElementById("navigation");
	var navRoot;
	if (navContainer.childNodes[0].nodeName == "UL") navRoot = navContainer.childNodes[0];
	else navRoot = navContainer.childNodes[1];
	
	if (navRoot.childNodes.length > 0)
	{
		for (i=0; i < navRoot.childNodes.length; i++)
		{
			node = navRoot.childNodes[i];
			if (node.nodeName == "LI")
			{
				if (node.childNodes.length > 1)
				{
					node.childNodes[0].onclick = function() { openMenu(this); return false; };
				}
			}
		}
	}
}
			

/* **************************************
Stack up the onload events
************************************** */
addLoadEvent(function() {
	windowLinks();
	navAddBehavior();
});

/* **************************************
Validate a turing test question
************************************** */
function antiSpam(thisForm, result)
{
    if(thisForm.captcha.value == result)
    {
        return true;
    }
    else
    {
        alert("Please answer the question at the bottom correctly.");
        thisForm.captcha.focus();
        return false;
    }
}
