/* Run onload scripts */

window.onload = function()
{
    try { adjustLinks(); } catch (err) {}
    try { startList(); } catch (err) {}
    try { linkableDivs(); } catch (err) {}
}

/* Adds css hover ability to LI list items for IE */
/* from http://alistapart.com/articles/dropdowns/ */
startList = function()
{
    if (document.all && document.getElementById)
    {
        if (document.getElementById("topnav"))
        {
            navRoot = document.getElementById("topnav").firstChild;

            for (i=0; i < navRoot.childNodes.length; i++)
            {
                node = navRoot.childNodes[i];
                if (node.nodeName=="LI")
                {
                    node.onmouseover=function()
                    {
                        this.className += " hover";
                    }

                    node.onmouseout=function()
                    {
                        this.className=this.className.replace(" hover", "");
                    }
                }
            }
        }

        if (document.getElementById("leftnav")) {
            listItems = document.getElementById("leftnav").getElementsByTagName("LI");
            for (i=0; i < listItems.length; i++)
            {
                node = listItems[i];
                if (node.nodeName=="LI")
                {
                    node.onmouseover=function()
                    {
                        this.className += " hover";
                    }

                    node.onmouseout=function()
                    {
                        this.className=this.className.replace(" hover", "");
                    }
                }
            }

        }
    }
}

adjustLinks = function()
{
    var content = document.getElementById("main-content");
    var links = content.getElementsByTagName("a");

    for (var i = 0; i < links.length; i++)
    {
        var href, href_arr, href_path, href_id, has_id, query_string;

        href = links[i].getAttribute("href");

        if (href == null || href == "")
            continue;
        if (href.indexOf("/goto.asp") == 0 || href.indexOf("http://") == 0 || href.indexOf("mailto:") == 0 || href.indexOf("ftp://") == 0 || href.indexOf("javascript:") == 0)
            continue;
        if (href.indexOf("?") != -1)
            continue;
        if (href.indexOf("#") != -1)
            continue;

        // Make relative (for IE)
        href = href.replace("http://" + location.host + "/", "/");


        href_arr = href.split(/#/);
        if (href_arr.length > 1)
        {
            has_id = true;
            href_path = href_arr[0];
            href_id = href_arr[1];
        }
        else
        {
            has_id = false;
            href_path = href;
        }

        if (reuse == true) {
            query_string = "?main=" + navmain + "&sub1=" + navsub1 + "&sub2=" + navsub2 + "&sub3=" + navsub3;
        }
        else
        {
            query_string = "";
        }

        links[i].setAttribute("href", href_path + query_string);
    }
}



linkableDivs = function()
{
    var divs = document.getElementsByClassName("clickable", "div");

    for (var i = 0; i < divs.length; i++)
    {
        var anchor, anchor_href, anchor_target;
        anchor = divs[i].getElementsByTagName("a")[0];

        if (typeof anchor == "undefined")
            continue;

        anchor_href = anchor.getAttribute("href") || "";
        anchor_target = anchor.getAttribute("target") || "";

        if (anchor_href == "")
            continue;
        else
        {
            divs[i].onclick = (function() {
                                   var href = anchor_href;
                                   var target = anchor_target;
                                   return function() { link_to(href, target); return false; }
                                })();

        }
    }
}

link_to = function(url, window)
{
    if (window == "")
        location.href = url;
    else if (window == "_blank")
        top.window.open(url);
    else if (window[0] != "_")
        top.window.open(url, window);
    else
        location.href = url;
}

validateSRForm = function(form) 
{
    var serial1_re = /^[0-9Ff][0-9]{2}[0-9EeFf]$/;
    var serial2_re = /^[0-9]{6}$/;
    var serial1 = document.getElementById("serialnumber-1").value;
    var serial2 = document.getElementById("serialnumber-2").value;
    
    if (document.getElementById("firstname").value == "" || document.getElementById("lastname").value == "")
    {
        alert(gLang == "en" ? "Please fill in the information below. All 3 fields are required." : "Veuillez saisir les renseignements ci-dessous. Les 3 champs sont obligatoires.");
        return false;
    }
    else if (serial1_re.test(serial1) == false || serial2_re.test(serial2) == false)
    {
        alert(gLang == "en" ? "Please enter a valid serial number." : "Veuillez saisir un numéro de série valide.");
        return false;
    }
    
    return true;
}
