/* Functions for the collapse and expand article */
$(document).ready(function () {
    $(".toggle").click(function () {

        var elementID = "collapse-expand-" + $(this).attr("id");
        var imgTagId = "img-" + $(this).attr("id");
        var imgTagSrc = $("#" + imgTagId).attr("src");

        //fetch the suffix and prefix of the imgsrc so we can alter the image name...
        var urlPrefix = imgTagSrc.slice(0, imgTagSrc.lastIndexOf("/") + 1);
        var urlSuffix = imgTagSrc.slice(imgTagSrc.lastIndexOf("/") + 1);

        //Check which image we are using and preform the appropriate switch...
        if (urlSuffix == "collapse.png") {
            urlSuffix = "expand.png";
        }
        else {
            urlSuffix = "collapse.png";
        }

        //Perform the src switch...
        $("#" + imgTagId).attr("src", urlPrefix + urlSuffix);
        //...and add the toogle.
        $("#" + elementID).slideToggle("slow");
    });
});

// Functions for expanding when clicking anchors on collapse-expand article
$(document).ready(function () {
    $(".anchor-expand").click(function () {

        var baseId = $(this).attr("href");
        baseId = baseId.slice(1, baseId.length);
        var elementToExpandID = "collapse-expand-" + baseId;
        var imgTagId = "img-" + baseId;
        var imgTagSrc = $("#" + imgTagId).attr("src");

        //fetch the suffix and prefix of the imgsrc so we can alter the image name...
        var urlPrefix = imgTagSrc.slice(0, imgTagSrc.lastIndexOf("/") + 1);
        var urlSuffix = imgTagSrc.slice(imgTagSrc.lastIndexOf("/") + 1);

        //Check which image we are using and preform the appropriate switch...
        urlSuffix = "collapse.png";

        //Perform the src switch...
        $("#" + imgTagId).attr("src", urlPrefix + urlSuffix);
        //...and expand the boxes...
        $("#" + elementToExpandID).show("slow");
    });
});

/* Could this be removed? */
$(document).ready(function () {

    var cookiesString = document.cookie;
    var pos = cookiesString.indexOf("currentArticleUrl=");
    var value = "";

    if (pos != -1) {
        var start = pos + 18;
        var end = cookiesString.indexOf(";", start);
        if (end == -1) {
            end = cookiesString.length;
        }
        value = cookiesString.substring(start, end);
        value = decodeURIComponent(value);
    }

    if (value != "") {
        if (document.getElementById("articleUrl")) {
            document.getElementById("articleUrl").value = value;
        }
    }
});


/* Function for altering the form submit url to match the sites correct external url */
$(document).ready(function () {
    if (searchActionUrl) {
        $("#article_search").attr("action", searchActionUrl);
    }
});


/* Functions for the sitemap */

function hideSitemaps() {
    $(".site_nav").hide();
}

$(document).ready(function () {
    hideSitemaps();
});

function hideExcept(object) {
    var sectionMaps = $(".site_nav").get();
    var skipThisID = object.attr("id");
    for (var i = 0; i < sectionMaps.length; i++) {
        if (sectionMaps[i].id != skipThisID) {
            var hideThis = $("#" + sectionMaps[i].id);

            if(hideThis.css("display") == "none")
                continue;

            hideThis.slideUp("fast");

            var referer = $("#ref-" + hideThis.attr("id"));
            referer.parent().css("background-color", "transparent");
            referer.css("background-color", "transparent");
            referer.children(".section").css("color", "white");
            referer.children(".subscript").css("color", "#ccc");
            referer.hover( function(){
                 $(this).css('background-color', '#c03');
            },
            function(){
                 $(this).css('background-color', 'transparent');
            });
        }
    }
}

$(document).ready(function () {
    $(".sitemap_link").click(function () {
        var referer = $(this).attr("id");
        var tmp = referer.split("-");
        var siteMapToOpenID = tmp.slice(1).join("-");

        /* Toggle the referred sitemap and hide others that are visible */
        var siteMapToToggle = $("#" + siteMapToOpenID);
        hideExcept(siteMapToToggle);

        if (siteMapToToggle.css("display") == "none") {
            $(this).children().css("color", "#c03");
            $(this).parent().css("background-color", "white");
            $(this).hover( function(){
                 $(this).css('background-color', 'white');
            },
            function(){
                 $(this).css('background-color', 'white');
            });

        } else {
            $(this).parent().css("background-color", "transparent");
            $(this).hover( function(){
                 $(this).css('background-color', '#c03');
            },
            function(){
                 $(this).css('background-color', 'transparent');
            });
            $(this).css("background-color", "transparent");
            $(this).children(".section").css("color", "white");
            $(this).children(".subscript").css("color", "#ccc");
        }

        siteMapToToggle.slideToggle("slow");

        return false;
    });
});

$(document).ready(function () {
    $(".close_site_map").click(function () {
        var id = $(this).attr("id");
        var tmp = id.split("-");
        var siteMapToHideId = tmp.slice(1).join("-");
        $("#" + siteMapToHideId).slideUp();

        var referer = $("#ref-" + siteMapToHideId);
        referer.parent().css("background-color", "transparent");
        referer.children(".section").css("color", "white");
        referer.children(".subscript").css("color", "#ccc");
        referer.css("background-color", "transparent");
        referer.hover( function(){
                 $(this).css('background-color', '#c03');
            },
            function(){
                 $(this).css('background-color', 'transparent');
            });
    });
});


/*
 ----------------------------------------------------------
 JS FOR THE COMPARISON TOOL
 ----------------------------------------------------------
 */

String.prototype.trim = function () {
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
}

function submitform()
{
    document.forms["selectionForm"].myAction.value = "selectionChanged";
    document.forms["selectionForm"].submit();
}

function submitcheckboxform()
{
    document.forms["checkBoxForm"].submit();
}


function submitselectionform()
{
    document.forms["selectionForm"].action = "";
    document.forms["selectionForm"].submit();
}


function beep() {
    java.awt.Toolkit.getDefaultToolkit().beep();
}

function checkInArray(array, value)
{
    for (var i = 0; i < array.length; i = i + 1)
    {
        if (array[i].value = value)
            return true;
    }

    return false;
}

// -------------------------------------------------------------------
// hasOptions(obj)
//  Utility function to determine if a select object has an options array
// -------------------------------------------------------------------
function hasOptions(obj) {
    if (obj != null && obj.options != null) {
        return true;
    }
    return false;
}

function moveOption(fromID, toID, idx)
{
    if (isNaN(parseInt(idx)))
    {
        var i = document.getElementById(fromID).selectedIndex;
    }
    else
    {
        var i = idx;
    }

    var o = document.getElementById(fromID).options[ i ];
    var theOpt = new Option(o.text, o.value, false, false);
    document.getElementById(toID).options[document.getElementById(toID).options.length] = theOpt;
    document.getElementById(fromID).options[ i ] = null;
}

function copyOption(fromID, toID, idx)
{
    if (isNaN(parseInt(idx)))
    {
        var i = document.getElementById(fromID).selectedIndex;
    }
    else
    {
        var i = idx;
    }

    var o = document.getElementById(fromID).options[ i ];
    var theOpt = new Option(o.text, o.value, false, false);
    document.getElementById(toID).options[document.getElementById(toID).options.length] = theOpt;
    //document.getElementById( fromID ).options[ i ] = null;
}


function deleteOption(fromID, idx)
{
    if (isNaN(parseInt(idx)))
    {
        var i = document.getElementById(fromID).selectedIndex;
    }
    else
    {
        var i = idx;
    }

    var o = document.getElementById(fromID).options[ i ];
    var theOpt = new Option(o.text, o.value, false, false);
    //document.getElementById( toID ).options[document.getElementById( toID ).options.length] = theOpt;
    document.getElementById(fromID).options[ i ] = null;
}


function moveOption2(fromID, idx)
{
    if (isNaN(parseInt(idx)))
    {
        var i = document.getElementById(fromID).selectedIndex;
    }
    else
    {
        var i = idx;
    }

    var o = document.getElementById(fromID).options[ i ];
    var theOpt = new Option(o.text, o.value, false, false);
    //document.getElementById( toID ).options[document.getElementById( toID ).options.length] = theOpt;
    document.getElementById(fromID).options[ i ] = null;
}


function moveOptions(fromID, toID)
{
    for (var x = document.getElementById(fromID).options.length - 1; x >= 0; x--)
    {
        if (document.getElementById(fromID).options[x].selected == true)
        {
            moveOption(fromID, toID, x);
        }
    }
}

function copyOptions(fromID, toID)
{
    trim(toID);

    for (var x = document.getElementById(fromID).options.length - 1; x >= 0; x--)
    {

        var option = document.getElementById(fromID).options[x].value;
        if (document.getElementById(fromID).options[x].selected == true && !exists(toID, option) && !beginsWith(option, "-"))
        {
            copyOption(fromID, toID, x);
        }
    }
}

function exists(fromID, option)
{
    for (var x = document.getElementById(fromID).options.length - 1; x >= 0; x--)
    {
        if (document.getElementById(fromID).options[x].value == option)
        {
            return true;
        }

    }
    return false;
}

function beginsWith(option, beginChar)
{
    var string = option;

    if (string.substr(0, 1) == beginChar)
    {
        return true;
    }
    else
        return false;

}


function trim(toID)
{
    for (var x = document.getElementById(toID).options.length - 1; x >= 0; x--)
    {
        value = document.getElementById(toID).options[x].value;
        //document.write(value.toString().substr(0, 1));
        //document.write(value);
        if (value == "blank")
        {
            //document.write("isEqual");
            document.getElementById(toID).options[x] = null;
        }
        //else
        //        document.write("isNotEqual");


    }
}

function deleteOptions(fromID)
{
    for (var x = document.getElementById(fromID).options.length - 1; x >= 0; x--)
    {
        if (document.getElementById(fromID).options[x].selected == true)
        {
            deleteOption(fromID, x);
        }
    }
}


function copyAllOptions(fromID, toID)
{
    for (var x = 0; x < document.getElementById(fromID).options.length; x++)
    {
        copyOption(fromID, toID, x);
    }
}

function clear(fromID)
{
    document.selectionForm.multiselect.options.length = 0;
}

function checkAll(field, state, value)
{

    for (i = 0; i < field.length; i++)
        if (field[i].value == value)
        {
            for (i++; field[i].value.substr(0, 1).toLowerCase() == "q"; i++)
            {
                field[i].checked = state;
            }
        }
}

/* FUNCTIONS FOR THE COMPARISON TOOL TEMPLATE FORM*/

function selectAll()
{
    for (var x = document.getElementById("multiselect2").options.length - 1; x >= 0; x--)
    {
        document.getElementById("multiselect2").options[x].selected = true;
    }

}


function submit()
{

    document.getElementById("selectionForm").submit();
    //document.selectionForm.submit();
}

function selectAllOptions(selStr)
{
    var selObj = document.getElementById(selStr);
    for (var i = 0; i < selObj.options.length; i++) {
        selObj.options[i].selected = true;
    }
}


