/* browser.js */
// from http://www.mozilla.org/docs/web-developer/sniffer/browser_type.html
// convert all characters to lowercase to simplify testing
var agt=navigator.userAgent.toLowerCase();

// *** BROWSER VERSION ***
// Note: On IE5, these return 4, so use is_ie5up to detect IE5.
var is_major = parseInt(navigator.appVersion);
var is_minor = parseFloat(navigator.appVersion);

// Note: Opera and WebTV spoof Navigator.  We do strict client detection.
// If you want to allow spoofing, take out the tests for opera and webtv.
var is_nav  = ((agt.indexOf('mozilla')!=-1) && (agt.indexOf('spoofer')==-1)
			&& (agt.indexOf('compatible') == -1) && (agt.indexOf('opera')==-1)
			&& (agt.indexOf('webtv')==-1) && (agt.indexOf('hotjava')==-1));
var is_nav2 = (is_nav && (is_major == 2));
var is_nav3 = (is_nav && (is_major == 3));
var is_nav4 = (is_nav && (is_major == 4));
var is_nav4up = (is_nav && (is_major >= 4));
var is_navonly = (is_nav && ((agt.indexOf(";nav") != -1) ||
				(agt.indexOf("; nav") != -1)) );
var is_nav6 = (is_nav && (is_major == 5));
var is_nav6up = (is_nav && (is_major >= 5));
var is_gecko = (agt.indexOf('gecko') != -1);

var is_ie     = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
var is_ie3    = (is_ie && (is_major < 4));
var is_ie4    = (is_ie && (is_major == 4) && (agt.indexOf("msie 4")!=-1) );
var is_ie4up  = (is_ie && (is_major >= 4));
var is_ie5    = (is_ie && (is_major == 4) && (agt.indexOf("msie 5.0")!=-1) );
var is_ie5_5  = (is_ie && (is_major == 4) && (agt.indexOf("msie 5.5") !=-1));
var is_ie5up  = (is_ie && !is_ie3 && !is_ie4);
var is_ie5_5up =(is_ie && !is_ie3 && !is_ie4 && !is_ie5);
var is_ie6    = (is_ie && (is_major == 4) && (agt.indexOf("msie 6.")!=-1) );
var is_ie6up  = (is_ie && !is_ie3 && !is_ie4 && !is_ie5 && !is_ie5_5);

// KNOWN BUG: On AOL4, returns false if IE3 is embedded browser
// or if this is the first browser window opened.  Thus the
// variables is_aol, is_aol3, and is_aol4 aren't 100% reliable.
var is_aol   = (agt.indexOf("aol") != -1);
var is_aol3  = (is_aol && is_ie3);
var is_aol4  = (is_aol && is_ie4);
var is_aol5  = (agt.indexOf("aol 5") != -1);
var is_aol6  = (agt.indexOf("aol 6") != -1);

var is_opera = (agt.indexOf("opera") != -1);
var is_opera2 = (agt.indexOf("opera 2") != -1 || agt.indexOf("opera/2") != -1);
var is_opera3 = (agt.indexOf("opera 3") != -1 || agt.indexOf("opera/3") != -1);
var is_opera4 = (agt.indexOf("opera 4") != -1 || agt.indexOf("opera/4") != -1);
var is_opera5 = (agt.indexOf("opera 5") != -1 || agt.indexOf("opera/5") != -1);
var is_opera6 = (agt.indexOf("opera 6") != -1 || agt.indexOf("opera/6") != -1);
var is_opera5up = (is_opera && !is_opera2 && !is_opera3 && !is_opera4);

var is_webtv = (agt.indexOf("webtv") != -1);

var is_TVNavigator = ((agt.indexOf("navio") != -1) || (agt.indexOf("navio_aoltv") != -1));
var is_AOLTV = is_TVNavigator;

var is_hotjava = (agt.indexOf("hotjava") != -1);
var is_hotjava3 = (is_hotjava && (is_major == 3));
var is_hotjava3up = (is_hotjava && (is_major >= 3));

// *** JAVASCRIPT VERSION CHECK ***
var is_js;
if (is_nav2 || is_ie3) is_js = 1.0;
else if (is_nav3) is_js = 1.1;
else if (is_opera5up) is_js = 1.3;
else if (is_opera) is_js = 1.1;
else if ((is_nav4 && (is_minor <= 4.05)) || is_ie4) is_js = 1.2;
else if ((is_nav4 && (is_minor > 4.05)) || is_ie5) is_js = 1.3;
else if (is_hotjava3up) is_js = 1.4;
else if (is_nav6 || is_gecko) is_js = 1.5;
// NOTE: In the future, update this code when newer versions of JS
// are released. For now, we try to provide some upward compatibility
// so that future versions of Nav and IE will show they are at
// *least* JS 1.x capable. Always check for JS version compatibility
// with > or >=.
else if (is_nav6up) is_js = 1.5;
// NOTE: ie5up on mac is 1.4
else if (is_ie5up) is_js = 1.3
// HACK: no idea for other browsers; always check for JS version with > or >=
else is_js = 0.0;

// end of browser detection

function MyGetElementById(id)
{
	return document.all[id];
}
if (document.getElementById == null)
	document.getElementById = MyGetElementById;



/* maxlength.js */
function ismaxlength(obj)
{
    var mlength=obj.getAttribute? parseInt(obj.getAttribute("maxlength")) : ""
    if (obj.getAttribute && obj.value.length>mlength)
    obj.value=obj.value.substring(0,mlength)
}


/* textformatter.js */

function MozillaInsertText(element, text, pos)
{
    element.value = element.value.slice(0,pos)+text+element.value.slice(pos);
}

function AddTag(t1, t2, elementID)
{
    if (is_ie5up)
    {
        if (document.selection)
        {
            var msgCell = document.getElementById(elementID);
            msgCell.focus();

            var txt = msgCell.value;
            var str = document.selection.createRange();

            if (str.text == "")
            {
                str.text = t1 + t2;
            }
            else if (txt.indexOf(str.text) >= 0)
            {
                str.text = t1 + str.text + t2;
            }
            else
            {
                msgCell.value = txt + t1 + t2;
            }

            str.select();
        }
    }
    else if (is_nav4up)
    {
        var element = document.getElementById(elementID);
        var sel_start = element.selectionStart;
        var sel_end = element.selectionEnd;
        MozillaInsertText(element, t1, sel_start);
        MozillaInsertText(element, t2, sel_end+t1.length);
        element.selectionStart = sel_start;
        element.selectionEnd = sel_end+t1.length+t2.length;
        element.focus();
    }
    else
    {
        document.getElementById(elementID).value = document.getElementById(elementID).value + t1 + t2;
    }
}

function AddTagFromChildWindow(t1, t2, elementID)
{
    if (is_ie5up)
    {
        if (opener.document.selection)
        {
            var msgCell = opener.document.getElementById(elementID);
            msgCell.focus();

            var txt = msgCell.value;
            var str = opener.document.selection.createRange();

            if (str.text == "")
            {
                str.text = t1 + t2;
            }
            else if (txt.indexOf(str.text) >= 0)
            {
                str.text = t1 + str.text + t2;
            }
            else
            {
                msgCell.value = txt + t1 + t2;
            }

            str.select();
        }
    }
    else if (is_nav4up)
    {
        var element = opener.document.getElementById(elementID);
        var sel_start = element.selectionStart;
        var sel_end = element.selectionEnd;
        MozillaInsertText(element, t1, sel_start);
        MozillaInsertText(element, t2, sel_end+t1.length);
        element.selectionStart = sel_start;
        element.selectionEnd = sel_end+t1.length+t2.length;
        element.focus();
    }
    else
    {
        opener.document.getElementById(elementID).value = opener.document.getElementById(elementID).value + t1 + t2;
    }
}

function OpenBlock(name)
{
    var el = document.getElementById(name);

    if (el._state != "show")
    {
        el._state = "show";
        el.style.display = "block";
    }
    else
    {
        el._state = "hide";
        el.style.display = "none";
    }
}


/* search.js */
function SubmitSearch()
{
    document.searchform.submit();
    return true;
}


/* add_event.js */
function addEvent(elm, evType, fn, useCapture){if(elm.addEventListener){elm.addEventListener(evType, fn, useCapture);return true;}else if (elm.attachEvent){var r = elm.attachEvent('on' + evType, fn);return r;}else{elm['on' + evType] = fn;}}

/* popup.js */
var newWindow = null;

function closeWin(){
        if (newWindow != null){
                if(!newWindow.closed)
                        newWindow.close();
        }
}

function popUpWin(url, type, strWidth, strHeight){

        closeWin();

        type = type.toLowerCase();

        if (type == "fullscreen"){
                strWidth = screen.availWidth;
                strHeight = screen.availHeight;
        }
        var tools="";
        if (type == "standard") tools = "resizable,toolbar=yes,location=yes,scrollbars=yes,menubar=yes,width="+strWidth+",height="+strHeight+",top=0,left=0";
        if (type == "console" || type == "fullscreen") tools = "resizable,toolbar=no,location=no,scrollbars=yes,width="+strWidth+",height="+strHeight+",left=0,top=0";
        newWindow = window.open(url, 'newWin', tools);
        newWindow.focus();
}

function doPopUp(e)
{
//set defaults - if nothing in rel attrib, these will be used
var t = "standard";
var w = "780";
var h = "580";
//look for parameters
attribs = this.rel.split(" ");
if (attribs[1]!=null) {t = attribs[1];}
if (attribs[2]!=null) {w = attribs[2];}
if (attribs[3]!=null) {h = attribs[3];}
//call the popup script
popUpWin(this.href,t,w,h);
//cancel the default link action if pop-up activated
if (window.event)
        {
        window.event.returnValue = false;
        window.event.cancelBubble = true;
        }
else if (e)
        {
        e.stopPropagation();
        e.preventDefault();
        }
}

function findPopUps()
{
var popups = document.getElementsByTagName("a");
for (i=0;i<popups.length;i++)
        {
        if (popups[i].rel.indexOf("popup")!=-1)
                {
                // attach popup behaviour
                popups[i].onclick = doPopUp;
                // add popup indicator
                if (popups[i].rel.indexOf("noicon")==-1)
                        {
                        popups[i].style.backgroundImage = "url(pop-up.gif)";
                        popups[i].style.backgroundPosition = "0 center";
                        popups[i].style.backgroundRepeat = "no-repeat";
                        popups[i].style.paddingLeft = "15px";
                        }
                // add info to title attribute to alert fact that it's a pop-up window
                popups[i].title = popups[i].title + " [Opens in pop-up window]";
                }
        }
}

addEvent(window, 'load', findPopUps, false);
