﻿/*###########################################################################*/
/*LA amends: 08/07/02011: new menu for Premium*/
var exitloop = false;

function trim(inputString) {
    // Removes leading and trailing spaces from the passed string. Also removes
    // consecutive spaces and replaces it with one space. If something besides
    // a string is passed in (null, custom object, etc.) then return the input.
    if (typeof inputString != "string") { return inputString; }
    var retValue = inputString;
    var ch = retValue.substring(0, 1);
    while (ch == " ") { // Check for spaces at the beginning of the string
        retValue = retValue.substring(1, retValue.length);
        ch = retValue.substring(0, 1);
    }
    ch = retValue.substring(retValue.length - 1, retValue.length);
    while (ch == " ") { // Check for spaces at the end of the string
        retValue = retValue.substring(0, retValue.length - 1);
        ch = retValue.substring(retValue.length - 1, retValue.length);
    }
    while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
        retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ") + 1, retValue.length); // Again, there are two spaces in each of the strings
    }
    return retValue; // Return the trimmed string back to the user
} // Ends the "trim" function

function hidealllevels(ID, classname) {
    var obj;
    $(classname).each(function(i) {
        if (document.getElementById(this.id)) {
            document.getElementById(this.id).style.display = "none";
            if (this.id != ID) {
            } //if id = id2
            else { //if id2 = id then we are on the menu we want to open
                if (document.getElementById(ID)) {
                    obj = document.getElementById(ID);
                    obj.style.display == "block";
                }
            }
        }
        obj == "";

    });        //end function


}

//function resetMenu() {
//    $(".level1").each(function(i) {
//        if (this.id != "") {
//            $(this.id).children().each(function(i) {
//                if (this.id != "") {
//                    document.getElementById(this.id).style.backgroundColor = "#1c201f";
//                    document.getElementById(this.id).style.color = "#cccccc";
//                    document.getElementById(this.id).style.textDecoration = "none";
//                }
//            });
//           // document.getElementById(this.id).style.display = "none";
//        }
//    });
//    $(".level2").each(function(i) {
//        if (this.id != "") {
//            $(this.id).children().each(function(i) {
//                if (this.id != "") {
//                    document.getElementById(this.id).style.backgroundColor = "#a3bcb4";
//                    document.getElementById(this.id).style.color = "#151112";
//                    document.getElementById(this.id).style.textDecoration = "none";
//                }
//            });
//             document.getElementById(this.id).style.display = "none";
//        }
//    });
//    $(".level3").each(function(i) {
//        if (this.id != "") {
//            $(this.id).children().each(function(i) {
//                if (this.id != "") {
//                    //document.getElementById(this.id).style.backgroundColor = "#a3bcb4";
//                    //document.getElementById(this.id).style.color = "#151112";
//                    document.getElementById(this.id).style.textDecoration = "none";
//                }
//            });
//            document.getElementById(this.id).style.display = "none";
//        }
//    });
//}


function removehighlight(id, classname) {
    var level1elm, level2elm;
    if (id != "") {
    //remove all top level highlights
        $(".level1").children().each(function(i) {
           
           
            if (this.id != "") {
                if (document.getElementById(this.id)) {
                    document.getElementById(this.id).style.backgroundColor = "#1c201f";
                    document.getElementById(this.id).style.color = "#cccccc";
                    document.getElementById(this.id).style.textDecoration = "none";
                }
            }
        });
        $(".level2").children().each(function(i) {
            if (this.id != "") {
                if (document.getElementById(this.id)) {
                    document.getElementById(this.id).style.backgroundColor = "#a3bcb4";
                    document.getElementById(this.id).style.color = "#151112";
                    document.getElementById(this.id).style.textDecoration = "none";
                }
            }
        });
        //if we are dealing with 2nd level then highlight top level to show where we are
        if (classname == ".level2") {
            if (id != "") {
                level1elm = "main_" + id;
                level2elm = id + "2";
                if (document.getElementById(level1elm)) {
                    document.getElementById(level1elm).style.backgroundColor = "#a3bcb4";
                    document.getElementById(level1elm).style.color = "#151112";
                    document.getElementById(level1elm).style.textDecoration = "none";

                }
            }
           
        }
        //if we are dealing with 3rd level then highlight 2nd and 1st tree structure
        if (classname == ".level3") {
            if (id != "") {
                level1elm = "main_" + id.substring(0, id.indexOf('_'));
                level2elm = id + "2";
                if (document.getElementById(level2elm)) {
                    document.getElementById(level2elm).style.backgroundColor = "#4e5a56";
                    document.getElementById(level2elm).style.color = "#c3c4c4";
                    document.getElementById(level2elm).style.textDecoration = "none";
                 }
                if (document.getElementById(level1elm)) {
                    document.getElementById(level1elm).style.backgroundColor = "#a3bcb4";
                    document.getElementById(level1elm).style.color = "#151112";
                    document.getElementById(level1elm).style.textDecoration = "none";
                }
                if (document.getElementById(this.id)) {
                   // document.getElementById(this.id).style.textDecoration = "underline";
                }
            }
        }
    }
    level1elm == "";
    level2elm == "";
  
}
function removeunderlineLevel3() {
    $(".level3").children().each(function(i) {
        if (document.getElementById(this.id)) {
            document.getElementById(this.id).style.textDecoration = "none";
        }
    });
}
//function to check if url in menu is the same as current url
function checklevels(classname, sname, parentid) {
    var a, b,c, hyperhref, parentid2, sublevels, myid, myname2, myname, level2elm, toplevel;
    
    //for each child element under element with classname of....
    $(classname).children().each(function(i) {

        //only process if an hyperlink
        if ($(this).attr("class") == "divwrap") {
        }
        else {
            //get hyperlink from child element
            hyperhref = $(this).attr("href");
            //if empty or # hyperlink then make it a # else remove / from url ready for matching
            if (hyperhref == "#" || hyperhref == undefined) {
                a = "#";
            }
            else {
                a = String(hyperhref.substring(1));
            }
            //get page url of current page/url
            b = String(sname.toString());
            c = String(a.toString()) + "?";
            //if hyperlink matches current url
            if ((trim(a) == trim(b)) || (trim(c) == trim(b))) {
                //get element id of element above this one
                parentid2 = this.parentNode.id
                //if element is at top level then
                if (classname == ".level1") {
                    myid = this.id;
                    //remove main from id so we can show sub level 2 menu items
                    sublevels = myid.split("_");
                    //style up top level element
                    if (myid != "") {
                        if (document.getElementById(myid)) {
                            document.getElementById(myid).style.color = "#000000";
                            document.getElementById(myid).style.backgroundColor = "#a3bcb4";
                            document.getElementById(myid).style.textDecoration = "underline";
                        }
                    }
                    //show sub menu if applicable
                    if (sublevels[1] != "" || sublevels[1] != undefined) {
                        if (document.getElementById(sublevels[1])) {
                            document.getElementById(sublevels[1]).style.display = "block";
                        }
                        //else if no sub menu then just underline do not highlight with sub menu background colour
                        else {
                            document.getElementById(myid).style.color = "#cccccc";
                            document.getElementById(myid).style.backgroundColor = "#1c201f";
                        }
                    }

                }
                //if element is at sub level 2
                if (classname == ".level2") {
                    //get this elements id
                    myid = $(this).attr("id");
                    //remove the number 2 from id of this element so we can find 3rd level elements
                    if (myid != "") {
                        sublevels = String(myid).substring(0, String(myid).length - 1);
                        //get top level menu elementid
                        myname2 = "main_" + parentid2;

                        //highlight top level hyperlink selected
                        if (document.getElementById(myname2)) {
                            document.getElementById(myname2).style.backgroundColor = "#a3bcb4";
                            document.getElementById(myname2).style.color = "#000000";
                        }
                        //show top level menu items
                        if (document.getElementById(parentid2)) {
                            document.getElementById(parentid2).style.display = "block";
                            document.getElementById(parentid2).style.color = "#000000";
                        }
                        //level 2 hyperlink selected
                        if (document.getElementById(myid)) {
                            document.getElementById(myid).style.backgroundColor = "#4e5a56";
                            document.getElementById(myid).style.color = "#c3c4c4";
                            document.getElementById(myid).style.textDecoration = "underline";
                        }
                        //show level 3 menu items
                        //alert(sublevels);

                        if (document.getElementById(sublevels)) {
                            document.getElementById(sublevels).style.display = "block";

                        }
                        //else if no sub menu underneath then do not highlight with sub menu background - just underline
                        else {
                            document.getElementById(myid).style.backgroundColor = "#a3bcb4";
                            document.getElementById(myid).style.color = "#151112";
                        }

                    }
                }
                //if element is a level3
                if (classname == ".level3") {
                    //find element id
                    myname = parentid2.substring(0, parentid2.indexOf('_'));
                    //if element id not empty
                    if (myname != "") {
                        //get level2 element id
                        sublevels = parentid2 + "2";
                        //get top level element id
                        toplevel = "main_" + myname;
                        //also highlight the level 2 item
                        if (document.getElementById(sublevels)) {
                            document.getElementById(sublevels).style.backgroundColor = "#4e5a56";
                            document.getElementById(sublevels).style.color = "#c3c4c4";
                        }
                        //  also highlight level1
                        if (document.getElementById(myname)) {
                            document.getElementById(myname).style.display = "block";
                        }
                        if (document.getElementById(toplevel)) {
                            document.getElementById(toplevel).style.backgroundColor = "#a3bcb4";
                            document.getElementById(toplevel).style.color = "#000000";
                        }


                    }


                    //open 3rd level
                    if (parentid2 != "") {
                        if (document.getElementById(parentid2)) {
                            document.getElementById(parentid2).style.display = "block";
                            document.getElementById(this.id).style.textDecoration = "underline";
                        }
                    }
                }

                exitloop = true;
                return false;
            }
            hyperhref == "";
            a == "";
            b == "";
            parentid2 == "";
            sublevels == "";
            myid == "";
            myname2 == "";
            myname == "";
            toplevel == "";
        }
    });
}


//function to check which page we are on and what menus to open
function checkpage() {
    var pagename, parentid, classname, hyperhref, classname;
    pagename = window.location.href;
    var sname = pagename.substring(pagename.lastIndexOf('/') + 1);
    if (sname.indexOf("#") > 1) {
        sname = sname.substring(0, sname.length - 1);
    }
    // alert(sname);
    //loop through all elements under premiummenu id
    $("#PremiumMenu").children().each(function(i) {
        //if exitloop is true (i.e. we have found what we are looking for in anohter function loop then quit this loop too
        if (exitloop) {
            return false;
        }
        else {
            //else not found anything yet so go through the levels and find something
            classname = $(this).attr("class");
            if (classname != "") {
                checklevels("." + classname, sname, this.id);
            }
        }
    });

    classname == "";

}
//if clicked on or hovered over then show or hide the menus accordingly
function showmenu(id1, id2) {
    var obj;
    if (document.getElementById) {
       if (id1 != "" && id2 == "") {
            if (document.getElementById(id1)) {
                obj = document.getElementById(id1);
              
            }
            else {
                obj = null;
            }
            hidealllevels(id1, ".level2");
            hidealllevels(id2, ".level3");
            removehighlight(id1, ".level2");
            removehighlight(id2, ".level3");
               
        }
        else {
            //if id2 has something  in it
            if (id1 != "" && id2 != "") {

                if (document.getElementById(id2)) {
                    obj = document.getElementById(id2);
               }
                else {
                    obj = null;
                }
                //turn off all level 3's apart from one we want to open before we do anything
                hidealllevels(id2, ".level3");
                removehighlight(id2, ".level3");
               
            } //end if id2 has something in it
        } //end else



    }
    if (obj != undefined || obj != null) {
        if (obj.style.display == "none" || obj.style.display == "") {
            obj.style.display = "block";
   }
        else {
    obj.style.display = "block";

        }
    }
    obj == "";


}

