/*

FUNCTIONS IN THIS INCLUDE FILE
------------------------------
	check_date()
	check_phone()
	check_zipcode()
	check_socialsecurity()
	check_email()
	validate_text()	
	trim()
*/

function check_date(dateobj, objval)
{
	// This function checks to see if the value of a date is valid.
	// It will be called by passing to it a single string value.
	if(objval == "val"){
		str = dateobj;
	}else{
		str = dateobj.value;
	}
	var err=0;
	if (str.length < 8)  // str.length is 7 or less so error
	{
		err = 1;
		month = ""
		slashone = ""
		day = ""
		slashtwo = ""
		century = ""
		year = ""
	}
	else
	{
		if (str.length > 9)  // if str.length is > 9, then it must be no more than 10
		{
			if (str.length != 10) err = 1;
			month = str.substring(0, 2)
			slashone = str.substring(2, 3)
			day = str.substring(3, 5)
			slashtwo = str.substring(5, 6)
			century = str.substring(6, 8)
			year = str.substring(8, 10)
		}
		else
		{
			if (str.length == 9)
			{
				if (str.length != 9) err = 1;
				if (str.substring(1,2) == '/')
				{
					month = str.substring(0, 1)
					slashone = str.substring(1, 2)
					day = str.substring(2, 4)
					slashtwo = str.substring(4, 5)
					century = str.substring(5, 7)
					year = str.substring(7, 9)
				}
				else
				{
					month = str.substring(0, 2)
					slashone = str.substring(2, 3)
					day = str.substring(3, 4)
					slashtwo = str.substring(4, 5)
					century = str.substring(5, 7)
					year = str.substring(7, 9)
				}
			}
			else
			{
				if (str.length == 8)
				{
					if (str.length != 8) err = 1;
					month = str.substring(0, 1)
					slashone = str.substring(1, 2)
					day = str.substring(2, 3)
					slashtwo = str.substring(3, 4)
					century = str.substring(4, 6)
					year = str.substring(6, 8)
				}
			}
		}
	}




	// Very basic error checking (code taken from Event.asp

	if ( (isNaN(month)) || (isNaN(day)) || (isNaN(century)) || (isNaN(year)) ) err = 1;

	if (month<01 || month>12) err = 1;   // month number
	if (slashone != '/') err = 1;
	if (day<01 || day>31) err = 1;  // day number
	if (slashtwo != '/') err = 1;
	if (century<19) err = 1;        // century number
	if (year<0 || year>99) err = 1;  // year number

	//months with 30 days
	//This is a check on the number of days in a month
	if (month==4 || month==6 || month ==9 || month==11)
	{
		if (day==31) err = 1;
	}
	if (month==2)  //The month is February
	{
		if (day>29) err = 1
		if ((day == 29) && ((((century/4) != parseInt(century/4)) && ((year/4) == 00)) || (((century/4) == parseInt(century/4)) && ((year/4) != parseInt(year/4))) || (((century/4) != parseInt(century/4)) && ((year/4) != parseInt(year/4))))) err=1;
	}
	if (err==1)
	{
		return false;
	}
	else
	{
		return true;
	}
}  // End function check_date()

function check_phone(phoneobj)
{
	/*This function will check the phone number and make sure that the value
	has numbers in it.*/
	
	phonestr = phoneobj.value;
	var phoneerr = 0;	
	if(phonestr.length < 12 || phonestr.length > 13)
	{
		phoneerr = 1;
		areacode = ""
		dashone = ""
		phprefix = ""
		dashtwo = ""
		phpostfix = ""
		aprentone = ""
		aprenttwo = ""
	}
	else
	{
		if(phonestr.length == 12)
		{
			areacode = phonestr.substring(0, 3)
			dashone = phonestr.substring(3, 4)
			phprefix = phonestr.substring(4, 7)
			dashtwo = phonestr.substring(7, 8)
			phpostfix = phonestr.substring(8, 12)
			aprentone = ""
			aprenttwo = ""
		}
		if(phonestr.length == 13)
		{
			aprentone = phonestr.substring(0, 1)
			areacode = phonestr.substring(1, 4)
			aprenttwo = phonestr.substring(4, 5)
			phprefix = phonestr.substring(5, 8)
			dashtwo = phonestr.substring(8, 9)
			phpostfix = phonestr.substring(9, 13)
			dashone = ""
		}
		
	}
	if ( (isNaN(areacode)) || (isNaN(phprefix)) || (isNaN(phpostfix)) ) phoneerr = 1;
	
	if (areacode < 01) phoneerr = 1;
	if (phprefix < 01) phoneerr = 1;
	if (phpostfix < 01) phoneerr = 1;
	
	if(dashone == "" && aprentone == "" && dashtwo == "" && aprenttwo == "")
	{
		phoneerr = 1;
	}
	else
	{
		if(dashone != "" && dashone != '-'){
			phoneerr = 1;
		}
		if(dashtwo != "" && dashtwo != '-'){
			phoneerr = 1;
		}
		if(aprentone != "" && aprentone != '('){
			phoneerr = 1;
		}
		if(aprenttwo != "" && aprenttwo != ')'){
			phoneerr = 1;
		}
	}
		
	if (phoneerr == 1)
	{
		return false;
	}
	else
	{
		return true;
	}
}  // End function check_phone()

//This function checks to make sure that a five digit zipcode was entered.
//It is designed to allow for easy implementation of the zip+5 format if needed in the future.
function check_zipcode(zipobj)
{
	zipstr = zipobj.value;
	var ziperr = 0;
	
	if (zipstr.length < 5)
	{
		ziperr = 1;
		fzipcode = ""
	}
	else
	{		
		if (zipstr.length == 5)
		{
			fzipcode = zipstr.substring(0, 5)
			if ( (isNaN(fzipcode)) ) ziperr = 1;
		}
		else
		{
			if (zipstr.length > 5)
			{
				//This will look for the dash in the zip code for the extension.
				if (zipstr.substring(5,6) != '-'){
					ziperr = 1;
					fzipcode = ""
				}
				if(isNaN(zipstr.substring(0,5)) || isNaN(zipstr.substring(6,zipstr.length))){
					ziperr = 1;
					fzipcode = ""
				}

				if(ziperr == 0){
					fzipcode = zipstr
				}
			}
		}
	}
	
	if (fzipcode < 0) ziperr = 1;
	
	if (ziperr == 1)
	{
		return false;
	}
	else
	{
		return true;
	}
}  // End function check_zipcode

//This function checks to make sure that the social security number is in the form of NNN-NN-NNNN.
function check_socialsecurity(socialsecurityobj)
{
	socialsecuritystr = socialsecurityobj.value;
	var socialsecurityerr = 0;
		
	if (socialsecuritystr.length < 11 || socialsecuritystr.length > 11)
	{
		socialsecurityerr = 1;
		a = ""
		b = ""
		c = ""
		d = ""
		e = ""
	}
	else
	{
		if (socialsecuritystr.length == 11)
		{
			a = socialsecuritystr.substring(0, 3)
			b = socialsecuritystr.substring(3, 4)
			c = socialsecuritystr.substring(4, 6)
			d = socialsecuritystr.substring(6, 7)
			e = socialsecuritystr.substring(7, 11)
		}
	}
		
	if ( (isNaN(a)) || (isNaN(c)) || (isNaN(e)) ) socialsecurityerr = 1;
		
	if (a < 01) socialsecurityerr = 1;
	if (b != '-') socialsecurityerr = 1;
	if (c < 01) socialsecurityerr = 1;
	if (d != '-') socialsecurityerr = 1;
	if (e < 01) socialsecurityerr = 1;
		
	if (socialsecurityerr == 1)
	{
		return false;
	}
	else
	{
		return true;
	}
}  // End function check_socialsecurity()

function check_email(emailStr) {
  /* 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=emailStr.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 (check @ and .'s)")
	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.")
    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!")
		return false
	    }
    }
    return true
  }

  // Domain is symbolic name
  var domainArray=domain.match(domainPat)
  if (domainArray==null) {
	//alert("The domain name doesn't seem to be valid.")
    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)
  var len=domArr.length
  if (domArr[domArr.length-1].length<2 ||
    domArr[domArr.length-1].length>3) {
   // the address must end in a two letter or three letter word.
   //alert("Email address must end in a three-letter domain, or two letter country.")
   return false
  }

  // Make sure there's a host name preceding the domain.
  if (len<2) {
   var errStr="This address is missing a hostname!"
   //alert(errStr)
   return false
  }

  // If we've gotten this far, everything's valid!
  return true;
}// end of check_email().

function validate_text(strtextfield){
	/*This function will check the text for characters that would be problematic.*/
	var err = 0;
	var bad_code_cnt = 0;
	var bad_code = new Array('"');
	/*When you add to the array you need to change the bad_code_cnt to the total number of 
	items in the array.*/

	//Loop through the array and check to see if the text matches the array
	for(var i=0;i<=bad_code_cnt;i++){
		if(strtextfield.indexOf(bad_code[i]) >= 0){
			err = 1;
		}else{
			err = 0;
		}
	}
	if(err == 1){
		return false;
	}else{
		return true;
	}
}

<!-- Hide from old browsers
function trim(inputString) 
{
   // Removes leading and trailing spaces from the passed string. Also removes
   // consecutive spaces and replaces it with one space. If something besides
   // a string is passed in (null, custom object, etc.) then return the input.
   if (typeof inputString != "string") { return inputString; }
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") { // Check for spaces at the beginning of the string
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") { // Check for spaces at the end of the string
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
   }
   return retValue; // Return the trimmed string back to the user
} // Ends the "trim" function
-->