/* Untangle Metrics */
var Unm = {
    LoadByDefault: true,
    BaseLocation : 'corp:home',
    getSkuString: function(skus){
        return ';'+skus.join(',;')
    },    
    productViewed: function(productSkus){
        s.products = Unm.getSkuString(productSkus);
        s.events = 'prodView,event7';
    },
    setClientState:function(state,pop){
        s.eVar13 = state; //OnGUI or OffGUI
        s.eVar18 = pop;
    },
    addToCartFirstM: function(productSkus){
        s.products = Unm.getSkuString(productSkus);
        s.events = 'scAdd,scOpen';            
       
    },
    addToCartM: function(productSkus){
        s.products = Unm.getSkuString(productSkus);
        s.events = 'scAdd';        
    },
    removeFromCart:function (productSkus){
        s.products = Unm.getSkuString(productSkus);
        s.events = 'scRemove';
    },
    getPageName:function(){
        var sUrl = document.location.href.replace(/https?:\/\//,'');
        sUrl = sUrl.replace(/index\.php\/?/i,'');
        sUrl = sUrl.replace(/\/$/,'');
        sUrl = sUrl.replace(/\.html/i,'');
        sUrl = sUrl.replace(/\#$/,'');
        var fragments = sUrl.split('/');
        fragments[0] = Unm.BaseLocation;
        return fragments.join(':').toLowerCase();        
    },
    beginCheckout:function(productSkus){
        s.products = Unm.getSkuString(productSkus);
        s.events = 'scCheckout';                
    },
    getCompanyType : function(){
        
        var ci = Unm.getCompanyInfo(),
            hc = /(healthcare|hospital|medical)/i,
            ed = /(education|government|school|university)/i;
            
        if(ci.CompanyName.toLowerCase()=='no-co' && ci.Industry.toLowerCase()=='no-in'){
            return 'generic';
        }else if (ci.CompanyName.match(hc) || ci.Industry.match(hc)){
            return 'healthcare';             
        }else if (ci.CompanyName.match(ed) || ci.Industry.match(ed)){
            return 'education';
        }else{
            return 'smb';
        } 
    },
    getCompanyInfo : function(val){
        var obj = {"CompanyName":1,"Size":2,"Industry":3,"EmployeeCount":4},ci = Unm._getCompanyInfo();
        if(val != null){
            if(val in obj){
                return ci[obj[i]];
            }else{
                return "";
            }
        }else{
            for(var i in obj){
                obj[i] = ci[obj[i]];
            }
            return obj;            
        }
    },
    _getCompanyInfo : function(){
        var info=["NO-ID","NO-CO","NO-SZ","NO-IN","NO-EC","NO-IP"], __info = Unm.getCookie("___dbci"),temp=null,i;
        if(__info!=""){
            temp = __info.split("##");
            for(i=0;i<temp.length;i++){
                info[i] = temp[i];
            }    
        }
        return info;            
    },
    setCompanyInfo : function(val){
        var info=["NO-ID","NO-CO","NO-SZ","NO-IN","NO-EC","NO-IP"];
        if(val.demandbase_sid){
            info[0] = val.demandbase_sid;
        }
        if(val.company_name){
            info[1] = val.company_name;
        }
        if(val.company_size){
            info[2] = val.company_size;
        }
        if(val.industry){
            info[3] = val.industry;
            if(val.sub_industry){
                info[3] = info[3] + ";" + val.sub_industry;
            }
        }
        if(val.employee_count){
            info[4] = val.employee_count;
        }
        if(val.ip){
            info[5] = val.ip;
        }                                
        s.eVar28 = info.join("##");
        Unm.setCookie("___dbci",info.join("##"),3)
    },
    setCustomerGroup:function(val){
        s.eVar12 = val;
    },
    setSearchVariable:function(val,resultCount){
        if(resultCount == 0){
            val = 'null: '+val
        }
        s.prop5 = val;   
    },
    purchaseComplete:function(skusQtysRevs,purchaseID,state,postalCode,server){
        s.products = Unm.getSkuString(skusQtysRevs);
        s.events = 'purchase';
        s.purchaseID = purchaseID;
        s.state = state;
        if(postalCode){
            s.zip = postalCode;        
        }
        if(server){
            s.prop16 = server;
        }
    },
    //downloadPageName is optional - default value is the current page name
    trackExternalDownload:function(downloadFileName,downloadPageName,transactionID){
       var g=s_gi(s_account);
       g.linkTrackVars='eVar7,eVar21,eVar22,events,prop8,prop9,transactionID'; 
       g.linkTrackEvents='event4'; 
       g.events='event4'; 
       g.eVar7=downloadFileName;
       g.prop7=downloadFileName;
       //transaction ID is used for product downloads only
       if(transactionID){
            g.transactionID = transactionID;
       }
       if(downloadPageName){
        g.prop8 = downloadPageName;
       }else{
        g.prop8 = s.pageName;            
       }
       //g.t();
       g.tl(this,'d',downloadFileName);
       //s.tl(this,'d',downloadFileName);
    },
    setBillingCheckoutEvent : function (){
        s.events = 'event15'; 
        s.eVar12 = 'Direct'; 
    },    
    setCheckoutPage:function(page){
        switch(page){
            case 'billing':
                s.events = 'event15';
            break;
            case 'payment':
                s.events = 'event16';
            break;    
            case 'contact':
                s.events = 'event17';
            break;
            case 'review':
                s.events = 'event18';
            break;     
            default :
                s.events = '';
            break;                  
        }
        s.pageName = Unm.getPageName()+':'+page;
        s.t();
    },
    setPaymentMethod: function(method){
        s.eVar10 = method;
    },
    _addToEventList:function(e){
        if(s.events != ''){
            s.events +=','+e;
        }else{
            s.events = e;
        }
    },
    setC:function(c){
        s.eVar14 = c;
    },
    setVoucherRedeem:function(skus,server){
        Unm._addToEventList('event10');
        s.prop12 = skus.join(';');
        s.prop13 = server;        
    },
    setProductReinstall:function(skus,server){
        Unm._addToEventList('event9');
        s.prop14 = skus.join(';');
        s.prop15 = server;
        s.t();
    },
    couponApplied:function(coupon){
        Unm._addToEventList('event11');
        s.eVar15 = coupon;
    },
    couponFailed:function(coupon){
        Unm._addToEventList('event13');
        s.prop17 = coupon;                    
    },
    setDownloadEmail : function(val,downloadType){
        if(val){
            if(downloadType=='msftDownload'){
                s.eVar22 = val;
            }else{
                s.eVar21 = val;
            }
        }
    },
    setCookie : function(c_name,value,expiredays){
        var exdate=new Date();
        exdate.setDate(exdate.getDate()+expiredays);
        document.cookie=c_name+ "=" +escape(value)+
        ((expiredays==null) ? "" : ";expires="+exdate.toGMTString());   
    },
    getCookie : function(c_name){
        if (document.cookie.length>0)
          {
          c_start=document.cookie.indexOf(c_name + "=");
          if (c_start!=-1)
            {
            c_start=c_start + c_name.length+1;
            c_end=document.cookie.indexOf(";",c_start);
            if (c_end==-1) c_end=document.cookie.length;
            return unescape(document.cookie.substring(c_start,c_end));
            }
          }
        return "";
    },    
    setMVTest1Var:function(val){
       if(val=="download-steps" || val == "download-regular" || val=="po-original" || val == "po-new"){
        s.eVar20 = val;
       }else{
        s.eVar16 = val;
       }

    }
}