﻿/// <reference path="jquery-1.6.1-vsdoc.js" />
var SessionTimeLeft = -99;
var SessionWarningTimeLeft;
var SessionInterval;
var SessionWarningTimeInterval;

$(document).ready(function () {

    //----------------------------------------------------------------------------
    // Set session timer if logged in
    //----------------------------------------------------------------------------
    if ($(".WelcomeMessageContainer span").text() != "") {
        SessionTimeLeft = 15;
        SessionWarningTimeLeft = 60;
        SessionInterval = setInterval('SessionTimeoutWarning()', 60000);
    }

    //----------------------------------------------------------------------------
    // Home Page Mouseover buttons
    //----------------------------------------------------------------------------
    $("[id$=imgLogin],[id$=LoginButton],[id$=imgCreateAccount],[id$=btnExportToPDF]").hover(function () {
        this.src = this.src.replace("_Off", "_On");
    },
        function () {
            this.src = this.src.replace("_On", "_Off");
        });


    //----------------------------------------------------------------------------
    // Set Active Master Page Navigation Style
    //----------------------------------------------------------------------------
    var url = window.location.pathname;

    if (url.indexOf("Admin") > -1) {
        $("[id$=lnkAdmin]").removeClass().addClass("ActiveTopLink").addClass("ActiveAdminLinkArrow");
        $("[id$=lnkAdmin]").removeAttr("href");
    }
    if (url.indexOf("AccountInformation") > -1) {
        $("[id$=lnkAccountInfo]").removeClass().addClass("ActiveTopLink").addClass("ActiveAccountLinkArrow");
        $("[id$=lnkAccountInfo]").removeAttr("href");
    }
    if (url.indexOf("Support") > -1) {
        $("[id$=lnkHelp]").removeClass().addClass("ActiveTopLink").addClass("ActiveHelpLink");
        $("[id$=lnkHelp]").removeAttr("href");
    }

    if (url.indexOf("GettingStarted") > -1) {
        $("[id$=lnkGettingStarted]").removeClass().addClass("ActiveLeftLink");
        $("[id$=lnkGettingStarted]").removeAttr("href");
    }
    if (url.indexOf("ReportSelection") > -1) {
        $("[id$=lnkViewReports]").removeClass().addClass("ActiveLeftLink");
        $("[id$=lnkViewReports]").removeAttr("href");
    }
    if (url.indexOf("Dashboard") > -1) {
        $("[id$=lnkDashboard]").removeClass().addClass("ActiveLeftLink");
        $("[id$=lnkDashboard]").removeAttr("href");
    }
    if (url.indexOf("EnterData") > -1) {
        $("[id$=lnkEnterData]").removeClass().addClass("ActiveLeftLink");
        $("[id$=lnkEnterData]").removeAttr("href");
    }

});

//------------------------------------------------------------------------
// Warn of Session Timeout
//------------------------------------------------------------------------
function SessionTimeoutWarning() {
    if (SessionTimeLeft != -99) {
        SessionTimeLeft--;
        if (SessionTimeLeft <= 1 && SessionTimeLeft > 0) {
            $get("SessionTimeoutModalBackground").style.display = "block";
            $get("SessionTimeoutModal").style.display = "block";
            $("#SessionTimeRemaining").text("Time Remaining: " + SessionWarningTimeLeft + " seconds");
            SessionWarningTimeInterval = setInterval('SessionTimeRemaining()', 1000);
        }
        else if (SessionTimeLeft <= 0) {
            var url = window.location.href;
            if (url.indexOf("TestSite") >= 0)
                location.href = "http://" + location.host + "/TestSite/Login.aspx?Timeout=true";
            else
                location.href = "http://" + location.host + "/Login.aspx?Timeout=true";
        }
    }
}
function SessionTimeRemaining() {
    SessionWarningTimeLeft--;
    $("#SessionTimeRemaining").text("Time Remaining: " + SessionWarningTimeLeft + " seconds");
}
function ResetSessionTimer() {
    $get("SessionTimeoutModalBackground").style.display = "none";
    $get("SessionTimeoutModal").style.display = "none";
    clearInterval(SessionInterval);
    clearInterval(SessionWarningTimeInterval);
    SessionTimeLeft = 15;
    SessionWarningTimeLeft = 60;
    SessionInterval = setInterval('SessionTimeoutWarning()', 60000);
}

//-------------------------------------------------------------------------------
// This function disables a button after it is clicked to avoid double postback.
// Validators will be checked before disabling the button. A validationGroup of
// "NONE" means do not perform any validation before disabling the button.
//-------------------------------------------------------------------------------
function DisableButton(btn, text, validationGroup) 
{
    if (validationGroup == 'NONE') 
    {
        setTimeout("$get('" + btn + "').disabled = true;", 10);
        setTimeout("$get('" + btn + "').value = '" + text + "'", 10);
    }
    else {
        if (validationGroup == '') 
        {
            if (Page_ClientValidate()) 
            {
                setTimeout("$get('" + btn + "').disabled = true;", 10);
                setTimeout("$get('" + btn + "').value = '" + text + "'", 10);
            }
        }
        else 
        {
            if (Page_ClientValidate(validationGroup)) 
            {
                setTimeout("$get('" + btn + "').disabled = true;", 10);
                setTimeout("$get('" + btn + "').value = '" + text + "'", 10);
            }
        }
    }
}

//----------------------------------------------------------------------------
// Enables a disabled button
//----------------------------------------------------------------------------
function EnableButton(button, text, time) {
    setTimeout(function () { EnableButton2(button, text) }, time);
}
function EnableButton2(button, text) {
    var btn = $get(button);
    btn.disabled = false;
    btn.value = text;
}
    
//----------------------------------------------------------------------------
// Show & limit character count for TextArea
//----------------------------------------------------------------------------
function CountChars(TextArea, MaxChars) {
    var charCount = $get("charCount");
    var text = $get(TextArea);

    if (document.all)
        charCount.innerText = text.value.length;  // IE
    else
        charCount.textContent = text.value.length;  // Firefox

    if (text.value.length > MaxChars)
        text.value = text.value.substr(0, MaxChars);
}


//----------------------------------------------------------------------------
// Load Mouseover Images
//----------------------------------------------------------------------------
if (document.images) {
    var baseURL = location.href;
    var rootURL = baseURL.substring(0, baseURL.indexOf('/', 8))

    imgTabFinance = new Image();
    imgTabFinance.src = rootURL + "/Images/TabFinance.gif";
    imgTabFinanceover = new Image();
    imgTabFinanceover.src = rootURL + "/Images/TabFinanceOver.gif";

    imgTabMarketing = new Image();
    imgTabMarketing.src = rootURL + "/Images/TabMarketing.gif";
    imgTabMarketingover = new Image();
    imgTabMarketingover.src = rootURL + "/Images/TabMarketingOver.gif";
    
    imgTabHumanResources = new Image();
    imgTabHumanResources.src = rootURL + "/Images/TabHumanResources.gif";
    imgTabHumanResourcesover = new Image();
    imgTabHumanResourcesover.src = rootURL + "/Images/TabHumanResourcesOver.gif";
    
    imgTabPlanning = new Image();
    imgTabPlanning.src = rootURL + "/Images/TabPlanning.gif";
    imgTabPlanningover = new Image();
    imgTabPlanningover.src = rootURL + "/Images/TabPlanningOver.gif";
    
    imgTabSpecialTopics = new Image();
    imgTabSpecialTopics.src = rootURL + "/Images/TabSpecialTopics.gif";
    imgTabSpecialTopicsover = new Image();
    imgTabSpecialTopicsover.src = rootURL + "/Images/TabSpecialTopicsOver.gif";
}

//----------------------------------------------------------------------------
// Show Mouseover Image
//----------------------------------------------------------------------------
function mouseover(imgName) {    
    if (document.images) {
        if (imgName.indexOf("imgEntryIcon") != -1)
            imgOn = eval("imgEntryIconover.src");
        else if (imgName.indexOf("imgPDFIcon") != -1)
            imgOn = eval("imgPDFIconover.src");
        else
            imgOn = eval(imgName + "over.src");
        
        document[imgName].src = imgOn;
    }
}

//----------------------------------------------------------------------------
// Show Mouseout Image
//----------------------------------------------------------------------------
function mouseout(imgName) {
    if (document.images) {
        if (imgName.indexOf("imgEntryIcon") != -1)
            imgOff = eval("imgEntryIcon.src");
        else if (imgName.indexOf("imgPDFIcon") != -1)
            imgOff = eval("imgPDFIcon.src");
        else
            imgOff = eval(imgName + ".src");
        
        document[imgName].src = imgOff;
    }
}

//----------------------------------------------------------------------------
// This function determines if any data in the input fields has changed since
// the data has been saved.
//----------------------------------------------------------------------------
function DataHasChanged() {
    var form = document.getElementById("aspnetForm");
    
    for (var i = 0; i < form.elements.length; i++) {
        var element = form.elements[i];
        var type = element.type;

        if (type == "checkbox" || type == "radio") {            
            if (element.checked != element.defaultChecked)
                return true;
        }
        else if (type == "text" || type == "textarea") {
        //alert("Type: " + type + " Value: " + element.value + "  -  Default:" + element.defaultValue);
            if (element.value != element.defaultValue)
                return true;
        }
        else if (type == "select-one" || type == "select-multiple") {
            for (var j = 0; j < element.options.length; j++) {
                if (element.options[j].selected != element.options[j].defaultSelected)
                    return true;
            }
        }
    }
    return false;
}

//----------------------------------------------------------------------------
// Opens a window centered on the screen
//----------------------------------------------------------------------------
function OpenWindowCentered(page, width, height, features)
{
    if(screen.width)
    {
        var winLeft = (screen.width-width)/2;
        var winTop = (screen.height-height)/2;
    }
    else    
    {
        winLeft = 0;
        winTop = 0;
    }
    
    if (winLeft < 0) winLeft = 0;
    if (winTop < 0) winTop = 0;
    
    var settings = 'height=' + height + ',';
        settings += 'width=' + width + ',';
        settings += 'top=' + winTop + ',';
        settings += 'left=' + winLeft + ',';
        settings += features;

    win = window.open(page,"",settings);
    win.window.focus();
}

//---------------------------------------------------------------------------------------
// AccountInformation.aspx
// If Legal Group is "Other", show text box to entry.  Otherwise, hide the textbox
//---------------------------------------------------------------------------------------
function CheckForOtherLegalStructure(ddLegalStructure, txtLegalStructure, rfvLegalStructure) {
    var dropdownLegal = document.getElementById(ddLegalStructure);
    var textboxLegal = document.getElementById(txtLegalStructure);
    var validatorLegal = document.getElementById(rfvLegalStructure);

    
    var index = dropdownLegal.selectedIndex;
    if (dropdownLegal.options[index].text == "Other") {
        textboxLegal.style.visibility = 'visible';
        ValidatorEnable(validatorLegal, true);
    }
    else {
        textboxLegal.value = "";
        textboxLegal.style.visibility = 'hidden';
        ValidatorEnable(validatorLegal, false);
    }
}

//---------------------------------------------------------------------------------------
// AccountInformation.aspx
//---------------------------------------------------------------------------------------
function GroupMemberSelected(radGroup, ddGroup, rfvGroup) {
    var radioInputGroup = document.getElementById(radGroup).getElementsByTagName("input");
    var dropdownGroup = document.getElementById(ddGroup);
    var validatorGroup = document.getElementById(rfvGroup);

    //------------------------------------------------------------
    // User is in group.  Enable group dropdown and validator.
    //------------------------------------------------------------
    if (radioInputGroup[0].checked) {
        dropdownGroup.disabled = false;
        ValidatorEnable(validatorGroup, true);
    }

    //------------------------------------------------------------
    // User not in group.  Disabled group dropdown and validator.
    //------------------------------------------------------------
    if (radioInputGroup[1].checked) {
        dropdownGroup.disabled = true;
        ValidatorEnable(validatorGroup, false);
        dropdownGroup.selectedIndex = 0;
    }
}

//----------------------------------------------------------------------------
// This function is called when the user attempts to leave this page.  If there
// is any unsaved data on the page, the user is warned before navigating away.
//----------------------------------------------------------------------------
function CheckForUnsavedData() {
    var UnsavedData = DataHasChanged();

    if (SkipUnsavedDataCheck == false && UnsavedData)
        return "There is unsaved data on this page.";
}

//-----------------------------------------------------------------------------------
// Returns the client ID for the passed in Server Control ID
// This replaces having to use <% %> in javascript functions.
//-----------------------------------------------------------------------------------
function GetClientID(ServerControlID) {
    var re = new RegExp(ServerControlID, 'g');
    var elems = document.getElementsByTagName('*');

    for (i = 0; i < elems.length; i++) {
        if (elems[i] != null && elems[i].id.match(re))
            return elems[i].id;
    }

    return "";
}
