FormValid=true;

function RegExpControlValidator(ctrl,regexp,msg,required)
{
	ret=true;
	value='';
	nulable=required;
	if (nulable==null)
	{
		nulable=false;
	}
	if(ctrl.type && ctrl.type.toLowerCase()=='text' || ctrl.type.toLowerCase()=='textbox')
	{
		value=ctrl.value;
	}
	else
	{
		ret=false;
		alert('nowy typ kontrolki do walidacji. sprawd funkcję RegExpControlValidator(...)');
	}

	if (ret && (nulable==null || nulable=='true' || nulable==true) && (value==null || value==''))
	{
		ret=false;
		alert('Pole "' + ((ctrl.id)?ctrl.id:ctrl.name) + '" jest wymagane!');
	}
	if (ret)
	{
		if (!RegExpValidator(value,regexp))
		{
			ret=false;
			if (msg!=null && msg!='')
				alert(msg);
			else
				alert('Niepoprawna wartosć w polu "' + ((ctrl.id)?ctrl.id:ctrl.name) + '"');
		}
		else
		{
			ret=true;
		}
	}

	FormValid=ret;
	if (!ret)
	{
		try{ctrl.focus();
		ctrl.select();}catch(error){}
	}
	
	return (ret);
}

function RegExpValidator(value,regexp)
{
	var reg = new RegExp('^'+regexp+'$', "g");
	if (value!=null && (!value.match(reg)) && value!='')
	{
		return (false);
	}
	else
	{
		return (true);
	}
}

function ValidateForm(action)
{
	if (FormValid)
	{ 
		it('Form1').action=action;
		it('Form1').submit();
	}else{
		it('Form1').action=ValidateForm(action);
		alert('Uzupełnij formularz!');
	}
}
