var pWindowHandle = "";
var WINDOW_BASE_NAME = "POPUP_";

function popupWindow(url, width, height, attributes, windowName)
{
	var	args = "width=" + width + ",height=" + height + ",scrollbars=yes,menubar=no";
	var	foo;

	if (attributes && ((typeof attributes) == "string")) {
		args += (attributes.indexOf("resize") != -1) ? ",resizable=yes" : ",resizeable=no";
		args += (attributes.indexOf("tool") != -1) ? ",toolbar=yes" : ",toolbar=no";
		args += (attributes.indexOf("loc") != -1) ? ",location=yes" : ",location=no";
		args += (attributes.indexOf("status") != -1) ? ",status=yes" : ",status=no";
	} else if (attributes) {	// this is for backwards compatibility...
		args += ",resizeable=no,toolbar=yes,location=no,status=no";
	} else {
		args += ",resizeable=no,toolbar=no,location=no,status=no";
	}	

	if (!windowName) {
		pWindowHandle = WINDOW_BASE_NAME + (Math.round(89999*Math.random() + 10000)).toString();
	} else {
		pWindowHandle = windowName;
	}
		
	foo = window.open(url, pWindowHandle, args);

	if (windowName)
		foo.focus();
}

function popupWindowReturn(url, width, height, attributes)
{
	popupWindow(url, width, height, attributes);
	return pWindowHandle;
}

// handle multiple window.onloads
function addOnloadEvent(fnc) {
    if (typeof window.addEventListener != "undefined")
        window.addEventListener("load", fnc, false);
    else if (typeof window.attachEvent != "undefined") {
        window.attachEvent("onload", fnc);
    }
    else {
        if (window.onload != null) {
            var oldOnload = window.onload;
            window.onload = function (e) {
                oldOnload(e);
                window[fnc]();
            };
        }
        else
            window.onload = fnc;
    }
}

// add support to make sure TextArea has a maxlength
addOnloadEvent(function () {
    var txts = document.getElementsByTagName('TEXTAREA')

    //alert(txts);

    for (var i = 0, l = txts.length; i < l; i++) {
        if (/^[0-9]+$/.test(txts[i].getAttribute("maxlength"))) {
            var func = function () {
                var len = parseInt(this.getAttribute("maxlength"), 10);

                if (this.value.length > len) {
                    alert('Maximum length exceeded: ' + len);
                    this.value = this.value.substr(0, len);
                    return false;
                }
            }

            txts[i].onkeyup = func;
            txts[i].onblur = func;
        }
    }
});

function clearCurrentLink() {
    var nav = document.getElementById("content");
    var a = nav.getElementsByTagName("A");
    for (var i = 0; i < a.length; i++)
        if (a[i].href == window.location.href.split("#")[0])
            removeNode(a[i]);
}

function removeNode(n) {
    if (n.hasChildNodes())
    // gets the text from the link and moves it to the previous node.
        for (var i = 0; i < n.childNodes.length; i++) {
            var span = document.createElement('span');
            var label = n.childNodes[i].cloneNode(true);
            span.className = 'current';
            span.appendChild(label);

            n.parentNode.appendChild(span);
        }
    n.parentNode.removeChild(n);
}
