//	This script is mental property of DaVinci Communications Switzerland and is subject of a strict copyright.
//	Any use outside of projects from DaVinci Communications are strict forbidden! 
//	www.davinci.ch

window.onerror=ErrorFunction;

function ErrorFunction(msg, url, line)
{
	alert(msg + ' in Zeile: ' + line)
	return false;
}



function isMail(MailAdresse)
{
	var FindAt = 0;
	FindAt = MailAdresse.indexOf('@');
	SecondString = MailAdresse.substring(FindAt+1, MailAdresse.length);
	FindPoint = SecondString.indexOf('.');
	if (FindPoint>1 && FindAt>0)
	{
		return (true);
	}
	else
	{
		return (false);
	}
}



function isDateEU(MyDate)
{
	var datePart = /^(\d{1,2})(\/|.)(\d{1,2})(\/|.)(\d{4})$/;
	var DateArray = MyDate.match(datePart);

	if (DateArray == null)
	{
        	return (false);
	}

	MyMonth = DateArray[3];
	MyDay = DateArray[1];
	MyYear = DateArray[5];

	if (MyDay < 1 || MyDay > 31)
	{
	        return (false);
	}

	if (MyMonth < 1 || MyMonth > 12)
	{
		return (false);
	}

	if ((MyMonth==4 || MyMonth==6 || MyMonth==9 || MyMonth==11) && MyDay==31)
	{
		return (false);
	}

	if (MyMonth == 2)
	{
	        var isleap = (MyYear % 4 == 0 && (MyYear % 100 != 0 || MyYear % 400 == 0));
		if (MyDay > 29 || (MyDay==29 && !isleap))
		{
	            return (false);
        	}
	}

	return true;
}



function isDateUS(MyDate)
{
	var datePart = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
	var DateArray = MyDate.match(datePart);

	if (DateArray == null)
	{
        	return (false);
	}

	MyMonth = DateArray[1];
	MyDay = DateArray[3];
	MyYear = DateArray[5];

	if (MyDay < 1 || MyDay > 31)
	{
	        return (false);
	}

	if (MyMonth < 1 || MyMonth > 12)
	{
		return (false);
	}

	if ((MyMonth==4 || MyMonth==6 || MyMonth==9 || MyMonth==11) && MyDay==31)
	{
		return (false);
	}

	if (MyMonth == 2)
	{
	        var isleap = (MyYear % 4 == 0 && (MyYear % 100 != 0 || MyYear % 400 == 0));
		if (MyDay > 29 || (MyDay==29 && !isleap))
		{
	            return (false);
        	}
	}

	return true;
}



function isChar(MyText, maxLength)
{
	if (MyText.length>maxLength)
	{
		return (false);
	}
	else
	{
		return (true);
	}
}



function isInt(MyNumber)
{
	var checkOK = '0123456789-';
	var checkStr = MyNumber;
	var allValid = true;
	var decPoints = 0;
	var allNum = "";
	for (i = 0;  i < checkStr.length;  i++)
	{
		ch = checkStr.charAt(i);
		for (j = 0;  j < checkOK.length;  j++)
			if (ch == checkOK.charAt(j))
				break;
			if (j == checkOK.length)
			{
				allValid = false;
				break;
			}
			allNum += ch;
	}
	if (!allValid)
	{
		return (false);
	}
	var MyTmpNumber = parseInt(MyNumber);
	if (MyTmpNumber > 32768 || MyTmpNumber < -32768)
	{
		return (false);
	}

	return (true);
}



function isLngInt(MyNumber)
{
	var checkOK = '0123456789-';
	var checkStr = MyNumber;
	var allValid = true;
	var decPoints = 0;
	var allNum = "";
	for (i = 0;  i < checkStr.length;  i++)
	{
		ch = checkStr.charAt(i);
		for (j = 0;  j < checkOK.length;  j++)
			if (ch == checkOK.charAt(j))
				break;
			if (j == checkOK.length)
			{
				allValid = false;
				break;
			}
			allNum += ch;
	}
		if (!allValid)
	{
		return (false);
	}
		var MyTmpNumber = parseInt(MyNumber);
		if (MyTmpNumber > 2147483648 || MyTmpNumber < -2147483648)
	{
		return (false);
	}

	return (true);
}



function isSingle(MyNumber)
{
	var checkOK = "0123456789-.";
	var checkStr = MyNumber;
	var allValid = true;
	var decPoints = 0;
	var allNum = "";
	for (i = 0;  i < checkStr.length;  i++)
	{
		ch = checkStr.charAt(i);
		for (j = 0;  j < checkOK.length;  j++)
		if (ch == checkOK.charAt(j))
			break;
		if (j == checkOK.length)
		{
			allValid = false;
			break;
		}
		if (ch == ".")
		{
			allNum += ".";
			decPoints++;
		}
		else if (ch != ",")
		{
			allNum += ch;
		}
	}
		if (!allValid)
	{
		return (false);
	}
		if (decPoints > 1)
	{
		return (false);
	}
	FindAt = MyNumber.indexOf('.');
	SecondString = MyNumber.substring(FindAt+1, MyNumber.length);
	if (SecondString.length > 7)
	{
		return (false);
	}
	var MyTmpNumber = parseFloat(MyNumber);
	if (MyTmpNumber > 2147483648.00 || MyTmpNumber < -2147483648.00)
	{
		return (false);
	}
		return (true);
}



function isDouble(MyNumber)
{
	var checkOK = "0123456789-.";
	var checkStr = MyNumber;
	var allValid = true;
	var decPoints = 0;
	var allNum = "";
	for (i = 0;  i < checkStr.length;  i++)
	{
		ch = checkStr.charAt(i);
		for (j = 0;  j < checkOK.length;  j++)
		if (ch == checkOK.charAt(j))
			break;
		if (j == checkOK.length)
		{
			allValid = false;
			break;
		}
		if (ch == ".")
		{
			allNum += ".";
			decPoints++;
		}
		else if (ch != ",")
		{
			allNum += ch;
		}
	}
		if (!allValid)
	{
		return (false);
	}
		if (decPoints > 1)
	{
		return (false);
	}
	FindAt = MyNumber.indexOf('.');
	SecondString = MyNumber.substring(FindAt+1, MyNumber.length);
	if (SecondString.length > 15)
	{
		return (false);
	}
	var MyTmpNumber = parseFloat(MyNumber);
	if (MyTmpNumber > 2147483648.00 || MyTmpNumber < -2147483648.00)
	{
		return (false);
	}
		return (true);
}



function FormCheck(theForm)
{
	var FieldCount 			= document.forms[0].length;
	var FieldName 			= '';
	var FieldPiece 			= '';
	var FieldControl		= '';
	var FieldValue 			= '';
	var FieldOption			= '';
	var FieldFormat			= '';
	var theAlert			= '';
	var DisplayName			= '';
	var radioSelected		= false;
	var TmpCount 			= 255;

	for (MyCount = 0;  MyCount < FieldCount;  MyCount++) 
	{
		FieldControl		= document.forms[0].elements[MyCount].name;
		FieldValue		= document.forms[0].elements[MyCount].value;
		FieldPiece		= FieldControl.split('_');
		DisplayName		= document.forms[0].elements[MyCount].id;

		if (FieldPiece.length > 1)
		{
			FieldLanguage	= document.forms[0].language.value;	// d,e,i,f
			FieldName	= FieldPiece[0];			// fieldname for ASP, not needet in this function
			FieldOption	= FieldPiece[1];			// M = must / O = optional
			FieldFormat	= FieldPiece[2];			// DateEU, DateUS, Mail, String, Int, LngInt, Single, Double, DropDown

			if (FieldOption == 'M' && FieldFormat != 'DropDown' && FieldFormat != 'Option')
			{
				if (FieldValue.length == 0)
				{
					switch(FieldLanguage)
					{
						case 'd':
						{
							theAlert = 'Das Feld \"' + DisplayName + '\" darf nicht leer sein.';
							break;
						}
						case 'e':
						{
							theAlert = 'The field \"' + DisplayName + '\" can not be empty.';
							break;
						}
						case 'f':
						{
							theAlert = 'Le domaine \"' + DisplayName + '\" ne peut pas être vide.';
							break;
						}
						case 'i':
						{
							theAlert = 'Il campo \"' + DisplayName + '\" non può essere vuoto.';
							break;
						}
						default:
							break;
					}
					alert(theAlert);
					document.forms[0].elements[MyCount].focus();
					return (false);
				}
			}

			if (FieldFormat)
			{
				switch(FieldFormat)
				{
					case 'DateEU':
						if (!isDateEU(FieldValue) && FieldValue.length >0)
						{
							switch(FieldLanguage)
							{
								case 'd':
								{
									theAlert = 'Das eingegebene Datum im Feld \"' + DisplayName + '\" ist falsch!\n- verwenden Sie das Format \"dd.mm.yyyy\"\n- überprüfen Sie das Datum';
									break;
								}
								case 'e':
								{
									theAlert = 'Please enter the date with the format \"dd.mm.yyyy\" in the field \"' + DisplayName + '\" and correct your input.';
									break;
								}
								case 'f':
								{
									theAlert = 'La date suggérée dans le domaine \"' + DisplayName + '\" est fausse. Veuillez utiliser le format \"dd.mm.yyyy\".';
									break;
								}
								case 'i':
								{
									theAlert = 'Fornisca prego la data con la disposizione \"dd.mm.yyyy\" nel campo \"' + DisplayName + '\" e corregga il vostro input.';
									break;
								}
								default:
									break;
							}
							alert(theAlert);
							document.forms[0].elements[MyCount].focus();
							return (false);
						}
						break;

					case 'DateUS':
						if (!isDateUS(FieldValue) && FieldValue.length >0)
						{
							switch(FieldLanguage)
							{
								case 'd':
								{
									theAlert = 'Das eingegebene Datum im Feld \"' + DisplayName + '\" ist falsch!\n- verwenden Sie das Format \"mm/dd/jjjj\"\n- überprüfen Sie das Datum';
									break;
								}
								case 'e':
								{
									theAlert = 'Please enter the date with the format \"mm/dd/jjjj\" in the field \"' + DisplayName + '\" and correct your input.';
									break;
								}
								case 'f':
								{
									theAlert = 'La date suggérée dans le domaine \"' + DisplayName + '\" est fausse. Veuillez utiliser le format \"mm/dd/jjjj\".';
									break;
								}
								case 'i':
								{
									theAlert = 'Fornisca prego la data con la disposizione \"mm/dd/jjjj\" nel campo \"' + DisplayName + '\" e corregga il vostro input.';
									break;
								}
								default:
									break;
							}
							alert(theAlert);
							document.forms[0].elements[MyCount].focus();
							return (false);
						}
						break;

					case 'Mail':
						if (!isMail(FieldValue) && FieldValue.length >0)
						{
							switch(FieldLanguage)
							{
								case 'd':
								{
									theAlert = 'Die eingegebene Mailadresse ist ungültig!';
									break;
								}
								case 'e':
								{
									theAlert = 'The entered mailaddress is invalid!';
									break;
								}
								case 'f':
								{
									theAlert = 'Les mailaddress écrits est inadmissible!';
									break;
								}
								case 'i':
								{
									theAlert = 'I mailaddress inseriti è non validi!';
									break;
								}
								default:
									break;
							}
							alert(theAlert);
							document.forms[0].elements[MyCount].focus();
							return (false);
						}
						break;

					case 'String':
						if (FieldPiece[3])
						{
							TmpCount = FieldPiece[4];
						}
						if (!isChar(FieldValue, TmpCount) && FieldValue.length >0)
						{
							switch(FieldLanguage)
							{
								case 'd':
								{
									theAlert = 'In das Feld \"' + DisplayName + '\" dürfen maximal ' + TmpCount + ' Zeichen eingegeben werden.';
									break;
								}
								case 'e':
								{
									theAlert = 'Into the field \"' + DisplayName + '\" maximally ' + TmpCount + ' characters may be entered.';
									break;
								}
								case 'f':
								{
									theAlert = 'Dans le domaine \"' + DisplayName + '\" au maximum ' + TmpCount + ' caractères peuvent être écrits.';
									break;
								}
								case 'i':
								{
									theAlert = 'Nel campo \"' + DisplayName + '\" al massimo ' + TmpCount + ' caratteri possono essere inseriti.';
									break;
								}
								default:
									break;
							}
							alert(theAlert);
							document.forms[0].elements[MyCount].focus();
							return (false);
						}
						break;

					case 'Int':
						if (!isInt(FieldValue) && FieldValue.length >0)
						{
							switch(FieldLanguage)
							{
								case 'd':
								{
									theAlert = 'Die eingegebene Zahl im Feld \"' + DisplayName + '\" ist keine Zahl oder zu gross(>32000)!\nEs darf keine Dezimalzahl eingegeben werden.';
									break;
								}
								case 'e':
								{
									theAlert = 'The entered number in the field \"' + DisplayName + '\" is invalid or too large(>32000).\nNo decimal number may be entered.';
									break;
								}
								case 'f':
								{
									theAlert = 'Le nombre écrit dans le domaine \"' + DisplayName + '\" est inadmissible ou trop grand(>32000).\nAucun nombre décimal ne peut être écrit.';
									break;
								}
								case 'i':
								{
									theAlert = 'Il numero inserito nel campo \"' + DisplayName + '\" è non valido o troppo grande(>32000).\nNessun numero decimale non può essere inserito.';
									break;
								}
								default:
									break;
							}
							alert(theAlert);
							document.forms[0].elements[MyCount].focus();
							return (false);
						}
						break;

					case 'LngInt':
						if (!isLngInt(FieldValue) && FieldValue.length >0)
						{
							switch(FieldLanguage)
							{
								case 'd':
								{
									theAlert = 'Die eingegebene Zahl im Feld \"' + DisplayName + '\" ist keine Zahl.\nEs darf keine Dezimalzahl eingegeben werden.';
									break;
								}
								case 'e':
								{
									theAlert = 'The entered number in the field \"' + DisplayName + '\" is invalid.\nNo decimal number may be entered.';
									break;
								}
								case 'f':
								{
									theAlert = 'Le nombre écrit dans le domaine \"' + DisplayName + '\" est inadmissible.\nAucun nombre décimal ne peut être écrit.';
									break;
								}
								case 'i':
								{
									theAlert = 'Il numero inserito nel campo \"' + DisplayName + '\" è non valido.\nNessun numero decimale non può essere inserito.';
									break;
								}
								default:
									break;
							}
							alert(theAlert);
							document.forms[0].elements[MyCount].focus();
							return (false);
						}
						break;

					case 'Single':
						if (!isSingle(FieldValue) && FieldValue.length >0)
						{
							switch(FieldLanguage)
							{
								case 'd':
								{
									theAlert = 'Die eingegebene Zahl im Feld \"' + DisplayName + '\" ist keine Zahl oder zu gross(>32000)!\nVerwenden Sie als Dezimalzeichen einen \".\"';
									break;
								}
								case 'e':
								{
									theAlert = 'The entered number in the field \"' + DisplayName + '\" is invalid or too large(>32000).\nUse as decimal character a \".\"';
									break;
								}
								case 'f':
								{
									theAlert = 'Le nombre écrit dans le domaine \"' + DisplayName + '\" est inadmissible ou trop grand(>32000).\nUtilisation en tant que caractère décimal \".\"';
									break;
								}
								case 'i':
								{
									theAlert = 'Il numero inserito nel campo \"' + DisplayName + '\" è non valido o troppo grande(>32000).\nUso come carattere decimale \".\"';
									break;
								}
								default:
									break;
							}
							alert(theAlert);
							document.forms[0].elements[MyCount].focus();
							return (false);
						}
						break;

					case 'Double':
						if (!isDouble(FieldValue) && FieldValue.length >0)
						{
							switch(FieldLanguage)
							{
								case 'd':
								{
									theAlert = 'Die eingegebene Zahl im Feld \"' + DisplayName + '\" ist keine Zahl oder zu gross(>32000)!\nVerwenden Sie als Dezimalzeichen einen \".\"';
									break;
								}
								case 'e':
								{
									theAlert = 'The entered number in the field \"' + DisplayName + '\" is invalid or too large(>32000).\nUse as decimal character a \".\"';
									break;
								}
								case 'f':
								{
									theAlert = 'Le nombre écrit dans le domaine \"' + DisplayName + '\" est inadmissible ou trop grand(>32000).\nUtilisation en tant que caractère décimal \".\"';
									break;
								}
								case 'i':
								{
									theAlert = 'Il numero inserito nel campo \"' + DisplayName + '\" è non valido o troppo grande(>32000).\nUso come carattere decimale \".\"';
									break;
								}
								default:
									break;
							}
							alert(theAlert);
							document.forms[0].elements[MyCount].focus();
							return (false);
						}
						break;

					case 'DropDown':
						if (document.forms[0].elements[MyCount].selectedIndex < 0 && FieldOption == 'M')
						{
							switch(FieldLanguage)
							{
								case 'd':
								{
									theAlert = 'Bitte wählen Sie im Feld \"' + DisplayName + '\" eine Option aus.';
									break;
								}
								case 'e':
								{
									theAlert = 'Please select an option in field \"' + DisplayName + '\".';
									break;
								}
								case 'f':
								{
									theAlert = 'Veuillez choisir une option dans le domaine \"' + DisplayName + '\".';
									break;
								}
								case 'i':
								{
									theAlert = 'Selezioni prego una opzione nel campo \"' + DisplayName + '\".';
									break;
								}
								default:
									break;
							}
							alert(theAlert);
							document.forms[0].elements[MyCount].focus();
							return (false);
						}

						if (document.forms[0].elements[MyCount].selectedIndex == 0 && FieldOption == 'M')
						{
							switch(FieldLanguage)
							{
								case 'd':
								{
									theAlert = 'Bitte wählen Sie im Feld \"' + DisplayName + '\" eine Option aus.';
									break;
								}
								case 'e':
								{
									theAlert = 'The first option in field \"' + DisplayName + '\" is not valid.';
									break;
								}
								case 'f':
								{
									theAlert = 'La première option dans le domaine \"' + DisplayName + '\" est inadmissible.';
									break;
								}
								case 'i':
								{
									theAlert = 'La prima opzione nel campo \"' + DisplayName + '\" è non valida.';
									break;
								}
								default:
									break;
							}
							alert(theAlert);
							document.forms[0].elements[MyCount].focus();
							return (false);
						}
						break;

					case 'Option':
						if (FieldOption == 'M')
						{
							radioSelected = false;
							var tmpFieldName = document.forms[0].elements[MyCount].name;
							var tmpCount = eval('document.forms[0].' + tmpFieldName).length
							for (OptLoop = 0; OptLoop < tmpCount; OptLoop++)
							{
								if (eval('document.forms[0].' + tmpFieldName)[OptLoop].checked)
									radioSelected = true;
							}
	
							if (!radioSelected)
							{
								switch(FieldLanguage)
								{
									case 'd':
									{
										theAlert = 'Bitte wählen Sie im Feld \"' + DisplayName + '\" eine Option aus.';
										break;
									}
									case 'e':
									{
										theAlert = 'Please select an option in field \"' + DisplayName + '\".';
										break;
									}
									case 'f':
									{
										theAlert = 'Veuillez choisir une option dans le domaine \"' + DisplayName + '\".';
										break;
									}
									case 'i':
									{
										theAlert = 'Selezioni prego una opzione nel campo \"' + DisplayName + '\".';
										break;
									}
									default:
										break;
								}
								alert(theAlert);
								document.forms[0].elements[MyCount].focus();
								radioSelected = false;
								return (false);
							}
						}

					default:
						break;
				}
			}
		}
	}
	return (true);
}
