

var FILE_VERSION = '0.9.5';

String.prototype.trim = function()
{
    return this.replace
    (
        /(^\s*)|(\s*$)/g,
        ''
    );
}


var COOKIE_DOMAIN = null;
var DIALOG_URL = '.\\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_INACTIVE_PROJECT =                            ERROR_BASE + 3;
var ERROR_INVALID_BROWSER =                             ERROR_BASE + 4;
var ERROR_INVALID_DIRECTORY =                           ERROR_BASE + 5;
var ERROR_INVALID_PARAMETER =                           ERROR_BASE + 6;
var ERROR_INVALID_USER_RESPONSE =                       ERROR_BASE + 7;
var ERROR_INVALID_WEBIQ_DIALOG =                        ERROR_BASE + 8;
var ERROR_OK =                                          0;
var ERROR_USER_HAS_ALREADY_BEEN_INVITED =               ERROR_BASE + 9;
var ERROR_USER_NOT_SELECTED =                           ERROR_BASE + 10;
var ERROR_WEBIQ_SERVER_IS_INACCESSIBLE =                ERROR_BASE + 11;
var GOOGLE_ADWORDS_SEARCH_EXPRESSION = 'referrer(%3D|\=)Google';
var INVITATION_DISPLAY_SESSION_COOKIE_NAME = 'InvitationDisplayed';
var INVITATION_DISPLAY_SESSION_COOKIE_VALUE = 'true';
var PROJECT_STATE = 'In Progress';
var SIZE_MODIFIER = 10;
var TEST_COOKIE_NAME = 'TestCookieName';
var TEST_COOKIE_VALUE = 'TestCookieValue';
var USER_RESPONSE_COOKIE_LIFESPAN = 30;
var USER_RESPONSE_COOKIE_NAME = 'UserResponse';
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=\"\"></iframe>';
var WEBIQ_LAUNCH_URL = 'http:\/\/www.webiqonline.com/webiq/webiqlaunch.dll?MfcISAPICommand=ShowInvitation&LDI={0}&SV=1';


function UserSettings(oWindow)
{
    this.BrowserLanguage = '';
    this.InternetExplorerVersion = '';
    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(/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 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 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;
}


var g_bIsWebIQServerAvailable = true;


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 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 closeWebIQInvitationDialog()
{
    var oWebIQInvitationDialog = getWebIQInvitationDialog();
    if (isValidObject(oWebIQInvitationDialog))
    {
        oWebIQInvitationDialog.style.visibility = 'hidden';
    }
    return;
}
function createWebIQInvitationDialog
(
    nLaunchDefinitionIndex,
    nWidth,
    nHeight
)
{
    var bUseIFrame = false;
    var oWebIQInvitationDialog = null;
    try
    {
        if
        (
            (getWebIQInvitationDialog() == null)
            ||
            (getWebIQInvitationDialog().tagName.toLowerCase() != 'div')
        )
        {
            document.write(WEBIQ_IFRAME_SOURCE);
            bUseIFrame = true;
        }
        oWebIQInvitationDialog = getWebIQInvitationDialog();
        if (!isValidObject(oWebIQInvitationDialog))
        {
            throw new Error
            (
                ERROR_INVALID_WEBIQ_DIALOG,
                'The reference to the WebIQ Invitation Dialog is invalid.'
            );
        }
        nHeight += SIZE_MODIFIER;
        oWebIQInvitationDialog.style.border = WEBIQ_DIALOG_BORDER_STYLE;
        oWebIQInvitationDialog.style.width = nWidth;
        oWebIQInvitationDialog.style.height = nHeight;
        oWebIQInvitationDialog.style.left = ((document.body.clientWidth - nWidth) / 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 (bUseIFrame)
        {
            oWebIQInvitationDialog.src = DIALOG_URL + '?ldi=' + nLaunchDefinitionIndex.toString();
        }
        else
        {
            populateWebIQInvitationDialogContents
            (
                oWebIQInvitationDialog,
                nLaunchDefinitionIndex
            );
        }
        oWebIQInvitationDialog.style.visibility = 'visible';
    }
    catch (oException)
    {
        oWebIQInvitationDialog = null;
    }
    return oWebIQInvitationDialog;
}
function getWebIQInvitationDialog()
{
    var oWebIQInvitationDialog = null;
    try
    {
        oWebIQInvitationDialog = window.top.document.getElementById('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;
    try
    {
        if (getCookieValue(INVITATION_DISPLAY_SESSION_COOKIE_NAME + 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 + nLaunchDefinitionIndex.toString(),
            INVITATION_DISPLAY_SESSION_COOKIE_VALUE,
            null,
            '/',
            COOKIE_DOMAIN
        );
        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(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.'
            );
        }
        createWebIQInvitationDialog
        (
            nLaunchDefinitionIndex,
            nDialogWidth,
            nDialogHeight
        );
    }
    catch (oException)
    {
        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)
        )
        {
            bMeetsWebIQSystemRequirements = true;
        }
    }
    catch (oException)
    {
        bMeetsWebIQSystemRequirements = false;
    }
    return bMeetsWebIQSystemRequirements;
}
function processUserResponse
(
    nLaunchDefinitionIndex,
    sUserResponse
)
{
    var bResult = false;
    var sLaunchURL = null;
    try
    {
        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')
        {
            sLaunchURL = WEBIQ_LAUNCH_URL.replace
            (
                /\{0\}/,
                nLaunchDefinitionIndex.toString()
            );
            window.top.location = sLaunchURL;
        }
    }
    catch (oException)
    {
        closeWebIQInvitationDialog();
    }
    return;
}
function updateServerAvailabilityFlag()
{
    g_bIsWebIQServerAvailable = false;
    return;
}
