/* Source code taken and simplified from original DAPAAA site
 * Author: Willy Wong '99
**/
<!--
// whitespace characters
var whitespace = " \t\n\r";


// CONSTANT STRING DECLARATIONS

var EMPTY_STRING="";
var SPACE=" ";
var DASH ="-";
var COLON =":";

var ENTRYPROMPT = "Please enter "
var LOCATION = "the location";
var SPONSOR = "the sponsor";
var DESCRIPTION = "the description";
var CONTACT = "the contact";
var SUBMITTER = "submitter's name";
var SUBMIT_EMAIL = "submitter's email address";
var MEMBER = "member status.";
var MEMBER1 = "Please select member status.";
var EVENT_NAME = "the event name";
var EVENT_DATE = "the date of the event";
var EVENT_TIME = "the time of the event";
var INVALID_ENTRY1 = "The entry for ";
var INVALID_ENTRY2 = " is invalid";
var THANKYOU_MESSAGE = "Thank you for your time.  Please come and visit again soon";

var INVALID_EMAIL = "This field must be a valid email address (like joe@dartmouth.edu). Please re-enter it now.";
var INVALID_DAY = "The day";
var INVALID_MONTH = "The month";
var INVALID_YEAR = "The year";
var INVALID_DATE_ENTRY1 = " for your date is invalid.  Please re-enter it now using the format mm-dd-yyyy";
var INVALID_DATE = "The Day, Month, and Year combination ";


var MONTH_START = 0;
var MONTH_LENGTH = 2;
var DAY_START = 3;
var DAY_LENGTH =2;
var YEAR_START = 6;
var YEAR_LENGTH = 4;
var YEAR_MAX = 9999;
var MONTH_MIN =1;
var MONTH_MAX = 12;
var DAY_MIN = 1;
var DAY_MAX = 31;
var MAX_AGE = 150;

var daysOfMonth = new Array(12);
daysOfMonth[1] = 31;
daysOfMonth[2] = 29;   // must programmatically check this
daysOfMonth[3] = 31;
daysOfMonth[4] = 30;
daysOfMonth[5] = 31;
daysOfMonth[6] = 30;
daysOfMonth[7] = 31;
daysOfMonth[8] = 31;
daysOfMonth[9] = 30;
daysOfMonth[10] = 31;
daysOfMonth[11] = 30;
daysOfMonth[12] = 31;
/* ***********************************************************
 * Name:		isEmpty
 * Params:		sText - a text string
 * Return:		false if the string is null or is length 0		
 * Description:	Checks that a string is not empty
 *************************************************************/
function isEmpty(sText)
{   return ((sText == null) || (sText.length == 0))
}

/* ***********************************************************
 * Name:		isWhiteSpace
 * Params:		sText - a text string
 * Return:		false if the string is null or is length 0		
 * Description:	Checks that a string is not empty
 *************************************************************/
function isWhitespace (sText)

{   var bContainsWhiteSpace=false;
    var i=0;

    if (isEmpty(sText))
    {	
      bContainsWhiteSpace=true;
    } 
    else 
   {
     for (i = 0; i < sText.length; i++)
     {   
       var c = sText.charAt(i);  //check to see if the current character is whitespace
       if (!(whitespace.indexOf(c) == -1))
       {
         bContainsWhiteSpace = true;
       }
      }
    }
    return bContainsWhiteSpace; 
}

/* ***********************************************************
 * Name:		isDigit
 * Params:		c - a character
 * Return:		true if the character is a digit, false otherwise		
 * Description:	Checks that the character is a digit
 *************************************************************/
function isDigit (c)
{   return ((c >= "0") && (c <= "9"))
}
/* ***********************************************************
 * Name:		isInteger
 * Params:		s - a string
 * Return:		true if the string only contains digits, false otherwise		
 * Description:	Checks that the string only contains digits
 *************************************************************/

function isInteger (s)

{   var nCounter=0;
    var bInteger=true;
    if (isEmpty(s))
    	bInteger=false
    else	
    { 
      // Move through the string checking that each character is a
      // digit.  If any are found that are not, stop checking,
      // set bInteger to false and return it.
    	while(nCounter < s.length && bInteger==true)
    	{   
        var cCurrentChar = s.charAt(nCounter);

        if (!isDigit(cCurrentChar)) 
	  {
        	bInteger=false;
	  }  
	  nCounter++;	
    	}
     	return bInteger;
    }	
}
/* ***********************************************************
 * Name:		isLetter
 * Params:		c - a character
 * Return:		true if the character is a letter, false otherwise		
 * Description:	Checks that the character is a letter
 *************************************************************/
function isLetter (c)
{   return ( ((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")) )
}
/* ***********************************************************
 * Name:		isAlphabetic
 * Params:		s - a string
 * Return:		true if the string only contains letters, false otherwise		
 * Description:	Checks that the string only contains letters
 *************************************************************/
function isAlphabetic (s)

{   var nCounter=0;
    var bAlphabetic=true;
    if (isEmpty(s))
    { 
    	bAlphabetic=false;
    }
    else
    {	     
      // Move through the string checking that each character is a
      // letter.  If any are found that are not, stop checking,
      // set bAlphabetic to false and return it.
       while(nCounter < s.length && bAlphabetic==true)
    	{   
        var cCurrentChar = s.charAt(nCounter);

        if (!isLetter(cCurrentChar))
	  {
        	bAlphabetic=false;
	  }
	  nCounter++;        
	   		}
   	}	
    return bAlphabetic;
}
/* ***********************************************************
 * Name:		isSpace
 * Params:		c - a character
 * Return:		true if the character is a space, false otherwise		
 * Description:	Checks that the character is a space
 *************************************************************/
function isSpace (c)
{   return (c == SPACE)
}
/* ***********************************************************
 * Name:		isAlphaNumeric
 * Params:		s - a string
 * Return:		true if the string only letters or numbers, false otherwise		
 * Description:	Checks that the string only letters or numbers
 *************************************************************/
function isAlphaNumeric (s)

{   var nCounter=0;
    var bAlphaNumeric=true;
    if (isEmpty(s))
    { 
    	bAlphaNumeric=false;
    }
    else
    {	     
      // Move through the string checking that each character is a
      // letter or number.  If any are found that are not, stop checking,
      // set bAlphaNumeric to false and return it.
       while(nCounter < s.length && bAlphaNumeric==true)
    	{   
        var cCurrentChar = s.charAt(nCounter);

        if (!isLetter(cCurrentChar) && !isDigit(cCurrentChar) && !isSpace(cCurrentChar))
	  {
        	bAlphaNumeric=false;
	  }
	  nCounter++;        
	   		}
   	}	
    return bAlphaNumeric;
}

/* ***********************************************************
 * Name:		isDash
 * Params:		c - a character
 * Return:		true if the character is a dash, false otherwise		
 * Description:	Checks that the character is a dash
 *************************************************************/
function isDash (c)
{   return (c == DASH)
}
/* ***********************************************************
 * Name:		isColon
 * Params:		c - a character
 * Return:		true if the character is a colon, false otherwise		
 * Description:	Checks that the character is a colon
 *************************************************************/
function isColon (c)
{   return (c == COLON)
}
/* ***********************************************************
 * Name:		isAlphaNumericTime
 * Params:		s - a string
 * Return:		true if the string only letters or numbers with some valid 
 *				characters, false otherwise		
 * Description:	Checks that the string only letters or numbers or valid character
 *************************************************************/
function isAlphaNumericTime (s)

{   var nCounter=0;
    var bAlphaNumeric=true;
    if (isEmpty(s))
    { 
    	bAlphaNumeric=false;
    }
    else
    {	     
      // Move through the string checking that each character is a
      // letter or number or valid character.  If any are found that are not, stop checking,
      // set bAlphaNumeric to false and return it.
       while(nCounter < s.length && bAlphaNumeric==true)
    	{   
        var cCurrentChar = s.charAt(nCounter);

        if (!isLetter(cCurrentChar) 
			&& !isDigit(cCurrentChar) 
			&& !isSpace(cCurrentChar) 
			&& !isDash(cCurrentChar) 
			&& !isColon(cCurrentChar))
		{
        	bAlphaNumeric=false;
		}
			nCounter++;        
		}
   	}	
    return bAlphaNumeric;
}
/* ***********************************************************
 * Name:		isIntegerInRange
 * Params:		sValToCheck - string value to check
 *		      nLowNum, nHighNum - numbers to chec that sValToCheck is between
 * Return:		true if sValToCheck is between nLowNum and nHighNum, false otherwise		
 * Description:	Check that a number is within the given range.
 *************************************************************/
function isIntegerInRange (sValToCheck, nLowNum, nHighNum)
{   var bInRange=false;
    if (isInteger(sValToCheck))
    {
	// The string parameter is explicity cast to an integer so that
	// arithmetic operators >=, and <= can be used

    	var nValToCheck = parseInt (sValToCheck);
    	bInRange = ((nValToCheck >= nLowNum) && (nValToCheck <= nHighNum));
    }
    return bInRange;	
}
/* ***********************************************************
 * Name:		isEmail
 * Params:		sEmail - a string to check for email format
 * Return:		true if the string has the format x@y.z, false otherwise		
 * Description:	Checks that the string is the correct format for an e-mail address
 *************************************************************/
function isEmail (sEmail)
{   
  var bValid=true;	//default value to return
  //Check that that the field is not empty
  if (isEmpty(sEmail)) 
  {
    bValid=false;
  }	
  else
  {	
    // check that the field is not white space
    if (isWhitespace(sEmail)) 
    {
      bValid = false;
    }	
    else
    {	
    /* In order for an address to be valid there must be at least one character before
       the @ symbol.  So, start looking for the @ symbol at position 1 */
       var nCharCounter = 1;
       var nEmailLength = sEmail.length;

       // look through the e-mail address to find the @ symbol
       while ((nCharCounter < nEmailLength) && (sEmail.charAt(nCharCounter) != "@"))
       {
         nCharCounter++;
        }
        if ((nCharCounter >= nEmailLength) || (sEmail.charAt(nCharCounter) != "@")) 
        {
           bValid = false; //The @ symbol has not been found
        }	
        else	// nCharCounter holds the position of the @ symbol
        { 
           nCharCounter += 2;    /* Increment nCharCounter by 2, because there must
	                      be at least 1 character between the @ and the . 
		       then look through the string until the . is found */
           while ((nCharCounter < nEmailLength) && (sEmail.charAt(nCharCounter) != "."))
           { 
	nCharCounter ++;
           }	
           // Now check that there is at least one character after the .
           if ((nCharCounter >= nEmailLength - 1) || (sEmail.charAt(nCharCounter) != "."))
           {
	bValid = false;
            }
          }
        }
      }
      return bValid;				
}
/* ***********************************************************
 * Name:		checkEmail
 * Params:		theField- a field containing the string to check
 * Return:		true if the contents of the field is a string with email format, false otherwise		
 * Description:	Extracts the string from the field and calls isEmail
 *  			to check the format of the string
 *************************************************************/
function checkEmail (theField)
{   
	var bValid=true;
	if (!isEmail(theField.value))
	{ 
		warnEmailInvalid (theField, INVALID_EMAIL);
		bValid=false;
	}
     return bValid;
}

/* ***********************************************************
 * Name:		isYear
 * Params:		sYear - string value to check
 * Return:		true if sYear is a valid year, false otherwise		
 * Description:	Check that a number could be a valid year for birth of user
 *  			Assumes that users are between the ages of 0 and 150.
 *************************************************************/
function isYear (sYear)
{  
	var today = new Date();
	sThisYear = today.getFullYear();
	return isIntegerInRange (sYear, sThisYear, YEAR_MAX);

}		 
/* ***********************************************************
 * Name:		isMonth
 * Params:		sMonth - string value to check
 * Return:		true if sMonth is a valid month, false otherwise		
 * Description:	Check that a number is a valid month
 *************************************************************/
function isMonth (sMonth)
{   return isIntegerInRange (sMonth, MONTH_MIN, MONTH_MAX); 
}

/* ***********************************************************
 * Name:		isDay
 * Params:		sDay - string value to check
 * Return:		true if sDay is a valid day, false otherwise		
 * Description:	Check that a number is a valid day - note that
 *		      this does not take into account different numbers
 *			of days for different months.
 *************************************************************/
function isDay (sDay)
{   return isIntegerInRange (sDay, DAY_MIN, DAY_MAX);
}
/* ***********************************************************
 * Name:		daysInFebruary
 * Params:		nYear - the number of the year to calculate
 *				 the days in feb for
 * Return:		number of days in february for the given year
 * Description:	This calculates the number of days in February. 
 *************************************************************/
function daysInFebruary (nYear)
{  
   var nNumber_of_days;
   // For any year that is divisible by 4, and either not divisible  
  // by 100 or  is divisible 400 return 29, otherwise return 28. 
   if ((nYear % 4 == 0) && ( (!(nYear % 100 == 0))|| (nYear % 400 == 0) ) )
   {
	nNumber_of_days = 29;
   }	
   else
   {
	nNumber_of_days = 28;	
   }
   return nNumber_of_days; 

}
/* ***********************************************************
 * Name:		isDate
 * Params:		sYear, sMonth, sDay - the year, month and day
 * Return:		true if the number of days is correct for the
 *			month and year, false otherwise
 * Description:	Checks that the year month and day are a valid combination
 *************************************************************/
function isDate (sYear, sMonth, sDay)
{		
	var bValidDate=true;
	var nYear = parseInt(sYear);
	var nMonth = parseInt(sMonth);
	var nDay = parseInt(sDay);
   	
	// catch invalid days, except for February
    	if (nDay > daysOfMonth[nMonth])
    	{
    	  bValidDate = false; 
	}
	else
	{
    	  if ((nMonth == 2) && (nDay > daysInFebruary(nYear))) 
    	  {
    		bValidDate = false;
   	  }	
	}

   return bValidDate;
}

/* ***********************************************************
 * Name:		prompt
 * Params:		sMessage
 * Return:		true
 * Description:	Displays sMessage to the status bar
 *************************************************************/
function prompt (sMessage)
{   window.status = sMessage;
    return true;
}
/* ***********************************************************
 * Name:		promptStringEntry
 * Params:		sMessage
 * Return:		true
 * Description:	Displays a standard message requesting entry 
 *			and sMessage to the status bar
 *************************************************************/
function promptStringEntry (sMessage)
{   window.status = ENTRYPROMPT + sMessage;
    return true;
}
/* ***********************************************************
 * Name:		checkRadioButtonValue
 * Params:		radio - name of the radios to get the value from
 * Return:		true if radio checked, or false if none is checked
 * Description:	checks for a value for the checked radio
 *************************************************************/
function checkRadioButtonValue (radio)
{  
	var radioValue=false;
	
	for(i=0; i<radio.length; i++)
	{
        if(radio[i].checked)
		{
			radioValue = true;
		}
	}

	if(!(radioValue))
	{
		warnMemberInvalid(MEMBER1);
	}

    return radioValue;
}
/* ***********************************************************
 * Name:		warnInvalid
 * Params:		theField - field that contains invalid data
 *	      	sFieldName - description of field
 * Return:		false
 * Description:	Displays a standard message telling the user that the
 *			field - sFieldName is invalid, and selects the
 *		 	field and gives it focus.
 *************************************************************/
function warnInvalid (theField, sFieldName)
{   theField.focus()
    theField.select()
    alert(INVALID_ENTRY1 + sFieldName + INVALID_ENTRY2)
    return false
}

/* ***********************************************************
 * Name:		warnMemberInvalid
 * Params:		theField - field that contains invalid data
 *	      	sFieldName - description of field
 * Return:		false
 * Description:	Displays a standard message telling the user that the
 *			field - sFieldName is invalid, and selects the
 *		 	field and gives it focus.
 *************************************************************/
function warnMemberInvalid (sFieldName)
{   
    alert(sFieldName)
    return false
}
/* ***********************************************************
 * Name:		warnEmailInvalid
 * Params:		theField - field that contains invalid data
 *	      	sFieldName - description of field
 * Return:		false
 * Description:	Displays a standard message telling the user that the
 *			field - sFieldName is invalid, and selects the
 *		 	field and gives it focus.
 *************************************************************/
function warnEmailInvalid (theField, sFieldName)
{   theField.focus()
    theField.select()
    alert(sFieldName)
    return false
}
/* ***********************************************************
 * Name:		warnDateInvalid
 * Params:		theField - field that contains invalid data
 *	      	sMessage - specific part of message
 * Return:		false
 * Description:	Displays a message telling the user that a section
 *			of the date is invalid, and selects the
 *		 	field and gives it focus.
 *************************************************************/
function warnDateInvalid (theField, sMessage)
{   
    theField.select()
    theField.focus()
    alert(sMessage + INVALID_DATE_ENTRY1)
    return false
}
/* ***********************************************************
 * Name:		checkString
 * Params:		theField - field to check
 *	      	sFieldDescription - Description of the field that is being checked
 * Return:		true if the field is alphanumeric, false otherwise
 * Description:	Extracts the contents of the field and checks
 *	that it is alphanumeric, if not displys an error
 *	message.
 *************************************************************/
function checkString (theField, sFieldDescription)
{   
    var bString=true;
    if(!isAlphaNumeric(theField.value))
    {
		warnInvalid(theField,sFieldDescription);
		bString=false;
    } 		 
    return bString;
}

/* ***********************************************************
 * Name:		checkTime
 * Params:		theField - field to check
 *	      	sFieldDescription - Description of the field that is being checked
 * Return:		true if the field is alphanumeric with some valid characters, false otherwise
 * Description:	Extracts the contents of the field and checks
 *	that it is alphanumeric with some valid characters, if not displys an error
 *	message.
 *************************************************************/
function checkTime (theField, sFieldDescription)
{   
    var bString=true;
    if(!isAlphaNumericTime(theField.value))
    {
		warnInvalid(theField,sFieldDescription);
		bString=false;
    } 		 
    return bString;
}
/* ***********************************************************
 * Name:		checkDate
 * Params:		theField - field that contains the date
 * Return:		true if each part of the date is valid, false otherwise
 * Description:	Extracts the day, month and year from theField
 *			and checks that they are all numeric, in the
 *			correct ranges and when combined form a 
 *			valid date
 *************************************************************/
function checkDate(sDate)
{
      var bValid = true;
	if (isEmpty(sDate))
	{
		bValid = false;
	}
	else
	{
	  sMonth = "" + sDate.value.substr(MONTH_START, MONTH_LENGTH);
	  sDay = "" + sDate.value.substr(DAY_START, DAY_LENGTH);
  	  sYear = "" + sDate.value.substr(YEAR_START, YEAR_LENGTH);

        if (!(isYear(sYear)))
	  {
		warnDateInvalid (sDate,INVALID_YEAR)
		bValid = false;
	  }
    	  else 
    	  {
	    if (!(isMonth(sMonth)))
	    {
	 	warnDateInvalid(sDate, INVALID_MONTH)
	 	bValid = false;
	    }
	    else
	    {
	      if (!(isDay(sDay)))
	      {
	 	 warnDateInvalid(sDate, INVALID_DAY);
	 	 bValid = false;
	      }
	      else
	      {
		 if (!(isDate (sYear, sMonth, sDay)))
  		 {
	  	   warnDateInvalid(sDate, INVALID_DATE);
	  	   bValid = false;
   		 }
	      }
	    }
        }
     }	
     return bValid
}

/* ***********************************************************
 * Name:		checkAll
 * Params:		form - name of the form to check
 * Return:		true if all validation is OK, false otherwise
 * Description:	Perform all validation on the user entriess.  If
 *		      an invalid entry is found an error message is
 * 			displayed and the function exits, otherwise
 *			a thankyou message is displayed.
 *************************************************************/
function checkAll(form)
{
	var bCheckOK = false;
	if(checkString(form.eventName,EVENT_NAME))
	{
		if(checkString(form.location,LOCATION))
		{
			if(checkDate(form.eventDate))
			{
				if(checkTime(form.eventTime,EVENT_TIME))
				{
					if(checkString(form.sponsor,SPONSOR))
					{
						if(checkString(form.contact,CONTACT))
						{
							if(checkString(form.description,DESCRIPTION))
							{
								if(checkString(form.submitter,SUBMITTER))
								{			
									if(checkEmail(form.submit_email))
									{
										if(checkRadioButtonValue(form.member))
										{
											bCheckOK = true;
										}	
									}
								}
							}
						}
					}
				}
			}
		}
	}
	if (bCheckOK==true)
	{
		alert(THANKYOU_MESSAGE);
	}
	return bCheckOK;		
}
-->
