//onload="checkReferrer(document.referrer);"

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

	document.cookie = 'ReferredBy=' + referrerURL + ';expires=' + expireDate.toGMTString();
	//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;
	}
	else {
		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');
	siteList = new Array('athena-inst.com', 'athena-inst.net', 'athena-inst.org', 'athenainstitute.com', 'athena-pheromone.com', 'athena1013.com', 'athena10x.com', 'athenainstitute.biz', 'athenainstitute.com', 'athenainstitute.net', 'athenainstitute.org', 'athenapheromone.com', 'athenapheromone1013.biz', 'athenapheromone1013.com', 'athenapheromone10x.biz', 'athenapheromone10x.com', 'athenapheromones.biz', 'athenapheromones.com', 'athenascience.com', 'feromonas.com', 'feromone.com', 'feromones.com', 'lovepotion.net', 'lovepotion.org', 'pheromone-athena.net', 'pheromone-athena.org', 'pheromone1013.com', 'pheromone10x.com', 'pheromoneathena.com', 'pheromoneathena.net', 'pheromoneathena.org', 'reducehysterectomy.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';
	refTagLength=refTag.length;
	currentURL=parent.document.location.href;
	return 'WEBAD' + currentURL.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(document.cookie);
	// alert('URL is ' + self.window.document.location.href);
	// alert('CHECKING (incoming is ' + 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());
				}
				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.');
						}
				}
			}
		}
	}
}
	
	// alert('readReferrerFromCookie: ' + readReferrerFromCookie());
	return 'referred by ' + readReferrerFromCookie();
}