<!-- hide from old browsers

// $Id: form.js,v 1.1 2001/12/05 18:51:14 husi Exp $

var gMsgBoxActive = false;
var gFieldsToCheck = new Array();

function OpenHelpWin(pLoc) {
	// Ausführen
	var lWnd = "chooseWindow";
 	var lChooseWindow = window.open(pLoc, lWnd, 'alwaysRaised=yes,resizable=yes,scrollbars=yes,status=0,left=150,top=80,width=600,height=500');
 	if (lChooseWindow.focus != null)
		lChooseWindow.focus();
} // OpenHelpWin

function checkText(pTextField, pNullable, pMinLen, pMaxLen, pMatchesRegex) {
	// Is there another function with an error message ...
	if (gMsgBoxActive)
		return false;
	// If the field is null
	if (pTextField.value == "")
		return checkNullable(pTextField, pNullable);
	// Checking the minimum and maximum length
	var lRangeErrorMsg = getRangeErrorMsg(pTextField.value.length, pMinLen, pMaxLen);
	if (lRangeErrorMsg != "") {
		alertFieldErrorMessage(pTextField, "The length of \"" + pTextField.value + "\" is not in the range for \"" + pTextField.name + "\". Please enter a text with a length " + lRangeErrorMsg + ".");
		return false;
	} // if
	// Checking the regular expression
	if (pMatchesRegex != null && pMatchesRegex != "") {
		if (!pMatchesRegex.test(pTextField.value)) {
			alertFieldErrorMessage(pTextField, "\"" + pTextField.value + "\" is not valid for \"" + pTextField.name + "\". Please enter a valid text.");
			return false;
		} // if
	} // if
	// No error
	return true;
} // checkText

function checkFilename(pFilenameField, pNullable, pMinLen, pMaxLen, pMatchesRegex) {
	// Is there another function with an error message ...
	if (gMsgBoxActive)
		return false;
	// Removing all invalid characters
	pFilenameField.value = pFilenameField.value.replace(/[^\w\d \.]/g, ''); // ungültige Zeichen entfernen
	pFilenameField.value = pFilenameField.value.replace(/^[\. ]+/g, ''); // Dateinamen nicht mit Punkt oder Abstand beginnen
	// Checking the rest
	return checkText(pFilenameField, pNullable, pMinLen, pMaxLen, pMatchesRegex);
} // checkFilename

function checkLong(pLongField, pNullable, pMin, pMax) {
	// Is there another function with an error message ...
	if (gMsgBoxActive)
		return false;
	// Removing all invalid letters
	pLongField.value = pLongField.value.replace(/[^-\d]/g, '');
	// If the field is null
	if (pLongField.value == "")
		return checkNullable(pLongField, pNullable);
	// Checking, if the type is correct
	var lLongExpr = /^-?\d+$/;
	if (!lLongExpr.test(pLongField.value)) {
		alertFieldErrorMessage(pLongField, "\"" + pLongField.value + "\" is not a valid number for \"" + pLongField.name + "\". Please enter a valid number.");
		return false;
	} // if
	pLongField.value = parseInt(pLongField.value, 10);
	// Checking the minimum and maximum
	var lRangeErrorMsg = getRangeErrorMsg(pLongField.value, parseInt(pMin, 10), parseInt(pMax, 10));
	if (lRangeErrorMsg != "") {
		alertFieldErrorMessage(pLongField, "\"" + pLongField.value + "\" is not in the range for \"" + pLongField.name + "\". Please enter a number " + lRangeErrorMsg + ".");
		return false;
	} // if
	// No error
	return true;
} // checkLong

function checkDouble(pDoubleField, pNullable, pMin, pMax, pFormat) {
	// Is there another function with an error message ...
	if (gMsgBoxActive)
		return false;
	// Removing all invalid letters
	pDoubleField.value = pDoubleField.value.replace(/,/g, '.');
	pDoubleField.value = pDoubleField.value.replace(/[^-\d\.]/g, '');
	// If the field is null
	if (pDoubleField.value == "")
		return checkNullable(pDoubleField, pNullable);
	// Checking, if the type is correct
	var lDoubleExpr = /^-?\d+\.?\d*$/;
	if (!lDoubleExpr.test(pDoubleField.value)) {
		alertFieldErrorMessage(pDoubleField, "\"" + pDoubleField.value + "\" is not a valid number for \"" + pDoubleField.name + "\". Please enter a valid number.");
		return false;
	} // if
	pDoubleField.value = parseFloat(pDoubleField.value);
	// Checking the minimum and maximum
	var lRangeErrorMsg = getRangeErrorMsg(pDoubleField.value, parseFloat(pMin), parseFloat(pMax));
	if (lRangeErrorMsg != "") {
		alertFieldErrorMessage(pDoubleField, "\"" + pDoubleField.value + "\" is not in the range for \"" + pDoubleField.name + "\". Please enter a number " + lRangeErrorMsg + ".");
		return false;
	} // if
	// Formats the number
	if (pFormat != null && pFormat != "" && pFormat.indexOf(".") != -1) {
		var lNumDecimals = parseInt(pFormat.substr(pFormat.indexOf(".") + 1), 10);
		if (!isNaN(lNumDecimals) && lNumDecimals != "")
			pDoubleField.value = formatNumber(pDoubleField.value, lNumDecimals);
	} // if
	// No error
	return true;
} // checkDouble

function checkDate(pDateField, pNullable, pMin, pMax, pFormat) {
	// Is there another function with an error message ...
	if (gMsgBoxActive)
		return false;
	// Removing all invalid letters
	pDateField.value = pDateField.value.replace(/[\,\-]/g, '.');  // Alle Kommas und Punkte durch Striche ersetzen
	pDateField.value = pDateField.value.replace(/[^\d\.]/g, '');
	// If the field is null
	if (pDateField.value == "")
		return checkNullable(pDateField, pNullable);
	// Checking, if the type is correct
	var lDateExpr = /\d{1,2}\.\d{1,2}\.\d{4}/;
	if (!lDateExpr.test(pDateField.value)) {
		alertFieldErrorMessage(pDateField, "\"" + pDateField.value + "\" is not a valid date for \"" + pDateField.name + "\". Please enter a valid date in the format \"dd.mm.yyyy\".");
		return false;
	} // if
	// Checking the minimum and maximum
	// it follows ...
	return true;
} // checkDate

function checkNullable(pField, pNullable) {
	if (pNullable == null || pNullable == "" || pNullable == "yes" || pNullable == true)
		pNullable = true;
	else
		pNullable = false;
	if (!pNullable) {
		alertFieldErrorMessage(pField, "The field \"" + pField.name + "\" can not be empty. Please enter a value.");
		return false;
	} else {
		return true;
	} // if
} // checkNullable

function getRangeErrorMsg(pValue, pMin, pMax) {
	var lRangeErrorMsg = "";
	if (pMin != null && pMin != "" && !isNaN(pMin)) {
		if (pMax != null && pMax != "" && !isNaN(pMax)) {
			if (pValue < pMin || pValue > pMax)
				lRangeErrorMsg = "between " + pMin + " and " + pMax;
		} else {
			if (pValue < pMin)
				lRangeErrorMsg = ">= " + pMin;
		} // if
	} else {
		if (pMax != null && pMax != "" && !isNaN(pMax)) {
			if (pValue > pMax)
				lRangeErrorMsg = "<= " + pMax;
		} // if
	} // if
	return lRangeErrorMsg;
} // getRangeErrorMsg

function putFieldConstraint(pFieldName, pType, pNullable, pMin, pMax, pOption) {
	gFieldsToCheck.push(new Array(pFieldName, pType, pNullable, pMin, pMax, pOption));
} // putFieldConstraint

function checkField(pField, pCheckNullable) {
	// Is there another function with an error message ...
	if (gMsgBoxActive)
		return false;
	
	// looking for the field to check
	var lFieldToCheck = null;
	for (var i = 0; i < gFieldsToCheck.length && lFieldToCheck == null; i++)
		if (gFieldsToCheck[i][0] == pField.name)
			lFieldToCheck = gFieldsToCheck[i];
	if (lFieldToCheck == null)
		return true;
	// saving the constraints
	var lType = lFieldToCheck[1];
	var lNullable = lFieldToCheck[2];
	var lMin = lFieldToCheck[3];
	var lMax = lFieldToCheck[4];
	var lFormat = lFieldToCheck[5];
	var lMatchesRegex = lFieldToCheck[5];

	// checking the field
	switch (lType) {
		case "string":
			return checkText(pField, (pCheckNullable)?lNullable:true, lMin, lMax, lMatchesRegex);
			break;
		case "long":
			return checkLong(pField, (pCheckNullable)?lNullable:true, lMin, lMax, lFormat);
			break;
		case "double":
			return checkDouble(pField, (pCheckNullable)?lNullable:true, lMin, lMax, lFormat);
			break;
		case "date":
			return checkDate(pField, (pCheckNullable)?lNullable:true, lMin, lMax, lFormat);
			break;
		default:
			return true;
	} // switch
} // checkField

function checkAllFields(pForm) {
	// Checking if all required fields have a value
	for (var i = 0; i < pForm.length; i++) {
		if (pForm[i].type != "hidden") {
			if (!checkField(pForm[i], true))
				return false;
		} // if
	} // for
	return true;
} // checkAllFields

function alertFieldErrorMessage(pField, pErrorMessage) {
	gMsgBoxActive = true;
	alert(pErrorMessage);
	pField.select();
	pField.focus();
	gMsgBoxActive = false;
} // alertFieldErrorMessage

function formatNumber(Number,Decimals,Separator) {
 // **********************************************************
 // Placed in the public domain by Affordable Production Tools
 // March 21, 1998
 // Web site: http://www.aptools.com/
 //
 // November 24, 1998 -- Error which allowed a null value
 // to remain null fixed. Now forces value to 0.
 //
 // October 28, 2001 -- Modified to provide leading 0 for fractional number
 // less than 1.
 //
 // This function accepts a number to format and number
 // specifying the number of decimal places to format to. May
 // optionally use a separator other than '.' if specified.
 //
 // If no decimals are specified, the function defaults to
 // two decimal places. If no number is passed, the function
 // defaults to 0. Decimal separator defaults to '.' .
 //
 // If the number passed is too large to format as a decimal
 // number (e.g.: 1.23e+25), or if the conversion process
 // results in such a number, the original number is returned
 // unchanged.
 // **********************************************************
 Number += ""          // Force argument to string.
 Decimals += ""        // Force argument to string.
 Separator += ""       // Force argument to string.
 if((Separator == "") || (Separator.length > 1))
  Separator = "."
 if(Number.length == 0)
  Number = "0"
 var OriginalNumber = Number  // Save for number too large.
 var Sign = 1
 var Pad = ""
 var Count = 0
 // If no number passed, force number to 0.
 if(parseFloat(Number)){
  Number = parseFloat(Number)} else {
  Number = 0}
 // If no decimals passed, default decimals to 2.
 if((parseInt(Decimals,10)) || (parseInt(Decimals,10) == 0)){
  Decimals = parseInt(Decimals,10)} else {
  Decimals = 2}
 if(Number < 0)
 {
  Sign = -1         // Remember sign of Number.
  Number *= Sign    // Force absolute value of Number.
 }
 if(Decimals < 0)
  Decimals *= -1    // Force absolute value of Decimals.
 // Next, convert number to rounded integer and force to string value.
 // (Number contains 1 extra digit used to force rounding)
 Number = "" + Math.floor(Number * Math.pow(10,Decimals + 1) + 5)
 if((Number.substring(1,2) == '.')||((Number + '')=='NaN'))
  return(OriginalNumber) // Number too large to format as specified.
 // If length of Number is less than number of decimals requested +1,
 // pad with zeros to requested length.
 if(Number.length < Decimals +1) // Construct pad string.
 {
  for(Count = Number.length; Count <= Decimals; Count++)
   Pad += "0"
 }
 Number = Pad + Number // Pad number as needed.
 if(Decimals == 0){
  // Drop extra digit -- Decimal portion is formatted.
  Number = Number.substring(0, Number.length -1)} else {
  // Or, format number with decimal point and drop extra decimal digit.
 Number = Number.substring(0,Number.length - Decimals -1) +
          Separator +
          Number.substring(Number.length - Decimals -1,
          Number.length -1)}
 if((Number == "") || (parseFloat(Number) < 1))
  Number="0"+Number // Force leading 0 for |Number| less than 1.
 if(Sign == -1)
  Number = "-" + Number  // Set sign of number.
 return(Number)
}

// end hiding from old browsers -->
