﻿//Test Variables.  Do not change those values.
var bCookie = true;
var bFrequency = true;

//Control Variables
var frequency = 100;
var waitingTime = 0;
var sondageUrl = "http://www.surveygizmo.com/s/208988/optimisation-du-site-d-adviso";
var scriptsUrl = "http://sondage.absolunet.com/adviso/";
var lang = "fr";

var layout = new Array(1);

layout['fr'] = '<div id="WrapperSondage">'+
					'<div id="BarreSondage">'+
						'<div id="InterieurSondage">'+
							'<img src="' + scriptsUrl + 'images/Logo_Adviso.png" alt="Adviso: Aidez-nous à améliorer notre site !" class="LogoAdviso" />'+
							'<p class="TexteInvitation">Après votre visite, prenez quelques minutes pour répondre à notre court sondage !</p>'+
							'<a class="OpenButton BoutonParticiper" href="#">Je veux participer</a>'+
							'<p class="TexteRefuser DeclinedButton"><a href="#">Non merci.</a></p>'+
							'<a class="CloseButton" href="#"><img src="' + scriptsUrl + 'images/Bt_Fermer.gif" alt="Fermer" class="BoutonFermer" /></a>'+
						'</div>'+
					'</div>'+
				'</div>'

//Global Variables
var idInterval;
var params = getUtmValues();

var timeObj = new Date;
var unixTimeMs = timeObj.getTime();
var unixTime = parseInt(unixTimeMs / 1000);
var GASID = secondTracker._visitCode() + '-' + unixTime;

var urlParams = "?source=" + params['source'] + "&campaign=" + params['campaign'] + "&term=" + params['term'] + "&medium=" + params['medium'] + "&gasid=" + GASID;

//Inclusions
//importJavascript("includes/js/jquery.js");
importCss("includes/site.css", "screen");

//Functions To Edit
function openSondage() {
	valueCookie = returnSessionCookie();
	if(valueCookie == -1)
		jQuery("#WrapperSondage").css("display", "block");
	else {
		jQuery("#WrapperSondage").css("bottom", "-125px");
		jQuery("#WrapperSondage").css("display", "block");
		jQuery("#WrapperSondage").animate({ bottom: 0}, 400, "swing");
	}
	secondTracker._trackPageview('/sondage/bandeau');
}

function closeSondage() {
	jQuery("#WrapperSondage").animate({ bottom: "-125px"}, 250, "swing", function(){
		jQuery(this).remove();															  
	});
	secondTracker._trackPageview('/sondage/fermer');
}

function closeSondageNoTrace() {
	jQuery("#WrapperSondage").animate({ bottom: "-125px"}, 250, "swing", function(){
		jQuery(this).remove();															  
	});
}

function yesClick() {
	secondTracker._trackPageview('/sondage/participer/' + GASID);
}

//Functions
function initSondage() {
	if(lang != "") {
		if (!checkCookie() || !bCookie) {
			if (getFrequency() || !bFrequency) {
				idInterval = setInterval('startPopup()', 1000);
			}
		}
	}
}

function openPopup() {
	jQuery("body").append(layout[lang]);
	jQuery(".CloseButton").click(function() {
		closeSondage();
		setSessionCookie("-10");
		return false;
	});
	jQuery(".DeclinedButton").click(function() {
		closeSondage();
		setCookie("-10");
		return false;
	});
	jQuery(".OpenButton").click(function() {
		openNewWindow();
		setCookie("-10");
		return false;
	});
	openSondage();
	setSessionCookie("-1");
	clearInterval(idInterval);
}

function closePopup() {
    closeSondage();
}

function closePopupNoTrace() {
    closeSondageNoTrace();
}

function startPopup() {
    valueCookie = returnCookie();
    if (valueCookie > 0) {
		setCookie(valueCookie - 1000);
    }
    else
        openPopup();
}

function setCookie(pTime) {
	var date = new Date();
	date.setTime(date.getTime()+(30*24*60*60*1000));
	var expires = "; expires="+date.toGMTString();

    document.cookie = "popup" + "=" + escape(pTime) + expires + "; path=/";
}

function checkCookie() {
    var cookieValue = returnCookie();
	var sessionCookieValue = returnSessionCookie();
    if (cookieValue == "-10" || sessionCookieValue == "-10")
        return true;
    return false;
}

function returnCookie() {
    var c_name = "popup";
    if (document.cookie.length > 0) {
        var c_start = document.cookie.indexOf(c_name + "=");
        if (c_start != -1) {
            c_start = c_start + c_name.length + 1;
            var 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 waitingTime * 1000;
}

function setSessionCookie(pTime) {
    document.cookie = "sessionpop" + "=" + escape(pTime) + "; path=/";
}

function returnSessionCookie() {
    var c_name = "sessionpop";
    if (document.cookie.length > 0) {
        var c_start = document.cookie.indexOf(c_name + "=");
        if (c_start != -1) {
            c_start = c_start + c_name.length + 1;
            var 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 waitingTime * 1000;
}

function getFrequency() {
    divider = Math.floor(100/frequency);
    randomNumber=Math.floor(Math.random()*divider)+1
    if(randomNumber == 1)
    	return true;
    return false;
}

function openNewWindow() {
    window.open(sondageUrl + urlParams,'sondage','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,copyhistory=yes,resizable=yes')
    self.focus();
    closePopupNoTrace();
	yesClick();
}

function importJavascript(src)
{
	document.write("<script type=\"text/javascript\" src=\"" + scriptsUrl + src + "\"></scr" + "ipt>");
}
function importCss(href, media)
{
	document.write('<link rel="stylesheet" type="text/css" media="' + media + '" href="' + scriptsUrl + href + '" />');
}

function setLang(pLang) {
	if(lang == "") {
		lang = pLang;
		initSondage();
	}
}

function _uGC(l,n,s) {
 if (!l || l=="" || !n || n=="" || !s || s=="") return "-";
 var i,i2,i3,c="-";
 i=l.indexOf(n);
 i3=n.indexOf("=")+1;
 if (i > -1) {
  i2=l.indexOf(s,i); if (i2 < 0) { i2=l.length; }
  c=l.substring((i+i3),i2);
 }
 return c;
}

function getUtmValues() {
	var z = _uGC(document.cookie, '__utmz=', ';'); 

	var source  = _uGC(z, 'utmcsr=', '|'); 
	var medium  = _uGC(z, 'utmcmd=', '|'); 
	var term    = _uGC(z, 'utmctr=', '|'); 
	var content = _uGC(z, 'utmcct=', '|'); 
	var campaign = _uGC(z, 'utmccn=', '|'); 
	var gclid   = _uGC(z, 'utmgclid=', '|'); 
	
	if (gclid !="-") { 
		  source = 'google'; 
		  medium = 'cpc'; 
	} 
	
	var csegment = _uGC(document.cookie, '__utmv=', ';'); 
	if (csegment != '-') { 
		  var csegmentex = /[1-9]*?\.(.*)/;
		  csegment    = csegment.match(csegmentex); 
		  csegment    = csegment[1]; 
	} else { 
		  csegment = ''; 
	} 
	
	var utmValues = new Array(6);

	utmValues['source']=source;
	utmValues['medium']=medium;
	utmValues['term']=term;
	utmValues['content']=content;
	utmValues['campaign']=campaign;
	utmValues['segment']=csegment;

	return utmValues;
}

initSondage();