/**
 * @author Coolmen
 */

// Login box class
function loginClass () {

	this.constructor = function () {
		return true;
	}

	this.init = function () {
		this.loginForm = $('login-form');
		if ( !this.loginForm ) return false;
		this.loginControl = $('ll');
		this.passwordControl = $('lp');
		this.passwordFalseControl = $('lt');
		this.submitControl = $('ls');

		this.loginControl.disabled = false;
		this.passwordControl.disabled = false;
		this.passwordFalseControl.disabled = false;

		this.loginControl.onfocus = _login.lc_onfocus;
		this.loginControl.onblur = _login.lc_onblur;
		this.loginControl.onkeydown = _login.on_keydown;
		this.passwordFalseControl.onfocus = _login.pc_onfocus;
		this.passwordControl.onkeydown = _login.on_keydown;
		this.passwordControl.onblur = _login.pc_onblur;
		this.submitControl.onclick = _login.sc_onclick;
	}
	
	this.lc_onfocus = function () {
		if ( this.value == 'логин' ) this.value = '';
	}
	this.lc_onblur = function () {
		if ( this.value == '' ) this.value = 'логин';
	}
	this.pc_onfocus = function () {
		this.className = 'hidden';
		_login.passwordControl.className = '';
		_login.passwordControl.focus();
	}
	this.pc_onblur = function () {
		if (this.value == '') {
			this.className = 'hidden';
			_login.passwordFalseControl.className = '';
		}
	}
	this.sc_onclick = function () {
		if ( _login.passwordControl.value && _login.loginControl.value ) {
			_login.loginForm.submit();
			return false;
		}
		else {
			return false;
		}
	}
	this.on_keydown = function ( event ) {
		if ( !event ) event = window.event;
		if ( event.keyCode == 0x0D ) {
			if ( _login.passwordControl.value && _login.loginControl.value ) {
				_login.loginForm.submit();
			} 
			else if ( !_login.loginControl.value )		_login.loginControl.focus()
//			else if ( !_login.passwordControl.value )	_login.passwordControl.focus()
			return false;
		}
	}
}


// Reg box class
function RegBoxClass () {
	this.constructor = function () {
		alert('Constructor');
	}
	
	this.init = function (id) {
		this.registry = $(id);
		if ( !this.registry ) return false;
		this.labels = new Array;
		var labels = this.registry.getElementsByTagName('label');
		var pass = null;
		for ( var i = 0; i < labels.length; i++ ) {
			if ( labels[i].className.match(/^valid/i) ) {
				if ( labels[i].htmlFor ) {
					document.getElementById( labels[i].htmlFor )._Label = labels[i];
					document.getElementById( labels[i].htmlFor ).onchange = function () { _reg.change(this) }; 
					document.getElementById( labels[i].htmlFor ).onkeyup = function () { _reg.change(this) };
					if ( pass &&  labels[i].className.match(/^valid-confirm/i) ) $( labels[i].htmlFor )._Confirm = pass;  
					if ( $( labels[i].htmlFor ).type == 'password' ) pass = $( labels[i].htmlFor );
					this.change(document.getElementById( labels[i].htmlFor ));
					this.labels.push( document.getElementById( labels[i].htmlFor ) );
				}
			}
		}
		this.valid();
	}

	this.valid = function(){
		var fl = 0;
		for (var i = 0; i < this.labels.length; i++) {
			if ( !this.labels[i].value.match(/[^\s]+/i) ) {
				fl = 1;
			} 
		}
//		$('submit').disabled = fl ? true : false;
	}

	this.change = function(el){
		if ( el._Label.className.match(/^valid-email/i) ) {
			if ( el.value.match(/\w+@\w+\.\w+/i) ) el._Label.className = "valid-email ok"
				else el._Label.className = "valid-email false";
		}
		else if ( el._Label.className.match(/^valid-confirm/i) ) {
			if ( el.value && el.value == el._Confirm.value ) el._Label.className = "valid-confirm ok"
				else el._Label.className = "valid-confirm false";
		}
		else {
			if ( el.value.match(/[^\s]+/i) ) el._Label.className = "valid ok"
				else el._Label.className = "valid false";
		}
		this.valid();
	}
} 


window.onload = function () {
	if ( _login = new loginClass() ) _login.init();
	if ( _reg = new RegBoxClass() ) _reg.init('reg-form');
}

// а-ля Prototype framework 
window.$ = function (id) {
	if ( !id ) return null;
	 return document.getElementById(id);
}

