var preventsurvey = false;
var showsurvery ;
if (typeof $ != 'undefined') {
  $('form').submit(function () {
    preventsurvey = true;
  });
}
if (window.location.toString().indexOf('survey-offer') != -1) {
  preventsurvey = true;
}
window.onbeforeunload = function () {
  if (!preventsurvey) {

    preventsurvey = true;
    exitsurvey.show = true;
    exitsurvey.test(); // try forcing survey to show
    return "\n\nWhy not take our 30 second survey and get a further $10 off your order instead?\n\nAll you have to do is complete the survey and the first 25 people to do so will get a further $10 off. Even if you don’t want to or can’t order today we will honor this $10 rebate for any future Rocket Languages purchase.\n\nTo continue navigating away from this page press \"OK\"' or IF YOU WANT TO TAKE THE SURVEY THEN PRESS \"Cancel\". So, what are you waiting for...\n\n";
  }
}
var exitsurvey = {
  show: false,
  timer: 0,
  test: function () {
    var x = exitsurvey
    if (x.show) {
      x.show = false;
      x.getVars();
      x.overlay();
      x.box();
      clearInterval(x.timer);
    }
  },
  init: function () {
    this.timer = setInterval(exitsurvey.test,100);
  },
  overlay: function () {
    var overlay = document.createElement('div');
    overlay.id = 'exitSurveyOverlay';
    overlay.style.width = this.handyvars.winwidth+'px';
    overlay.style.height = this.handyvars.winheight+'px';
    overlay.style.position = 'fixed';
    overlay.style.top = '0';
    overlay.style.left = '0';
    overlay.style.zIndex = '100';
    overlay.style.background = '#000000';
   /* overlay.onclick = this.close; */


    this.setOpacity(overlay,0.7);

    this.append2Body(overlay);
  },
  box: function () {
    var box = $('<div></div>')[0];
    box.id = 'exitSurveyBox';
    box.style.width = '660px';
    box.style.height = '430px';
    box.style.position = 'fixed';
    box.style.padding = '5px';
    box.style.top = (this.handyvars.winheight / 2 - (430 /2))+'px';
    box.style.left = (this.handyvars.winwidth / 2 - (660 /2))+'px';
    box.style.zIndex = '200';
    box.style.background = '#ffffff';

    var clink = $("<a></a>")[0];
    clink.setAttribute("href", "#");
      clink.style.fontFamily = "Arial";
      clink.style.fontSize = "10px";
      clink.style.color = '#666';
      clink.style.cssFloat = "right";
    clink.style.textDecoration = 'none';
    clink.onclick = this.close;

    $(clink).text('CLOSE');
    $(box).append(clink);
    $(box).append($('<img>').attr('src','http://cdn.rocketlanguages.com/common/images/page/rocket-logo.png').css('margin','0 0 0 160px'));
    var h2 = $('<h2></h2>')[0];
      h2.style.fontFamily = "Arial";
      h2.style.fontSize = "20px";
      h2.style.color = '#333';
      h2.style.margin = '10px';
      h2.style.textAlign = 'center';
      $(h2).text('The Rocket Languages 30 Second Survey');

    //var questions = ['Don\'t have time','Too expensive','Product unsuitable','Don\'t have a credit card','Not good value','Other (please state)'];
    var questions = [{
        q: "I don&rsquo;t think I will have enough time",
        a: "You only need 15 minutes practice a day to achieve a conversational level"
      },{
        q: "The language looks too hard to learn",
        a: "Our courses are designed to make learning as easy as possible and keep you motivated"
      },{
        q: "I don&rsquo;t know enough about what’s in the courses",
        a: "Please contact our support using <a href='mailto:support@rocketlanguages.com'>support@rocketlanguages.com</a> to help!"
      },{
        q: "I can’t afford it",
        a: "we do have student rates, so if that’s you let us know"
      },{
        q: "I don’t have a credit card",
        a: "There are other <a href='/paymentoptions.php'>payment options</a>"
      },{
        q: "I don’t think the course represents value for money",
        a: "Our research has shown that you get more course material with Rocket Languages than any other competitors product"
      },{
        q: "Something else is holding me back!",
        a: "Please let us know...<br /><input type='text' name='other' style='width: 95%;' />"
      }];


    $(box).append(h2);

    var para = $('<p></p>')[0];
    para.style.margin = '10px 20px';
    $(para).text("What is stopping you from purchasing a Rocket Languages product today? (Before you got an extra $10 off!)");
    box.appendChild(para);

    var form = $('<form></form>')[0];
    $(form).submit(function () {
      try {
        //pageTracker._trackEvent('Exit Survey', 'Submit', $('#exitSurveyBox').find('input[type=text]:visible').length ? $('#exitSurveyBox').find('input[type=text]').val() : $('#exitSurveyBox').find('[name=select]:checked').val() );
        _gaq.push(['_trackEvent', 'Exit Survey', 'Submit', $('#exitSurveyBox').find('input[type=text]:visible').length ? $('#exitSurveyBox').find('input[type=text]').val() : $('#exitSurveyBox').find('[name=select]:checked').val()]);
        } catch (err) { }
    });
    form.method="post";
    form.style.margin = '20px';
    form.action="/exitsurvey.php";

    for (var i = 0; i < questions.length; i++) {
      var radio = document.createElement('input');
      radio.type = 'radio';
      radio.name = 'select';
      radio.style.marginRight = '5px';
      radio.value = questions[i].q;
      radio.id = 'es_q_'+i;
      $(radio).change(function () {
        var id = '#'+this.id.replace('q','a');
        try {
          //pageTracker._trackEvent('Exit Survey', 'Click', $(this).val() );
          _gaq.push(['_trackEvent', 'Exit Survey', 'Click', $(this).val()]);
        } catch (err) { }
        $('#exitSurveyBox').find('.answer:visible').not(id).slideUp();
        $(id).slideDown();
      }).click(function() { $(this).change() });
      var label = document.createElement('label');
      label.style.display = 'block';
      label.style.margin = '5px';
      $(label).append(radio);
      $(label).append(questions[i].q).click(function () { $(this).find('input[type=radio]').change(); });
      //label.appendChild(document.createElement('br'));
      $(form).append(label);
      $(form).append(
        $('<div></div>').hide()
          .attr('id','es_a_'+i)
          .addClass('answer')
          .css('border','1px solid #999')
          .css('color', '#666')
          .css('background','#f0f0f0')
          .css('padding','5px')
          .css('margin','2px 0px 10px 15px')
          .css('width','auto')
          .html(questions[i].a)
      );
    }
    /*
      var other = document.createElement('input');
      other.type = 'text';
      other.name = 'other';
      other.style.marginRight = '5px';
      other.value = '';
      var label = document.createElement('label');
      label.id = 'exitsurveyOtherInput';
      label.style.display = 'block';
      label.style.margin = '5px';
      label.appendChild(document.createTextNode('Other: '));
      label.appendChild(other);
      this.setOpacity(label,.01);
      //label.appendChild(document.createElement('br'));
      form.appendChild(label); */

      var submit = document.createElement('input');
      submit.type = 'submit';
      submit.value = 'Send';
      submit.style.marginTop = '15px';
      submit.style.padding = '2px';
      form.appendChild(submit);

    box.appendChild(form);




    this.append2Body(box);

  },
  handyvars: {},
  getVars: function () {
    if (window.innerWidth) {
      this.handyvars.winwidth = window.innerWidth;
      this.handyvars.winheight = window.innerHeight;
    }else{
      this.handyvars.winwidth = document.documentElement.clientWidth;
      this.handyvars.winheight = document.documentElement.clientHeight;
    }
  },
  append2Body: function (el) {
    var _body = document.getElementsByTagName('body') [0];
    _body.appendChild(el);
  },
  close: function () {
    window.close();
    var overlay = document.getElementById('exitSurveyOverlay');
    var box = document.getElementById('exitSurveyBox');
    overlay.style.display = 'none';

    box.parentNode.removeChild(box);

    return false;
  },
  setOpacity: function ( el, opaciLevel ) {
      if ( el.style.opacity !== undefined ) {
          el.style.opacity = opaciLevel;
      } else if( el.style.MozOpacity !== undefined ) {
          el.style.MozOpacity = opaciLevel;
      } else if ( el.style.filter !== undefined ) {
          var oplvl = Math.round(opaciLevel*100);
          el.style.filter="alpha(opacity=" + oplvl + ")";
      }
  }
}
exitsurvey.init();
