// <![CDATA[
/*
* Copyright: 2005 - 2007 SI Works Internet Solutions
* If you have come across this page, its cause you are snooping through code, and are generally a developer as such
* If you would like to use some of the code on this page, please simply email support@siworks.co.za and ask permission
* it would be much appreciated, as we have worked very hard on these func.tions() and scri.pts()
* 
* Note: All functions below are to make sure that we are sticking to standards and are mainly
* for visual effects and loading events and handlers.
*
* General functions to use accross all sites and use for loading events
* @page common.functions.js
* @version 2.3.3
* @author Greg Shiers, Jarratt Ingram (SI Works Internet)
* @copyright: SI Works Internet Solutions 2005 - 2007
*/

/*
* Function to grab one or more elements
* @usage $('element1','element2')
* @returns Array
* @version 1.1
*/
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;
}
/**
* Function that creates an element
* @param element
* @version 1.2
* @returns string
* @author Greg Shiers
*/
function $_C ( element ){
	if ( typeof document.createElement != 'undefined' ) {
		return document.createElement(element);
	}
	else {
		alert('Your browser does not support document.createElement')
	}
}
/**
* Function to return the value of a form field
* @param element
* @version 1.2
* @returns string
* @author Greg Shiers
*/
function $_F( element ) {
	if (typeof element == 'string') {
		element = $( element );
	}
	return element.value;
}
/*
* Function to get the value of a cookie
* @usage GetCookie ( name )
* @param name
* @version 1.5
*/
function getCookie( name ) {
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
		return null;
	}
	if ( start == -1 ) return null;
	var end = document.cookie.indexOf( ';', len );
	if ( end == -1 ) end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );
}
/*
* Function to set a new Cookie
* @usage setCookie ( name, value, expires, path, domain, secure )
* @param name
* @param value
* @param expires
* @param path
* @param domain
* @param secure
* @version 1.0
*/
function setCookie( name, value, expires, path, domain, secure ) {
	var today = new Date();
	today.setTime( today.getTime() );
	if ( expires ) {
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	document.cookie = name+'='+escape( value ) +
		( ( expires ) ? ';expires='+expires_date.toGMTString() : '' ) + //expires.toGMTString()
		( ( path ) ? ';path=' + path : '' ) +
		( ( domain ) ? ';domain=' + domain : '' ) +
		( ( secure ) ? ';secure' : '' );
}
/*
* Function to check all checkboxes within a table
* @usage selectCookie ( name , path , domain )
* @param name
* @param path
* @param domain
* @version 1.2
*/
function deleteCookie( name, path, domain ) {
	if ( getCookie( name ) ) {
		document.cookie = name + '=' +
		( ( path ) ? ';path=' + path : '') +
		( ( domain ) ? ';domain=' + domain : '' ) +
		';expires=Thu, 01-Jan-1970 00:00:01 GMT';
	}
}
/*
* Function document.getElementsByClassName to find all element with a certain className
* @usage element.getElementsByClassName ( clsName )
* @param ( clsName ) the class you want to find
* @version 1.5
* @author 
*/
document.getElementsByClassName = function( clsName ){
	// Make the return value into a new arry
	var retVal = new Array();
	// Go through ALL Elements in the DOM by using the *
    var elements = document.getElementsByTagName("*");
	// Loop through ALL elements as defined
    for( var i = 0;i < elements.length;i++ ) {
		// Find all elements with a className
        if( elements[i].className.indexOf(" ") >= 0 ){
            var classes = elements[i].className.split( " " );
            for( var j = 0; j < classes.length; j++ ){
                if( classes[j] == clsName )
					// Return all elements by using the push() method which adds the next
					// element to the array of elements with the same classname
                    retVal.push( elements[i] );
            }
        }
        else if(elements[i].className == clsName)
            retVal.push ( elements[i] );
    }
	// Return the value
    return retVal;
}
/*
* Function to add a load event to the page when it loads
* to load a function with parameters we need to use addLoadListener ( function () { loadfunction ( parameters ) })
* @usage addLoadListener ( func )
* @param ( func ) the function you want to load
* @version 1.4
* @author 
*/
function addLoadListener( func ) { 
	if (typeof window.addEventListener != 'undefined') { //Check for window.addEventListener (FireFox)
    	window.addEventListener('load', func, false); // Set for FF
  	}
  	else if (typeof document.addEventListener != 'undefined') { // Check for document.addEventListener (FireFox)
    	document.addEventListener('load', func, false);
  	}
  	else if (typeof window.attachEvent != 'undefined') { // Check for window.attachEvent (IE)
    	window.attachEvent('onload', func);
  	}
  	else {
    var oldfn = window.onload;
    	if (typeof window.onload != 'function') {
      		window.onload = func;
    	}
    	else {
      		window.onload = function() {
        		oldfn();
        		func();
      		};
    	}
  	}
}
/*
* Function to add a event to a certain element
* to load a event with parameters we need to use attachEventListener ( function () { loadfunction ( parameters ) })
* @usage attachEventListener (  target, eventType, functionRef, capture  ) target: which element, eventType: which event, functionRef: which function, capture: true / false;
* @param ( func ) the function you want to load
* @version 1.1
* @returns Boolean
* @author 
*/
function attachEventListener( target, eventType, functionRef, capture ) {
	if (typeof target.addEventListener != "undefined") { //Check for target.addEventListener (FireFox)
		target.addEventListener(eventType, functionRef, capture); // Set the listener
	}
	else if (typeof target.attachEvent != "undefined") { // Check for target.attachEvent (IE)
		target.attachEvent("on" + eventType, functionRef);
	}
	else {
		eventType = "on" + eventType;

		if (typeof target[eventType] == "function") {
			var oldListener = target[eventType];

			target[eventType] = function() {
				oldListener();
				return functionRef();
			}
		}
		else {
			target[eventType] = functionRef;
		}
	}
	return true;
}
/*
* Function to remove an event to a certain element
* to load a event with parameters we need to use attachEventListener ( function () { loadfunction ( parameters ) })
* @usage detachEventListener (  target, eventType, functionRef, capture  ) target: which element, eventType: which event, functionRef: which function, capture: true / false;
* @param ( func ) the function you want to load
* @version 1.2
* @author 
*/
function detachEventListener( target, eventType, functionRef, capture ) {
	if (typeof target.removeEventListener != "undefined") {
		target.removeEventListener(eventType, functionRef, capture);
	}
	else if (typeof target.detachEvent != "undefined") {
		target.detachEvent("on" + eventType, functionRef);
	}
	else {
		target["on" + eventType] = null;
	}
	return true;
}

/*
* Opens a rel="external" in a new window, to validate in XHTML strict
* This code was found on http://www.sitepoint.com/article/standards-compliant-world in order to be able to validate a page with 
* opening a new link in a new window without target="_blank"
* @usage addLoadListener ( externalLinks );
* @version 1.0
* @author www.sitepoint.com
*/
function externalLinks(){
	if (!document.getElementsByTagName) return; // Check for DOM / Browser compatability
	var anchors = document.getElementsByTagName("a"); // Set var, which will narrow down all the a elements in the document
	for (var i=0; i<anchors.length; i++){
		var anchor = anchors[i];
		if (anchor.getAttribute("href") &&
		anchor.getAttribute("rel") == "external")
		anchor.target = "_blank";
	}
}
// Load this function on page load
addLoadListener ( externalLinks );

/**
* Suckerfish menu loader
* @param 
* @version 
* @returns 
* @type 
* @author Greg Shiers
*/
/* 
sfHover = function() {
	var sfEls = document.getElementById("nav").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" sfhover";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", sfHover);
*/
// ]]>