
var _pvValidatorClasses = new Array();
var _pvValidatorFields = new Array();


/********************************************************************************/
/* FORM VALIDATOR  Version 0.0																									*/
/********************************************************************************/

function ValidatorAddField(fvField, fvTitle, fvObligation, fvType)
{
	if (!document.getElementsByName(fvField).length) alert("unexpected form validator error!");

	var oField = document.getElementsByName(fvField)[0];
	var vName = oField.name;


	_pvValidatorFields[vName] = new Array();

	_pvValidatorFields[vName]['name'] = oField.name;
	_pvValidatorFields[vName]['title'] = fvTitle;
	_pvValidatorFields[vName]['obligation'] = fvObligation;
	_pvValidatorFields[vName]['type'] = fvType;

	_pvValidatorClasses[vName] = oField.className;
}



function ValidatorSubmit(foForm, fvAlertClass)
{
	var i;
	var vAdd;
	var vElement;
	var oElement;
	var oElement2;

	var vReturnAlert = "Folgende Felder müssen angegeben werden bzw. sind fehlerhaft:\n\n";
	var vReturnOK = true;

	if (!fvAlertClass) fvAlertClass = "validator_error";


	for (i=0; i<foForm.elements.length; i++)
	{
		oElement = foForm.elements[i];
		if (oElement.type == "checkbox")
			for (j=0; j<foForm.elements.length; j++)
			{
				oElement2 = foForm.elements[j];
				if (oElement2.type == "hidden" && oElement2.name == oElement.name.substr(1))
					oElement2.value = (oElement.checked ? oElement.value : oElement.alt);
			}
	}


	for (var vIndex in _pvValidatorFields)
	{
		vAdd = false;
		vElement = _pvValidatorFields[vIndex];
		oElement = document.getElementsByName(vElement['name'])[0];


		// CHECK FORM
		// select box with "0" value
		if (oElement.type == "select-one" && vElement['obligation'] == true
			&& (oElement.options[oElement.selectedIndex].value == "" || oElement.options[oElement.selectedIndex].value == "0"))
			vAdd = true;

		// other input fields with "" value
		else if (vElement['obligation'] == true && oElement.value == "")
			vAdd = true;

		// email verification
		else if (vElement['type'] == "email" && oElement.value != "")
		{
			if (!ValidatorCheckEmail(oElement.value))
				vAdd = true;
		}

		// phone number verification
		else if (vElement['type'] == "phone" && oElement.value != "")
		{
			if (!ValidatorCheckPhone(oElement.value))
				vAdd = true;
		}

		// zip verification
		else if (vElement['type'] == "zip" && oElement.value != "")
		{
			if (!ValidatorCheckZip(oElement.value))
				vAdd = true;
		}

		// minimum age: 18
		/*else if (vElement['type'] == "age18" && oElement.value != "")
		{
			if (!ValidatorCheckAge18(oElement.value))
				vAdd = true;
		}*/


		// ADD TO ERROR MESSAGE
		oElement.className = vAdd
			? (oElement.className + " " + fvAlertClass)
			: _pvValidatorClasses[oElement.name];
		if (vAdd)
		{
			vReturnAlert += "> " + vElement['title'] + "\n";
			vReturnOK = false;
		}
	}

	vReturnAlert += "\nBitte korregieren Sie Ihre Eingabe.";


	if (!vReturnOK)
		alert(vReturnAlert);

	return vReturnOK;
}



function ValidatorCheckEmail(fvEmail)
{
	var i;

	// alias@vDomain format
	var vIsEmail = /^(.+)@(.+)$/;

	// special characters like  ( ) < > @ , ; : \ " [ ]
	var vSpecialChars = "\\(\\)<>@,;:\\\\\\\"\\[\\]";

	// allowed chars
	var vValidChars = "\[^\\s" + vSpecialChars + "\]";

	// valid alias
	var vValidAlias = "(\"[^\"]*\")";

	// valid host
	var vValidHost = "/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/";

	// non-special characters
	var vAtom = vValidChars + "+";


	// check
	var vWord = "(" + vAtom + "|" + vValidAlias + ")";
	var vUserPattern = new RegExp("^" + vWord + "(\\." + vWord + ")*$");
	var vDomainPattern = new RegExp("^" + vAtom + "(\\." + vAtom + ")*$");


	// validate
	var vMatchArray = fvEmail.match(vIsEmail);
	if (vMatchArray == null)
		return false;

	var vUser = vMatchArray[1];
	var vDomain = vMatchArray[2];
	if (vUser.match(vUserPattern) == null)
		return false

	var vIPArray = vDomain.match(vValidHost);
	if (vIPArray != null)
	{
		for (i=1; i<=4; i++)
			if (vIPArray[i] > 255)
				return false;
	}

	var vDomainArray = vDomain.match(vDomainPattern);
	if (vDomainArray == null)
		return false

	var vAtomPat = new RegExp(vAtom, "g");
	var vDomArr = vDomain.match(vAtomPat);
	var vLen = vDomArr.vLength;
	//if (vDomArr[vDomArr.vLength-1].vLength<2 || vDomArr[vDomArr.vLength-1].vLength>3)
	if (vDomain.search(/\./) < 0)
		return false;

	if (vLen<2)
		return false;


	// valid!
	return true;
}



function ValidatorCheckPhone(fvPhone)
{
	var i;

	// allowed chars
	var vValidChars = /^\d+$/;

	// ignore special characters
	fvPhone = fvPhone.replace(/ /g, "");
	fvPhone = fvPhone.replace(/\-/g, "");
	fvPhone = fvPhone.replace(/\//g, "");
	fvPhone = fvPhone.replace(/\(/g, "");
	fvPhone = fvPhone.replace(/\)/g, "");
	fvPhone = fvPhone.replace(/^\+/, "00");


	// validate
	var vMatchArray = fvPhone.match(vValidChars);
	if (vMatchArray == null)
		return false;


	// valid!
	return true;
}



function ValidatorCheckZip(fvZip)
{
	var i;

	// allowed chars
	var vValidChars = /^\d{4,6}$/;


	// validate
	var vMatchArray = fvZip.match(vValidChars);
	if (vMatchArray == null)
		return false;


	// valid!
	return true;
}



/*function ValidatorCheckAge18(fvDayOfBirth)
{
	var vAge = 18;

	var vYear = parseInt(fvDayOfBirth.substr(0,4));
	var vMonth = parseInt(fvDayOfBirth.substr(5,2));
	var vDay = parseInt(fvDayOfBirth.substr(8,2));

	var oNow = new Date();
	var oMin = new Date(vYear, vMonth-1, vDay);

	alert(oMin.getDay());
	alert(oMin.getMonth());
	alert(oMin.getYear());

	// validate
	if (oNow < oMin)
		return false;


	// valid!
	return true;
}*/

