function utlSetDivVisible(szDivID, iState) // 1 visible, 0 hidden
{
    if(document.layers)    //NN4+
    {
       document.layers[szDivID].visibility = iState ? "show" : "hide";
    }
    else if(document.getElementById)      //gecko(NN6) + IE 5+
    {
        var obj = document.getElementById(szDivID);
        obj.style.visibility = iState ? "visible" : "hidden";
    }
    else if(document.all)       // IE 4
    {
        document.all[szDivID].style.visibility = iState ? "visible" : "hidden";
    }
}

function utlSetSize(doc, id, width, height)
{
    if(doc.layers)    //NN4+
    {
       doc.layers[id].width = width;
       doc.layers[id].height = height;
    }
    else if(doc.getElementById)      //gecko(NN6) + IE 5+
    {
        var obj = doc.getElementById(id);
        obj.style.width = width;
        obj.style.height = height;
    }
    else if(doc.all)       // IE 4
    {
        doc.all[id].style.width = width;
        doc.all[id].style.height = height;
    }
}

function utlGetRefToDiv(divID, /* optional */ oDoc) 
{
    if( document.getElementById ) {
        return document.getElementById(divID); 
    }
    if( document.all ) {
        return document.all[divID]; 
    }
    if( !oDoc ) { 
        oDoc = document; 
    }
    if( document.layers ) {
        if( oDoc.layers[divID] ) { 
            return oDoc.layers[divID]; 
        } else {
            // repeatedly run through all child layers
            for( var x = 0, y; !y && x < oDoc.layers.length; x++ ) {
                //on success, return that layer, else return nothing
                y = utlGetRefToDiv(divID,oDoc.layers[x].document); 
            }
            return y; 
        }
    }
    return false;
}

function utlGetRefToDivMod( divID, /* optional */ oDoc ) 
{
  if( !oDoc ) { oDoc = document; }
  if( document.layers ) {
    if( oDoc.layers[divID] ) { return oDoc.layers[divID]; } else {
      for( var x = 0, y; !y && x < oDoc.layers.length; x++ ) {
        y = utlGetRefToDivMod(divID,oDoc.layers[x].document); }
      return y; } }
  if( document.getElementById ) { return oDoc.getElementById(divID); }
  if( document.all ) { return oDoc.all[divID]; }
  return document[divID];
}

function utlSetText(obj, txt) {
    obj.firstChild ? obj.firstChild.data = txt : 
        obj.appendChild(document.createTextNode(txt))
}

function utl_setHtmlInner(obj, html) {
    obj.innerHTML = html;
}

function utl_setHtmlDoc(doc, html) {
    doc.open();
    doc.write(html);
    doc.close();
}

function utl_setHtmlInFrame( frameId, elId, html )
{
    var fr = document.getElementById(frameId);
    var el = fr.document.getElementById(elId);
    utl_setHtmlInner(el, html);
}

/* getElementById and getElementByName does not seem to work for iframes 
 * on Firefox 2.0.0.7. However, referring to "frames" seems to work,
 * so we iterate over them looking for the name (the id does not seem
 * inquirable). */
function utlFindIFrameByName(nm)
{
    for( var i = 0; i < frames.length; i++ ) {
        ifrm = frames[i];
        if( nm == ifrm.name ) {
            return ifrm;
        }
    }

    return null;
}

function utlGetNodeList()
{
    var newNodeList = new Array();

    nodelist = document.childNodes;
    // find out if we got anything interesting
    for(i = 0; i < nodelist.length; i++) {
        // test for an element else ignore
        if(nodelist[i].nodeType != 1)	{
            continue;
        }

        // if no child nodes we ignore the node
        if( ! nodelist[i].hasChildNodes() ) {
            continue;
        }

        // else get 'em 
        newnodelist = nodelist[i].childNodes;
        // do something or keep on going
    }

    return nodelist;
}

/* All IDs are supposed to be unique, but at least we can get 
 * all that share the same prefix. */
function utlGetElementsByIdPrefix(arr, inRoot, prefix)
{
    if( typeof inRoot.firstChild == 'undefined' ) {
        return;
    }
    var elem = inRoot.firstChild; 
    while( elem != null ) {
        if( typeof elem.firstChild != 'undefined' ) {
            utlGetElementsByIdPrefix(arr, elem, prefix);
        } 

        if( typeof elem.id!= 'undefined' ) {
            var reg = new RegExp ( '^'+prefix+'.*' ); 
            if( elem.id.match(reg) ) {
                arr.push(elem); 
            }
        } 
        elem = elem.nextSibling; 
    }
}

/* Credit for this function goes to Mark Wilton-Jones.
 * Reference: http://www.howtocreate.co.uk/perfectPopups.html */
function utlAutosizeAndPositionWindowToContent(divId) 
{
    var extra = 200;
    var x = window;

    var H = utlGetRefToDivMod( divId, x.document ); 
    if( !H ) { 
        return false; 
    }

    var oW = H.clip ? H.clip.width : H.offsetWidth; 
    if( !oW ) { 
        return false; 
    }
    var oH = H.clip ? H.clip.height : H.offsetHeight; 
    if( !oH ) { 
        return false; 
    }

    if( x.opera ) { oH += 24; oW += 32; }
    x.resizeTo( oW + extra, oH + extra );

    var myW = 0, myH = 0, d = x.document.documentElement, b = x.document.body;

    if( x.innerWidth ) { 
        myW = x.innerWidth; myH = x.innerHeight; 
    } else if( d && d.clientWidth ) { 
        myW = d.clientWidth; myH = d.clientHeight; 
    } else if( b && b.clientWidth ) { 
        myW = b.clientWidth; myH = b.clientHeight; 
    }

    if( x.opera && !document.childNodes ) { myW += 16; oW += 64; }

    x.resizeTo( oW + ( ( oW + extra ) - myW ), oH + ( (oH + extra ) - myH ) );

    // oW = oW + ( (oW + extra) - myW ); 
    // oH = oH + ( (oH + extra) - myH );
    var scW = screen.availWidth ? screen.availWidth : screen.width;
    var scH = screen.availHeight ? screen.availHeight : screen.height;
    x.moveTo(Math.round((scW-oW)/2),Math.round((scH-oH)/2));

    if( x.focus ) { x.focus(); }

    return false;
}

/* Original source from: 
 * http://www.fleegix.org/articles/2006/05/30/getting-the-scrollbar-width-in-pixels */
function utlGetScrollerWidth() {
    var scr = null;
    var inn = null;
    var wNoScroll = 0;
    var wScroll = 0;

    // Outer scrolling div
    scr = document.createElement('div');
    scr.style.position = 'absolute';
    scr.style.top = '-1000px';
    scr.style.left = '-1000px';
    scr.style.width = '100px';
    scr.style.height = '50px';
    // Start with no scrollbar
    scr.style.overflow = 'hidden';

    // Inner content div
    inn = document.createElement('div');
    inn.style.width = '100%';
    inn.style.height = '200px';

    // Put the inner div in the scrolling div
    scr.appendChild(inn);
    // Append the scrolling div to the doc
    document.body.appendChild(scr);

    // Width of the inner div sans scrollbar
    wNoScroll = inn.offsetWidth;
    // Add the scrollbar
    scr.style.overflow = 'auto';
    // Width of the inner div width scrollbar
    wScroll = inn.offsetWidth;

    // Remove the scrolling div from the doc
    document.body.removeChild(document.body.lastChild);

    // Pixel width of the scroller
    return (wNoScroll - wScroll);
}

function utlOpenWindow(height, width, title, url, scroll)
{
    var left = parseInt((screen.availWidth/2) - (width/2));
    var top = parseInt((screen.availHeight/2) - (height/2));
    var windowFeatures = "width=" + width + ",height=" +
        height + ",status,resizable,left=" + left + ",top=" + top
        + "screenX=" + left + ",screenY=" + top;
    window.open(url, "subWind", windowFeatures);
}
