﻿//global.js - javscript helper functions
function WriteBackHyperLink(label, postbacks)
{
    document.write("<div class=\"pageBack\">");
    document.write("<A href=\"#\" onclick=\"javascript:history.go(-" + postbacks + ");\" onkeypress=\"javascript:history.go(-" + postbacks + ");\">");
    document.write("<div class=\"pageBackLinkAuxItem\">");
    document.write("</div>");
    document.write("<div class=\"pageBackLinkText\">");
    document.write(label);
    document.write("</div>");
    document.write("</A>");
    document.write("</div>");
}

function WritePrintHyperlink(label)
{
    document.write("<div class=\"printLink\">");
    document.write("<a href=\"#\" onclick=\"javascript:window.print();return false;\" onkeypress=\"javascript:window.print();return false;\">");
    document.write("<div class=\"printLinkAuxItem\">");
    document.write("</div>");
    document.write("<div class=\"printLinkText\">");
    document.write(label);
    document.write("</div>");
    document.write("</a>");
    document.write("</div>");
}

function SetAsDefaultHomePage(label)
{
    document.write("<a href=\"#\" onclick=\"this.style.behavior='url(#default#homepage)';this.setHomePage(location.href);\" onkeypress=\"this.style.behavior='url(#default#homepage)';this.setHomePage(location.href);\">");
    document.write("<span class=\"setHomePageLinkAuxItem\"></span>");
    document.write("<span class=\"setHomePageLinkText\">");
    document.write(label);
    document.write("</span></a>");
            
}

function AddToFavorites(label)
{
    document.write("<a href=\"#\" onclick=\"javascript:window.external.addFavorite(location.href,document.title);\" onkeypress=\"javascript:window.external.addFavorite(location.href,document.title);\">");
    document.write("<span class=\"addToFavoritesLinkAuxItem\"></span>");
    document.write("<span class=\"addToFavoritesLinkText\">");
    document.write(label);
    document.write("</span></a>");
}

function ClearTextBox(textBoxID, defaultValue) {
    for (i = 0; i < document.forms[0].length; i++) {
        if (document.forms[0].elements[i].name.indexOf(textBoxID) != -1) {
            if (document.forms[0].elements[i].value == defaultValue)
                document.forms[0].elements[i].value = "";
        }
    }
}

//checkes if the menu has a second level or a survey. If it hasn't it hides the second level zone so that the main
//content fits the whole page
//it also sets the survey position, if it exists, according to the second level menu number of items
function CheckMenuAndSurvey() {
    if ($('.menuItemActive > div').size() == 0 && $('.quiz > div').size() == 0) {
        SetContentWholePage();
    }
    //set quiz position in the page according to second level menu number of items (in homepage this is not necessary)
    //it is considered to be homepage if homePageNewsContainer element is not present
    if ($('.quiz > div').size() > 0 && $('.homePageNewsContainer > div').size() == 0)
    {
        //find the number of second level menu items despite of its level in hierarchy
        var menuItemsCount = $('.menuItemActive').find('.secondLevel').find('.menuItemActive').find('li').size() + 1;
        //alert(menuItemsCount);
        //initial position when page is accessed by an anonymous user
        var initialQuizTopPosition = 170;
        //initial position when page is accessed by an authenticated user
        if ($('.ms-cui-ribbonTopBars > div').size() > 0)
            initialQuizTopPosition = 235;
        //if there are no second level items, set the survey position a little bit higher in the page
        if ($('.menuItemActive > div').size() == 0)
            initialQuizTopPosition = initialQuizTopPosition - 20;
        //calculate the quiz top position according to the number of second level menu items
        var quizTopPosition = initialQuizTopPosition + (menuItemsCount * 20);
        //alert(initialQuizTopPosition);
        //set the quiz top position to the previously calculated value
        $('.quiz').css('top', quizTopPosition + 'px');
    }
}

//check if content image exists, if yes, removes it from page html
function CheckIfImageExists() {
    if ($('.contentImage').find('img').size() == 0) {
        $('.contentImage').remove();
    }
}

//sets the main content to fit the whole page
function SetContentWholePage() {
    $('.content').css('width', '745px');
}
