 
// Name:  SIMS.js

/* 
    PageOnloadEnvironTestingPagesOnly()
    String.prototype.trim    ' A special way to go Trim ()s... 
    setCookie()
    getCookie()
    getCookieSubkey() 
    addSubkeyToCookie()
    parseCookieSubkeys()
    HideElement()
    startTimeCountDown()   // 09/09/2009  Thinking about  a) Time Delay  &  b) Time Intensity  support for clients 
*/

/// <reference path="jquery-1.3.2-vsdoc2.js" />
/// <reference path="jquery-1.3.2.js" />


// Get screen resolution.
function getScreenResolution() {

    addSubkeyToCookie("Screen", "Width", screen.availWidth);
    addSubkeyToCookie("Screen", "Height", screen.availHeight);
    var scrInfo;
    scrInfo = "ScreenWidthPx=" + screen.width + "&ScreenHeightPx=" + screen.height + "&ScreenAvailWidthPx=" + screen.availWidth + "&ScreenAvailHeightPx=" + screen.availHeight + "&ScreenColorDepthPx=" + screen.colorDepth;
    hSetCookie("Environment", scrInfo);
    scrInfo = getCookie("Environment");
}

function hSetCookie(name, value, expires) {
    var cookie_string = name + "=" + value;
    if (expires) {
        expires = new Date(expires);
        cookie_string += ";expires=" + expires.toGMTString();
    }
    document.cookie = cookie_string + ";path=/";
} 


// Break Out of Frames:  Source: http://www.htmlcenter.com/tutorials/tutorials.cfm/81/Javascript/
function breakOutOfFrames() {

    if (top.location != self.location) { top.location = self.location };
}

function maximizeBrowser() {

    window.moveTo(0, 0)
    window.resizeTo(screen.availWidth, screen.availHeight);
}

// Add Bookmark for surfers.  Source: www.dynamicdrive.com
function addBookmark() {

    if (document.all) {
        window.external.AddFavorite("http://www.SensoryTest.com", "SensoryTest.com -- Sensory Computer Systems") 
    }
}

// Disable the use of the browser back button by advancing the history
function DisableBrowserBackButton() {

    window.history.go(1)
}

//   PageOnloadEnvironTestingPagesOnly
function PageOnloadEnvironTestingPagesOnly() {

    MaximizeWindowSize();
    window.history.go(1);
}


//  This creates a string trim function
function trim(string) {

    return string.replace(/(^\s*)|(\s*$)/g, "")
}


// COOKIES Javascript
// ******************

// Fun Testing, all four possibilities,   Cookies  Set & Read,  Simple & Array,  
//addSubkeyToCookie("PanelistLogin","JRTestCookieArray","Value In JRTestCookieArray");
//setCookie("JRTestCookieSimple","Value In JRTestCookieSimple","");
//window.alert ("This is cookie array named (PanelistLogin)(JRTestCookieArray)=" + getCookieSubkey("PanelistLogin","JRTestCookieArray") + "\r\r" + "This is cookie simple named (JRTestCookieSimple)=" + getCookie("JRTestCookieSimple")  );

/* SetCookie creates a cookie in JavaScript  
Example:   setCookie("Test","Yes");     setCookie("FromLoginAsp","NO");
 
expires:  If no value is set for expires, it will only last as long as the current session of the visitor, 
and will be automatically deleted when they close their browser. 
If the expires variable is set, make the correct expires time, the current script below will set 
it for x number of days, to make it for hours, delete * 24, for minutes, delete * 60 * 24

path:  As a good general rule, set the path to '/', the root of your website. 
Generally 'domain' and 'secure' are not something you will be needing to use unless you set the cookie on a subdomain
 
domain and secure:  Are not utilized. Use 'domain' on the Javascript cookie if you are using it on a subdomain, like widgets.yoursite.com
*/
function setCookie(name, value, expires, path, domain, secure) {

    if (expires) { expires = expires * 1000 * 60 * 60 * 24; }

    // set current time, then add expires , everything is in milliseconds
    var today = new Date(); today.setTime(today.getTime());
    var expires_date = new Date(today.getTime() + (expires));

    document.cookie = trim(name) + "=" + unescape(value) +
      ((expires) ? ";expires=" + expires_date.toGMTString() : "") +
      ((path) ? ";path=" + path : "") +
      ((domain) ? ";domain=" + domain : "") +
      ((secure) ? ";secure" : "");
}

function getCookie(name) {

    // first we'll split this cookie up into name/value pairs
    // note: document.cookie only returns name=value, not the other components
    var allCookies = document.cookie.split(';');
    var cookieExists = false;

    for (i = 0; i < allCookies.length; i++) {

        var aCookie = new Object;

        // now we'll split apart each name=value pair
        aCookie = allCookies[i].split('=');

        // and trim left/right whitespace while we're at it
        aCookie.name = trim(aCookie[0]);

        // if the extracted name matches passed name
        if (aCookie.name == name) {
            cookieExists = true;
            // we need to handle case where cookie has no value but exists (no = sign, that is):
            if (aCookie.length > 1) {
                aCookie.value = unescape(aCookie[1].replace(/^\s+|\s+$/g, ''));
            }

            // note that in cases where cookie is initialized but no value, null is returned
            return aCookie.value;
            break;
        }
    }

    if (!cookieExists) {
        return null;
    }
}

	
//  Cookie Array Usage , more complex then simple cookies.
function getCookieSubkey(name, subkey) {

    var subkeys = parseCookieSubkeys(name)
    return subkeys[subkey]
}

//  Cookie Array Usage
//   Example:  addSubkeyToCookie("Test","Value","Yes");
function addSubkeyToCookie(name, subkey, value) {

    var subkeys = parseCookieSubkeys(name)
    subkeys[subkey] = value
    
    var aCookie = trim(name) + "="
    for (var item in subkeys) {
        aCookie += item + "=" + subkeys[item] + "&"
    }
    aCookie = aCookie.substring(0, aCookie.length - 1)
    aCookie += "; path=/"

    document.cookie = aCookie
}

function deleteCookie(name) {

    createCookie(name, "", -1);
}


// Called by above two, for  Cookie Array Usage
function parseCookieSubkeys(name) {

    var subkeyStrings = new Object;
    // Then split document.cookie on semicolons. a becomes an array containing all cookies that are set for this domain and path.
    var cookies = document.cookie.split(";")
    for (var i = 0; i < cookies.length; i++) {
        if (cookies[i].substring(0, name.length) == name) {
            if (cookies[i].substring(name.length + 2).length > 0) {
                subkeyStrings = (cookies[i].substring(name.length + 1)).split("&");
            }
            break;
        }
    }

    var subkeys = new Object;
    if (subkeyStrings.length) {
        for (i = 0; i < subkeyStrings.length; i++) {
            var subkey = subkeyStrings[i].split("=")   // 2 dimension, 0 subkey  1 value
            subkeys[subkey[0]] = subkey[1]   // builds array
        }
    }

    return subkeys
}

function hideElement(ElementName) {
    document.getElementById(ElementName).style.display = "none";
}


// Thinking about  a) Time Delay  &  b) Time Intensity  support for clients 
//              http://forums.digitalpoint.com/showthread.php?t=189943
function startTimeCountDown() {
    if (countdownfrom > 0) {
        document.getElementById("countDownDiv").innerHTML = "&nbsp; " + countdownfrom + " seconds...";
        countdownfrom = countdownfrom - 1;

        if (typeof fRef.submit1 != 'undefined') {
            fRef.submit1.disabled = true;
            fRef.submit1.style.background = 'lightgrey';
            fRef.submit1.style.background = 'ivory';
            fRef.submit1.style.background = 'whitesmoke';
        }
        
        setTimeout("startTimeCountDown()", 1000);

    } else {
        document.getElementById("countDownDiv").innerHTML = "&nbsp;  Done.    Please Press the Next Page button.";
        if (typeof fRef.submit1 != 'undefined') {
            fRef.submit1.style.background = 'deepskyblue';
            fRef.submit1.disabled = false;   // has to be enabled for click to fire
            fRef.submit1.click();
        }
    }
}


// FUNCTION:    IsSensoryStr()
//              Used to Validate a String to ensure it is equal to "SENSORY"
//              Used in CustomValidators instead of CAPTCHA
function IsSensoryStr(oSrc, args) {
    var str = args.Value;
    args.IsValid = (str.toLowerCase() == 'sensory');
} 

