﻿var gWndProtHandler;
var gCloseWaitTime;
var gCookieName;
var gDetectedUrl;

// This function supports the default button functionality
// for wizard controls. It must be bound to the html body's
// onload event handler in the code-behind file.
function CaptureKeyPress()
{
    document.onkeypress = EvaluateDefaultButton;
}

// This function evaluates the key code (e.g. [Enter]), determines
// if it is appropriate to call the default button and checks to
// see if there is a default button.
function EvaluateDefaultButton()
{
    if (window.event.keyCode == 13)
    {
        if (!CanReturn(window.event.srcElement))
        {
            defButton = document.getElementById(document.forms[0].defaultbutton);
            if (defButton != null && defButton.name != null && defButton.name.length > 0)
            {
                ExecuteDefaultButton(defButton.name);
            }
        }
    }
}

// This functin deterines if it is approprate to invoke the
// default button when the user presses [Enter]. Things to
// consider are whether the control that has focus is allowed
// to have it's own submit-type functionality. As an example,
// if an anchor control has focus, [Enter] should take the
// user to the anchor's href, not invoke the default button.
// The TextArea control is also a special case because the
// [Enter] key is a valid character in the control (i.e. it
// puts the cursor on a new line.
function CanReturn(element)
{
    if (element != null && element.tagName != null)
    {
        if (element.disabled) return false;

        if (element.type != null && element.type.toLowerCase() != "hidden")
        {
            var elementType = element.type.toLowerCase();
            var elementTag = element.tagName.toLowerCase();
            if (elementTag == "textarea" || elementTag == "button" || elementTag == "a" || elementTag == "select")
            {
                return true;
            }
            else if (elementTag == "input" &&
                    (elementType == "button" || elementType == "image" || elementType == "reset" || elementType == "submit"))
            {
                return true;
            }
        }
    }
    return false;
}

function VerifyCookiesEnabled()
{
    var oExpires = new Date();

    oExpires.setMonth(oExpires.getMonth() - 1);
    SetCookie("_DetectCookies", "Y", oExpires, "/");

    oExpires = new Date();
    oExpires.setMonth(oExpires.getMonth() + 1);
    SetCookie("_DetectCookies", "Y", oExpires, "/");

    var cookieValue = GetCookieValue("_DetectCookies");
    var warning = document.getElementById('divNoCookies');

    if (cookieValue != null)
    {
        //warning.style.display = "none";
    }
    else
    {
        warning.style.display = "block";
        warning.style.visibility = "visible";
    }
}

// Set the client UTC offset cookie.
// Value is in minutes.
function SetClientUTCOffset()
{
    var oExpires = new Date();
    oExpires.setMonth(oExpires.getMonth() + 1);

    var localTime = new Date();
    var offset = -1 * localTime.getTimezoneOffset();

    SetCookie("smsClientUTCOffset", offset, oExpires, "/");
}

function SetCookie(cookieName, cookieValue, oCookieExpires, cookiePath)
{
    cookieValue = escape(cookieValue);

    var cookieExpires = (oCookieExpires == null) ? "; " : "; expires=" + oCookieExpires.toUTCString();
    
    if (cookiePath != null && cookiePath.length > 0)
    {
        cookiePath = "; path=" + cookiePath;
    }
    
    document.cookie = cookieName + "=" + cookieValue + cookieExpires + cookiePath;
}

function GetCookieValue(cookieName)
{
    var cookieValue = document.cookie;
    var cookieStartsAt = cookieValue.indexOf(" " + cookieName + "=");
    if (cookieStartsAt == -1)
    {
        cookieStartsAt = cookieValue.indexOf(cookieName + "=");
    }
    if (cookieStartsAt == -1)
    {
        cookieValue = null;
    }
    else
    {
        cookieStartsAt = cookieValue.indexOf("=", cookieStartsAt) + 1;
        var cookieEndsAt = cookieValue.indexOf(";", cookieStartsAt);
        if (cookieEndsAt == -1)
        {
            cookieEndsAt = cookieValue.length;
        }
        cookieValue = unescape(cookieValue.substring(cookieStartsAt, cookieEndsAt));
    }
    return cookieValue;
}

function DetectOutlook(cookieName, detectedUrl, timeout)
{
    gCookieName = cookieName;
    gDetectedUrl = detectedUrl;
    
    setTimeout(ProcessDetectionResult, timeout);
}

function ProcessDetectionResult()
{
    var sOutlookVersion = GetCookieValue(gCookieName);
    if (sOutlookVersion == "12")
    {
        document.location.href = gDetectedUrl;
    }
    else
    {
        document.forms[0].submit();
    }
    return;
}

function ExecProtHandler(protHandlerPopupUrl, detectWaitTime, closeWaitTime)
{
    gCloseWaitTime = closeWaitTime;

    var warning = document.getElementById("divProtHandlerWarning");
    warning.style.display = "none";
    warning.style.visibility = "hidden";

    gWndProtHandler = window.open(protHandlerPopupUrl, "ProtHandler", "height=150,width=300");

    try
    {
        gWndProtHandler.focus();
        setTimeout(VerifyProtHandler, detectWaitTime);
    }
    catch (oError)
    {
        DisplayProtHandlerWarning();
    }
}

function VerifyProtHandler()
{
    try
    {
        var protHandlerForm = gWndProtHandler.document.getElementById("frmProtHandler");
        if (!protHandlerForm)
        {
            DisplayProtHandlerWarning();
        }
        else
        {
            setTimeout(CloseProtHandler, gCloseWaitTime);
        }
    }
    catch(oError)
    {
        DisplayProtHandlerWarning();
    }
}

function DisplayProtHandlerWarning()
{
    gWndProtHandler = window.open("", "ProtHandler");
    gWndProtHandler.close();

    var warning = document.getElementById("divProtHandlerWarning");
    warning.style.display = "block";
    warning.style.visibility = "visible";
}

function CloseProtHandler()
{
    try
    {
        if (typeof(gWndProtHandler) != "undefined" && !gWndProtHandler.closed)
        {
            gWndProtHandler.close();
        }
    }
    catch (oError)
    {}
}

function LaunchDemo(demoUrl)
{
    window.open(demoUrl, "SMSLinkDemo", "width=740,height=540,resizable=yes");
}

function LargerImage(imageUrl, popupName, attributes)
{
    popupWindow = window.open(imageUrl, popupName, attributes);
    popupWindow.focus();
}
