// JavaScripts used throughout the site...


/* ----------------------------------------------------------------------
addEvent
Adds functions to the window.onload function.

Based on these pages:
http://simonwillison.net/2004/May/26/addLoadEvent/
http://www.dustindiaz.com/top-ten-javascript
http://www.dustindiaz.com/rock-solid-addevent/
*/
if(typeof addEvent == 'undefined') {
	var addEvent = function( obj, type, fn ) {
		type = type.replace(/^on/, '');
		if (obj.addEventListener) {
			obj.addEventListener( type, fn, false );
			EventCache.add(obj, type, fn);
		}
		else if (obj.attachEvent) {
			obj["e"+type+fn] = fn;
			obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
			obj.attachEvent( "on"+type, obj[type+fn] );
			EventCache.add(obj, type, fn);
		}
		else {
			obj["on"+type] = obj["e"+type+fn];
		}
	}

	var EventCache = function(){
		var listEvents = [];
		return {
			listEvents : listEvents,
			add : function(node, sEventName, fHandler){
				listEvents.push(arguments);
			},
			flush : function(){
				var i, item;
				for(i = listEvents.length - 1; i >= 0; i = i - 1){
					item = listEvents[i];
					if(item[0].removeEventListener){
						item[0].removeEventListener(item[1], item[2], item[3]);
					};
					if(item[1].substring(0, 2) != "on"){
						item[1] = "on" + item[1];
					};
					if(item[0].detachEvent){
						item[0].detachEvent(item[1], item[2]);
					};
					item[0][item[1]] = null;
				};
			}
		};
	}();
	addEvent(window,'unload',EventCache.flush);
} // end addEvent

if(typeof addLoadEvent == 'undefined') {
	var addLoadEvent = function(func) {
		addEvent(window, 'onload', func);
	}
}

/* ----------------------------------------------------------------------
toggle
show or hide an item
*/
if(typeof toggle == 'undefined') {
	var toggle = function(obj, force) {
		var el = document.getElementById(obj);
		if(el) {
			if(typeof(force) == 'string') {
				el.style.display = force;
			} 
			else if ( el.style.display != 'none' ) {
				el.style.display = 'none';
			}
			else {
				el.style.display = '';
			}
		}
	}
}


if(typeof $ == 'undefined') {
	var $ = function() {
		var elements = new Array();
		for (var i = 0; i < arguments.length; i++) {
			var element = arguments[i];
			if (typeof element == 'string')
				element = document.getElementById(element);
			if (arguments.length == 1)
				return element;
			elements.push(element);
		}
		return elements;
	}
}



/* ----------------------------------------------------------------------
FormFocus
This function places the focus on a form field when a page loads
if it finds a field designated "inputFocusTarget"

Call using this code:
<body onload="FormFocus()"...
<input type="text" name="WHATEVER" id="inputFocusTarget"/>
*/
if(typeof FormFocus == 'undefined') {
	var FormFocus = function() {
		if(document) {
			if(document.getElementById) {
				if(document.getElementById("inputFocusTarget")) {
					document.getElementById("inputFocusTarget").focus();
				}
			}
		}
	}
}

// Cookie Functions  ////////////////////  (:)

// Set the cookie.
// SetCookie('your_cookie_name', 'your_cookie_value', exp);

// Get the cookie.
// var someVariable = GetCookie('your_cookie_name');
var expDays = 30;
var exp = new Date(); 
exp.setTime(exp.getTime() + (expDays*24*60*60*1000));

if(typeof getCookieVal == 'undefined') {
	var getCookieVal = function(offset) {  
		var endstr = document.cookie.indexOf (";", offset);  
		if (endstr == -1) { endstr = document.cookie.length; }
		return unescape(document.cookie.substring(offset, endstr));
	}
}

if(typeof GetCookie == 'undefined') {
	var GetCookie = function(name) {  
		var arg = name + "=";  
		var alen = arg.length;  
		var clen = document.cookie.length;  
		var i = 0;  
		while (i < clen) {    
			var j = i + alen;    
			if (document.cookie.substring(i, j) == arg) return getCookieVal (j);    
			i = document.cookie.indexOf(" ", i) + 1;    
			if (i == 0) break;   
		}  
		return null;
	}
}
if(typeof SetCookie == 'undefined') {
	var SetCookie = function(name, value) {  
		var argv = SetCookie.arguments;  
		var argc = SetCookie.arguments.length;  
		var expires = (argc > 2) ? argv[2] : null;  
		var path = (argc > 3) ? argv[3] : null;  
		var domain = (argc > 4) ? argv[4] : null;  
		var secure = (argc > 5) ? argv[5] : false;  
		document.cookie = name + "=" + escape (value) + 
		((expires == null) ? "" : ("; expires=" + expires.toGMTString())) + 
		((path == null) ? "" : ("; path=" + path)) +  
		((domain == null) ? "" : ("; domain=" + domain)) +    
		((secure == true) ? "; secure" : "");
	}
}

if(typeof DeleteCookie == 'undefined') {
	var DeleteCookie = function(name) {  
		var exp = new Date();  
		exp.setTime (exp.getTime() - 1);  
		var cval = GetCookie (name);  
		document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
	}
}


/*
Ajax()
Uses XML formed like this:
<response>
	<method>
		<function>1ST JAVASCRIPT FUNCTION TO CALL</function>
		<parameter>FIRST PARAMETER</parameter>
		<parameter>ETC...</parameter>
	</method>
	<method>
		<function>NTH FUNCTION TO CALL</function>
		<parameter>FIRST PARAMETER</parameter>
		<parameter>SECOND PARAMETER</parameter>
		<parameter>ETC...</parameter>
	</method>
</response>

Based on documentation from this excellent tutorial:
http://www.xml.com/pub/a/2005/02/09/xml-http-request.html
*/
if(typeof Ajax == 'undefined') {
	
	var req;
	function Ajax(url) {
		// branch for native XMLHttpRequest object
		if (window.XMLHttpRequest) {
			req = new XMLHttpRequest();
			req.onreadystatechange = AjaxProcessor;
			req.open("GET", url, true);
			req.send(null);
		// branch for IE/Windows ActiveX version
		} else if (window.ActiveXObject) {
			req = new ActiveXObject("Microsoft.XMLHTTP");
			if (req) {
				req.onreadystatechange = AjaxProcessor;
				req.open("GET", url, true);
				req.send();
			}
		}
	}
	/*
	Processes the XML when the request is returned.  Works with Ajax();
	*/
	function AjaxProcessor() {
		
		if (req.readyState == 4) { // only if req shows "complete"
			
			if (req.status == 200) { // only if "OK"
					
				response = req.responseXML.documentElement;
				
				if(response == null) {
					AjaxError("The XML response was not well formed.");
				} else {
				
					if(response.getElementsByTagName('method')[0] == null) {
						AjaxError("No method call defined.");
					} else {
					
						// Get the method to call from the XML returned
						method = response.getElementsByTagName('method'); //[0].firstChild.data;
						
						for(var j=0; j<method.length; j++) {
							
							// Get the function name
							functionName = method[j].getElementsByTagName('function')[0].firstChild.data
							
							// Get the parameters to call from the XML.
							params = method[j].getElementsByTagName('parameter');
							
							// Build a parameters list from the params.
							pList = '';			
							for(i=0; i < params.length; i++) {
								pList += (pList == '' ? '' : ', ') +
									(params[i].firstChild != null ? 
										'params['+i+'].firstChild.data' : 
										'\'\'');
							}
							
							// Execute the method with the given parameters.
							eval(functionName + '('+pList+')');
						
						} // end each method
					} // end if method defined
				} // end if well formed
			} // end if ok
		} // end if complete
	}
	
	function AjaxError(errorMsg) {
		if(errorMsg != null) { alert(errorMsg); }
	}
	function AjaxSilent() { }

}
