//***Code Is Intellectual Property Of crmmetrix inc.(Copyright 2011).
//***The use os this code is permitted for this project only as per the license agreement.
//***Code Has Been Developed For The Sole Purpose Of crmmetrix inc./Varilux Project.
//***Licensed For crmmetrix inc./Varilux Project.

/*Browser - OS to enabled or disables*/
var CtrixBrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{
			string: navigator.userAgent,
			subString: "Chrome",
			identity: "Chrome"
		},
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari",
			versionSearch: "Version"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [  
			{
			string: navigator.userAgent,
			subString: "iPod",
			identity: "iPod"
		}
		,
		{
			string: navigator.userAgent,
			subString: "iPad",
			identity: "iPad"
		}
		,

		{
			string: navigator.userAgent,
			subString: "Windows CE",
			identity: "Windows CE"
		}
		,
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			   string: navigator.userAgent,
			   subString: "iPhone",
			   identity: "iPhone/iPod"
	    },
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]
};

CtrixBrowserDetect.init();

var Crmmetrixbr=CtrixBrowserDetect.browser;
/*var brv=CtrixBrowserDetect.version;*/
var CrmmetrixOS=CtrixBrowserDetect.OS;

if((Crmmetrixbr=="Firefox"||Crmmetrixbr=="Explorer")&&(CrmmetrixOS=="Linux"||CrmmetrixOS=="Mac"||CrmmetrixOS=="Windows"))
{
// ----Declare Variables Below----replace all "" values or any value below if applicable
var maindirurlpopcrm="http://www.crm-metrix.fr/projects/2009/sc/vrluxscfrq49/sitecrm";



//-------Declare the intercept rates (Should add up to 100%)
var exit=100;     //Enter percent
var entry=0;    //Enter percent
var nosurvey=0; //Enter percent
var exiturl= maindirurlpopcrm+"/stealth.htm"; //enter the stealth.htm path
var clientnom="Varilux"; // declare the client name here
var clientsite="varilux.com"; // declare the clients website here
 
var insec=2;//Timer that delays the show of pop-in (in seconds)
var cookieexp_seen=3*24*30; //Cookie for 3 months
var cookieexp_enabled=20; //Cookie for 20 hours
var cookieexp_no=20*24; //Cookie for 20 days 
var cookieexp_donex=5; //Cookie for 5 hours
var cookieexp_nosurvey=24; //Cookie for 1 day

var wtx=(screen.width);
var htx=(screen.height);
var wtx2=((wtx-400)*0.5);
var htx2=100; // From 10 to 150

var pid="vrluxscfrq49";
//var sniffernet=1;

//############### FOR TEST LANG ##################
if(typeof(crm_popin_lang) != "undefined")
{
	var language=crm_popin_lang;
}
else
{
	var language="fr";
}
//################################################

var tab_language=new Array('fr','en');

//Note : To deactivate project assign sniffernet=2;
//The below sniffernet code is to centrally activate,deactivate the project.
//The survey is by default disabled and its only enabled if the site visitor has cookies enabled
//on his system

//-----------------------------------------------------FUNCTIONS---DO NOT EDIT BELOW THIS

function getDomain(dot)
{	
	var myDomain="";

	var pathname=window.location.host;
		
	var pos=pathname.lastIndexOf('.');
	var pathname2=pathname.substring(0,pos-1);
	if (dot)
	{
		pos=pathname2.lastIndexOf('.');
	}
	else
	{
		pos=pathname2.lastIndexOf('.')+1;
	}
		
	myDomain=pathname.substring(pos,pathname.length);
	
	
	
	//for the urls with ip
	var firstcar='';
	
	if (!dot)
	{
		firstcar=myDomain.substring(0,1);
	}
	
	else
	{
		firstcar=myDomain.substring(1,2);
	}
	
	//alert(firstcar);
	
	if (!isNaN(firstcar))
	{
		pos=pathname2.lastIndexOf('.');
		pathname2=pathname2.substring(0,pos-1);
		
		pos=pathname2.lastIndexOf('.');
		pathname2=pathname2.substring(0,pos-1);
		
		pos=pathname2.lastIndexOf('.');
		pathname2=pathname2.substring(0,pos-1);
				
		myDomain=pathname.substring(pos,pathname.length);	
		
		//alert('ip '+myDomain+ ' point ? '+dot);	
	}

	//alert('sitecrm cookie '+myDomain+ ' point ? '+dot);
	return myDomain;
}

var site_domain=getDomain(false);

if(site_domain=="crm-metrix.fr")
{
	var sniffernet=1;
}
else
{
	var sniffernet=2;
}


function writeCookie(name, value, hours)
{
  var expire = "";
  if(hours != null)
  {
    expire = new Date((new Date()).getTime() + hours * 3600000);
    expire = "; expires=" + expire.toGMTString();
  }
  document.cookie = name + "=" + escape(value) + expire +";path=/;";
}


function readCookie(name)
{
  var cookieValue = "";
  var search = name + "=";
  if(document.cookie.length > 0)
  { 
    offset = document.cookie.indexOf(search);
    if (offset != -1)
    { 
      offset += search.length;
      end = document.cookie.indexOf(";", offset);
      if (end == -1) end = document.cookie.length;
      cookieValue = unescape(document.cookie.substring(offset, end))
    }
  }
  return cookieValue;
}

function GetParam(name)
{
  var start=location.search.indexOf("?"+name+"=");
  if (start<0) start=location.search.indexOf("&"+name+"=");
  if (start<0) return '';
  start += name.length+2;
  var end=location.search.indexOf("&",start)-1;
  if (end<0) end=location.search.length;
  var result=location.search.substring(start,end);
  var result='';
  for(var i=start;i<=end;i++) {
    var c=location.search.charAt(i);
    result=result+(c=='+'?' ':c);
  }
  return unescape(result);
}


//------To block the survey for people who are visiting from http://video.msn.com/v/us/v.htm,www.fusionamerica.com/zsitecrm.asp

writeCookie("crm_cookieEnabled","1",cookieexp_enabled);	//writing cookie to check if cookies are enabled or disabled.
var cookieEnabled=readCookie("crm_cookieEnabled");	//reading the cookie value to see if cookie is written or not.

if(cookieEnabled=="1")
{

	//-------Determining whether this is the first visit to tagged page. Tracking the referer url.
	var visitctr=readCookie("cntr");
		
	if (visitctr!="1")
	{
		//-------Capturing Referring URL
		if (document.referrer&&document.referrer!="")
		{
			writeCookie("refer",document.referrer,cookieexp_enabled);			
		}
		writeCookie("cntr","1",cookieexp_enabled);
	}

}
else
{
	sniffernet=2;
}

//--------------------------------------------------------------------------------------------------------------------------------
//-------------------------------------- DO NOT EDIT -----------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------------------------------------

function hideAd(divId) 

{ 
	if (document.layers) document.layers[divId].visibility = 'hide';
	else if (document.all) document.all[divId].style.visibility = 'hidden';
	else if (document.getElementById) document.getElementById(divId).style.visibility = 'hidden';
}

function showAd(divId) 
{ 
	if (document.layers) document.layers[divId].visibility = 'show';
	else if (document.all) document.all[divId].style.visibility = 'visible';
	else if (document.getElementById) document.getElementById(divId).style.visibility = 'visible';
}

function create(divId) 
{ 
	if (document.layers) document.layers[divId].visibility = 'show';
	else if (document.all) document.all[divId].style.visibility = 'visible';
	else if (document.getElementById) document.getElementById(divId).style.visibility = 'visible';
}

function adDown(divId) 
{ 
	state=typeof topPos;
	var fposi;
	if (divId=='bg')
	{
	fposi=0;
	}
	else
	{
	fposi=80;
	}

	if(state=='undefined') {topPos=-440;}
	if(topPos < fposi)
	{ 
		topPos+=40;
		if (document.layers)
		{
			document.layers[divId].top = topPos;
		}
		else
		{
			if(document.all)
			{
				document.all[divId].style.top = topPos;
			}
			else
			{
				if(document.getElementById)
				{
					document.getElementById(divId).style.top = topPos;
				}
			}
		}
		
		{setTimeout("adDown('"+divId+"');",5);}
	 }
}


function stealthfx(idx)
{   
	var win2=window.open(maindirurlpopcrm+'/stealth'+idx+'.htm','stealth','height=30,width=60,scrollbars=yes,toolbars=no,location=no,resizable=yes');

	if(win2!=null)
	{
		win2.blur();
		writeCookie("crmseen","seen",cookieexp_seen);
	}
	
	hideAd('stealth_pop_'+idx);
}

function crmlinkfx()
{
popupWin = window.open('http://fr.crmmetrix.com/','open_window','menubar, toolbar, location, directories, status, scrollbars, resizable, dependent, width=640, height=480, left=100, top=100')
}


function nosurveyfx(divId)
{
	hideAd(divId);
	writeCookie("crmseen","seen",cookieexp_no);
}


	var thiscookie=readCookie("crmseen");


function changelang(status,divId)
{
	var language_popin=language_popin='stealth_pop_'+divId;
	var language_imgfx=language_imgfx='exit_'+divId;
	
	showAd(language_popin);
	adDown(language_popin);
	JSFX_FloatDiv(language_popin, wtx2,htx2).floatIt();
	if(readCookie("donex1") != 1)
	{
		imgfx(divId); 
		writeCookie("donex1",1,cookieexp_donex);
	}
	
}

if (parseInt(navigator.appVersion) >= 4 ) 
{ 

var status='stealth';

//------------Popin Français-FR--------------------------
document.write('<div class="divcrm" id="stealth_pop_fr">');
document.write('<div class="divcrm" id="crm_headercrm">');
//document.write('<a href="javascript:hideAd(\'stealth_pop_fr\');changelang(\''+status+'\',\'en\');"><img src="'+maindirurlpopcrm+'/image/en.png" class="c2" / ></a>');
document.write('</div>');
document.write('<div id="crm_content" class="divcrm">');
document.write('<div id="crm_fr_content" class="divcrm">');
document.write('<span id="crm_fr_title">Bonjour !</span>');
document.write('<span id="crm_fr_texte">Aidez-nous &agrave; am&eacute;liorer le site '+clientsite+' en nous donnant votre avis &agrave; la fin de votre visite.<br/><br/>');
document.write('Merci d&acute;avance pour votre participation !<br /><br />');
document.write('</span>');
document.write('<span id="crm_fr_sign">L&acute;&eacute;quipe '+clientnom+'</span>');
document.write('</div>');
document.write('<div id="crm_bouton_rep">');
document.write('<a href="javascript:stealthfx(\'fr\');"><img alt="Je participe" src="'+maindirurlpopcrm+'/image/okButton.gif"/></a>');
document.write('<span><a href="javascript:stealthfx(\'fr\');" id="crm_a1">Je participe</a></span>');
document.write('</div>');
document.write('<div id="crm_bouton_non">');
document.write('<a href="javascript:nosurveyfx(\'stealth_pop_fr\');"><img alt="Non merci" src="'+maindirurlpopcrm+'/image/noButton.gif"/></a>');
document.write('<span><a href="javascript:nosurveyfx(\'stealth_pop_fr\');" class="noli" id="crm_a2">Non merci</a></span>');
document.write('</div>');
document.write('</div>');
document.write('<div id="crm_footercrm">');
document.write('<span>Toutes vos r&eacute;ponses resteront strictement confidentielles et les informations recueillies seront utilis&eacute;es uniquement dans le cadre de cette &eacute;tude.<br/></span>');
document.write('</div>');
document.write('</div>');

//------------Popin Anglais-EN--------------------------
document.write('<div class="divcrm" id="stealth_pop_en">');
document.write('<div class="divcrm" id="crm_headercrm">');
//document.write('<a href="javascript:hideAd(\'stealth_pop_en\');changelang(\''+status+'\',\'fr\');"><img src="'+maindirurlpopcrm+'/image/fr.png" class="c2" / ></a>');
document.write('</div>');
document.write('<div id="crm_content" class="divcrm">');
document.write('<div id="crm_fr_content" class="divcrm">');
document.write('<span id="crm_fr_title">Hello!</span>');
document.write('<span id="crm_fr_texte">Help us improve the '+clientnom+' site by giving us your opinion at the end of your visit.<br/><br/>Thank you for agreeing to take part !<br/><br/>');
document.write('</span>');
document.write('<span id="crm_fr_sign">The '+clientnom+' team</span>');
document.write('</div>');
document.write('<div id="crm_bouton_rep_en">');
document.write('<a href="javascript:stealthfx(\'en\');"><img alt="I will take part" src="'+maindirurlpopcrm+'/image/okButton.gif"/></a>');
document.write('<span><a href="javascript:stealthfx(\'en\');" id="crm_a1">I will take part</a></span>');
document.write('</div>');
document.write('<div id="crm_bouton_non_en">');
document.write('<a href="javascript:nosurveyfx(\'stealth_pop_en\');"><img alt="No thank you" src="'+maindirurlpopcrm+'/image/noButton.gif"/></a>');
document.write('<span><a href="javascript:nosurveyfx(\'stealth_pop_en\');" class="noli" id="crm_a2">No thank you</a></span>');
document.write('</div>');
document.write('</div>');
document.write('<div id="crm_footercrm">');
document.write('<span>All your replies will remain strictly confidential, and the information collected will be used solely within the context of this study.</span>');
document.write('</div>');
document.write('</div>');


		 if (document.layers) document.layers.stealth_pop_fr.left = 10;
		 else if (document.all) document.all.stealth_pop_fr.style.left = 10;
		 else if (document.getElementById) document.getElementById("stealth_pop_fr").style.left = 10;
		  
		 if (document.layers) document.layers.stealth_pop_fr.zIndex = 1000000;
		 else if (document.all) document.all.stealth_pop_fr.style.zIndex = 1000000;
		 else if (document.getElementById) document.getElementById("stealth_pop_fr").style.zIndex = 1000000;
		
		 hideAd("stealth_pop_fr");
		 
		 if (document.layers) document.layers.stealth_pop_en.left = 10;
		 else if (document.all) document.all.stealth_pop_en.style.left = 10;
		 else if (document.getElementById) document.getElementById("stealth_pop_en").style.left = 10;
		  
		 if (document.layers) document.layers.stealth_pop_en.zIndex = 1000000;
		 else if (document.all) document.all.stealth_pop_en.style.zIndex = 1000000;
		 else if (document.getElementById) document.getElementById("stealth_pop_en").style.zIndex = 1000000;
		
		 hideAd("stealth_pop_en");		 

}
//--------------------------------------------Floating code------------------------------------
//--------------------------------------------Pl do not modify it------------------------------

var ns = (navigator.appName.indexOf("Netscape") != -1);
var d = document;
function JSFX_FloatDiv(id, sx, sy)
{
	var el=d.getElementById?d.getElementById(id):d.all?d.all[id]:d.layers[id];
	var px = document.layers ? "" : "px";
	window[id + "_obj"] = el;
	if(d.layers)el.style=el;
	el.cx = el.sx = sx;el.cy = el.sy = sy;
	el.sP=function(x,y){this.style.left=x+px;this.style.top=y+px;};

	el.floatIt=function()
	{
		var pX, pY;
		pX = (this.sx >= 0) ? 0 : ns ? innerWidth : 
		document.documentElement && document.documentElement.clientWidth ? 
		document.documentElement.clientWidth : document.body.clientWidth;
		pY = ns ? pageYOffset : document.documentElement && document.documentElement.scrollTop ? 
		document.documentElement.scrollTop : document.body.scrollTop;
		if(this.sy<0) 
		pY += ns ? innerHeight : document.documentElement && document.documentElement.clientHeight ? 
		document.documentElement.clientHeight : document.body.clientHeight;
		this.cx += (pX + this.sx - this.cx)/8;this.cy += (pY + this.sy - this.cy)/8;
		this.sP(this.cx, this.cy);
		setTimeout(this.id + "_obj.floatIt()", 40);
	}
	return el;
}


//-----------------------------End of floating code-------------------------------------

//Timer function
// to declare timer example - for 10 minutes assign secs = 600
var secs = insec;
var timerID = null;
var timerRunning = false;
var delay = 1000;

function timerfx()
{ 
	
	if (secs==0)
    {
        if(timerRunning)
        clearTimeout(timerID);
    	timerRunning = false;
		recontact_crmfx();
		window.onerror=null;
    }
    else
    {
        //self.status = secs;
        secs = secs - 1;
        timerRunning = true;
        timerID = self.setTimeout("timerfx()", delay);
    }
}

if (sniffernet==1)
{
	timerfx();
	document.write("<img id='imgx' height='0' width='0'>");
}

function recontact_crmfx()
{
	
	//Browser Sniffer
	var agt=navigator.userAgent.toLowerCase();
	var client=(agt.indexOf("msie"));

	//reading popseen cookie to make sure the user hasn't taken the survey within past 6 months
	var thiscookie=readCookie("crmseen");
	if(thiscookie!="seen")
	{

		//***Random Number Generated For Control Sample / Test Sample.
		rndNumber=readCookie("rndNumber");
		var lang="";
		var x;
		x=Math.random()*100;
		donex1=readCookie("donex1");
			
		for (i=0;i<tab_language.length;i++)
		{
			if (language==tab_language[i])
				lang=tab_language[i];
		}
		
		var language_exit_popin='stealth_pop_'+lang;
		var language_exit_imgfx='exit_'+lang;
				
		//****************
		//***Exit Survey Interception Rate.		
	
		if (x>(100-exit))
		{
			showAd(language_exit_popin);
			adDown(language_exit_popin);		 
			JSFX_FloatDiv(language_exit_popin, wtx2,htx2).floatIt();
			if(readCookie("donex1") != 1)
			{
				imgfx(language);
				writeCookie("donex1",1,cookieexp_donex);
			}
		}

		if (x>entry && x<=(100-exit))
		{
			//writing cookie which expires in 24 hrs
			writeCookie("crmseen","seen",cookieexp_nosurvey);
		}
	}
}

//------------------------------------------------------------------------------------------------------------
//-------------------------- CRMMETRIX TRACKING CODES - DO NOT MODIFY ----------------------------------------
//------------------------------------------------------------------------------------------------------------
function imgfx(x)
{

	var elem = document.getElementById("imgx");
	elem.src = "http://www.crm-metrix.fr/projects/invitetrack.asp?project=exitpopin_vrluxsc"+x+"q49_NEW";
}

function loadStyleSheet(file)
{
	// Create script DOM(Document Object Model) element
	var script = document.createElement('link');
	script.rel = 'stylesheet';
	script.type = 'text/css';
	script.href = file;

	// Alert when the script is loaded
	if (typeof(script.onreadystatechange) == 'undefined') // W3C
		script.onload = function(){ this.onload = null;  };
	else // IE
		script.onreadystatechange = function(){ if (this.readyState != 'loaded' && this.readyState != 'complete') return; this.onreadystatechange = null;  }; // Unset onreadystatechange, leaks mem in IE

	// Add script DOM(Document Object Model) element to document tree
	document.getElementsByTagName('head')[0].appendChild(script);
}

loadStyleSheet(maindirurlpopcrm+"/style.css");


} /*End enable for the platform*/
