﻿var messageLabs = function() {
    function __ToggleItem(link, div) {

        var clickedLink = document.getElementById(link);
        var objLink = '';
        
        if(clickedLink.parentNode.parentNode.childNodes[1].innerHTML != "| ") {
            objLink = clickedLink.parentNode.parentNode.childNodes[1];
        } else {
            objLink = clickedLink.parentNode.parentNode.childNodes[0];
        }

        var objDiv = document.getElementById(div);
        if(objDiv.style["display"] == "none") {
            var pos = messageLabsCommon.__AbsolutePosition(objLink);
            var top = pos.top + pos.height;
            var left = pos.left;
		    
if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){
 var ieversion=new Number(RegExp.$1) 
 if (ieversion==7){
	    objDiv.style["position"] = "absolute";
	    objDiv.style["top"] =  "10px";
	    objDiv.style["left"] ="308px";
}

 if (ieversion==6){
	    objDiv.style["position"] = "absolute";
	    objDiv.style["top"] =  "18px";
	    objDiv.style["left"] ="300px";
}

}else{
objDiv.style["position"] = "absolute";
	    objDiv.style["top"] =  "18px";
	    objDiv.style["left"] ="88px";
}
            objDiv.style["display"] = "";
            objDiv.style["z-index"] = 1000;
        }
        else {
            objDiv.style["display"] = "none";
        }
        return false;
    }
    
    messageLabsCommon.currentCulture = function() {
        //Check cookie first
        var culture = Cookie.get("culture");
        
        if (!culture) {
            var current = window.location.href;
            for (i = 0; i < LANGUAGE_TABLE.values().length; i++) {
                if (current.indexOf(LANGUAGE_TABLE.values()[i]) != -1) {
                    culture = LANGUAGE_TABLE.keys()[i];
                    break;
                }
            }
        }
        
        return culture;
    }
    
    messageLabsCommon.detectCulture = function() {
        var culture;
                
        /* Check if the user has a session cookie set stating we already redirected them */
        var detected = Cookie.get("detected");
        
        var qs = new Querystring();
        var setCulture = qs.get("culture");
        if ((setCulture) && ("" != setCulture)) {
            var cookieOptions = { duration: 365, path:'/' }; 
            if(COOKIE_DOMAIN != "localhost") cookieOptions.domain = COOKIE_DOMAIN;
            Cookie.set("culture",setCulture, cookieOptions);               
        }
        
        if (!detected) {
            //Check if they have a cookie set for what culture they want
            culture = Cookie.get("culture"); 

            /*
             * If the culture cookie is not set then check the browser's settings
             */
            if (!culture) {
                /*
                 * Loop through all of the available domains and determine
                 * which domain we are on and then pull the culture
                 * for that domain
                 */
                var current = window.location.href;
                var found = false;
                for (i = 0; i < LANGUAGE_TABLE.values().length; i++) {
                    if (current.indexOf(LANGUAGE_TABLE.values()[i]) != -1) {
                        culture = LANGUAGE_TABLE.keys()[i];
                        found = true;
                        break;
                    }
                }
                
                /*
                 * If we didn't detect it based upon the URL or if it's
                 * the US culture, than attempt to determine the culture
                 * based upon the browser
                 */
                if ((!found) || (culture.toLowerCase() == "en-us")) {
                    try {
                        if (window.ie) {
                            culture = navigator.userLanguage;
                        } else {
                            culture = navigator.language;
                        }
                    } catch(err) {
                        culture = 'en-US';
                    }
                }
            }
            culture = culture.toLowerCase();
                      
            var redirect;
            
          
            if (LANGUAGE_TABLE.hasKey(culture)) {
                redirect = LANGUAGE_TABLE.get(culture)
            } else {
                redirect = LANGUAGE_TABLE.get("en-us");    
            }
           
            var cookieOptions = { duration: false }; 
            if(COOKIE_DOMAIN != "localhost") cookieOptions.domain = COOKIE_DOMAIN;
            Cookie.set("detected","1", cookieOptions);
            
            //try to read the cookie we just set, if we can't, then do NOT
            //perform a redirect, because it will just create an infinite loop
            detected = Cookie.get("detected")
            if (detected == "1")
            {
                //check if we're already on this url, if so, don't redirect
                if (window.location.href.indexOf(redirect) == -1)
                    window.location = redirect + CURRENT_PAGE + location.search;
            }
        }     
        return true;
    }
    
    messageLabsCommon.changeLanguage = function() {
        __ToggleItem("changeLanguage", "languageContainer");
        return false;
    };
    
    messageLabsCommon.selectLanguage = function(culture, redirect, domain) {
        if (redirect.toLowerCase().indexOf(CURRENT_PAGE.toLowerCase()) == -1)
        {
            redirect = redirect + CURRENT_PAGE;
       
        }
        var cookieOptions = { duration: 365, path:'/' }; 
        if(COOKIE_DOMAIN != "localhost") cookieOptions.domain = COOKIE_DOMAIN;
        Cookie.set("culture",culture, cookieOptions);        

        /* We can't do this in javascript since IE doesn't set the referer
        if (Browser.Engine.trident)
            window.navigate(redirect + "?culture=" + escape(culture));
        else
            window.location = redirect + "?culture=" + escape(culture);
        */
    };    
    
    messageLabsCommon.campaignCheck = function() {
        var qs = new Querystring();
        
        var temp = qs.get("c");
        var subcampaign = qs.get("s");
        var thirdparty = qs.get("CC");
        
        /*
         * thirdparty campaigns will override our local campaings
         */
        if ((thirdparty) && (thirdparty != "")) {
            var cookieOptions = { duration: 7, path:'/' }; 
            Cookie.set("campaign",thirdparty,cookieOptions);        
        }
        else if ((temp) && (temp != "")) {
            var cookieOptions = { duration: 7, path:'/' }; 
            Cookie.set("campaign",temp,cookieOptions);
            
            //check if there's a subcampaign
            if ((subcampaign) && (subcampaign != "")) {
                Cookie.set("subcampaign",subcampaign,cookieOptions);
            } else {
                Cookie.set("subcampaign","",cookieOptions);
            }
        }
    };
    
    messageLabsCommon.adWords = function() {
        if (GP) {
            var val = Cookie.get("mlc");
            if (val) {
              arr = val.split("|");
              if (arr.length == 3) {
                f2convert(arr[1],arr[0] + '#' + arr[2] + '*1');
              }
            }
            
            var cookieOptions = { duration: -1, path:'/' };             
            Cookie.set("mlc","",cookieOptions);
        }
    };
    
    return messageLabsCommon;
}();

function getUniqueTrackingId(page){
var id = new Date().getTime().toString();
var val = Cookie.get("wpCheckCampaign");
if(val != null && val != false ){
    var temp = new Array();
    temp = val.split('|');
    val = temp[0];
}else{
val = page;
}
return val + '-' + id;
      
}
function openLiveChat()
 {
window.open('../../LiveChat.aspx','LiveChat','width=480,height=360,toolbar=no,scrollbars=no,location=no,resizable=no');
 }  


   function DisplayListChart(fileName, containerName, col1Title, col2Title){

          var url= 'ListGraphHandler.get?file=' + fileName + '&col1=' + col1Title + '&col2=' + col2Title+ '&rand=' + new Date().getTime();
          var req = newXMLHttpRequest();
          var callbackHandler = getReadyStateHandler(req, containerName);
          req.onreadystatechange = callbackHandler;
          req.open("GET", url, true);
          req.setRequestHeader("Content-Type", "text/xml");
      
          req.send("");
        }
        
        
   function DisplayHistogram(fileName, containerName, displayType, title){

          var url= 'HistogramGraphHandler.get?file=' + fileName + '&type=' + displayType + '&title=' + title + '&rand=' + new Date().getTime();
          var req = newXMLHttpRequest();
          var callbackHandler = getReadyStateHandler(req, containerName);
          req.onreadystatechange = callbackHandler;
          req.open("GET", url, true);
          req.setRequestHeader("Content-Type", "text/xml");
      
          req.send("");
        }
        
          function DisplayLineGraph(fileName, containerName, displayType){

          var url= 'LineGraphHandler.get?file=' + fileName + '&type=' + displayType + '&rand=' + new Date().getTime();
          var req = newXMLHttpRequest();
          var callbackHandler = getReadyStateHandler(req, containerName);
          req.onreadystatechange = callbackHandler;
          req.open("GET", url, true);
          req.setRequestHeader("Content-Type", "text/xml");
      
          req.send("");
        }
        
        //create a XmlHttp object
        function newXMLHttpRequest() {
        var xmlreq = false;
        //if support XmlHttp, then create the XmlHttp object
        if (window.XMLHttpRequest) {
	        xmlreq = new XMLHttpRequest();
        } else if (window.ActiveXObject) {
		        // judge whether to support the XmlHttp ActiveX
	        try { 
		        xmlreq = new ActiveXObject("Msxml2.XMLHTTP");
	        } catch (e1) { 
		        // fail in creaing the first ActiveX control
		        try {
			        xmlreq = new ActiveXObject("Microsoft.XMLHTTP");
		        } catch (e2) {
			         // both failed, then return false
		        } 
	        }
        }
        return xmlreq;
        } 

        //the server side callback function
        function getReadyStateHandler(req, containerName) {
	        return function () {
	           
	        if (req.readyState == 4) {//already loaded
		        if (req.status == 200) {//return successfully
		      document.getElementById( containerName ).innerHTML = req.responseText;
		        } 
    	        }
 	        }
        }