function $(id) { return document.getElementById(id); }

/*
Credit: david_kw
URL: http://www.codingforums.com/archive/index.php/t-105808.html
*/
function codeSelect(sId){
//if string, must be Id
//else, must be an element (e.g. this)
if (typeof(sId) == 'string')
    myDiv = $(sId);
else
    myDiv = sId;

if (window.getSelection) {
var selection = window.getSelection();
if (selection.setBaseAndExtent) { /* for Safari */
selection.setBaseAndExtent(myDiv, 0, myDiv, 1);
} else { /* for FF, Opera */
var range = document.createRange();
range.selectNodeContents(myDiv);
selection.removeAllRanges();
selection.addRange(range);
}
} else { /* for IE */
var range = document.body.createTextRange();
range.moveToElementText(myDiv);
range.select();
}
}

//sets the ondblclick event for all code.regex elements
//to select the text in the element
function setRegExDblClick()
{
    codeTags = document.getElementsByTagName("code");
    var matchRegEx = /\b(?:regex|string|match|range)\b/;

    for (i = 0; i < codeTags.length; i++)
    {
        var element = codeTags[i];

        if (matchRegEx.test(element.className))
            element.ondblclick = function()
            {
                codeSelect(this);
            }
    }
}

/*
sets the onclick event for all anchor tags
that link to an anchor on the same page

action:
if the clicked link is for a colapsed section, expand it.
*/
function setAnchorOnClick()
{
    anchorTags = document.getElementsByTagName("a");

    for (i = 0; i < anchorTags.length; i++)
    {
        var element = anchorTags[i];

        if (element.href.indexOf(location.href + "#") == 0)
        {
            // +1 for "#"
            gotoAnchor = element.href.substring(location.href.length + 1);
            
            if (gotoAnchor.length != 0)
                element.onclick = new Function("show('" + gotoAnchor + "');");
        }
    }
}

/*
if the new hash is for a colapsed section, expand the section

Note: since the user can always click the section to open it,
this behavior isn't required for proper functionality of the site.
However, it is supplied for the browsers which support the feature.

(supported in IE 8 and Firefox 3.6 Alpha 1)
*/
onhashchange = function() {
    if (location.hash.length > 1)
        show(location.hash.substring(1));
};

/*
JavaScript Tree Control
http://gethelp.devx.com/techtips/dhtml_pro/10min/10min0702/td072602-1.asp
(slightly modified)
*/

var openImg = new RegExp("open(?=\\.\\w+$)");
var closedImg = new RegExp("closed(?=\\.\\w+$)");

/*
Toggle.js
http://www.webdeveloper.com/forum/showpost.php?s=f58a1ad12bec81ee64cb647699db9225&p=770885&postcount=2
*/
function toggle(id) {
setState(id, -1);
}

function show(id) {
setState(id, 1);
}

function hide(id) {
setState(id, 0);
}

function setState(id, state) {
  branch = id + "_branch";
  folder = id + "_folder";

  var CState = $(branch);
  var objImg = $(folder);

  if (CState == null || objImg == null)
    return;

  if (state != 0 && CState.style.display == "none") {
    //show
    CState.style.display = "block";

    objImg.alt='-';
    objImg.src = objImg.src.replace(closedImg, "open");
  } else if (state != 1) {
    //hide
    CState.style.display = "none";

    objImg.alt='+';
    objImg.src = objImg.src.replace(openImg, "closed");
  }
}
