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=bademail";
     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>";
 }
 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.pwd.focus();
		frm.pwd.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="/apps/pbcs.dll/frontpage?RegLogout=1";
}