/*------------------------------------------------------------------------ //
   Coca-Cola / mycokerewards
   -----------------------------------------------------------------------
   Description:  Custom JavaScript to capture search engine data and
                 keywords used to get to the MCR site.
   Filename:     referralFlash.js
   ----------------------------------------------------------------------- */

/* ======================================================================= //
   REFERRAL:
   The following code is used to capture and parse certain search engine
   referral URLs into separate search engine names and keywords.
   ======================================================================= */
var referral = {
	engine: ["alltheweb","altavista","aol","ask","dmoz","google","live","lycos","msn","netscape","nlsearch","yahoo"],
	searchstring: ["q|query","q","query","q|ask","search","q|as_q|as_epq|as_oq","q","query|wfq","q|MT","query","textQuery","p"],
	referrer: "",//referrer
	rsrc: "",//rsrc
	query: "",//query
	se: "",//search engine
	kw: "",//keywords
	pn: "searchReferral",//parameter name(s)
	fn: "storeSearchReferralOnSession.do",//filename
	xmlHttp: null,
	init: function() {
		if(document.referrer && document.referrer != "")
			this.referrer = document.referrer;
		
		//this.referrer = "http://www.alltheweb.com/search?cat=web&cs=iso88591&q=this+is+a+list+of+keywords&rys=0&itag=crv&_sb_lang=pref";//alltheweb
		//this.referrer = "http://www.altavista.com/web/results?itag=ody&q=this+is+a+list+of+keywords&kgs=1&kls=0";//altavista
		//this.referrer = "http://search.aol.com/aol/search?invocationType=comsearch30&query=this+is+a+list+of+keywords&do=Search";//aol
		//this.referrer = "http://www.ask.com/web?q=this+is+a+list+of+keywords&search=search&qsrc=0&o=0&l=dir";//ask
		//this.referrer = "http://search.dmoz.org/cgi-bin/search?search=this+is+a+list+of+keywords";//dmoz
		//this.referrer = "http://www.google.com/search?hl=en&rlz=1G1GGLQ_ENUS260&q=this+is+the+list+of+keywords&btnG=Search";//google
		//this.referrer = "http://search.live.com/results.aspx?q=this+is+a+list+of+keywords&go=Search&mkt=en-us&scope=&FORM=LIVSOP";//live
		//this.referrer = "http://search.lycos.com/?query=this+is+a+list+of+keywords&x=58&y=19";//lycos
		//this.referrer = "http://search.msn.com/results.aspx?q=this+is+a+list+of+keywords&FORM=MSNH";//msn
		//this.referrer = "http://search.netscape.com/search/search?invocationType=nscpportal&query=this+is+a+list+of+keywords&do=Search";//netscape
		//this.referrer = "http://www.nlsearch.com/results.php?searchForm=rssearch.php&datasource=BREC&textQuery=this+is+a+list+of+keywords&x=0&y=0&titleQuery=&pubQuery=&urlQuery=&sourceGroups%5B%5D=1&periodDate=-5&sort=1&resultsPerPage=10";//nlsearch
		//this.referrer = "http://search.yahoo.com/search?p=this+is+a+list+of+keywords&fr=yfp-t-501&toggle=1&cop=mss&ei=UTF-8";//yahoo
		
		this.parse();
		this.track();
	},
	parse: function() {
		if(location.search.indexOf("rsrc=") != -1)
		{
			var i = 0;
			var ap;//array position
			
			this.rsrc = location.search.split("?");
			this.rsrc = this.rsrc[1].split("&");
			
			for(i = 0; i < this.rsrc.length;i++)
				if(this.rsrc[i].indexOf("rsrc") != -1)
					ap = i;
					
			this.rsrc = this.rsrc[ap].split("=");
			this.rsrc = this.rsrc[1];
			
			//console.log(this.rsrc);
		}
		if((this.referrer.indexOf("http://www.mycokerewards.com") != -1 || this.referrer == "") && (siteReferrer != null || siteReferrer != ""))
			this.referrer = siteReferrer;
		
		if(this.referrer.indexOf("?") != -1)
		{
			var i = 0;
			var j = 0;
			var ap;//array position
			
			this.referrer = unescape(this.referrer);
			this.referrer  = this.referrer.split("?");
			this.se = this.referrer[0];
			this.qs = this.referrer[1].split("&");
			
			while(i < this.engine.length && this.se.indexOf(this.engine[i] + ".") == -1)
				i++;
			
			if(i != this.engine.length)
			{	
				this.se = this.engine[i];
				this.sq = this.searchstring[i].split("|");
				
				for(i = 0; i < this.qs.length; i++)
					for(j = 0;j < this.sq.length; j++)
						if(this.qs[i].indexOf(this.sq[j] + "=") != -1 && this.qs[i].charAt(0) == this.sq[j].charAt(0))
								ap = i;
				
				if(ap <= this.sq.length)
					if(this.qs[ap].indexOf("=") != -1)
					{
						this.qs = this.qs[ap].split("=")
						this.kw = escape(this.qs[1]);
						//console.log(this.referrer)
						//console.log(this.se)
						//console.log(this.kw)
					}
			}
		}
	},
	track: function() {
		try {
			this.xmlHttp = new XMLHttpRequest();
		} catch (e) {
			try {
				this.xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
				this.xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
		}
		
		this.xmlHttp.onreadystatechange = function() {
			if(referral.xmlHttp.readyState == 4)
				try {
					if(referral.xmlHttp.status == 200 || this.xmlHttp.status == 0)
					{
						//referral.xmlHttp.responseText;
					}
				} catch(e) {
					//e.description;
				}
		}
		
		if(this.rsrc != "")
			this.query = this.pn + "=" + "PD_" + this.rsrc + "_MCR_" + this.kw;
		else
			if(this.se != "")
				this.query = this.pn + "=" + "OG_" + this.se + "_MCR_" + this.kw;
		
		//console.log(this.query);
		this.xmlHttp.open("post",this.fn,true);
		this.xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		this.xmlHttp.send(this.query);
	}
}
referral.init();