//gets revsci info
if (document.domain != '') {
document.write('<s\cript src="http://js.revsci.net/gateway/gw.js?csid=J05531"></s\cript>');
}

//this function puts a semi-colon on the end if there is none
//and takes one off the front if there is one
//-TM 03/29/07
function semiColonizer(varString)
{
	if(varString=='' || varString == ';') return '';
	
	//if the first character is a semi-colon, remove it
	if(varString.substring(0,1) == ";")
	{
		varString=varString.substring(1,varString.length)
	}
	if(varString.substring(varString.length-1,varString.length) != ";")
	{
		varString+=";"
	}
	//if the last character is not a semi-colon, add it
	return	varString
}

//generates random number (but not ord string) once for entire page
function RandomInt()
{
	if (typeof this.randReturnValue == 'undefined')
	{
		this.randReturnValue = Math.random() * 1000000000000000000;
    } 

	return this.randReturnValue
}

//generates ord string once for entire page
function ord()
{	
	if (typeof this.ordReturnValue == 'undefined')
	{	
		this.ordReturnValue = "ord=" + RandomInt();
    } 
	return this.ordReturnValue
}

//checks for dfpcomp variable. if it exists, return dfpcomp as a keyvalue.
function dfpComp()
{
	if(typeof this.dfpReturnValue == 'undefined' || this.dfpReturnValue == '')
	{	
		if(typeof dfpcomp != 'undefined')
		{	
			this.dfpReturnValue = 'dfpcomp=' + dfpcomp + ";";
		}
		else
		{
			this.dfpReturnValue = '';
		}
	}
	return this.dfpReturnValue;
}

function pageId()
{
	if(typeof this.idReturnValue == 'undefined')
	{
		if(site!='slate')
		{
			var url = document.location.href;
			url = url.toLowerCase();
			var urlarray = url.split('/');
			var tail = urlarray[urlarray.length -1];
			if (tail.indexOf('nav=') != -1)
			{
				tail = tail.substring(0,tail.indexOf('nav='));
			}
			var illegals = ['?no_interstitial','?test_ads','?template_test','?debugadcode','?pageidtest','?','=','/','\\',':',';',',','*','(',')','&','$','%','@','!','^','+',' ','[',']','{','}','.html','.htm','.',];
			for (i=0;i<illegals.length;i++)
			{
				sRE = new RegExp('(\\' + illegals[i] + ')', 'g');
				tail = tail.replace(sRE,"");
			}
			if (tail == 'index' || tail == '')
			{
				tail = urlarray[urlarray.length -2];
			}
			for(i=1;i<urlarray.length;i++)
			{
				if(urlarray[i] == 'entry')
				{
					tail = urlarray[urlarray.length -4]+'_'+tail;
				}
			}
			this.idReturnValue = 'pageId='+tail;
			if (this.idReturnValue.length > 55)
			{
				this.idReturnValue = this.idReturnValue.substring(0,55);
			}
		}
		else
		{	this.idReturnValue = "pageId=";
			slateURL = location.href;
			if(slateURL.match('view'))
			{
				this.idReturnValue += slateURL.split('/')[6]
			}
			else
			{	
				if(slateURL.split('/')[4] == null || typeof slateURL.split('/')[4] == 'undefined')
				{
					this.idReturnValue += 'www.slate.com'
				}
				else
				{
					this.idReturnValue += slateURL.split('/')[4]
				}
			}
		}
	}
	return this.idReturnValue + ";"
}


//if revsci array exists, creates revsci keyvalues
function revsci()
{	
	if (typeof rsinetsegs != 'undefined' && (typeof this.revsciReturnValue == 'undefined' || this.revsciReturnValue == '') )
	{
		this.revsciReturnValue='';
		for(i=0;i<rsinetsegs.length;i++)
		{
			this.revsciReturnValue += "rs="+rsinetsegs[i].replace("J05531_","j")+";";
		}
	}
	else if (typeof this.revsciReturnValue=='undefined')
	{
		this.revsciReturnValue='';
	}
	
	return this.revsciReturnValue
}

//creates debugAdCode box for each ad spot
function debugTextArea(ac)
{
  var re = new RegExp("\"","g") ;
  var ac = ac.replace(re,"\\\"");
  var re = new RegExp("<","g") ;
  var ac = ac.replace(re,"<\\");
  document.write('<div style="position:relative;float:left;z-index:1000000000">');
  document.write('<div style="text-align:left;text-transform:none;letter-spacing:normal;line-spacing:normal;padding:8px;position:absolute:top:0px;left:0px;width:300px;background-color:#FFAA00;color:#770000;font-family:verdana;font-size:9px;word-wrap:break-word;text-wrap:unrestricted;overflow:scroll">');
  document.write(ac+'<br/>');
  var re = new RegExp("/adj/","g")
  var ac = ac.replace(re,"/adi/")
  document.write('<a style="color:#111177;font-weight:bold;text-decoration:none" target="_blank" href="' + ac + '">Click Here To See The Ad Call In Its Own Window</a>')
  document.write('</div></div>')
}

//getCookie function
function getCookie(name) {
	var cookie = " " + document.cookie;
	var search = " " + name + "=";
	var setStr = null;
	var offset = 0;
	var end = 0;
	if (cookie.length > 0) {
		offset = cookie.indexOf(search);
		if (offset != -1) {
			offset += search.length;
			end = cookie.indexOf(";", offset)
			if (end == -1) {
				end = cookie.length;
			}
			setStr = unescape(cookie.substring(offset, end));
		}
	}
	return(setStr);
}

//returns size if it isn't a flex

function wpFlex(flexBoolean,size)	
{	
	this.flexAndSizeReturnValue = '';
	if (flexBoolean==false)
	{
	this.flexAndSizeReturnValue = 'sz='+size+';'
	}
	return this.flexAndSizeReturnValue
}

//setcookie function
function setCookie (name, value, expires, path, domain, secure) {
      document.cookie = name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

//sets Point of Entry value--once for entire page
function poe()
{
	if(typeof this.poeReturnValue == 'undefined')
	{
		this.poeReturnValue = 'poe=no;';
		if (getCookie("slate_poe") == null || getCookie("slate_poe") == "false")
		{
			this.poeReturnValue = 'poe=yes;';
			setCookie("slate_poe","true","","/","",'')
		}
	}
	return this.poeReturnValue
}

//sets rss-user values--once for entire page
function rss()
{	
	if (typeof this.rssReturnValue == 'undefined')
	{
		if (location.href.indexOf('/fr/rss/') != -1)
		{
			this.rssReturnValue="fromrss=y;";
			setCookie('rss_now','true',''+thisDate().toString()+'','/',wpniDomain,'');
			setCookie('rss','true',''+thisDate().toString()+'','/',wpniDomain,'');
		}
		else
		{
			this.rssReturnValue="fromrss=n;";
			setCookie('rss_now','false',''+thisDate().toString()+'','/',wpniDomain,'');
		}
		if (getCookie('rss') != null) 
		{
			this.rssReturnValue+='rss=y;'
		}
		else 
		{
			this.rssReturnValue += 'rss=n;'
		}
	}
	
	return this.rssReturnValue
}

//detects msn cobranding status--once a page
function msnCobrand()
{
	if (typeof this.msnReturnValue == 'undefined')
	{
		this.msnReturnValue='msn_refer=n;'
		if (typeof msn_cobrand != "undefined")
		{
			if (msn_cobrand != 0)
			{
				this.msnReturnValue='msn_refer=y;';
			}
		}
	}
	
	return this.msnReturnValue
}






function heavy()
{
	if (typeof this.heavyReturnValue == 'undefined')
	{
		this.heavyReturnValue = "heavy=n;"
		if (typeof document.referrer != "undefined")
		{
			if (document.referrer == '') this.heavyReturnValue = "heavy=y;"
		}
	}
	return this.heavyReturnValue
}

function tile(tileBoolean,varDcCount)
{
	if (tileBoolean == true)
	{
		return "tile=" + varDcCount + ";"
	}
}

function interstitial(interstitialCode,varDcCount)
{	
	this.intReturnValue = ''
	
	
	/*if (location.href.match('test_ads')){*/
	
	if (varDcCount=="2" && location.href.indexOf('no_interstitial') == -1)
	{	
		this.intReturnValue = interstitialCode
		if (location.href.match("g=1") || location.href.match("sid=1"))
		{
			this.intReturnValue += "!category=nopops;";
		}
	}
	return this.intReturnValue

	}
	
	/*else if (location.href.match('test_ads') != 1) {
	if (varDcCount=="3" && location.href.indexOf('no_interstitial') == -1)
	{	
		this.intReturnValue = interstitialCode
		if (location.href.match("g=1") || location.href.match("sid=1"))
		{
			this.intReturnValue += "!category=nopops;";
		}
	}
	return this.intReturnValue
}*/


function thisDate()
{
	if (typeof this.dateReturnValue == 'undefined')
	{
		this.dateReturnValue = new Date()
	}
	return this.dateReturnValue
}

function dateToString()
{
	if (typeof this.dtsReturnValue == 'undefined')
	{
		var thisDateObj = thisDate()
		var yyyy = thisDateObj.getYear();
		var mm = thisDateObj.getMonth() + 1;
		var dd = thisDateObj.getDate();
		var hour = thisDateObj.getHours();
		var min = thisDateObj.getMinutes();
		if (mm < 10) mm = "0"+mm;
		if (dd < 10) dd = "0"+dd;
		if (hour < 10) hour = "0"+hour;
		if (min < 10) min = "0"+min;
		this.dtsReturnValue = ''+mm+dd+hour+min;
	}
	return this.dtsReturnValue
}

function dateCheck(when,now)
{
	this.dateCheckReturnValue=false;
	if (typeof when == "undefined")
	{
		this.dateCheckReturnValue = true;
	}
	else
	{
		dateSets = when.split(';')
		for (x=0;x<dateSets.length;x++)
		{
			if (now >= dateSets[x].split('/')[0] && now <= dateSets[x].split('/')[1])
			{
				this.dateCheckReturnValue=true
			}
		}
	}
	return this.dateCheckReturnValue	
}

function locationCheck(where,currentLoc)
{
	this.locCheckReturnValue=false;
	if (where == "undefined")
	{
		where = true;
	}
	if (where.indexOf('-') != -1 || where == '')
	{
		this.locCheckReturnValue = true
	}
	else
	{
		this.locCheckReturnValue = false
	}
		
	var nodes = where.split(';');
		
	for (x=0;x<nodes.length;x++)
	{
		if (currentLoc == nodes[x])
		{
			this.locCheckReturnValue = true;
		}
		if (('-' + currentLoc) == nodes[x])
		{
			this.locCheckReturnValue = false;
		}
	}
	return this.locCheckReturnValue
}

function getHighestValue()
{
	var longest = 0;
	var loop;
	for (loop=0;loop<arguments.length;loop++)
	{
		if (arguments[loop] > longest)
		{
			longest = arguments[loop];
		}
	}
return longest;
}

function debugTextBox(adString) {
	
}

function AdInstance()
{	
	this.when = new Array()
	this.where = new Array()
	this.managerListLength = 0
	
	this.debugAction = function (output) { return debugTextArea(output) }
	
	this.dateObject = new Date()
	this.now = dateToString(this.dateObject)
	
	this.tile = true
	this.flex = false
	
	this.interstitial = "dcopt=ist;"
	
	this.doubleClickFlag = true;
	this.doubleClickAdType = "adj";
	this.dcCode =  msnCobrand() + heavy() + pageId() + poe() + revsci() + dfpComp() + rss(this.dateObject) 
		
	this.testAction = function (testFlagArray)
	{	
		return doubleClickTestCode(testFlagArray)
	}
	
	this.code = ''
	

	
	this.adExecute = function (currentLoc,varDcCount,what,flexBoolean,onTheFly) {
		var doIt = true;
		
			this.isThisManaged = false
		
		if (this.isThisManaged == true)
		{
			doIt = false;
			for (loop=0;loop<this.managerListLength;loop++)
			{
				if (dateCheck(this.when[loop],this.now) && dateCheck(this.where[loop],currentLoc))
				{
					doIt = true;
				}
			}
		}
		
		
		if (doIt == true)
		{
			
			
			if (this.doubleClickFlag == true)
			{
				
				adAddress = "http://ad.doubleclick.net/" + this.doubleClickAdType + "/" + siteZoneDir(currentLoc) + this.code + this.testAction(this.testFlagArray) + wpFlex(flexBoolean,this.size) + tile(this.tile,varDcCount) + interstitial(this.interstitial,varDcCount) + this.dcCode + pos(what) + semiColonizer(onTheFly) +  ord() +  "?"
				
				output="<s\cript language='JavaScript1.1' src='" + adAddress + "'></s\cript>"
				document.write(output)
				
			}
		}
		for(var x in this.debugFlagArray)
				{
					if (location.href.match(this.debugFlagArray[x]))
					{
						this.debugAction(adAddress)
					}
				}
	}
}


function pos(what)
{
	if (typeof this.usedSpots == 'undefined')
	{
		this.usedSpots = new Array();
	}
	if (this.usedSpots[what] == null)
	{
		this.usedSpots[what] = 1;
		return 'pos='+what+';';
	}
	else
	{
		this.usedSpots[what]++;
		return 'pos='+what+"_"+this.usedSpots[what]+';';
	}
}

function placeAd2(currentLoc,what,flex,onTheFly)
{	
	//if no_ads is in the url, it bails
	if (location.href.match('no_ads'))
	{
	return
	}
	
	//if array adClasses doesn't exist, initializes it.
	if (typeof adClasses == "undefined")
	{
		adClasses = new Array()
	}

	//if the specified ad type (what) doesn't exist in the array adClasses, define it with initAdType
	
	if (typeof adClasses[what] == "undefined")
	{
		adClasses[what] = initAdType(what)
	}
	
	//if this is a doubleclick thing, iterate (or initialize) the tile number. 
	if (adClasses[what].doubleClickFlag == true)
	{
		if (typeof this.varDcCount == 'undefined')
		{
			this.varDcCount = 1
		}
		else
		{
			this.varDcCount++
			
		}
	}
	
	//run this adtype's adExecute method, which actually does what has to be done.
	adClasses[what].adExecute(currentLoc,varDcCount,what,flex,onTheFly)
}

function siteZoneDir(currentLoc)
{
	
	if(typeof this.szdReturnValue == 'undefined')
	{
		var currentLocArray = currentLoc.split('/');
		this.szdReturnValue = site + "." + ((currentLocArray[0])?currentLocArray[0]:'news') + ((currentLocArray[1])?'/'+currentLocArray[1]:'') + ";";
		for(var x in currentLocArray)
		{
			this.szdReturnValue += 'dir=' + currentLocArray[x] + ";"
		}
	}
	
	return this.szdReturnValue
}

function doubleClickTestCode(testFlagArray)
{
	dctCodeValue = ''
	for(x in testFlagArray)
	{	
		if(location.href.match(testFlagArray[x]))
		{
			dctCodeValue += 'kw=test_ads;'
		}
	}
	return dctCodeValue
}

function pos(what)
{
	if (typeof this.usedSpots == 'undefined')
	{
		this.usedSpots = new Array();
	}
	if (this.usedSpots[what] == null)
	{
		this.usedSpots[what] = 1;
		return 'pos='+what+';';
	}
	else
	{
		this.usedSpots[what]++;
		return 'pos='+what+"_"+this.usedSpots[what]+';';
	}
}

function placeAd2(currentLoc,what,flex,onTheFly)
{	
	//if no_ads is in the url, it bails
	if (location.href.match('no_ads'))
	{
	return
	}
	
	//if array adClasses doesn't exist, initializes it.
	if (typeof adClasses == "undefined")
	{
		adClasses = new Array()
	}

	//if the specified ad type (what) doesn't exist in the array adClasses, define it with initAdType
	
	if (typeof adClasses[what] == "undefined")
	{
		adClasses[what] = initAdType(what)
	}
	
	//if this is a doubleclick thing, iterate (or initialize) the tile number. 
	if (adClasses[what].doubleClickFlag == true)
	{
		if (typeof this.varDcCount == 'undefined')
		{
			this.varDcCount = 1
		}
		else
		{
			this.varDcCount++
			
		}
	}
	
	//run this adtype's adExecute method, which actually does what has to be done.
	adClasses[what].adExecute(currentLoc,varDcCount,what,flex,onTheFly)
	}









//EVERYTHING ABOVE IS WPNI GLOBAL--CHANGES MUST BE REFLECTED IN WPNI_AD.JS
//EVERYTHING BELOW IS SITE SPECIFIC


//this declares what site we're working with
site='bt'
//this declares the domain for all cookies
wpniDomain=(dateToString()<'08010000')?'budgetravelonline.com':'budgettravel.com';
show_doubleclick_ad = true



//this declares what kinds of ads are on this site
function initAdType(what)
{
	thisAdType = new AdInstance()
	thisAdType.testFlagArray = new Array('test_ads','test_' + what);	
	thisAdType.debugFlagArray = new Array('debugAdCode','debugAdCode_' + what);

	switch(what)
	{
		
		case 'leaderboard':
			thisAdType.size = "728x90"
			thisAdType.code = "ad=lb;"
		break
		
		case 'leaderboard2':
			thisAdType.size = "728x90"
			thisAdType.code = "ad=lb;"
		break

		case 'skyscraper':
			thisAdType.size = "160x600"
			thisAdType.code = "ad=ss;"
		break

		case 'bigboxSkyFlex':
			thisAdType.size = "300x250,160x600"
			thisAdType.code = "ad=bb;ad=ss;"
		break
		
		case 'bigboxHPFlex':
			thisAdType.size = "300x250,336x850"
			thisAdType.code = "ad=bb;ad=hp;"
		break
		
		case 'bigboxSkyHPFlex':
			thisAdType.size = "300x250,160x600,336x850"
			thisAdType.code = "ad=bb;ad=ss;ad=hp;"
		break

		case 'sponsorship':
			thisAdType.size = "446x33"
			thisAdType.code = "ad=fb;"
		break
		
		case 'featurebar':
			thisAdType.size = "251x87"
			thisAdType.code = "ad=fb;"
		break
	
		case '125x125':
			thisAdType.size = "125x125"
			thisAdType.code = "ad=tt;"
		break
		
		case '120x240':
			thisAdType.size = "120x240"
			thisAdType.code = "ad=btt;"
		break
		
		case '120x240':
			thisAdType.size = "120x240"
			thisAdType.code = "ad=btt;"
		break
		
		case '88x31':
			thisAdType.size = "88x31"
			thisAdType.code = "ad=88x31TT;"
		break
		
		case '160x90':
			thisAdType.size = "160x90"
			thisAdType.code = ""
		break
		
		
		case 'video':
			
			thisAdType.adServer = 'http://ad.doubleclick.net/pfadx/'
			thisAdType.code = "ad=video"
			thisAdType.adExecute = function (currentLoc,varDcCount,what,flexBoolean)
			{
				if(typeof config == 'undefined')
				{
					config = new Array();
				}
				
				config['adServerURL'] = this.adServer + siteZoneDir(currentLoc) + this.code + ";" + thisAdType.testAction(thisAdType.testFlagArray)
				config['additionalAdTargetingParams'] = ';' + this.dcCode
				
				for(var x in this.debugFlagArray)
				{
					if (location.href.match(this.debugFlagArray[x]))
					{
						this.debugAction()
					}
				}
			}
			
			thisAdType.debugAction = function()
			{
				var output = "config['adServerURL']:" + config['adServerURL'] + "\n\r"
				output += "config['additionalAdTargetingParams']:" + config['additionalAdTargetingParams'] + "\n\r"
				alert(output)
			}

		break

	}
	
	if (thisAdType.isThisManaged = true)
	{
	thisAdType.managerListLength = getHighestValue(thisAdType.when.length,thisAdType.where.length)
	}
	
	
return thisAdType
}

show_doubleclick_ad = true;

//this translates from the old placeAd to the new one


function placeAd()
{
	currentLoc=arguments[1]
	spot = arguments[2]	
	onTheFly=arguments[3]
	onTheFly+=(arguments[0]=='SECTION')?'front=y;page=section;':'front=n;page=article;'
	hyundaiHack = ['misc/brooklyn/CobbleHill.html','misc/brooklyn/ParkSlope.html','misc/brooklyn/Williamsburg.html','google_maps/sandiego/','google_maps/washingtondc/','misc/GG_BigCityMaps/Chicago/chicago_pop.html','misc/GG_BigCityMaps/Montreal/montreal_pop.html','misc/GG_BigCityMaps/Miami/miami_pop.html','misc/GG_BigCityMaps/NewYorkCity/newyork_pop.html','misc/GG_BigCityMaps/SanFrancisco/sanfrancisco_pop.html','misc/projectrunway/newyorkcity.html','misc/davinci/paris.html','misc/davinci/london.html','misc/davinci/scotland.html','misc/starmaps/newyorkcity.html','misc/starmaps/losangeles.html','/bt-srv/microsite/hyundai']
	
	for(var _a = 0;_a<hyundaiHack.length;_a++)
	{
		onTheFly+=(location.href.match(hyundaiHack[_a]))?'hyundai=y;':'';
	}
	
				
	
		
	if (location.href.match('AR2007021300533') || location.href.match('AR2005060400571') || location.href.match('AR2005060400903') || location.href.match('AR2005060400565') || location.href.match('AR2006061301067') || location.href.match('AR2005060400433') || location.href.match('AR2006030901272') || location.href.match('AR2005060400896')){
    	currentLoc = 'usandcanada/us/texas';
    }
    
    
    if (location.href.match('zipcode=73301') || location.href.match('zipcode=78401') || location.href.match('zipcode=Dallas%2C+Texas') || location.href.match('zipcode=79901') || location.href.match('zipcode=galveston%2C+texas') || location.href.match('zipcode=Houston%2C+Texas') || location.href.match('zipcode=78201')){
    	currentLoc = 'usandcanada/us/texas';
    }
	
	
	var transArray = new Array('','leaderboard','leaderboard2','skyscraper','skyscraper','bigboxSkyFlex','bigboxSkyHPFlex','sponsorship','125x125','120x240','160x90','bigboxHPFlex','featurebar','88x31')
	
	//BT sending multiple sizes starting may 15
	//so all incoming flex states are getting turned off
	flexVal=false;
	//if (spot==5 || spot==6 || spot ==11) {flexVal=true};	
	
	
	placeAd2(currentLoc,transArray[spot],flexVal,onTheFly)
	
}
