/**
 * tags.js
 *
 * 09/25/06 JLS P6847  Updated the getAttributeValue function to handle
 *          errors thrown by the eval function.  Also added getObject
 *          and getStyle for cross-browser compliance.
 * 02/07/07 JLS P7054  Added determineEvent, determineSourceElement,
 *              translateToNeededObject, findFirstParentWithinAnId,
 *              and getParent functions for working with cross-browser
 *              DOM events and tag objects.
 * 04/05/07 JLS P7106  Added setAttributeValues, createHTMLElement,
 *              createHTMLTextNode, and alignObjects functions.
 * 04/27/07 JLS P7107  Added deleteChildNodes.
 */
    /**
     *  This retrieves the value of an attribute that exists for a tag.  For
     *  any non-html attributes, this ensures the retrieval of the value for
     *  Netscape browsers.
     *  @param tagObj - The tag object containing all attributes and functions.
     *  @param String attrName - The attribute of the tag to retrieve the value
     *      of.
     *  @return String - Returns the String value assigned to the attribute for
     *      the tag object passed in.
     */
    function getAttributeValue(tagObj, attrName)
    {
		var attrValue = null; 
		
        try
        {
		    attrValue = eval("tagObj." + attrName);
		}
		catch(ex)
		{
		    // ignore exception.
		}
		
		if(attrValue == null)
		{
		    // execute a lookup of the attribute value:
		    if(tagObj.attributes)
		    {
		        var attribute = tagObj.attributes[attrName];
		        
		        if(attribute != null)
		            attrValue = attribute.value;
		    }
		}

		return attrValue;

    }

    /**
     * Sets a list of name/value attributes on the tagObj passed in.
     * @arg tagObj The tag object on which to set the attributes.
     * @arg attrs - The named array which identifies each attribute
     *      and value:  attrs["border"] = "0".
     */
    function setAttributeValues(tagObj, attrs)
    {
        for(var attr in attrs)
        {
            try
            {
                tagObj.setAttribute(attr, attrs[attr]);
            }
            catch(ex)
            {
                // ignore.
            }
        }
    }

	/**
	 * For more information about objType, see a JavaScript reference
	 * for document.createElement function.
	 */
	function createHTMLElement(parentObj, objType, objIdToCreate, attributesArray)
	{
	    if(objIdToCreate == null)
	        return null;
	        
	    try
	    {
		    var newObj = document.createElement(objType);
		    
		    newObj.id = objIdToCreate;
		    if(attributesArray != null)
			    for(var attr in attributesArray)
			    {
			        try
			        {
					    newObj.setAttribute(attr, attributesArray[attr]);
					}
					catch(ex)
					{
					    // ignore, try the next attribute.
					}
			    }
	
		    parentObj.appendChild(newObj);
		    
		    return newObj;
		}
		catch(ex)
		{
		    return null;
		}
	}
	
	/**
	 * Creates a text node for the parentObj passed in.  The object
	 * is returned (or null if the object is not created or could
	 * not be appended to the parent). 
	 */
	function createHTMLTextNode(parentObj, text)
	{
		try
		{
			var textNode = document.createTextNode(text);
			parentObj.appendChild(textNode);
			return textNode;
		}
		catch(ex)
		{
		    return null;
		}
	}

	/**
	 * Deletes all child nodes of the object passed in.
	 */    
    function deleteChildNodes(parentObj)
    {
        if(parentObj == null || parentObj.childNodes === undefined 
        	|| parentObj.childNodes == null || parentObj.childNodes.length <= 0)
        	return;
        
        for(var loop = 0; loop < parentObj.childNodes.length; loop++)
        {
			try
			{
            	parentObj.removeChild(parentObj.childNodes[loop]);
            }
            catch(ex)
            {
                // unable to remove the child node.
            }
        }
    }
    
	/**
	 * Executes a cross-browser object lookup.
	 * @param id = The value set in a tag's id attribute.
	 */
	function getObject(id)
	{
		var obj = null;
		
		if (document.getElementById)
		{	
			// standards-compliant mechanism
			obj = document.getElementById(id);
		}
		else if (document.all)
		{	// older IE mechanism
			obj = document.all[id];
		}
		else if (document.layers)
		{	
			// Netscape 4 mechanism
			obj = document.layers[id];
		}
		
		return obj;
	}
	
	/**
	 * Retrieves the object's styles.
	 * @return - The style object for the object specified.
	 */
	function getStyle(object)
	{
		var style = null;
		
	    if(object.style)
	    	style = object.style;
	    else
	        style = object;
	
		return style;
	}

	function determineEvent(e)
	{
		if (typeof e == 'undefined') e = window.event;
		if (typeof e.layerX == 'undefined') e.layerX = e.offsetX;
		if (typeof e.layerY == 'undefined') e.layerY = e.offsetY;
		return e;
	}
	
	/**
	 * Determines the source element of an event (ie. the element 
	 * clicked on). For the TTI Website, the Popup tag creates a child
	 * div that has "Handle" appended as it's ID.  This function ignores
	 * that object and returns it's parent.
	 */
	function determineSourceElement(e)
	{
		var id = null;
		var obj = null;

		window.status = "determineSourceElement called.";
	    if(e.srcElement)
	    {
	        return translateToNeededObject(e.srcElement);
	    }
	    else if(e.target)
	    {
	    	return translateToNeededObject(e.target);
	    }
	    else
	    {
			window.status += "    No target or srcElement found.";
	    }
	    
	    return null;
	}
	
	/**
	 * This will ensure the desired object is retrieved.  "Handle" id
	 * is created by the PopupTag used on the TTI website.  This 
	 * function ensures that the needed object is retrieved especially
	 * when dealing with Popup tag events.
	 */
	function translateToNeededObject(obj)
	{
        var id = new String(obj.id);
        if(id.indexOf("Handle", 0) > 0)
        {
            id = id.substring(0, id.indexOf("Handle"));
            return getObject(id);
        }
        
        return obj;
	}
	
	/**
	 * Seeks the first parent of obj that has an id and returns it.
	 * If none found having an id, then null is returned.
	 */
	function findFirstParentWithAnId(obj)
	{
	    var tmpObj = translateToNeededObject(getParent(obj));

		while(tmpObj != null && (tmpObj.id == null || tmpObj.id == ""))
		{
		    tmpObj = translateToNeededObject(getParent(tmpObj));
		}

		return tmpObj;
	}
	
	/**
	 * Retrieves the object's parent, or null if no parent exists.
	 */
	function getParent(obj)
	{
		if(obj.parentNode)
			return obj.parentNode;

	    if(obj.parentElement)
	    	return obj.parentElement;
	    
	    return null;
	}


	var currentPopup = null;
	var MOVE_TOBACK_INDEX = 50;
	var MOVE_TOFRONT_INDEX = 60;

	/**
	 * Assigns the object specified to the MOVE_TOFRONT_INDEX z-index.
	 */
    function moveToFront(whichObject)
    {
        var style = getStyle(whichObject);
        
        if(style != null)
        {
	        style.zIndex = MOVE_TOFRONT_INDEX;
	    }
    }

	/**
	 * Assigns the object specified to the MOVE_TOBACK_INDEX z-index.
	 */
    function moveToBack(whichObject)
    {
    	var style = getStyle(whichObject);
    	
    	if(style != null)
    	{
	    	style.zIndex = MOVE_TOBACK_INDEX;
	    }
    }

	/**
	 * This function toggles the show/hide state of a <div> section.
	 * parameter 'whichLayer' is the div's id.  Moves the window to the
	 * front or back depending on the toggle function (hide/show) being executed. 
	 */		 
	function toggleWindow(whichLayer)
	{
	    var obj = getObject(whichLayer);

		if(currentPopup != null)
			moveToBack(currentPopup);

		var style2 = null;
			    
	    if(obj != null)
	    {
	        currentPopup = obj;
	        style2 = getStyle(currentPopup);
		}

		if(style2 && style2 != null)
		{
			style2.display = (style2.display != "none") ? "none" : "block";
			(style2.display != "none") ? moveToFront(currentPopup) : moveToBack(currentPopup);
		}	
	}	
	
	/**
	 * Moves the shimObj parameter under the popupObj.  This allows the
	 * second parameter to be used as a shim in IE6 where the shimObj is 
	 * an iframe and is used to hide select drop downs and flash objects. 
	 */
    function alignObjects(popupObj, shimObj)
    {
        try
        {
			if(shimObj == null || popupObj == null)
				return;
				
			var shimRef = shimObj;
	        
			// the !document.all check restricts this to IE only.
		    if(!popupObj || !shimRef || !document.all)
		    {
		        // alert("Exiting without setting shim.");
		    	return;
		    }
		    
		    var popupStyle = getStyle(popupObj);
		    var shimStyle = getStyle(shimObj);
		    
		    // divStyle.display = "block";
		    shimStyle.width = popupObj.offsetWidth;
		    shimStyle.height = popupObj.offsetHeight;

		    shimStyle.top = popupStyle.top
		    shimStyle.left = popupStyle.left;

		    shimStyle.zIndex = popupStyle.zIndex - 1;
		    shimStyle.display = "block";
		}
		catch(ex)
		{
		   // alert(ex.message);
		   // ignore, do not interrupt process.
		}        
    }
	
