
function IsEmailFmt(strEmail) {
  // The following pattern is used to check if the entered e-mail address
  // fits the user@domain format.  It also is used to separate the username
  // from the domain.
  var emailPat = /^(.+)@(.+)$/;

  // The following string represents the pattern for matching all special
  // characters.  We don't want to allow special characters in the address. 
  // These characters include ( ) < > @ , ; : \ " . [ ]
  var specialChars = "\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";

  // The following string represents the range of characters allowed in a 
  // username or domainname.  It really states which chars aren't allowed.
  var validChars = "\[^\\s" + specialChars + "\]";

  // The following pattern applies if the "user" is a quoted string (in
  // which case, there are no rules about which characters are allowed
  // and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
  // is a legal e-mail address.
  var quotedUser = "(\"[^\"]*\")";

  // The following pattern applies for domains that are IP addresses,
  // rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
  // e-mail address. NOTE: The square brackets are required.
  var ipDomainPat = /^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;

  // The following string represents an atom (basically a series of
  // non-special characters.)
  var atom = validChars + '+';

  // The following string represents one word in the typical username.
  // For example, in john.doe@somewhere.com, john and doe are words.
  // Basically, a word is either an atom or quoted string.
  var word = "(" + atom + "|" + quotedUser + ")";

  // The following pattern describes the structure of the user
  var userPat = new RegExp("^" + word + "(\\." + word + ")*$");

  // The following pattern describes the structure of a normal symbolic
  // domain, as opposed to ipDomainPat, shown above.
  var domainPat = new RegExp("^" + atom + "(\\." + atom +")*$");

  // Begin with the coarse pattern to simply break up user@domain into
  // different pieces that are easy to analyze.
  var matchArray = strEmail.match(emailPat);
  if (matchArray == null) return "Email address seems incorrect (check @ and .'s)";

  // See if "user" is valid 
  var user=matchArray[1];
  var domain=matchArray[2];
  if (user.match(userPat) == null) return "The username doesn't seem to be valid.";

  // if the e-mail address is at an IP address (as opposed to a symbolic
  // host name) make sure the IP address is valid.
  var IPArray = domain.match(ipDomainPat);
  if (IPArray != null) {
    // this is an IP address
    for (var i=1;i<=4;i++) {
      if (IPArray[i]>255) return "Destination IP address is invalid (e.g. 255.255.255.255)!"; } }

  // Domain is symbolic name
  var domainArray=domain.match(domainPat);
  if (domainArray==null) return "The email address domain name doesn't seem to be valid.";

  // domain name seems valid, but now make sure that it ends in a
  // three or four-letter TLD (like com, edu, gov, info) or a two-letter TLD,
  // representing country (uk, nl), and that there's a hostname preceding 
  // the domain or country.

  // Now we need to break up the domain to get a count of how many atoms
  // it consists of.
  var atomPat = new RegExp(atom,"g");
  var domArr = domain.match(atomPat);
  var len = domArr.length;
  if (domArr[domArr.length-1].length < 2 || domArr[domArr.length-1].length > 4) {
    return "The email address must end in a two, three, or four-letter top-level \n domain (e.g. CA, UK, TV, COM, NET, ORG, INFO, etc.)."; }

  // Make sure there's a subdomain preceding the TLD.
  if (len < 2) return "The email domain is incomplete!";

  // Subdomain minimum length check
  if (domArr[domArr.length-2].length < 2) {
    return "The email address must contain at least a two-letter domain (e.g. GE.com, MSN.com, etc.)."; }

  return ""; }


function IsDomainFmt(strDomain) {
  var tchar;
  var tlen = strDomain.length;

  // check for minimum maximum domain name length
  if (tlen < 2) return 'A domain name must be at least 2 characters in length.';
  if (tlen > 63) return 'A domain name cannot exceed 63 characters in length.';

  // check for alphanumeric first and last character
  tchar = strDomain.substring(0,1);
  if (!( ((tchar.toUpperCase() >= 'A') && (tchar.toUpperCase() <= 'Z')) ||
         ((tchar >= '0') && (tchar <= '9')) ))
    return 'The first character in a domain name must be alphanumeric.';

  tchar = strDomain.substring(tlen-1,tlen);
  if (!( ((tchar.toUpperCase() >= 'A') && (tchar.toUpperCase() <= 'Z')) ||
         ((tchar >= '0') && (tchar <= '9')) ))
    return 'The last character in a domain name must be alphanumeric.';

  // make sure the rest of the domain name is only hyphens and alphanumeric characters
  for (i = 1; i < tlen-1; i++) {
     tchar = strDomain.substring(i,i+1);
     if (!( ((tchar.toUpperCase() >= 'A') && (tchar.toUpperCase() <= 'Z')) ||
            ((tchar >= '0') && (tchar <= '9')) || (tchar == '-') ))
       return 'A domain name can contain only hyphens and alphanumeric characters.';
  }
  return '';
}


function Validate() {
  var doSubmit = true;
  var strError = '';

  // check for value in CUSTFIRSTNAME field
  if (document.frmSignup.CUSTFIRSTNAME.value == '') {
    alert('Please enter your first name.');
    document.frmSignup.CUSTFIRSTNAME.focus();
    doSubmit = false; }

  // check for value in CUSTLASTNAME field
  if ((doSubmit) && (document.frmSignup.CUSTLASTNAME.value == '')) {
    alert('Please enter your last name.');
    document.frmSignup.CUSTLASTNAME.focus();
    doSubmit = false; }

  // check for valid email address
  strError = IsEmailFmt(document.frmSignup.EMAIL.value);
  if ((doSubmit) && (strError != '')) {
    alert(strError + '\n\nPlease enter a valid email address.');
    document.frmSignup.EMAIL.focus();
    doSubmit = false; }

  // check for value in CUSTDOMAIN field
  strError = IsDomainFmt(document.frmSignup.CUSTDOMAIN.value);
  if ((doSubmit) && (strError != '')) {
    alert(strError + '\n\nPlease enter a valid domain name.');
    document.frmSignup.CUSTDOMAIN.focus();
    doSubmit = false; }

  // if no errors then submit the form
  if (doSubmit == true) { document.frmSignup.submit(); } }


function popUp(url) {
  newWin=window.open(url,"win",'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=1,width=500,height=300');
  self.name = "mainWin"; }


function CheckAvailability() {
  var strError = IsDomainFmt(document.frmSignup.CUSTDOMAIN.value);
  if (strError != '') {
    alert(strError + '\n\nPlease enter a valid domain name.');
    document.frmSignup.CUSTDOMAIN.focus(); }
  else {
    popUp('/checkavailability.asp?domain=' + document.frmSignup.CUSTDOMAIN.value + document.frmSignup.CUSTTLD.options[document.frmSignup.CUSTTLD.selectedIndex].text); } }


