//*********************************************
//** This file contains the functions used to
//** Edit and Validate data in the e-Training
//** application.
//*********************************************

function PlaceFocus()
{
if (document.forms.length > 0) {
  var field = document.forms[0];
  for (i = 0; i < field.length; i++) {
    if ((field.elements[i].type == "text") || (field.elements[i].type == "textarea") || (field.elements[i].type.toString().charAt(0) == "s")) {
    document.forms[0].elements[i].focus();
  break;
         }
      }
   }
}

function w_onload()
{
    window.parent.frames(0).location = "banner.asp";
    window.parent.frames(1).location = "left_side.asp";
}

function comparePasswords(form) {
  var pw1 = form.password.value
  var pw2 = form.vpassword.value
  if (pw1 == '' || pw2 == '') {
    alert('Please enter your password twice.');
    form.password.focus();
  }
  else {
    if (pw1 != pw2) {
      alert ("You did not enter the same new password twice.\n Please re-enter your password.");
      form.password.focus();
    }
  }
}

function notyet()
{
  alert("Function not availble in this Release");
}

function CheckForm(cmd)
{
  var msg = ''
//  alert("Processing for the Following: " + cmd);
  switch (cmd)
  {
    case "cat":
      if(etMain.descr.value == ''){
        msg = "Please Enter the Category Description"
      }
    break;

    case "sub":
      if(etMain.descr.value == ''){
        msg = "Please Enter the Subcategory Description\n"
      }
      if(etMain.catcode.value == ''){
        msg = msg + "Please Enter a Category Code\n"
      }
//      if(etMain.minstdnt.value == ''){
//        msg = msg + "Please Enter Class Minimum\n"
//      }
//      if(etMain.maxstdnt.value == ''){
//        msg = msg + "Please Enter class Max\n"
//      }
//      if(etMain.duration.value == ''){
//        msg = msg + "Please Enter class duration\n"
//      }
    break;

    case "crse":
      if(etMain.descr.value == ''){
        msg = "Please Enter the Course Title\n"
      }
      if(etMain.status.value == ''){
        msg = msg + "Please Enter Course Status\n"
      }
      if(etMain.minstdnt.value == ''){
        msg = msg + "Please Enter Class Minimum\n"
      }
      if(etMain.maxstdnt.value == ''){
        msg = msg + "Please Enter class Max\n"
      }
      if(etMain.duration.value == ''){
        msg = msg + "Please Enter class duration\n"
      }
      if(etMain.edunits.value == ''){
        msg = msg + "Please Enter Course Units\n"
      }
      if((etMain.schoolcd.value == '') && (etMain.school.value == '')){
        msg = msg + "Please Enter a School Code or School Name\n"
      }
      if(etMain.cat.value == ''){
        msg = msg + "Please Enter a Category Code"
      }
    break;

    case "empl":
      if(etMain.firstname.value == ''){
        msg = "Please Enter the First Name\n"
      }
      if(etMain.lastname.value == ''){
        msg = msg + "Please Enter the Last Name\n"
      }
      if(etMain.phone.value == ''){
        msg = msg + "Please Enter a Phone Number\n"
      }else{
        msg = ValidatePhone(etMain.phone);
      }
      if(etMain.emailaddr.value ==''){
        msg = msg + "Please Enter your Email Address\n"
      }else{
        msg = msg + EmailCheck(etMain.emailaddr.value);
      }
      if(etMain.address1.value == ''){
        msg = msg + "Please Enter Address Line 1\n"
      }
      if(etMain.city.value == ''){
        msg = msg + "Please Enter the City\n"
      }
      if(etMain.state.value == ''){
        msg = msg + "Please Enter a State/Province\n"
      }
      if(etMain.postal.value == ''){
        msg = msg + "Please Enter a Postal Code"
      }else{
        msg = msg + CheckZip(etMain.postal);
      }
    break;

    case "tvp":
      if(etMain.descr.value == ''){
        msg = "Please Enter the Vendor Description\n"
      }
      if(etMain.ventype.value == ''){
        msg = msg + "Please Enter the Vendor Type\n"
      }
      if(etMain.apvenid.value == ''){
        msg = msg + "Please Enter the AP Vendor ID\n"
      }
      if(etMain.pucost.value == ''){
        msg = msg + "Please Enter a Per Unit Cost\n"
      }
      if(etMain.address1.value == ''){
        msg = msg + "Please Enter Address Line 1\n"
      }
      if(etMain.city.value == ''){
        msg = msg + "Please Enter the City\n"
      }
      if(etMain.state.value == ''){
        msg = msg + "Please Enter a State/Province\n"
      }
      if(etMain.postal.value == ''){
        msg = msg + "Please Enter a Postal Code"
      }else{
        msg = msg + CheckZip(etMain.postal);
      }
    break;
    
    case "schl":
      if(etMain.descr.value == ''){
        msg = "Please Enter the School Description\n"
      }
      if(etMain.country.value == ''){
        msg = msg + "Please Enter a country. (ie: USA, CAN)\n"
      }
    break;
    
    case "fac":
      if(etMain.descr.value == ''){
        msg = "Please Enter the Facility Name\n"
      }
      if(etMain.country.value == ''){
        msg = "Please Enter a country. (ie: USA, CAN)\n"
      }
      if(etMain.address1.value == ''){
        msg = msg + "Please Enter Address Line 1\n"
      }
      if(etMain.city.value == ''){
        msg = msg + "Please Enter the City\n"
      }
      if(etMain.state.value == ''){
        msg = msg + "Please Enter a State/Province\n"
      }
      if(etMain.postal.value == ''){
        msg = msg + "Please Enter a Postal Code"
      }else{
        msg = msg + CheckZip(etMain.postal);
      }
    break;
    
    case "dgree":
      if(etMain.descr.value == ''){
        msg = "Please Enter the Degree Description\n"
      }
    break;
    
    case "comp":
      if(etMain.descr.value == ''){
        msg = "Please Enter the Competency Description\n"
      }
    break;
    
    case "cert":
      if(etMain.descr.value == ''){
        msg = "Please Enter the License/Certification Description\n"
      }
    break;
    
    case "mshp":
      if(etMain.descr.value == ''){
        msg = "Please Enter the Membership Description\n"
      }
    break;
  }
  if (msg == ''){
    return true;
  }else{
    return msg;
  }
}

function ValidatePhone(Field){
  var n = Field.name
  var p = Field.value
  var msg = ''
  p = p.replace("(","")
  p = p.replace(")","")
  p = p.replace("-","")
  
  if(p.length == 10){
	  len = p.length;
		p30 = p.substring(0,3);
		p30 = "(" + p30 + ")";
    p31 = p.substring(3,len -4);
		p32 = p.substring(6,len);
		p32 = "-" + p32;
		pp = p30 + p31 + p32;
		document.etMain.phone.value = "";
  	document.etMain.phone.value = pp;
  	msg = ''
	}else{
	    msg = "The phone number is not in the correct format.\n"
	    msg = msg + "Please check it and reenter. ex.(123)456-7890\n"
	}
	return msg;
}

function EmailCheck(Field)
{
  var msg = '';
  /* The following variable tells the rest of the function whether or not
  to verify that the address ends in a two-letter country or well-known
  TLD.  1 means check it, 0 means don't. */
  
  var checkTLD=1;
  
  /* The following is the list of known TLDs that an e-mail address must end with. */
  var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|us|info|pro|museum)$/;

  /* 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=Field.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. */

  msg = "Email address seems incorrect (check @ and .'s)\n"
  return msg;
  }
  var user=matchArray[1];
  var domain=matchArray[2];

  // Start by checking that only basic ASCII characters are in the strings (0-127).

  for (i=0; i<user.length; i++) {
    if (user.charCodeAt(i)>127) {
      msg = "Ths username contains invalid characters.\n"
      return msg;
    }
  }
  for (i=0; i<domain.length; i++) {
    if (domain.charCodeAt(i)>127) {
      msg = "Ths domain name contains invalid characters.\n"
      return msg;
    }
  }

  // See if "user" is valid 

  if (user.match(userPat)==null) {
    msg = "The username doesn't seem to be valid.\n"
    return msg;
  }

  /* 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) {
    for (var i=1;i<=4;i++) {
      if (IPArray[i]>255) {
        msg = "Destination IP address is invalid!\n"
        return msg;
      }
    }
  return msg;
  }

  // Domain is symbolic name.  Check if it's valid.
   
  var atomPat=new RegExp("^" + atom + "$");
  var domArr=domain.split(".");
  var len=domArr.length;
  for (i=0;i<len;i++) {
    if (domArr[i].search(atomPat)==-1) {
      msg = "The domain name does not seem to be valid.\n"
      return msg;
    }
  }

  /* domain name seems valid, but now make sure that it ends in a
  known top-level domain (like com, edu, gov) or a two-letter word,
  representing country (uk, nl), and that there's a hostname preceding 
  the domain or country. */

  if (checkTLD && domArr[domArr.length-1].length!=2 && 
  domArr[domArr.length-1].search(knownDomsPat)==-1) {
    msg = "The address must end in a well-known domain or two letter " + "country.\n"
    return msg;
  }

  // Make sure there's a host name preceding the domain.

  if (len<2) {
    msg = "This address is missing a hostname!\n"
    return msg;
  }

  // If we've gotten this far, everything's valid!
  return msg;
}

function CheckZip(field)
{
  var msg = '';
  var valid = "0123456789-";
  var zip = field.value;
  zip = zip.replace("-","");
  var len = zip.length;

  if (len != 5 && len != 9){
    msg = "Please enter your 5 digit or 5 digit+4 zip code."
    return msg;
  }
  if(zip.length == 9){
	  len = zip.length;
		p30 = zip.substring(0,5);
    p31 = zip.substring(5,len);
		var pp = p30 + "-" + p31;
		document.etMain.postal.value = "";
  	document.etMain.postal.value = zip;
  	msg = ''
	}
	if (zip.length == 5){
		document.etMain.postal.value = "";
  	document.etMain.postal.value = zip;
  	msg = ''
	}
	return msg;
}

//  End -->
