﻿var gXHRequest = null;
var gCurLawyer = "";

var token = "?lawyer=";
var url = document.URL;
var pos = url.lastIndexOf(token);
if(pos > 0)
{
    gCurLawyer = url.substr(pos+token.length);
}

function createXmlHttpRequest()
{
     if ( window.ActiveXObject ) // IE
    {
        var activeXo = ['MSXML2.XMLHTTP','Microsoft.XMLHTTP'] ;
        for ( var i = 0 ; i < activeXo.length ; i++ )
        try {return new ActiveXObject( activeXo[i] );}catch (e){};
    }
    else if ( window.XMLHttpRequest )    // Gecko
    {
        return new XMLHttpRequest() ;
    }
    else
    {
        throw "can not create XMLHttpRequest";
    }
}

function parseLawyerInfo(name,info)
{
    if(info =="")
    {
        document.getElementById("LAWYER_PIC").src="lawyer/"+gCurLawyer+".jpg";
        alert("没有此律师信息!");
        return;
    }
    
    var infos = info.split(/[\r\n]+={4,}/);
    //alert(infos[0].charAt(infos[0].length-1))
    if(typeof infos[0] == "string")
        document.getElementById("MINI_INFO").innerHTML="<pre>"+infos[0]+"</pre>";
    
    if(typeof infos[1] == "string")
        document.getElementById("FULL_INFO").innerHTML="<pre>"+infos[1]+"</pre>";
    
    if(gCurLawyer != "")
    {
        changeCss(document.getElementById("LAWYER_A_"+gCurLawyer),'lawyeractive');
        changeCss(document.getElementById("LAWYER_B_"+gCurLawyer),'lawyeractive');
    }
    changeCss(document.getElementById("LAWYER_A_"+name),'lawyerselect');
    changeCss(document.getElementById("LAWYER_B_"+name),'lawyerselect');
    gCurLawyer = name;
}

function showLawyerInfo(name)
{
    if(name == null || name == "") return;
    
    document.getElementById("LAWYER_PIC").src="lawyer/"+name+".jpg";
    
    if(gXHRequest == null)
    {
        gXHRequest = createXmlHttpRequest();
    }
    
    gXHRequest.onreadystatechange = function()
    {
        if ( gXHRequest.readyState == 4 )
        {
            if (gXHRequest.status == 0 || gXHRequest.status == 200 || gXHRequest.status == 304 )
            {
                resText = gXHRequest.responseText==null?"":gXHRequest.responseText;
                gXHRequest.abort(); // 在iE里必须设置,否则readyState在下次使用的时候永远是4
                parseLawyerInfo(name,resText);
            }
            else
            {
                throw "request failed";
            }
        }
    }
    gXHRequest.open("GET", "lawyer/"+name+".txt",true);
    gXHRequest.send(null);
    
}
