checkForm=new Array();

function check_alert(e, regex, message, mode) {
	var _sVal = e.value;
	if ((e.tagName.toLowerCase() == 'select') && !_sVal) {
		_sVal = e.options[e.selectedIndex].text
	}
	
	var matched = _sVal.match(regex);

	if (2 == mode) {
		if (check_constraint(_sVal, regex))
			return true;
	} else if (3 == mode) { 	
		var _fVal = parseFloat(_sVal.replace(',', '.'));
		if (!isNaN(_fVal))
			_sVal = _fVal;
		if ((_fVal >= regex.min) && (_fVal <= regex.max)) {
			return true;
		}
	} else if ((mode && !matched) || (matched && !mode))
		return true;
	
	e.focus();
	alert(message);
	//if (show_tooltip) {
//		var tooltip = document.getElementById('tooltip'+e.id);
//		if (window.TOOLTIP_SHOW) clearTimeout(window.TOOLTIP_SHOW);
//		e.onkeyup = e.onblur = function () {			
//			window.TOOLTIP_SHOW  = setTimeout(function() {_tipoff(true);}, 1000);
//			e.onkeyup = e.onblur = null;			
//		}		
//		tn_4c();
//		_tipon(tooltip, '<div class="tooltip_width">'+message+'<div>', {tipRight: true, clickclose:true});
		//new PopUp(tooltip, message);
		
	//}
	
	return false;
}

function check_constraint($value, constraint) {
	return eval(constraint);
}

function strlen(s) {
	return s.length;
}

function ereg(pat, s) {
	var re = new RegExp('/'+ pat+'/');
	return s.match(pat);
}

function check_form(id) {
	var f = document.getElementById(id);
	var func = null;
	var el = null;

	for (var i=0; i < f.elements.length; i++) {
		el = f.elements[i];

		if (!(el.id && checkForm[id][el.id]))
			continue;

		//Не проверяем выключенные элементы
		if (el.disabled || el.getAttribute('readonly'))
			continue;
	
		//Необязательное пустое поле - пропускаем
		if (($F(el) == '') && checkForm[id][el.id][1] && checkForm[id][el.id][1]['optional'])
			continue;
		
		if (($F(el) == '') && checkForm[id][el.id][1] && !checkForm[id][el.id][1]['optional']) 
			return check_alert(el,"/^.{1,}$/","Необходимо заполнить это поле",0);
					
		if (el.id && checkForm[id][el.id]) {			
			for(var n=1; n<=checkForm[id][el.id].length;n++)
				if(checkForm[id][el.id][n]) {					
					func = 'func=function(el) {return check_alert(el,';
										
					if (checkForm[id][el.id][n]['mode'] != 2)
						func = func + checkForm[id][el.id][n]['regex'];
					else
						func = func + '"' + checkForm[id][el.id][n]['regex']+'"';
						
					func = func + ',"'+
						checkForm[id][el.id][n]['message'].replace(/\"/g,'\\"')+'",'+
						checkForm[id][el.id][n]['mode']+')}';										
					eval(func);
					
					if (!func(el)) {													
						return false; 
					}
				}
		}
	}
	
	return true;
}

