/*Logic to determine if the dynamic target test should be run.  Ari Koinuma Oct 16 2009 */
//Regardless of the outcome of this script, the trial code will run
//this same code runs in c5_330_product_contentBody.js, because this test is supposed to run on two pages.  If updating script, updated on the other file also!
//the other URL where it runs is product.aspx

  //build internal referrers
  var wordIntReferrer = 'http://office.microsoft.com/en-us/word/FX100487981033.aspx';
  var excelIntReferrer = 'http://office.microsoft.com/en-us/excel/FX100487621033.aspx';
  var pptIntReferrer = 'http://office.microsoft.com/en-us/powerpoint/FX100487761033.aspx';
  var outlookIntReferrer = 'http://office.microsoft.com/en-us/outlook/FX100487751033.aspx';

  
  //function that checks to see if the referrer is one of the internals
  function checkIntReferrer(referrer){
    switch(referrer){
      case wordIntReferrer:
        return 'word';
        break;
      case excelIntReferrer:
        return 'excel';
        break;
      case pptIntReferrer:
        return 'ppt';
        break;
      case outlookIntReferrer:
        return 'outlook';
        break;
      default: 
        return null;
    }
  }
  
  //function to determine the domain
  function idDomain(referrer) {
    var referrer = referrer.toLowerCase();
    var domain = referrer.slice(0,referrer.indexOf('?'));
    if (domain.indexOf('google.com') != -1) {
      var referrerDomain = 'google';
    } else if (domain.indexOf('yahoo.com') != -1) {
      var referrerDomain = 'yahoo';
    } else if (domain.indexOf('bing.com') != -1) {
      var referrerDomain = 'bing';
    } else {
      var referrerDomain = null;
    }
    return referrerDomain;
  }
  
  //function to pick out key phrases;
  function isolateSrchTerms(referrer){
    var referrer = referrer.toLowerCase();
    var referrerDomain = idDomain(referrer);
    
    if (referrerDomain != null) {
      //find the q.  if it's Yahoo, find the "p" is where the search terms are
      if (referrerDomain == 'yahoo') {
        var srchStrings = 'p=';
      }
      else {
        var srchStrings = 'q=';
      }
      
      var qStartPos = referrer.indexOf(srchStrings);
      var queryString = referrer.slice(qStartPos);
      if (queryString.indexOf('&') != -1) {
        var qEndPos = queryString.indexOf('&');
      }
      else {
        var qEndPos = queryString.length;
      }
      var srchTerms = queryString.slice(2, qEndPos);// it starts at 2 because the first two characters are either 'q=' or 'p='
      return srchTerms;
    } else {
      //referrerDomain is null.  It's not one of the domains we're looking for. 
      return null;
    }
  }

  //function to search against one product's search terms
  function compareSrchTerms(srchTerms) {
    if (srchTerms.indexOf('word') != -1) {
      return 'word';
    } else if (srchTerms.indexOf('excel') != -1) {
      return 'excel';
    } else if (srchTerms.indexOf('powerpoint') != -1 || srchTerms.indexOf('power+point') != -1) {
      return 'ppt';
    } else if (srchTerms.indexOf('outlook') != -1) {
      return 'outlook';
    } else {
      return null;
    }
  }
  
  
  //the master function to figure out if the referrer meets one of the criteria. 
  function evalReferrer(referrer){
    
      var prod = null;  //will be word, excel, ppt or outlook
      prod = checkIntReferrer(referrer);  //if it doesn't match one of our internal referrers, then it'll remain null
      
      //if it's not an internal referrer, check to see if it was a search engine with one of the keywords
      if (prod == null) {
        var srchTerms = isolateSrchTerms(referrer);
        //if there are search terms to evaluate, then check to see if it contains one of our keywords.  If not, the prod will remain null.  
        if (srchTerms != null) {
          prod = compareSrchTerms(srchTerms);
        }
      }
      
    return prod;
  }
  
  //execute 
  var referrer = document.referrer;
  var prod = evalReferrer(referrer);
  var runDynamicTarget = false;
  
  //initialize the optrial object
  if (typeof(optrial) == 'undefined') {
      var optrial = new Object();
    }
  
  if (prod != null) {
    //set the page view attributes
    
    
    //figure out which site is the referrer
    var intReferrer = checkIntReferrer(referrer);
    if (intReferrer != null) {
      optrial.referrerType = 'msOffice';
    } else {
      optrial.referrerType = idDomain(referrer);
    }
    
    //this is the variable that'll make the test run when it's one of four valid values (word, excel, ppt, outlook) as well as setting the page view attribute. 
    optrial.productType = prod;
    
    //this the variable OT will look to figure out which product to show
    var otProd = prod;  
    
  }
  

/*
Portfolio: c5_455 & c5_330 (Buy/Try Office en-US)
Subject: TRY home & PD pages
Trial: Trial 1
Referenced context:
- JavaScript variables: optrial.productType, optrial.referrerType, optrial.sticky
Note: This code should not be included in a secure (https) page.

*/

(function(){var _o=ptomodule;_o.U="http://pto.digitalriver.com/trial/390/p/tryhomepdpages.9d1/326/content.js";
_o.ST="script";_o.SA={"type":"text/javascript"};_o.B();_o.A["optrial.productType"]=(typeof optrial.productType!=
"undefined")?optrial.productType:null;_o.A["optrial.referrerType"]=(typeof optrial.referrerType!="undefined")
?optrial.referrerType:null;_o.R(1000,null,null,null);})();


  
  
/* End of Dynamic Testing script */

/*
Portfolio: c5_455 & c5_330 (Buy/Try Office en-US)
Subject: Try Homepage
Trial: Trial 1
Referenced context:
- JavaScript variables: optrial.sticky
Note: This code should not be included in a secure (https) page.
*/
(function(){var _o=ptomodule;_o.U="http://pto.digitalriver.com/trial/390/p/tryhomepage.e0f/248/content.js";
_o.ST="script";_o.SA={"type":"text/javascript"};_o.B();_o.R(1000,null,null,null);})();