

function IsValidCharsInEmail(part) {
   allPossibleChars = "abcdefghijklmnopqrstuvwxyz_-.";
   allPossibleNums = "0123456789";
   valid=1;
   
   for(i=0; i<part.length; i++) {
      c = part.substr(i,1);
      //alert('c = ' + c);
      if(allPossibleNums.indexOf(c) == -1) { //if c is not a number
         c1 = c.toLowerCase();  //make c lowercase
   	 if(allPossibleChars.indexOf(c1) == -1) { //if c is not a valid character
   	    valid=0;
   	    //alert('Invalid character found in e-mail address: "' + c1 + '"');
   	    break;
   	 }
      }
   } //end for loop
   
   return valid;
}

function IsValidEmail(emailStr) {
     result=1;
     
     if(document.FrontPage_Form1.emailConfirmation[0].checked==true) {
     
        if(emailStr.length>0) {
           firstPart = emailStr.substr(0, emailStr.indexOf("@"));
        
           if(firstPart.length != 0) {
              lastPartLength = ((emailStr.length)-(firstPart.length));
              lastPart = emailStr.substr(firstPart.length + 1, lastPartLength);
           
              if(IsValidCharsInEmail(firstPart)==1)  { //if all characters are valid in firstPart
                 if(IsValidCharsInEmail(lastPart)==1)  { //if all characters are valid in lastPart
                    //make sure there is a "." in the last part of the e-mail address
                    if(lastPart.indexOf(".") != -1) { 
                       //make sure the first and last characters of the e-mail address are not a "."
                       if((emailStr.substr(0,1) != ".") && (emailStr.substr(firstPart.length, 1) != "."))  {
                          //get length of "com", "edu", etc.  It should be two or three characters long
                          endLength = (lastPart.length)-(lastPart.lastIndexOf(".")+1); 
                          if((endLength==2)||(endLength==3)) {
                             if((emailStr.indexOf("..") == -1) && (emailStr.indexOf("@@") == -1)){
                             }
                             else {
                                result=0;
                                alert('Invalid character sequence found in e-mail field: ' + 
                                     emailStr.substr(emailStr.indexOf("..")));
                             }
                          }
                          else {
                             result=0;
                             alert('INVALID E-MAIL ADDRESS: "' + emailStr + '".' + '\u000A' + '\u000A' +
                            'E-mail address must end with ".com", ".edu", etc.');
                          }
                       }
                       else {
                          result=0;
                          alert('INVALID E-MAIL ADDRESS: ' + emailStr);
                       }
                    }
                    else {
                       result=0;
                       alert('INVALID E-MAIL ADDRESS: "' + emailStr + '".' + '\u000A' + '\u000A' +
                            'E-mail address must end with ".com", ".edu", etc.');
                    }
                 }
                 else {
                    result=0;
                    alert('INVALID E-MAIL ADDRESS: "' + emailStr + '".' + '\u000A' + '\u000A' +
                    '             Invalid character found --> "' + c1 + '"');
                 }
              }
              else {
                 result=0;
                 alert('INVALID E-MAIL ADDRESS: "' + emailStr + '".' + '\u000A' + '\u000A' +
                    '             Invalid character found --> "' + c1 + '"');
              }
           }
           else {
              result=0;
              alert('INVALID E-MAIL ADDRESS: ' + emailStr);
           }
        }
        else {
           result=0;
           alert('If you would like confirmation e-mail, you must enter an email address!!');
        }
     }
     
     return result;
}

function IsValidEmailSpecial(emailStr) {
     result=1;

     if(document.FrontPage_Form1.Contact_Method[0].checked==true) {

        if(emailStr.length>0) {
           firstPart = emailStr.substr(0, emailStr.indexOf("@"));

           if(firstPart.length != 0) {
              lastPartLength = ((emailStr.length)-(firstPart.length));
              lastPart = emailStr.substr(firstPart.length + 1, lastPartLength);

              if(IsValidCharsInEmail(firstPart)==1)  { //if all characters are valid in firstPart
                 if(IsValidCharsInEmail(lastPart)==1)  { //if all characters are valid in lastPart
                    //make sure there is a "." in the last part of the e-mail address
                    if(lastPart.indexOf(".") != -1) {
                       //make sure the first and last characters of the e-mail address are not a "."
                       if((emailStr.substr(0,1) != ".") && (emailStr.substr(firstPart.length, 1) != "."))  {
                          //get length of "com", "edu", etc.  It should be two or three characters long
                          endLength = (lastPart.length)-(lastPart.lastIndexOf(".")+1);
                          if((endLength==2)||(endLength==3)) {
                             if((emailStr.indexOf("..") == -1) && (emailStr.indexOf("@@") == -1)){
                             }
                             else {
                                result=0;
                                alert('Invalid character sequence found in e-mail field: ' +
                                     emailStr.substr(emailStr.indexOf("..")));
                             }
                          }
                          else {
                             result=0;
                             alert('INVALID E-MAIL ADDRESS: "' + emailStr + '".' + '\u000A' + '\u000A' +
                            'E-mail address must end with ".com", ".edu", etc.');
                          }
                       }
                       else {
                          result=0;
                          alert('INVALID E-MAIL ADDRESS: ' + emailStr);
                       }
                    }
                    else {
                       result=0;
                       alert('INVALID E-MAIL ADDRESS: "' + emailStr + '".' + '\u000A' + '\u000A' +
                            'E-mail address must end with ".com", ".edu", etc.');
                    }
                 }
                 else {
                    result=0;
                    alert('INVALID E-MAIL ADDRESS: "' + emailStr + '".' + '\u000A' + '\u000A' +
                    '             Invalid character found --> "' + c1 + '"');
                 }
              }
              else {
                 result=0;
                 alert('INVALID E-MAIL ADDRESS: "' + emailStr + '".' + '\u000A' + '\u000A' +
                    '             Invalid character found --> "' + c1 + '"');
              }
           }
           else {
              result=0;
              alert('INVALID E-MAIL ADDRESS: ' + emailStr);
           }
        }
        else {
           result=0;
           alert('If you would like confirmation e-mail, you must enter an email address!!');
        }
     }

     return result;
}

function threeWayRadioFieldAction(radioButton, fieldsDisabled, fieldsEnabled, str) {
    arrayOfEnabledFields = new Array(25);
    arrayOfDisabledFields = new Array(25);
    
    arrayOfEnabledFields = fieldsEnabled.split(",");
    arrayOfDisabledFields = fieldsDisabled.split(",");
    
    if(document.FrontPage_Form1.Contact_Method[2].checked==true) {
       for (i=0; i<arrayOfDisabledFields.length; i++) {
          eval("disable(document.FrontPage_Form1." + arrayOfDisabledFields[i] +")");
          //alert("arrayOfDisabledFields[" + i + "] = " + arrayOfDisabledFields[i]);
          eval("document.FrontPage_Form1." + arrayOfDisabledFields[i] + ".value=str");
       }
    }
    else {
       for (i=0; i<arrayOfEnabledFields.length; i++) {
          //alert("arrayOfEnabledFields[" + i + "] = " + arrayOfEnabledFields[i]);
             blank = '';
             eval("enable(document.FrontPage_Form1." + arrayOfEnabledFields[i] +")");
             if(eval("document.FrontPage_Form1." + arrayOfEnabledFields[i] + ".value==str")) {
                eval("document.FrontPage_Form1." + arrayOfEnabledFields[i] + ".value=blank");
             }
       }
       for (j=0; j<arrayOfDisabledFields.length; j++) {
          eval("disable(document.FrontPage_Form1." + arrayOfDisabledFields[j] +")");
          //alert("arrayOfDisabledFields[" + j + "] = " + arrayOfDisabledFields[j]);
          eval("document.FrontPage_Form1." + arrayOfDisabledFields[j]+ ".value=str");
       }
    }
 }
   

function radioFieldAction(radioButton, field, str) {
   if((radioButton[0].checked==false) && (radioButton[1].checked==false)) {
      field.value="";
   }
   else if(radioButton[0].checked==true) {
      enable(field);
      if(field.value==str) {
        field.value="";
      }
      else {
        //do nothing
      }
   }
   else {
      disable(field);
      field.value=str;
   }
}

function submitForm() {
   setRecipients();
   if(IsValidEmail(document.FrontPage_Form1.email.value)==1)  {
      document.FrontPage_Form1.submit();
      //alert('E-mail address is valid.');
   }
   else {
      alert('Error:  Could not submit form.');
   }
}
      
function setRequiredFields(ReqFields) {
   if(document.FrontPage_Form1.emailConfirmation[0].checked==true) {
      document.FrontPage_Form1.formRequiredFields.value = ReqFields + ', email';
   }
   else {
      document.FrontPage_Form1.formRequiredFields.value = ReqFields;
   }
}

function setRecipients() {
   
   if(document.FrontPage_Form1.emailConfirmation[0].checked==true){
      document.FrontPage_Form1.formRecipients.value = document.FrontPage_Form1.formAscentRecipient.value + ', ' + document.FrontPage_Form1.email.value;
   }
   else {
      document.FrontPage_Form1.formRecipients.value = document.FrontPage_Form1.formAscentRecipient.value;

   }
}
   
   
   
// This function courtesy of Javascript
function BrowserCheck()  {
   var b = navigator.appName;
   if(b=="Netscape") 
      this.b = "ns";
   else if(b=="Microsoft Internet Explorer") 
      this.b = "ie";
   else 
      this.b = b;
}

function details() {
   //THE CURRENT DOCUMENT IS CONTAINED WITHIN A FRAMESET

   //Get Array Size
   for(i=0; i<document.forms.length; i++) {
      nArraySize += document.forms[i].elements.length;
   }

   this.arrElementsNames = new Array(nArraySize);
   var k=0;

   //Go through each element : assign name &  readonly value to associative array
	for(i=0; i<document.forms.length; i++) {
	   for(j=0; j<document.forms[i].elements.length; j++) {
	      if(document.forms[i].elements[j].type == "select-multiple") {
	         arrListBoxValue[document.forms[i].elements[j].name] = document.forms[i].elements[j].selectedIndex;
	      }
	      arrElementsStatus[document.forms[i].elements[j].name] = 'editable';
	      arrElementsNames[k++] = document.forms[i].elements[j].name;
	   }
	}
}


function disableAll() {
  if(ver.b == 'ns') {
    for(i=0; i<document.forms.length; i++) {
       for(j =0; j<document.forms[i].elements.length; j++) {
	 if(document.forms[i].elements[j].type == "select-multiple")
	   arrListBoxValue[document.forms[i].elements[j].name] = document.forms[i].elements[j].selectedIndex;
       }
    }
    for(j=0; j < arrElementsNames.length; j++) {
	arrElementsStatus[arrElementsNames[j]] = 'readonly';
    }
  }
  else {
    for(i=0; i<document.all.length; i++) {
	//if (document.all[i].type == "radio" && document.all[i].checked == 1)
	document.all[i].disabled = 1;
    }
  }

 // THE NEXT 2 LINES ENABLE THE 'ENABLE ALL' BUTTON
 // SO IT NEEDS TO BE REMOVED WHEN THE SCRIPT IS USED ELSEWHERE.

    arrElementsStatus['btnEnAll'] = "editable";
    document.forms[0].btnEnAll.disabled = 0;
}

function enableAll() {
   if(ver.b == 'ns') {
      for(j=0; j < arrElementsNames.length; j++) {
	arrElementsStatus[arrElementsNames[j]] = 'editable';
      }
   }
   else {
      for (i=0; i<document.all.length; i++) {
	//if(document.all[i].type == "radio" && document.all[i].checked == 1)
	   document.all[i].disabled = 0;
      }
   }
}

function enable(objName) {
   if(ver.b == 'ns') {
     if(objName[0] != null && objName[0].type == 'radio') {
	arrElementsStatus[objName[0].name] = 'editable';
     }
     else {
	this.arrElementsStatus[objName.name] = 'editable';
     }
   }
   else {
      if(objName.length  && objName[0].type == 'radio') {
	for(i=0; i<objName.length; i++) {
	   objName[i].disabled = 0;
	}
      }
      else {
	objName.disabled = 0;
      }
   }
}

function disable(objName) {
   if(ver.b == 'ns') {
      if(objName[0] != null  && objName[0].type == 'radio') {
	arrElementsStatus[objName[0].name] = 'readonly';
      }
      else {
	if(objName.type == "select-multiple") {
	  arrListBoxValue[objName.name] = objName.selectedIndex;
	}
	this.arrElementsStatus[objName.name] = 'readonly';
      }
   }
   else {
      if(objName.length  && objName[0].type == 'radio') {
	 for(i=0; i<objName.length; i++) {
	    objName[i].disabled = 1;
	 }
      }
      else {
	 objName.disabled = 1;
      }
   }
}

function checkEvent(e) {
   if(e.target !="") {
      if(ver.b == 'ns') {
	 if(e.target.name) {
	   if(arrElementsStatus[e.target.name] == 'readonly') {
	      if(e.target.type == "select-multiple") {
		 if(arrListBoxValue[e.target.name] != -1) {
		    e.target[arrListBoxValue[e.target.name]].selected = true;
		 }
		 else {
		    e.target.selectedIndex = -1;
		 }
	      }
	      e.target.blur();
	      return false;
	   }
	   else {
	   return true;
	 }
      }
   }
   else {
      return true;
   }
  }
}

function init() {
   if(ver.b == 'ns') {
      netscapeItems = new details();
   }
   radioFieldAction(document.FrontPage_Form1.emailConfirmation, document.FrontPage_Form1.email, '<No E-mail Requested>');
}

var netscapeItems;
ver = new BrowserCheck()
var o;
var arrElementsStatus = new Array();
var arrElementsNames = new Array();
var arrListBoxValue = new Array();


if(ver.b == 'ns') {
   this.document.captureEvents(Event.MOUSEDOWN);
   this.document.captureEvents(Event.FOCUS);
   this.document.onMouseDown=checkEvent;
   this.document.onFocus=checkEvent;
}

