﻿// JScript File
// Add event handler to body when window loads
function addLoadEvent(func) {
	var oldonload = window.onload;
	
	if (typeof window.onload != "function") {
		window.onload = func;
	} else {
		window.onload = function () {
			oldonload();
			func();
		}
	}
}

addLoadEvent(function () {
	DocLinks.init();
});

/*----------------------------------------------+
 | DocLinks - Add icon after links to documents |
 +----------------------------------------------*/
var DocLinks = {
	init : function() {
		// Find all links
		var links = document.getElementsByTagName("a");
		
		for (var i = 0; i < links.length; i++) {
			var theLink = links[i];
			var address = theLink.href.toLowerCase();
			
			// Don't add icon to links styled to look different
			if (theLink.className.indexOf("btn-fancy") == -1) {
				// Check if link points to files with common extensions
				var matches = address.match(/\.(doc|pdf|xls|ppt)/);
				
				if (matches) {
					// Using "match" always returns two results (not sure why)
					var ext = matches[0].substr(1, 3);
					
					// Create new image and insert it
					var newImg = document.createElement("img");
					newImg.alt = "(" + ext.toUpperCase() + ")";
					newImg.className = "icon";
					newImg.src = "/images/shared/icon-" + ext + ".gif";
					newImg.title = newImg.alt;
					
					if (theLink.getElementsByTagName("img").length <= 0)
						theLink.parentNode.insertBefore(newImg, theLink);
					
					// Make link open in new window/tab
					theLink.onclick = function () {
						window.open(this.href);
						return false;
					};
				}
			}
		}
	}
};

/*--------------------------------------------------+
| Tog - Toggle visibility of two opposing elements |
+--------------------------------------------------*/
var Tog = {
    check: function(a, b) {
        for (var i = 0; i < a.length; i++) {
            if (document.getElementById(a[i]).checked) {
                Tog.toggle(b[i]);
            }
        }
    },
    checkRadioSelected: function(radiolist, toggleElement) {
        for (var i = 0; i < radiolist.length; i++) {
            if (radiolist[i].checked) {
                if (radiolist[i].value == "3") {
                    Tog.toggle(toggleElement);
                }
            }
        }
    },
    swap: function(a, b) {
        a = document.getElementById(a);
        b = document.getElementById(b);

        if (!a || !b) return false;

        if (a.className.indexOf("closed") != -1) {
            oldClass = a.className;
            newClass = oldClass.replace(/closed/g, "");
            a.className = newClass;

            b.className += " closed";
        } else {
            oldClass = b.className;
            newClass = oldClass.replace(/closed/g, "");
            b.className = newClass;

            a.className += " closed";
        }
    },
    toggle: function(a) {
        a = document.getElementById(a);

        if (!a) return false;

        if (a.className.indexOf("closed") != -1) {
            oldClass = a.className;
            newClass = oldClass.replace(/closed/g, "");
            a.className = newClass;
        } else {
            a.className += " closed";
        }
    },
    togglePropertyValue: function(a, prop, value1, value2) {
        a = document.getElementById(a);

        if (!a) return false;

        if (a[prop].indexOf(value1) != -1) {
            a[prop] = value2;
        } else {
            a[prop] = value1;
        }
    }
};





var remote;
function launchWindow(helpURL, size){
	remote = window.open(helpURL, "help", size+",scrollbars=1,resizable=1, menuBar=0, status=1, toolbar=0");
	remote.focus();
}