function readCookie(name){
	if (document.cookie == '') {
		return false;
		}
	else {
		// if pcode is undefined, set it to ''
		return unescape(getCookieValue2(name));
		}
}

// this function makes sure there is an = sign after the name in order to avoid repeated names
function getCookieValue2(name){
	var firstChar, lastChar;
	var theBigCookie = document.cookie;
	firstChar = theBigCookie.indexOf(name + "=");
	if (firstChar != -1){
		firstChar += name.length + 1;
		lastChar = theBigCookie.indexOf(';', firstChar);
		if (lastChar == -1) lastChar = theBigCookie.length;
			return theBigCookie.substring(firstChar, lastChar);
		}
	else
		return "";
}

function SetCookie (name,value,expires,path,domain,secure) {
  document.cookie = name + "=" + escape (value) +
	((expires) ? "; expires=" + expires.toGMTString() : "") +
	((path) ? "; path=" + path : "") +
	((domain) ? "; domain=" + domain : "") +
	((secure) ? "; secure" : "");
}

// set the Target cookie so that we can come back here after login in case we arent logged in currently
spLocation = document.URL
spCookiePath = '/';
spSiteDomain = ".amamember.org";

// if there isnt an authentication cookie set, then go back to the login page
function doTest(reloc){
	// if there isnt an authentication cookie set, then go back to the login page
	if (getCookieValue2('DomAuthSessId').length != 32){
		SetCookie('target', spLocation, null, spCookiePath, spSiteDomain, false);
		window.location=reloc;
	}
	doCount();
}

// count the number of hits - if there are too many (ie a site scraper) then send them back to the login
function doCount(){
	var maxCount = 500;
	if (readCookie('hcount').length == 0){
		SetCookie('hcount', 0, null, spCookiePath, spSiteDomain, false);
	}
	var CookieCount = parseInt(readCookie('hcount'))
	if (CookieCount >= maxCount) {
		SetCookie('DomAuthSessId', '', null, spCookiePath, spSiteDomain, false);
		SetCookie('hcount', 0, null, spCookiePath, spSiteDomain, false);
	} else {
		SetCookie('hcount', CookieCount + 1, null, spCookiePath, spSiteDomain, false);
	}
}