//
// compatabilityTools.js
//
// Functions to assist with compatability in older browsers
//

//for student facing files need any text to be in a variable, that way it can be overloaded by the xsl, and translated where needed
var MEScot1="Object type is not handled by this browser. \nPlease upgrade your browser to Internet Explorer 6 or Mozilla 1.3 (1.2 for Mac).";

// Before redefining document.getElementById, capture browser version in a variable
var browserDOMVersion;
if (document.getElementById)
    browserDOMVersion = "getElementById";
else if (document.all)
    browserDOMVersion = "all";
else if (document.layers)
    browserDOMVersion = "layers";
else
    browserDOMVersion = "";

// Redefine document.getElementById to use function getDocElement (definition below)
if (!(document.getElementById))
    document.getElementById = getDocElement;

// returns a universal object reference
// this function should return a valid object independent of the browser version
function getDocElement( elemID )
{
    if (document.all)
        return document.all[elemID];
    else if (document.layers)
        return document.layers[elemID];
    else
        alert(MEScot1);
}

//creates non-IE versions of insertAdjacentElement, insertAdjacentHTML, and insertAdjacentText for non-IE browsers (e.g. Mozilla)
if(typeof(HTMLElement) != "undefined" && !HTMLElement.prototype.insertAdjacentElement)
{
	HTMLElement.prototype.insertAdjacentElement = function(where,parsedNode)
	{
		switch (where)
		{
			case 'beforeBegin':
				this.parentNode.insertBefore(parsedNode,this);
				break;
			case 'afterBegin':
				this.insertBefore(parsedNode,this.firstChild);
				break;
			case 'beforeEnd':
				this.appendChild(parsedNode);
				break;
			case 'afterEnd':
				if (this.nextSibling) 
					this.parentNode.insertBefore(parsedNode,this.nextSibling);
				else 
					this.parentNode.appendChild(parsedNode);
				break;
		}
	}

	HTMLElement.prototype.insertAdjacentHTML = function(where,htmlStr)
	{
		var r = this.ownerDocument.createRange();
		r.setStartBefore(this);
		var parsedHTML = r.createContextualFragment(htmlStr);
		this.insertAdjacentElement(where,parsedHTML);
	}


	HTMLElement.prototype.insertAdjacentText = function(where,txtStr)
	{
		var parsedText = document.createTextNode(txtStr);
		this.insertAdjacentElement(where,parsedText);
	}
} //if

//
// slip 23364: the JavaScript method "encodeURIComponent" is necessary for localization support, but it is only present in IE 5.5 and newer.
// The following code is therefore in place solely for Internet Explorer 5.0. 
//
if (typeof encodeURIComponent != 'function')
{
    function utf8(wide)
    {
        var c, s;
        var enc = "";
        var i = 0;
        while(i < wide.length)
        {
            c = wide.charCodeAt(i++);
            // handle UTF-16 surrogates
            if (c >= 0xDC00 && c < 0xE000)
                continue;
            if (c >= 0xD800 && c < 0xDC00)
            {
                if (i >= wide.length)
                    continue;
                s = wide.charCodeAt(i++);
                if (s < 0xDC00 || c >= 0xDE00)
                    continue;
                c = ((c - 0xD800) << 10) + (s - 0xDC00) + 0x10000;
            }
            // output value
            if (c < 0x80) enc += String.fromCharCode(c);
            else if (c < 0x800)
                enc += String.fromCharCode(0xC0+(c >> 6),0x80 + (c & 0x3F));
            else if
                (c < 0x10000) enc += String.fromCharCode(0xE0 + (c >> 12), 0x80 + (c >> 6 & 0x3F), 0x80 + (c & 0x3F));
            else
                enc += String.fromCharCode(0xF0 + (c >> 18), 0x80 + (c >> 12 & 0x3F), 0x80 + (c >> 6 & 0x3F), 0x80+(c & 0x3F));
        }
        return enc;
    }

    function toHex(n)
    {
        var hexChars = "0123456789ABCDEF";
        
        return hexChars.charAt(n >> 4) + hexChars.charAt(n & 0xF);
    }

    function encodeURIComponentIE5(inputString)
    {
        /*
         * inputString will often be null, as it is considered to be an optional parameter
         * in functions that call this one (like openView()).  Need to check for this.
         */
        if(inputString == null)
            return "";
            
        var validURIcharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-";
  
        var convertedInputString = utf8(inputString);
        var newEncoding = "";
        for (var i = 0; i < inputString.length; i++)
        {
            // if the current character isn't a valid URI character, append its hex value to the newEncoding string
            if (validURIcharacters.indexOf(convertedInputString.charAt(i)) == -1)
                newEncoding += "%" + toHex(convertedInputString.charCodeAt(i));
            else
                newEncoding += convertedInputString.charAt(i);
        }
        return newEncoding;
    }
}

// definition of encodeURIComponent for IE 5.0 support
if (typeof encodeURIComponent == 'undefined')
    encodeURIComponent = encodeURIComponentIE5;