var loginForm = function(formId) {
    this.formId = "#" + formId;
    this.action = '/visitor/ajaxLogin';

    var options = {
        url:        this.action,
        id: this.formId,
        beforeSerialize:   function(f, o) {
            if (f.hasClass('submitted')) {
                return false;
            }
            f.addClass('submitted'); return true;
        },
        success:    function(response) {
            if (response) {
                alert(response);
                $(this.id).removeClass('submitted');
            } else {
                document.location = '/visitor/home/';
            }
        }
    };
    $(this.formId).ajaxForm(options);
}

var lostPassForm = function(formId) {
    this.formId = "#" + formId;
    this.action = '/visitor/ajaxLostPass';

    var options = {
        url:        this.action,
        id: this.formId,
        beforeSerialize:   function(f, o) {
            if (f.hasClass('submitted')) {
                return false;
            }
            f.addClass('submitted'); return true;
        },
        success:    function(response) {
            if (response) {
                alert(response)
            }
            $(this.id).removeClass('submitted');
        }
    };
    $(this.formId).ajaxForm(options);
}

var joinForm = function(formId) {
    this.formId = "#" + formId;

    $(this.formId).action = this.actionStep1 = '/visitor/ajaxJoin1';
    var options = {
        url:        this.actionStep1,
        id: this.formId,
        beforeSerialize:   function(f, o) {
            if (f.hasClass('submitted')) {
                return false;
            }
            f.addClass('submitted'); return true;
        },
        success:    function(response, status) {
            if (response) {
                if ('{'==response[0]) {
                    respo = eval('(' + response + ')');

                    var date = new Date();
                    date.setTime(date.getTime() + (60*60*24*30));

                    $.cookie("username", respo.login, { path: '/', host: respo.domain, expires: date });
                    $.cookie("justlogged", 1, { path: '/', host: respo.domain, expires: date });
                    $.cookie("joined", "1", { path: '/', host: respo.domain, expires: date });

                    document.location = '/visitor/join2';

                } else if('go_inside' == response) {
					document.location = '/visitor/join1';
				} else {
	                alert(response);
                    $(this.id).removeClass('submitted');
				}
            }
        }
    };
    $(this.formId).ajaxForm(options);
}

var bulkForm = function(formId) {
    this.formId = "#" + formId;

    var options = {
        url:        '/visitor/sendPlatinumBulk',
        id: this.formId,
        beforeSerialize:   function(f, o) {
            if ("" == $("#msg").val()) {
                alert('Mesajınız giriniz');
                return false;
            }
            if (f.hasClass('submitted')) {
                return false;
            }
            f.addClass('submitted'); return true;
        },
        success:    function(response, status) {
            if ('ok' == response) {
                $(this.id).removeClass('submitted');
                var bouncesLeft = $("#bulksCount").html();
                bouncesLeft--;
                $("#bulksCount").html(1 > bouncesLeft ? 0 : bouncesLeft);
                if (bouncesLeft < 1) {
                    $("#bulkBox").hide();
                    $("#bulkForm").hide();
                    $("#noBulksBox").show();
                }
                alert('Mesajınız sıraya alınmıştır. Operatörlerimiz tarafından gözden geçirilip 1 gün içinde gönderilecektir');
                document.location = document.location;
            } else {
                alert(response);
                $(this.id).removeClass('submitted');
            }
        }
    };
    $(this.formId).ajaxForm(options);
}
