function Right(str,i) {
        str = str + "";
        return str.substring(str.length-i,str.length);
}

function getDateFromForm(frm, baseName) {
	var strYear, strMonth, strDay;
	
	strYear = eval('document.' + frm.name + '.' + baseName + 'Year.value');
	strMonth = eval('document.' + frm.name + '.' + baseName + 'Month.value');
	strDay = eval('document.' + frm.name + '.' + baseName + 'Day.value');
	
	if (blankString(strYear) && blankString(strMonth) && blankString(strDay)) return '';
	
	return (strYear + '-' + Right('00' + strMonth, 2) + '-' + Right('00' + strDay, 2));
}

function convertToDate(strDate) {
	return new Date(parseInt(strDate.substr(0, 4)), parseInt(strDate.substr(5, 2)) - 1, parseInt(strDate.substr(8, 2)));
}

function getTimeFromForm(frm, baseName) {
	var strHour, strMinute;
	
	strHour = eval('document.' + frm.name + '.' + baseName + 'Hour.value');
	strMinute = eval('document.' + frm.name + '.' + baseName + 'Min.value');
	
	if (blankString(strHour) && blankString(strMinute)) return '';
	
	return (Right('00' + strHour, 2) + ':' + Right('00' + strMinute, 2));
}

function validYear(strYear, bMayBeInPast, bMayBeInFuture) {
	if (strYear == '') { return true; }
	
	if (isNaN(strYear)) { return false; }
	if (strYear.length != 4) { return false; }
	var tmpDate = new Date();
	if (!bMayBeInPast) {
		if (parseFloat(strYear) < tmpDate.getUTCFullYear()) { return false; }
	}
	if (!bMayBeInFuture) {
		if (parseFloat(strYear) > tmpDate.getUTCFullYear()) { return false; }
	}
	
	return true;
}

function ageCheck(strDOB) {
	var now = new Date();
	var Day18 = parseFloat(now.getDate());
	var Month18 = parseFloat(now.getMonth()) + 1;
	var Year18 = parseFloat(now.getFullYear()) - 18;
	var DOBDay = parseFloat(strDOB.substr(8, 2));
	var DOBMonth = parseFloat(strDOB.substr(5, 2));
	var DOBYear = parseFloat(strDOB.substr(0, 4));
	
	if (DOBYear > Year18) return false;
	if (DOBYear < Year18) return true;
	if (DOBMonth > Month18) return false;
	if (DOBMonth < Month18) return true;
	if (DOBDay > Day18) return false;
	
	return true;
}

function validTime(strTime) {
	if (strTime == '') return true;
	
	if (strTime.length != 5) return false;
	if (strTime.substr(2, 1) != ':') return false;
	if (isNaN(strTime.substr(0, 2)) || isNaN(strTime.substr(3, 2))) return false;
	if (parseInt(strTime.substr(0, 2)) < 0 || parseInt(strTime.substr(0, 2)) > 23) return false;
	if (parseInt(strTime.substr(3, 2)) < 0 || parseInt(strTime.substr(3, 2)) > 59) return false;
	
	return true;
}

function validDate(strDate, strDateFormat, MustBeBefore, MustBeAfter) {
	var fCheckBeforeDate = false;
	var fCheckAfterDate = false;
	
	if (MustBeBefore && MustBeBefore != '') fCheckBeforeDate = true;
	if (MustBeAfter && MustBeAfter != '') fCheckAfterDate = true;
	
	if (strDate == '') { return true; }
	
	if (strDateFormat) {
		if (strDateFormat == '') { strDateFormat = 'YYYY-MM-DD'; }
	} else {
		strDateFormat = 'YYYY-MM-DD';
	}
	
	var intYear = -1;
	var intMonth = -1;
	var intDay = -1;
	var strSep1 = '';
	var strSep2 = '';
	var intSep1 = -1;
	var intSep2 = -1;
	
	for (var i=0; i<strDateFormat.length; i++) {
		if (strDateFormat.substr(i, 4) == 'YYYY') {
			intYear = i;
		} else if (strDateFormat.substr(i, 2) == 'MM') {
			intMonth = i;
		} else if (strDateFormat.substr(i, 2) == 'DD') {
			intDay = i;
		} else if (strDateFormat.substr(i, 1) != 'Y' && strDateFormat.substr(i, 1) != 'M' && strDateFormat.substr(i, 1) != 'D') {
			if (intSep1 == -1) {
				intSep1 = i;
				strSep1 = strDateFormat.substr(i, 1);
			} else {
				intSep2 = i;
				strSep2 = strDateFormat.substr(i, 1);
			}
		}
	}
	
	// Now, validate the date according to the given format
	if (strDate.length != 10) { return false; }
    if (strDate.substr(intSep1, 1) != strSep1) { return false; }
	if (strDate.substr(intSep2, 1) != strSep2) { return false; }
    	
	var strYear=strDate.substr(intYear, 4);
    var strMonth=strDate.substr(intMonth, 2);
	var strDay=strDate.substr(intDay, 2);
	if (strMonth.substr(0, 1) == '0') strMonth = strMonth.substr(1, 1);
	if (strDay.substr(0, 1) == '0') strDay = strDay.substr(1, 1);
  	
	if (!isNaN(parseInt(strYear))) strYear = String(parseInt(strYear));
	if (strYear.length != 4) return false;
	
	if (isNaN(parseInt(strYear)) || isNaN(parseInt(strMonth)) || isNaN(parseInt(strDay))) { return false; }
	if (parseInt(strMonth) < 1 || parseInt(strMonth) > 12) { return false; }
    
//			document.write(parseInt(strDay));
	var intCheckDay = 1;
	
	if (parseInt(strMonth) == 1 || parseInt(strMonth) == 3 || parseInt(strMonth) == 5 || parseInt(strMonth) == 7 || parseInt(strMonth) == 8 || parseInt(strMonth) == 10 || parseInt(strMonth) == 12) {
		intDayMax = 31;
	} else if (parseInt(strMonth) == 4 || parseInt(strMonth) == 6 || parseInt(strMonth) == 9 || parseInt(strMonth) == 11) {
		intDayMax = 30;
	} else if (parseInt(strMonth) == 2) {
		if (isLeapYear(strYear)) {
			intDayMax = 29;
		} else {
			intDayMax = 28;
		}
	}
	
	if (parseInt(strDay) < 1 || parseInt(strDay) > intDayMax) { return false; }


	if (fCheckBeforeDate) {
		var myDate = new Date(parseInt(strYear), parseInt(strMonth) - 1, parseInt(strDay));
		if (myDate > MustBeBefore) return false;
	}
	
	if (fCheckAfterDate) {
		var myDate = new Date(parseInt(strYear), parseInt(strMonth) - 1, parseInt(strDay));
		if (myDate < MustBeAfter) return false;
	}
	
	return true;
}

function isLeapYear(strYear) {
	if (strYear == '') { return false; }
	if (strYear.length != 4) { return false; }
	if (isNaN(strYear)) { return false; }
	
	var fltBy4 = (parseInt(strYear) / 4) - parseInt(parseInt(strYear) / 4);
	var fltBy100 = (parseInt(strYear) / 100) - parseInt(parseInt(strYear) / 100);
	var fltBy400 = (parseInt(strYear) / 400) - parseInt(parseInt(strYear) / 400);
	
	if (fltBy4 == 0) {
		if (fltBy100 == 0) {
			if (fltBy400 == 0) {
				return true;
			} else {
				return false;
			}
		} else {
			return true;
		}
	} else {
		return false;
	}
}

function blankString(strString) {
	strString = String(strString);
	
	if (strString == '') { return true; }
	
	for (var i=0; i<strString.length; i++) {
		if (strString.charCodeAt(i) != 10 && strString.charCodeAt(i) != 13 && strString.charCodeAt(i) != 32) { 
			return false;
		}
	}
	
	return true;
}

function validUsername(strName) {
	if (strName == '') return true;
	if (strName.length >= 2 && strName.match('^[a-zA-Z0-9-_]+$')) return true;
	
	return false;
}

function validPassword(strPassword) {
	if (strPassword == '') return true;
	if (strPassword.length >= 6 && strPassword.match('^[a-zA-Z0-9-_.]+$')) return true;
	
	return false;
}

function validEMail(strEMail) {
	strEMail = strEMail.toString();
	
	if (strEMail == '') return true;
	if (strEMail.search(/^([0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,4})$/) == -1) return false;
	return true;
}

function isNumber(num) {
	if(!blankString(num))
		if(isNaN(num))
			return false;
	return true;
}

function isPLZ(num){
	if (blankString(num)) return true;
	if (!isNumber(num)) return false;
	if (parseInt(num) < 1000 || parseInt(num) > 9999) return false;
	
	return true;
}

function isSelected(opt){
	if (opt.selectedIndex == 0){
		return false;
	}
	return true;
}

function isChosen(rb){
	for(i=0;i<rb.length;i++){
		if(rb[i].checked){
			return true;
		}
	}
	return false;
}

function showErrorMessage(strMessage, objControl) {
	if (objControl.focus) objControl.focus();
	if (objControl.select) objControl.select();
	alert(strMessage);
	return false;
}

function moreThanMaxLength(strData, lngMaxLength) {
	if (strData.length > lngMaxLength) return true;
	else return false;
}

function hasMinimumNumOfChar(strData,numOfChar){
	if(strData.length < numOfChar) return false;
	else return true;
}

function stringLenght(strData) {
	return strData.length;
}

function validURL(strURL) {
	if (strURL.length <= 10) return false;
	if (strURL.substr(0, 7).toLowerCase() != 'http://' && strURL.substr(0, 8).toLowerCase() != 'https://') return false;
	return true;
}

function validZipCode(strZip) {
	if (strZip == '') return true;
	if (strZip.length != 4 || isNaN(strZip)) return false;
	if (parseInt(strZip) < 1000 || parseInt(strZip) > 9999) return false;
	return true;
}

function openWindow(strURL, strWinName, lngWidth, lngHeight, fScrollbars, lngTop, lngLeft) {
	if (!lngWidth) lngWidth = 520;
	if (lngWidth == -1) lngWidth = 520;
	if (lngWidth.toString() == '') lngWidth = 520;
	if (!lngHeight) lngHeight = 600;
	if (lngHeight == -1) lngHeight = 600;
	if (lngHeight.toString() == '') lngHeight = 600;
	if (!lngTop) lngTop = ((screen.height - lngHeight) / 2);
	if (!lngLeft) lngLeft = ((screen.width - lngWidth) / 2);
	
	var strScrollbars = 'yes';
	if (!fScrollbars) strScrollbars = 'no';
	
	var hWin = window.open(strURL, strWinName, 'width=' + lngWidth + ',height=' + lngHeight + ',left=' + lngLeft + ',top=' + lngTop + ',location=no,menubar=no,resizeable=no,scrollbars=' + strScrollbars + ',status=no,toolbar=no');
	if (hWin.focus) hWin.focus();
}

var currObj = '';
var keyFunction = '';
var useObj = '';

function createObjectKeyPressEvent(htmlObject, keyFunc) {
	keyFunction = keyFunc;
	useObj = htmlObject;
	
	if(is.ns) {
		window.captureEvents(Event.KEYPRESS); // <----- offensichtlich wichtig
		window.onkeypress = doKeyEvent;
	} else {
		htmlObject.onkeypress = doKeyEvent;
	}
}

function doKeyEvent(keyEvent){
	if (useObj != currObj) return true;
    if (is.ns) {
        if (keyEvent.which == 13) eval(keyFunction);
		window.captureEvents(Event.KEYPRESS);
		window.onkeypress = doKeyEvent;
    } else if(window.event.keyCode == 13)eval(keyFunction);
    return true; 
}

function setObj(objTxt) {
	currObj = objTxt;
}

/*
   DynAPI Distribution
   Browser Class

   The DynAPI Distribution is distributed under the terms of the GNU LGPL license.
*/
function Browser() {
	var b=navigator.appName;
	if (b=="Netscape") this.b="ns";
	else if ((b=="Opera") || (navigator.userAgent.indexOf("Opera")>0)) this.b = "opera";
	else if (b=="Microsoft Internet Explorer") this.b="ie";
	if (!b) alert('Unidentified browser./nThis browser is not supported,');
	this.version=navigator.appVersion;
	this.v=parseInt(this.version);
	this.ns=(this.b=="ns" && this.v>=4);
	this.ns4=(this.b=="ns" && this.v==4);
	this.ns6=(this.b=="ns" && this.v==5);
	this.ie=(this.b=="ie" && this.v>=4);
	this.ie4=(this.version.indexOf('MSIE 4')>0);
	this.ie5=(this.version.indexOf('MSIE 5')>0);
	this.ie55=(this.version.indexOf('MSIE 5.5')>0);
	this.opera=(this.b=="opera");
	this.dom=(document.createElement && document.appendChild && document.getElementsByTagName)?true:false;
	this.def=(this.ie||this.dom); // most used browsers, for faster if loops
	var ua=navigator.userAgent.toLowerCase();
	if (ua.indexOf("win")>-1) this.platform="win32";
	else if (ua.indexOf("mac")>-1) this.platform="mac";
	else this.platform="other";
}
is = new Browser();
