var valid=true;          //Флаг общей проверки
var bgcolor='#DDDDDD';   //Цвет поля по умолчанию
var colors=new Array(6); //Массив цветов
var matches='';          //Захват шаблона
var value_matches='';    //Захват шаблона

function init_validate(FormObj){
	valid=true;

	colors[0]='#FFB0B0';
	colors[2]='#D7B0FF';
	colors[3]='#B2FFB0';
	colors[1]='#FFB0D8';
	colors[4]='#6495ED';
	colors[5]='#CC9AE4';

	ind=Math.round(Math.random()*5.4);
	bgcolor=colors[ind];

	form_validate(FormObj);

	if(valid) return true;
	else return false;
}

function form_validate(FormObj){
	var children=FormObj.childNodes;
	var regExpObj=/(.+)@(.+)\.(.+)/;
	for (var i=0; i<children.length; i++){    if(children.item(i).tagName=='INPUT' || children.item(i).tagName=='TEXTAREA'){
  		if(children.item(i).getAttribute('validate')){
			  children.item(i).style.backgroundColor='';
				switch (children.item(i).getAttribute('validate')){
  				case 'not_empty': if(children.item(i).value==''){
					  children.item(i).style.backgroundColor=bgcolor;
					    alert('Поле "'+children.item(i).getAttribute('title')+'" не должно быть пустым');
							valid=false;
							return false;
							break;
						}
					break;

					case 'number':
  					if(parseFloat(children.item(i).value)!=children.item(i).value){
  						children.item(i).style.backgroundColor=bgcolor;
							alert("Введено не числовое значение");
              valid=false;
              return false;
              break;
  					}
					break;

					case 'email':
						if(!regExpObj.test(children.item(i).value)){
							children.item(i).style.backgroundColor=bgcolor;
							alert("Введен некорректный адрес email");
							valid=false;
							return false;
							break;
						}
					break;

					case 'email_or_empty':
					  if(children.item(i).value!=''){
						  if(!regExpObj.test(children.item(i).value)){
  							children.item(i).style.backgroundColor=bgcolor;
								alert("Введен некорректный адрес email");
								valid=false;
								return false;
								break;
							}
						}
					break;
				 }


				 if( children.item(i).getAttribute('validate').substring(0,5)=='range' && children.item(i).value!=''){
	         matches=children.item(i).getAttribute('validate').match('range.*?([0-9]+),.*?([0-9]+).*');
					 if(children.item(i).value.match('^[0-9]+$')){
	  	     if( (children.item(i).value*1) > matches[1] && (children.item(i).value*1) < matches[2]){}
					 else{					  children.item(i).style.backgroundColor=bgcolor;
						alert("Число не находится в диапазоне от "+matches[1]+" до "+matches[2]);
	          valid=false;
	          return false;
					 }
				 }

				 else if(children.item(i).value.match('^[0-9]+-[0-9]+$')){
	  		   value_matches=children.item(i).value.match('^([0-9]+)-([0-9]+)$');
				   if(value_matches[1]*1 > matches[1] && value_matches[1]*1 < matches[2] && value_matches[2]*1 > matches[1] && value_matches[2]*1 < matches[2]){}
				   else{
					   children.item(i).style.backgroundColor=bgcolor;
					   alert("Введенный Вами диапазон не находится внутри разрешенного: от "+matches[1]+" до "+matches[2]);
	           valid=false;
	           return false;
					 }
	       }

				 else{				   children.item(i).style.backgroundColor=bgcolor;
					 alert("Не число или диапазон вида 100-1000");
	         valid=false;
	         return false;
				 }
			 }
		 }
   }

	 if(children.item(i).hasChildNodes){form_validate(children.item(i));}
	 if(valid == false) break;
 }

 if(valid) return true;
 else return false;
}

