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


function addResizeEvent(func) {
	var oldresize = window.onresize;
	if (typeof window.onresize != 'function') {
		window.onresize= func;
	} else {
		window.onresize = function() {
			oldresize();
			func();
		}
	}
}

//Function that takes element ID and pixel offset and changes height of elment.

function e(id) {
      return document.getElementById(id);
}

function autoResizeElement(id,bottomOffset) {
      var topOffset = 0;
      for (var elem = e(id);
       elem != null;
       elem = elem.offsetParent) {
            topOffset += elem.offsetTop;
      }
      var windowHeight = getViewportHeight();
      var height = windowHeight - topOffset - bottomOffset;
      if (height >= 0) {
            e(id).style.height = height + "px";
      }
}

function getViewportHeight() {
      if (window.self && self.innerHeight) {
            return self.innerHeight;
      }
      if (document.documentElement && document.documentElement.clientHeight) {
            return document.documentElement.clientHeight;
      }
      return 0;
}

function getViewportWidth() {
      if (window.self && self.innerWidth) {
            return self.innerWidth;
      }
      if (document.documentElement && document.documentElement.clientWidth) {
            return document.documentElement.clientWidth;
      }
      return 0;
}


// Function that takes a String URL and sets the parent's location to the URL
function goToPage(obj) {
	if (obj.value != null && obj.value != "void" && obj.value != "") {
		parent.location =obj.value;
	}
}

// Generic pop up window function the window features are passed from the parent page
function openBrWindow(theURL,winName,features) {
	var winObj = window.open(theURL,winName,features);
	if (!winObj.opener) winObj.opener = self;
	winObj.focus();
}

// Function that takes in a name, reads that name from a cookie, and returns the value of that name.
function readCookie(name) {
	var cookies = document.cookie;
	var start = cookies.indexOf(name + "=");
	if (start == -1) return null;
	start = cookies.indexOf("=", start) + 1;
	var end = cookies.indexOf(";", start);
	if (end == -1) end = cookies.length;
	var value = unescape(cookies.substring(start, end));
	return value;
}

// Function that takes a name/value pair and sets them to a cookie
/*
   name - name of the cookie
   value - value of the cookie
   [expires] - expiration date of the cookie in days (defaults to end of current session)
   [path] - path for which the cookie is valid (defaults to path of calling document)
   [secure] - Boolean value indicating if the cookie transmission requires a secure transmission
   [domain] - domain for which the cookie is valid (defaults to domain of calling document)
   * an argument defaults when it is assigned null as a placeholder
   * a null placeholder is not required for trailing omitted arguments
*/
function setCookie(name, value, expires, path, secure, domain) {
	var today = new Date();
	today.setTime(today.getTime());
	
	if (expires) { expires = expires * 1000 * 60 * 60 * 24; }
	var expires_date = new Date( today.getTime() + (expires) );

	var curCookie = name + "=" + escape(value) +
	(( expires ) ? "; expires=" + expires_date.toGMTString() : "") + 
	((path) ? "; path=" + path : "") +
	((domain) ? "; domain=" + domain : "") +
	((secure) ? "; secure" : "");
	document.cookie = curCookie;
}

// Function that deletes a cookie
/*
   name - name of the cookie
   [path] - path of the cookie (must be same as path used to create cookie)
   [domain] - domain of the cookie (must be same as domain used to create cookie)
   * path and domain default if assigned null or omitted if no explicit argument proceeds
*/
function deleteCookie(name, path, domain) {
	if (readCookie(name)) {
		document.cookie = name + "=" +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		"; expires=Thu, 01-Jan-70 00:00:01 GMT";
	}
}

// Function to switch current display of id between "block" and "none"
function switchDisplay(elementID, swapType) {
	var element = document.getElementById(elementID);
	if (swapType == 'show'){
		element.style.display = 'block';
	} else {
		element.style.display = 'none';
	}
}

// Function to toggle an elements display
// elementID is the name of the element you want to toggle.
// currentDisplay is a boolean that's passed in and represents the elements current display type.
function toggleDisplay(elementID, currentDisplay) {
	var element = document.getElementById(elementID);
	if (currentDisplay){
		element.style.display = "none";
		currentDisplay = false;
	} else {
		element.style.display = "block";
		currentDisplay = true;
	}
	return currentDisplay;
}

var currentClass;
function classSwitcher(el,stateClass) {
	var element = (typeof el == "object") ? el : e(el);
	if(currentClass==undefined || currentClass==stateClass) currentClass = element.className;

	if(element.className == currentClass || element.className == "") {
		element.className = stateClass;
	} else {
		element.className = currentClass;
	}
}
// Function to determine if a field is null
function isNull(fieldValue) {
	if (fieldValue == null) {fieldValue=''};
		return (fieldValue.length == 0);	
}

// Function to determine if two fields match each other
function isMatch(field1,field2) {
	if ((field1 != null && field1 != '') && ( field2 != null && field2 != ''))
		return (field1 == field2);
}

// Function to parse the querystring into useable variables
var parsequery_args = new Object();
function parsequery(qs) {
	if (qs.length > 2) {
		var query = qs.substring(1); // get query string (without initial "?")
		var pairs = query.split("&")  //break at ampersand into pairs
		var re = /\+/g; //the unescape() function does not remove +
		for (var i = 0; i < pairs.length; i++) {
			var pos = pairs[i].indexOf('=');  //look for "name=value"
			if (pos == -1) continue;      //if not found skip
			var argname = pairs[i].substring(0,pos);  //extract the name
			var value = pairs[i].substring(pos + 1);  //extract the value
			parsequery_args[argname] = unescape(value.replace(re," "));  //store as a property
		}
	}
}

// Function that takes a string (s) and 1 character (bag) to strip from the string.
// the function returns the string without the 1 character (bag) in it.
function stripCharsInBag (s, bag){
	var i;
	var returnString = "";
	for (i = 0; i < s.length; i++) {
		var c = s.charAt(i);
		if (bag.indexOf(c) == -1) returnString += c;
	}
	return returnString;
}

// Function that gives you the screen x and screen y, even after the user has scrolled
var scrOfX = 0, scrOfY = 0;
function getScrollXY() {
	if( typeof( window.pageYOffset ) == 'number' ) {
	//Netscape compliant
		scrOfY = window.pageYOffset;
		scrOfX = window.pageXOffset;
	} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
	//DOM compliant
		scrOfY = document.body.scrollTop;
		scrOfX = document.body.scrollLeft;
	} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
	//IE6 standards compliant mode
		scrOfY = document.documentElement.scrollTop;
		scrOfX = document.documentElement.scrollLeft;
	}
	return [ scrOfX, scrOfY ];
}

// Fixes the IE background-image hover bug
try {
    document.execCommand( "BackgroundImageCache", false, true );
} catch( e ) { };
