
var FILE_VERSION = '0.9.8';


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_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_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 PROJECT_STATE = 'In Progress';
var SIZE_MODIFIER = 10;
var TEST_COOKIE_NAME = 'TestCookieName';
var TEST_COOKIE_VALUE = 'TestCookieValue';
var USER_RESPONSE_COOKIE_LIFESPAN = 3;
var USER_RESPONSE_COOKIE_NAME = 'UserResponse';
var WEBIQ_DIALOG_BORDER_STYLE = '2px 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:\/\/www.webiqonline.com\/webiq\/webiqlaunch.dll?MfcISAPICommand=ShowInvitation&LDI={0}&SV=1';
var bTURNOFF = false;


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 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.display = 'none';
    }
    return;
}
function createWebIQInvitationDialog
(
    nLaunchDefinitionIndex,
    nWidth,
    nHeight,
    sCountryCode,
    sLanguageCode
    
)
{
    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';


//HERE IS WHERE I will lookup the language and country code to determine the correct question.html and the correct LDI.

     switch (sCountryCode.toUpperCase())
     {
        //  If a web page in the domain is being navigated away from...
        case "GLOBAL":
        	nLaunchDefinitionIndex = 394;
			DIALOG_URL = '\\webiq\\question.html';
		break;
        case "US":
        	nLaunchDefinitionIndex = 396;
			DIALOG_URL = '\\webiq\\question_us.html';
	
		break;		
        case "AU":
        	nLaunchDefinitionIndex = 398;
			DIALOG_URL = '\\webiq\\question_au.html';
	
		break;		
        case "NZ":
        	nLaunchDefinitionIndex = 400;
			DIALOG_URL = '\\webiq\\question_nz.html';
	
		break;		
        case "UK":
        	nLaunchDefinitionIndex = 402;
			DIALOG_URL = '\\webiq\\question_uk.html';
	
		break;						
        case "CN":
        	if(sLanguageCode.toUpperCase() == 'EN')
        	{
        		nLaunchDefinitionIndex = 404;
				DIALOG_URL = '\\webiq\\question_cn_en.html';
			}
        	if(sLanguageCode.toUpperCase() == 'ZH')
        	{
        		nLaunchDefinitionIndex = 406;
				DIALOG_URL = '\\webiq\\question_cn_zh.html';
				oWebIQInvitationDialog.style.height = nHeight + 70;
				oWebIQInvitationDialog.style.top = document.body.scrollTop + WEBIQ_DIALOG_VERTICAL_OFFSET - 50;
				
			}	
		break;						
        case "MX":
        	if(sLanguageCode.toUpperCase() == 'EN')
        	{
        		nLaunchDefinitionIndex = 408;
				DIALOG_URL = '\\webiq\\question_mx_en.html';
			}
        	if(sLanguageCode.toUpperCase() == 'ES')
        	{
        		nLaunchDefinitionIndex = 410;
				DIALOG_URL = '\\webiq\\question_mx_es.html';
			}		
		break;						
        case "RU":
        	if(sLanguageCode.toUpperCase() == 'EN')
        	{
        		nLaunchDefinitionIndex = 412;
				DIALOG_URL = '\\webiq\\question_ru_en.html';
			}
        	if(sLanguageCode.toUpperCase() == 'RU')
        	{
        		nLaunchDefinitionIndex = 414;
				DIALOG_URL = '\\webiq\\question_ru_ru.html';
				oWebIQInvitationDialog.style.height = nHeight + 100;
				oWebIQInvitationDialog.style.top = document.body.scrollTop + WEBIQ_DIALOG_VERTICAL_OFFSET - 50;
			}
        	if(sLanguageCode.toUpperCase() == 'FR')
        	{
        		nLaunchDefinitionIndex = 416;
				DIALOG_URL = '\\webiq\\question_ru_fr.html';
			}	
		break;						
        case "TW":
        	if(sLanguageCode.toUpperCase() == 'EN')
        	{
        		nLaunchDefinitionIndex = 418;
				DIALOG_URL = '\\webiq\\question_tw_en.html';
			}
        	if(sLanguageCode.toUpperCase() == 'ZH')
        	{
        		nLaunchDefinitionIndex = 420;
				DIALOG_URL = '\\webiq\\question_tw_zh.html';
				oWebIQInvitationDialog.style.height = nHeight + 70;
				oWebIQInvitationDialog.style.top = document.body.scrollTop + WEBIQ_DIALOG_VERTICAL_OFFSET - 50;
				
			}
	
		break;						
        case "CA":
        	if(sLanguageCode.toUpperCase() == 'EN')
        	{
        		nLaunchDefinitionIndex = 422;
				DIALOG_URL = '\\webiq\\question_ca_en.html';
			}
        	if(sLanguageCode.toUpperCase() == 'FR')
        	{
        		nLaunchDefinitionIndex = 424;
				DIALOG_URL = '\\webiq\\question_ca_fr.html';
			}
	
		break;		
		default:
			oWebIQInvitationDialog = null;
			 return oWebIQInvitationDialog;
		break;		
	}												


        if (bUseIFrame)
        {
            oWebIQInvitationDialog.src = DIALOG_URL + '?ldi=' + nLaunchDefinitionIndex.toString();
        }
        else
        {
            populateWebIQInvitationDialogContents
            (
                oWebIQInvitationDialog,
                nLaunchDefinitionIndex
            );
        }
        oWebIQInvitationDialog.style.visibility = 'visible';
    }
    catch (oException)
    {
        oWebIQInvitationDialog = null;
    }
    return oWebIQInvitationDialog;
}
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 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,
    sCountryCode,
    sLanguageCode    
)
{
	
	
	     switch (sCountryCode.toUpperCase())
     {
        //  If a web page in the domain is being navigated away from...
        case "GLOBAL":
        	nLaunchDefinitionIndex = 394;
			DIALOG_URL = '\\webiq\\question.html';
		break;
        case "US":
        	nLaunchDefinitionIndex = 396;
			DIALOG_URL = '\\webiq\\question_us.html';
	
		break;		
        case "AU":
        	nLaunchDefinitionIndex = 398;
			DIALOG_URL = '\\webiq\\question_au.html';
	
		break;		
        case "NZ":
        	nLaunchDefinitionIndex = 400;
			DIALOG_URL = '\\webiq\\question_nz.html';
	
		break;		
        case "UK":
        	nLaunchDefinitionIndex = 402;
			DIALOG_URL = '\\webiq\\question_uk.html';
	
		break;						
        case "CN":
        	if(sLanguageCode.toUpperCase() == 'EN')
        	{
        		nLaunchDefinitionIndex = 404;
				DIALOG_URL = '\\webiq\\question_cn_en.html';
			}
        	if(sLanguageCode.toUpperCase() == 'ZH')
        	{
        		nLaunchDefinitionIndex = 406;
				DIALOG_URL = '\\webiq\\question_cn_zh.html';
			}	
		break;						
        case "MX":
        	if(sLanguageCode.toUpperCase() == 'EN')
        	{
        		nLaunchDefinitionIndex = 408;
				DIALOG_URL = '\\webiq\\question_mx_en.html';
			}
        	if(sLanguageCode.toUpperCase() == 'ES')
        	{
        		nLaunchDefinitionIndex = 410;
				DIALOG_URL = '\\webiq\\question_mx_es.html';
			}		
		break;						
        case "RU":
        	if(sLanguageCode.toUpperCase() == 'EN')
        	{
        		nLaunchDefinitionIndex = 412;
				DIALOG_URL = '\\webiq\\question_ru_en.html';
			}
        	if(sLanguageCode.toUpperCase() == 'RU')
        	{
        		nLaunchDefinitionIndex = 414;
				DIALOG_URL = '\\webiq\\question_ru_ru.html';
			}
        	if(sLanguageCode.toUpperCase() == 'FR')
        	{
        		nLaunchDefinitionIndex = 416;
				DIALOG_URL = '\\webiq\\question_ru_fr.html';
			}	
		break;						
        case "TW":
        	if(sLanguageCode.toUpperCase() == 'EN')
        	{
        		nLaunchDefinitionIndex = 418;
				DIALOG_URL = '\\webiq\\question_tw_en.html';
			}
        	if(sLanguageCode.toUpperCase() == 'ZH')
        	{
        		nLaunchDefinitionIndex = 420;
				DIALOG_URL = '\\webiq\\question_tw_zh.html';
			}
	
		break;						
        case "CA":
        	if(sLanguageCode.toUpperCase() == 'EN')
        	{
        		nLaunchDefinitionIndex = 422;
				DIALOG_URL = '\\webiq\\question_ca_en.html';
			}
        	if(sLanguageCode.toUpperCase() == 'FR')
        	{
        		nLaunchDefinitionIndex = 424;
				DIALOG_URL = '\\webiq\\question_ca_fr.html';
			}
	
		break;		
		default:
	 		bSuccess = false;
	 		return bSuccess;
		
		break;
	}					
	
	
	
    var bSuccess = false;
    try
    {
    	
    	
        if (getCookieValue(INVITATION_DISPLAY_SESSION_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 (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.'
            );
        }

        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))
        {
        	
        bResult = writeCookie
        (
            INVITATION_DISPLAY_SESSION_COOKIE_NAME + nLaunchDefinitionIndex.toString(),
            INVITATION_DISPLAY_SESSION_COOKIE_VALUE,
            null,
            '/',
            COOKIE_DOMAIN
        );
            throw new Error
            (
                ERROR_USER_NOT_SELECTED,
                'The user was not randomly selected to participate in the WebIQ study.'
            );
        }
        bResult = writeCookie
        (
            INVITATION_DISPLAY_SESSION_COOKIE_NAME,
            INVITATION_DISPLAY_SESSION_COOKIE_VALUE,
            null,
            '/',
            COOKIE_DOMAIN
        );        
        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,
            sCountryCode,
            sLanguageCode
        );
    }
    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)
            &&
            !oUserSettings.MaskingAsIE
        )
        {
            bMeetsWebIQSystemRequirements = true;
        }
    }
    catch (oException)
    {
        bMeetsWebIQSystemRequirements = false;
    }
    return bMeetsWebIQSystemRequirements;
}
function openSupportWindow
(
    sProjectName
)
{
    window.open
    (
        'http:\/\/www.webiqonline.com\/support.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
        (
            (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')
        {
            sLaunchURL = WEBIQ_LAUNCH_URL.replace
            (
                /\{0\}/,
                nLaunchDefinitionIndex.toString()
            );
            window.top.location = sLaunchURL;
        }
    }
    catch (oException)
    {
        closeWebIQInvitationDialog();
    }
    return;
}
function updateServerAvailabilityFlag()
{
    g_bIsWebIQServerAvailable = false;
    return;
}


