var DATE_DEFAULT_SEP	 = ".";
var DATE_SPLIT			 = /[\-\/\.\,\*\+\ ]/;
var VALID_DATE_CHARS     = VALID_NUMBERS + "-/.,*+ ";

var VALID_DATE_PATTERNS  = new Array();

initDateInputChecks();

// ------------------------------------------------------------------
// Return true if string s is a valid year
function isYear (s, required) {
    if (! required && isWhiteSpace(s)) return true;

    if (! isInteger(s, true)) return false;

    if (s > 99 && s < 1000) return false; // 3-stellig
    if (s < 1 || s > 9999) return false;

    return true;
}

// ------------------------------------------------------------------
// erzeugt 4-stelliges Jahr aufgrund der Eingabe
function getYear (s) {
    if (s >= 1 && s <= 99) return 2000 + Number (s);
    return s;
}

// ------------------------------------------------------------------
// Return true if string s is a valid Month
function isMonth (s, required) {
    if (! required && isWhiteSpace(s)) return true;

    if (! isInteger(s, true)) return false;

    if (s < 1 || s > 12) return false;

    return true;
}

// ------------------------------------------------------------------
// Return true if string s is a valid Month
function isDay (s, required) {
    if (! required && isWhiteSpace(s)) return true;

    if (! isInteger(s, true)) return false;

    if (s < 1 || s > 31) return false;

    return true;
}

// ------------------------------------------------------------------
function isDate(year,month,day) {

    var daysInMonth = new Array();
    daysInMonth[1] = 31;
    daysInMonth[2] = 29;   // must programmatically check this
    daysInMonth[3] = 31;
    daysInMonth[4] = 30;
    daysInMonth[5] = 31;
    daysInMonth[6] = 30;
    daysInMonth[7] = 31;
    daysInMonth[8] = 31;
    daysInMonth[9] = 30;
    daysInMonth[10] = 31;
    daysInMonth[11] = 30;
    daysInMonth[12] = 31;

    if (! isYear(year,true)) {
        return false;
    }

    if (! isMonth(month,true)) {
        return false;
    }

    if (! isDay(day,true)) {
        return false;
    }

    var intYear = parseInt(year, 10);
    var intMonth = parseInt(month, 10);
    var intDay = parseInt(day, 10);
    // catch invalid days, except for February

    if (intDay > daysInMonth[intMonth]) return false;

    if ((intMonth == 2) && (intDay > daysInFebruary(intYear))) return false;

    function daysInFebruary (year)
    {  // February has 29 days in any year evenly divisible by four,
       // EXCEPT for centurial years which are not also divisible by 400.
        return (  ((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0) ) ) ? 29 : 28 );
    }

    return true;

}

function initDateInputChecks() {
	VALID_DATE_PATTERNS[0] = /^\d\d?[\-\/\.\,\*\+\ ]+\d\d?[\-\/\.\,\*\+\ ]+\d\d?$/;
	VALID_DATE_PATTERNS[1] = /^\d\d?[\-\/\.\,\*\+\ ]+\d\d?[\-\/\.\,\*\+\ ]+\d\d\d\d$/;
	VALID_DATE_PATTERNS[2] = /^\d\d\d\d\d\d$/;
	VALID_DATE_PATTERNS[3] = /^\d\d\d\d\d\d\d\d$/;
}

// --------------------------------------------------
// Datum Eingabecheck
// --------------------------------------------------
function date_onKeyPress(keyEvent) {
	if (isNavigator() == false) keyEvent = getIEEvent();;
	var field = getEventField(keyEvent);
	if (isEnterKey(keyEvent)) {
		if (field.value.length == 0) return true;
		if (date_isValid(field) == false) {
			field.blur();
			return false;
		}
		date_format(field);
		return true;
	}
	if (isValidKey(keyEvent, VALID_DATE_CHARS) == false) return false;
	return true;
}

function date_onBlur(field) {
	if (field.value.length == 0) return;

	if (date_isValid(field) == false) {
                nbalert(msgDateError);
		field.value = "";
		field.focus();
		return;
	}
	date_format(field);
}

function date_format(field) {
	var parts = date_getParts(field);
	field.value = fillWithNull(parts[0],2) + DATE_DEFAULT_SEP +
	              fillWithNull(parts[1],2) + DATE_DEFAULT_SEP + parts[2];
}

function date_isValid(field) {
	if (field.value.length == 0) return true;
	if (hasValidPattern(field, VALID_DATE_PATTERNS)==false) return false;
	var parts = date_getParts(field);
	return isDate(parts[2], parts[1], parts[0]);
}

function date_getParts(field) {
	var value = field.value;
	var parts;
	if (DATE_SPLIT.test(value))
		parts = value.split(DATE_SPLIT);
	else {
		parts = new Array();
		parts[0] = value.substring(0,2);
		parts[1] = value.substring(2,4);
		parts[2] = value.substring(4);
	}
	if (parts[2].length < 4) {
		if(parts[2] > 20) {
			parts[2] = "19" + parts[2];
		} else {
			parts[2] = "20" + parts[2];
		}
	}
	return parts;
}
