// *** GOOGLE ANALYTICS VARIABLES *** //

var googleAnalyticsAccountNumber = "UA-6219537-1";
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
var pageTracker;

// *** GLOBAL VARIABLES *** //

var AC_FL_RunContent = 0;
var DetectFlashVer = 0;
var javaVersion = null; // variable that stores the JRE version (null = unable to detect JRE and version)
var daysToExpireCookie = 3650; // ten years!
var defaultContext = "lnr";
var context = defaultContext;
var locale = "en";
var defaultAdCode = "281602";
var adCode = defaultAdCode;
var id = "0";
var cookieId = "0";
var sessionId = "0";
var pageId = "0";
var pageEventId = "0";
var groupId = 0;

var hostUrl = "http://" + location.host;
var trackerUrl = hostUrl + "/voiceLavalife/voiceLLTracker.do";
var utilityUrl = hostUrl + "/voiceLavalife/utility.do";
var campaignServiceUrl = hostUrl + "/voiceLavalife/service/campaign";
var loggerUrl = hostUrl + "/voiceLavalife/logger.do";
var neSurveyData = hostUrl + "/voiceLavalife/voiceLLSurveyData.do";
var urlPrefix = hostUrl + "/web/guest/";

// *** GLOBAL FUNCTIONS *** //

// use to set cookieId and sessionId variables
function setCookieIdAndSessionId()
{
    var nowDate = new Date();
    var url = trackerUrl + "?doUpdateTrackingData=false&seed=" + nowDate.getTime().toString();
    var value = httpPost(url);
    var record = value.split("&");
    if (record.length == 2)
    {
        var i;
        for (i = 0; i < record.length; i++)
        {
            var temp = record[i].split("=");
            if (temp.length == 2 && temp[0] == "cookieId")
            {
                cookieId = temp[1];
            }
            else if (temp.length == 2 && temp[0] == "sessionId")
            {
                sessionId = temp[1];
            }
        }
    }
    cookieId = evalParamAndCookieValue("cookieId", cookieId);
}

// use to get locale info
function getLocale()
{
    var myLocale = getURLParamValue("locale");
    if (myLocale == null) myLocale = getCookie("locale");
    if (myLocale == null || myLocale == "en")
    {
        // get country iso code using ip
        var url = utilityUrl + "?tool=requestTypeGetCountryFromIP";
        var countryIsoCode = httpPost(url);
        if (countryIsoCode.length > 1) myLocale = "en_" + countryIsoCode;
        else myLocale = "en";
    }
    // save locale info
    saveCookie("locale", myLocale);
    return myLocale;
}

// use to create a log for any ad code rewrites
function logAdCodeRewrite(myCookieId, myContext, myOldAdCode, myAdCode, myReferrer)
{
    var url = loggerUrl + "?lfPrefix=adCodeRewrite&cookieId=" + myCookieId + "&context=" + myContext + "&oldAcode=" + myOldAdCode + "&adCode=" + myAdCode + "&referrer=" + myReferrer;
    httpPost(url);
}

// use to update adCode if specified url has known domain
function setAdCodeBasedOnDomain(theUrl, defaultAdCode)
{
    var myAdCode = defaultAdCode;
    var myDomain = getUrlDomain(theUrl);
    var matchFound = true;
    
    if ((myDomain.toLowerCase()).indexOf("google.com") > -1)
    {
        myAdCode = "281603";
    }
    else if ((myDomain.toLowerCase()).indexOf("google.ca") > -1)
    {
        myAdCode = "281604";
    }
    else if ((myDomain.toLowerCase()).indexOf("ca.search.yahoo.com") > -1)
    {
        myAdCode = "281606";
    }
    else if ((myDomain.toLowerCase()).indexOf("search.yahoo.com") > -1)
    {
        myAdCode = "281605";
    }
    else if ((myDomain.toLowerCase()).indexOf("search.msn.com") > -1)
    {
        myAdCode = "281607";
    }
    else if ((myDomain.toLowerCase()).indexOf("search.live.com") > -1)
    {
        myAdCode = "281608";
    }
    else if ((myDomain.toLowerCase()).indexOf("aol.com") > -1)
    {
        myAdCode = "281609";
    }
    else if ((myDomain.toLowerCase()).indexOf("aol.ca") > -1)
    {
        myAdCode = "281610";
    }
    else if ((myDomain.toLowerCase()).indexOf("ask") > -1)
    {
        myAdCode = "281611";
    }
    else
    {
        matchFound = false;
    }
    
    if (matchFound == true)
    {
        saveCookie("adCodeBasedOnDomain", "true");
    }
    
    return myAdCode;
}

// return the domain portion of a url
function getUrlDomain(theUrl)
{
    var leadSlashes = theUrl.indexOf("//");
    var domainStart;
    if (leadSlashes < 0)
    {
        domainStart = 0;
    }
    else
    {
        domainStart = leadSlashes + 2;
    }
    var withoutResource = theUrl.substring(domainStart, theUrl.length);
    var nextSlash = withoutResource.indexOf("/");
    return withoutResource.substring(0, nextSlash);
}

// builds and return an absolute url path to the html page being passed
function buildHtmlUrl(htmlPage)
{
    return urlPrefix + htmlPage;
}

// get the url param value
function getURLParamValue(paramName)
{
    paramName = paramName.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
    var regexS = "[\\?&]" + paramName + "=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href);
    if (results == null)
    {
        return null;
    }
    else
    {
        return results[1];
    }
}

// set the url params
function setURLParams(url)
{
    var newUrl = url;
    if (newUrl.indexOf("?") < 0) newUrl += "?";
    else newUrl += "&";
    newUrl += "a=" + adCode;
    return newUrl;
}

// open popup window
function launchpopup(url, name, width, height, toolbar, location, directories, menubar, scrollbars, resizable)
{
    winSet = "toolbar="+toolbar+",location="+location+",directories="+directories+",menubar="+menubar+",scrollbars="+scrollbars+",resizable="+resizable;
    winSet += ",width="+width +",height="+height+",left="+(screen.availWidth-width)/2+",top="+(screen.availHeight-height)/2;
    mypopwnd = window.open(setURLParams(url), name, winSet);
    mypopwnd.focus();
}

// open popup window
function launchpopupscroll(url, name, width, height, scroll)
{
    winSet = "toolbar=no,location=no,directories=no,menubar=no,scrollbars="+scroll+",resizable=no";
    winSet += ",width="+width +",height="+height+",left="+(screen.availWidth-width)/2+",top="+(screen.availHeight-height)/2;
    mypopwnd = window.open(setURLParams(url), name, winSet);
    mypopwnd.focus();
}

// changes parent window to new url and closes window that called this function
function returnToParentWithNewUrl(url)
{
    opener.location.href = url;
    window.close();
}

// sends a http get to the pURL and returns the result page (SYNC method)
function httpPost(pURL)
{
    if (window.XMLHttpRequest)
    {
        // code for Mozilla, Safari, etc
        xmlHttp = new XMLHttpRequest();
        xmlHttp.open("GET", pURL, false);
        xmlHttp.send(null);
        return xmlHttp.responseText;
    }
    else if (window.ActiveXObject)
    {
        //IE
        xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
        if (xmlHttp)
        {
            xmlHttp.open('GET', pURL, false);
            xmlHttp.send();
            return xmlHttp.responseText;
        }
    }
    return "";
}

// sends a http get to update tracking data (AJAX method)
function trackerUpdate(myPageId, myPageEventId)
{
    var xmlHttp;
    var nowDate = new Date();
    var url = trackerUrl + "?pageId=" + myPageId + "&pageEventId=" + myPageEventId + "&seed=" + nowDate.getTime().toString();
    url = setURLParams(url);
    httpPost(url);
    /*/if (window.XMLHttpRequest)
    {
        // code for Mozilla, Safari, etc
        xmlHttp = new XMLHttpRequest();
    }
    else if (window.ActiveXObject)
    {
        //IE
        xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
    }
    if (xmlHttp != undefined)
    {
        xmlHttp.onreadystatechange = function()
        {
            if (xmlHttp.readyState == 4)
            {
                var value = xmlHttp.responseText;
                var record = value.split("&");
                if (record.length == 2)
                {
                    var i;
                    for (i = 0; i < record.length; i++)
                    {
                        var temp = record[i].split("=");
                        if (temp.length == 2 && temp[0] == "cookieId")
                        {
                            cookieId = temp[1];
                        }
                        else if (temp.length == 2 && temp[0] == "sessionId")
                        {
                            sessionId = temp[1];
                        }
                    }
                }
            }
        }
        xmlHttp.open('GET', url, true);
        xmlHttp.send(null);
    }*/
}

// used to update param/cookie values based on a priority rule
// 1. url param
// 2. cookie data
function evalParamAndCookieValue(name, defaultValue)
{
    var value = getURLParamValue(name);
    if (value != null)
    {
        saveCookie(name, value);
        return value;
    }
    else
    {
        value = getCookie(name);
        if (value == null)
        {
            saveCookie(name, defaultValue);
            return defaultValue;
        }
        else
        {
            return value;
        }
    }
}

// use to parse xml string and return a DOM object (null if error)
function parseXmlString(xmlString)
{
    var xmlDoc = null;
    try //Internet Explorer
    {
        xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
        xmlDoc.async = "false";
        xmlDoc.loadXML(xmlString);
    }
    catch(e)
    {
        try //Firefox, Mozilla, Opera, etc.
        {
            var parser = new DOMParser();
            xmlDoc = parser.parseFromString(xmlString, "text/xml");
        }
        catch(e)
        {
            return xmlDoc;
        }
    }
    return xmlDoc;
}

/******************************************************************************
***                           Cookie support                                ***
******************************************************************************/

/**
 * Read the JavaScript cookies tutorial at:
 *   http://www.netspade.com/articles/javascript/cookies.xml
 */

function saveCookie(name, value)
{
    var expiryDate = new Date();
    expiryDate.setDate(expiryDate.getDate() + daysToExpireCookie);
    setCookie(name, value, expiryDate.toGMTString(), "/", "", "");
}

/**
 * Sets a Cookie with the given name and value.
 *
 * name       Name of the cookie
 * value      Value of the cookie
 * [expires]  Expiration date of the cookie (default: end of current session)
 * [path]     Path where the cookie is valid (default: path of calling document)
 * [domain]   Domain where the cookie is valid
 *              (default: domain of calling document)
 * [secure]   Boolean value indicating if the cookie transmission requires a
 *              secure transmission
 */
function setCookie(name, value, expires, path, domain, secure)
{
    var cook = name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
    document.cookie = cook;
}

/**
 * Gets the value of the specified cookie.
 *
 * name  Name of the desired cookie.
 *
 * Returns a string containing value of specified cookie,
 *   or null if cookie does not exist.
 */
function getCookie(name)
{
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1)
    {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    }
    else
    {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1)
    {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}

/**
 * Deletes the specified cookie.
 *
 * name      name of the cookie
 * [path]    path of the cookie (must be same as path used to create cookie)
 * [domain]  domain of the cookie (must be same as domain used to create cookie)
 */
function deleteCookie(name, path, domain)
{
    if (getCookie(name))
    {
        document.cookie = name + "=" +
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}
