/* 
 * DOM Script Library. D.J.Holloway 2004. v0.02
 * --------------------------------------------
 * Note: This script is under heavy development and may not be programmed in the
 * most efficient way. Functions may be phased out if they are deemed unnecessary
 * and thus the script is not fit to be used in a commercial environment.
 *
 * Note: This script presents one method of adding interactivity to HTML pages. There
 * are many other methods which may be better or worse.
 *
 * Description: This script contains a set of cross-browser functions which provide
 * an interface to add Javascript interactivity to an HTML document without modifying
 * HTML source code. This allows an HTML page to be behaviourally independant (in the
 * same way that CSS allows a page to be presentationally independant).
 *
 * To use the script, the library file must be first included on the HTML page using:
 *
 *            <script type="text/javascript" src="js/dom.js"></script>
 *
 * dom.js is a library and should never be edited. To start adding interactivity to the
 * page, a new blank script should be created and included as below:
 *
 *            <script type="text/javascript" src="js/script.js"></script>
 *
 * script.js is where all the site interactive programming should take place.
 *
 * n.B. The functions in the library make use of object detection instead of the common
 *      user-agent detection.
 *      Reference: http://www.quirksmode.org/index.html?/js/support.html
 */
 
// EDITABLE SECTION
// turn debug mode alert boxes on. Turn this on while developing and the script will 
// alert you when HTML elements have not been found or if functions have failed etc..
var debug=0;

// DO NOT MODIFY BELOW THIS LINE
/* -----------------------------------------------------------------------------------*/
if (!document.attachEvent)
{	var gecko=1;	
}
if (document.attachEvent)
{	var ie=1;	
}


/* Function: getId(x)
 * Arguments: x = an HTML element ID as a string e.g. <div id="content"> = "content"
 * Usage: e.g. getId("content")
 * Purpose: This function replaces the standard function document.getElementById() but
 *          adds debug messages if the specified HTML element is not found.
 */
function gId(x)
{	return document.getElementById(x);
}
 
function getId(x)
{	if (document.getElementById(x))
	{	y = document.getElementById(x);
		return y;
	}
	else 
	{	if (debug>0)
		{	err("Element: "+x+" not found.");
			return 0;
		}
		return false;
	}
}
/* -----------------------------------------------------------------------------------*/
/* Function: err(x)
 * Arguments: x = an error message
 * Usage: e.g. err("That element does not exist")
 * Purpose: This function pops up an alert box with a custom error message. This is used
 *          when debug mode is turned on.
 * FAQ: Q: "Why can't you just use alert(msg);?"
 *      A: "It is intended in future releases to make all generated errors appear in one
 *          alert box. This cannot be done as easily with alert(msg);"
 */
function err(msg)
{	alert('DEBUG MODE: \t\t\t\t\n\n'+msg);
}
/* -----------------------------------------------------------------------------------*/
/* Function: SetStyleOnId(x,y)
 * Arguments: x = An HTML element ID, y= a CSS classname
 * Usage: e.g. SetStyleOnId("content","hidden")
 * Purpose: This function applies a CSS class to a given HTML element.
 *
 * n.B This function may be phased out in favour of using getId("content").className="hidden"
 */
function SetStyleOnId(x,y)
{	if (getId(x))
	{	getId(x).className=y;
	}
}
/* -----------------------------------------------------------------------------------*/
/* Function: addEvent()
 * Arguments: x = An HTML element ID, y= a CSS classname
 * Usage: SetEventOnId(element id,event,javascript function)
 *        e.g. SetEventOnId('homeLink','mouseover','alertUser')
 * Purpose: Hooks onto an element with an ID attribute
 *          and applies a javascript function to a specified event.
 * FAQ: Q: "Why not use element.onclick or element.onmouseover?"
 *      A: "element.onclick etc. does not allow the execution of more than one function at once."
 */
function aE(x,handler,jsprogram)
{

	if (ie) x.setAttribute('on'+handler,function() {eval(jsprogram)});
	if (gecko) x.setAttribute('on'+handler,jsprogram);

/*	if (getId(x))
	{	function bom() {eval(jsprogram);}	
		
		if(gecko)
		{   // Gecko browsers	
		    document.getElementById(x).addEventListener(jstrigger,bom,false);
		}
		if(ie)
		{	// Internet Explorer
			getId(x).attachEvent("on" + jstrigger,bom);
		}
	}*/
}

function params( param1, param2 )
{
	return function()
	{
		alert( "param1 = " + param1 + "\nparam2 = " + param2 );
	}
}



//Experimental getVars
function getVars(string) 
{ 
     varArray = string.split('?')[1].split('&'); 
     for(var x=0; x<varArray.length; x++) 
     { var tmp = varArray[x].split('='); 
       eval(unescape("URL"+tmp[0]) + '="' + unescape(tmp[1]) + '"'); 
     }
} 


//Experimental getVar
function getVar(name)
 {
 get_string = document.location.search;         
 return_value = '';
 
 do { //This loop is made to catch all instances of any get variable.
	name_index = get_string.indexOf(name + '=');
	
	if(name_index != -1)
	  {
	  get_string = get_string.substr(name_index + name.length + 1, get_string.length - name_index);
	  
	  end_of_value = get_string.indexOf('&');
	  if(end_of_value != -1)                
		value = get_string.substr(0, end_of_value);                
	  else                
		value = get_string;                
		
	  if(return_value == '' || value == '')
		 return_value += value;
	  else
		 return_value += ', ' + value;
	  }
	} while(name_index != -1)
	
 //Restores all the blank spaces.
 space = return_value.indexOf('+');
 while(space != -1)
	  { 
	  return_value = return_value.substr(0, space) + ' ' + 
	  return_value.substr(space + 1, return_value.length);
					 
	  space = return_value.indexOf('+');
	  }
  
 return(return_value);        
 }



 
 

 
function getStyle(elem,property)
{	//CHANGELOG
	/* 2006/01/28 -	Fixed width and height 
					for IE when they are set
					to auto by default.
	*/

	if (!document.attachEvent)
	{	return document.defaultView.getComputedStyle(elem,'').getPropertyValue(property);
	}
	if (document.attachEvent)
	{	
		if (property=="width")
		{	return elem.offsetWidth;
		}
		if (property=="height")
		{	return elem.offsetHeight;
		}
		if (property=="margin-top")
		{	property="marginTop";
		}		
		if (property=="margin-right")
		{	property="marginRight";
		}		
		if (property=="margin-bottom")
		{	property="marginBottom";
		}		
		if (property=="margin-left")
		{	property="marginLeft";
		}		
		return elem.currentStyle[property];
	}
}

function include(src)
{	//Include a new script in the HTML document

	if (document.getElementsByTagName('HEAD')[0])
	{	//If the HEAD tag can be detected, append the script using the DOM.
		//i.e. the include() has been executed after the page has loaded.
		var newscript=document.createElement('script');
		newscript.setAttribute('type', 'text/javascript');
		newscript.setAttribute('src', src);
		document.getElementsByTagName('HEAD')[0].appendChild(newscript); 
	}
	else
	{	//If the include function cannot find the HEAD tag.
		//i.e. the include() has been executed upon page load..
		document.write('<script type="text/javascript" src="'+src+'"><\/script>');
	}
}
 


 
 
 
 
//New 28/01/2006
function getElementsByClassName(x)
{	var elems=document.getElementsByTagName('*');
	var foundelems=new Array();
	for (i=0;i<elems.length;i++)
	{	if (elems[i].className==x)
		{	if (foundelems.length==0)
			{	next=0;
			}
			else
			{	next=foundelems.length;
			}
			
			foundelems[next]=elems[i];
		}
	}
	return foundelems;
}


function generateFlash(movie,width,height)
{  var flashcode;
   flashcode="<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" codebase=\"http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0\" width=\"" + width + "\" height=\"" + height + "\">"
           + "<param name=\"movie\" value=\"" + movie + "\" />"
           + "<param name=\"quality\" value=\"high\" />"
           + "<param name=\"wmode\" value=\"transparent\" />"
		   + "<param name=\"scale\" value=\"exactfit\" />"
           + "<embed src=\"" + movie +"\"" + "debug=\"false\" scale=\"exactfit\" quality=\"high\" wmode=\"transparent\" pluginspage=\"http://www.macromedia.com/go/getflashplayer\" type=\"application/x-shockwave-flash\" width=\"" + width + "\" height=\"" + height + "\"></embed>"
           + "</object>";
   return flashcode;
}

// This code was written by Tyler Akins and has been placed in the
// public domain.  It would be nice if you left this header intact.
// Base64 code from Tyler Akins -- http://rumkin.com

var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

function encode64(input) {
   var output = "";
   var chr1, chr2, chr3;
   var enc1, enc2, enc3, enc4;
   var i = 0;

   do {
      chr1 = input.charCodeAt(i++);
      chr2 = input.charCodeAt(i++);
      chr3 = input.charCodeAt(i++);

      enc1 = chr1 >> 2;
      enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
      enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
      enc4 = chr3 & 63;

      if (isNaN(chr2)) {
         enc3 = enc4 = 64;
      } else if (isNaN(chr3)) {
         enc4 = 64;
      }

      output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2) + 
         keyStr.charAt(enc3) + keyStr.charAt(enc4);
   } while (i < input.length);
   
   return output;
}

function decode64(input) {
   var output = "";
   var chr1, chr2, chr3;
   var enc1, enc2, enc3, enc4;
   var i = 0;

   // remove all characters that are not A-Z, a-z, 0-9, +, /, or =
   input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

   do {
      enc1 = keyStr.indexOf(input.charAt(i++));
      enc2 = keyStr.indexOf(input.charAt(i++));
      enc3 = keyStr.indexOf(input.charAt(i++));
      enc4 = keyStr.indexOf(input.charAt(i++));

      chr1 = (enc1 << 2) | (enc2 >> 4);
      chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
      chr3 = ((enc3 & 3) << 6) | enc4;

      output = output + String.fromCharCode(chr1);

      if (enc3 != 64) {
         output = output + String.fromCharCode(chr2);
      }
      if (enc4 != 64) {
         output = output + String.fromCharCode(chr3);
      }
   } while (i < input.length);

   return output;
}


function emailLink(x)
{	document.write('<a href="mailto:'+decode64(x)+'">'+decode64(x)+'</a>');
}
