﻿String.prototype.trim=function(){return this.replace(/(\s*$)/g, ""); }
function webservice(url, callback, pars)
{
    $.ajax({
        data: pars,
        url: url,
        type: "POST",
        contentType: "application/json;utf-8",
        dataType: 'json',
        cache: false,
        success: function(json){
            callback(json.d);
        },
        error:function(xml,status){
            if(status=='error'){
                try{
                    var json = eval('(' + xml.responseText + ')');
                    alert(json.Message + '\n' + json.StackTrace);
                }catch(e){}
            }else{
                alert(status);
            }
        },
        beforeSend:function(xml){
            if(!pars)  xml.setRequestHeader("Content-Type", "application/json;utf-8");
        }
    });
}

    function logon(){
        var username = $("#username").val();
        var password = $("#password").val();
        if(username.trim()=='')
        {
            alert('用户名不能为空！');
            return;
        }
        
        if(password.trim()=='')
        {
            alert('密码不能为空！');
            return;
        }
        
        var susername = username.replace('\\','\\\\');//encodeURIComponent(username);
        password = encodeURIComponent(password);

        webservice("http://www.smartshe.com/WebService.asmx/UserLogin", function(obj1){logonCallBack(obj1,username)}, '{username:"' + susername + '",pwd:"' + password + '",expires:"43200",remme:"true"}');
    }
    
    function logonCallBack(obj,username){
        var isok = false;
        if(obj.Result!=null && obj.Result!='undefined')
        {
            isok = obj.Isok;
//alert(isok);
            if(!isok){
                var result = obj.Result;
                if(result=='用户名不存在')
                {
                    alert('您输入的用户名不存在，请检查后重新输入。');  
		    $("#username").val('');
		    $("#password").val('');                  
                }else{
		    alert('您输入的密码不正确，请检查后重新输入。');
		    $("#password").val('');
		}
            }else{
		//alert(username);
                $('#loginmsg').html('<li class="dq"><a href="http://www.smartshe.com/help.html" target="_blank">帮助</a></li><li class="dq"><a href="http://www.smartshe.com/bauer" target="_blank">鲍尔中国</a></li><li class="dq"><a href="http://www.smartshe.com/logout.aspx?act=u">退出</a></li><li class="dq"><a href="http://www.smartshe.com/my/my.aspx"target="_blank">个人中心</a></li><li class="dq">欢迎：' + username + '</li>');
            }
        }
    }

function logon2(){
        var username = $("#username").val();
        var password = $("#password").val();
        if(username.trim()=='')
        {
            alert('用户名不能为空！');
            return;
        }
        
        if(password.trim()=='')
        {
            alert('密码不能为空！');
            return;
        }
        
        var susername = username.replace('\\','\\\\');//encodeURIComponent(username);
        password = encodeURIComponent(password);

        webservice("http://www.smartshe.com/WebService.asmx/UserLogin", function(obj1){logonCallBack2(obj1,username)}, '{username:"' + susername + '",pwd:"' + password + '",expires:"43200",remme:"true"}');
    }
    
    function logonCallBack2(obj,username){
        var isok = false;
        if(obj.Result!=null && obj.Result!='undefined')
        {
            isok = obj.Isok;
//alert(isok);
            if(!isok){
                var result = obj.Result;
                if(result=='用户名不存在')
                {
                    alert('您输入的用户名不存在，请检查后重新输入。');  
		    $("#username").val('');
		    $("#password").val('');                  
                }else{
		    alert('您输入的密码不正确，请检查后重新输入。');
		    $("#password").val('');
		}
            }else{
		//alert(username);
                $('#loginmsg').html('<ul><li><a href="http://www.smartshe.com/my/my.aspx" target="_blank">个人中心</a></li><li>&nbsp;欢迎：' + username + '</li></ul>');
                $('#loginmsg2').html('<a href="http://www.smartshe.com/logout.aspx?act=u">退出</a> <a href="#" onClick="this.style.behavior=\'url(#default#homepage)\';this.setHomePage(\'http://www.smartshe.com/\');return(false);">设为首页</a>  <a href="http://www.smartshe.com/bauer/" target="_blank">鲍尔中国</a>  <a href="http://www.smartshe.com/help/help.html" target="_blank">帮助</a>');
            }
        }
    }