var auth={
	onSignIn: function(){
		var email=DOM('auth_signin_email');
		if (!email.value){
			return this.throwError(email);
		}
		if (!/[a-z0-9_\.-]+@[a-z0-9_\.-]+\.[a-z]{2,}/i.test(email.value)){
			return this.throwError(email);
		}
		var password=DOM('auth_signin_password');
		if (!password.value){
			return this.throwError(password);
		}
		return true;
	},
	onRegister: function(){
		
		var name=DOM('auth_register_name');
		if (!name.value){
			return this.throwError(name);
		}
		var email=DOM('auth_register_email');
		if (!email.value){
			return this.throwError(email);
		}
		if (!/[a-z0-9_\.-]+@[a-z0-9_\.-]+\.[a-z]{2,}/i.test(email.value)){
			return this.throwError(email);
		}
		var password=DOM('auth_register_password');
		if (password.value.length < 4){
			return this.throwError(password);
		}
		
		var password2=DOM('auth_register_password_confirm');
		if (password.value!=password2.value){
			return this.throwError(password2);
		}
		
		var captcha=DOM('auth_register_captcha_value');
		if (!captcha.value){
			return this.throwError(captcha);
		}
	},
	onRemind: function(){
		var email=DOM('auth_remind_email');
		if (!email.value){
			return this.throwError(email);
		}
		if (!/[a-z0-9_\.-]+@[a-z0-9_\.-]+\.[a-z]{2,}/i.test(email.value)){
			return this.throwError(email);
		}
		return true;
	},
	onChangePassword: function(){
				
		var password=DOM('auth_remind_password');
		if (password.value.length < 4){
			return this.throwError(password);
		}
		
		var password2=DOM('auth_remind_password_confirm');
		if (password.value!=password2.value){
			return this.throwError(password2);
		}
		return true;
	},
	
	displayPopup: function(selected, returnTo, callback){
		var fader=DOM('auth_fader');
		
		var screen=op.position.screen();

		document.body.appendChild(fader);
		
		fader.style.width=screen.width+'px';
		fader.style.height=screen.height+'px';
		fader.style.position='absolute';
		fader.style.display='block';
		
		this.callback=callback;
		
		var popup=DOM('auth_popup');
		document.body.appendChild(popup);
		popup.style.position='absolute';
		popup.style.visibility='hidden';
		popup.style.display='block';
		
		var pos=op.position.rectangle({width:Math.min(popup.clientWidth,op.position.screen().width), height:popup.clientHeight, left:'center',top:100});
					
		popup.style.top=pos.top+'px';
		popup.style.left=pos.left+'px';
		popup.style.visibility='visible';
		DOM('auth_signin_url').value=returnTo;
		DOM('auth_register_url').value=returnTo;
		this.activateForm(selected);
		return false;
	},
	captcha:false,
	activateForm: function(id){
		var forms=['signin','register'];		
		for (var i=0; i<forms.length; i++){
			if (id==forms[i]){				
				DOM('auth_'+id).disabled=false;
				setElementOpacity(DOM('auth_'+forms[i]),1);
				DOM('auth_'+id+'_radio').checked=true;
				DOM('auth_'+id).getElementsByTagName('input')[0].focus();
			}
			else {				
				DOM('auth_'+forms[i]).disabled=true;
				setElementOpacity(DOM('auth_'+forms[i]),0.5);
				DOM('auth_'+forms[i]+'_radio').checked=false;
			}
		}
		if (!this.captcha && id=='register'){
			
			this.captcha=DOM('auth_register_captcha');
			if (this.captcha){
				//Выводим каптчу:
				var img=DOM(new Image());
				var self=this;
				img.listen('load', function(){										
					self.captcha.parentNode.appendChild(img);
					self.captcha.parentNode.removeChild(self.captcha);
				});
				img.src=this.captcha.getAttribute('rel');
			}
			
		}
	},
	throwError: function(input){	
		alert(input.title);
		input.focus();
		return false;
	},
	initClose: function(){
		DOMReady(function(){
			var auth_close=function(){
				DOM('auth_popup').hide();
				DOM('auth_fader').hide();	
			}
			DOM('auth_close').listen('click', auth_close);
			DOM('auth_fader').listen('click', auth_close);
			DOM(null).listen('keyup',function(e){
				if (e.keyCode==keyCode.esc) auth_close();
				return true;
			})
		});
	}
};