include("ajax.js");
include("password.js");

var Registration = function(caller){
    var progressBar = "#loadingbar";
	this.ajax = new Ajax("module", "Registration", progressBar, caller);
}

Registration.prototype.checkForm = function(form, mode){
    this.ajax.serializeForm(form);

    this.ajax.appendVar("mode", mode);

	return this.ajax.request("check",
        function(response){
            regInit();
            if (response == 1){                
                $(form).attr("method", "post").submit();
            }
            else {
                var tmp = $.evalJSON(response);
                $(".reg_error").html("");
                $("#"+tmp.name+"_span").html(tmp.message);
            }
        }
    );
}

Registration.prototype.updateCaptcha = function(captcha, mode){
    this.ajax.appendVar("mode", mode);

	return this.ajax.request("update",
        function(response){            
            $(captcha).attr("src",response);
            regInit();
        }
    );
}

Registration.prototype.signIn = function(mode){
    this.ajax.appendVar("mode", mode);

    this.ajax.appendVar("login", $("#login").val());
    this.ajax.appendVar("pass", $("#pass").val());

	return this.ajax.request("signIn",
        function(response){
            regInit();
            if (response == 1){                
                window.location.reload();
            }
            else {
                $(".mess_p .error").html(response);
            }
        }
    );
}

Registration.prototype.remind = function(mode){
    this.ajax.appendVar("mode", mode);

    this.ajax.appendVar("email", $("#email").val());

	return this.ajax.request("remind",
        function(response){
            if (response){
                $(".remind .error").html(response);
                regInit();
            }
        }
    );
}

Registration.prototype.logout = function(mode){
    this.ajax.appendVar("mode", mode);

	return this.ajax.request("logout",
        function(response){
            if (response == 1){
                window.location.reload();
            }
        }
    );
}

function regInit(){
    $("#regMe").unbind();
    $("#register input").unbind();
    $("#captcha").unbind();
    $("#signIn").unbind();
    $("#auth_login1 input").unbind();
    $("#remind").unbind();
    $("#auth_login2 input").unbind();
    $("#logout").unbind();

    $("#regMe").click(function(){
        var registration = new Registration(this);
        registration.checkForm("#register", "1");
    });
    $("#register input").keypress(function(e){
        if(e.which == 13){
            var registration = new Registration(this);
            registration.checkForm("#register", "1");
        }
    });
    $("#captcha").click(function(){
        $(this).unbind();
        var registration = new Registration(this);
        registration.updateCaptcha("#captcha", "99");
    });
    $("#signIn").click(function(){
        var registration = new Registration(this);
        registration.signIn("2");
    });
    $("#auth_login1 input").keypress(function(e){
        if(e.which == 13){
            var registration = new Registration(this);
            registration.signIn("2");
        }
    });
    $("#remind").click(function(){
        var registration = new Registration(this);
        registration.remind("2");
    });
    $("#auth_login2 input").keypress(function(e){
        if(e.which == 13){
            var registration = new Registration(this);
            registration.remind("2");
        }
    });
    $("#logout").click(function(){
        var registration = new Registration(this);
        registration.logout("2");
    });
}

$(document).ready(
	function(){
		regInit();
	}
);

/*
 * Использовалось с плагином Форм.
 *
var Registration = function(){
    var progressBar = "#loadingbar";
	this.ajax = new Ajax("plugin", "Form", progressBar);
}

Registration.prototype.approve = function (module, currForm, template, customId){
	var formId = "#" + "module" + module + "form" + currForm;
	var errorDiv = "#" + "module" + module + "form" + currForm + "error";

    this.ajax.serializeForm(formId);

    this.ajax.appendParam("form", currForm);
    this.ajax.appendParam("module", module);
    this.ajax.appendParam("template", template);
    this.ajax.appendParam("customid", customId);
    var error = 0;
    $(".warn[formc='"+currForm+"']").each(function(){
        if($(this).attr("chk") == "0"){
            //$("#onerror").html("Не заполнены обязательные поля.");
            alert("Не заполнены обязательные поля.");
            error = 1;
        }
    })

    if (error == 0){
        var result = this.ajax.request("approve", null,
            function(message){
                $("#onerror").html(message);
            }
        );
        if(result){
            $(errorDiv).html("");
            $(formId).html(result);
            $(formId + " form").submit();
        }
    }
}

Registration.prototype.check = function (module, currForm, template, typeInput, val, nameInput){
    this.ajax.appendVar("typeInput",typeInput);
    this.ajax.appendVar("valueInput",Base64.encode(val));
    this.ajax.appendVar("nameInput",nameInput);
    if (nameInput == "rq_data_repassword"){
        this.ajax.appendVar("revalueInput",Base64.encode($("#valpassword"+currForm).val()));
    }

    this.ajax.appendParam("form", currForm);
    this.ajax.appendParam("module", module);
    this.ajax.appendParam("template", template)

    var alImg = ".al";
    var currentImg = "#"+nameInput + currForm;

	this.ajax.request("check", 
        function(response){
            if(response){
                $(currentImg).removeClass("data_wrong");
                $(currentImg).addClass("data_right");
                $("input[name='"+nameInput+"']").attr("chk","1");
            }
            else {
                $(currentImg).removeClass("data_right");
                $(currentImg).addClass("data_wrong");
                $("input[name='"+nameInput+"']").attr("chk","0");
            }            
        }
    );
}

function regInit(){
	//$(".warn").unbind();

	$(".warn").keyup(function(){
		var module = "Users";
        var forma = $(this).attr("rel");
        var tpl = "reg_customer";
        var typeInput = $(this).attr("type");
        var val = $(this).val();
        var nameInput = $(this).attr("name");
		var frm = new Registration();
		frm.check(module, forma, tpl, typeInput, val, nameInput);
		//regInit();
	});

	$(".approveForm").click(function(){
		var module = "Users";
        var forma = $(this).attr("rel");
        var tpl = "reg_customer";
        var customId = $(this).attr("group");
		var frm = new Registration();
		frm.approve(module, forma, tpl, customId);
	});
}

$(document).ready(
	function(){
		regInit();
	}
);
*/
