// ------------------------------------------------------------------
// MBi Validate 1.0 (c) 2000 MB-Interactive Online & Multimedia GmbH
// ------------------------------------------------------------------

function ResString(aID)
{
switch (aID)
{
case 'js_alert':
return ('Geben Sie einen Wert %minmax%in das Feld \"%name%\" ein.');
case 'js_alertInt':
return ('Geben Sie eine ganze Zahl %minmax%in das Feld \"%name%\" ein.');
case 'js_alertFloat':
return ('Geben Sie eine Zahl %minmax%in das Feld \"%name%\" ein.');
case 'js_alertDate':
return ('Geben Sie ein gültiges Datum (tt.mm.jjjj [hh:mm[:ss]]) %minmax%in das Feld \"%name%\" ein.');
case 'js_alertEmail':
return ('Geben Sie eine gültige Email-Adresse (name@domain.ort) in das Feld \"%name%\" ein.');
case 'js_alertUrl':
return ('Geben Sie eine gültige Internet-Adresse (name.domain.ort) in das Feld \"%name%\" ein.');
case 'js_alertRadio':
return ('Wählen Sie eine Option des Feldes \""%name%\"" aus.');
case 'js_alertMinMax':
return ('größer oder gleich \"%min%\" und kleiner oder gleich \"%max%\" ');
case 'js_alertMin':
return ('größer oder gleich \"%min%\" ');
case 'js_alertMax':
return ('kleiner oder gleich \"%max%\" ');
case 'js_alertMaxLen':
return ('bestehend aus maximal %maxlen% Zeichen ');
case 'js_confirmCancel':
return ('Sollen die Änderungen verworfen werden?');
case 'js_confirmDelete':
return ('Soll der Datensatz wirklich gelöscht werden?\n\nAchtung: Wenn sie den Datensatz wiederherstellen\nmöchten,wenden Sie sich an den Administrator!');
case 'js_confirmDeletePermanent':
return ('Soll der Datensatz wirklich endgültig gelöscht werden?\n\nAchtung: der Datentensatz kann nicht wiederhergestellt werden!');
case 'js_confirmUndelete':
return ('Soll der Datensatz wiederhergestellt werden?');
}
}
function showHelpWindow()
{
help = open("/scripts/Webdb/help/","Help","width=480,height=320,scrollbars,resizable");
help.focus()
}


function LeapYear(year) {
   return ((year % 4) == 0) ? (((year % 100) == 0) ? (((year % 400) == 0) ? true : false) : true) : false;
}

function DaysInMonth(month, year) {
   var days;

   switch (month) {
      case 1:
      case 3:
      case 5:
      case 7:
      case 8:
      case 0:
      case 12:
         days = 31;
         break;
      case 4:
      case 6:
      case 9:
      case 11:
         days = 30;
         break;
      case 2:
         days = LeapYear(year) ? 29 : 28;
         break;
   }
   return days;
}

function ValidDateTime(theDate)
{
	var day, month, year;
	var hour, minute, second;
	var i1,i2,i3,i4, i5;
	i1 = theDate.indexOf(".");
	if (i1 == -1)
		return (false);
	i2 = theDate.indexOf(".", i1 + 1);
	if (i2 == -1)
		return (false);
	i3 = theDate.indexOf(" ", i2 + 1);
	day = Number(theDate.substr(0, i1));
	month = Number(theDate.substr(i1 + 1, i2 - i1 - 1));
	if (i3 == -1)
	{
	 	year = Number(theDate.substr(i2 + 1));
	 	hour = 0;
	 	minute = 0;
	 	second = 0;
	 }
	 else
	 {
	 	year = Number(theDate.substr(i2 + 1, i3 - i2 - 1));
	 	i4 = theDate.indexOf(":", i3 + 1);
	 	if (i4 == -1)
	 		return (false)
	 	hour = Number(theDate.substr(i3 + 1, i4 - i3 - 1));
	 	i5 = theDate.indexOf(":", i4 + 1);
	 	if (i5 == -1)
	 	{
	 		minute = Number(theDate.substr(i4 + 1));
	 		second = 0;
	 	}
	 	else
	 	{
	 		minute = Number(theDate.substr(i4 + 1, i5 - i4 - 1));
	 		second = Number(theDate.substr(i5 + 1));
	 	}
	}
	if (isNaN(day) || isNaN(month) || isNaN(year) || isNaN(hour) || isNaN(minute) || isNaN(second))
	   return (false);
	if (year < 100)
	{
	 	if (year < 30)
	 		year = year + 2000;
	 	else
	 		year = year + 1900;
	}
	if ((day < 1) || (day > 31))
	   return (false);
	if ((month < 1) || (month > 12))
	   return (false);
	if (day > DaysInMonth(month, year))
	   return (false);
	if ((hour < 0) || (hour > 23))
	   return (false);
	if ((minute < 0) || (minute > 59))
	   return (false);
	if ((second < 0) || (second > 59))
	   return (false);
	return (true);
}

function ValidEmail(theEmail)
{
	var i1, i2;
	i1 = theEmail.indexOf("@");
	if (i1 == -1)
		return (false);
	i2 = theEmail.indexOf(".", i1 + 1);
	if (i2 == -1)
		return (false);
	return (true);
}

function ValidUrl(theUrl)
{
	var i1, i2;
	i1 = theUrl.indexOf(":");
	if (i1 == -1)
		i1 = 0;
	i2 = theUrl.indexOf(".", i1 + 1);
	if (i2 == -1)
		return (false);
	return (true);
}

function ValidRadio(theElement)
{
	var i
	for (i=0;i<theElement.length;i++)
	{
		if (theElement[i].checked)
			return (true);
	}
	return (false);
}

function DisplayName(theElement)
{
	return (eval('theElement.cname'))?theElement.cname:theElement.name;
}

function ShowAlert(theElement)
{
	var strAlert = ResString('js_alert');
	var vFormat = (eval('theElement.format'))?theElement.format:null;
	switch(theElement.format)
	{
		case 'int':
			strAlert = ResString('js_alertInt');
			break;
		case 'float':
			strAlert = ResString('js_alertFloat');
			break;
		case 'date':
			strAlert = ResString('js_alertDate');
			break;
		case 'email':
			strAlert = ResString('js_alertEmail');
			break;
		case 'url':
			strAlert = ResString('js_alertUrl');
			break;
		default:
			if (theElement.type=='radio')
			{
				strAlert = ResString('js_alertRadio');
			}
	}

	var strMinmax = '';
	var vMaxLen = (eval('theElement.maxlen'))?theElement.maxlen:null;
	var vMin = (eval('theElement.min'))?theElement.min:null;
	var vMax = (eval('theElement.min'))?theElement.max:null;
	if ((vMaxLen!=null)&&(theElement.value.length > Number(vMaxLen)))
		strMinmax = ResString('js_alertMaxLen');
	else if ((vMin!=null)&&(vMax!=null))
		strMinmax = ResString('js_alertMinMax');
	else if (vMin!=null)
		strMinmax = ResString('js_alertMin');
	else if (vMax!=null)
		strMinmax = ResString('js_alertMax');
	strAlert = strAlert.replace(/%name%/, DisplayName(theElement));
	strAlert = strAlert.replace(/%minmax%/, strMinmax);
	strAlert = strAlert.replace(/%min%/, (eval('theElement.min'))?theElement.min:'');
	strAlert = strAlert.replace(/%max%/, (eval('theElement.max'))?theElement.max:'');
	strAlert = strAlert.replace(/%maxlen%/, (eval('theElement.maxlen'))?theElement.maxlen:'');
	alert(strAlert);
}

function CheckField(theElement)
{
	var requiredAttr = eval('theElement.required');
	if(!requiredAttr) requiredAttr = theElement.getAttribute("required");
	var vRequired = (requiredAttr)?Number(requiredAttr):0;

	if (theElement.value=='')
	{
		if (vRequired!=0)
			return (false);
		else
			return (true);
	}

	if (theElement.type=='radio')
	{
		if (!ValidRadio(theElement.form.elements[theElement.name]))
			return (false);
		return (true);
	}

	var vFormat = (eval('theElement.format'))?theElement.format:null;
	switch(vFormat)
	{
		case 'int':
			if ((isNaN(theElement.value))||(Number(theElement.value)!=parseInt(theElement.value)))
				return (false);
			break;
		case 'float':
			if (isNaN(theElement.value.replace(/,/, '.')))
				return (false);
			break;
		case 'date':
			if (!ValidDateTime(theElement.value))
				return (false);
			break;
		case 'email':
			if (!ValidEmail(theElement.value))
				return (false);
			break;
		case 'url':
			if (!ValidUrl(theElement.value))
				return (false);
			break;
	}

	var vMin = (eval('theElement.min'))?theElement.min:null;
	if (vMin!=null)
	{
		switch(vFormat)
		{
			case 'int':
			case 'float':
				if (Number(theElement.value.replace(/,/, '.')) < Number(vMin.replace(/,/, '.')))
					return (false);
				break;
			default:
				if (theElement.value < vMin)
					return (false);
		}
	}

	var vMax = (eval('theElement.min'))?theElement.max:null;
	if (vMax!=null)
	{
		switch(vFormat)
		{
			case 'int':
			case 'float':
				if (Number(theElement.value.replace(/,/, '.')) > Number(vMax.replace(/,/, '.')))
					return (false);
				break;
			default:
				if (theElement.value > vMax)
					return (false);
		}
	}
	
	var vMaxLen = (eval('theElement.maxlen'))?theElement.maxlen:null;
	if (vMaxLen!=null)
	{
		if (theElement.value.length > Number(vMaxLen))
			return (false);
	}	
	return (true);
	
}

function ValidateField(theElement)
{

	if (!CheckField(theElement))
	{
		ShowAlert(theElement);
		theElement.focus();
		return (false);
	}
	return (true);
}

function ValidateForm(theForm)
{
	var i;
	//skdkadja;
	for(i=0;i<theForm.length;++i)
	{
	
		if (!ValidateField(theForm.elements[i]))
		{
			return (false);
		}
	}

	try {
		if (CheckThisForm2) {
			return CheckThisForm2(theForm);
		}
	}
	catch(eIgnoreFunctionMayNotExist) {}

	return (true);
}


