//uploaded by Jen Davenport 8/24/2011

/* BEGIN general functions and variables *****************************/



// cookie functions

function SetCookie(n, v, d, e){var de = new Date;de.setTime(de.getTime() + e*24*60*60*1000);document.cookie = n +

 "=" + escape(v) + ((e==null) ? "" : ("; expires=" + de.toGMTString())) + "; path=/" + ((d==null) ? "" : (";domain=" + d));}

function GetDomain(){var sld = document.domain;var dp = sld.split(".");var l = dp.length;if (l < 2)

sld = null;else if (!isNaN(dp[l-1]) && !isNaN(dp[l-2])) sld = null;else sld = "." + dp[l-2] + "." + dp[l-1];return sld;}

function GetCookie(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 ""; }



// get URL param

function getURLparam(name) { var regexS = "[\\?&]"+name+"=([^&#]*)"; var regex = new RegExp( regexS ); var tmpURL = window.location.href; var results = regex.exec( tmpURL ); if( results == null ) return ""; else return results[1]; }



var url = location.href.toLowerCase();

var sku = getURLparam('sku');

var pageName = '';



var tsDays = parseInt(Date.parse(Date()));

tsDays = tsDays/1000/60/60/24;

tsDays = Math.round(tsDays);







function getCookieb(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 "";

}

var expiresa = new Date();

expiresa.setDate(expiresa.getDate() + 1);

var expiresb = new Date();

expiresb.setDate(expiresb.getDate() + 30);



/* END general functions and variables *******************************/



if (typeof(optrial) === 'undefined') {



    var optrial = {};



}









//capturing the initial referrer if on Microsoft.com

if (!getCookieb('op_referrerType') && document.location.href.match(/microsoft\.com/i)){

  var referrer = document.referrer, referrerType = null;

  if (referrer.match(/google/i) || referrer.match(/yahoo/i) || referrer.match(/bing/i)){

    referrerType = 'search';

  } else if (referrer.match(/microsoft\.com\/mac/i)){

    referrerType = 'direct';

  } else if (referrer.match(/microsoft\.com/i)){

    referrerType = 'microsoft'; 

  } else if (referrer.match(/facebook/i) || referrer.match(/twitter/i) || referrer.match(/linkedin/i)){

    referrerType = 'social-media';

  }

  

  if (referrerType){

    document.cookie = 'op_referrerType='+referrerType+';expires=' + expiresb.toGMTString()+';domain=.microsoft.com;path=/;';

  }

}



//Global Monitor trial code.  Runs on all microsoft.com pages



if (document.location.href.match(/microsoft\.com/i)) {

  //new vs. return visitor

  optrial.visitStatus = (getCookieb('op390globalmonitorus2011liid')) ? 'returning' : 'new';

  

}



//cross-domain session capturing on Microsoft.com  This info is then retrieved once the user gets to QuickBuyCart.

if(document.location.href.indexOf("/mac") != -1){



  $(document).ready(function() {

    //first, get the session value from the cookie

    var bcv=getCookieb('op390buy20113liid');

    var hcv=getCookieb('op390home20112liid');

    var pcv=getCookieb('op390products20112liid');

    var dcv='empty';

    var tcv='empty';

    var gcv='empty';

    var cahcv='empty';

    var capcv='empty';

    var cabcv=getCookieb('op390buy201111liid');



    

    //then surround it with identical identifier values (example: 'home'+sessionID+'home') and store it to window.name



    window.name='home'+hcv+'homeproducts'+pcv+'productsdownloads'+dcv+'downloadsbuy'+bcv+'buytry'+tcv+'tryglobal'+gcv+'globalcah'+cahcv+'cahcap'+capcv+'capcab'+cabcv+'cab';

  });

}



//cross-domain session retrieving on Cart page.

if(document.location.href.indexOf("Cart") != -1){

  var cstr1 = window.name.split('home');

  var cstr2 = window.name.split('products');

  var cstr3 = window.name.split('downloads');

  var cstr4 = window.name.split('buy');

  var cstr5 = window.name.split('try');

  var cstr6 = window.name.split('global');



  document.cookie = 'op390buy20113liid='+cstr4[1]+';expires=' + expiresa.toGMTString()+';domain=.officeformac.com;path=/;'

  document.cookie = 'op390buy20113gum='+cstr4[1]+';expires=' + expiresb.toGMTString()+';domain=.officeformac.com;path=/;'



  document.cookie = 'op390home20112liid='+cstr1[1]+';expires=' + expiresa.toGMTString()+';domain=.officeformac.com;path=/;'

  document.cookie = 'op390home20112gum='+cstr1[1]+';expires=' + expiresb.toGMTString()+';domain=.officeformac.com;path=/;'


 document.cookie = 'op390products20112liid='+cstr2[1]+';expires=' + expiresa.toGMTString()+';domain=.officeformac.com;path=/;'

  document.cookie = 'op390products20112gum='+cstr2[1]+';expires=' + expiresb.toGMTString()+';domain=.officeformac.com;path=/;'



}
