
// ----------------------------------------------------------------------
// MBi Validate 1.0 (c) 2000-2001 MB-Interactive Online & Multimedia GmbH
// ----------------------------------------------------------------------

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 (theElement.cname)?theElement.cname:theElement.name;
}

function ShowAlert(theElement)
{
	var strAlert = ResString('js_alert');
	var vFormat = (theElement.format)?theElement.format:null;
	switch(vFormat)
	{
		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');
			}
	}

	strAlert = strAlert.replace(/%name%/, DisplayName(theElement));

	var strMinmaxLen = '';
	var vMinLen = (theElement.minlen)?theElement.maxlen:null;
	var vMaxLen = (theElement.maxlen)?theElement.maxlen:null;
	if ((vMinLen!=null)&&(vMaxLen!=null))
		if (vMinLen==vMaxLen)
			strMinmaxLen = ResString('js_alertExactLen');
		else
			strMinmaxLen = ResString('js_alertMinMaxLen');
	else if (vMaxLen!=null)
		strMinmaxLen = ResString('js_alertMaxLen');
	else if (vMinLen!=null)
		strMinmaxLen = ResString('js_alertMaxLen');
	strAlert = strAlert.replace(/%minmaxlen%/, strMinmaxLen);
	strAlert = strAlert.replace(/%minlen%/, (theElement.minlen)?theElement.minlen:'');
	strAlert = strAlert.replace(/%maxlen%/, (theElement.maxlen)?theElement.maxlen:'');

	var strMinmax = '';
	var vMin = (theElement.min)?theElement.min:null;
	var vMax = (theElement.min)?theElement.max:null;
	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(/%minmax%/, strMinmax);
	strAlert = strAlert.replace(/%min%/, (theElement.min)?theElement.min:'');
	strAlert = strAlert.replace(/%max%/, (theElement.max)?theElement.max:'');
	alert(strAlert);
}

function CheckField(theElement)
{
	var vRequired = (theElement.required)?Number(theElement.required):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 = (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 = (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 = (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 vMinLen = (theElement.minlen)?theElement.minlen:null;
	if (vMinLen!=null)
	{
		if (theElement.value.length < Number(vMinLen))
			return (false);
	}	

	var vMaxLen = (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);
		if (theElement.type!='hidden') 
		{
			theElement.focus();
		}
		return (false);
	}
	return (true);
}

function ValidateForm(theForm)
{
	var i;
	for(i=0;i<theForm.length;++i)
	{
		if (!ValidateField(theForm.elements[i]))
		{
			return (false);
		}
	}
	return (true);
}

function DoConfirm(theConfirm)
{
  if (changed)
  {
    if (confirm(ResString(theConfirm)))
    {
      return (true);
    }
    return (false);
  }
  return (true);
}

function NotifyChange()
{
	changed = true;
	return (false);
}

changed = false;