var customizationExpireDate = "11-10-2009";

function chgSection(type) {
    if (type == "earnings") {
        setCookie("HPUMT1", "1", customizationExpireDate, "/", "morningstar.com");
        document.getElementById("HP_EarningsCalendar").style.display = "block";
        document.getElementById("HP_EconomicReport").style.display = "none";
    }
    else if (type == "economic") {
        setCookie("HPUMT1", "2", customizationExpireDate, "/", "morningstar.com");
        document.getElementById("HP_EarningsCalendar").style.display = "none";
        document.getElementById("HP_EconomicReport").style.display = "block";
    }
    else if (type == "sitedirectory") {
        setCookie("HPURT1", "1", customizationExpireDate, "/", "morningstar.com");
        document.getElementById("HP_SiteDirectory").style.display = "block";
        document.getElementById("HP_SiteMap").style.display = "none";
        document.getElementById("HP_Products").style.display = "none";
    }
    else if (type == "sitemap") {
        setCookie("HPURT1", "2", customizationExpireDate, "/", "morningstar.com");
        document.getElementById("HP_SiteDirectory").style.display = "none";
        document.getElementById("HP_SiteMap").style.display = "block";
        document.getElementById("HP_Products").style.display = "none";
    }
    else if (type == "products") {
        setCookie("HPURT1", "3", customizationExpireDate, "/", "morningstar.com");
        document.getElementById("HP_SiteDirectory").style.display = "none";
        document.getElementById("HP_SiteMap").style.display = "none";
        document.getElementById("HP_Products").style.display = "block";
    }
    else if (type == "treasuryYield") {
        setCookie("HPURT3", "1", customizationExpireDate, "/", "morningstar.com");
        document.getElementById("HP_TreasuryYieldList").style.display = "block";
        document.getElementById("HP_TreasuryHistoryList").style.display = "none";
        document.getElementById("HP_GovtBondList").style.display = "none";
    }
    else if (type == "histYield") {
        setCookie("HPURT3", "2", customizationExpireDate, "/", "morningstar.com");
        document.getElementById("HP_TreasuryYieldList").style.display = "none";
        document.getElementById("HP_TreasuryHistoryList").style.display = "block";
        document.getElementById("HP_GovtBondList").style.display = "none";
    }
    else if (type == "govtBond") {
        setCookie("HPURT3", "3", customizationExpireDate, "/", "morningstar.com");
        document.getElementById("HP_TreasuryYieldList").style.display = "none";
        document.getElementById("HP_TreasuryHistoryList").style.display = "none";
        document.getElementById("HP_GovtBondList").style.display = "block";
    }
    else if (type == "portfolio") {
        setCookie("HPURT2", "1", customizationExpireDate, "/", "morningstar.com");
        document.getElementById("HP_PortfolioList").style.display = "block";
        document.getElementById("HP_GainerLoserList").style.display = "none";
        document.getElementById("HP_TopFundStockList").style.display = "none";
    }
    else if (type == "gainerloser") {
        setCookie("HPURT2", "2", customizationExpireDate, "/", "morningstar.com");
        document.getElementById("HP_PortfolioList").style.display = "none";
        document.getElementById("HP_GainerLoserList").style.display = "block";
        document.getElementById("HP_TopFundStockList").style.display = "none";
    }
    else if (type == "topFundStock") {
        setCookie("HPURT2", "3", customizationExpireDate, "/", "morningstar.com");
        document.getElementById("HP_PortfolioList").style.display = "none";
        document.getElementById("HP_GainerLoserList").style.display = "none";
        document.getElementById("HP_TopFundStockList").style.display = "block";
    }

}

function getTreasuryMaturity(periodname) {
    var TreasuryImg = document.getElementById("TreasuryMaturityGraph");
    var TreasuryTitle = document.getElementById("TreasuryMaturityTitle");

    if (periodname == '6 Month')
        TreasuryImg.src = "http://tools.morningstar.com/new_home_page/home_page_image.aspx?type=BC_6MONTH&width=245&height=118";
    if (periodname == '2 Year')
        TreasuryImg.src = "http://tools.morningstar.com/new_home_page/home_page_image.aspx?type=BC_2YEAR&width=245&height=118";
    if (periodname == '5 Year')
        TreasuryImg.src = "http://tools.morningstar.com/new_home_page/home_page_image.aspx?type=BC_5YEAR&width=245&height=118";
    if (periodname == '10 Year')
        TreasuryImg.src = "http://tools.morningstar.com/new_home_page/home_page_image.aspx?type=BC_10YEAR&width=245&height=118";
    if (periodname == '30 Year')
        TreasuryImg.src = "http://tools.morningstar.com/new_home_page/home_page_image.aspx?type=BC_30YEAR&width=245&height=118";
    TreasuryTitle.innerText = periodname + " Note Yield"; // for ie
    TreasuryTitle.textContent = periodname + " Note Yield"; // for all other

}

function getBMK(ticker, name) {
    var BMKImg = document.getElementById("BMKGraph");
    var BMKTitle = document.getElementById("BMKTitle");

    BMKImg.src = "http://tools.morningstar.com/new_home_page/home_page_image.aspx?type=BMK&ticker=" + ticker + "&CountryId=USA&ExchangeId=&TimePeriod=M1&width=245&height=118";

    BMKTitle.innerText = name + " Bond"; // for ie
    BMKTitle.textContent = name + " Bond"; // for all other

}
function openVideoWindow(url) {
    window.open(url, '', 'width=860,height=705');
}

function directToQuote(ticker) {
    return "http://quote.morningstar.com/switch.html?ticker=" + ticker;
}

function MM_findObj(n, d) { //v4.01
    var p, i, x; if (!d) d = document; if ((p = n.indexOf("?")) > 0 && parent.frames.length) {
        d = parent.frames[n.substring(p + 1)].document; n = n.substring(0, p);
    }
    if (!(x = d[n]) && d.all) x = d.all[n]; for (i = 0; !x && i < d.forms.length; i++) x = d.forms[i][n];
    for (i = 0; !x && d.layers && i < d.layers.length; i++) x = MM_findObj(n, d.layers[i].document);
    if (!x && d.getElementById) x = d.getElementById(n); return x;
}

function MM_showHideLayers() { //v6.0
    var i, p, v, obj, args = MM_showHideLayers.arguments;
    for (i = 0; i < (args.length - 2); i += 3) if ((obj = MM_findObj(args[i])) != null) {
        v = args[i + 2];
        if (obj.style) { obj = obj.style; v = (v == 'show') ? 'visible' : (v == 'hide') ? 'hidden' : v; }
        obj.visibility = v;
    }
}

function ClearField(fid) {
    document.getElementById(fid).value = "";
}
function ClearSearchBox(sSearchType) {
    if (sSearchType == "quote") {
        var text = document.quoteForm.sSrchTerm.value;
        if (text == "Quotes")
            document.quoteForm.sSrchTerm.value = "";
    }
    else {
        var text = document.searchForm.sSrchTerm.value;

        if (text == "Search")
            document.searchForm.sSrchTerm.color = "#FFFFFF";
        document.searchForm.sSrchTerm.value = "";
    }
}

function srchTerm(sSearchType) {
    if (sSearchType == "search") {
        document.searchForm.SrchTerm.value = document.searchForm.sSrchTerm.value;
        document.searchForm.action = "http://search.morningstar.com/MstarSrch/MstarSrch2.aspx";
        document.searchForm.submit();
    } else {
        if (document.quoteForm.sSrchTerm.value == "") {
            document.quoteForm.action = "http://quote.morningstar.com/TickerNotFound.html";
            document.quoteForm.submit();
        }
        else {
            document.quoteForm.action = "http://quote.morningstar.com/switch.html?ticker=" + escape(document.quoteForm.sSrchTerm.value);
            document.quoteForm.submit();
        }
    }
}

function ClearSearchBox2(sSearchType) {
    if (sSearchType == "quote") {
        var text = document.quoteForm2.sSrchTerm.value;
        if (text == "Quotes")
            document.quoteForm2.sSrchTerm.value = "";
    }
    else {
        var text = document.searchForm2.sSrchTerm.value;
        if (text == "Search") {
            document.searchForm2.sSrchTerm.color = "#FFFFFF";
            document.searchForm2.sSrchTerm.value = "";
        }
    }
}

function srchTerm2(sSearchType) {
    if (sSearchType == "search") {
        document.searchForm2.SrchTerm.value = document.searchForm.sSrchTerm.value;
        document.searchForm2.action = "http://search.morningstar.com/MstarSrch/MstarSrch2.aspx";
        document.searchForm2.submit();
    } else {
        if (document.quoteForm2.sSrchTerm.value == "") {
            document.quoteForm2.action = "http://quote.morningstar.com/TickerNotFound.html";
            document.quoteForm2.submit();
        }
        else {
            document.quoteForm2.action = "http://quote.morningstar.com/switch.html?ticker=" + escape(document.quoteForm.sSrchTerm.value);
            document.quoteForm2.submit();
        }
    }
}


function GetXmlHttpObject() {
    var xmlHttp = null;
    try {
        // Firefox, Opera 8.0+, Safari
        xmlHttp = new XMLHttpRequest();
    } catch (e) {
        // Internet Explorer
        try {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    return xmlHttp;
}

function UpdPortfolioHolding(start, pfid) {
    var oRequest = GetXmlHttpObject();
    if (oRequest == null) {
        alert("Your browser does not support AJAX!");
        return;
    }

    var url = "GetPortfolio.aspx";
    url = url + "?s=" + start;
    url = url + "&i=" + pfid;
    url = url + "&dt=" + Math.random();
    //alert(url);
    oRequest.open("GET", url, true);
    oRequest.onreadystatechange = function() {
        if (oRequest.readyState == 4) {
            var txt = oRequest.responseText;
            //alert (txt);   
            var oPortHoldingDiv = document.getElementById("HP_PortfolioHolding");
            oPortHoldingDiv.innerHTML = txt;
        }
    }
    oRequest.send(null);
}

/* for site directory */
function turnon(id) {
    for (var i = 1; i <= 12; i++) {
        document.getElementById("Tabmenu" + i).className = "sdir_menu";
        document.getElementById("menu" + i).style.display = "none";

    }
    document.getElementById("Tab" + id).className = "sdir_menuOn";
    document.getElementById(id).style.display = "block";

}

function adjustPos(target, orig, left, top) {
    var origElem = document.getElementById(orig);
    var x = getX(origElem);
    var y = getY(origElem);
    var targetElem = document.getElementById(target);
    targetElem.style.left = (x + left) + "px";
    targetElem.style.top = (y + top) + "px";
}

// get position of an object on the page
function getX(obj) {
    var x = 0;
    var o = obj;
    while (o.offsetParent) {
        x += o.offsetLeft;
        o = o.offsetParent;
    }
    return x;
}
function getY(obj) {
    var y = 0;
    var o = obj;
    while (o.offsetParent) {
        y += o.offsetTop;
        o = o.offsetParent;
    }
    return y;
}

function setCookie(name, value, expires, path, domain) {

    var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "");
    document.cookie = curCookie;

}

function getCookie(name) {
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1) {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    } else
        begin += 2;
    var end = document.cookie.indexOf(";", begin);
    if (end == -1)
        end = dc.length;
    return unescape(dc.substring(begin + prefix.length, end));
}

function CustomSetting() {
    ApplySetting("HPUMT1");
    ApplySetting("HPURT1");
    ApplySetting("HPURT2");
    ApplySetting("HPURT3");
}

function ApplySetting(name) {
    if (getCookie(name) != null) {
        var id = getCookie(name);
        //var elems = document.getElementsByName(name); this doesn't work for IE
        var elems = document.getElementsByTagName("DIV");
        var cnt = 0;

        for (var i = 0; i < elems.length; i++) {
            if (elems[i].getAttribute("name") == name) {
                cnt++;
                if (cnt == id) {
                    elems[i].style.display = "block";
                }
                else {
                    elems[i].style.display = "none";
                }
            }
        }
    }
}
function IsIE7More() {
    return $.browser.msie && parseFloat($.browser.version) >= 7;
}
function showDropdown(b, d, targetId) {
    
    if (d == null) { d = document.ulEle; }
    var target = d.getElementsByTagName("UL")[0];
    var e = target.style.display;
    
    if (e == "none" || e == "") {
        target.style.display = "block";
        d.getElementsByTagName("IMG")[0].style.display = "block";
//        if (!IsIE7More()) {
//            if (d.setCapture) { d.setCapture(false) } else { document.addEventListener("click", showDropdown, true); window.captureEvents(Event.CLICK); } document.ulEle = d;
//            target.focus();
//        }
    } else {
    target.style.display = "none";
    d.getElementsByTagName("IMG")[0].style.display = "none";
//        if (!IsIE7More())
//        {
//            if (d.setCapture) {d.releaseCapture(); } else {
//                document.removeEventListener("click", showDropdown, true); window.releaseEvents(Event.CLICK);
//                b.cancelBubble = true;
//            }
//            var c = (b.srcElement ? b.srcElement : b.target); if (c.className == "pmddh1") {c = c.parentNode; } if (c.className != "pmdd1") {return; } if (c == d) {return; }showDropdown(null, c);
//        }
}
   }

var ReferId = "A3332";
function redirect(url) {
    window.location.href = url + "&referid=" + ReferId;
}
var CustomUrl = window.location.href;
function redirectCustom(url) {
    window.location.href = (url + "&vurl=" + escape(CustomUrl));
}
