//Оббертка
/*Стили
	button - Активная кнопка сабмита
	button_d - Неактивная кнопка сабмита
*/

var checkClass = function (bID){
	var fields = [];
	var submit = bID;
	
	this.checking = function (){
		for (i = 0; i < fields.length; i++){
			
			if (!fields[i].getP_ok()){
				$(submit).attr("disabled","disabled");	
				$(submit).attr("class","button_d");
				return false;
				break;
			}
		}
			$(submit).attr("disabled","");	
			$(submit).attr("class","button");
			return true;
	}
	
	this.add = function (eID, regExp, erText, depend, empty){
		fields.push(new checkingField(eID, regExp, erText, this.checking, depend, empty));
	}
}

//Класс поля
	var checkingField = function (_eID, _regExp, erText, checking, _depend, _empty){
		var p_ok = false;
		var erID = _eID+"_error_b";
		var eID = _eID;
		var regExp = _regExp;
		var depend = _depend;
		var empty = _empty;
		var val;
		
		
		$(eID).after("<div id="+eID.substring(1)+"_error_b></div>");
		
		this.getP_ok = function (){
			p_ok = hidecheck();
			return p_ok;
		};

		$(eID).blur(function(){
			if (val != $(eID).val() || p_ok == false){
				p_ok = check();
				checking();
			}
		});
		
		$(eID).keyup(function(){
			if (depend != ""){
				$(depend).val("");
				$(depend+"_error_b").html("");
				$(depend+"_error_b").attr("class","error_hide");
				$(depend).attr("class","input_normal");
			}
			p_ok = hidecheck();
			checking();
		});
		
		$(eID).focus(function(){
			val = $(eID).val(); 
			if (!p_ok){
				$(erID).html(erText);
				$(eID).attr("class","input_normal");
				$(erID).attr("class","error_info");
			 }
		});
		
		function check (){
			var re = regExp;
			if (re.substring(0,1) == '#'){
				var id = re;
				re = "^"+$(re).val()+"$";
				if ($(eID).val().length == 0){
					$(erID).html("");
					$(eID).attr("class","input_normal");
					$(erID).attr("class","error_hide");
					if (empty && $(id).val()=="")
						return true;
					else
						return false;
				}
			}
			
			if (trim($(eID).val().toLowerCase()).search(re) != -1){
				$(erID).html("");
				$(erID).attr("class","error_hide")
				$(eID).attr("class","input_ok");
				return true;
			} else {
				if ($(eID).val().length == 0){
					$(erID).html("");
					$(eID).attr("class","input_normal");
					$(erID).attr("class","error_hide");
					if (empty)
						return true;
					else
						return false;
				} else {
					$(erID).html("Поле заполнено неверно!");
					$(eID).attr("class","input_warn");
					$(erID).attr("class","error_warn");
					return false;
				}
			}
		}

		function hidecheck(){
			var re = regExp;
			if (re.substring(0,1) == '#'){
				var id = re;
				re = "^"+$(re).val()+"$";
				if ($(eID).val().length == 0){
					if (empty  && $(id).val()=="")
						return true;
					else
						return false;
				}
			}
			
			if (trim($(eID).val().toLowerCase()).search(re) != -1){
				return true;
			} else {
				if ($(eID).val().length == 0){
					if (empty)
						return true;
					else
						return false;
				} else {
					return false;
				}
			}
		}
		
	}
	
	
	
	// Removes leading whitespaces
	function LTrim( value ) {
		
		var re = /\s*((\S+\s*)*)/;
		return value.replace(re, "$1");
		
	}

	// Removes ending whitespaces
	function RTrim( value ) {
		
		var re = /((\s*\S+)*)\s*/;
		return value.replace(re, "$1");
		
	}

	// Removes leading and ending whitespaces
	function trim( value ) {
		
		return LTrim(RTrim(value));
		
	}
	
	

