/**
 * @author screamge
 */
var Login = function (){
	this.http = this.getHTTPObject ();
}

Login.prototype = {
	getHTTPObject: function(){
	
		if (window.XMLHttpRequest) {
			var xmlhttp = new XMLHttpRequest();
		}
		else 
			if (window.ActiveXObject) {
				var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
		return xmlhttp;
	},
	
	
	login: function (lang){
		this.lang = lang;
		this.bool = false;
		this.mailcheck = false;
		this.mailcorrect = false;
		this.passlength = false;
		
		this.inputs = document.getElementsByTagName('input');
		
		for (var k = 0; k < this.inputs.length; k++) {
			if (this.inputs[k].value == '' && this.bool == false){
				this.bool = true;
			}
		}
		
		document.getElementById ('error_message').innerHTML = '';
		
		if (this.bool == true){
			this.getfrase ('reg_notall');
		} else {
			var emailRe = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*\.(\w{2}|(com|net|org|edu|int|gov|biz|ru|name|ge|info|pro|museum))$/;
			
			if (!emailRe.test(this.inputs['email'].value)){
				this.getfrase ('reg_notmail');
			} else {
				this.mailcorrect = true;
			}
			
			
			if (this.inputs['pass'].value.length < 6){
				this.getfrase ('reg_passlength');
			} else {
				this.passlength = true;
			}
			
			
			if (this.mailcorrect == true && this.passlength == true){
				this.log ();
			}
			
		}
		
	},
	
	
	log: function (){
		this.param = "email=" + this.inputs['email'].value + "&pass=" + this.inputs['pass'].value + '&lang=' + this.lang;
		
		this.http.open("POST", 'ajax.login.php' , true);
		
		this.http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		this.http.setRequestHeader("Content-length", this.param.length);
		this.http.setRequestHeader("Connection", "close");
		
 		this.http.onreadystatechange = this.delegate (this, this.onlogas);
  		this.http.send(this.param);
	},
	
	
	onlogas: function (){
		if (this.http.readyState == 4) {
			var txt = this.http.responseText;
			if (txt == -1){
				this.getfrase ('log_notfound');
			} else {
				document.getElementById ('log_form').width = '100%';
				document.getElementById ('log_form').innerHTML = "<td><blockquote><br><span class='finish'>" + this.http.responseText + '</span></blockquote></td>';
			}
			
			
		}
	},
	
	
	getfrase: function (name) {
		var mes = 'ajax.getfrase.php?name=' + name + '&lang=' + this.lang;
		this.http.open("GET", mes, true);
		this.http.onreadystatechange = this.delegate (this, this.onloadform);
		this.http.send(null);
	}, 
	
	
	onloadform: function (){
		if (this.http.readyState == 4) {
			var text = this.http.responseText;
			var field = document.getElementById ('error_message').innerHTML;

			if (field.indexOf (text) == '-1') {
				document.getElementById ('error_message').innerHTML += '<br>* ' + text + '<br>';
			}
			
		}
	}, 
	
	
	
	delegate: function (obj, method ) {
    return function() { return method.call(obj); }
  	}
}
