﻿// This is a javascript library with cross browser functions.

//Add an event on the given HTML "element"
//the strEventType must not include the "on" becuase it's for IE only.
function AddJsEvent(element, strEventType, refFunctionReference) {
    if (element.attachEvent) {
        //ie method.  The "on" must be appended to the event type.
        element.attachEvent("on" + strEventType, refFunctionReference)
    } else {
        if (element.addEventListener) {            
            //mozilla method;  the 3rd parameter indicates to only run the function on bubbling events
            //since ie doesn't supoort "capturing" and "target" events.
            element.addEventListener(strEventType, refFunctionReference, false)
        } else {
            alert("Javascript error: Function AddJsEvent must be modified to add an event listener for this browser");
            }
        }
}
//Remove an event on the given "element"
function DetachJsEvent(element, strEventType, refFunctionReference) {
    if (element.detachEvent) {
        //ie method.  The "on" must be appended to the event type.
        element.detachEvent("on" + strEventType, refFunctionReference)
    } else {
        if (element.removeEventListener) {            
            //mozilla method;  the 3rd parameter indicates to only run the function on bubbling events
            //since ie doesn't supoort "capturing" and "target" events.
            element.removeEventListener(strEventType, refFunctionReference, false)
        } else {
            alert("Javascript error: Function DetachJsEvent must be modified to add an event listener for this browser");
            }
        }
}
//Get the property name of an element.
function GetPropertyFromElement(element, propertyName) {
    if (element.getAttribute(propertyName)) {
     //ie way
        return element.getAttribute(propertyName)
    } else {
     //w3c way
        var str
        eval("str=element."+propertyName)
        return str
    }
}
//Fires an event of eventType.  No parameters are allowed to be passed to the function.
//(however the "event" still must be part of the function for firefox to work correctly.  e.g.  
//     <input .... onclick="DoThis(event)" />
function fireEvent(targetElem, eventType) {
    if (targetElem.fireEvent) {
        //ie way.  Append the word "on" to the IE event
        targetElem.fireEvent("on" + eventType)
    } else if (document.createEvent) {
        //firefix way
        var evt = document.createEvent("HTMLEvents");
        evt.initEvent(eventType, true, true);
        targetElem.dispatchEvent(evt);
    };    
}
  
//**********************************************************************
//** 
//** The cbEvent object handles the browser event object for IE and W3C 
//** browsers.  (NOTE: The aEvent parameter is only for  
//**  It's implemented like this:
/*    
        function HandOnClickEvent(aEvent) (
           // get a reference to the event
           var e = new cbEvent(aEvent)
           // now do one of several things:
            var target = e.target()   // get the target element
            var keyPressed = e.keyPressed()  //get the ascii code of the key that was pressed.
            e.preventDefault()  // cancel the event
            e.stopPropagation()  // stop the event from bubbling.                       
*/
//**********************************************************************
function cbEvent(aEvent) {
	//var theEvent
	theEvent = aEvent || window.event;
//    thi	s.theEvent = (window.event ? window.event : aEvent);
}
cbEvent.prototype.event = function () {
    return theEvent    
}
cbEvent.prototype.target = function () {
    return (theEvent.srcElement ? theEvent.srcElement : theEvent.target)
}
cbEvent.prototype.keyPressed = function () {
    var key
    if (window.event) {
        key = theEvent.keyCode
    } else {        
        key = (theEvent.charCode != 0 ? theEvent.charCode : theEvent.keyCode);
    }
    return key
}
cbEvent.prototype.preventDefault = function () {
	if (window.event) {
	    //ie specific
	    theEvent.returnValue = false;
	} else {
	    //w3c specific
	    theEvent.preventDefault();
	}
}
cbEvent.prototype.stopPropagation = function ()  {
	if (window.event) {
	    //ie specific
	    theEvent.cancelBubble = true;			    
	} else {
	    //w3c specific
	    theEvent.stopPropagation();
	}
}
//***************************************************


