// SUMMARY
//
// This is a set of JavaScript functions for validating input on 
// an HTML form.  Functions are provided to validate:
//      - U.S. ZIP codes (5 or 9 digit postal codes)
//	- email addresses
// isZIPCode (s [,eok])                True if string s is a valid U.S. ZIP code.
// isEmail (s [,eok])                  True if string s is a valid email address.
// stripCharsInBag (s, bag)            Removes all characters in string bag from string s.
// non-digit characters which are allowed in ZIP Codes

	var ZIPCodeDelimiters = "-";

// U.S. ZIP codes have 5 or 9 digits.
// They are formatted as 12345 or 12345-6789.
	
	var digitsInZIPCode1 = 5
	var digitsInZIPCode2 = 9
	
// s is an abbreviation for "string"
	
	var sZIPCode = "ZIP Code"

	var sEmail = "Email"
	
// isWhitespace (s)             Check whether string s is empty or whitespace.
// isInteger (s [,eok])                True if all characters in string s are numbers.

// isInteger (STRING s [, BOOLEAN emptyOK])
// 
// Returns true if all characters in string s are numbers.
//
// Accepts non-signed integers only. Does not accept floating 
// point, exponential notation, etc.
//
// We don't use parseInt because that would accept a string
// with trailing non-numeric characters.
//
// By default, returns defaultEmptyOK if s is empty.
// There is an optional second argument called emptyOK.
// emptyOK is used to override for a single function call
//      the default behavior which is specified globally by
//      defaultEmptyOK.
// If emptyOK is false (or any value other than true), 
//      the function will return false if s is empty.
// If emptyOK is true, the function will return true if s is empty.

var reInteger = /^\d+$/

function isInteger (s)

{   var i;

    if (isEmpty(s)) 
       if (isInteger.arguments.length == 1) return defaultEmptyOK;
       else return (isInteger.arguments[1] == true);

    return reInteger.test(s)
}




// Global variable defaultEmptyOK defines default return value 
// for many functions when they are passed the empty string. 
// By default, they will return defaultEmptyOK.
//
// defaultEmptyOK is false, which means that by default, 
// these functions will do "strict" validation.  Function
// isInteger, for example, will only return true if it is
// passed a string containing an integer; if it is passed
// the empty string, it will return false.
//
// You can change this default behavior globally (for all 
// functions which use defaultEmptyOK) by changing the value
// of defaultEmptyOK.
//
// Most of these functions have an optional argument emptyOK
// which allows you to override the default behavior for 
// the duration of a function call.
//
// This functionality is useful because it is possible to
// say "if the user puts anything in this field, it must
// be an integer (or a phone number, or a string, etc.), 
// but it's OK to leave the field empty too."
// This is the case for fields which are optional but which
// must have a certain kind of content if filled in.

	var defaultEmptyOK = false


// Removes all characters which appear in string bag from string s.

function stripCharsInBag (s, bag)

{   var i;
    var returnString = "";

    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }

    return returnString;
}



// isZIPCode (STRING s [, BOOLEAN emptyOK])
// 
// isZIPCode returns true if string s is a valid 
// U.S. ZIP code.  Must be 5 or 9 digits only.
//
// NOTE: Strip out any delimiters (spaces, hyphens, etc.)
// from string s before calling this function.  
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

function isZIPCode (s)
{ // if (isEmpty(s)) 
  //     if (isZIPCode.arguments.length == 1) return defaultEmptyOK;
  //     else return (isZIPCode.arguments[1] == true);
  // return (isInteger(s) && 
  //          ((s.length == digitsInZIPCode1) ||
  //           (s.length == digitsInZIPCode2)))
	return true;
}



// isEmail (STRING s [, BOOLEAN emptyOK])
// 
// Email address must be of form a@b.c -- in other words:
// * there must be at least one character before the @
// * there must be at least one character before and after the .
// * the characters @ and . are both required
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

function isEmail (s)
{   
    // there must be >= 1 character before @, so we
    // start looking at character position 1 
    // (i.e. second character)
    var i = 1;
    var sLength = s.length;

    // look for @
    while ((i < sLength) && (s.charAt(i) != "@"))
    { i++
    }

    if ((i >= sLength) || (s.charAt(i) != "@")) return false;
    else i += 2;

    // look for .
    while ((i < sLength) && (s.charAt(i) != "."))
    { i++
    }

    // there must be at least one character after the .
    if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
    else return true;
}






function checkdate(a)
{

//	window.onerror=null for all other strange errors
	var err=0
	if (a.length != 10) err=1
    {
	  b = a.substring(0, 2)// month
	  c = a.substring(2, 3)// '/'
	  d = a.substring(3, 5)// day
	  e = a.substring(5, 6)// '/'
	  f = a.substring(6, 10)// year

	 //basic error checking
	 if (b<1 || b>12) err = 1
	 if (c != '/') err = 1
	 if (d<1 || d>31) err = 1
	 if (e != '/') err = 1
	 if (f<=0 || f>9999) err = 1

	 //advanced error checking

	 // months with 30 days
	 if (b==4 || b==6 || b==9 || b==11)
	 {
		if (d==31) err=1
	 }

	 // february, leap year
	 if (b==2)
	 {
		// feb
		var g=parseInt(f/4)
		if (isNaN(g))
		{
		 err=1
		}

		if (d>29) err=1
		if (d==29 && ((f/4)!=parseInt(f/4))) err=1
	 }

	 if (err==1)
	 {
		alert("Please check date format for field : " );
	 }

     }

}
