﻿var webapi_urls = {
    addfav: "/webapi/addfav",
    checkfav: "/webapi/checkfav",
    get_local_papers: "/webapi/local_papers",
    search_paper: "/webapi/search_paper",
    manage_paper: "/webapi/manage_paper",
    record_paper: "/webapi/record_paper"
};

function Ajax() {
    var xmlHttpReq = null;
    if (window.XMLHttpRequest) {
        xmlHttpReq = new XMLHttpRequest();
    } else {
        if (window.ActiveXObject) {
            var versions = ['Microsoft.XMLHTTP', 'MSXML.XMLHTTP', 'Microsoft.XMLHTTP',
 'Msxml2.XMLHTTP.7.0', 'Msxml2.XMLHTTP.6.0',
 'Msxml2.XMLHTTP.5.0', 'Msxml2.XMLHTTP.4.0',
 'MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP'];
            for (var i = 0; i < versions.length; i++) {
                try {
                    xmlHttpReq = new ActiveXObject(versions[i]);
                    if (xmlHttpReq) {
                        break;
                    }
                } catch (e) { }
            }
        }
    }

    var handler = null;

    this.invoke = function(mode, url, value, _handler) {
        handler = _handler;
        if (mode == 'get') {
            var querystring = url + '?' + value + '&t=' + Math.random();
            if (window.XMLHttpRequest) {
                xmlHttpReq.open('GET', querystring, true);
                xmlHttpReq.onreadystatechange = this.callback;
                xmlHttpReq.send(null);
            } else {
                xmlHttpReq.open('GET', querystring, true);
                xmlHttpReq.onreadystatechange = this.callback;
                xmlHttpReq.send();
            }
        }
        else if (mode == 'post') {
            xmlHttpReq.open('POST', url);
            xmlHttpReq.onreadystatechange = this.callback;
            xmlHttpReq.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
            xmlHttpReq.send(value);
        }
    };

    this.callback = function() {
        if (xmlHttpReq.readyState == 4) {
            if (xmlHttpReq.status == 200) {
                handler(xmlHttpReq.responseText);
            } else {
                //alert("请刷新页面！");
            }
        }
    };
}

function check_login() {
    if (document.getElementById("checked_login") != null && document.getElementById("checked_login").value=="1"){
        return false;
    }
    new Ajax().invoke(
  "get",
  "/api/webapi.aspx",
  'action=check_login',
   function (response) {
       json_data = eval("(" + response + ")");
       if (json_data.UserId != null) {
           document.getElementById("login_bar_user_name").innerHTML = "欢迎您！" + json_data.UserName;
           document.getElementById("nologin_bar").style.display = "none";
           document.getElementById("login_bar").style.display = "block";
           if (window['abbao_login_callback'] != undefined && abbao_login_callback != undefined && abbao_login_callback != null) {
               abbao_login_callback(json_data.user);
           }
       } else {
           document.getElementById("nologin_bar").style.display = "block";
           document.getElementById("login_bar").style.display = "none";
       }
   }
    );
}

function ajax_login(userCode, password, isRem) {
    if (userCode == null || userCode==''||password == null || password==''){
        alert("用户名、密码不能为空！");
        return false;
    }
    var reg = /^\d+(\.\d+)?$/;
	if (!reg.test(isRem)){
		alert("参数不正确");
		return false;
	}

    var value = "action=login" + "&userCode=" + userCode + "&password=" + password + "&isRem=" + isRem;
    new Ajax().invoke(
  "get",
  "/api/webapi.aspx",
  value,
   function (response) {
       json_data = eval("(" + response + ")");
       if (json_data.result != 1 && json_data.result != 0) {
           if (json_data.result == 5) {
               alert("帐号已经被锁定,登录失败.");
               return false;
           } else if (json_data.result == -1) {
               alert("你尝试登录次数过多,请稍候再试.");
               return false;
           } else {
               alert("用户名或者密码不正确,登录失败.");
               return false;
           }
       }
       if (json_data.user.UserId != null) {
           document.getElementById("login_bar_user_name").innerHTML = "欢迎您！" + json_data.user.UserName;
           document.getElementById("nologin_bar").style.display = "none";
           document.getElementById("login_bar").style.display = "block";

           if (window['abbao_login_callback'] != undefined && abbao_login_callback != undefined && abbao_login_callback != null) {
               abbao_login_callback(json_data.user);
           }
       } else {
           document.getElementById("nologin_bar").style.display = "block";
           document.getElementById("login_bar").style.display = "none";
       }
     }
  );
}

function Logout() {
    window.location.href = "/account/logout.aspx";
}
