﻿//<!--
function getHTTPObject() {
    var xmlhttp;
    /*@cc_on
    @if (@_jscript_version >= 5)
    try {
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
        try {
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        } catch (E) {
            xmlhttp = false;
        }
    }
    @else
      xmlhttp = false;
      @end
    @*/
    if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
        try {
            xmlhttp = new XMLHttpRequest();
        } catch (e) {
            xmlhttp = false;
        }
    }
    return xmlhttp;
}
var http = getHTTPObject(); // We create the HTTP Object

//---------------------------------------------------------
var _id;
var _page;
function getContent(id, page) {
    if (_id == id && _page == page) { return; }
    if (id == 0 && _page == page) { return; }
    _id = id;
    _page = page;
    if (!isNaN(page)) {
        ChangeQuicklinks(page);
    }
    else {
        ChangeQuicklinks(id);
    }

    showExpandedMenu(id);

    var date = new Date();
    var d = date.getTime();

    flashMessage(id);
    changeDownloads(id);

    $("#div_line").css("background-color", "#ffffff").fadeOut("fast");
    try { if (ccTitle == undefined) return; } catch (error) { return; }

    $("#" + ccTitle).css("background-color", "#ffffff").fadeOut("fast", function() {

        var url = "/AJAX/indivInvestors.aspx?ID=" + id + "&date=" + d;

        if (page != null && isNaN(page)) {
            url = page;
            if (page.substring(0, 1) == "/" && id > 0) {
                window.location = page;
                return;				
            }

            if (page.indexOf("?") == -1) {
                url = page + "?date=" + d.toString();
            }
            else {
                url = page + "&date=" + d.toString();
            }
        }
        $.ajax({
            url: url,
            success: function(result) {
                $("#" + ccTitle).html(result);
                $("#div_line").fadeIn("fast");
                $("#" + ccTitle).fadeIn("fast");
                trackClick($("#hdnTitle").attr('value'));
                if ($("#hdnURL").attr('value') != null && $("#hdnURL").attr('value') != '') {                    
					if ($("#hdnURL").attr('value') == location.hash.replace(/#/,''))return;
					window.location = "#" + $("#hdnURL").attr('value');
                }
            }
        });

    });
}

//---------------------------------------------------------

function emailPage() {

    var url = '/AJAX/getcontentID.aspx?str=' + location.hash.toString().replace(/#/, "");

    $.ajax({
        url: url,
        success: function(data) {
            var pos = window.location.toString().indexOf("#");
            var a = window.location.toString().substring(0, pos)
            a = a + "?id=" + data;
            parent.location = "mailto:?subject=Information from Allan Gray website&body=" + a;
        }
    })

}

function emailArticle() {

    var url = '/AJAX/getcontentID.aspx?str=' + location.hash.toString().replace(/#/, "");
    $.ajax({
        url: url,
        success: function(data) {
            var pos = window.location.toString().indexOf("#");
            var a = window.location.toString().substring(0, pos).replace("?id", "?pre");
            var full = window.location.toString();
            a = a + "?id=" + data;
            parent.location = "mailto:?subject=Information from Allan Gray website&body=" + full;
        }
    })
}

function emailArticleQCStatic() {

    var url = '/AJAX/getcontentID.aspx?str=' + location.hash.toString().replace(/#/, "");
    $.ajax({
        url: url,
        success: function(data) {
            var pos = window.location.toString().indexOf("#");
            var a = window.location.toString().substring(0, pos).replace("?id", "?pre");
            a = a + "?id=" + data;
            parent.location = "mailto:?subject=Information from Allan Gray website&body=" + window.location;
        }
    })
}

//---------------------------------------------------------
var alreadyRun;
function getQuicklinkContent(id, page, catID) {

    var date = new Date();
    var d = date.getTime();

    $("#div_line").css("background-color", "#ffffff").fadeOut("fast");
    $("#" + ccTitle).css("background-color", "#ffffff").fadeOut("fast", function() {
        var url = "/AJAX/indivInvestors.aspx?ID=" + id + "&date=" + d;
        if (page != null) {
            url = page;
            if (page.substring(0, 1) == "/" && id > 0) {
                window.location = page;
            }
            if (!isNaN(catID)) {
                if (page.indexOf("?") == -1) {
                    url = page + "?catID=" + catID;
                }
                else {
                    url = page + "&catID=" + catID;
                }

                url = url + "&date=" + d.toString();
            }
        }

        http.open("GET", url, true);
        http.onreadystatechange = function() {
            if (http.readyState == 4) {
                result = http.responseText;

                //Showing Content
                $("#" + ccTitle).html(result);
                $("#div_line").fadeIn("fast");
                $("#" + ccTitle).fadeIn("fast");

                //Tracking
                trackClick($("#hdnTitle").attr('value'));

                if ($("#hdnURL").attr('value') != null && $("#hdnURL").attr('value') != '') {
                    alreadyRun = true;
                    window.location = "#" + $("#hdnURL").attr('value');
                }

                return false;
            }
        };
        http.send(null);

    });
}

//---------------------------------------------------------

function trackClick(link) {
    try { pageTracker._trackPageview(link); }
    catch (err) { }
    try { _gaq.push(['_trackPageview', link]); }
    catch (err) { }

}

//---------------------------------------------------------

function getFund(id, txt) {

    var date = new Date();
    var d = date.getTime();

    var url = "/AJAX/getFund.aspx?ID=" + id + "&type=" + txt + "&d=" + d;
    http.open("GET", url, true);
    http.onreadystatechange = function() {
        if (http.readyState == 4) {

            result = http.responseText;

            if (result.substring(0, 5).toLowerCase() == 'error') {
                alert(result);
            }
            else {
                trackPDF(result);
                //window.open(result);
            }

            return false;
        }
    };
    http.send(null);

    trackClick(url);
}
//---------------------------------------------------------

function toggleFunds(id, div) {
    if (id == 1) {
        if ($("#fund_otherDomestic").css("display") == 'none') {
            viewFunds(1);
            fundDiv(div);
        }
    }
    else {
        if ($("#fund_otherInt").css("display") == 'none') {
            viewFunds(2);
            fundDiv(div);
        }
    }
}

function viewFunds(id) {
    var date = new Date();
    var d = date.getTime();

    var url = "/AJAX/getOtherFunds.aspx?ID=" + id + "&d=" + d;
    http.open("GET", url, true);
    http.onreadystatechange = function() {
        if (http.readyState == 4) {

            result = http.responseText;

            if (result.substring(0, 5).toLowerCase() == 'error') {
                alert(result);
            }
            else {
                if (id == 1) {
                    $("#divDomesticOther").html(result);
                }
                else {
                    $("#divInternationalOther").html(result);
                }
            }

            return false;
        }
    };
    http.send(null);
}

function hideFunds(id) {
    $("#vf" + id + "-1").css("display", "none");
    $("#vf" + id).css("display", "block");

    if (id == 1) {
        $("#divDomesticOther").css("display", "none");
    }
    else {
        $("#divInternationalOther").css("display", "none");
    }
}

var i;
function getPageHash(str) {
    /*
    if (str.indexOf("qcre") > -1) {
    i = 1;
    }
    if (str.indexOf("qcre") > -1 && i == 1) return;
    */


    if (window.location.search.substring(1).indexOf('id') != -1) {
        return;
    }
    $(document).ready(function() {
        var hash = location.hash;

        if (hash == '' || hash == 'undefined') {
            if (str != null && str != undefined) {
                hash = str;
            }
            else {
                return;
            }
        }
        else {
            hash = hash.substring(1);
        }
        //if (hash.toLowerCase() == str.toLowerCase() && i > 0) { i = 0; return; } else { i = i + 1; }

        var url = '/AJAX/getcontentID.aspx?str=' + hash;
        http.open("GET", url, true);
        http.onreadystatechange = function() {
            if (http.readyState == 4) {
                result = http.responseText;                
                if (isNaN(result)) {
                    getContent(0, result);
                }
                else {
                    getContent(result);
                }
                return false;
            }
        };
        http.send(null);
    });


}

function genSpreadsheet(id) {

    var startdate;
    var enddate;

    var sYear = document.getElementById("ddlYear" + id.toString());
    var sMonth = document.getElementById("ddlMonth" + id.toString());
    var sDay = document.getElementById("ddlDay" + id.toString());

    var eYear = document.getElementById("ddlYear" + id.toString() + "End");
    var eMonth = document.getElementById("ddlMonth" + id.toString() + "End");
    var eDay = document.getElementById("ddlDay" + id.toString() + "End");

    //new Date("yy,mm,dd")

    startdate = new Date(sYear[sYear.selectedIndex].value, sMonth[sMonth.selectedIndex].value - 1, sDay[sDay.selectedIndex].value);
    enddate = new Date(eYear[eYear.selectedIndex].value, eMonth[eMonth.selectedIndex].value - 1, eDay[eDay.selectedIndex].value);

    if (startdate.getMonth() != (sMonth[sMonth.selectedIndex].value - 1)) {
        alert("Your start date is invalid, please adjust your dates");
        return false;
    }

    if (enddate.getMonth() != (eMonth[eMonth.selectedIndex].value - 1)) {
        alert("Your end date is invalid, please adjust your dates");
        return false;
    }

    if (startdate > enddate || startdate == enddate) {
        alert("Please adjust your end date");
        return false;
    }

    var sd = startdate.getFullYear().toString() + "-" + (startdate.getMonth() + 1).toString() + "-" + startdate.getDate().toString();
    var ed = enddate.getFullYear().toString() + "-" + (enddate.getMonth() + 1).toString() + "-" + enddate.getDate().toString();


    var url = '/AJAX/createSpreadsheet.aspx?locationID=' + id + '&startdate=' + sd + '&enddate=' + ed;
    window.open(url);
}


function ChangeQuicklinks(id) {
    if (id == undefined) return;

    var date = new Date();
    var d = date.getTime();

    var url = '/AJAX/changeQuicklinks.aspx?ID=' + id + "&d=" + d;
    http.open("GET", url, true);
    http.onreadystatechange = function() {
        if (http.readyState == 4) {
            result = http.responseText;

            if (result.substring(0, 5).toLowerCase() == 'error') {
                //alert(result);
                return;
            }

            if (result.replace(/^\s+|\s+$/g, "") == '') {
                //$("#quicklinksParent").css("display", "none");
                return;
            }

            $("#divQL").html(result);
            $("#quicklinksParent").css("display", "block");
            return false;
        }
    };
    http.send(null);
}


function changeDownloads(id) {
    var date = new Date();
    var d = date.getTime();

    var url = '/AJAX/changeDownloads.aspx?ID=' + id + "&d=" + d;
    $.ajax({ url: url, success: function(result) {
        if (result.substring(0, 5).toLowerCase() == 'error') {
            //alert(result);
            return;
        }

        if (result.replace(/^\s+|\s+$/g, "") == '') {
            $("#downloadParent").css("display", "none");
            return;
        }
        else {
            $("#divDL").html(result);
            $("#downloadParent").css("display", "block");
            return false;
        }
    }
    });
}

//Comments
var MailSend = false;
function SendMail() {
    var comment, about, email, name;

    comment = document.getElementById("txtComment");
    about = document.getElementById("txtAbout");
    email = document.getElementById("txtEmail");
    name = document.getElementById("txtName");

    if (comment.value.replace(/^\s+|\s+$/g, "") == "") {
        alert('Please enter your comment');
        comment.focus();
        return;
    }

    if (!MailSend) {
        var ebRand = Math.random() + '';
        ebRand = ebRand * 1000000;
        $("#ContactForm").html("<ifr" + "ame src='http://fls.doubleclick.net/activityi;src=2835527;type=indiv330;cat=yourc800;ord=" + ebRand + "?' width='1' height='1' frameborder='0' style='display:none'></ifr" + "ame>");        
        MailSend = true;
    }
    $("#ContactForm").html("<p style='color:#FF373F'>Sending...</p>");

    

    var vars = 'name=' +  encodeURIComponent(name.value) + "&comment=" + encodeURIComponent(comment.value) + "&about=" + encodeURIComponent(about.value) + "&email=" + encodeURIComponent(email.value);
    var url = '/AJAX/sendEmail.aspx';
    $.ajax({
        url: url,
        type: "POST",
        data: vars,
        success: function (result) {
            $("#ContactForm").html("<p style='color:#FF373F'>" + result + "</p>");
            return false;
        }
    });   
}

//contact details / contact me
var ContactSend = false;
function SendContactMail() {
    var comment, about, email, name, number;
    var rdoMethod;

    comment = document.getElementById("txtComment");
    number = document.getElementById("txtNumber");
    email = document.getElementById("txtEmail");
    name = document.getElementById("txtName");

    rdoMethod = document.getElementsByName("rdoGroup");
    var selected = '';
    for (i = 0; i < rdoMethod.length; i++) {
        if (rdoMethod[i].checked) {
            selected = rdoMethod[i].value;
        }
    }


    switch (selected) {
        case "Email":
            if (valEmail(email)) {
                email.focus();
                return;
            }
            if (comment.value.replace(/^\s+|\s+$/g, "") == "") {
                alert('Please enter a comment');
                comment.focus()
                return;
            }
            break;
        case "Telephone":
            if (!validateTel("txtNumber")) {
                alert('Please enter your telephone number');
                return;
            }
            break;
        case "Either":
            if (email.value.replace(/^\s+|\s+$/g, "") != "") {
                if (valEmail(email)) {
                    email.focus();
                    return;
                }
                if (comment.value.replace(/^\s+|\s+$/g, "") == "") {
                    alert('Please enter a comment');
                    comment.focus()
                    return;
                }
            }
            if (number.value.replace(/^\s+|\s+$/g, "") != "") {
                if (!validateTel("txtNumber")) {
                    alert('Please enter your telephone number');
                    return;
                }
            }

            if (email.value.replace(/^\s+|\s+$/g, "") == "" && number.value.replace(/^\s+|\s+$/g, "") == "") {
                alert('Please enter either your telephone number or email address');
                return;
            }

            break;        
    }

    var vars = 'name=' + name.value + "&comment=" + comment.value + "&number=" + number.value + "&email=" + email.value + "&cMethod=" + selected;
    var url = '/AJAX/sendContactMeEmail.aspx';

    if (!ContactSend) {
        var ebRand = Math.random() + '';
        ebRand = ebRand * 1000000;
        $("#ContactForm").html("<ifr" + "ame src='http://fls.doubleclick.net/activityi;src=2835527;type=indiv330;cat=conta791;ord=" + ebRand + "?' width='1' height='1' frameborder='0' style='display:none'></ifr" + "ame>");
        ContactSend = true;
    }

    $("#ContactForm").html("<p style='color:#FF373F'>Sending...</p>");
    $.ajax({
        url: url,
        type: "POST",
        data: vars,
        success: function (result) {
            $("#ContactForm").html("<p style='color:#FF373F'>" + result + "</p>");
            return false;
        }
    });
}

function getQcPdf() {
    var url = $("#pdfLink").attr('value');
    window.open(url);
}


function fundDiv(id) {
    if ($("div[id*='_fund']:visible").attr("id") == id) return;

    $("#factsheetNav img").each(function() {
        var src = $(this).attr('src');
        if (src.indexOf("_o.") > -1) {
            src = src.replace('_o.', '.');
            $(this).attr('src', src);
        }
    });

    var imgSrc = $("#img_" + id).attr('src');
    imgSrc = imgSrc.replace(/.gif/, '_o.gif');
    $("#img_" + id).attr('src', imgSrc);

    $("div[id*='fund_']").each(function() {
        $(this).css('display', 'none');
    });

    $("#" + id).fadeIn('400');
}

function showYears(suffix) {
    var ddl = document.getElementById('ddlfundlist' + suffix);
    if (ddl.selectedIndex == 0) return;

    var url = '/AJAX/getFundDates.aspx?format=yyyy&id=' + ddl[ddl.selectedIndex].value + "&suffix=" + suffix;
    http.open("GET", url, true);
    http.onreadystatechange = function() {
        if (http.readyState == 4) {
            result = http.responseText;
            $("#year" + suffix).html(result);
            $("#tblDates" + suffix).fadeIn(500);
            $(".monthTR").css({ visibility: 'hidden' });
            $("#btnTR" + suffix).css({ display: 'none' });
            if (suffix != 'PooledPortfolio') {
                $("#headPooledPortfolio").css('visibility', 'hidden');
                $("#monthPooledPortfolio").css('visibility', 'hidden');
                $("#btnTRPooledPortfolio").html('');
            }
            return false;
        }
    };
    http.send(null);
}
function showMonths(suffix, fundID) {
    if (isNaN(fundID) || fundID == undefined || fundID == null) {
        var ddl = document.getElementById('ddlfundlist' + suffix);
        if (ddl.selectedIndex == 0) return;
        fundID = ddl[ddl.selectedIndex].value;
    }
    var ddlY = document.getElementById('ddlfundYear' + suffix);
    if (ddlY.selectedIndex == 0) return;

    var url = '/AJAX/getFundDates.aspx?format=mm&id=' + fundID + '&year=' + ddlY[ddlY.selectedIndex].value + "&suffix=" + suffix;
    http.open("GET", url, true);
    http.onreadystatechange = function() {
        if (http.readyState == 4) {
            result = http.responseText;
            $("#month" + suffix).html(result);
            $(".monthTR").css({ opacity: 0, visibility: 'visible' }).animate({ opacity: 100 }, 500);
            if (suffix != 'PooledPortfolio') {
                $("#headPooledPortfolio").css('visibility', 'hidden');
                $("#monthPooledPortfolio").css('visibility', 'hidden');
                $("#btnTRPooledPortfolio").html('');
            }
            return false;
        }
    };
    http.send(null);
}

function showButton(suffix, fundID) {
    var ddlM = document.getElementById('ddlfundMonth' + suffix);
    if (ddlM.selectedIndex == 0) return;
    if (isNaN(fundID) || fundID == undefined || fundID == null) {
        var ddl = document.getElementById('ddlfundlist' + suffix);
        if (ddl.selectedIndex == 0) return;
        fundID = ddl[ddl.selectedIndex].value;
    }
    var ddlY = document.getElementById('ddlfundYear' + suffix);
    if (ddlY.selectedIndex == 0) return;


    var url = '/AJAX/getFundByDetails.aspx?month=' + ddlM[ddlM.selectedIndex].value + '&id=' + fundID + '&year=' + ddlY[ddlY.selectedIndex].value;
    http.open("GET", url, true);
    http.onreadystatechange = function() {
        if (http.readyState == 4) {
            result = http.responseText;
            //$("#aDownload").attr("href", result);
            $("#btnTR" + suffix).html('<a id="aDownload" target="_blank" href="' + result + '" onclick="hideDownloads();"><img src="/images/FundFactSheets/btn_GetStatement.gif" /></a>').fadeIn(500);
            return false;
        }
    };
    http.send(null);

}
function hideDownloads() {
    $(".monthTR").css({ visibility: 'hidden' });
    $("#tblDates").css({ display: 'none' });
    $("#btnTR").css({ display: 'none' });

    var ddl = document.getElementById('ddlfundlist');
    ddl.selectedIndex = 0;
}

function showPortfolio(id) {
    if ($("#" + id).is(":visible")) { $("#" + id).slideUp(200); return; }
    $(".portfolios:visible").slideUp(200);
    $("#" + id).slideDown(200);
}

function trackPDF(pdfname) {
    try {
        _gaq.push(['_trackPageview', pdfname]);
        //ebConversionTracker(pdfname);
        trackDownloads(pdfname);
        window.open(pdfname);
    }
    catch (er) { alert(er); }

}

function ebConversionTracker(pdfname) {
    var ebConversionImg = new Image();
    var ebConversionURL = "HTTP://bs.serving-sys.com/BurstingPipe/ActivityServer.bs?cn=as&ActivityID=71810&Value=" + pdfname + "&ns=1";
    ebConversionImg.src = ebConversionURL;
}

/*Downloads*/
function trackDownloads(pdf) {    
    var axel = Math.random() + "";
    var a = axel * 10000000000000;
    var dlImage = new Image();
    dlImage.src = 'http://ad.doubleclick.net/activity;src=2835527;type=downl376;cat=downl891;u1=' + unescape(pdf) + ';ord=' + a + '?';        
}

function trackInvestNow() {
    var axel = Math.random()+"";
    var a = axel * 10000000000000;
    var dlImage = new Image();
    var src = 'http://ad.doubleclick.net/activity;src=2835527;type=inves656;cat=inves698;ord='+ a + '?';
    dlImage.src = src;
}

function trackPage(type, cat) {
    var axel = Math.random() + "";
    var a = axel * 10000000000000;
    var dlImage = new Image();
    dlImage.src = 'http: //fls.doubleclick.net/activityi;src=2835527;type=' + type + ';cat=' + cat + ';ord=' + a + '?';
}

function GetByFriendlyUrl(url, callback) {
    $.ajax({
        url: '/AJAX/GetByFriendlyUrl.aspx?url=' + url,
        success: function(result) {
            callback(result);
        }
    });
}
function makeTooltip(url, idToEdit, attributeToChange, toolTipId) {
    GetByFriendlyUrl(url, function(result) {
        $("#" + idToEdit).attr(attributeToChange, result);
        $("#" + toolTipId).tipTip({ maxWidth: 'auto', defaultPosition: 'right', edgeOffset: 0, delay: 100 });
    });       
}

//-->
