/* Andy Gale Javascript Library - 2006
 *
 * 30/01/2006 - getElementPosition function
 * 14/07/2006 - centerElement function
 *
 */

// getElementPosition(ele)
//
// Get element position on page (including any scrolling)
//
// Returns an object with the following properties
//  - offsetHeight, offsetTop, offsetLeft, offsetWidth

function getElementPosition(ele) 
{
    offsetHeight = 0;
    offsetTop    = 0;
    offsetLeft   = 0;
    offsetWidth  = 0;

    offset = ele;

    while (offset) { 
        offsetTop    += offset.offsetTop;
        offsetHeight += offset.offsetHeight;
        offsetWidth  += offset.offsetWidth;
        offsetLeft   += offset.offsetLeft;
        offset = offset.offsetParent;
    }

    o = new Object;
    o.offsetHeight = offsetHeight;
    o.offsetWidth  = offsetWidth;
    o.offsetTop    = offsetTop;
    o.offsetLeft   = offsetLeft;

    return o;
}

/*
 * centerElement
 *
 * Center element in window 
 *
 * Parameters
 * 
 * ele     - element to center
 * ewidth  - desired element width
 * eheight - desired element height
 */

function centerElement(ele, ewidth, eheight)
{
    w = windowSize();

    width  = w.width;
    height = w.height; 

    tleft = (width / 2) - (ewidth / 2);
    ttop  = (height / 2) - (eheight / 2);
   
    s = scrollXY();
    
    tleft += s.x;
    ttop  += s.y;

    ele.style.width  = ewidth + 'px';
    ele.style.height = eheight + 'px';
    ele.style.left   = tleft + 'px';
    ele.style.top    = ttop + 'px';
}

/*
 * scrollXY
 *
 * Get window scroll x and y
 *
 */

function scrollXY()
{
    var scrOfX = 0, scrOfY = 0;
    
    if (typeof(window.pageYOffset) == 'number') {
        // Netscape compliant
        scrOfY = window.pageYOffset;
        scrOfX = window.pageXOffset;
    } else if (document.body && ( document.body.scrollLeft || document.body.scrollTop)) {
        // DOM compliant
        scrOfY = document.body.scrollTop;
        scrOfX = document.body.scrollLeft;
    } else if (document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
        //IE6 standards compliant mode
        scrOfY = document.documentElement.scrollTop;
        scrOfX = document.documentElement.scrollLeft;
    }

    s = new Object;
    
    s.x = scrOfX;
    s.y = scrOfY;
    
    return s;
}

/*
 * windowSize
 *
 * Get windowSize 
 * 
 */

function windowSize() {
    var myWidth = 0, myHeight = 0;
    if (typeof(window.innerWidth) == 'number') {
        // Non-IE
        myWidth  = window.innerWidth;
        myHeight = window.innerHeight;
    } else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
        //IE 6+ in 'standards compliant mode'
        myWidth  = document.documentElement.clientWidth;
        myHeight = document.documentElement.clientHeight;
    } else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
        //IE 4 compatible
        myWidth  = document.body.clientWidth;
        myHeight = document.body.clientHeight;
    }

    w = new Object;

    w.width  = myWidth;
    w.height = myHeight;

    return w;
}


// From Javascript & DHTML cookbook By Danny Goodman

function getIEVersionNumber()
{
    var ua = navigator.userAgent;
    var MSIEOffset = ua.indexOf('MSIE');
    
    if (MSIEOffset == -1) { 
        return 0;
    } else {
        return parseFloat(ua.substr(MSIEOffset + 5, ua.indexOf(";", MSIEOffset)));
    }
}

/* 
 * getLabelById
 *
 * Get label with for="id"
 *
 */

function getLabelById(id) 
{
    labels = document.getElementsByTagName('label');
    result = false;    

	for (i = 0; i < labels.length; i++) { 
        if (labels[i].htmlFor == id) { 
            result = labels[i];
            break;
        }
    }
    
    if (!result) {
        return false;
    } else {
        return result;
    }
}                       


