var browser = new BrowserDetect();
var _http = document.location.protocol;

/***********************************************/
var localSwitch = true;
//var localSwitch = false;  //TO STOP ALL INVITATIONS
/***********************************************/

//If Google Chrome : don't serve
try { if (browser.isChrome) { localSwitch = false; } } catch (Err) { }

/***********************************************/
var _rate = 0;
var _max_rate = 10000;

var url = location.href.toLowerCase();
var _ext = '';

if (url.indexOf('www.intercontinental.com') != -1) {
    _ext = 'interContinental';
    //July 20, 2010: 12%
    _rate = 1400; //14.00%
} else if (url.indexOf('www.ichotelsgroup.com/intercontinental/') != -1) {
    _ext = 'interContinental';
    //July 20, 2010: 12%
    _rate = 1400; //14.00%

} else if (url.indexOf('www.candlewoodsuites.com') != -1) {
    _ext = 'candlewood';
    _rate = 2200;  //22.00%
} else if (url.indexOf('www.ichotelsgroup.com/h/d/cw/') != -1) {
    _ext = 'candlewood';
    _rate = 2200;  //22.00%

} else if ((url.indexOf('www.crowneplaza.com') != -1)) {
    _ext = 'crownePlaza';
    //July 16th 2010: rate 4.50%
    //July 20, 2010: 2.25%
    _rate = 425; //4.25%
} else if ((url.indexOf('www.ichotelsgroup.com/h/d/cp/') != -1)) {
    _ext = 'crownePlaza';
    //July 16th 2010: rate 4.50%
    //July 20, 2010: 2.25%
    _rate = 425; //4.25%

} else if (url.indexOf('www.holidayinn.com') != -1) {
    _ext = 'holidayInn_v2';
    //July 16th 2010: rate 4.80%
    //July 20, 2010: was 0.96%
    _rate = 296; //2.96%
} else if (url.indexOf('www.ichotelsgroup.com/h/d/hi/') != -1) {
    _ext = 'holidayInn';
    //July 16th 2010: rate 4.80%
    //July 20, 2010: was 0.96%
    _rate = 296; //2.96%

} else if (url.indexOf('www.hiexpress.com') != -1) {
    _ext = 'holidayInnExpress_v2';
    //July 16th 2010: rate 2.18%
    //July 20, 2010: was 1.09%
    _rate = 309;  //3.09%
} else if (url.indexOf('www.ichotelsgroup.com/h/d/ex/') != -1) {
    _ext = 'holidayInnExpress';
    //July 16th 2010: rate 2.18%
    //July 20, 2010: was 1.09%
    _rate = 309;  //3.09%

} else if (url.indexOf('www.priorityclub.com/hotels/us/en/reservation?splitreq=endeavor') != -1) {
    _ext = 'priorityClubBeta';  //BETA priority Club
    //Oct.  8th 2010: rate 1.5%
    //Oct. 11th 2010: rate at 10%
    //Oct. 11th 2010: rate at 75%
    //_rate = 7500; //75%
    //Oct. 13th 2010: rate at 100%
    //Oct. 15th 2010: rate at 1.5%
    _rate = 2500; //25%
} else if (url.indexOf('www.priorityclub.com/hotels/us/en/home') != -1) {
    _ext = 'priorityClubBeta';  //BETA priority Club
    //Oct. 15th 2010: rate at 100%
    _rate = 2500; //25%
} else if (url.indexOf('www.priorityclub.com') != -1) {
    _ext = 'priorityClub';
    //July 16th 2010: rate 15%
    _rate = 150;  //1.50%
} else if (url.indexOf('www.ichotelsgroup.com/h/d/pc/') != -1) {
    _ext = 'priorityClub';
    //July 16th 2010: rate 15%
    _rate = 150;  //1.50%
} else if (url.indexOf('priorityclub.com') != -1) {
    _ext = 'priorityClub';  
    _rate = 150;  //1.50%  //September 20th 2010

} else if (url.indexOf('www.staybridge.com') != -1) {
    _ext = 'staybridge';
    //July 20, 2010: was 15%
    _rate = 1600;  //16.00%
} else if (url.indexOf('www.ichotelsgroup.com/h/d/sb/') != -1) {
    _ext = 'staybridge';
    //July 20, 2010: was 15%
    _rate = 1600;  //16.00%
    
} else {
    _rate = 0;
}

/**********************/
/* FOR TESTS ONLY !!! */
//_rate = 0; //STOP ALL INVITATIONS!!!!
//if (url.indexOf('http://qa.') != -1) { _rate = _max_rate; /*100;*/ }
//if (url.indexOf('qa.www.priorityclub.com') != -1) { _rate = _max_rate; }
///**********************/

/**********************/
//display invitation only for English language!
if (url.indexOf('/en/') == -1) { _rate = 0; }
/**********************/
//to change creatives only
//if (_ext == 'priorityClub') {_rate = 0;}
/**********************/

var gJSFile = _http + "//ipinvite.iperceptions.com/Invitations/Javascripts/Layer_749_" + _ext + ".js";

/***********************************************/
//Added to capture the coremetrics ID from cookie
var coreID = '';
try {
    coreID = RC("CoreID6");
    if ((coreID === null) || (coreID === 'undefined')) {
        coreID = 'novalue';
    } //if
} catch (Err) {
} //try

/***********************************************/

function BrowserDetect_version() {
    ua = navigator.userAgent.toLowerCase(); this._string = navigator.userAgent.toLowerCase();
    this.isGecko = (ua.indexOf('gecko') != -1 && ua.indexOf('safari') == -1); this.isKonqueror = (ua.indexOf('konqueror') != -1);
    this.isSafari = (ua.indexOf('safari') != -1); this.isOpera = (ua.indexOf('opera') != -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.isNS = ((this.isGecko) ? (ua.indexOf('netscape') != -1) : ((ua.indexOf('mozilla') != -1) && !this.isOpera && !this.isSafari && (ua.indexOf('spoofer') == -1) && (ua.indexOf('compatible') == -1) && (ua.indexOf('webtv') == -1) && (ua.indexOf('hotjava') == -1)));
    this.versionMinor = parseFloat(navigator.appVersion);
    if (this.isGecko && !this.isMozilla) { this.versionMinor = parseFloat(ua.substring(ua.indexOf('/', ua.indexOf('gecko/') + 6) + 1)); }
    else if (this.isMozilla) { this.versionMinor = ua.substring(ua.indexOf('rv:') + 3, ua.indexOf('rv:') + 8); }
    else if (this.isIE && this.versionMinor >= 4) { this.versionMinor = ua.substring(ua.indexOf('msie ') + 5, ua.indexOf('msie ') + 8); }
    else if (this.isKonqueror) { this.versionMinor = parseFloat(ua.substring(ua.indexOf('konqueror/') + 10)); }
    else if (this.isSafari) { this.versionMinor = parseFloat(ua.substring(ua.lastIndexOf('safari/') + 7)); }
    else if (this.isOpera) { this.versionMinor = parseFloat(ua.substring(ua.indexOf('opera') + 6)); }
    if (this.isFirefox) { this.versionMinor = ua.substring(ua.indexOf('firefox') + 8, ua.indexOf('firefox') + 13); }
    if (this.isNS) { var ua_arr = ua.split(" "); for (a in ua_arr) { if (ua_arr[a].indexOf("net") == 0) { versionMinor = ua_arr[a].split("/")[1]; } } }
    this.versionMajor = parseInt(this.versionMinor);
}


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=/";
}

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() {

    /*
    //Survey Cookie
    var sName = "PNcookie356";
    var sValue = "PNcookie356_COOKIE";
    var sValueRet = "???"
	
	//Test Cookie
    var gCookieName="AVCOOKIETEST";
    var gCookieValue="AV_TESTING_COOKIE";
    var gCookieValueRet="???"

	//%---------------------------------------------
    _ext = 'interContinental';
    //Survey Cookie
    var sName = "PNcookie478";
    var sValue = "PNcookie478_COOKIE";
    var sValueRet = "???"
	
	//Test Cookie
    var gCookieName="AVCOOKIETEST";
    var gCookieValue="AV_TESTING_COOKIE";
    var gCookieValueRet="???"
    */
    
    if (_ext == 'interContinental') {
        //survey cookie
        var sCName = "PNcookie478"; var sCVal = "PNcookie478_COOKIE";
        //test cookie
        var tCVName = "AVCOOKIETEST"; var tCVVal = "AV_TESTING_COOKIE";
    } else {
        //survey cookie
        var sCName = "PNcoe356"; var sCVal = "PNcookie356_COOKIE"; 
        //test cookie
        var tCVName = "AVCOOKIETEST"; var tCVVal = "AV_TESTING_COOKIE"; 
    }
    
    /*
    //survey cookie
    var sCName = "IPERCEPTIONS_749"; var sCVal = "iPe_749_User";
    //test cookie
	var tCVName="IPERCEPTIONS_TEST"; var tCVVal="iPe_Test";
    */

    var tCVValRet; //test cookie value
    var sCValRet; //survey cookie value
    
	//session cookie
	var sCN = "IPE_S_749"; var sCV = "IPE_749_Session"; var sCVR; sCVR = RC(sCN);
	
	CC(tCVName,tCVVal,1);
	tCVValRet = RC(tCVName); 			
	sCValRet = RC(sCName);
	EC(tCVName);
	
	var rndNum= Math.floor ((Math.random()* _max_rate));

	if (rndNum < _rate && 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);
}
