﻿var browser = new BrowserDetect(); 
var localSwitch = true;
//var localSwitch= false;
var _http = document.location.protocol;

/***********************************************/
var url = document.location.href;

//Parsing URL to get the country
var country = "other";  //default value
if (url.indexOf('india') > -1) { country = "india"; }

/***********************************************/
//Parsing URL to get the page
var ipe_page = "other";  //default value
if ((url.indexOf('secure.vonage.com/vonage-web/dashboard/index.htm') > -1) || 
    (url.indexOf('secure.vonage.com/webaccount/') > -1)) {
    ipe_page = 'OnlineAccount';
} else if (url.indexOf('contactus') > -1) { 
    ipe_page = 'ContactUs';
} else if (url.indexOf('support.vonage.com') > -1) {
    ipe_page = 'Support';
} else if(/www\.vonage\.com\/?$/.test(url) ||
        /www\.vonage\.com\/?\?/.test(url)) {   //www.vonage.com or www.vonage.com/ or www.vonage.com?... or www.vonage.com/?...
    ipe_page = 'General';
} else if(url.indexOf('www.vonage.com/how_vonage_works/') > -1 ||
          url.indexOf('www.vonage.com/how_vonage_works_adapters/') > -1 ||
          url.indexOf('www.vonage.com/how_vonage_works_faq/') > -1) {
    ipe_page = 'HowWorks';
} else if(url.indexOf('www.vonage.com/us-canada-calling-plans/') > -1 ||
          url.indexOf('www.vonage.com/world-calling-plans/') > -1) {
    ipe_page = 'Calling';
} else if(url.indexOf('www.vonage.com/features.php') > -1) {
    ipe_page = 'Features';
}

//Added Dec. 5 2011
//if(url.indexOf('support.vonage.com/app/ask') > -1) {
//    ipe_page = 'ContactUs';
//}

/*
//} else if ((url == 'http://www.vonage.com/')
//        || (url == 'https://www.vonage.com/') 
//        || (url.indexOf('http://www.vonage.com/?refer_id=') > -1)) {
//    ipe_page = 'Homepage';
*/

/***********************************************/

//don't show invitation for pages from 'http://espanol.vonage.com/'
if (url.indexOf(_http + '//espanol.vonage.com') > -1) { localSwitch = false; }
/***********************************************/

//If Google Chrome : don't serve
try { if (browser.isChrome) { localSwitch = false; } } catch (Err) { }

//var gJSFile = "javascript/Layer_XXX.js";
var gJSFile = _http + "//ipinvite.iperceptions.com/Invitations/Javascripts/Layer_722.js";

function BrowserDetect() {
   ua = navigator.userAgent.toLowerCase(); this._string = navigator.userAgent.toLowerCase();
   this.isOpera= (ua.indexOf('opera') != -1);  this.isGecko= (ua.indexOf('gecko') != -1 && ua.indexOf('safari') == -1);
   this.isIE= (ua.indexOf('msie') != -1 && !this.isOpera && (ua.indexOf('webtv') == -1) ); 
   this.isMozilla= (this.isGecko && ua.indexOf('gecko/') + 14 == ua.length);
   this.isFirefox = (ua.indexOf('firefox') != -1);
   this.isChrome = (ua.indexOf('chrome') != -1);
}

function lScript(file) {
	var script = document.createElement('script');
	script.type = 'text/javascript'; script.src = file;	
	if (typeof(script.onreadystatechange) == 'undefined') 
	    script.onload = function() { this.onload = null; }; 
	else  
	    script.onreadystatechange = function() { if (this.readyState != 'loaded' && this.readyState != 'complete') return; this.onreadystatechange = null;  }; 
	
	document.getElementsByTagName('head')[0].appendChild(script);
}

function CC(name,value,days) {
	if (days) { var expDate = new Date(); expDate.setTime(expDate.getTime()+(days*24*60*60*1000)); var expires = "; expires="+expDate.toUTCString(); }	
	else var expires = ""; document.cookie = name+"="+value+expires+"; path=/;"
    //Client requested NOT to have the cross-domain.  Dec 13, 2011
    //"domain=" + document.domain.split(".").splice(-2).join(".");
}

function RC(NameOfCookie) {
    if (document.cookie.length > 0) {
        begin = document.cookie.indexOf(NameOfCookie+"=");
        if (begin != -1) {
            begin += NameOfCookie.length+1; end = document.cookie.indexOf(";", begin);          
            if (end == -1) end = document.cookie.length;
            return unescape(document.cookie.substring(begin, end)); 
        }
    }
    return null;
}
	
function EC(name) { CC(name,"",-1); }

function Exec() 
{
	var sCName = "IPERCEPTIONS_722"; var sCVal = "iPe_722_User";
	var sCN = "IPE_S_722"; var sCV = "IPE_722_Session"; var sCVR; sCVR= RC(sCN);
	var sCValRet; var tCVName="IPERCEPTIONS_TEST"; var tCVVal="iPe_Test"; var tCVValRet;
	CC(tCVName,tCVVal,1);
	tCVValRet = RC(tCVName); 			
	sCValRet = RC(sCName);
	EC(tCVName);
	var rndNum= Math.floor ((Math.random()*100));

	//Mar 03, 2010: 10%
    //December 8, 2011: 10%
	if (rndNum < 10 && tCVValRet !== null && sCVR === null && sCValRet === null) {
    		CC(sCName, sCVal, 90); lScript(gJSFile);
	}

   if (sCVR == null) {CC(sCN, sCV);}
}



if (localSwitch) {
    var GJS = _http + "//ipinvite.iperceptions.com/Invitations/Javascripts/Layer_Global_aicollect.js"; lScript(GJS);
}	
	



