
var FILE_VERSION = '0000.0013.0001';


String.prototype.trim = function()
{
    return this.replace
    (
        /(^\s*)|(\s*$)/g,
        ''
    );
}


var COOKIE_DOMAIN = null;
var DIALOG_URL = '\\Colgate\\US\\OC\\Common\\Includes\\webiq\\question.html';
var COOKIE_ERROR_MODIFIER =                             0x1000;
var ERROR_BASE =                                        0x80040000;
var ERROR_CAME_FROM_GOOGLE_ADWORDS_LINK =               ERROR_BASE + 1;
var ERROR_CANNOT_CREATE_COOKIE =                        ERROR_BASE + 2;
var ERROR_COOKIE_COULD_NOT_WRITE_COOKIE =               ERROR_BASE + COOKIE_ERROR_MODIFIER + 1;
var ERROR_COOKIE_NO_COOKIE_PRESENT =                    ERROR_BASE + COOKIE_ERROR_MODIFIER + 3;
var ERROR_COOKIE_SPECIFIED_COOKIE_DOES_NOT_EXIST =      ERROR_BASE + COOKIE_ERROR_MODIFIER + 2;
var ERROR_COULD_NOT_LOAD_WEB_BEACON =                   ERROR_BASE + 14;
var ERROR_FAILED_QUALIFICATION =                        ERROR_BASE + 15;
var ERROR_INACTIVE_PROJECT =                            ERROR_BASE + 3;
var ERROR_INVALID_BROWSER =                             ERROR_BASE + 4;
var ERROR_INVALID_DIRECTORY =                           ERROR_BASE + 5;
var ERROR_INVALID_LAUNCH_DEFINITION_INDEX =             ERROR_BASE + 6;
var ERROR_INVALID_PARAMETER =                           ERROR_BASE + 7;
var ERROR_INVALID_USER_RESPONSE =                       ERROR_BASE + 8;
var ERROR_INVALID_WEBIQ_DIALOG =                        ERROR_BASE + 9;
var ERROR_OK =                                          0;
var ERROR_OPERATION_TIMEOUT_EXCEEDED =                  ERROR_BASE + 13;
var ERROR_USER_HAS_ALREADY_BEEN_INVITED =               ERROR_BASE + 10;
var ERROR_USER_NOT_SELECTED =                           ERROR_BASE + 11;
var ERROR_WEBIQ_SERVER_IS_INACCESSIBLE =                ERROR_BASE + 12;
var GOOGLE_ADWORDS_SEARCH_EXPRESSION = 'referrer(%3D|\=)Google';
var INVITATION_DISPLAY_SESSION_COOKIE_NAME = 'InvitationDisplayed';
var INVITATION_DISPLAY_SESSION_COOKIE_VALUE = 'true';
var INVITATION_EVENT_BEGINNING =                        1;
var INVITATION_EVENT_CANCELING_INVITATION =             2;
var INVITATION_EVENT_FINISHED_RENDERING_DIALOG =        3;
var INVITATION_EVENT_QUALIFICATION_FAILED =             4;
var INVITATION_EVENT_QUALIFICATION_PASSED =             5;
var INVITATION_EVENT_USER_RESPONSE_ACCEPTED =           6;
var INVITATION_EVENT_USER_RESPONSE_DECLINED =           7;
var PROJECT_STATE = 'In Progress';
var SIZE_MODIFIER = 10;
var TEST_COOKIE_NAME = 'TestCookieName';
var TEST_COOKIE_VALUE = 'TestCookieValue';
var USER_RESPONSE_COOKIE_LIFESPAN = 90;
var USER_RESPONSE_COOKIE_NAME = 'UserResponse';
var WEB_BEACON_BASE_URL = 'http:\u002f\u002fWebIQ003.WebIQOnline.com\u002fLegacyWebIQInvitationCounter\u002fWebBeacon.aspx';
var WEB_BEACON_IMAGE_LOADING_TIMEOUT = 500;
var WEB_BEACON_MAXIMUM_IMAGE_ID = 4;
var WEBIQ_DIALOG_BORDER_STYLE = '1px solid black';
var WEBIQ_DIALOG_VERTICAL_OFFSET = 80;
var WEBIQ_IFRAME_SOURCE = '<iframe border="0" frameBorder="0" id="oWebIQInvitationDialog" scrolling="no" style="position:absolute; width:0; height:0; visibility:hidden; z-index:1000;" src="' + DIALOG_URL + '"></iframe>';
var WEBIQ_LAUNCH_URL = 'http:\u002f\u002fwww.webiqonline.com\u002fwebiq\u002fwebiqlaunch.dll?MfcISAPICommand=ShowInvitation&LDI={0}&SV=1';


var g_bIsWebIQServerAvailable = true;
var g_bRecordInvitationEvents = false;
var g_bUseIFrame = true;
var g_dInvitationInitiationTime = new Date();
var g_nDialogHeight = 0;
var g_nDialogWidth = 0;
var g_nLaunchDefinitionIndex = 0;
var g_nSelectionProbability = 0;
var g_nWebBeaconImageID = 4;


function UserSettings(oWindow)
{
    this.BrowserLanguage = '';
    this.InternetExplorerVersion = '';
    this.MaskingAsIE = false;
    this.IsAOL = false;
    this.ScreenHeight = 0;
    this.ScreenWidth = 0;
    this.SystemLanguage = '';
    this.WindowsVersion = '';
    var sUserAgent = null;
    var sWindowsVersion = null;
    var vMatches = null;
    try
    {
        if (isValidObject(oWindow))
        {
            if (!isNaN(oWindow.screen.width))
            {
                this.ScreenWidth = oWindow.screen.width;
            }
            if (!isNaN(oWindow.screen.height))
            {
                this.ScreenHeight = oWindow.screen.height;
            }
            sUserAgent = oWindow.navigator.userAgent;
            if (isValidString(sUserAgent))
            {
                vMatches = sUserAgent.match(/MSIE ([\.\w]+);?/i);
                if
                (
                    (vMatches != null)
                    &&
                    (vMatches.length > 0)
                )
                {
                    this.InternetExplorerVersion = vMatches[1];
                    vMatches = sUserAgent.match(/Opera/i);
                    if (vMatches != null && vMatches.length > 0)
                    {
                        this.MaskingAsIE = true;
                    }
                }
                vMatches = sUserAgent.match(/AOL/i);
                if
                (
                    (vMatches != null)
                    &&
                    (vMatches.length > 0)
                )
                {
                    this.IsAOL = true;
                }
                vMatches = sUserAgent.match(/(Windows|Win) ?([\.\w ]+);?/i);
                if
                (
                    (vMatches != null)
                    &&
                    (vMatches.length > 0)
                )
                {
                    sWindowsVersion = vMatches[2];
                    sWindowsVersion = sWindowsVersion.replace
                    (
                        /NT 5.0/i,
                        '2000'
                    );
                    sWindowsVersion = sWindowsVersion.replace
                    (
                        /NT 5.1/i,
                        'XP'
                    );
                    sWindowsVersion = sWindowsVersion.replace
                    (
                        /NT 5.2/i,
                        'Server 2003'
                    );
                    sWindowsVersion = sWindowsVersion.replace
                    (
                        /NT 6.0/i,
                        'Longhorn'
                    );
                    sWindowsVersion = sWindowsVersion.replace
                    (
                        /32/i,
                        '95'
                    );
                    if
                    (
                        (sWindowsVersion == 'XP')
                        &&
                        (sUserAgent.indexOf('SV1') > -1)
                    )
                    {
                        sWindowsVersion += 'SP2';
                    }
                    this.WindowsVersion = sWindowsVersion;
                }
            }
            if (isValidString(oWindow.navigator.browserLanguage))
            {
                this.BrowserLanguage = oWindow.navigator.browserLanguage;
            }
            if (isValidString(oWindow.navigator.systemLanguage))
            {
                this.SystemLanguage = oWindow.navigator.systemLanguage;
            }
        }
    }
    catch (oException)
    {
        alert(oException.message);
    }
    return;
}


function findElement(sElementID)
{
    var oElement = null;
    try
    {
        if (!isValidString(sElementID))
        {
            throw new Error
            (
                ERROR_INVALID_PARAMETER,
                'The element ID input parameter is invalid.'
            );
        }
        oElement = document.getElementById(sElementID);
        if (!isValidObject(oElement))
        {
            if (window.frames.length > 0)
            {
                for (var nFrameCounter = 0; nFrameCounter < window.frames.length; nFrameCounter++)
                {
                    oElement = window.frames[nFrameCounter].document.getElementById(sElementID);
                    if (isValidObject(oElement))
                    {
                        break;
                    }
                }
            }
            else
            {
                oElement = window.top.document.getElementById(sElementID);
            }
        }
    }
    catch (oException)
    {
        oElement = null;
    }
    return oElement;
}
function isValidObject(oVariable)
{
    var bIsValidObject = false;
    try
    {
        if
        (
            (oVariable != null)
            &&
            (typeof(oVariable) == 'object')
        )
        {
            bIsValidObject = true;
        }
    }
    catch (oException)
    {
    }
    return bIsValidObject;
}
function isValidString(sVariable)
{
    var bIsValidString = false;
    try
    {
        if
        (
            (sVariable != null)
            &&
            (typeof(sVariable) == 'string')
            &&
            (sVariable.trim().length > 0)
        )
        {
            bIsValidString = true;
        }
    }
    catch (oException)
    {
    }
    return bIsValidString;
}
function loadImage
(
    sURL,
    nTimeoutValue
)
{
    var bExceededTimeout = false;
    var oImage = null;
    var dStartTime = new Date();
    try
    {
        if (!isValidString(sURL))
        {
            throw new Error
            (
                ERROR_INVALID_PARAMETER,
                'The URL input parameter is invalid.'
            );
        }
        if
        (
            (isNaN(nTimeoutValue))
            ||
            (nTimeoutValue < 1)
        )
        {
            throw new Error
            (
                ERROR_INVALID_PARAMETER,
                'The timeout input parameter is invalid.'
            );
        }
        oImage = new Image();
        oImage.src = sURL;
        sleep(nTimeoutValue / 5);
        oImage.src = sURL;
        while (oImage.readyState != 'complete')
        {
            if (new Date() >= new Date(dStartTime.valueOf() + nTimeoutValue))
            {
                bExceededTimeout = true;
                break;
            }
            sleep
            (
                Math.ceil
                (
                    nTimeoutValue / 10
                )
            );
        }
        if
        (
            (bExceededTimeout)
            ||
            (oImage.readyState != 'complete')
        )
        {
            throw new Error
            (
                ERROR_OPERATION_TIMEOUT_EXCEEDED,
                'The loading of the image timed out.'
            );
        }
    }
    catch (oException)
    {
        oImage = null;
    }
    return oImage;
}
function sleep
(
    nDelay
)
{
    var dStartTime = new Date();
    while (new Date() <= new Date(dStartTime.valueOf() + nDelay))
    {
    }
    return;
}


function canCreateAndAccessCookies()
{
    var bCanCreateAndAccessCookies = false;
    var bResult = false;
    try
    {
        bResult = writeCookie
        (
            TEST_COOKIE_NAME,
            TEST_COOKIE_VALUE,
            0,
            '/',
            COOKIE_DOMAIN
        );
        if (bResult)
        {
            if (getCookieValue(TEST_COOKIE_NAME) == TEST_COOKIE_VALUE)
            {
                bCanCreateAndAccessCookies = true;
            }
        }
    }
    catch (oException)
    {
        bCanCreateAndAccessCookies = false;
    }
    return bCanCreateAndAccessCookies;
}
function cookieExists(sName)
{
    var bCookieExists = false;
    try
    {
        if (!isValidString(sName))
        {
            throw new Error
            (
                ERROR_INVALID_PARAMETER,
                'The name input parameter is invalid.'
            );
        }
        if (getCookieValue(sName) != null)
        {
            bCookieExists = true;
        }
    }
    catch (oException)
    {
        bCookieExists = false;
    }
    return bCookieExists;
}
function deleteCookie(sName)
{
    var bSuccessful = false;
    var dExpirationDate = new Date();
    try
    {
        if (!isValidString(sName))
        {
            throw new Error
            (
                ERROR_INVALID_PARAMETER,
                'The name input parameter is invalid.'
            );
        }
        if (!cookieExists(sName))
        {
            throw new Error
            (
                ERROR_COOKIE_SPECIFIED_COOKIE_DOES_NOT_EXIST,
                'A cookie with the given name could not be found.'
            );
        }
        dExpirationDate.setTime(dExpirationDate.getTime() - 1);
        document.cookie = sName +
            '=; expires=' +
            dExpirationDate.toGMTString();
        bSuccessful = true;
    }
    catch (oException)
    {
        bSuccessful = false;
    }
    return bSuccessful;
}
function getCookieValue(sName)
{
    var sCookie = '';
    var sCookieValue = null;
    var oRegularExpression = null;
    var sPattern = '';
    var vMatches = null;
    try
    {
        if (!isValidString(sName))
        {
            throw new Error
            (
                ERROR_INVALID_PARAMETER,
                'The name input parameter is invalid.'
            );
        }
        sPattern = '(' +
            sName.trim() +
            '=)([^;]*);?';
        oRegularExpression = new RegExp
        (
            sPattern,
            'ig'
        );
        sCookie = document.cookie;
        if (!isValidString(sCookie))
        {
            throw new Error
            (
                ERROR_COOKIE_NO_COOKIE_PRESENT,
                'The document.cookie property does not contain any data.'
            );
        }
        vMatches = sCookie.match(oRegularExpression);
        if (vMatches != null)
        {
            sCookieValue = unescape(RegExp.$2);
        }
        else
        {
            throw new Error
            (
                ERROR_COOKIE_SPECIFIED_COOKIE_DOES_NOT_EXIST,
                'A cookie with the given name could not be found.'
            );
        }
    }
    catch (oException)
    {
        sCookieValue = null;
    }
    return sCookieValue;
}
function writeCookie
(
    sName,
    sValue,
    nLifeSpan,
    sPath,
    sDomain,
    bSecure
)
{
    var bSuccessful = false;
    var dExpirationDate = new Date();
    var sCookieData = '';
    var sDomainSection = '';
    var sExpirationSection = '';
    var sPathSection = '';
    var sSecureSection = '';
    try
    {
        if (!isValidString(sName))
        {
            throw new Error
            (
                ERROR_INVALID_PARAMETER,
                'The name input parameter is invalid.'
            );
        }
        if (nLifeSpan > 0)
        {
            dExpirationDate.setHours(dExpirationDate.getHours() + nLifeSpan);
            sExpirationSection = '; expires=' + dExpirationDate.toGMTString();
        }
        if (isValidString(sDomain))
        {
            sDomainSection = '; domain=' + sDomain.trim();
        }
        if (isValidString(sPath))
        {
            sPathSection = '; path=' + sPath.trim();
        }
        if (bSecure)
        {
            sSecureSection = '; secure';
        }
        sCookieData += sName.trim() +
            '=' +
            escape(sValue.trim()) +
            sExpirationSection +
            sPathSection +
            sDomainSection +
            sSecureSection;
        document.cookie = sCookieData;
        if (getCookieValue(sName) != sValue)
        {
            throw new Error
            (
                ERROR_COOKIE_COULD_NOT_WRITE_COOKIE,
                'An error occurred while trying to write the given cookie.'
            );
        }
        bSuccessful = true;
    }
    catch (oException)
    {
        bSuccessful = false;
    }
    return bSuccessful;
}


function cameFromGoogleAdWords(sSearchExpression)
{
    var bCameFromGoogleAdWord = false;
    var oRegularExpression = null;
    var sQueryString = '';
    try
    {
        if (!isValidString(sSearchExpression))
        {
            throw new Error
            (
                ERROR_INVALID_PARAMETER,
                'The search expression input parameter is invalid.'
            );
        }
        sQueryString = window.document.location.search;
        if (isValidString(sQueryString))
        {
            oRegularExpression = new RegExp
            (
                sSearchExpression,
                'i'
            );
            if (sQueryString.match(oRegularExpression) != null)
            {
                bCameFromGoogleAdWord = true;
            }
        }
    }
    catch (oException)
    {
        bCameFromGoogleAdWord = false;
    }
    return bCameFromGoogleAdWord;
}
function closeWebIQInvitationDialog()
{
    var oWebIQInvitationDialog = getWebIQInvitationDialog();
    if (isValidObject(oWebIQInvitationDialog))
    {
        oWebIQInvitationDialog.style.display = 'none';
    }
    return;
}
function createWebIQInvitationDialog()
{
    var oWebIQInvitationDialog = null;
    try
    {
        oWebIQInvitationDialog = getWebIQInvitationDialog();
        if (!isValidObject(oWebIQInvitationDialog))
        {
            throw new Error
            (
                ERROR_INVALID_WEBIQ_DIALOG,
                'The reference to the WebIQ Invitation Dialog is invalid.'
            );
        }
        g_nDialogHeight += SIZE_MODIFIER;
        oWebIQInvitationDialog.style.border = WEBIQ_DIALOG_BORDER_STYLE;
        oWebIQInvitationDialog.style.width = g_nDialogWidth;
        oWebIQInvitationDialog.style.height = g_nDialogHeight;
        oWebIQInvitationDialog.style.left = ((document.body.clientWidth - g_nDialogWidth) / 2);
        oWebIQInvitationDialog.style.top = document.body.scrollTop + WEBIQ_DIALOG_VERTICAL_OFFSET;
        oWebIQInvitationDialog.style.margin = '0px';
        oWebIQInvitationDialog.style.padding = '0px';
        oWebIQInvitationDialog.style.overflow = 'hidden';
        oWebIQInvitationDialog.style.overflowX = 'hidden';
        oWebIQInvitationDialog.style.overflowY = 'hidden';
        if (g_bUseIFrame)
        {
            oWebIQInvitationDialog.src = DIALOG_URL +
                '?LDI=' + g_nLaunchDefinitionIndex.toString() +
                '&IIT=' + g_dInvitationInitiationTime.valueOf().toString();
        }
        else
        {
            populateWebIQInvitationDialogContents
            (
                oWebIQInvitationDialog,
                g_nLaunchDefinitionIndex
            );
        }
        oWebIQInvitationDialog.style.visibility = 'visible';
        if (g_bRecordInvitationEvents)
        {
            recordInvitationEvent
            (
                g_nLaunchDefinitionIndex,
                INVITATION_EVENT_FINISHED_RENDERING_DIALOG,
                null
            );
        }
    }
    catch (oException)
    {
        closeWebIQInvitationDialog();
    }
    return;
}
function getWebIQInvitationDialog()
{
    var oWebIQInvitationDialog = null;
    try
    {
        oWebIQInvitationDialog = findElement('oWebIQInvitationDialog');
        if
        (
            (oWebIQInvitationDialog.tagName.toLowerCase() != 'iframe')
            &&
            (oWebIQInvitationDialog.tagName.toLowerCase() != 'div')
        )
        {
            throw new Error
            (
                ERROR_INVALID_WEBIQ_DIALOG,
                'The reference to the WebIQ Invitation Dialog is invalid.'
            );
        }
    }
    catch (oException)
    {
        oWebIQInvitationDialog = null;
    }
    return oWebIQInvitationDialog;
}
function inviteUserToBeginWebIQ
(
    nSelectionProbability,
    nLaunchDefinitionIndex,
    nDialogWidth,
    nDialogHeight
)
{
    var bSuccess = false;
    var oWebIQInvitationDialog = null;
    try
    {
        g_nSelectionProbability = nSelectionProbability;
        g_nLaunchDefinitionIndex = nLaunchDefinitionIndex;
        g_nDialogWidth = nDialogWidth;
        g_nDialogHeight = nDialogHeight;
        if (getCookieValue(INVITATION_DISPLAY_SESSION_COOKIE_NAME + g_nLaunchDefinitionIndex.toString()) != null)
        {
            throw new Error
            (
                ERROR_USER_HAS_ALREADY_BEEN_INVITED,
                'The user has already been invited to participate in the WebIQ study.'
            );
        }
        bResult = writeCookie
        (
            INVITATION_DISPLAY_SESSION_COOKIE_NAME + g_nLaunchDefinitionIndex.toString(),
            INVITATION_DISPLAY_SESSION_COOKIE_VALUE,
            null,
            '/',
            COOKIE_DOMAIN
        );
        if (g_bUseIFrame)
        {
            oWebIQInvitationDialog = document.createElement(WEBIQ_IFRAME_SOURCE);
            document.body.insertBefore(oWebIQInvitationDialog);
        }
        if (g_bRecordInvitationEvents)
        {
            recordInvitationEvent
            (
                g_nLaunchDefinitionIndex,
                INVITATION_EVENT_BEGINNING,
                qualifyUser
            );
        }
        else
        {
            qualifyUser();
            createWebIQInvitationDialog();
        }
    }
    catch (oException)
    {
        closeWebIQInvitationDialog();
        bSuccess = false;
    }
    return bSuccess;
}
function isActiveProject()
{
    var bIsActive = false;
    try
    {
        if (PROJECT_STATE.toLowerCase() == 'in progress')
        {
            bIsActive = true;
        }
    }
    catch (oException)
    {
        bIsActive = false;
    }
    return bIsActive;
}
function isSelected(nSelectionProbability)
{
    var bIsSelected = false;
    try
    {
        if
        (
            (
                Math.floor
                (
                    Math.random() * nSelectionProbability
                )
            ) == 0
        )
        {
            bIsSelected = true;
        }
    }
    catch (oException)
    {
        bIsSelected = false;
    }
    return bIsSelected;
}
function meetsWebIQSystemRequirements()
{
    var bMeetsWebIQSystemRequirements = false;
    var oUserSettings = null;
    try
    {
        oUserSettings = new UserSettings(window);
        if
        (
            (
                (oUserSettings.WindowsVersion == '95')
                ||
                (oUserSettings.WindowsVersion == '98')
                ||
                (oUserSettings.WindowsVersion.toLowerCase() == 'me')
                ||
                (oUserSettings.WindowsVersion == '2000')
                ||
                (oUserSettings.WindowsVersion.toLowerCase() == 'xp')
                ||
                (oUserSettings.WindowsVersion.toLowerCase() == 'xpsp2')
            )
            &&
            (parseFloat(oUserSettings.InternetExplorerVersion) > 5.1)
            &&
            !oUserSettings.MaskingAsIE
        )
        {
            bMeetsWebIQSystemRequirements = true;
        }
    }
    catch (oException)
    {
        bMeetsWebIQSystemRequirements = false;
    }
    return bMeetsWebIQSystemRequirements;
}
function openSupportWindow
(
    sProjectName
)
{
    window.open
    (
        'http:\u002f\u002fwww.webiqonline.com\u002fsupport.asp?pid=' + sProjectName,
        '_blank',
        'top=100, left=100, height=480, width=500, status=yes, toolbar=no, menubar=no, location=no',
        true
    );
    return;
}
function processUserResponse
(
    sUserResponse
)
{
    var bResult = false;
    var nLaunchDefinitionIndex = 0;
    var sLaunchURL = null;
    var sQueryString = '';
    try
    {
        sQueryString = location.search;
        nLaunchDefinitionIndex = parseInt(sQueryString.match(/LDI=(\d+)/i)[1]);
        if (sQueryString.match(/IIT=(\d+)/i) != null)
        {
            g_dInvitationInitiationTime = new Date(parseInt(sQueryString.match(/IIT=(\d+)/i)[1]));
        }
        if
        (
            (isNaN(nLaunchDefinitionIndex))
            ||
            (nLaunchDefinitionIndex < 1)
        )
        {
            throw new Error
            (
                ERROR_INVALID_LAUNCH_DEFINITION_INDEX,
                'The launch definition index is invalid.'
            );
        }
        if (!isValidString(sUserResponse))
        {
            throw new Error
            (
                ERROR_INVALID_USER_RESPONSE,
                'The user\'s response is invalid.'
            );
        }
        if (!g_bIsWebIQServerAvailable)
        {
            throw new Error
            (
                ERROR_WEBIQ_SERVER_IS_INACCESSIBLE,
                'The WebIQ server is inaccessible.'
            );
        }
        bResult = writeCookie
        (
            USER_RESPONSE_COOKIE_NAME,
            sUserResponse,
            (USER_RESPONSE_COOKIE_LIFESPAN * 24),
            '/',
            COOKIE_DOMAIN
        );
        closeWebIQInvitationDialog();
        if (sUserResponse.toLowerCase() == 'accept')
        {
            if (g_bRecordInvitationEvents)
            {
                recordInvitationEvent
                (
                    nLaunchDefinitionIndex,
                    INVITATION_EVENT_USER_RESPONSE_ACCEPTED,
                    closeWebIQInvitationDialog
                );
            }
            sLaunchURL = WEBIQ_LAUNCH_URL.replace
            (
                /\{0\}/,
                nLaunchDefinitionIndex.toString()
            );
            window.top.location = sLaunchURL;
        }
        else
        {
            if (g_bRecordInvitationEvents)
            {
                recordInvitationEvent
                (
                    nLaunchDefinitionIndex,
                    INVITATION_EVENT_USER_RESPONSE_DECLINED,
                    closeWebIQInvitationDialog
                );
            }
        }
    }
    catch (oException)
    {
        closeWebIQInvitationDialog();
    }
    return;
}
function qualifyUser()
{
    try
    {
        if (cameFromGoogleAdWords(GOOGLE_ADWORDS_SEARCH_EXPRESSION))
        {
            throw new Error
            (
                ERROR_CAME_FROM_GOOGLE_ADWORDS_LINK,
                'The user appears to have reached the site via a Google AdWords link.'
            );
        }
        if (!isActiveProject())
        {
            throw new Error
            (
                ERROR_INACTIVE_PROJECT,
                'The WebIQ study is no longer accepting new sessions.'
            );
        }
        if (!isSelected(g_nSelectionProbability))
        {
            throw new Error
            (
                ERROR_USER_NOT_SELECTED,
                'The user was not randomly selected to participate in the WebIQ study.'
            );
        }
        if (!meetsWebIQSystemRequirements())
        {
            throw new Error
            (
                ERROR_INVALID_BROWSER,
                'The user\'s browser does not support the WebIQ client.'
            );
        }
        if (!canCreateAndAccessCookies())
        {
            throw new Error
            (
                ERROR_CANNOT_CREATE_COOKIE,
                'An error occurred in writing a cookie for the user\'s browser.'
            );
        }
        if (getCookieValue(USER_RESPONSE_COOKIE_NAME) != null)
        {
            throw new Error
            (
                ERROR_USER_HAS_ALREADY_BEEN_INVITED,
                'The user has already been invited to participate in the WebIQ study.'
            );
        }
        if (g_bRecordInvitationEvents)
        {
            recordInvitationEvent
            (
                g_nLaunchDefinitionIndex,
                INVITATION_EVENT_QUALIFICATION_PASSED,
                createWebIQInvitationDialog
            );
        }
    }
    catch (oException)
    {
        if (g_bRecordInvitationEvents)
        {
            recordInvitationEvent
            (
                g_nLaunchDefinitionIndex,
                INVITATION_EVENT_QUALIFICATION_FAILED,
                closeWebIQInvitationDialog
            );
        }
        else
        {
            throw new Error
            (
                ERROR_FAILED_QUALIFICATION,
                'The user failed the qualification to participate in the WebIQ study.'
            );
        }
    }
    return;
}
function recordInvitationEvent
(
    nLDI,
    nEventID,
    fnCallback
)
{
    var oWebBeacon = null;
    var nRandomNumber = -1;
    var sImageElementSource = '';
    var sURL = '';
    if (g_bRecordInvitationEvents)
    {
        try
        {
            sSource = '<img id="imgWebBeacon' + nEventID.toString() + '" style="visibility: hidden;" />';
            oWebBeacon = document.createElement(sSource);
            document.body.insertBefore(oWebBeacon);
            nRandomNumber = Math.ceil(Math.random() * 1000000) + 1;
            sURL = WEB_BEACON_BASE_URL +
                '?RN=' +
                nRandomNumber.toString() +
                '&LDI=' +
                nLDI.toString() +
                '&EI=' +
                nEventID.toString() +
                '&MS=' +
                g_dInvitationInitiationTime.valueOf().toString() +
                '&II=' +
                g_nWebBeaconImageID.toString();
            oWebBeacon.onload = fnCallback;
            oWebBeacon.src = sURL;
        }
        catch (oException)
        {
            g_bRecordInvitationEvents = false;
        }
    }
    return;
}
function updateServerAvailabilityFlag()
{
    g_bIsWebIQServerAvailable = false;
    return;
}
