﻿var PopUnderManager={
	WinProps: "toolbar=yes,location=yes,directories=no,status=yes,scrollbars=yes,resizable=yes,copyhistory=no,width=1000,height=600",
	AllowedCobrands: ["br3","br3_b","br3_c","baw","bhabr","bhn2","blm","bos","bosre","bravd_t","brm_c","brm_sem","cic","cld","crc","crt","csoup","cycl","deal","dls","dmn","fci","gookeyword","hch","igg","kgw","kings","kmov","kvue","lapalma","mchx","mhe","milfn","movebr","mtry","ndaq","nltrack","nscre","nwcn","prj","realfc","rss","_trk","rtrs","rvrt","sae","sam","selcbsmw","selcweb","selexct","sema","semb","sfc2","stb","tnp","treebr","usn","wfaa","whas","wpbf","zlw"],
	URLs: $H({ "cdsandinvestments": "/funnel/cd-investments/cd-investment-results.aspx?local=false&tab=cds&prods=15","checkingandsavings": "/funnel/savings/savings-results.aspx?local=false&prods=33","mortgage": "/funnel/mortgages/?prods=1&points=Zero","refinance": "/funnel/mortgages/?prods=216&points=All" }),
	Params: "ic_id=br3int_popup",
	ExpDays: 1,
	AllowCobrand: true,
	CobrandID: "",
	Location: null,
	PageType: null,
	ProcessPopUnder: function () {
		this.Initialize();
		var query=location.href.toQueryParams([separator="&"]);

		if((this.CobrandID==""||(this.AllowCobrand&&this.AllowedCobrands.indexOf(this.CobrandID)> -1))&&(query.pop!="nopop")) {
			var key=(this.Location==null?"Default":this.Location)+(this.PageType==null?"":this.PageType)
			var count=this.GetCookie(key);
			if(count==null) {
				if(!IsNullorUndefined(this.URLs.get(this.Location))) {
					var pageName=location.protocol+"//"+location.host+this.URLs.get(this.Location);
					var sep=(pageName.indexOf("?")== -1)?"?":"&";
					pageName=pageName+sep+this.Params;
					var exp=new Date();
					exp.setTime(exp.getTime()+(this.ExpDays*2*60*60*1000));
					this.SetCookie(key,1,exp);
					var nWindow=window.open(pageName,key,this.WinProps);
					nWindow.blur();
					window.focus();
				}
			}
			else {
				count++;
				this.SetCookie(key,count,exp);
			}
		}
	},
	Initialize: function () {
		if(PageManager) {
			if(PageManager.PageMetaData) {
				if(PageManager.PageMetaData.ContainsKey('Location')) {
					this.Location=PageManager.PageMetaData.Item('Location').replace(' ','').replace(' ','').toLowerCase();
				}
				if(PageManager.PageMetaData.ContainsKey('PageType')) {
					this.PageType=PageManager.PageMetaData.Item('PageType').toLowerCase();
				}
			}
		}
		if(CookieManager) {
			var pidvalue=CookieManager.Open('brmp').Get('pid');
			if(!IsNullorUndefined(pidvalue)) {
				PID$=new $H(pidvalue);
				if(!IsNullorUndefined(PID$)&&!IsNullorUndefined(PID$.get('p'))&&!IsNullorUndefined(PID$.get('c'))) {
					this.CobrandID=PID$.get('p')+PID$.get('c');
				}
				else if(!IsNullorUndefined(PID$)&&!IsNullorUndefined(PID$.get('p'))) {
					this.CobrandID=PID$.get('p');
				}
			}
		}
	},
	GetCookie: function (key) {
		var arg=key+"=";
		var alen=arg.length;
		var clen=document.cookie.length;
		var i=0;
		while(i<clen) {
			var j=i+alen;
			if(document.cookie.substring(i,j)==arg) {
				return this.GetCookieVal(j);
			}
			i=document.cookie.indexOf(" ",i)+1;
			if(i==0) {
				break;
			}
		}
		return null;
	},
	SetCookie: function (key,value) {
		var argv=this.SetCookie.arguments;
		var argc=this.SetCookie.arguments.length;
		var expires=(argc>2)?argv[2]:null;
		var path="/";
		var domain=(argc>4)?argv[4]:null;
		var secure=(argc>5)?argv[5]:false;
		document.cookie=key+"="+escape(value)+((expires==null)?"":("; expires="+expires.toGMTString()))+
				((path==null)?"":("; path="+path))+
				((domain==null)?"":("; domain="+domain))+
				((secure==true)?"; secure":"");
	},
	GetCookieVal: function (offset) {
		var endstr=document.cookie.indexOf(";",offset);
		if(endstr== -1) {
			endstr=document.cookie.length;
		}
		return unescape(document.cookie.substring(offset,endstr));
	}
}
PopUnderManager.ProcessPopUnder();

