﻿var directory_host="http://www.tcdcconnect.com";
var member_login = {
    isLogin: false,
    lang: "th",
    el: {
        form: null,
        login: null,
        nav_log: null,
        login_cen: null,
        login_bot: null,
        btn_login: null,
        btn_logout: null,
        welcome: null,
        username: null,
        password: null,
        validator: null
    },
    init: function() {
        var el = this.el;
        el.form = $("#frm-login");
        el.form.submit(function() {
            return false;
        });
        el.nav_log = $("#nav-log");
        el.login = $("#login");
        el.login_bot = $("#login-bot");
        el.login_cen = $("#login-cen");
        el.welcome = $(".welcome");
        el.btn_login = $("#btn-login", el.form);
        el.btn_logout = $("#panel2m");
        el.username = $("#username");
        el.password = $("#password");
    },
    initValidator: function() {
        var _this = this;
        var requireMsg = "*";
        var form = this.el.form;

        this.el.validator = form.validate({
            rules: {
                username: {
                    required: true
                },
                password: {
                    required: true
                }
            },
            messages: {
                username: {
                    required: "กรุณากรอกชื่อผู้ใช้"
                },
                password: {
                    required: "กรุณากรอกรหัสผ่าน"
                }
            },
            errorPlacement: function(error, element) {

            },
            success: function(label) {
            }
        });
    },
    initEvents: function() {
        var _this = this;
        _this.el.password.keydown(function(e) {
            if (e.keyCode == 13)
                _this.doLogin();
        });
        _this.el.btn_login.click(function() {
            _this.doLogin();
        });
        _this.el.btn_logout.click(function() {
            _this.doLogout();
        });

        _this.el.username.keyup(function(event) {
            if (event.keyCode == '13') {
                _this.doLogin();
            }
        });

        _this.el.password.keyup(function(event) {
            if (event.keyCode == '13') {
                _this.doLogin();
            }
        });

    },
    qtipValidate: function(e, message) {

        // Destroy currrent tooltip if present
        if (e.data("qtip")) e.qtip("destroy");

        e.qtip({
            content: message, // Set the tooltip content to the current corner
            position: {
                corner: {
                    tooltip: 'bottomMiddle', // Use the corner...
                    target: 'topMiddle' // ...and opposite corner
                }
            },
            show: {
                when: false, // Don't specify a show event
                ready: true, // Show the tooltip when ready
                effect: {
                    type: 'fade',
                    length: 700
                }
            },
            hide: { when: { event: 'keyup'} },
            style: {
                border: {
                    width: 5,
                    radius: 5
                },
                padding: 7,
                //textAlign: 'center',
                tip: true, // Give it a speech bubble tip with automatic corner detection
                name: 'light' // Style it according to the preset 'cream' style
            },
            api: {
                onShow: function() {
                    setTimeout(function() {
                        if (e.data("qtip")) e.qtip("destroy");
                    }, 3000);
                }
            }
        });
    },
    doLogin: function() {
        var _this = this;
        var login_url = directory_host + "/tcdc/member/login.rhtml";
        if (this.el.validator.form()) {
            $.get(login_url,
            {
                username: this.el.username.val(),
                password: this.el.password.val()
            }
            , function(res) {
                if (res.success) {
                    //_this.checkLogin();
                    window.location.reload(true);
                } else {
                    _this.qtipValidate(_this.el.username, "ชื่อผู้ใช้/รหัสผ่านไม่ถูกต้อง กรุณาตรวจสอบ");
                }
            }, 'jsonp');
        } else {
            this.el.validator.focusInvalid();
            var erList = this.el.validator.errorList;
            if (erList.length > 0) {
                var el = erList[0].element;
                var msg = erList[0].message;

                this.qtipValidate($(el), msg);
            }
        }
    },
    doLogout: function() {
        var _this = this;
        var logout_url = directory_host + "/tcdc/member/logout.rhtml";
        $.get(logout_url
            , null
            , function(res) {
                window.location.href = directory_host + "/" + _this.lang + "/directory.rhtml";
                //_this.checkLogin();
            }
            , 'jsonp');
    },
    checkLogin: function() {
        var _this = this;
        var _url = directory_host + "/tcdc/member/checklogin.rhtml";
        $.get(_url
            , null
            , function(res) {
                _this.lang = res.lang;
                _this.isLogin = res.login;
                $("a.link-register").attr("href", directory_host + "/" + res.lang + "/pre-register.rhtml");
                if (res.login) {
                    $("#login,#login-bot,#login-cen").hide();
                    var displayName = res.data["DisplayName" + res.lang.toUpperCase()];
                    var titleName = res.data["TitleName" + res.lang.toUpperCase()];
                    
                    $(".welcome-name").html(
						(displayName && $.trim(displayName) != "")
						? titleName + " " + displayName
						: res.data.Username
					);
                    $("#nav-welcome #panel1m a").attr("href", directory_host + "/" + res.lang + "/" + ((res.data.MemberType == "individual") ? "individual" : "organize") + "-profile.rhtml?memberId=" + res.data.Id);
                    $(".welcome").show();
                } else {
                    $("#login,#login-bot").show();
                    $(".welcome").hide();
                }
            }
            , 'jsonp');
    }
};
$(function() {
    member_login.init();
    member_login.initValidator();
    member_login.initEvents();
	member_login.checkLogin();
});

/*Forgot Password*/

var $forgot_cancel=function(a){
    $.facebox.close();
};
var $forgot_ok=function(a){
   var qtip = function(e,message){
        if (e.data("qtip")) e.qtip("destroy");
        e.qtip({
            content: message, // Set the tooltip content to the current corner
            position: {
                corner: {
                    tooltip: 'leftMiddle', // Use the corner...
                    target: 'rightMiddle' // ...and opposite corner
                }
            },
            show: {
                when: false, // Don't specify a show event
                ready: true, // Show the tooltip when ready
                effect: {
                    type: 'fade',
                    length: 700
                }
            },
            hide: { when: { event: 'keyup'} },
            style: {
                border: {
                    width: 5,
                    radius: 5
                },
                padding: 7,
                //textAlign: 'center',
                tip: true, // Give it a speech bubble tip with automatic corner detection
                name: 'light' // Style it according to the preset 'cream' style
            },
            api: {
                onShow: function() {
                    setTimeout(function() {
                        if (e.data("qtip")) e.qtip("destroy");
                    }, 3000);
                }
            }
        }); 
    };
    var v_require = function(el,msg){
        var val=el.val();
        var b=($.trim(val).length>0);
        if(!b) qtip(el,msg);
        return b;
    };
    var v_email = function(el,msg){
        var val=el.val();
        var isemail=function(value){return /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i.test(value);};
        var b=isemail(val);
        if(!b) qtip(el,msg);
        return b;
    };
    var ct=$("#facebox");
    if(
        v_email($("input[name=email]",ct),"กรุณากรอกอีเมล์ให้ถูกต้อง")
    ){
        $("a.btn-ok",ct).attr("disbled",true);
        $.get(directory_host+"/tcdc/member/forgotpassword.rhtml",{
            email:$("input[name=email]",ct).val()
        },function(res){
            $("a.btn-ok",ct).attr("disbled",false);
            if(res.success===true){
                qtip($("a.btn-ok",ct),"ส่งข้อมูลสำเร็จ");
                setTimeout(function(){
                    $.facebox.close();
                },3000);
            }else{
                qtip($("input[name=email]",ct),res.message);
            }
        },"jsonp");
    }
};
$(function(){
    $(".forgot-password").facebox();
});
