/* STATUS:FINAL */

var forms = {};

/*	
	Add required fields to a form (by formname) and validate before submit
	----------------------------------------------------------------------
	formid -> the id of the form
	fields -> an array containing the ids of the fields that are required
	fieldnames -> an array containing the names of the fields, how they are displayed in the warning

	there should be a %formname%_alert div that can hold the warning list (ul)
	it should be like:
	
	<form name="someform" id="someform" method="post">
		required - Field 1: <input id="req" name="req"/><br/>
		not required - Field 2: <input id="req" name="notreq"/><br/>
	</form>
	<a href="#" onclick="submitForm('someform')">
	<div id="someform_alert"></div>

	<script>
		addLoadEvent(
			function() {
				addForm("someform",
				        new Array("req"),
				        new Array("Field 1")
				);
			}
		);
	</script>
*/

function addForm(formid, fields, fieldnames)
{
  forms[formid] = new Array(fields, fieldnames);
}



function addNotification(formid, notification)
{
	alertDiv = document.getElementById(formid + "_alert");
	if (!alertDiv.firstChild)
	{
		//there is no UL yet
		ul = document.createElement("ul");
		alertDiv.appendChild(ul);
	}
	li = document.createElement("li");
	li.innerHTML = notification;
	alertDiv.firstChild.appendChild(li);
}

function submitForm(formid)
{
	form = document[formid];
	fields = forms[formid][0];
	fieldnames = forms[formid][1];
	
	error = "";
	
	for (i=0; i<fields.length; i++)
	{
		fid   = fields[i];
		fname = fieldnames[i];
	
		field = form[fid];
		
		if (!field || field.value == '')
		{
			error = "Das Feld '" + fname + "' muss ausgefüllt werden.";
			addNotification(formid, error);
		}
	}
	if (error) return false;

	//if (error != '')
	//{
	//	alertdiv = document.getElementById(formid + "_alert");
	//	ul = document.createElement("ul");
	//	ul.innerHTML = error;
	//	if (alertdiv.firstChild) alertdiv.removeChild(alertdiv.firstChild);
	//	alertdiv.appendChild(ul);
	//	return false;
	//}
	form.submit();
	return false;
}

















































































/* Limitz */

