<!--

/*------------------------------------------------------------------------------
    form validation

    usage: <form ... onSubmit="return isFormComplete(this);">

    given an html form

    returns false if there is any problem (after displaying an alert message)
    returns true otherwise

    notes:
    - This takes advantage of the "user-defined parameters" feature allowed in
      IE5+ and NS6+ (e.g., the 'altname' here and 'req' in the validation functions).
      These do not work in earlier browsers. They are case-sensitive, so make sure
      the element.property used in the code matches the property="value" in the html
      (e.g., theElement.altname references altname="Gender" but not ALTname="Gender").

    - The validation functions can be modified to include any parameters necessary.
      For example, you could have a format parameter (format='date' or format='ssn')
      that forced an input element into a particular format.
*/
 
  
function isFormComplete(theForm)
{
  var theElement ;
  var theGroup ;
  var eType ;
  var incr ;
  var eMsg ;

   // traverse the form elements, invoking the appropriate validation function for each

  for ( var i = 0 ; i < theForm.elements.length ; i += incr )
  {
    
    theElement = theForm.elements[i] ;                    // get the element and its type
    eType = theElement.type ;
    incr = 1 ;                                            // assume a non-grouped element
	
    if ( eType.indexOf( 'text' ) == 0 )                   // text or textarea
    {
      eMsg = validTextBox( theElement ) ;
    }
    else
    if ( eType.indexOf( 'select' ) == 0 )                 // select-one or select-multiple
    {
      eMsg = validSelectBox( theElement ) ;
    }
    else
    if ( eType == 'radio' )                               // radio buttons (first element in the group)
    {
    
      if (theElement.name=='as::NRSReporting.1')
      {
      }
      else
      {
      theGroup = theForm[theElement.name] ;                 // grouped elements
      eMsg = validRadioButtons( theElement, theGroup ) ;
      incr = theGroup.length ;                              // skip past the group
      }
    }
    else
    if ( eType == 'checkbox' )                            // checkboxes (first element in the group)
    {
      if (theElement.name=='carryoverstudent')
      {
      
      }
      else
      {
      theGroup = theForm[theElement.name] ;                 // grouped elements     
      eMsg = validCheckBoxes( theElement, theGroup ) ;
      incr = theGroup.length ;                              // skip past the group
      }
    }
	

	 
	 
	
    if ( eMsg )                                         // we have an error message
    {
      var classN = theElement.className;
      var result = classN.split("|");
 	  var req = result[0];
 	  var altname = result[1];
 	  var minchars = result[2];
	  var eName= altname;
	  
      //var eName = ( theElement.altname ) ? theElement.altname : theElement.name ;
      if (altname)
      {
      alert( altname + ': ' + eMsg ) ;                        // display the message (use altname if given)    
	  }
	  else
	  {
	  alert(eMsg);
	  }
 
	  //alert ('req='+ req + '  altname='+ altname + '  minchars='+minchars);

      theElement.focus() ;                                  // go to the offending element
      return  false ;                                       // the form is NOT complete
    }
  }
 
  return  true ;                                        // the form is complete
}


/*------------------------------------------------------------------------------
    text element validation

    given a text element (type is 'text' or 'textarea')

    returns a message string if
      the text is empty and the 'req' parameter is true
      the text length is not in the range of any specified 'minchars' and/or 'maxchars' parameters
      the word count is not in the range of any specified 'minwords' and/or 'maxwords' parameters
    returns null otherwise
*/

function isWholeNumber(thisNumber)
{
	var input=thisNumber.toString();
	for (var i=0;i<input.length;i++)
	{
	var thisChar=input.charAt(i);
	if(thisChar<"0" || thisChar>"9") 
	return true;
	}
 	return false;
 }


function validTextBox( theElement )
{
  var textVal = theElement.value ;
  var nChars = textVal.length ;
  var classN = theElement.className;
  var result = classN.split("|");
  var req = result[0];
  var altname = result[1];
  var inputtype= result[2];
  
  if (inputtype=='text'){
  var minchars = parseInt(result[3]);
  var maxchars = parseInt(result[4]);
   }
  
  if (inputtype=='textarea'){
  var minwords = result[3];
  var maxwords = result[4];
  }
  if (inputtype=='num')
  {
   if (isNaN(textVal))
   {
   return 'must be a number.';
   }
   if (textVal < 0)
   {
   return 'must be a POSITIVE number.';
   }
   if (isWholeNumber(textVal))
   {
   return 'must not be a decimal number.';
   }   
  }
  
  if (inputtype=='date')
  {
    if (textVal !='')
    {
    var dateTest = new Date(textVal);
    var dateFloor = new Date('5/1/2002'); //don't allow a date before the start of PROJECT IDEAL in 2002
    var dateCeiling = new Date('6/30/2005'); //don't allow a date after the end of this fiscal year
    
    if (isNaN(dateTest)||textVal.length<8)
    {
    
    return 'please enter a valid date in the format mm/dd/yyyy.';
    }
    else if (dateTest <= dateFloor)
    {
    return 'please enter a date after 5/1/2002.';
    }
    else if (dateTest >= dateCeiling)
    {
    return 'please enter a date before 6/30/2005';
    }
    }
  }
  
  if (inputtype=='phone')
  {
    
    eMsg= formatPhone(theElement.name);
  	
  }
  

  if ( req )                               // req specified
  {
    if ( req && nChars < 1 )
      return  'Must be provided.' ;
  }

  if ( minchars || maxchars )   // minchars or maxchars specified
  {
  	

    var rangeMsg = inRange( nChars, minchars, maxchars )
    if ( rangeMsg )
    {
      return  'must have ' + rangeMsg + ' characters.' ;
    }
  }

  if ( minwords || maxwords )   // minwords or maxwords specified
  {
    var nWords = textVal.split( /\s+/ ).length ;        // split on whitespace
    var rangeMsg = inRange( nWords, minwords, maxwords )
    if ( rangeMsg )
    {
      return  'must have ' + rangeMsg + ' words.' ;
    }
  }
  
  if(inputtype == 'time')
  {
  
  eMsg = validTime( theElement, theElement.value ) ;

  return eMsg;
  }
  
  

  return null ;                            // no msg: element is ok
}


function validTime( theElement, timeStr) 
{
  var classN = theElement.className;
  var result = classN.split("|");
  var req = result[0];
  var altname = result[1];
  var inputtype= result[2];
  var unused = result[3];
  var unused2 = result[4];

// Checks if time is in HH:MM:SS AM/PM format.
// The seconds and AM/PM are optional.
	
	var timeStr = theElement.value;
	var timePat = /^(\d{1,2}):(\d{2})(:(\d{2}))?(\s?(AM|am|PM|pm))?$/;

	var matchArray = timeStr.match(timePat);
    var ampm = matchArray[6];
    if (ampm=="") { ampm = null }
    
	if( req )
	{
	
		if (matchArray == null || ampm == null) 
		return 'Time is not in a valid format. Be sure to include AM/PM';

	}
	return null;
}


/*------------------------------------------------------------------------------
    select element validation

    given a select element (type is 'select-one' or 'select-multiple')

    returns a message string if
      the 'req' parameter is true and the first option is selected
    returns null otherwise

    note: This could be more robust. It assumes the first option is a "default" option that, if
    selected, means the user didn't select a "real" option.
*/

function validSelectBox( theElement )
{
  var classN = theElement.className;
  var result = classN.split("|");
  var req = result[0];
  var altname = result[1];
  var inputtype= result[2];
  var unused = result[3];
  var unused2 = result[4];

  if ( req )                     // req specified
  {
    if ( req && theElement.selectedIndex < 1 )
      return  'must be selected.' ;
  }
  return  null ;                            // no msg: element is ok
}


/*------------------------------------------------------------------------------
    radio button group validation

    given a radio button element (type is 'radio') and the group it's a member of

    returns a message string if
      none of the radio buttons is checked and the 'req' parameter is true
    returns null otherwise
*/

function validRadioButtons( theElement, theGroup )
{
  var classN = theElement.className;
  var result = classN.split("|");
  var req = result[0];
  var altname = result[1];
  var inputtype= result[2];
  var unused = result[3];
  var unused2 = result[4];

  if ( req )                     // req specified
  {
    if ( req && numChecked( theGroup ) < 1 )
      return  'must be selected.' ;
  }
  return  null ;                            // no msg: element is ok
}


/*------------------------------------------------------------------------------
    checkbox group validation

    given a checkbox element (type is 'checkbox') and the group it's a member of

    returns a message string if
      the number of checked checkboxes is not in the range of the specified 'minchecked' and/or 'maxchecked' parameters
    returns null otherwise
*/

function validCheckBoxes( theElement, theGroup )
{
  var classN = theElement.className;
  var result = classN.split("|");
  var req = result[0];
  var altname = result[1];
  var inputtype= result[2];
  var minchecked = result[3];
  var maxchecked = result[4];

  if ( req || maxchecked )   // minchecked or maxchecked specified
  {
    var rangeMsg = inRange( numChecked( theGroup ), minchecked, maxchecked )
    if ( rangeMsg )
    {
      return  'must have ' + rangeMsg + ' checked.' ;
    }
  }
  return  null ;                            // no msg: element is ok
}


/*------------------------------------------------------------------------------
    generate a phrase for a numeric range checking error message

    given a numeric value and min and/or max limits

    returns a message string if
      the numeric value is not in the range of the specified min and max limits
    returns null otherwise
*/

function inRange( theValue, minValue, maxValue )
{
  var classN = theElement.className;
  var result = classN.split("|");
  var req = result[0];
  var altname = result[1];
  var inputtype= result[2];
  var minValue = result[3];
  var maxValue = result[4];


  if ( minValue )                // min specified
  
  {
    if ( maxValue )                // both min and max specified
    {
      if ( theValue < minValue || theValue > maxValue )
      {
        if ( minValue == maxValue )
          return  'exactly ' + minValue ;
        else
          return  'between ' + minValue + ' and ' + maxValue ;
      }
    }
    else                           // only min specified
    {
      if ( theValue < minValue )
        return  'at least ' + minValue ;
    }
  }
  else
  if ( maxValue )                // only max specified
  {
    if ( theValue > maxValue )
      return  'at most ' + maxValue ;
  }
  return  null ;
}


/*------------------------------------------------------------------------------
    count checked checkboxes (or radio buttons)

    given a group of checkboxes with the same name

    returns the number that are checked

    note: also works for a group of radio buttons (returns 0 or 1)
*/

function numChecked( theGroup )
{
  var n = 0 ;
  for ( var j = 0 ; j < theGroup.length ; j++ )
  {
    if ( theGroup[j].checked )  n++ ;
  }
  return  n ;
}


/*------------------------------------------------------------------------------
    select element branching function

    usage: <select name="isStudent" ... onchange="branchTo(this, 1, Advisor, Color);">

    given a select element to test, a yes-option index, a yes-element to move
    to if the yes-option is selected, and a no-element to move to if not

    if yes-option is selected, set the yes-element req=true and move there
    otherwise, set the yes-element req=false and move to the no-element

    note: this is not part of the form validation process: it is invoked by an
    onChange event for an element, not the onSubmit event for the form.
*/

function branchTo( theElement, yesIndex, yesElement, noElement )
{
  if ( theElement.selectedIndex == yesIndex )
  {
    yesElement.req = 'true' ;
    yesElement.focus();
  }
  else
  {
    yesElement.req = 'false' ;
    noElement.focus();
  }
}

function amIaDate(theField)
	{
	
	if (theField != "")
	{
		var theWorkingDate = new Date(theField);
		alert(theWorkingDate);
		var theFlag = true;
		
		if (isNaN(theWorkingDate)||length(theWorkingDate)<5)
		{
		
		return true;
		}
		else
		{
		var theMonth = theWorkingDate.getMonth() + 1;
		var theDay = theWorkingDate.getDate();
		var theYear = theWorkingDate.getFullYear();
			if (theYear < 2000)
			{
			theYear = theYear + 100;
			
			}
		var fancyDate = theMonth + '/' + theDay + '/' + theYear;
		theField = fancyDate
		return true;
		
		}
		
	}
	}
 

function formatPhone(fieldName){
var nums= fieldName;

var re= /\D/;
// test for this format: (xxx)xxx-xxxx
var re2 = /^\({1}\d{3}\)\d{3}-\d{4}/; 
// test for this format: xxx-xxx-xxxx
//var re2 = /^\d{3}-\d{3}-\d{4}/;


var num=eval(nums+'.value');

var newNum;
 if (num != "" && re2.test(num)!=true){
   if (num != ""){
     while (re.test(num)){
     num = num.replace(re,"");
     
     }
   }

  if (num.length != 10){
    alert ('Please enter a 10 digit phone number');
    
    eval(nums).select();
    
    }
   else {
     // for format (xxx)xxx-xxxx
     newNum = '(' + num.substring(0,3) + ')' + num.substring(3,6) + '-' + num.substring(6,10);
     // for format xxx-xxx-xxxx
     // newNum = num.substring(0,3) + '-' + num.substring(3,6) + '-' + num.substring(6,10);
     eval(nums).value=newNum;
    
     }
   }
  
}








