// <![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 to open a new window
* @usage openWindow(name, url, width, height, scroll )
* @param (name, url, width, height, scroll ) name: window name, url: page to be opened, width: window width, height: window height, scroll: window scroll
* @version 1.0
*/
function openWindow ( name , url , width , height , scroll ) {
	// Set Variables to position screen in the middle of the page
	var left = (screen.width) ? (screen.width-width)/2 : 0;
	var top = (screen.height) ? (screen.height-height)/2 : 0;

	// Open the new window
	// Set all to 0 except status cuase we need to show this for usability
	window.open(url,name,'left='+left+',top='+top+',toolbar=0,location=0,directories=0,status=1, menubar=0,scrollbars='+scroll+',resizable=0,width='+width+',height='+height);
}
/*
* Function to count charactors on a text field
* @usage countText ( field, countfield, maxlimit )
* @param ( field, countfield, maxlimit ) field: textarea that we are counting, countfield: field that updates with the true left charactors, maxlimit: max charactors
* @version 1.5
*/
function countText ( field, countfield, maxlimit ) {
	if ( document.getElementById && document.createTextNode ){
		// Lets set come variables
		var fld = document.getElementById( field );
		var count = document.getElementById ( countfield );
		// if too long...trim it!
		( fld.value.length > maxlimit ) ? fld.value = fld.value.substring( 0, maxlimit ) : count.value = ( maxlimit - fld.value.length )
	}
}
/*
* 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 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;
}
/*
* 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;
};
/**
* Function that inserts and element after a certain one
* @param parent
* @param node
* @param referenceNode
* @version 1.6
* @author Greg Shiers
*/
function insertAfter(parent, node, referenceNode) {
	parent.insertBefore(node, referenceNode.nextSibling);
}

/*
* Function to stop the default action of a element
* @usage findFocus ( ) loads on pageload
* @version 2.0
* @author Greg Shiers
*/
function findFocus() {
	var form, elements;
	if ( document.forms[0] != 'undefined' || document.forms[1] != 'undefined' ) { // Check that there is a form
		// find the form
		if ( document.forms[0] && document.forms[0].getAttribute('id') != "login" ) {
			form = document.forms[0];
			elements = form.elements[1]; // We select elements[1] because the elements[0] is the fieldset
			if ( elements ) { // If there is an element lets focus onto it
				if (elements.type=="text" || elements.type=="textarea" || elements.type=="password" ) {
					elements.focus();
				}
			}
		}
		else if ( document.forms[1] ) { 
			form = document.forms[1];
			elements = form.elements[1]
			if ( elements ) { // If there is an element lets focus onto it
				if (elements.type=="text" || elements.type=="textarea" || elements.type=="password" ) {
					elements.focus();
				}
			}
		}
		else { return false; } // return false if there is no element for either
	}
	else { return false; }
}
// Add the load listener to the find the focus ;)
addLoadListener( findFocus );

/*
* 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 );
/**
* Swaps the main image on the products page
* @param elements
* @version 
* @returns 
* @type 
* @author Greg Shiers
*/

function clearForm ( form ){
	var form = document.forms[0];
	// Loop through all elements in the form
	for ( var i = 0; i < form.length; i++ ) {
		var inputs = form[i]; // Set inputs as each of the form[i]
		switch ( inputs.type ) {
			// Check for input
			case 'text' :
				inputs.value = '';
			break;
			// Check for radio box or checkbox
			case 'radio' :
			case 'checkbox' :
				inputs.checked = false;
			break;
			// Check for select boxes
			case 'select-one' :
			case 'select-multiple' :
				inputs.selectedIndex = 0;
			break;
		}
	}
}

function selectUnselectAll ( txt , sel ) {
		// find the table to select from
	var selectbox  	= $(sel).getElementsByTagName('option');
	var text		= $(txt);
	
	if( text.innerHTML == "Select All" ) {
		for (i=0; i < selectbox.length; i++) {
			selectbox[i].selected = true;
		}
		text.innerHTML = "De-Select All";
	}
	else if ( text.innerHTML == "De-Select All" ) {
		for (i=0; i < selectbox.length; i++) {
			selectbox[i].selected = false;
		}
		text.innerHTML = "Select All";
	}
	else {
		alert('There has been an error, administrators have been notified, sorry about it');
		return false;
	}
}
/**
* 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);
*/
// ]]>