// Ending JS file pulled into ALL AWSM pages

// ********************************************** //
// *************** REFER A FRIEND *************** //
// ******************* START ******************** //
// ********************************************** //
if (document.getElementById("displayInfoContactAnchor")) {

    var tempURL = document.getElementById("displayInfoContactAnchor").getAttribute("href");
    var faEVar = "";
    var faECr = "";

    if (tempURL.indexOf("recipient") > -1) {
        faECr = qsReturnValue(tempURL, "recipient");
    }
    else if (tempURL.indexOf("email") > -1) {
        faEVar = qsReturnValue(tempURL, "email");
    }

    // Create Text for Anchor Element
    var anchorTextNode = document.createTextNode("Refer A Friend");

    // Create Anchor Element and attach text node
    var elemNewAnchor = document.createElement("a");
    elemNewAnchor.setAttribute("href", "/branches/library/features/refer_a_friend/?de=" + faECr + "&e=" + faEVar + "&loc=" + filteredResult(window.location));
	elemNewAnchor.setAttribute("target", "_blank");
    elemNewAnchor.setAttribute("onclick", "window.open(this.href,'_blank','top=100,left=100,width=800,height=600,resizable,scrollbars,status'); return false;");
    elemNewAnchor.appendChild(anchorTextNode);

    // Create DIV and attach paragraph element
    var elemNewDiv = document.createElement("div");
    elemNewDiv.setAttribute("id", "referFriendBlock");
    elemNewDiv.appendChild(elemNewAnchor);
}

var tempSetFriendURL = (function () {
    if (document.getElementById("siteContactDisplayInfoBlock") && elemNewDiv) {
        document.getElementById("siteContactDisplayInfoBlock").appendChild(elemNewDiv);
    }
});
// ********************************************** //
// *************** REFER A FRIEND *************** //
// ******************* END ********************** //
// ********************************************** //




// ********************************************** //
// *************** CURRENT ANCHOR *************** //
// ******************* START ******************** //
// ********************************************** //
var currentAnchorElm = (function () {

    var currentPage = location.href;
    var anchorArray = document.getElementsByTagName("a");
    var item;

    for (item in anchorArray) {
        if (anchorArray[item]) {
            if (anchorArray[item].href == currentPage) {
                if (anchorArray[item].className != "") {
                    anchorArray[item].className = anchorArray[item].className + " current";
                }
                else {
                    anchorArray[item].className = "current";
                }
            }
        }
    }
});
// ********************************************** //
// *************** CURRENT ANCHOR *************** //
// ******************* END ********************** //
// ********************************************** //




// ********************************************** //
// ********* CALCULATOR FORMATTING ************** //
// ******************* START ******************** //
// ********************************************** //
var showContent = (function (attBlockID, objTitle) {
	//Hide and Show Sections for Calcs
	
	if (document.getElementById(attBlockID).style.display == "none") {
		document.getElementById(attBlockID).style.display = "block";
		objTitle.innerHTML = objTitle.innerHTML.replace("+", "-");
	}
	else if (document.getElementById(attBlockID).style.display == "block") {
		document.getElementById(attBlockID).style.display = "none";
		objTitle.innerHTML = objTitle.innerHTML.replace("-", "+");
	}
});

var formatCalcPage = (function () {

    //Initialize Array for lists
    var grpOne = "";
    var grpTwo = "";
    var grpThree = "";
    var grpFour = "";
    var grpFive = "";
    var grpSix = "";
    var grpOther = "";
    var grpResources = "";

    //Initalize Array for puling out page from URL
    tempArray = new Array();
    tempArray = location.href.split("/");

    //Build list items for placement on page.
    function setCalcTbl(strCalcName, objCalcHREF, intGroup) {
        var strFormatLine;

        strFormatLine = "<li><a href=\"" + objCalcHREF + "\">" + strCalcName + "</a></li>";

        switch (intGroup) {
            case 1:
                grpOne = grpOne + strFormatLine;
                break;
            case 2:
                grpTwo = grpTwo + strFormatLine;
                break;
            case 3:
                grpThree = grpThree + strFormatLine;
                break;
            case 4:
                grpFour = grpFour + strFormatLine;
                break;
            case 5:
                grpFive = grpFive + strFormatLine;
                break;
            case 6:
                grpSix = grpSix + strFormatLine;
                break;
            case 7:
                grpResources = grpResources + strFormatLine;
                break;
            default:
                grpOther = grpOther + strFormatLine;
                break;
        }
    }

    //Variables used for loopsing and decision tree.
    var countIndex, item, subItem, strCalcName, objCalcHREF, grpIndex, grpTitleString;
    var altObjCalcHREF, altStrName;

    if (document.getElementById("pageHeadingTitle")) {
        if (document.getElementById("pageHeadingTitle").innerHTML === "Financial Resources") {

            if (document.getElementById("financialResourceBlock")) {

                var unorderedLists, listItem;

                for (unorderedLists in document.getElementById("financialResourceBlock").childNodes) {
                    if (document.getElementById("financialResourceBlock").childNodes[unorderedLists].nodeName === "UL") {
                        for (listItem in document.getElementById("financialResourceBlock").childNodes[unorderedLists].childNodes) {
                            if (document.getElementById("financialResourceBlock").childNodes[unorderedLists].childNodes[listItem].nodeName === "LI") {

                                objCalcHREF = document.getElementById("financialResourceBlock").childNodes[unorderedLists].childNodes[listItem].childNodes[0].getAttribute("href");
                                strCalcName = document.getElementById("financialResourceBlock").childNodes[unorderedLists].childNodes[listItem].childNodes[0].childNodes[0].innerHTML;

                                if (document.getElementById("financialResourceBlock").childNodes[unorderedLists].previousSibling.innerHTML) {
                                    grpTitleString = document.getElementById("financialResourceBlock").childNodes[unorderedLists].previousSibling.innerHTML;
                                }
                                else {
                                    grpTitleString = document.getElementById("financialResourceBlock").childNodes[unorderedLists].previousSibling.previousSibling.innerHTML
                                }

                                grpTitleString = grpTitleString.replace("Calculators - ", "");

                                switch (grpTitleString) {
                                    //Loans                           
                                    case "Loans":
                                        grpIndex = 1;
                                        break;
                                    //Retirement                         
                                    case "Retirement":
                                        grpIndex = 2;
                                        break;
                                    //Savings &amp; Investment                         
                                    case "Savings and Investments":
                                        grpIndex = 3;
                                        break;
                                    //Insurance                         
                                    case "Insurance":
                                        grpIndex = 4;
                                        break;
                                    //Personal Finance                         
                                    case "Personal Finance":
                                        grpIndex = 5;
                                        break;
                                    //Tax                         
                                    case "Taxes":
                                        grpIndex = 6;
                                        break;
                                    //Resources     
                                    case "Resources":
                                        grpIndex = 7;
                                        break;
                                    default:
                                        grpIndex = 8;
                                }

                                setCalcTbl(strCalcName, objCalcHREF, grpIndex);
                            }
                        }
                    }
                }
            }
        }
    }

    //Variable to hold the HTML block which will be written onto the page.
    var strContent = "";

    if (grpOne != "") {
        strContent = strContent + "<p><a href=\"\" onclick=\"showContent('loanBlock',this); return false;\">+ Loans</a></p>\n\
                 <div id=\"loanBlock\" style=\"display: none; padding-top: 0px;\">\n\
                 <ul>\n\
                 " + grpOne + "\n\
                 </ul>\n\
                 </div>";
    }

    if (grpTwo != "") {
        strContent = strContent + "<p><a href=\"\" onclick=\"showContent('retirementBlock',this); return false;\">+ Retirement</a></p>\n\
                 <div id=\"retirementBlock\" style=\"display: none; padding-top: 0px;\">\n\
                 <ul>\n\
                 " + grpTwo + "\n\
                 </ul>\n\
                 </div>";
    }

    if (grpThree != "") {
        strContent = strContent + "<p><a href=\"\" onclick=\"showContent('savingsInvestmentBlock',this); return false;\">+ Savings &amp; Investments</a></p>\n\
                 <div id=\"savingsInvestmentBlock\" style=\"display: none; padding-top: 0px;\">\n\
                 <ul>\n\
                 " + grpThree + "\n\
                 </ul>\n\
                 </div>";
    }

    if (grpFour != "") {
        strContent = strContent + "<p><a href=\"\" onclick=\"showContent('insuranceBlock',this); return false;\">+ Insurance</a></p>\n\
                 <div id=\"insuranceBlock\" style=\"display: none; padding-top: 0px;\">\n\
                 <ul>\n\
                 " + grpFour + "\n\
                 </ul>\n\
                 </div>";
    }

    if (grpFive != "") {
        strContent = strContent + "<p><a href=\"\" onclick=\"showContent('personalFinanceBlock',this); return false;\">+ Personal Finance</a></p>\n\
                 <div id=\"personalFinanceBlock\" style=\"display: none; padding-top: 0px;\">\n\
                 <ul>\n\
                 " + grpFive + "\n\
                 </ul>\n\
                 </div>";
    }

    if (grpSix != "") {
        strContent = strContent + "<p><a href=\"\" onclick=\"showContent('taxBlock',this); return false;\">+ Taxes</a></p>\n\
                 <div id=\"taxBlock\" style=\"display: none; padding-top: 0px;\">\n\
                 <ul>\n\
                 " + grpSix + "\n\
                 </ul>\n\
                 </div>";
    }

    if (grpOther != "") {
        strContent = strContent + "<p>Miscellaneous Calculators</p>\n\
                 <ul>\n\
                 " + grpOther + "\n\
                 </ul>";
    }

    if (grpResources != "") {
        strContent = strContent + "<h3>Resources</h3>\n\
                 <div id=\"altResources\" style=\"padding-top: 0px;\">\n\
                 <ul>\n\
                 " + grpResources + "\n\
                 </ul>\n\
                 </div>"
    }

    if (document.getElementById("financialResourceBlock")) {
        document.getElementById("financialResourceBlock").innerHTML = "<h3>Calculators</h3>" + strContent;
    }
});
// ********************************************** //
// ********* CALCULATOR FORMATTING ************** //
// ******************* END ********************** //
// ********************************************** //




// ********************************************** //
// ****** ADD SOLUTION STYLE SHEET ************** //
// *************** START ************************ //
// ********************************************** //
var solutionStyles = (function () {
    if (typeof urlArray === "object") {
        if (urlArray[(urlArray.length - 1)].indexOf("RetirementPlanning") > -1) {
            // Determine if MAIN STYLE SHEET EXISTS
            if (document.getElementById("mainStyle")) {
                // Make sure primary style sheet is valild
                if (document.getElementById("mainStyle").nodeName === "LINK") {
                    // Create LINK element, set attrbutes
                    var solutionLink = document.createElement("link");
                    solutionLink.setAttribute("href", document.getElementById("mainStyle").href.replace(".css", "_solution.css"));
                    solutionLink.setAttribute("rel", "stylesheet");
                    solutionLink.setAttribute("type", "text/css");
                    // Add LINK element to HEAD element
                    document.getElementsByTagName("head")[0].appendChild(solutionLink);
                }
            }
        }
    }
});
// ********************************************** //
// ****** ADD SOLUTION STYLE SHEET ************** //
// ******************* END ********************** //
// ********************************************** //




// ********************************************** //
// ********** MARKET VIEW STYLE ***************** //
// *************** START ************************ //
// ********************************************** //
var marketViewStyle = (function () {
    if (typeof urlArray === "object") {
        if (urlArray[(urlArray.length - 1)].indexOf("MarketView") > -1) {
            var marketViewLink = document.createElement("link");
            marketViewLink.setAttribute("href", "/AdvisorSites/AdvisorWebSites/Common/styles/marketViewTabs.css");
            marketViewLink.setAttribute("rel", "StyleSheet");
            marketViewLink.setAttribute("type", "text/css");

            document.getElementsByTagName("head")[0].appendChild(marketViewLink);
			
			var proSpeakingArrayScript = document.createElement("script");
			proSpeakingArrayScript.setAttribute("src", "/AdvisorSites/AdvisorWebSites/Common/script/pro_speaking_js_array.asp");
			proSpeakingArrayScript.setAttribute("type", "text/javascript");
			
			document.getElementsByTagName("head")[0].appendChild(proSpeakingArrayScript);

			var proSpeakingFormatScript = document.createElement("script");
			proSpeakingFormatScript.setAttribute("src", "/AdvisorSites/AdvisorWebSites/Common/script/market_view_formating.js");
			proSpeakingFormatScript.setAttribute("type", "text/javascript");
			
			document.getElementsByTagName("body")[0].appendChild(proSpeakingFormatScript);
        }
    }
});
// ********************************************** //
// ********** MARKET VIEW STYLE ***************** //
// *************** START ************************ //
// ********************************************** //




// ********************************************** //
// ************** WI DISCLAIMER ***************** //
// ********************************************** //
// ********************************************** //
// If the site has the State WI in it's TITLE element it adds a disclaimer as listed below
var customWiDisclaimer = (function () {
    var pageTitle, daComma, strEnd, stateCheck, affType, phoneNum;

    if (document.getElementsByTagName("title")) {
        pageTitle = document.getElementsByTagName("title");
        pageTitle = pageTitle[0].innerHTML;

        daComma = pageTitle.indexOf(",") + 2;
        strEnd = pageTitle.length;
        stateCheck = pageTitle.substring(daComma, strEnd);
    }

    if (document.getElementById("disclaimerCell")) {
        affType = document.getElementById("disclaimerCell");
    }

    if (document.getElementById("siteContactDisplayInfoBlock")) {
        phoneNum = document.getElementById("siteContactDisplayInfoBlock");
    }

    if (stateCheck && affType && phoneNum) {
        if (affType.innerHTML.indexOf("Raymond James Financial Services") >= 0) {
            if (stateCheck.indexOf("WI") >= 0) {
                if (phoneNum) {

                    var disclaimerText;

                    if (phoneNum.innerHTML.indexOf("262-378-4328") >= 0) {
                        disclaimerText = document.createTextNode("Investment advisory services offered through Schreiber Investment Advisory Services");
                    }
                    else {
                        disclaimerText = document.createTextNode("Investment advisory services offered through Raymond James Financial Services Advisors, Inc");
                    }

                    var addDisc = document.createElement("p");
                    addDisc.appendChild(disclaimerText);
                    document.getElementById("disclaimerCell").getElementsByTagName("div")[0].appendChild(addDisc);
                }
            }
        }
    }
});
// ********************************************** //
// ************** WI DISCLAIMER ***************** //
// ********************************************** //
// ********************************************** //




// ********************************************** //
// *************** WINDOW ONLOAD **************** //
// ******************* START ******************** //
// ********************************************** //
if (window.addEventListener) {
    //Works for NON-IE and IE9+ Browsers
    window.addEventListener("load", currentAnchorElm, false);
    window.addEventListener("load", tempSetFriendURL, false);
    window.addEventListener("load", formatCalcPage, false);
    window.addEventListener("load", solutionStyles, false);
    window.addEventListener("load", marketViewStyle, false);
    window.addEventListener("load", customWiDisclaimer, false);
}
else if (window.attachEvent) {
    //Works for ALL IE Browsers
    window.attachEvent("onload", currentAnchorElm);
    window.attachEvent("onload", tempSetFriendURL);
    window.attachEvent("onload", formatCalcPage);
    window.attachEvent("onload", solutionStyles);
    window.attachEvent("onload", marketViewStyle);
    window.attachEvent("onload", customWiDisclaimer);
}

// ********************************************** //
// *************** WINDOW ONLOAD **************** //
// ******************* END ********************** //
// ********************************************** //


