// <![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.0
* @author Greg Shiers, Jarratt Ingram (SI Works Internet)
* @copyright: SI Works Internet Solutions 2005 - this.year()
*/

/*
* 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 to show or hide an element based on mouse event
* @usage showHideElement ( element )
* @param ( element ) element that needs the function applied
* @version 1.5
*/
function showHideElement ( element ) {
	var div = document.getElementById( element );
	( div.style.display == "block" || div.style.display == "" ) ? div.style.display = "none" : div.style.display = "block" ;
}
/*
* Function to show or hide an element based on mouse event via a checkbox
* @usage showHideCheckbox ( element )
* @param ( element ) checkbox that needs the function applied
* @version 1.4
*/
function showHideCheckbox ( element , checkbox ) {
	// set the element variables
	var div = document.getElementById ( element ), chk = document.getElementById ( checkbox );
	( chk.checked ) ? div.style.display = "none" : div.style.display = "block";
}
/*
* Function to show or hide an element based on mouse event via a radio button
* @usage showHideRadioButton ( element, show )
* @param ( element, show ) radio button that needs the function applied, show: if you want to show the element or hide it
* @version 1.2
*/
function showHideRadioButton ( element , show ) {
	// set the element variables
	var div = document.getElementById ( element );
	( show ) ? div.style.display = "block" :  div.style.display = "none";
}
/*
* Function to show or hide an element based on mouse event via a radio button
* @usage showHideRadioButton ( element , selectbox , index )
* @param ( element , selectbox , index ) element: element effected, selectbox: the select box that needs to be validated, index: which index is the one to be selected
* @version 1.5
*/
function showHideSelectBox ( element , selectbox , index ) {
	// set the element variables
	var element = document.getElementById ( effected ), sel = document.getElementById ( selectbox );
	( sel.selectedIndex != index ) ? element.style.display = "none" : element.style.display = "block";
}
/*
* 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;
}
/*
* Function to stop the default action of a element
* @usage stopDefaultAction ( event ) which event we want to stop
* @param ( event ) the event we want to stop
* @version 1.1
* @returns Boolean
* @author 
*/
function stopDefaultAction ( event ) {
	event.returnValue = false;
	if (typeof event.preventDefault != "undefined") {
	    event.preventDefault();
  	}
  return true;
}

// Array.prototype.inArray
// inArray Prototype Array object by EmbiMedia
Array.prototype.inArray = function (value) {
	var i;
	for (i=0; i < this.length; i++) {
		if (this[i] === value) {
			return true;
		}
	}
	return false;
};

/*
* 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 );

/*
* Function to preload any icons / images that we might use for dynamic content
* @usage preloadImages (  ) 
* @version 1.5.2
* @author 
*/
function preloadImages () {
	// This is not a function, but will load images on the fly as we moive along parsing the page
	var names = ['progress'];
	// Create an object to pass the above array through the for loop below
	var objects = [];
	
	// Loop through all the images if there are more than 1
	for (var i = 0; i < names.length; i++) {
		objects[i] = new Image(); // Create the image
		objects[i].src = '/images/icons/'+names[i] + '.gif'; // Give it a src
	}
}
// Load this function on page load
addLoadListener ( preloadImages );

/**
* 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);

addLoadListener ( function() { 
	attachEventListener( $('login_email'), "focus", function () {
		if ($('login_email').value == 'email address') {
			$('login_email').value = '';
		}
	} , false );
	attachEventListener( $('login_email'), "blur", function () {
		if ($('login_email').value == '') {
			$('login_email').value = 'email address';
		}
	} , false );
	attachEventListener( $('login_password'), "focus", function () {
		if ($('login_password').value == 'your password') {
			$('login_password').type = "password";
			$('login_password').value = '';
		}
	} , false );
	attachEventListener( $('login_password'), "blur", function () {
		if ($('login_password').value == '') {
			$('login_password').type = "text";
			$('login_password').value = 'your password';
		}
	} , false );
 } )
// ]]>