// DHTML Lib - compiled by scully - started 2003
// **** This only works in IE and FF - that I know about should work in any W3C browser ****
// Stuff gets added as I realize I'm repeating things I've done before
// I only add stuff when I'm 100% sure it is becoming extremely common
// Major Tidy up 2005

AltimisApplication = false;

function isIE()
{
    return navigator.appName == 'Microsoft Internet Explorer';
}
function isIE7()
{
    return navigator.appName == 'Microsoft Internet Explorer' && (navigator.appVersion.indexOf('MSIE 7') != -1);
}

function getEvent(ev)
{
    if (isIE())
        return window.event;
    else
        return ev;
}
function getEventElement(ev)
{
   return (isIE())? event.srcElement : (ev.srcElement?ev.srcElement:ev.currentTarget);
}
//*********************************************************************
//** Specific DOM/Document element location and navigation functions **
//*********************************************************************
function getElement(element) {
    // Get document element by either ID or assume it is already the element
    if (typeof element == "string") {
        try {
            return document.getElementById(element);
        } catch (e) {}
        return null;
    }
    return element;
}

function deleteElement(element)
{
    var ele = getElement(element);
    if (ele)
    {
        ele.parentNode.removeChild(ele);
    }
}

function findChildNode(element,name)
{
    // Find a child document element of element by its tag name
    // retrives and returns the document element for the FIRST located child
    if (getElement(element))
    {
    	var uls = getElement(element).childNodes;
	    var u = 0;
        for (u=0; u<uls.length; u++) 
	    {
		    if (uls[u].tagName==name)
    			return uls[u];
	    }
    }
	return null;
}

function findSiblingElement(element,elementType)
{
    // Finds a sibling document element of element by its tag name
    // retrives and returns the document element for the FIRST located child
	var ns = element==null?null:element.nextSibling;
	while (ns && ns.tagName!=elementType)
		ns = ns.nextSibling;
	return ns;
}

function findParentElementByClass(element,className)
{
    // Locates an ancestor of a given element that has a
    // specific style class name. Iteration will contoinue up the
    // entire dom ancestor tree if neccessary
	var ns = element==null?null:element;
	while (ns && !hasClassName(ns,className))
		ns =ns.parentNode;
	return ns;
}

function findChildByClass(element,name)
{
    // Find a child document element of element by its class name
    // retrives and returns the document element for the FIRST located child
    if (getElement(element))
    {
    	var uls = getElement(element).childNodes;
	    var u = 0;
        for (u=0; u<uls.length; u++) 
	    {
		    if (hasClassName(uls[u],name))
    			return uls[u];
	    }
    }
	return null;
}

function findDescendantByClass(element, name) {
    // Find a descendant document element of element by its class name
    // retrives and returns the document element for the FIRST located descendant
    if (getElement(element)) {
        var uls = getElement(element).childNodes;
        var u = 0;
        for (u = 0; u < uls.length; u++) {
            if (hasClassName(uls[u], name))
                return uls[u];
            else {
                var result = findDescendantByClass(uls[u], name);
                if (result != null) return result;
            }
        }
    }
    return null;
}

//////////////////////////////////////////////////////
// I want to do this by DOM extension e.g. Element.prototype.getElementsByClassName
// But unfortunately IE (pile of crap that it is) won't let me do it
// without a right load of messing about (see http://delete.me.uk/2004/09/ieproto.html)
// So I've decided to do it the nasty way for now
//////////////////////////////////////////////////////
function getElementsByClassName(node) {
    if (!getElement(node))
        return undefined;
	var i, j, nodes, element, matches;
	var list = new Array();

	// Get all the nodes
	if (getElement(node).getElementsByTagName) {
		nodes = getElement(node).getElementsByTagName("*");
	} else {
		return undefined;
	}

	// Iterate through the nodes
	for (i = 0; i < nodes.length; i++) {
		element = nodes.item(i);

		matches = true;
		// Iterate through the class name arguments to check the element has all of them
		for (j = 1; j < arguments.length && matches; j++) {
			// See Element.prototype.hasClassName defined below.
			if (!hasClassName(element,arguments[j])) matches = false;
		}
		if (matches) list.push(element);
	}
	return list;
}

function removeClassName(node,className)
{
    if (getElement(node) && hasClassName(node,className))
    {
    	var classNames = "";
    	// HTML, not XHTML documents
	    if (getElement(node).className) classNames = getElement(node).className;
        var p = classNames.split(" ");
        var np = [];
        var j = 0;
        for (var i = 0; i < p.length; i++) {
           if (p[i] != className) np[j++] = p[i];
        }
        getElement(node).className = np.join(" ");    
    }
}

function addClassName(node,className)
{
    if (getElement(node) && !hasClassName(node,className))
    {
    	var classNames = "";
	    if (getElement(node).className) classNames = getElement(node).className;
        getElement(node).className = classNames + ' '+className;
    }
}

function hasClassName(node,className) {
    if (getElement(node))
    {
    	var classNames = "";
    	// HTML, not XHTML documents
	    if (getElement(node).className) classNames = getElement(node).className;

    	// HTML is case-insensitive, XML and XHTML are case-sensitive
    	// RegExp flags: g: global, i: ignore case, m: multi-line
	    var flags = "gim"; //isHTML ? "gim" : "gm";
        return new RegExp("(^|\\s)" + className + "(\\s|$)", flags).test(classNames);
    }
    return false;
}

function getElementStyle(node,StyleProp) {
    // Retrive an elements acctual style (i.e. the one that is currently
    // active for rendering as opposed to the one that might not be defined in the
    // style sheet)
    if (getElement(node))
    {
        if (getElement(node).currentStyle) {
            return getElement(node).currentStyle[StyleProp];
        } else if (window.getComputedStyle) {
            var compStyle = window.getComputedStyle(getElement(node), "");
            return compStyle.getPropertyValue(StyleProp);
        }
    }
    return undefined;
}

//***********************************************
//** Specific CSS style manipulation functions **
//***********************************************

function setFocus(element)
{
    // Set 'focus' to a given element
	var df = getElement(element);
	if (df)
	    df.focus();
}

function hide(element)
{
    // Hide a specified element via its style
    var div = getElement(element);
    if (div)
    {
        div.style.display="none";
        return false;
    }
    return true;    
}

function removeTableRow(id)
{
    var div = getElement(id);
    if (div)
    {
        div.innerHTML="";
        div.style.display="none";
    }
}

function show(element)
{
    // Hide a specified element via its style
    var div = getElement(element);
    if (div)
    {
        if (div.tagName == 'TR')
        {
            if (isIE())
//                if (div.currentStyle)
                div.style.display='block'; // IE doesn't support table-row
            else
                div.style.display='table-row'; // FF doesn;t like block
        }
        else
            div.style.display='block';
        return false;
    }
    return true;    
}

function closeDiv(element)
{
    // Hide a specified element via its style
    // and totlay erase it's inner HTML (i.e. content)
    var div = getElement(element);
    if (div)
    {
        div.style.display="none";
        div.innerHTML="";
        if (div.parentNode && div.parentNode.id == 'popupHolder')
            div.parentNode.style.display='none';
        return false;
    }
    return true;    
}

function removeDiv(element)
{
    // Hide a specified element via its style
    // and totlay erase it's inner HTML (i.e. content)
    var div = getElement(element);
    if (div)
    {
        closeDiv(div);
        div.parentNode.removeChild(div);
        return false;
    }
    return true;    
}

function toggleShowHide(element,imgNode)
{
    // Toggle the show/hide display status of an element
    var div = getElement(element);
    if (div)
    {
        if (getElementStyle(div,"display")=='none')
        {
            if (div.tagName == 'TR')
            {
                if (isIE())
//                if (div.currentStyle)
                    div.style.display='block'; // IE doesn't support table-row
                else
                    div.style.display='table-row'; // FF doesn;t like block
            }
            else
                div.style.display='block';
            if (imgNode)
            {
    			imgNode.src= ILib.Resource('gfx/collapse.gif');
            }
        }
        else
        {
            div.style.display='none';
            if (imgNode)
            {
    			imgNode.src= ILib.Resource('gfx/expand.gif');
            }
        }
    }
}

function printCompleted(element)
{
    var ele = getElement(element);
    if (ele)
    {
        // And get shut
        element.parentNode.removeChild(ele);
        var divs = document.getElementsByTagName('DIV');
        for (d = 0; d < divs.length; d++)
        {
            removeClassName(divs[d],'noprn');
        }
    }
}

function printDiv(eleId)
{
    var printWhat = getElement(eleId);
    if (printWhat)
    {
        var divs = document.getElementsByTagName('DIV');
    
        var pDiv = document.createElement('DIV');
        pDiv.id = "$PRN$"+eleId
        pDiv.innerHTML = printWhat.innerHTML;
        pDiv.onafterprint = new Function("printCompleted(\""+pDiv.id+"\")");
   
        for (d = 0; d < divs.length; d++)
        {
                addClassName(divs[d],'noprn');
        }
    
        addClassName(pDiv,'prn');
        document.body.insertBefore(pDiv,document.body.firstChild);
    
        window.print();
        // Let the onafterprint tidy up
    }
}
function setAlpha(ele,val)
{
    setOpacity(ele,val)
}
function setOpacity(ele,val) {
    node = getElement(ele);
    if (node)
    {
        if (isIE()) {
            try {
                if (node.style.filter['alpha'] && node.style.filter['alpha'] != null)
                    node.style.filter['alpha'].opacity = val*100;
                else
                    node.style.filter = 'alpha(opacity='+(val*100)+')';
            } catch (e) { }
        } else
            try {
            node.style.opacity = val;
            } catch (e) { }
    }
}
function setOpacityDelta(ele,val) {
    node = getElement(ele);
    if (node)
    {
        if (isIE()) {
            try {
                node.filters['alpha'].opacity = parseInt(parseInt(node.filters['alpha'].opacity)+parseInt((val*100)));
            } catch (e) { }
        } else {
            try {
            node.style.opacity = ''+parseFloat(parseFloat(node.style.opacity)+parseFloat(val));
            }catch (e) { }
        }
    }
}

//*****************************************************************
//** Specific DHTML positioning,size,etc. manipulation functions **
//*****************************************************************
function getInsideWindowWidth() {
    // Return the available content width space in browser window
    if (typeof( window.innerWidth ) == 'number')
        return window.innerWidth;
    else if (document.documentElement && document.documentElement.clientWidth)
        return document.documentElement.clientWidth;
    else if (document.body && document.body.clientWidth)
        return document.body.clientWidth;
    return 0;
}
function getInsideWindowHeight() {
    // Return the available content height space in browser window
    if (typeof( window.innerHeight ) == 'number')
        return window.innerHeight;
    else if (document.documentElement && document.documentElement.clientHeight)
        return document.documentElement.clientHeight;
    else if (document.body && document.body.clientHeight)
        return document.body.clientHeight;
    return 0;
}
// Retrieve the x coordinate of a positionable object
function getElementLeft(obj)  {
    var eElement = getElement(obj);
    if (eElement)
    {
        var nLeftPos = eElement.offsetLeft;          // initialize var to store calculations
        var eParElement = eElement.offsetParent;     // identify first offset parent element  
        while (eParElement != null)
        {                                            // move up through element hierarchy
            nLeftPos += eParElement.offsetLeft;      // appending left offset of each parent
            eParElement = eParElement.offsetParent;  // until no more offset parents exist
        }
        return parseInt(nLeftPos);
    }
    return undefined;
}

// Retrieve the y coordinate of a positionable object
function getElementTop(obj)  {
    var eElement = getElement(obj);
    if (eElement)
    {
        var nTopPos = eElement.offsetTop;            // initialize var to store calculations
        var eParElement = eElement.offsetParent;     // identify first offset parent element  
        while (eParElement != null)
        {                                            // move up through element hierarchy
            nTopPos += eParElement.offsetTop;        // appending top offset of each parent
            eParElement = eParElement.offsetParent;  // until no more offset parents exist
        }
        return parseInt(nTopPos);                    // return the number calculated
    }
    return undefined;
}

// Retrieve the rendered width of an element
function getElementWidth(obj)  {
    var elem = getElement(obj);
    var result = 0;
    if (elem.offsetWidth) {
        result = elem.offsetWidth;
    } else if (elem.clip && elem.clip.width) {
        result = elem.clip.width;
    } else if (elem.style && elem.style.pixelWidth) {
        result = elem.style.pixelWidth;
    }
    return parseInt(result);
}

function getElementHeight(obj)  {
    // Retrieve the rendered height of an element
    var elem = getElement(obj);
    var result = 0;
    if (elem.offsetHeight) {
        result = elem.offsetHeight;
    } else if (elem.clip && elem.clip.height) {
        result = elem.clip.height;
    } else if (elem.style && elem.style.pixelHeight) {
        result = elem.style.pixelHeight;
    }
    return parseInt(result);
}
function getElementBottom(obj) {
    return getElementTop(obj)+getElementHeight(obj);
}

function getElementRight(obj) {
    return getElementLeft(obj)+getElementWidth(obj);
}

function getElementScreenLeft(el) {
	if (el.currentStyle) {
		if (el.currentStyle.left == "auto")
			return 0;
		else
			return parseInt(el.currentStyle.left);
	}
	else {
		return el.style.pixelLeft;
	}
}

function getElementScreenTop(el) {
	if (el.currentStyle) {
		if (el.currentStyle.top == "auto")
			return 0;
		else
			return parseInt(el.currentStyle.top);
	}
	else {
		return el.style.pixelTop;
	}
}

function moveTo(element, x, y) {
    // Position an object at a specific pixel coordinate
    var ele = getElement(element);
    if (ele) {
        // equalize incorrect numeric value type
        var units = (typeof ele.left == "string") ? "px" : 0 
        ele.left = x + units;
        ele.top = y + units;
    }
}

function moveBy(element, deltaX, deltaY) {
    // Move an object by x and/or y pixels
    var ele = getElement(element);
    if (ele) {
        // equalize incorrect numeric value type
        var units = (typeof ele.left == "string") ? "px" : 0 
        ele.left = getElementLeft(ele) + deltaX + units;
        ele.top = getElementTop(ele) + deltaY + units;
    }
}

//////////////////////////////////////////////////////////////////////////////
//** Routines/Function/Methods and globals for managing dynamic data retrieval
//**  (XmlHttp) for the page
//**
//////////////////////////////////////////////////////////////////////////////
var gaHttp = new Array(); // Gloabkl array to hold the Http requests currently active

function freeHttpRequest(name) {
    ILib.FreeHttpRequest(name);
}

function findHttpRequest(name) {
    return ILib.FindHttpRequest(name);
}

function getHttpRequest(name) {
    return ILib.GetNamedHttpRequest(name);
}
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
function NewWindow(url,title,w,h,scroll,pos)
{
//    if(pos=="random")
//    {
//        LeftPosition=(screen.width)?Math.floor(Math.random()*(screen.width-w)):100;
//        TopPosition=(screen.height)?Math.floor(Math.random()*((screen.height-h)-75)):100;
//    }
//    if(pos=="center")
//    {
//        LeftPosition=(screen.width)?(screen.width-w)/2:100;
//        TopPosition=(screen.height)?(screen.height-h)/2:100;
//    }
//    else if((pos!="center" && pos!="random") || pos==null)
//    {
//        LeftPosition=0;TopPosition=20;
//    }
//    settings='width='+w+',height='+h+',top='+TopPosition+',left='+LeftPosition+',scrollbars=yes,location=no,directories=no,status=yes,menubar=no,toolbar=no,resizable=yes';
    settings='scrollbars=yes,location=no,directories=no,status=yes,menubar=no,toolbar=no,resizable=yes';
    return window.open(url,title,settings);
}

function getURLDomain(url)
{
    try {
        var pattern = /(\w+):\/\/([\w.]+)\/(\S*)/;
        var result = url.match(pattern);
        return result[2];
    } catch (e) { }
    return null;
}
