
function toggleEnglish(forceSet) {
    $('.englishField').each(function () {
        if (forceSet == "true") {
            $(this).show();
        }
        else if (forceSet == "false") {
            $(this).hide();
        }
        else {
            $(this).toggle();
        }
    });
}

function toggleFrench(forceSet) {
    $('.frenchField').each(function () {
        if (forceSet == "true") {
            $(this).show();
        }
        else if (forceSet == "false") {
            $(this).hide();
        }
        else {
            $(this).toggle();
        }
    });
}

function addOption() {
    $('.pnlOptionsEng').each(function () {
        count = 0;
        $('.englishOption').each(function () {
            count++;
            uniqueID++;
        });
        // BWC put "name" in variables?
        if (count < 5) {
            $(this).append('<div id="pollDiv_' + uniqueID + '"><input type="text" name="newPollOption_' + uniqueID + '_en-ca" class="optionTextBox englishOption"/> <input type="button" value="Delete" class="deleteNewOption" id="newPollOptionDeleteButton_' + uniqueID + '_en-ca"/></div>');
            $('#newPollOptionDeleteButton_' + uniqueID + '_en-ca').click(function () {
                optionCount = 0;
                $('.englishOption').each(function () {
                    optionCount++;
                });

                if (optionCount > 2) {
                    var parentSelector = "#" + $(this).parent().attr("id");
                    $(parentSelector).remove();
                    $(parentSelector).remove();
                }
                else {
                    alert("A poll must have at least 2 options");
                }
            });
        }
        else {
            alert("A poll may only have 5 options.");
        }
    });
    $('.pnlOptionsFr').each(function () {
        count = 0;
        $('.frenchOption').each(function () {
            count++;
        });
        // BWC put "name" in variables?
        if (count < 5) {
            $(this).append('<div id="pollDiv_' + uniqueID + '"><input type="text" name="newPollOption_' + uniqueID + '_fr-ca" class="optionTextBox frenchOption"/> <input type="button" value="Delete" class="deleteNewOption" id="newPollOptionDeleteButton_' + uniqueID + '_fr-ca"/></div>');
            $('#newPollOptionDeleteButton_' + uniqueID + '_fr-ca').click(function () {
                optionCount = 0;
                $('.frenchOption').each(function () {
                    optionCount++;
                });

                if (optionCount > 2) {
                    var parentSelector = "#" + $(this).parent().attr("id");
                    $(parentSelector).remove();
                    $(parentSelector).remove();
                }
                else {
                    alert("A poll must have at least 2 options");
                }
            });
        }
    });
}

function hideShareButtons() {
    $('.sharebuttons').hide();
}

function hideSearchBox() {
    $('#searchBox').hide();
}


function UpdatePageTitle(title) {
    document.title = title;
}

function SetExternalLinks() {
    $('a').click(function () {
        if (this.hostname && this.hostname !== location.hostname && !this.href.match('^javascript:*?')) {
            if (this.target == '' || this.target == undefined) $(this).attr({ 'target': '_blank' });
        }
    });
}

function InitAccordion() {
    try {
        $('.accordion h3').click(function () {
            $(this).next().toggle();
            $(this).children('a').toggleClass('expanded');
            return false;
        }).next().hide();
    } catch (err) { }
}

var isFrenchSite = true;
$(document).ready(function () {
    isFrenchSite = document.location.href.toLowerCase().indexOf('/sites/fr-ca/') > -1;
    //setSiteLanguageToggle();
    //hideSearchBox();
    initSearchFunction();
    SetExternalLinks();
    setActiveLink();
    InitAccordion();
});

function alertNoOption() {
    alert("You have not selected a poll option.");
}

function setActiveLink() {
    try {
        var strArray = document.location.pathname.split('/');

        var folderPath = '';
        if (isFrenchSite && strArray.length > 5) {
            folderPath = strArray[4];
        } else if (strArray.length > 3) {
            folderPath = strArray[2];
        }

        $('#asideNav ul li a').each(function () {
            if (this.href.toLowerCase() == document.location.href.toLowerCase() && ((this.parentElement != undefined && this.parentElement.tagName == 'LI') || (this.parentNode != undefined && this.parentNode.nodeName == 'LI'))) {
                $(this).addClass('active');
            }
        });

        if (folderPath != '') {
            folderPath = folderPath.replace(/-/gi, '');
            $('#topnav' + folderPath.toLowerCase()).addClass('active');
        }
    } catch (err) { alert(err); }
}

var searchText = 'Search';
function initSearchFunction() {
    if (isFrenchSite)
        searchText = 'Rechercher';

    $('#searchInput').val(searchText);

    $('#searchButton').click(function () {
        return SearchFunction();
    });

    $('#searchInput').blur(function () {
        if ($('#searchInput').val() == '') {
            $('#searchInput').val(searchText);
        }
    });

    $('#searchInput').focus(function () {
        if ($('#searchInput').val().toLowerCase() == searchText.toLowerCase()) {
            $('#searchInput').val('');
        }
    });

    $("#searchInput").keydown(function (event) {
        if (event.keyCode == 13) {
            SearchFunction();
            return false;
        }
    });
}

function SearchFunction() {
    
    if ($('#searchInput').val() == '' || $('#searchInput').val().toLowerCase() == searchText.toLowerCase())
        return false;

    if (isFrenchSite) {
        document.location = '/sites/fr-ca/search/results.aspx?k=' + encodeURIComponent($('#searchInput').val()) + '&r=site%3D%22http%3A%2F%2Fwww%2Eethicalfunds%2Ecom%2Fsites%2Ffr%2Dca%22';
    } else {
        document.location = '/search/results.aspx?k=' + encodeURIComponent($('#searchInput').val()) + '&r=site%3D%22http%3A%2F%2Fwww%2Eethicalfunds%2Ecom%22';
    }
    return true;
}

function RegisterLanguageToggle(url) {
    $('#languageToggle').click(function () {
        this.target = '_self';
        this.href = url;
    });
}

function setSiteLanguageToggle() {
    try {
        var langPath = document.location.pathname;
        if (isFrenchSite) {
            langPath = 'http://www.ethicalfunds.com' + document.location.pathname.replace(/\/sites\/fr-ca/i, "") + document.location.search;
        } else {
            langPath = 'http://www.fondsethiques.com/sites/fr-ca' + langPath + document.location.search;
        }

        $('#languageToggle').click(function () {
            this.target = '_self';
            this.href = langPath;
        });
    } catch (err) { }
}
