function NewWindow(width,height,url) {
	window.open(url,"PopUp","menubars=0,scrollbars=1,resizable=1,height="+height+",width="+width);
}

function CheckError(status) {
 prevpage = document.referrer.toLowerCase();
if(status != "") {
   //Typed invalid e-mail into edit account login
   if((status.indexOf("Passwords doesn't match") != -1) || (status.indexOf("Passwords doesn''t match") != -1) || (status.indexOf("Invalid e-mail address") != -1)) {
     error = "&error=passwordmismatch";
     document.cookie="usernamepassword=;Path=/";
     document.cookie="UserRegID=;Path=/";
     document.location = prevpage + error;
   }
   //Tried to register with an existing email address
   else if((prevpage.indexOf("forceuserreg=1") > 0 && prevpage.indexOf("edit=1") == -1) && status.indexOf("Wrong password") != -1) {
     error = "&error=emailexists";
     document.cookie="usernamepassword=;Path=/";
     document.cookie="UserRegID=;Path=/";
     /*document.write(prevpage + error+"<br>");
     document.write(prevpage.indexOf("forceuserreg=1")+"<br>");
     document.write(prevpage.indexOf("edit=1")+"<br>");
     document.write(status.indexOf("Wrong password"));
     */
     document.location = prevpage + error;
   }
 }
}

function DisplayMsg(error) {
 //Error for trying to login with an invalid email address
 if(error == "bademail") {
    msg = "<br><span class=\"systemmsg\">You entered an unregistered email address.<br>Please try again or create an account.</span>";
 }
 //Error for trying to register with an email address that is already registered
 else if( error == "emailexists") {
    msg = "<br><span class=\"systemmsg\">The email address you submitted is already registered.<br>You may <a href=\"javascript:NewWindow(400,300, '<pbs:prog>/misc?URL=/misc/password_email.pbs');\">email the password to this address</a> or register a different one.</span>";
 }
 //Error for trying to register with an email address that is already registered
 else if( error == "passwordmismatch") {
    msg = "<br><span class=\"systemmsg\">The two passwords you typed don't match. <br>Please try again.";
 }
 document.write(msg);
}

function CheckInputData(frm) {
	var skip = false;
	if (!checkEmail(frm.email)) {
		if (EmailReturnCode == "inValid") {
				alert("A valid e-mail address is required.");
		} else if(EmailReturnCode == "wireless") {
			frm.email.value="";
		}
		frm.email.focus();
		frm.email.select();
		skip = true;
	}
	if (!skip && frm.id.value=="" && frm.pwd.value=="") {
		alert("Password is required");
		frm.pwd.focus();
		frm.pwd.select();
		skip = true;
	}
	if (!skip && frm.id.value=="" && frm.pwdr.value=="") {
		alert("Please confirm password");
		frm.pwdr.focus();
		frm.pwdr.select();
		skip = true;
	}
	if (!skip && (frm.id.value=="") && (frm.pwd.value != frm.pwdr.value)) {
		alert("Passwords do not match.");
		frm.pwdr.focus();
		frm.pwdr.select();
		skip = true;
	}
	if (!skip && frm.extra_gender.value == "") {
		alert("Please select gender.");
		frm.extra_gender.focus();
		skip = true;
	}
	if (!skip && isNaN(frm.extra_age.value)) {
		alert("A valid age is required.");
		frm.extra_age.focus();
		frm.extra_age.select();
		skip = true;
	}
  if (!skip && !checkCOPPA(frm.extra_age.value)) {
    if (ageLimit == "u13") {
				alert('We do not collect information from children under the age of 13. Therefore, you will not be able to receive this service, and your personal information will not be retained. Please contact us if you or your parent have any questions.')
		} else if (ageLimit == "o110") {
				alert('Please enter a valid age.')
		}
		skip = true;
	}

//	if (!skip && !checkCOPPA(frm.extra_age.value)) {
//		alert('We do not collect information from children under the age of 13. Therefore, you will not be able to receive this service, and your personal information will not be retained. Please contact us if you or your parent have any questions.');
//		skip = true;
//	}
	if (!skip && !checkZip(frm.extra_zipcode.value)) {
		alert("Zip code is invalid. Please enter your zipcode in the format 12345 or 12345-1234.");
		frm.extra_zipcode.focus();
		frm.extra_zipcode.select();
		skip = true;
	}
	if (!skip) {
 	  frm.submit();
	}
	return (!skip);
}

function checkEmail(email) {
  var email_format = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z])+$/;
  if (!email_format.test(email.value)) {
    EmailReturnCode='inValid';
    return false;
  } else {
  	var emailaddress = email.value;
  	var thisDomain = emailaddress.substring(emailaddress.indexOf('@')+1, emailaddress.length);
  	if(thisDomain != null) {
  		for(i=0; i < ETNoEmailCheck.length; i++) {
          //
          // check domain against no email list (mobile devices)
          //
          if(thisDomain == ETNoEmailCheck[i]) {
      	      alert("We cannot send news letters to wireless devices, please use another address.\nThe domain you supplied is listed on a national 'do not mail' list which can be found at:\nhttp://www.fcc.gov/cgb/policy/DomainNameDownload.html");
      	      EmailReturnCode='wireless';
      	      return false;
      	  }
      }
    }
  }
  return true;
}

function checkZip(zipcode) {  // Check that a US Postal Code is valid
  if (!(zipcode.length == 5 || zipcode.length == 10)) return false;
  if (zipcode.length == 5 && !isNumeric(zipcode)) return false;
  if (zipcode.length == 10 && zipcode.search(/^\d{5}-\d{4}$/) == -1) return false;
  return true;
}

function setDefaultNewsletter(frm, defaultNewsletter) {
  for (x=0;x<frm.elements.length;x++) {
   if (frm.elements[x].type == "checkbox") {
    if (frm.elements[x].name == "extra_"+defaultNewsletter && frm.elements[x].value == defaultNewsletter) {
	  frm.elements[x].checked = true;
    }
   }
  }
}


function checkCOPPA(age) {
  if(age >= 13 && age <= 110) {
      return true;
  } else if (age > 110) {
  	  ageLimit = "o110";
  	  return false;
  } else {
  	  ageLimit = "u13";
      return false;
  }
}

//function checkCOPPA(age) {
//  if(age >= 13)
//    return true;
//  else
//    return false;
//}

function isNumeric(string) {
  if (string.search) {
    if (string.search(/[^\d]/) != -1) return false;
  }
  return true;
}

function SelectAll(b, c) {
  var f = document.selections;
  var s = new String();
  for (x=0;x<f.elements.length;x++) {
   if (f.elements[x].type == "checkbox") {
    if (f.elements[x].name == "Category") {
     s = f.elements[x].value;
	 if (s.substr(0, c.length) == c) {
	  f.elements[x].checked = b;
	 }
	}
   }
  }
}

function testIsValidObject(objToTest) {
  if (null == objToTest) {
    return false;
  }
  if ("undefined" == typeof(objToTest) ) {
    return false;
  }
  return true;
}

function doSubmit() {
 var frm = document.selections;
 frm.submit();
}

function EatCookie() {
	document.cookie="usernamepassword=;Path=/";
	document.cookie="UserRegID=;Path=/";
	document.location="http://yourupstate.com/apps/pbcs.dll/frontpage?RegLogout=1";
}

/*
 * 3/6/2007 (T. Woodham):	We need a way to broadcast registration cookies across the whole GreenvilleOnline network of sites.
 *							These functions should do the trick for us.
 */

function deleteCookie( name, path, domain ) {
	if (readCookie(name)) {
		// set time, it's in milliseconds
		var today = new Date();
		today.setTime( today.getTime() );
		expiredate = new Date( today.getTime() + (-86400000) );
		document.cookie = name + "0=;expires=" + expiredate.toGMTString() + ((path) ? ";path=" + path : "") + ((domain) ? ";domain=" + domain : "");
		alert ("deleteCookie() called for " + name + " (" + expiredate.toGMTString() + ")\nHere's a test: " + readCookie(name));
	}
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function createCookie( name, value, expires, path, domain, secure ) 
{
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );

	/*
	if the expires variable is set, make the correct 
	expires time, the current script below will set 
	it for x number of days, to make it for hours, 
	delete * 24, for minutes, delete * 60 * 24
	*/
	if ( expires )
	{
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );

	document.cookie = name + "=" +escape( value ) +
	( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
	( ( path ) ? ";path=" + path : "" ) + 
	( ( domain ) ? ";domain=" + domain : "" ) +
	( ( secure ) ? ";secure" : "" );
}

function cookiemonster () {
	var login = readCookie ('login');
	var user = readCookie ('UserRegID');
	var body = document.getElementsByTagName('body');

	if (login != null) {
		var go_iframe = document.createElement('iframe');
		go_iframe.setAttribute('src', 'http://greenvilleonline.com/misc/cookies.htm?site=greenvilleonline&pbsid=' + login + '&' + user);
		go_iframe.setAttribute('width', '0');
		go_iframe.setAttribute('hieght', '0');
		go_iframe.setAttribute('name', 'greenvilleonline_proxy');
		go_iframe.setAttribute('id', 'greenvilleonline_proxy');

		body[0].appendChild(go_iframe);
	}
}

/*
 * 5/4/2007 (T. Woodham):	We need to override the pbsSetCookie function so we're writing domain information.
 */
function pbsSetCookie(excludelist) {
	var ca = new Array;
	var xl = "";
	var rootdomain = window.location.hostname;
	rootdomain = rootdomain.replace(/www\./, '');
	rootdomain = "." + rootdomain;

//	alert("My root domain:  " + rootdomain);
	if ((excludelist != null) && (!excludelist.name)) { xl = ","+excludelist+","; }
	for (var i=0;i<csp_f.length;i++) {
		if ((xl == "") || (xl.indexOf(","+csp_f[i].name+",") != -1)) {
			if ((csp_f[i].type == "text") || (csp_f[i].type == "password")) {
				ca.length++;
				ca[ca.length-1] = csp_f[i].name + "=" + escape(csp_f[i].value);
			} else if ((csp_f[i].type == "select-multiple") || (csp_f[i].type == "select-one")) {
				ca.length++;
				ca[ca.length-1] = csp_f[i].name + "=" + escape(csp_MakeSelectCookie(csp_f[i]));
			} else if (csp_f[i].type == "checkbox") {
				ca.length++;
				ca[ca.length-1] = csp_f[i].name + "=" + (csp_f[i].checked ? "1" : "0");
			} else if (csp_f[i].type == "radio") {
				if (csp_f[i].checked) {
					ca.length++;
					ca[ca.length-1] = csp_f[i].name + "=" + csp_GetCheckedRadioIndex(csp_f[i].name);
				}
			}
		}
	}
	if (ca.length > 0) {
		var cstr = ca.join("&");
		edate = new Date;
		edate.setFullYear(edate.getFullYear()+1);
		document.cookie = csp_cname + "=" + cstr + "; expires=" + edate.toGMTString() + "; domain=" + rootdomain + "; path=/";
	}
}

function LogMeOut() {
	var rootdomain = window.location.hostname;
	rootdomain = rootdomain.replace(/www\./, '');
	var mywholedomain = rootdomain;
	var mydomain = mywholedomain.replace(/\.com/, '');
	rootdomain = "." + rootdomain;

	if (mywholedomain.match (/forums\./)) {
		mywholedomain = mywholedomain.replace (/forums\./, '');
		mydomain =		mydomain.replace (/forums\./, '');
		rootdomain =	rootdomain.replace (/forums\./, '');
	}

	createCookie('UserRegID', '', -1, '/', rootdomain, '');
	createCookie('usernamepassword', '', -1, '/', rootdomain, '');
	createCookie('login', '', -1, '/', rootdomain, '');
	var forumprefix = 'goforums';
	createCookie(forumprefix + '_sid', '', -1, '/', rootdomain, '');
	createCookie(forumprefix + '_data', '', -1, '/', rootdomain, '');
	createCookie('ForumUser', '', -1, '/', rootdomain, '');

	// Publicus create a whole bunch of cookies we should get rid of.
	createCookie ('s_sq', '', -1, '/', rootdomain, '');
	createCookie ('s_cc', '', -1, '/', rootdomain, '');
	createCookie ('rsi_ct', '', -1, '/', rootdomain, '');
	createCookie ('PBCSSESSIONID', '', -1, '/', rootdomain, '');
	createCookie ('GCIONSN', '', -1, '/', rootdomain, '');
	createCookie ('RDB', '', -1, '/', rootdomain, '');

	//document.cookie="usernamepassword=;Path=/";
	//document.cookie="UserRegID=;Path=/";
	document.location="http://" + mywholedomain + "/apps/pbcs.dll/section?Category=" + mydomain.toUpperCase() + "&RegLogout=1";
}

// The UserRegID cookie isn't set through the above function, so we'll find it and hack it if we have to...
function checkregcookie () {
	var regcookie = readCookie ('UserRegID');
	var rootdomain = window.location.hostname;
	var olddomain = rootdomain;
	rootdomain = rootdomain.replace(/www\./, '');

	if (!rootdomain.match (/forums\./)) {
		rootdomain = "." + rootdomain;
		if (regcookie) {
			createCookie('UserRegID', '', -1, '/', '', '');
			createCookie('UserRegID', regcookie, 365, "/", rootdomain, '');

			// We need to deal with the 'usernamepassword' cookie as well.
			var up = readCookie ('usernamepassword');
			createCookie('usernamepassword', '', -1, '/', '', '');
			createCookie('usernamepassword', up, 0, "/", rootdomain, '');
			createCookie('ForumUser', ForumUser, 365, '/', rootdomain, '');
		} else {
			createCookie('UserRegID', '', -1, '/', '', '');
			// Don't forget the login cookie.
			createCookie('login', '', -1, '/', '', '');
			// We may have a ForumUser cookie to mess with as well.
			createCookie ('ForumUser', '', -1, '/', rootdomain, '');
			// Publicus create a whole bunch of cookies we should get rid of.
			createCookie ('s_sq', '', -1, '/', rootdomain, '');
			createCookie ('s_cc', '', -1, '/', rootdomain, '');
			createCookie ('rsi_ct', '', -1, '/', rootdomain, '');
			createCookie ('PBCSSESSIONID', '', -1, '/', rootdomain, '');
			createCookie ('GCIONSN', '', -1, '/', rootdomain, '');
			createCookie ('RDB', '', -1, '/', rootdomain, '');
		}
	}
}

var rootdomain = window.location.hostname;
rootdomain = rootdomain.replace(/www\./, '');
rootdomain = "." + rootdomain;

window.onload = checkregcookie ();