/* =====================================================================	
	natives.js
========================================================================
   	Author: James Dacosta
   	Date:	09/01/08
------------------------------------------------------------------------
	Comments
------------------------------------------------------------------------
   	
   	Javascript functions used within Natives
   	
   	
========================================================================*/ 




// addLoadEvent function as seen at http://simon.incutio.com/archive/2004/05/26/addLoadEvent
// allows you to stack functions and apply them to the onload event and also means you 
// can abstract onload functions from the html
// I've added an argument 'arg' for onload functions with an argument
// Could probably be better implemented with 'arguments[]'

function addLoadEvent(func,arg) {
	
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func(arg);
    }
  }
}


 
/*
	Copyright Robert Nyman, http://www.robertnyman.com
	Free to use if this text is included
*/
// ---
function $(strId){
	return document.getElementById(strId);
}
// ---
function getElementsByClassName(oElm, strTagName, strClassName){
	var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
	var arrReturnElements = new Array();
	strClassName = strClassName.replace(/\-/g, "\\-");
	var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
	var oElement;
	for(var i=0; i<arrElements.length; i++){
		oElement = arrElements[i];		
		if(oRegExp.test(oElement.className)){
			arrReturnElements.push(oElement);
		}	
	}
	return (arrReturnElements)
}
// ---
function addClassName(oElm, strClassName){
	var strCurrentClass = oElm.className;
	if(!new RegExp(strClassName, "i").test(strCurrentClass)){
		oElm.className = strCurrentClass + ((strCurrentClass.length > 0)? " " : "") + strClassName;
	}
}
// ---
function removeClassName(oElm, strClassName){
	var oClassToRemove = new RegExp((strClassName + "\s?"), "i");
	oElm.className = oElm.className.replace(oClassToRemove, "").replace(/^\s?|\s?$/g, "");
}
// ---

/* EJ-E functions */
function getElementsByAttribute(oElm, strTagName, strAttributeName, strAttributeValue){
	var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
	var arrReturnElements = new Array();
	var oAttributeValue = (typeof strAttributeValue != "undefined")? new RegExp("(^|\\s)" + strAttributeValue + "(\\s|$)") : null;
	var oCurrent;
	var oAttribute;
	for(var i=0; i<arrElements.length; i++){
		oCurrent = arrElements[i];
		oAttribute = oCurrent.getAttribute && oCurrent.getAttribute(strAttributeName);
		if(typeof oAttribute == "string" && oAttribute.length > 0){
			if(typeof strAttributeValue == "undefined" || (oAttributeValue && oAttributeValue.test(oAttribute))){
				arrReturnElements.push(oCurrent);
			}
		}
	}
	return arrReturnElements;
}

function url_param(p) {
 // Retrieve an url param specified by p=value
 var re = new RegExp( p + "=([^&]+)" );
 var matches = document.location.href.match(re);
 if (matches) {
 return matches[1];
 }
 else {
 return '';
 }
}


function clearfields(){

	if(!document.getElementById('home-signup')) return false;
	
	var f = document.getElementById('home-signup');
	
	var a = f.getElementsByTagName('INPUT');
	
	for(var i = 0; i <= a.length; i++)
	{
		
		if(a[i]){
			a[i].onclick = a[i].onfocus = function(){
				if (this.value == 'First name' || this.value == 'Email address'){
				this.store = this.value;
				this.value = "";
				}
			}
			
			a[i].onblur = function(){
				if (!this.value){
				this.value = this.store;
				}
			}
		}
	}
}


function highlight_subnav() {
	if(!document.getElementById('pagenav')) return false;
 	// Highlight the right subnav item by comparing the URL with
	// the href of each <a> elt in the <div id="subnav">, or by
	// using the hint if supplied as arguments[0]. The hint
	// specifies a part of the URL, useful for actions with
	// lots of paramters
	
	var this_page = document.location.href;
	
	// Remove anchor if there is one -- anything following #, including #
	this_page = this_page.replace( /\#.*/, '' );
	
	var hint = "";
	if (arguments[0]) {
		hint = arguments[0];
	}
	
	// Try both subnav and subnavIndent for our item to highlight
	 
	var divs = [ "pagenav" ];
	for (var d = 0; d < divs.length; d++) {
	
		if (document.getElementById(divs[d])) {
			var subnav_elts = document.getElementById(divs[d]);
			var lTags = subnav_elts.getElementsByTagName("a");
 			var i = 0;
			for (i = 0; i < lTags.length; i++) {
				// Is this a link?
				var elt = lTags[i];
				if (elt.href == this_page || (hint && elt.href.indexOf(hint) != -1) ) {
					elt.parentNode.className = elt.parentNode.className + " active";
				}
 			}
		}
	}
}

