/****************************************************************/

// DEFINE VARIABLES

// www.aspfaqs.com/webtech/091998-1.shtml



/* PURPOSE:  Returns true if the string is a valid date number.

	A method is passed in (1 = month, 2 = day).  If the string is

	nonnumeric, false is passed back.  If the day in the date string

	is greater than 31, false is returned.  If the month is greater

	than 12, an error is returned.

*/



/****************************************************************/

function isDateNumber(strNum,method)

{

	var str = new String(strNum);

	var i = 0;



	if (isNaN(parseInt(str)) || parseInt(str) < 0) return false;



	if (method == 2)

		if (parseInt(str) > 31)

			return false;

	if (method == 1)

		if (parseInt(str) > 12)

			return false;



	for (i = 0; i < str.length; i++)

		if (str.charAt(i) < '0' || str.charAt(i) > '9')

			return false;





	return true;

}



/****************************************************************/



// Check whether string s is empty.



function isEmpty(s)

{   return ((s == null) || (s.length == 0))

}



/****************************************************************/



// Check whether val has exactly n characters.



function isCount(val,str,n) {

	var strInput = new String(val.value);

   if (val.value.length != n) {

		alert("You must enter exactly " + n + " characters in field: '" + str + "'" );

		val.focus();

		val.select();

		return false;

	} else

		return true;

}



/****************************************************************/

// whitespace characters

var whitespace = " \t\n\r";





// Returns true if string s is empty or 

// whitespace characters only.



function isWhitespace (s)



{   var i;



    // Is s empty?

    if (isEmpty(s)) return true;



    // Search through string's characters one by one

    // until we find a non-whitespace character.

    // When we do, return false; if we don't, return true.



    for (i = 0; i < s.length; i++)

    {   

	// Check that current character isn't whitespace.

	var c = s.charAt(i);



	if (whitespace.indexOf(c) == -1) return false;

    }



    // All characters are whitespace.

    return true;

}



/****************************************************************/


function ForceEntry(val, str) {
	var strInput = new String(val.value);
	if (isWhitespace(strInput)) {
		alert("You must have an entry in field: '" + str + "'");
		val.focus();
		val.select();
		return false;
	} else
		return true;
}


/****************************************************************/



function ForceNumber(val, str) {

	if (isNaN(val.value)) {

		alert("You must enter a number in field: '" + str + "'");

		val.focus();

		val.select();

		return false;

	} else

		return true;

}



/****************************************************************/



// Displays an alert box with the passed in string...



function PromptErrorMsg(Field,strError)

{

	alert("You have entered an invalid date for " + strError + ".  Please make sure your date format is MM/DD/YYYY.");

	Field.focus();

	Field.select();

}



/****************************************************************/



/* PURPOSE: Checks to see if the string is a valid date.  A valid

	date is defined as any of the following:



		MM/DD/YY, MM/DD/YYYY, M/D/YY, M/D/YYYY,

		MM-DD-YY, MM-DD-YYYY, M-D-YY, M-D-YYYY

*/



function ForceDate(strDate,strField)

{

	var str = new String(strDate.value);

//	if (!ForceEntry(strDate.value, "Please enter a date")) {

//		return false;

//	}

	if (isWhitespace(str)) {

		return true;

		// if the field is empty, just return true...

	}



	var i = 0, count = str.length, j = 0;

	while ((str.charAt(i) != "/" && str.charAt(i) != "-") && i < count)

		i++;



	if (i == count || i > 2) {

		PromptErrorMsg(strDate,strField);

		return false;

	}



	var addOne = false;

	if (i == 2) addOne = true;



	if (!isDateNumber(str.substring(0,i),1)) {

		PromptErrorMsg(strDate,strField);

		return false;

	}



	j = i+1;

	i = 0;



	while ((str.charAt(i+j) != "/" && str.charAt(j+i) != "-") && i+j < count)

		i++;



	if (i+j == count || i > 2) {

		PromptErrorMsg(strDate,strField);

		return false;

	}



	if (!isDateNumber(str.substring(j,i+j),2)) {

		PromptErrorMsg(strDate,strField);

		return false;

	}



	j = i+3;

	i = 0;



	if (addOne) j++;



	while (i+j < count)

		i++;





	if (i != 2 && i != 4) {

		PromptErrorMsg(strDate,strField);

		return false;

	}



	if (!isDateNumber(str.substring(j,i+j),3)) {

		PromptErrorMsg(strDate,strField);

		return false;

	}



	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)

{

//    if (isEmpty(s)) 

//       if (isEmail.arguments.length == 1) return defaultEmptyOK;

//       else return (isEmail.arguments[1] == true);

  

    // is s whitespace?

    if (isWhitespace(s)) return false;

    

    // 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;

}



// testIsEmail (s, [, Boolean emptyOK])



function testIsEmail (s)

{

	if (testIsEmail.arguments[1]) {

		if ( isEmpty(s.value) || isWhitespace(s.value) )

			return true;

	}

	if (isEmail (s.value))

		return true;

	else {

		alert("email address must be in the form: a@b.c");

		s.focus();

		s.select();

		return false;

	}

}

