/*
survey.js
Author: Nate Parsons
Date: May 11th, 2008
Script to manage intercept survey
*/
//Load Libraries:
//js_include("jquery-1.2.3.min.js");

//Global Variables
var sPrefix = "/apco/";
var js_path = sPrefix + "js/";
var aPageFrequency = new Array();
var aPageDestination = new Array();
aPageFrequency['/'] = .5;
aPageFrequency['/default.asp'] = .5;
aPageFrequency['/default.asp#FromHeader'] = .5;
aPageFrequency['/about_altria/1_0_aboutaltriaover.asp'] = 1;
aPageFrequency['/investors/2_0_investorsover.asp'] = 1;
aPageFrequency['/media/03_00_mediarecpressrel.asp'] = 1;
aPageFrequency['/responsibility/4_0_responsibilityover.asp'] = 1;
aPageDestination['/'] = "survey_home.html";
aPageDestination['/default.asp'] = "survey_home.html";
aPageDestination['/default.asp#FromHeader'] = "survey_home.html";
aPageDestination['/about_altria/1_0_aboutaltriaover.asp'] = "survey_about.html";
aPageDestination['/investors/2_0_investorsover.asp'] = "survey_investors.html";
aPageDestination['/media/03_00_mediarecpressrel.asp'] = "survey_media.html";
aPageDestination['/responsibility/4_0_responsibilityover.asp'] = "survey_responsibility.html";
//var ip_check_page = sPrefix + "/ipcheck.asp";
var ip_check_page = "/ipcheck.asp";

//REMOVE THE COMMENT "/*" DIRECTLY BELOW (around line 24) AND "*/" DOWN AT THE END OF THIS BLOCK
//(around line 52) TO ENABLE IP CHECKING

$.ajax({
    url: ip_check_page,
    type: 'GET',
    dataType: 'xml',
    timeout: 1000,
    async: false,
    error: function(){
        console.warn('Error loading XML document');
        take_survey = 'no';
    },
    success: function(xml){
        // do something with xml
        var user_ip = $('ip', xml).text();

        var take_survey = $('access', xml).text();
        if(take_survey == "no")
        {
        	SetCookie('survey_status','no_survey',expiry, '/');
        	console.info("User restricted from taking survey by IP");
        }
        else
        {
        	SetCookie('user_ip', user_ip, expiry, '/');
        }
    }

});

//REMOVE THE COMMENT "*/" above and the matching "/*" around line #24 to enable ip checking

//Begin MAIN
//check for access:
  //Does browser accept cookies
  SetCookie('test','1',expiry, '/');
  if(GetCookie('test') == 1) //we got cookie!
  {
    //clean up
    DeleteCookie('test');
    sStatus = GetCookie('survey_status');
    //get current page url
    var sCurrentUrl = location.pathname;

    if(sStatus == null || sStatus == "")
    {
      //They haven't been surveyed yet
      //what page are they on

      if(aPageFrequency[sCurrentUrl] != null && aPageFrequency[sCurrentUrl] != "")
      {

        fFrequency = aPageFrequency[sCurrentUrl];
        sSurveyPage = aPageDestination[sCurrentUrl];
        fRandom = Math.random();
        if(fRandom < fFrequency)
        {
          console.info("YES Survey: Page Frequency: %f, Random Chance: %f", fFrequency, fRandom);
          AJS.AEV(window, 'load', function()
          {
                //create path cookie
                SetCookie('user_path',sCurrentUrl,expiry, '/');
                GB_showCenter("Altria Site Survey", sPrefix  + sSurveyPage, 480, 640);
          });
        }
        else
        {
          console.info("NO Survey: Page Frequency: %f, Random Chance: %f", fFrequency, fRandom);
          //If frequency not met, set no survey cookie
          SetCookie('survey_status','no_survey',expiry, '/');
        }
      }
      else
      {
        //we don't know this page... is there a default frequency?
        console.info("Unknown Frequency: %s", sCurrentUrl);
      }

    }
    else if(sStatus == "underway")
    {
      //they are browsing site
      sUserPath = GetCookie('user_path');
      sUserPath += "*" + sCurrentUrl;
      SetCookie('user_path',sUserPath,expiry, '/');
      console.info("User is taking survey");
      //record user data for this page
    }
    else
    {
      //(sStatus == "complete" || sStatus == "no_survey")
      //status is either complete, no survey, or an unknown value
      console.info("user doesn't want to take survey");
    } //end survey status check

  }  //end cookies accepted
  else
  {
    console.info("client does not accept cookies");
  }


//Functions
function js_include(script)
{
  var script = document.createElement('script');
  script.src = js_path + script;
  script.type = 'text/javascript';
  var head = document.getElementsByTagName('head').item(0);
  head.appendChild(script);
}
function css_include(cssFile)
{
  var cssNode = document.createElement('link');
  cssNode.type = 'text/css';
  cssNode.rel = 'stylesheet';
  cssNode.href = js_path + cssFile;
  cssNode.media = 'screen';
  var headID = document.getElementsByTagName("head").item(0);
  headID.appendChild(cssNode);
}