window.onload = function()
{
    if (document.getElementById("searchform"))
    {
        usable_form("searchform");
    }

    if (document.getElementById("loginform"))
    {
        usable_form("loginform");
    }

    target_links();
}

function target_links()
{
    // drop out if code is unsupported
    if ( ! document.getElementsByTagName) return;

    /* Fix IE 'blockquote' issue
    * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    var isMSIE = /*@cc_on!@*/false;
    if (isMSIE)
    {
        var quotes = document.getElementsByTagName("blockquote");
        for (var i = 0, quote; quote = quotes[i]; i++)
        {
            quote.innerHTML = '<span class="startQuote">&ldquo;</span>' + quote.innerHTML + '<span class="endQuote">&rdquo;</span>';
        }
    }

    /* Basic anchor tags...
    * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
    var links = document.getElementsByTagName("a");

    for (var i = 0, link; link = links[i]; i++)
    {
        var therel = link.getAttribute("rel");

        if (therel == "external" || therel == "client")
        {
            // if no title exists and it isn't an anchor link, add proper title
            // todo: use DOM instead of innerHTML
            if ((link.title == '' || !link.title) && !link.name) link.title = "External link: " + link.innerHTML.substring(0,20);

            link.title += " (Opens in new window)";

            if (isIE()) link.alt = "External link: " + link.alt + " (Opens in new window)";

            if (therel == "external")
            {
                link.oldhref = link.href;
                link.href = "#";

                link.onmouseup = function()
                                 {
									var theCompany = 'MSD';
									var m = "";

												m += "Links are provided merely as a convenience. "+theCompany+" makes no warranties or ";
												m += "representations of any kind as to the accuracy, completeness, reliability or usefulness of any ";
												m += "information contained in such sites and shall have no liability for any loss or damage of any kind that ";
												m += "may arise from your use of such content or information. Inclusion of any third party link does not ";
												m += "imply an endorsement or recommendation by "+theCompany+".";

                                        if(confirm(m)) window.open(this.oldhref);

                                        return false;
                                 };
            }
            else
            {
                link.setAttribute('target', "_blank");
            }
        }


        if (therel == "download")
        {
            // if no title exists and it isn't an anchor link, add proper title
            // todo: use DOM instead of innerHTML
            if ((link.title == '' || !link.title) && !link.name) link.title = "Download: " + link.innerHTML.substring(0,20);

            link.setAttribute('target', "_blank");

            if (isIE())
            {
                link.title += " (Right-click and select 'Save Target as...')";
                link.alt = "Download: " + link.alt+  " (Right-click and select 'Save Target as...')";
            }
            else
            {
                link.title += " (Right-click and select 'Save Link as...')";
            }
        }

        if (therel == "pop")
        {
            link.title = "Click here to view a larger picture";

            link.imageLoc = link.href.replace('?','&');

            //alert("href: " + link.href + "\nimageLoc: " + link.imageLoc);

            link.href = "#";

            // url, width, height, left, top, resizable, scrollable, toolbar

            link.onmouseup = function () {
                                            // alert(this.imageLoc);
                                            popwin(('view.php?p='+this.imageLoc), 1, 1, 1, 1, 1, 0, 0);
                                         };
        }
    }
}


function fixclass(e)
{
    if (document.getElementById(e))
    {
        var i = document.getElementById(e);

        if (jscss('check', i, 'missing', null))
        {
            jscss('remove', i, 'missing', null);
        }
    }

    if (document.getElementById(e+"lab"))
    {
        var l = document.getElementById(e+"lab");

        if (jscss('check', l, 'missing', null))
        {
                jscss('remove', l, 'missing', null);
        }
    }
}


function jscss(a, o, c1, c2)
{
    switch (a)
    {
        case 'swap':    o.className = ! jscss('check', o, c1) ? o.className.replace(c2, c1) : o.className.replace(c1, c2);
                        break;

        case 'add':     if ( ! jscss('check', o, c1)) { o.className += o.className ? ' ' + c1 : c1; }
                        break;

        case 'remove':  var rep = o.className.match(' ' + c1) ? ' ' + c1 : c1;
                                  o.className = o.className.replace(rep, '');
                        break;

        case 'check':   return new RegExp('\\b' + c1 + '\\b').test(o.className);
                        break;
    }
}


function popwin(theurl, w, h, l, t, r, s, tool) // url, width, height, left, top, resizable, scrollable, toolbar
{
    var final_width = (w==0) ? screen.width - (screen.width / 2) : w;

    var final_height = (h==0) ? screen.height - (screen.height / 2) : h;

    var left = (l==1) ? (screen.width-final_width) / 2 : l;

    var top = (t==1) ? (screen.height-final_height) / 2 : t;

    var popwin = window.open(theurl,'','left='+left+',top='+top+',width='+final_width+',height='+final_height+',toolbar='+tool+',location=0,directories=0,status=0,menubar=0,scrollbars='+s+',resizable='+r);

    return false;
}

function isIE()
{
    return /msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent);
}

function usable_form(formid)
{
    if (document.forms[formid])
    {
        for (var f = 0; f < document.forms[formid].elements.length; f++)
        {
            var e = document.forms[formid].elements[f];

            if (e.type == "textarea" ||
                e.type == "text" ||
                e.type == "password" ||
                e.type == "select-one")
                {
                    e.onfocus=function()
                    {
                        if (this.value == "Search..." ||
                            this.value == "Username"  ||
                            (default_value != null && this.value == default_value))
                            {
                                this.value = "";
                            }
                            else
                            {
                                var default_value = this.name.charAt(0).toUpperCase() + this.name.slice(1) + "...";

                                if (default_value != null && this.value == default_value) this.value = "";
                            }

                        fixclass(this.id);
                    };
                }
        }
    }
}
