var isDOM=document.getElementById?1:0,
isIE=document.all?1:0,
isNS4=navigator.appName=='Netscape'&&!isDOM?1:0,
isOp=self.opera?1:0,
isDyn=isDOM||isIE||isNS4;

O.toolbox.tools = function (){
	return { 
			//body: document.getElementsByTagName('body')[0],

			//Define 
			isObject:   function(o){return (o && typeof o == 'object') || this.isFunction(o);},
			isArray:    function(o){return this.isObject(o) && o.constructor == Array;},
			isBoolean:  function(o){return typeof o == 'boolean';},
			isString:   function(o){return typeof o == 'string';},
			isFunction: function(o){return typeof o == 'function';},

			createEl:   function (sType) { return document.createElement(sType); },
			createTxt:  function (sTxt) { return document.createTextNode(sTxt); },
			getEl:      function (sEl) { return document.getElementById(sEl); },

			//Append to DOM global scope
			appendDOMGlobal: function (o) {
				if(this.isArray(o)){
					for( var i = 0;i < o.length; i ++ ){ 
						document.getElementsByTagName('body')[0].appendChild(o[i]);
					} 
				}else if(this.isObject(o)) 
					document.getElementsByTagName('body')[0].appendChild(o);

			},

			//Append to DOM in local scope
			appendDOMLocal: function (oScope,o){
				if(this.isArray(o)){
					for( var i = 0;i < o.length; i ++ ){ 
						this.appendChild(o[i]) 
					} 
				}else if(this.isObject(o)) 
					oScope.appendChild(o);
			},

			//Get mouse poition, in X/Y on e
			mousePos: function(e) {
				var posx = 0;
				var posy = 0;
				if (!e) var e = window.event;
				if (e.pageX || e.pageY) 	{
					posx = e.pageX;
					posy = e.pageY;
				}
				else if (e.clientX || e.clientY) 	{
					posx = e.clientX + document.body.scrollLeft
						+ document.documentElement.scrollLeft;
					posy = e.clientY + document.body.scrollTop
						+ document.documentElement.scrollTop;
				}
				return {"x":posx,"y":posy};
			},

			//Get viewport space, in X/Y on e //quirksmode.org
			getVPWH: function (){
				var x,y;
				if (self.innerHeight){ // all except Explorer
					x = self.innerWidth;
					y = self.innerHeight;
				}else if (document.documentElement && document.documentElement.clientHeight){
					// Explorer 6 Strict Mode
					x = document.documentElement.clientWidth;
					y = document.documentElement.clientHeight;
				}else if (document.body){ // other Explorers
					x = document.body.clientWidth;
					y = document.body.clientHeight;
				}
				return {"w" : x, "h" : y};
			},

			//quirksmode.org // get El Postion, continues up the tree, while
			//there is a parent, adding, to get pos relative to page
			getXYPos: function(el) {
				var oEl;
				if(this.isObject(el)) oEl = el;	
				else if(this.isString(el)) oEl = this.getEl(el);
				else return null;

				var curleft = curtop = 0;
				if (oEl.offsetParent) {
					curleft = oEl.offsetLeft
					curtop = oEl.offsetTop
					while (oEl = oEl.offsetParent) {
						curleft += oEl.offsetLeft
						curtop += oEl.offsetTop
					}
				}
				return {"x" : curleft, "y" : curtop};
			},

			//Get width and height of particular el
			getWH: function (el){
				var oEl;
				if(this.isObject(el)) oEl = el;	
				else if(this.isString(el)) oEl = this.getEl(el);
				else return null;
				return {"w": oEl.offsetWidth,"h" : oEl.offsetHeight};
			},

			//Get space available surrounding an element
			getSpaceAvail: function(oArgs){
				var oFromWH = this.getWH(oArgs.startEl);
				var oFromXY = this.getXYPos(oArgs.startEl);
				var oEndWH   = (oArgs.endEl == "window") ? this.getVPWH() : this.getWH(oArgs.endEl);

				if (oArgs.where == "bottom")
					return oEndWH.h - (oFromWH.h + oFromXY.y);
				else if(oArgs.where == "top")
					return  oFromXY.y;
				else if(oArgs.where == "left")
					return oFromXY.x;
				else if(oArgs.where == "right")
					return oEndWH.w - (oFromXY.x + oFromWH.w); 
			},
			
			removeEvent: function(obj, type, fn) {
				if (obj.removeEventListener)
					obj.removeEventListener( type, fn, false );
				else if (obj.detachEvent)
					obj.detachEvent( "on"+type, fn);
				else
					obj["on"+type] = null;
			},

            formatDate:function(date,format){
                var MONTH_NAMES=new Array('January','February','March','April','May','June','July','August','September','October','November','December','Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');var DAY_NAMES=new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sun','Mon','Tue','Wed','Thu','Fri','Sat'); 
                format=format+"";
                var result="";
                var i_format=0;
                var c="";
                var token="";
                var y=date.getYear()+"";
                var M=date.getMonth()+1;
                var d=date.getDate();
                var E=date.getDay();
                var H=date.getHours();
                var m=date.getMinutes();
                var s=date.getSeconds();
                var yyyy,yy,MMM,MM,dd,hh,h,mm,ss,ampm,HH,H,KK,K,kk,k;
                var value=new Object();
                if(y.length < 4){
                    y=""+(y-0+1900);
                }
                value["y"]=" "+y;
                value["yyyy"]=y;
                value["yy"]=y.substring(2,4);
                value["M"]=M;
                value["MMM"]=MONTH_NAMES[M-1];
                value["NNN"]=MONTH_NAMES[M+11];
                value["d"]=d;
                value["E"]=DAY_NAMES[E+7];
                value["EE"]=DAY_NAMES[E];
                value["H"]=H;
                if(H==0){
                    value["h"]=12;
                }else if(H>12){
                    value["h"]=H-12;
                }else{
                    value["h"]=H;
                }
                if(H>11){
                    value["K"]=H-12;
                }else{
                    value["K"]=H;
                }
                value["k"]=H+1;
                if(H > 11){
                    value["a"]="PM";
                }else{
                    value["a"]="AM";
                }
                value["m"]=m;
                value["s"]=s;
                while(i_format < format.length){
                    c=format.charAt(i_format);
                    token="";
                    while((format.charAt(i_format)==c) &&(i_format < format.length)){
                        token += format.charAt(i_format++);
                    }
                    if(value[token] != null){
                        result=result + value[token];
                    }else{
                        result=result + token;
                    }
                }

                return result;
            },

            popIt:function(loc,w,h){
                var fw = ((screen.availWidth-w)/2);
                var fh = ((screen.availHeight-h)/2);
                myRef = window.open(''+loc,'mywin','top='+fh+',left='+fw+',width='+w+',height='+h+',toolbar=0,resizable=0,scrollbars=0');
                myRef.focus();
            },

            //Calls from outside prototpye - inside modal's specifically
            initializeTips:function(el,type){
                var oTipEls = el.getElementsByTagName(type);
                for (var i=0;i<oTipEls.length;i++){
                    if(!oTipEls[i].className)continue;
                    if(/^oTip_static/.test(oTipEls[i].className)) continue;
                    if(/^oTip.*/.test(oTipEls[i].className)){ 
                        window["dynTip"+i] = new oTip("dynTip"+i,oTipEls[i]);
                        if(/^oTip_modal$/.test(oTipEls[i].className)) 
                            window["dynTip"+i].oTipPckg.style.zIndex = 9999;

                    }
                }
                /* Use like this for unchangin tips that have to click through - assign oTip_static class name
                <script>
                    oCoinsuranceTip = new oTip("oCoinsuranceTip",O.toolbox.tools.getEl('oCoTip'));
                    oCoinsuranceTip.dynamicTip= false;//Ajax in show
                    oCoinsuranceTip.sContent='<div class="ttPopContainer" style="text-align:left;width:200px;">Coinsurance is a set percentage you pay of the total cost of your medical services - after you meet your deductible.</div>';
                </script>
                */
            },
            initializeModals:function(el,type,obj){
                if(navigator.appVersion.indexOf("Mac")!=-1) return;
                if(navigator.appVersion.indexOf("Win")!=-1) return;

                var docEls = el.getElementsByTagName(type);
                for(var i=0;i<docEls.length;i++){
                    if(docEls[i].className=="modalMoxy"){
                        docEls[i].onclick=null;
                        EventManager.Add(docEls[i],'click',function(event){MD.show(event)});
                        docEls[i].actionHref = docEls[i].href;
                        docEls[i].href = "javascript:void(0);";
                        if(obj){
                            EventManager.Add(docEls[i],'click',function(){obj.close('local')});//Tips
                            
                        }   
                    }
                    //Hide action for modals, inside actual modal
                    if(/^close.*$/i.test(docEls[i].className)&&/^modal.*/i.test(el.className))
                        EventManager.Add(docEls[i],'click',function(){MD.hide()});
                }
            },
			alertTest: function(){alert('test')}
	}
}();


/*
 * EventManager.js
 * by Keith Gaughan
 *
 * This allows event handlers to be registered unobtrusively, and cleans
 * them up on unload to prevent memory leaks.
 *
 * Copyright (c) Keith Gaughan, 2005.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Common Public License v1.0
 * (CPL) which accompanies this distribution, and is available at
 * http://www.opensource.org/licenses/cpl.php
 *
 * This software is covered by a modified version of the Common Public License
 * (CPL), where Keith Gaughan is the Agreement Steward, and the licensing
 * agreement is covered by the laws of the Republic of Ireland.
 */

// For implementations that don't include the push() methods for arrays.
if (!Array.prototype.push)
{
    Array.prototype.push = function(elem)
    {
        this[this.length] = elem;
    }
}

var EventManager =
{
    _registry: null,

    Initialise: function()
    {
        if (this._registry == null)
        {
            this._registry = [];

            // Register the cleanup handler on page unload.
            EventManager.Add(window, "unload", this.CleanUp);
        }
    },

    /**
     * Registers an event and handler with the manager.
     *
     * @param  obj         Object handler will be attached to.
     * @param  type        Name of event handler responds to.
     * @param  fn          Handler function.
     * @param  useCapture  Use event capture. False by default.
     *                     If you don't understand this, ignore it.
     *
     * @return True if handler registered, else false.
     */
    Add: function(obj, type, fn, useCapture)
    {
        this.Initialise();

        // If a string was passed in, it's an id.
        if (typeof obj == "string")
            obj = document.getElementById(obj);
        if (obj == null || fn == null)
            return false;

        // Mozilla/W3C listeners?
        if (obj.addEventListener)
        {
            obj.addEventListener(type, fn, useCapture);
            this._registry.push({obj: obj, type: type, fn: fn, useCapture: useCapture});
            return true;
        }

        // IE-style listeners?
        if (obj.attachEvent && obj.attachEvent("on" + type, fn))
        {
            this._registry.push({obj: obj, type: type, fn: fn, useCapture: false});
            return true;
        }

        return false;
    },

    /**
     * Cleans up all the registered event handlers.
     */
    CleanUp: function()
    {
        for (var i = 0; i < EventManager._registry.length; i++)
        {
            with (EventManager._registry[i])
            {
                // Mozilla/W3C listeners?
                if (obj.removeEventListener)
                    obj.removeEventListener(type, fn, useCapture);
                // IE-style listeners?
                else if (obj.detachEvent)
                    obj.detachEvent("on" + type, fn);
            }
        }

        // Kill off the registry itself to get rid of the last remaining
        // references.
        EventManager._registry = null;
    }
};

