var $_Form=function(form,lang,onfocus,onsent,onload){
	if(onsent&&onsent.constructor==Function)form.onsubmit=onsent;
	if(onload&&onload.constructor==Function)onload.call(form);
	this.initialize(form,lang,onfocus);
}

$_Form.prototype={
	initialize:function(form,lang,fn){
		var as,list=form.elements;
		for(var i=0,count=list.length;i<count;i++){
			try{as=$_valid[list[i].name][0];}catch(e){as=null}
			list[i].old_value=lang[list[i].name]||'';
			list[i].onfocus=fn;
			list[i].onblur=$_Form.simple;
			if(as===null){ continue;
			}else if(as===''){
				list[i].onblur=function(){
					$_Form.validate.call(this,true,$_valid[this.name][1]);
			}}else if($_Form[as]&&$_Form[as].constructor==Function){
				if(list[i].type=='radio')list[i].onclick=$_Form[as];
				else list[i].onblur=$_Form[as];
			}else if($_Form[as]&&$_Form[as].constructor==RegExp){
				list[i].onblur=function(){
					var as=$_valid[this.name][0];
					return $_Form.validate.call(this,$_Form[as].test(this.value),$_valid[this.name][1])
			}}else if(form[as]&&$_valid[form[as].name][0]==list[i].name){
				var f=function(){
					arguments.callee.fn.call(this);
					$_Form.changeTypes.call(this,'password');
				};f.fn=fn;
				list[i].onfocus=f;
				list[i].onblur=function(){
					return $_Form.validateMerge.call(this,$_valid[this.name][0],$_valid[this.name][1]);
				}
			}
		}
		form=fn=lang=list=null;
	}
}

$_Form.classes={
	wrong:'wrong',
	active:'active',
	correct:'correct',
	error:'error',
	need:'imp'
}

$_Form.is_mail=/[\w\d-_]+@[\w\d-_]+\.([\w\d-_]+\.*)+/i;
$_Form.is_date=/\d{2,4}\.\d{2}\.\d{2,4}/i;
$_Form.is_phone=/[\d\s()-]+/i;
$_Form.is_number=/\d+/i;
$_Form.is_word=/[^\d]/i;
$_Form.is_login=/[a-zA-Z0-9_]+/i

$_Form.isNeed=function(){
	return $_Form.find.call(this.parentNode,$_Form.classes.need)
}

function setClassName(obj,className){
	if(!hasClassName(obj,className)) obj.className+=' '+className;
}

function removeClassName(obj,className){
	var cl=obj.className.split(' '),count=cl.length,i;
	for(i=0;i<count;i++){
		if(!cl[i]) continue;
		if(cl[i]==className) cl[i]='';
	}
	obj.className=cl.join(' ');
}

function hasClassName(obj,className){
	var cl=obj.className.split(' '),count=cl.length,i,check=false;
	for(i=0;i<count;i++){
		if(!cl[i]||cl[i]!=className)continue;
		check=true;
		break;
	}
	return check;
}

$_Form.validate=function(is,msg){
	var state=true,err=$_Form.find.call(this.parentNode,$_Form.classes.error);
	removeClassName(this,$_Form.classes.active);
	removeClassName(this,$_Form.classes.wrong);
	if(!is || this.value=='' || this.value==this.old_value){
		setClassName(this,$_Form.classes.wrong);
		err.innerHTML=msg;
		state=false;
	}else{
		err.innerHTML='';
		//setClassName(this,$_Form.classes.correct);
		state=true;
	}
	if(this.value=='')this.value=this.old_value;
	this.is=is||this.value=='';
	return state;
}

$_Form.simple=function(){
	removeClassName(this,$_Form.classes.active);
	if(this.value==''){
		this.value=this.old_value;
		return false
	}else{
		return true
	}
}

$_Form.validateMerge=function(nm,msg){
	removeClassName(this,$_Form.classes.active);
	removeClassName(this,$_Form.classes.wrong);
	removeClassName(this.form[nm],$_Form.classes.active);
	removeClassName(this.form[nm],$_Form.classes.wrong);
	var err=$_Form.find.call(this.parentNode,$_Form.classes.error),inp=this,state=true;
	if(this.value==''){
		inp=$_Form.changeTypes.call(this,'text');
		inp.value=inp.old_value;
		setClassName(inp,$_Form.classes.wrong);
		err.innerHTML=msg;
		state=false
	}
	if(inp.type==inp.form[nm].type) if(inp.value!=inp.form[nm].value){
		setClassName(inp,$_Form.classes.wrong);
		setClassName(inp.form[nm],$_Form.classes.wrong);
		err.innerHTML=msg;
		state=false
	}else{
		state=true
		//setClassName(inp,$_Form.classes.correct);
		//setClassName(inp.form[nm],$_Form.classes.correct);
		err.innerHTML='';
	}
	err=inp=null;
	return state
}

$_Form.validateRadio=function(name,msg){
	var radio=this.form[this.name],count=radio.length,i,check=false;
	for(i=0;i<count;i++) if(radio[i].checked){
		check=true;
		break;
	}
	if(!check){
		var err=$_Form.find.call(this.parentNode,$_Form.classes.error);
		err.innerHTML=msg;
	}
	name.v=this.name;
	return check;
}

$_Form.changeTypes=function(type){
	var b=navigator.userAgent.toLowerCase();
	if(/msie/.test(b)&&!/opera/.test(b)){
		var inp;
		if(!(inp=this.form[this.changer])){
			inp=document.createElement('input');
			inp.type='password';
			inp._blur=this.onblur;
			this.onblur=this.onfocus=null;
			removeClassName(this,$_Form.classes.active);
			removeClassName(this,$_Form.classes.wrong);
			this.parentNode.insertBefore(inp,this);
			this.onfocus=function(){
				removeClassName(this,$_Form.classes.active);
				removeClassName(this,$_Form.classes.wrong);
				this.previousSibling.style.display='';
				this.style.display='none';
				setClassName(this.previousSibling,$_Form.classes.active);
				this.previousSibling.focus();
			}
			this.onblur=function(){
				this.previousSibling.style.display='';
				this.style.display='none';
			}
			this.value=this.old_value;
			inp.onblur=function(){
				this._blur.call(this);
				if(this.value==''){
					this.nextSibling.style.display='';
					setClassName(this.nextSibling,$_Form.classes.wrong);
					this.style.display='none';
				}
			};
			inp.onfocus=function(){
				setClassName(this,$_Form.classes.active);
			}
			inp.setAttribute('name',this.name);
			inp.changer=this.changer=inp.name;
			this.removeAttribute('name');
			this.form[inp.name]=inp;
			inp.className=this.className;
		}
		if(type!=inp.type){
			inp.style.display='none';
			inp=inp.nextSibling;
			inp.style.display='';
		}else{
			inp.style.display='';
			inp.nextSibling.style.display='none';
			inp.focus();
		}
		return inp;
	}else{
		$_Form.changeTypes=function(type){
			this.type=type;
			return this;
		}
		return $_Form.changeTypes.call(this,type);
	}
}

$_Form.find=function(type){
	var ch=this.childNodes,count=ch.length,i,error=null;
	for(i=count-1;i>=0;i--){
		if(ch[i].nodeType==3||!hasClassName(ch[i],type)) continue;
		error=ch[i];break;
	}
	return error;
}

$_Form.enabled=function(){
	this.form.u_childs.disabled=false;
}

$_Form.disabled=function(){
	this.form.u_childs.disabled=true;
	removeClassName(this.form.u_childs,$_Form.classes.wrong);
	var err=$_Form.find.call(this.form.u_childs.parentNode,$_Form.classes.error);
	err.innerHTML='';
}
