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

Register.prototype = {
	getHTTPObject: function(){
	
		if (window.XMLHttpRequest) {
			var xmlhttp = new XMLHttpRequest();
		}
		else 
			if (window.ActiveXObject) {
				var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
		return xmlhttp;
	},
	
	
	register: function (lang, type){
		this.lang = lang;
		this.bool = false;
		this.mailcheck = false;
		this.mailcorrect = false;
		this.passcorrect = 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 != this.inputs['pass2'].value){
				this.getfrase ('reg_notpass');
			} else {
				this.passcorrect = true;
			}
			
			if (this.inputs['pass'].value.length < 6){
				this.getfrase ('reg_passlength');
			} else {
				this.passlength = true;
			}
			
			
			if (this.mailcorrect == true && this.passcorrect && this.passlength == true){
				this.check_mail (this.inputs['email'].value);
			}
		}
		
	},
	
	
	regas: function (){
		this.param = "name=" + this.inputs['name'].value + "&surname=" + this.inputs['surname'].value;
		this.param += "&email=" + this.inputs['email'].value;
		this.param += "&pass=" + this.inputs['pass'].value;
		this.param += "&company=" + this.inputs['company'].value;
		this.param += "&prof=" + this.inputs['prof'].value;
		this.param += "&phone=" + this.inputs['phone'].value;
		
		var save_url = "ajax.register.php";
		this.http.open("POST", save_url , 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.onregas);
  		this.http.send(this.param);
	},
	
	
	onregas: function (){
		if (this.http.readyState == 4) {
			if (this.http.responseText == -1){
				this.getfrase ('reg_error');
			} else if (this.http.responseText == -2){
			    this.getfrase ('reg_already');	
			}else {
				this.finish_frase ();
			}
		}
	},

	
	check_mail: function (nam){
		var param = "mail=" + nam;
		var save_url = "ajax.check_mail.php";
		this.http.open("POST", save_url , true);
		
		this.http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		this.http.setRequestHeader("Content-length", param.length);
		this.http.setRequestHeader("Connection", "close");
		
 		this.http.onreadystatechange = this.delegate (this, this.onmailcheck);
  		this.http.send(param);
	},
	
	onmailcheck: function (){
		if (this.http.readyState == 4) {
			if (this.http.responseText == 1){
				this.getfrase ('reg_mailis');
			} else {
				this.regas ();
			}
		}
	},
	
	
	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 += '* ' + text + '<br>';
			}
			
		}
	}, 
	
	finish_frase: function (){
		var mes = 'ajax.getfrase.php?name=reg_end&lang=' + this.lang;
		this.http.open("GET", mes, true);
		this.http.onreadystatechange = this.delegate (this, this.onfinish);
		this.http.send(null);
	},
	
	onfinish: function (){
		if (this.http.readyState == 4) {
			document.getElementById ('register_form').innerHTML = "<span class='finish'>" + this.http.responseText + '</span>';
		}
	},
	
	
	delegate: function (obj, method ) {
    return function() { return method.call(obj); }
  	}
}
