//onload="checkReferrer(document.referrer);"
/*/*3/21/11 4:26PM  JC tried to ad Share tracking for use with facebook 
JC trying to fashion a parsed share line on FB to add a "?share" tag Goal 
is to have the shared URL follow the string "SHARE-"  so we can  track which
 pages are shared on FB.  the link works in the Template, but not n the real
  page with the template applied.
The referrer script is not running at all as currently programmed.

*/
function saveReferrerToCookie(referrerURL) {
	expireDate = new Date;
	
	expMonth=1;
	// following sets the cookie to expire expMonth  months later
	expireDate.setMonth(expireDate.getMonth() + expMonth);

	// 030310Lee defined the path as well   4/26/11 8:53AM  JC defined the domain with leading '.' so it will work with or without the www.
	document.cookie = 'ReferredBy=' + referrerURL + '; domain=.athenainstitute.com; expires=' + expireDate.toGMTString() + '; path=/'; 
	//alert('SAVED: ' + 'ReferredBy=' + referrerURL);  // REMOVE LATER!!! REMOVE LATER!!! REMOVE LATER!!!
	return true;
}


function readReferrerFromCookie() {
	//alert('READ: ' + document.cookie);  // REMOVE LATER!!! REMOVE LATER!!! REMOVE LATER!!!
	if (document.cookie != '') {
		return document.cookie.split('=')[1];  // [0] is name, [1] is value
	}
	else {
		return 'ERROR';
	}
}

function checkPromo() {
	currentURL=document.location.href;
	if (currentURL.indexOf('#email-promo') > -1) {
		return true;
	}
	else {
		return false;
	}

}

function checkWebad() {
	currentURL=document.location.href;
	if (currentURL.indexOf('?webad') > -1) {
		return true;
	};
	if (currentURL.indexOf('?WEBAD') > -1) {
                return true;
        };
	return false;
}
//added 071211
function checkScan() {
	currentURL=document.location.href;
	if (currentURL.indexOf('?scan') > -1) {
		return true;
	};
	if (currentURL.indexOf('?SCAN') > -1) {
                return true;
        };
	return false;
}

function checkShare() {
	currentURL=document.location.href;
	if (currentURL.indexOf('?share_service=') > -1) {
		return true;
	};
	if (currentURL.indexOf('?SHARE_SERVICE=') > -1) {
                return true;
        };
	return false;
}

function checkDomain() {
	currentURL=document.location.href;
	if (currentURL.indexOf('?domain') > -1) {
		return true;}
	else {
		return false;}
}

function checkList(referrerURL) {
	//siteList = new Array('bob.html', 'jeff.html', 'fred.html', 'index.html', 'test.html', 'another.html');  3/8/10 10:53AMJC removed athenainstitute.com
	siteList = new Array('athena-inst.com', 'scienceofattraction.com', 'sexualattraction.net');


	siteListLength = siteList.length;
	isInList = false;

	referrerURL=referrerURL.toLowerCase();

	for (i=0;i< siteListLength;i++) {
		oneSite = siteList[i];
		oneLoc=referrerURL.indexOf(oneSite);
		if (oneLoc > -1) {
			isInList=true;
		}
	}
	return isInList;
}

function checkURL() {
	refTag='#Referrer='
	currentURL=document.location.href;
	if (currentURL.indexOf(refTag) > -1) {
		return true;}
	else {
		return false;}
}

function getFromURL() {
	// reads out the referrer if it was put into a URL
	refTag='#Referrer=';
	refTagLength=refTag.length;
	currentURL=parent.document.location.href;
	return currentURL.substring(currentURL.indexOf(refTag) + refTagLength );
}

function getFromEmail() {
	// reads out the referrer if it was put into a URL
	refTag='#email-promo';
	refTagLength=refTag.length;
	currentURL=parent.document.location.href;
	return 'EMAIL' + currentURL.substring( currentURL.indexOf(refTag) + refTagLength );
}

function getFromWebad() {
	// reads out the referrer if it was put into a URL
	refTag='?WEBAD';
	if (currentURL.indexOf('?webad') > -1) {
		refTag='?webad';
	};
	refTagLength=refTag.length;
	currentURL=parent.document.location.href;
	return 'WEBAD' + currentURL.substring( currentURL.indexOf(refTag) + refTagLength );
}
//jc added 071211
function getFromScan() {
	// reads out the referrer if it was put into a URL
	refTag='?Scan';
	if (currentURL.indexOf('?scan') > -1) {
		refTag='?scan';
	};
	refTagLength=refTag.length;
	currentURL=parent.document.location.href;
	return 'Scan' + currentURL.substring( currentURL.indexOf(refTag) + refTagLength );
}

function getFromShare() {
	// reads out the referrer if it was put into a URL
	// 3/21/11 2:56PM  jc added
	//Script grapbbed from http://befused.com/javascript/get-filename-url
	  //function getPageFileName() {
		  //this gets the full url
		  var pageUrl = document.location.href;
		  //this removes the anchor at the end, if there is one
		  pageUrl = pageUrl.substring(0, (pageUrl.indexOf("#") == -1) ? pageUrl.length : pageUrl.indexOf("#"));
	  //this removes the query after the file name, if there is one
		  pageUrl = pageUrl.substring(0, (pageUrl.indexOf("?") == -1) ? pageUrl.length : pageUrl.indexOf("?"));
	  //this removes everything before the last slash in the path
		  pageUrl = pageUrl.substring(pageUrl.lastIndexOf("/") + 1, pageUrl.length);
	  //return
	  //	return pageUrl;
		  //alert('this page name:' + pageUrl);
	  //}
	//end script grabbed

   
	refTag='?SHARE';
	badTag='&';
	if (currentURL.indexOf('?share_service=') > -1) {
		refTag='?share_service=';
	};
	refTagLength=refTag.length;
	if (currentURL.indexOf('&') > -1) {
		badTag='xxx';
	};
	if (pageUrl ==null){
		pageUrl='homepage';
	};
	currentURL=parent.document.location.href;
	//var cleanedUrl = currentURL.replace('&', '-');
	  //this removes the query after the file name, if there is one
	  
	//4/25/11 2:09PM  this cleans it out on facebook
	var cleanedUrl =  currentURL = currentURL.substring(0, (currentURL.indexOf("&") == -1) ? currentURL.length : currentURL.indexOf("&"));
	//var cleanedURL	
	//cleanedURL= replaceCharacters(currentURL,'&','-')
	return 'SHARE-'+ pageUrl+'-' +cleanedUrl.substring( currentURL.indexOf(refTag) + refTagLength );
	
}

function getFromServerRedirect() {
	// reads out the domain from a server side transfer
	refTag='?domain=';
	refTagLength=refTag.length;
	currentURL=parent.document.location.href;
	var domain=currentURL.substring( currentURL.indexOf(refTag) + refTagLength );
	//alert('Domain:' + domain);
	return domain;
}


function checkReferrer(referrerURL) {
//alert(referrerURL);
	//alert(document.cookie);
	//alert('URL is ' + self.window.document.location.href);
	//alert('CHECKING (incoming is ' + self.window.document.referrer +')'); // REMOVE LATER!!! REMOVE LATER!!! REMOVE LATER!!!
	if (document.cookie != '') {
		// cookie exists, so get the referrer from it
	}
	else {
		emailPromo=checkPromo();
		if (emailPromo) {
			// the current URL indicates it was an email-promo link
			saveReferrerToCookie(getFromEmail());
		}
		else {
			passedURL=checkURL();
			//alert('Passed in URL is: ' + passedURL);
			if (passedURL) {
				saveReferrerToCookie(getFromURL());
			}
			else {
				webadURL=checkWebad();
				if (webadURL) {
					// the current URL indicates it was an email-promo link
					saveReferrerToCookie(getFromWebad());
				}
				
				//jc added 071211
				else {
				scanURL=checkScan();
				if (scanURL) {
					// the current URL indicates it was an email-promo link
					saveReferrerToCookie(getFromScan());
				}
				  else {
				  shareURL=checkShare();
				  if (shareURL) {
					  // the current URL indicates it was an email-promo link
					  saveReferrerToCookie(getFromShare());
				  }
					else {
						if (referrerURL=='') {
							DomainURL=checkDomain();
							if (DomainURL){
								saveReferrerToCookie(getFromServerRedirect());}
							else{	
								saveReferrerToCookie('Direct.');}
						}
						else {			
							// referrer is not empty, 
							// document.cookie is not defined, or an email-promo, so check to see if it's an outside URL
							isInList=checkList(referrerURL);
							// alert('is in list: ' + isInList);
							if (isInList==false) {
								saveReferrerToCookie(referrerURL);
							}
							else {
								saveReferrerToCookie('AffiliateWebsite.');
							}
					}
				  }
			  }
		  }
	  }
  }
} //jc added share statement 4/13/11
	
	// alert('readReferrerFromCookie: ' + readReferrerFromCookie());
	return 'referred by ' + readReferrerFromCookie();
}

