// Javascript to check for appropriate values in NMA Contact_Editor form

function checkform(form) {

        if (form.fname.value.length == 0) {
                alert("Please tell us your first name.");
                form.fname.focus();
                return false;
              }                

        if (form.lname.value.length == 0) {
                alert("Please tell us your last name.");
                form.lname.focus();
                return false;
                }

        if (form.email.value.length == 0) {
                alert("Please tell us your email address.");
                form.email.focus();
                return false;
                }                       

        if (form.dobmo.selectedIndex == 0) {
                alert("Please tell us your date of birth.");
                form.dobmo.focus();
                return false;
                }
        if (form.dobdd.selectedIndex == 0) {
                alert("Please tell us your date of birth.");
                form.dobdd.focus();
                return false;
                }
        if (form.dobyr.selectedIndex == 0) {
                alert("Please tell us your date of birth.");
                form.dobyr.focus();
                return false;
                }
        if (isValidDate(form.dobyr[form.dobyr.selectedIndex].value, form.dobmo[form.dobmo.selectedIndex].value, form.dobdd[form.dobdd.selectedIndex].value) == false) {
                alert("Invalid Date. Please tell us your date of birth.");
                form.dobyr.focus();
                return false;
                }
	if (getAge(form.dobyr[form.dobyr.selectedIndex].value, form.dobmo[form.dobmo.selectedIndex].value, form.dobdd[form.dobdd.selectedIndex].value) < 13 ) {
		window.location="contactus_ua.html";
		return false;
	}

// Define mailStr for pattern analysis
   var mailStr = form.email.value;

/* 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 +")*$")

/* Finally, let's start trying to figure out if the supplied address is
   valid. */

/* Begin with the coarse pattern to simply break up user@domain into
   different pieces that are easy to analyze. */

 var matchArray=mailStr.match(emailPat)  

if (matchArray==null) {
  /* Too many/few @'s or something; basically, this address doesn't
     even fit the general mould of a valid e-mail address. */
	alert("Email address seems incorrect (should look something like john.doe@there.edu)")
    form.email.focus()
	 return false
  }

var user=matchArray[1]
var domain=matchArray[2]
// See if "user" is valid 

if (user.match(userPat)==null) {
    // user is not valid
    alert("The username doesn't seem to be valid, should be something like jane.doe.")
    form.email.focus();
    return false
}

/* 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) {
	        alert("Destination IP address is invalid!")
	         form.email.focus();
		return false
	    }
    }
    return true
}

// Domain is symbolic name

var domainArray=domain.match(domainPat)

if (domainArray==null) {
	alert("The domain name, the portion after the @,\ndoesn't seem to be valid.\nShould be something like somewhere.com")
	form.email.focus();
    return false
}

/* domain name seems valid, but now make sure that it ends in a
   three-letter word (like com, edu, gov) or a two-letter word,
   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)

// dm is the actual domain name after the last period
var dm=domArr[domArr.length-1];
var len=domArr.length
var legalDomain = 0;

// Don't check actual country values -- 2-letter domains like us, uk, ca, tv
// Assume they are correct
if (domArr[domArr.length-1].length==2) {
      legalDomain = 1; }

// Check for all legal three letter, com, edu, gov, int, mil, net, org, biz
else if (domArr[domArr.length-1].length==3 && 
        (dm=='com' || dm=='net' || dm=='edu' || dm=='org' || dm=='gov' || 
         dm=='mil' ||  dm=='int' || dm=='biz')) {
      legalDomain = 1; }

// Check for the new domains longer than three-letters
else if (dm=='info' || dm=='name' || dm=='museum' || dm=='coop' || dm=='aero') {
      legalDomain = 1; }

if (legalDomain == 0) {
var errStr = "An address must end in a two-letter country domain (like us, uk, ca)\n";
  errStr = errStr + "or a three-letter choice of -- com edu gov int mil net org biz --\n";
  errStr = errStr + "or one of the newer four-letter plus domains -- aero coop info museum name.";
        alert (errStr);
	form.email.focus();   
   return false
   }


// Make sure there's a host name preceding the domain.
if (len<2) {
   var errStr="This address is missing a hostname\nlike \"somewhere\" in j.doe@somewhere.net!"
   alert(errStr)
	form.email.focus();
	return false
}

// Check to see if message has content

        if (form.message.value.length == 0) {
                alert("Please enter your message.");
                form.message.focus();
                return false;
              }

// If we've gotten this far, everything's in good shape

return true;

}

function isValidDate(iY, iM, iD){
  // expects yyyy, mm, dd
  iM = iM-1;
  var dTemp = new Date(iY, iM, iD, 0, 0, 0);
  var bValid = false;
  if ( dTemp ) {
    if ( dTemp.getFullYear() == iY && dTemp.getMonth() == iM && dTemp.getDate() == iD ) {
      bValid = true;
    }
  } 
  return bValid;
}

function getAge(iY, iM, iD){
  // expects yyyy, mm, dd
  var iAge = 0;
  var doTemp = new Date();
  var iCurYear=doTemp.getFullYear();
  var iCurMonth=doTemp.getMonth()+1;
  var iCurDay=doTemp.getDate();
  iAge = iCurYear-iY;
  if ( iCurMonth<iM ) {
    iAge--;
  } else if ( iCurMonth==iM && iCurDay<iD ) {
    iAge--;
  }
  return iAge;
}

