// JavaScript Document


(function(){
		  
	//The DH namespace
	if(!window.DH) { window['DH'] = {} }
	
	
	function isCompatible(other) 
	{ 
		//Use capability detection to check requirements
		if(other===false
		   || !Array.prototype.push
		   || !Object.hasOwnProperty
		   || !document.createElement
		   || !document.getElementsByTagName
		   )
		{ 
			return false;
		}
		return true;
	}
	window['DH']['isCompatible'] = isCompatible;
	
	
	function $() 
	{ 
		var elements = new Array();
		
		//find all elements supplied as arguments
		for(var i = 0 ; i < arguments.length; i++)
		{
			var element = arguments[i];
			
			//if the argument is a string assume it's an id
			if(typeof element == 'string')
			{
				element = document.getElementById(element);	
			}
			
			//if only one argument was supplied, return the element immediately
			if(arguments.length == 1)return element;
			
			//Otherwise add it to the array
			elements.push(element);
		}
		
		//Return the array of multiple requested elements
		return elements;
	}
	window['DH']['$'] = $;
	
	
	function addEvent(node,type,listener)
	{ 
		//Check compatibility using the earlier method to ensure graceful degradation
		if(!isCompatible())return false;
		
		if(!(node = $(node)))return false;
					  
		if(node.addEventListener)
		{
			//W3C method
			node.addEventListener(type,listener,false);
			return true;
		} else if(node.attachEvent){
				//MSIE method
				node['e'+type+listener] = listener;
				node[type+listener] = function(){
					node['e'+type+listener](window.event);
				};
				node.attachEvent('on'+type,node[type+listener]);
				return true;
		}
		
		//Didn't have either so return false
		return false;
	}
	window['DH']['addEvent'] = addEvent;
	
	
	function removeEvent(node,type,listener)
	{ 
		if(!(node = $(node))){return false;}
		
		if(node.removeEventListener)
		{	
			node.removeEventListener(type,listener,false);
			return true;
		} else if(node.detachEvent){
			//MSIE method
			node.detachEvent('on' + type, node[type+listener]);
			node[type+listener] = true;
			return true;
		}
		
		//Didn't have either so return false
		return false;
	}
	window['DH']['removeEvent'] = removeEvent;
	
	
	function getElementsByClassName(className,tag,parent)
	{ 
		parent = parent || document;
		
		if(!(parent = $(parent))){return false;}
		
		//Locate all the matching tags
		var allTags = (tag == "*" && parent.all) ? parent.all : parent.getElementsByTagName(tag);
		
		var matchingElements = new Array();
		
		//Create a regular expression to determine if the className is correct
		className = className.replace(/\-/g, "\\-");
		var regex = new RegExp("(^|\\s)" + className + "(\\s|$)");
		
		var element;
		//Check each element
		for(var i = 0; i < allTags.length; i++)
		{
			element = allTags[i];
			if(regex.test(element.className))
			{
				matchingElements.push(element);	
			}
		}
		
		//Return any matching elements
		return matchingElements;
	}
	window['DH']['getElementsByClassName'] = getElementsByClassName;
	
	function getElementByTagAndNameAttribute(name,tag,parent)
	{
		if(typeof arguments[0] == 'undefined' || (typeof arguments[0] != 'undefined' && typeof arguments[0] != 'string'))
		{
			throw new Error('DH.getElementByTagAndNameAttribute() expects argument 1 of string type');
		}
		
		if(typeof arguments[1] == 'undefined' || (typeof arguments[1] != 'undefined' && typeof arguments[1] != 'string'))
		{
			throw new Error('DH.getElementByTagAndNameAttribute() expects argument 2 of string type');
		}
		
		parent = parent || document;	
		
		var allTags = parent.getElementsByTagName(tag);
		
		for(var i = 0; i < allTags.length; i++)
		{
			if(allTags[i].name && allTags[i].name == name)
			{
				return allTags[i];
			}
		}
		return null;
	}
	window['DH']['getElementByTagAndNameAttribute'] = getElementByTagAndNameAttribute;
	
	function toggleDisplay(node,value)
	{ 
		if(!(node = $(node))){return false;}
		
		if(node.style.display != 'none')
		{
			node.style.display = 'none';
		} else {
			node.style.display = value || '';
		}
		return true;
	}
	window['DH']['toggleDisplay'] = toggleDisplay;
	
	
	function insertAfter(node,referenceNode)
	{ 
		if(!(node = $(node))){return false;}
		
		if(!(referenceNode = $(referenceNode)))return false;
		
		return referenceNode.parentNode.insertBefore(node,referenceNode.nextSibling);
	}
	window['DH']['insertAfter'] = insertAfter;
	
//DH.createXHR - create cross-browser XHR object
	function createXHR()
    {
        if (typeof XMLHttpRequest != "undefined") 
        {
            return new XMLHttpRequest();
        } else if (window.ActiveXObject) 
               {
            var aVersions = [ "MSXML2.XMLHttp.6.0", "MSXML2.XMLHttp.3.0"];
            for (var i = 0; i < aVersions.length; i++) 
            {
                try {
                    var oXHR = new ActiveXObject(aVersions[i]);
                    return oXHR;
                } catch (oError) {
                //Do nothing
                }
            }
        }
        throw new Error("XMLHttp object could not be created.");
    }
    window['DH']['createXHR'] = createXHR;
    
	
//DH.createImg - creates an image tag
// imgSrc - image source (optional)
// imgAlt - image alternate text (optional)
// errorFunction - function to call on load error (option)
	function createImg(imgSrc,imgAlt,errorFunction)
    {
        //If source is passed and not a valid string
        if((typeof arguments[0] != 'undefined') && typeof arguments[0] != 'string')
        {
            throw new Error('DH.createImg() expects argument 1 to be of \'string\' type');
        }
        
        //If imgAlt is not a valid string
        if((typeof arguments[1] != 'undefined') && typeof arguments[1] != 'string')
        {
            throw new Error('DH.createImg() expects argument 2 to be of \'string\' type');
        }
        
        //If errorFunction is not a valid function
        if(typeof arguments[2] != 'undefined' && arguments[2] != null && typeof arguments[2] != 'function')
        {
            throw new Error('DH.createImg() expects argument 3 to be of \'function\' type');
        }
        
        var oImg = document.createElement('img');
        if(imgSrc)oImg.src = imgSrc;
        if(imgAlt)oImg.setAttribute('alt',imgAlt);
        if(errorFunction)oImg.onerror = errorFunction;
        return oImg;
    }
    window['DH']['createImg'] = createImg;

//DH.createDiv - creates a div tag
// divId - sets id attribute (optional)
// divClass - set class attribute (optional)
    function createDiv(divId,divClass)
    {
        //If divId is passed and not a valid string
        if((typeof arguments[0] != 'undefined') && typeof arguments[0] != 'string')
        {
            throw new Error('DH.createImg() expects argument 1 to be of \'string\' type');
        }
        
        //If divClass is not a valid string
        if((typeof arguments[1] != 'undefined') && typeof arguments[1] != 'string')
        {
            throw new Error('DH.createImg() expects argument 2 to be of \'string\' type');
        }
        
        var oDiv = document.createElement('div');
        if(divId)oDiv.setAttribute('id',divId);
        if(divClass)oDiv.className = divClass;
        return oDiv;
    }
    window['DH']['createDiv'] = createDiv;
//DH.createP - creates a P tag
// pId - sets id attribute (optional)
// pClass - set class attribute (optional)
    function createP(pChild,pId,pClass)
    {
        //If id provided does not validate        
        if((typeof arguments[1] != 'undefined') && typeof arguments[1] != 'string')
        {
            throw new Error('DH.createP() expects argument 2 to be of \'string\' type');
        }  
        //If class provided does not validate
        if((typeof arguments[2] != 'undefined') && typeof arguments[2] != 'string')
        {
            throw new Error('DH.createP() expects argument 3 to be of \'string\' type');

        }  
        
        //Crete p tag
        var p = document.createElement('p');
        if(pId)p.setAttribute('id',pId);
        if(pClass)p.className = pClass;
        
        //If inner content has been provided
        if(pChild)
        {  
            //If p child is not an element attach it to text node          
            if(typeof pChild == 'string')
            {
                pChild = document.createTextNode(pChild);
            }
            p.appendChild(pChild);
        }
        return p;
    }
    window['DH']['createP'] = createP;
    
//DH.createLabel = creates a label tag
// inner - sets inner text (required)
// lFor - sets the for attribute (optional)
    function createLabel()
    {
        if(typeof arguments[0] == 'undefined')
        {
            throw new Error('DH.createLabel() expects argument 1 to be of string type');
        }
        
        var label = document.createElement('label');
        label.appendChild(document.createTextNode(arguments[0]));
        
        if(typeof arguments[1] != 'undefined' 
           && arguments[1].length 
           && arguments[1].length > 0)
        {
            label.setAttribute('for',arguments[1]);
        }
        return label;
    }
    window['DH']['createLabel'] = createLabel;

//DH.createInput
// type = type attribute (required)
// name = name attribute (required)
// value = value attribute (optional)
// tabindex = tabindex attribute (optional)
    function createInput()
    {
        //validate type attribute      
        if((typeof arguments[0] == 'undefined') || typeof arguments[0] != 'string' || arguments[0].length < 1)
        {
            throw new Error('DH.createInput() expects argument 1 to be of \'string\' type');
        }
        
        //validate name attribute      
        if((typeof arguments[1] == 'undefined') || typeof arguments[1] != 'string' || arguments[1].length < 1)
        {
            throw new Error('DH.createInput() expects argument 2 to be of \'string\' type');
        }
        
        var oInput = document.createElement('input');
        oInput.setAttribute('type',arguments[0]);
        oInput.setAttribute('name',arguments[1]);
        
        //Attach value
        if((typeof arguments[2] != 'undefined') && typeof arguments[2] == 'string' && arguments[2].length > 0)
        {
            oInput.setAttribute('value',arguments[2]);
        }
        
        //Attach tabindex
        if((typeof arguments[3] != 'undefined') && typeof arguments[3] == 'string' && (!isNaN(arguments[2])))
        {
            oInput.setAttribute('tabindex',arguments[3]);
        }
        return oInput;
    }
    window['DH']['createInput'] = createInput;

//DH.createHiddenInput
// name = name attribute (required)
// value = value attribute (required)
    function createHiddenInput()
    {
        //validate name attribute      
        if((typeof arguments[0] == 'undefined') || typeof arguments[0] != 'string' || arguments[0].length < 1)
        {
            throw new Error('DH.createInput() expects argument 1 to be of \'string\' type');
        }
        
        //validate value attribute      
        //if((typeof arguments[1] == 'undefined') || typeof arguments[1] != 'string' || arguments[1].length < 1)
        if((typeof arguments[1] == 'undefined'))
        {
            throw new Error('DH.createInput() expects argument 2 to be of \'string\' type');
        }
        
        var oInput = document.createElement('input');
        oInput.setAttribute('type','hidden');
        oInput.setAttribute('name',arguments[0]);
        oInput.setAttribute('id',arguments[0]);
        oInput.setAttribute('value',arguments[1]);
        
        return oInput;
    }
    window['DH']['createHiddenInput'] = createHiddenInput;


//DH.createOption
// inner - sets inner text (required)
// value = value attribute (required)
    function createOption()
    {
        //validate inner     
        if((typeof arguments[0] == 'undefined') || typeof arguments[0] != 'string' || arguments[0].length < 1)
        {
            throw new Error('DH.createOption() expects argument 1 to be of \'string\' type');
        }
        
        //validate type attribute      
        if((typeof arguments[1] == 'undefined') || typeof arguments[1] != 'string' || arguments[1].length < 1)
        {
            throw new Error('DH.createOption() expects argument 2 to be of \'string\' type');
        }
        
        var option = document.createElement('option');
        option.setAttribute('value',arguments[1]);
        option.appendChild(document.createTextNode(arguments[0]));
        return option;
    }
    window['DH']['createOption'] = createOption;
    
//DH.createAnchor = creates an anchor tag
// inner - sets inner text (required)
// aHref - sets href attribute (required)
// aId - sets id attribute (optional)
// aClass - sets class attribute (optional)
// aTitle - sets title attribute (optional)
    function createAnchor(inner,aHref,aId,aClass,aTitle)
    {
        //If required inner is not valid
        //if(typeof arguments[0] == 'undefined' || typeof arguments[0] != 'string' || arguments[0].length == 0)
		if(typeof arguments[0] == 'undefined')
        {
            throw new Error('DH.createAnchor() expects argument 1 to be of string type');
        }
        /*
        //If required aHref is not valid
        if(typeof arguments[1] == 'undefined' || typeof arguments[1] != 'string' || arguments[1].length == 0)
        {
            throw new Error('DH.createAnchor() expects argument 2 to be of string type');
        }
        */
        var oAnchor = document.createElement('a');
        if(aHref)oAnchor.setAttribute('href',aHref);
        if(aId)oAnchor.setAttribute('id',aId);
        if(aClass)oAnchor.className = aClass;
        if(aTitle)oAnchor.setAttribute('title',aTitle);
		
		//Check if inner content is an object
		if(typeof inner != 'string')
		{
			oAnchor.appendChild(inner);
		} else {
        	oAnchor.appendChild(document.createTextNode(inner));
		}
		return oAnchor;    
    }
    window['DH']['createAnchor'] = createAnchor;
	
//DH.addWindowLoadEvent
// func - function to be attached to window.onload (required)
	function addWindowLoadEvent(func)
    {
        if(typeof arguments[0] == 'undefined' || typeof arguments[0] != 'function')
        {
            throw new Error('DH.addWindowLoadEvent() expects argument 1 to be of \'function\' type');
        }
        
	    var ondonload = window.onload;
	    //if window.onload does not have any functions attached
	    if(typeof window.onload != 'function')
	    {
		    window.onload = func;
	    } else {
		    window.onload = function()
		    {
			    ondonload();
			    func();
		    };
	    }
    }
    window['DH']['addWindowLoadEvent'] = addWindowLoadEvent;
    
//DH.stopEvent
// oEvent - event reference (required)
    function stopEvent(oEvent)
    {
        if(typeof arguments[0] == 'undefined')
        {
            throw new Error('DH.stopEvent() expects at least one argument');
        }
  
        if(window.event)
        { 
            var target = window.event.srcElement;
            window.event.returnValue = false;
        } else {
            var target = oEvent.target;
            oEvent.preventDefault();
        }
        return target;
    }
	window['DH']['stopEvent'] = stopEvent;
	
	function getURLParam(param)
	{
		var winLoc = window.location.toString().toLowerCase();
		
		if(winLoc.indexOf('?') ==-1) return null;
		
		var winLocParams = (decodeURIComponent(winLoc.split('?')[1])).split('&');
		
		for(var i = 0; i < winLocParams.length; i++)
		{
			if(winLocParams[i].split('=')[0] == param.toLowerCase())
			{
				return (winLocParams[i].split('=')[1]).replace(/\+/gi,' ');
			}
		}
		return null;
	}
	window['DH']['getURLParam'] = getURLParam;

//DH.getCookie
	function getCookie(sName) 
	{
        var sRE = "(?:; )?" + sName + "=([^;]*);?";
        var oRE = new RegExp(sRE);
        if(oRE.test(document.cookie)) 
        {
            return decodeURIComponent(RegExp["$1"]);
        } else {
            return null;
        }
    }
    window['DH']['getCookie'] = getCookie;	

//DH.setCookie		
    function setCookie(sName, sValue, oExpires, sPath, sDomain, bSecure) 
    {
        var sCookie = sName + "=" + encodeURIComponent(sValue);
        
        if(oExpires) 
        {
            sCookie += "; expires=" + oExpires.toGMTString();
        }
        
        if(sPath) 
        {
            sCookie += "; path=" + sPath;
        }
        
        if(sDomain) 
        {
            sCookie += "; domain=" + sDomain;
        }
        
        if(bSecure) 
        {
            sCookie += "; secure";
        }
        
        document.cookie = sCookie;
    }
	window['DH']['setCookie'] = setCookie;
	
//DH.findPos
    function findPos(obj) 
    {
	    var curleft = curtop = 0;
	    if(obj.offsetParent) 
	    {
	        do
	        {
			    curleft += obj.offsetLeft;
			    curtop += obj.offsetTop;
            } while (obj = obj.offsetParent);
        }
        return [curleft,curtop];
    }
    window['DH']['findPos'] = findPos;	 
    
    function getEventObject(obj)
    {
        return obj || window.event;
    } 
    window['DH']['getEventObject'] = getEventObject;
    
    function getKeyPressed(eventObject)
    {
        eventObject = eventObject || getEventObject(eventObject);
        
        var code = eventObject.keyCode;
        var value = String.fromCharCode(code);
        return {'code':code,'value':value};
    }  
    window['DH']['getKeyPressed'] = getKeyPressed;  
    
    
})();