function validateEmail(email) {

  // Just for not required but needs to be email
	var _retval = false;
	var mailpart = email.match("^(.+)@(.+)$");

	if ( mailpart!=null && mailpart[1]!=null) {

		var regexp_user=/^\"?[\w\-_\.]*\"?$/;
		if ( mailpart[1].match(regexp_user) != null && mailpart[2]!=null ) {

			var regexp_domain=/^[\w-\.]*\.[A-Za-z]{2,4}$/;
			var regexp_ip =/^\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]$/;
			if( mailpart[2].match(regexp_domain) != null || mailpart[2].match(regexp_ip) != null)

				_retval = true;
		}
	}
	return _retval;
}

function validateData( VLField, VLRule, VLRuleArg, VLElement, VLRequired, VLNumeric) {

	if ( (rule = VLRule.split("=")) !=null ) {
		VLRule = rule[0];
		VLRValue = rule[1];
	}
	if (VLField=="")
		VLField = VLElement.name;

	var _retval = "";
	if (VLNumeric) {
		if (VLRule=="minlen") VLRule = "greaterthan";
		else if (VLRule=="maxlen") VLRule = "lessthan";

	}

	switch(VLRule)
	{
		case "required":
			if ( VLElement.type=='select' )
				tValue = VLElement.options[VLElement.selectedIndex].value;
			else
				tValue = VLElement.value;
				
			if ((VLElement.type=='checkbox' && !(VLElement.checked)) ||
				 tValue.length == 0 ||
				(typeof(VLRuleArg)=='string' && tValue==VLRuleArg) )

					_retval = vl_required_msg.replace(/FNAME/i,VLField);

			break;

		case "alphanumeric":

			var wrongpos = VLElement.value.search("[^-A-Za-z0-9_\s\f\n\r\t\v ]");
			if ( VLElement.value.length > 0 && wrongpos >= 0) {

					wrongpos++;
					_retval = vl_alphanumeric_msg.replace(/FNAME/i,VLField);
					_retval = _retval.replace(/FWRONG/i,wrongpos);

			}
			break;

		case "numeric":

			var wrongpos = VLElement.value.search("[^0-9\.]");
			if ( VLElement.value.length > 0 && wrongpos >= 0) {

					wrongpos++;
					_retval = vl_numeric_msg.replace(/FNAME/i,VLField);
					_retval = _retval.replace(/FWRONG/i,wrongpos);
			}
			break;

		case "alpha":

			var wrongpos = VLElement.value.search("[^-A-Za-z\s\f\n\r\t\v ]");
			if ( VLElement.value.length > 0 && wrongpos >= 0) {

					wrongpos++;
					_retval = vl_alpha_msg.replace(/FNAME/i,VLField);
					_retval = _retval.replace(/FWRONG/i,wrongpos);
			}
			break;

		case "email":

			if ((VLRequired || VLElement.value!="") && !validateEmail(VLElement.value) )

					_retval = vl_email_msg.replace(/FNAME/i,VLField);

			break;

		case "minlen":

			if ( VLElement.value.length < VLRValue*1 ) {

					_retval = vl_minlen_msg.replace(/FNAME/i,VLField);
					_retval = _retval.replace(/FMUST/i,VLRValue);
					_retval = _retval.replace(/FWRONG/i,VLElement.value.length);
			}
			break;

		case "maxlen":

			if ( VLElement.value.length > VLRValue*1 ) {

					_retval = vl_maxlen_msg.replace(/FNAME/i,VLField);
					_retval = _retval.replace(/FMUST/i,VLRValue);
					_retval = _retval.replace(/FWRONG/i,VLElement.value.length);
			}
			break;

		case "lessthan":

			if ( isNaN(VLElement.value) )
			   _retval = vl_notnum_msg.replace(/FNAME/i,VLField);
			else {
				if ( VLElement.value*1 > VLRValue*1 ) {

					_retval = vl_maxval_msg.replace(/FNAME/i,VLField);
					_retval = _retval.replace(/FMUST/i,VLRValue);
					_retval = _retval.replace(/FWRONG/i,VLElement.value);
				}
			}
			break;

		case "greaterthan":

			if ( isNaN(VLElement.value) )
			   _retval = vl_notnum_msg.replace(/FNAME/i,VLField);
			else {
				if ( VLElement.value*1 < VLRValue*1 ) {

					_retval = vl_minval_msg.replace(/FNAME/i,VLField);
					_retval = _retval.replace(/FMUST/i,VLRValue);
					_retval = _retval.replace(/FWRONG/i,VLElement.value);
				}
			}
			break;

		case "regexp":

			if ( !VLElement.value.match(VLRValue)) {

					_retval = vl_reqexp_msg.replace(/FNAME/i,VLField);
					_retval = _retval.replace(/FMUST/i,VLRValue);
			}
			break;
	}
	if (_retval.length>0) _retval+="\n";
	return _retval;
}

function validateForm( VLForm, VLObject) {

	if (typeof(vl_required_msg)=='undefined') {
		vl_required_msg 	= "FNAME : Required Field";
		vl_alphanumeric_msg	= "FNAME : Only alpha-numeric symbols, wrong symbol at (FWRONG)";
		vl_alpha_msg		= "FNAME : Only alphabetic symbols, wrong symbol at (FWRONG)";
		vl_numeric_msg		= "FNAME : Only numeric symbols, wrong symbol at (FWRONG)";
		vl_maxlen_msg		= "FNAME : FMUST characters maximum, now is ( FWRONG )";
		vl_minlen_msg		= "FNAME : FMUST characters minimum, now is ( FWRONG )";
		vl_maxval_msg		= "FNAME : value should be less than FMUST, now is FWRONG";
		vl_minval_msg		= "FNAME : value should be greater than FMUST, now is FWRONG";
		vl_notnum_msg		= "FNAME : Should be a number";
		vl_email_msg		= "FNAME : Enter a valid Email address";
		vl_regexp_msg		= "FNAME : does not satisfy regexp FMUST";		
	}
	
	var _alertmess = "";
	for (var VLElement in VLObject)

		if (typeof(VLForm[VLElement]) == "undefined")

			_alertmess += "Error: there is no "+VLElement+" element in form "+VLForm+"\n";

		else
			for (var vlrule=1; vlrule < VLObject[VLElement].length; vlrule++) {

				_alertmess += validateData( VLObject[VLElement][0][0],
											VLObject[VLElement][vlrule][0],
											VLObject[VLElement][vlrule][1],
											VLForm[VLElement],
											VLObject[VLElement][0][1],
											VLObject[VLElement][0][2]
										   );
//function validateData( VLField, VLRule, VLElement, VLRequired, VLNumeric) {
			}

	if (_alertmess.length>0) alert(_alertmess);
	return (_alertmess.length==0);
}