
/*
* 	Check the date as format: "mm/dd/yyyy" or "mm-dd-yyyy".
*	Checks that the date entered is a valid date.
*	aDate - the date string to check
*	return - true if date format is valid, false otherwise
*/
function checkDate(aDate)
{
	var checkstr = "0123456789";
	var DateTemp = "";
	var seperator = "/";
	var day;
	var month;
	var year;
	var leap = 0;
	var err = 0;
	var i;
	err = 0;
	
	// check to see if a "-" or a "/" is used as the seperator
	if (aDate.indexOf("/") > 0) {
		seperator = "/";
	}
	else {
		seperator = "-";
	}
	 
	/* year is wrong if year = 0000 */
	indx = aDate.lastIndexOf(seperator);
	year = aDate.substr(indx+1);
	
	if (!isInteger(year))
		err = 20;
	year = parseInt(year);
	if ((year < 1000) || (year > 3000))
		return false;
	  
	
	/* Validation of month*/
	indx = aDate.indexOf(seperator);
	month = aDate.substr(0,indx);
	
	if(month.length > 2)
		return false;
	 
	if((!isInteger(month) || month < 1) || (month > 12)) {
		err = 21;
	}
	/* Validation of day*/
	indx1 = aDate.indexOf(seperator,indx+1);
	day = aDate.substring(indx+1,indx1);
	
	if(day.length > 2)
		return false;
	 
	if(!isInteger(day) || day < 1 || day > 31) {
		err = 22;
	}
	/* Validation leap-year / february / day */
	if((year % 4 == 0) || (year % 100 == 0) || (year % 400 == 0)) {
		leap = 1;
	}
	if((month == 02) && (leap == 1) && (day > 29)) {
		err = 23;
	}
	if ((month == 02) && (leap != 1) && (day > 28)) {
		err = 24;
	}
	/* Validation of other months */
	if ((day > 31) && ((month == "01") || (month == "03") || (month == "05") || (month == "07") || (month == "08") || (month == "10") || (month == "12"))) {
		err = 25;
	}
	if ((day > 30) && ((month == "04") || (month == "06") || (month == "09") || (month == "11"))) {
		err = 26;
	}
	/*if 00 ist entered, no error, deleting the entry*/ 
	if((day == 0) && (month == 0) && (year == 00)) {
		err = 0; day = ""; month = ""; year = ""; seperator = "";
	}
	/* if((year.length!=4) || (month.length != 2) || (day.length!= 2))
	{
		err = 27;
	} */
	/* if no error, write the completed date to Input-Field (e.g. 13.12.2001) */
	if(err == 0) {
		return true;
	}
	
	/* Error-message if err != 0 */
	else {
		return false;
	}
}


// 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.
//
// EXAMPLE FUNCTION CALL:     RESULT:
// isInteger ("5")            true 
// isInteger ("")             defaultEmptyOK
// isInteger ("-5")           false
// isInteger ("", true)       true
// isInteger ("", false)      false
// isInteger ("5", false)     true

function isInteger (s)

{   var i;

    if (isEmpty(s)) 
       if (isInteger.arguments.length == 1) return false;
       else return (isInteger.arguments[1] == true);

    // Search through string's characters one by one
    // until we find a non-numeric character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);

        if (!isDigit(c)) return false;
    }

    // All characters are numbers.
    return true;
}



// Returns true if character c is a digit 
// (0 .. 9).

function isDigit (c)
{   return ((c >= "0") && (c <= "9"))
}

// Check whether string s is empty.

function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}