﻿document.write('<link href="http://www.byecity.com/fn/css/loginNew.css" rel="stylesheet" type="text/css" />');
document.write('<scr' + 'ipt src="http://www.byecity.com/fn/js/ajaxpro.js"></scr' + 'ipt>');

var JsLogin = {
    loginDivObj: null,
    loginLinkObj: null,
    errorSpanObj: null,
    guestLoginLinkObj: null,
    userNameObj: null,
    passwordObj: null,
    loginCloseObj: null,
    loginClick: function() { },
    loginHandlerUrl: "method.ashx",
    ie: navigator.userAgent.toLowerCase().indexOf('msie') > -1,
    ie6: navigator.userAgent.toLowerCase().indexOf('msie 7') > -1 ? false : navigator.userAgent.toLowerCase().indexOf('msie 6') > -1,
    ie7: navigator.userAgent.toLowerCase().indexOf('msie 7') > -1,
    firefox: navigator.userAgent.toLowerCase().indexOf('firefox') > -1,
    initLoginDiv: function() {
        JsLogin.createLoginDiv();
        JsLogin.loginLinkObj = getObj("logindiv_loginLink");
        JsLogin.errorSpanObj = getObj("logindiv_loginError");
        JsLogin.guestLoginLinkObj = getObj("logindiv_guestLoginLink");
        JsLogin.userNameObj = getObj("logindiv_userName");
        JsLogin.passwordObj = getObj("logindiv_password");
        JsLogin.loginCloseObj = getObj("logindiv_close");
        JsLogin.userNameObj.onkeydown = function(e) {
            if (e)
                event = e;
            if (event.keyCode == 13 && this.value.length > 0 && document.activeElement.id == "logindiv_userName")
                JsLogin.passwordObj.focus();
        }
        JsLogin.userNameObj.onfocus = function() {
            if (this.value == "手机号/ 邮箱") {
                this.value = "";
            }
        }
        JsLogin.userNameObj.onblur = function() {
            if (this.value == "") {
                this.value = "手机号/ 邮箱";
            }
        }
        JsLogin.passwordObj.onkeydown = function(e) {
            if (e)
                event = e;
            if (event.keyCode == 13 && this.value.length > 0 && document.activeElement.id == "logindiv_password")
                JsLogin.loginClick();
        }
        JsLogin.loginCloseObj.onclick = function() {
            JsLogin.loginDivObj.hide();
        }
    },
    createLoginDiv: function() {
        if (document.getElementById("logindiv") != null) {
            JsLogin.loginDivObj = document.getElementById("logindiv");
        }
        else {
            JsLogin.loginDivObj = document.createElement("div");
            JsLogin.loginDivObj.id = 'logindiv';
            JsLogin.loginDivObj.innerHTML = '<div class="bar"> \
                    <img id="logindiv_close" src="http://user.byecity.com/images/logindiv_shut.gif" style="cursor:pointer;" /> \
                    <h1>登录提示</h1> \
                    </div> \
                    <div class="cnt"> \
                        <div class="member"> \
                            <h2>佰程会员预订</h2> \
                            <div class="loginform"> \
                                <label>用户名：</label><input type="text" id=logindiv_userName value="手机号/ 邮箱"/><span id=logindiv_loginError style="color:red;"></span><br />\
                                <label>密&nbsp;&nbsp;码：</label><input type=password id=logindiv_password type="text"  style="width:126px;" />\
                            </div> \
                            <a id=logindiv_loginLink href="javascript:;" class="btn"><span>登录并继续</span></a><a href="http://user.byecity.com/fgtpwd.aspx" target=_blank>忘记密码</a> \
                        </div>\
                        <div class="notmember"> \
                        <h2>还不是会员？</h2> \
                        <p>&nbsp<br /><strong>注册佰程会员即能享受：</strong><br />\
                            免费注册，不收取会员费！<br /> \
                            享受高品质的会员服务！</p>\
                            <a  href="http://user.byecity.com/useradd.aspx?url=' + encodeURI(document.location.toString()) + '"  target="_blank" class="btn"><span>注册</span></a>\
                            <a id=logindiv_guestLoginLink />\
                        </div>\
                    </div>';

            document.body.appendChild(JsLogin.loginDivObj);
        }
        var pos = JsLogin.center(JsLogin.loginDivObj)

        with (JsLogin.loginDivObj.style) {
            display = "none";
            position = JsLogin.ie6 ? "absolute" : "fixed";
            zIndex = 1000;
            top = pos.top + "px";
            left = pos.left + "px";
        }
        JsLogin.loginDivObj.show = function() {
            this.style.display = "";
        }
        JsLogin.loginDivObj.hide = function() {
            this.style.display = "none";
            JsLogin.userNameObj.value = '';
            JsLogin.passwordObj.value = '';
            JsLogin.errorSpanObj.innerHTML = '';
        }
        return JsLogin.loginDivObj;
    },
    checkBook: function(__url, isdirect, func) {
        var userinfo = Cookie.Get("BtUser");

        if (userinfo != null)
            return true;

        JsLogin.loginClick = JsLogin.loginLinkObj.onclick = function() {
            if (JsLogin.userNameObj.value == "手机号/ 邮箱")
                JsLogin.userNameObj.value = "";

            if (JsLogin.userNameObj.value == "") {
                JsLogin.errorSpanObj.innerHTML = "请输入用户名";
                JsLogin.userNameObj.focus();
                return false;
            }
            if (JsLogin.passwordObj.value == "") {
                JsLogin.errorSpanObj.innerHTML = "请输入密码";
                JsLogin.passwordObj.focus();
                return false;
            }
            JsLogin.errorSpanObj.innerHTML = "<span style='color:#808080'><img src='http://www.byecity.com/fn/images/noc.gif'>正在登录...</span>";

            var url = JsLogin.loginHandlerUrl + "?method=login2&t=" + Math.random();
            var data = "u=" + escape(JsLogin.userNameObj.value) + "&p=" + escape(JsLogin.passwordObj.value);

            AjaxPro.PostData(url, data, function(response) {
                //response = "0:错误"
                //response = "1,ID"
                var flag = response.charAt(0);
                if (flag == "0" || flag == "1") {
                    if (flag == "1") {
                        JsLogin.errorSpanObj.innerHTML = "";
                        if (func) {
                            //var id = response.sub":"));
		var id = response.substr(2, response.lastIndexOf(":"));
                            func(id, JsLogin.userNameObj.value, response);
                        }
                        if (__url && __url.length > 0) {
                            if (isdirect)
                                document.location.href = __url;
                            else {
                                open(__url);
                                JsLogin.loginDivObj.hide();
                            }
                        }
                        else
                            JsLogin.loginDivObj.hide();
                    }
                    else
                        JsLogin.errorSpanObj.innerHTML = "登录名或密码错误";
                }
                else
                    JsLogin.errorSpanObj.innerHTML = "登录失败:[" + response + "]";
            }, function(err) {
                JsLogin.errorSpanObj.innerHTML = "登录失败:[" + err + "]";
            });
        }
        JsLogin.guestLoginLinkObj.onclick = function() {
            var url = JsLogin.loginHandlerUrl + "?method=guestlogin&t=" + Math.random();
            //open(url)
            AjaxPro.Request(url, function(rsp) {
                if (func)
                    func(-1, '', '');
                if (__url && __url.length > 0) {
                    if (isdirect)
                        document.location.href = __url;
                    else {
                        open(__url);
                        JsLogin.loginDivObj.hide();
                    }
                }
                else
                    JsLogin.loginDivObj.hide();
            });
        }
        JsLogin.loginDivObj.show();
        try {
            JsLogin.userNameObj.focus();
        }
        catch (ex)
        { }
        return false;
    },
    avail: function() {//浏览器宽高
        var winWidth = 0, winHeight = 0;
        if (window.innerWidth)//for ie
        {
            winWidth = window.innerWidth;
            winHeight = window.innerHeight;
        }
        else ((document.body) && (document.body.clientWidth))
        {
            winWidth = document.body.clientWidth;
            winHeight = document.body.clientHeight;
        }

        //通过深入Document内部对body进行检测，获取窗口大小
        if (document.documentElement && document.documentElement.clientWidth) {
            winWidth = document.documentElement.clientWidth;
            winHeight = document.documentElement.clientHeight;
        }
        var _avail = {};
        _avail.width = winWidth;
        _avail.height = winHeight;
        return _avail;
    },
    center: function(obj) {//浏览器中心点，指供obj时，返回能够使obj居中的top和left
        var avail = this.avail();
        var loc = {};
        if (typeof obj == 'undefined' || obj == null) {
            loc.left = avail.width / 2;
            loc.top = avail.height / 2;
        }
        else if (typeof obj == 'object') {
            loc.left = (avail.width - obj.offsetWidth) / 2;
            loc.top = (avail.height - obj.offsetHeight) / 2;
        }
        else if (typeof obj == 'string') {
            obj = fn.idObj(obj);
            if (obj == null) {
                loc.left = avail.width / 2;
                loc.top = avail.height / 2;
            }
            else {
                loc.left = (avail.width - obj.offsetWidth) / 2;
                loc.top = (avail.height - obj.offsetHeight) / 2;
            }
        }
        return loc;
    },
    domReady: function(func) {
        if (document.addEventListener)
            document.addEventListener("DOMContentLoaded", func, false);

        if (document.attachEvent)
            document.onreadystatechange = function() {
                if (window.document.readyState == "complete") {
                    func();
                }
            }
    }
}

//if (window.addEventListener) 
//    window.addEventListener('load', JsLogin.initLoginDiv, false);
//else if (window.attachEvent)
//    window.attachEvent('onload', JsLogin.initLoginDiv);

JsLogin.domReady(function() {//alert();
    if (document.getElementById("logindiv") == null)
        JsLogin.initLoginDiv();
});
