//处理浏览器，用来选择不同的XML读取函数
var agt = navigator.userAgent.toLowerCase();
var is_opera = (agt.indexOf("opera") != -1);
var is_ie = (agt.indexOf("msie") != -1) && document.all && !is_opera;
var is_ie5 = (agt.indexOf("msie 5") != -1) && document.all;
var uniqnum_counter = (new Date).getTime();
var is_regexp = (window.RegExp) ? true : false;
var xmlhttp = null;

function UniqueNum() {  
	++uniqnum_counter;  
	return uniqnum_counter;
}
//创建Get请求
function StartGETRequest(url, handler)
{
	xmlhttp = null;
	if (is_ie) {    
		var control = (is_ie5) ? "Microsoft.XMLHTTP" : "Msxml2.XMLHTTP";
		try {      
			xmlhttp = new ActiveXObject(control);
		} catch(e) {
			alert("You need to enable active scripting and activeX controls");
			DumpException(e);
		}
	} else {
		xmlhttp = new XMLHttpRequest();
	}
	xmlhttp.onreadystatechange = function() {handler();}
	if (url.indexOf("?") != -1){
		var urltemp = url + "&rand=" + UniqueNum();
	} else {
		var urltemp = url + "?rand=" + UniqueNum();
	}
	//alert(urltemp);
	xmlhttp.open('GET', urltemp, true);
	
	xmlhttp.send(null);	
}
//创建POST请求
function StartPOSTRequest(url, data, handler)
{
	xmlhttp = null;
	if (is_ie) {    
		var control = (is_ie5) ? "Microsoft.XMLHTTP" : "Msxml2.XMLHTTP";
		try {      
			xmlhttp = new ActiveXObject(control);
		} catch(e) {
			alert("You need to enable active scripting and activeX controls");
			DumpException(e);
		}
	} else {
		xmlhttp = new XMLHttpRequest();
	}
	xmlhttp.onreadystatechange = function() {handler();}
	xmlhttp.open('POST', url, true);
	if (typeof(xmlhttp.setRequestHeader) != "undefined") {
		xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; Charset=gbk');
	}
	xmlhttp.send(data);	
}
//处理方法
function GetObjValue(objName)
{
	if(document.getElementById){
		return eval('document.getElementById("' + objName + '")');
	}else{
		return eval('document.all.' + objName);
	}
}
function fetchObject(idname)
{
	if (document.getElementById) {
		return document.getElementById(idname);
	} else if (document.all) {
		return document.all[idname];
	} else if (document.layers) {
		return document.layers[idname];
	} else {
		return null;
	}
}
// function to emulate document.getElementsByTagName
function fetchTags(parentobj, tag)
{
	if (typeof parentobj.getElementsByTagName != 'undefined') {
		return parentobj.getElementsByTagName(tag);
	} else if (parentobj.all && parentobj.all.tags) {
		return parentobj.all.tags(tag);
	} else {
		return null;
	}
}
function fetchXmlValue(parentobj, tag)
{
	try{
		var tags = fetchTags(parentobj, tag);
		return tags[0].firstChild.nodeValue;
	} catch(e) {
		return null;
	}
}
function checkpic(this_form)
{	
	var fso,f; 
	if (this_form.upload_file.value.length > 0){
		var filesize = 1;  
		try {
			fso=new ActiveXObject("Scripting.FileSystemObject");  
   			f = fso.GetFile(this_form.upload_file.value); filesize = f.size;
		} catch (e1) {}
		if (filesize >200000 || filesize == 0) {
			_error_msg_show("文件不能为空，并且小于200K");
			return false;
		}
	}
	return true;
}
function checkForm(this_form)
{
	if ((this_form.answer.value == '') || (this_form.answer.value.length > 10000)) {
		_error_msg_show("回答的内容长度必须为1-1000个汉字！");
        this_form.answer.focus();
        return false;
	}
  	if (this_form.referto.value.length > 60) {
		_error_msg_show("对不起，参考文献内容长度不能大于30个汉字！");
        this_form.referto.focus();
        return false;
    }

	return true;
}
function showSending() 
{
	document.getElementById("sending").style.visibility="visible";
	document.getElementById("cover").style.visibility="visible";
}

function on(tdbg)
{
	tdbg.style.background="#fff17d";
}

function off(tdbg)
{
	tdbg.style.background="#EFF7FE";
}

function checkLogin()
{
if (document.login.U_Loginname.value=="")
{
_error_msg_show("会员名不能为空，请输入会员名！");
document.login.U_Loginname.focus();
return false;
}
if (document.login.U_Pass.value=="")
{
_error_msg_show("密码不能为空，请输入密码！");
document.login.U_Pass.focus();
return false;
}
return true;
}
function getCommentHtmlList(qid, aid)
{
	
	var strText;
	var fError = false;
	var obj = new Object();
	if (xmlhttp.readyState == 4){
		if (xmlhttp.status == 200){	
			strText = fetchXmlValue(xmlhttp.responseXML, 'body');	
			if (null == strText) {
				fError = true;
			} else {
				fetchObject("commentsecform" + qid + aid).innerHTML = strText;
			}
			Field.focus(fetchObject("txtComment" + qid + aid));
		}else{
			fError = true;
		}
	}
	if (fError) {
		fetchObject("commentsecform" + qid + aid).innerHTML = "";			
		fetchObject("comment" + qid + aid).style.display = "none";
	} else {
		fetchObject("comment" + qid + aid).scrollIntoView(true);
	}

}

function submitComment(qid, aid)
{ 
	var strText;
	var fnServerError = DisplayPostCommentError(qid, aid);
	if (xmlhttp.readyState == 4){
		if (xmlhttp.status == 200){
			var res = fetchXmlValue(xmlhttp.responseXML, 'result');
			strText = fetchXmlValue(xmlhttp.responseXML, 'body');
			if (null == strText) {
				fnServerError();
			} else {
				if (res.toLowerCase() == 'ok') {	
					fetchObject("commentsec" + qid + aid).innerHTML = strText;
					var t_total = fetchXmlValue(xmlhttp.responseXML, 'total');
					if (t_total > 0)
					{
						fetchObject("commenttotal" + qid + aid).innerHTML = "<a href=\"javascript:OpenSection('"+qid+"','"+aid+"');\" title=\"单击显示此项的评论。\">评论</a><b>"+t_total+"</b><img id=\"commentimg"+qid+aid+"\" src=\"http://www.unilawyers.com/images/question/img2_jj.gif\" border=\"0\" hspace=\"3\">";
					}
					else
					{
						fetchObject("commenttotal" + qid + aid).innerHTML = "评论";
					}					
				} else {
					fetchObject("Err" + qid + aid).innerHTML = "<table cellspacing=3 class='bvError'><tr><td width=15 valign=top><img src='http://www.unilawyers.com/images/question/error_icon.gif' width=15 height=16 border=0></td><td height=20 width=100%>"+ strText +"</td></tr></table>";
				}
			}
		}else{
			fnServerError();
		}
	}
}
	function OpenSection(qid, aid, pages)
{
	var sectionTitleObj = fetchObject("commentsecform" + qid + aid);
	if (pages >= 0)
	{
	}
	else
	{
		if (sectionTitleObj.style.display == "block")
		{
			Close(qid, aid, pages);
			return ;
		}
	}
	if (sectionTitleObj.style.display == 'none' || sectionTitleObj.style.display == '') {
		sectionTitleObj.style.display = "block";
		try{
			fetchObject("commentimg" + qid + aid).src = "http://www.unilawyers.com/images/question/img2_jj.gif";
		}catch(e){};
	}
	
	if (pages >= 0) {
		var strA = "questionid="+ qid;
		strA += "&answerid=" + aid;
		//strA += "&pages=" + pages;
		
		StartGETRequest('http://www.unilawyers.com/question/comment.php?'+strA, function() {getCommentHtmlList(qid, aid)});
	} else {		
		if(fetchObject("commentsecform" + qid + aid).innerHTML == "") {
			var strA = "questionid="+ qid;
			strA += "&answerid=" + aid;
			//var strA = "&replyid=" + aid;
			StartGETRequest('http://www.unilawyers.com/question/comment.php?'+strA, function() {getCommentHtmlList(qid, aid)});
		} 
	}
}

function Close(qid, aid, pages)
{
	fetchObject("commentsecform" + qid + aid).style.display = "none";
	try{
		fetchObject("commentimg" + qid + aid).src = "http://www.unilawyers.com/images/question/img2_jh.gif";
	}catch(e){};
	if (pages > 0) {
		fetchObject("commentsecform" + qid + aid).innerHTML = "";
	}
}

function CloseComment(qid, aid)
{
	if (fetchObject("err"+ qid + aid) != null) {
		fetchObject("Err"+qid + aid).innerHTML = "";
		fetchObject("txtComment"+qid + aid).value = "";
	}
	Close(qid, aid);
}

function DisplayPostCommentError(qid, aid)
{
	return function(){
		document.getElementById("Err"+qid+aid).innerHTML = "<table cellspacing=3 class='bvError'><tr><td width=15 valign=top><img src='http://www.unilawyers.com/images/question/error_icon.gif' width=15 height=16 border=0></td><td height=20 width=100%>服务器出现问题。请重试。</td></tr></table>";
	}
}
//没有办法获取radio的值，所以采用hidden来存贮
function setScore(score,qid,aid)
{
	var strClass = fetchObject("class"+qid +aid);
   	strClass.value = score;
}
function PostComment(qid, aid)
{//发表评论

	var strComment = fetchObject("txtComment"+qid + aid);
	var strClass = fetchObject("class"+qid +aid);
	var strError = "";
	nCommentLength = strComment.value.length;
	if (nCommentLength == 0 || nCommentLength > 200 ) {
		if (strError.length == 0) {
			strComment.focus();
		}
	}
	if (nCommentLength == 0) {
		strError += "评论不能为空。请输入评论，然后单击“发表评论”。"
	} else if (nCommentLength > 200) {
		strError += "您输入的评论太长。请缩短您的评论，然后单击“确定”。";
	}
	if (strError.length > 0)
	{
		document.getElementById("Err" + qid + aid).innerHTML = "<table cellspacing=0 border=0 class='bvError'>" + strError + "</table>";
		return false;
	}
/*	if (window.RegExp && window.encodeURIComponent) {
		var newStrComment = encodeURIComponent(strComment.value);
	} else {
		var newStrComment = strComment.value;
	}
*/

	var strA = "questionid="+ qid;
	strA += "&answerid=" + aid;
	//strA += "&do=add";
	//strA += "&answer_flag=5";
	if(strClass.value==1)
	{
	    strA += "&score=1";
	}
	else if(strClass.value==2)
	{
		strA += "&score=2";
	}
	else
	{
		strA += "&score=0";
	}
	strA += "&txtcomment="+strComment.value;
	StartGETRequest('http://www.unilawyers.com/question/request_comment_new.php?'+strA, function() {submitComment(qid, aid)});
}
function judgelen(judgeobj, dispobj, maxlen)
{
	if(judgeobj.value.length > maxlen)
	{
		judgeobj.value = judgeobj.value.substring(0,maxlen);
	}
	else
	{
		dispobj.value = maxlen - judgeobj.value.length;
	}
}
function checklawyercardmessge()
{
	var strText;
	var fError = false;
	var obj = new Object();
	if (xmlhttp.readyState == 4){
		if (xmlhttp.status == 200){	
			strText = fetchXmlValue(xmlhttp.responseXML,'result');	
			if (null == strText) {
				fError = true;
			} else {
				//alert(strText);
				if('ok' == strText)
				{
					fetchObject('lawyercard_notice').innerHTML = "<span style='color:blue'>&nbsp;已经查到您部分信息，请在后面步骤中，核对填写！</span>";
				}
				else
				{
					fError = true;
				}
			}
		}else{
			fError = true;
		}
	}
}
function checklawyercard(number)
{
    if(number.trim() == '')
	{
		fetchObject('lawyercard_notice').innerHTML = "<span style='color:red'>&nbsp;执业证号不能为空！</span>";
	}
	else
	{
		var pars = "number="+number;
		fetchObject('lawyercard_notice').innerHTML = "<span style='color:green'>&nbsp;该执业证号合法，可以使用！</span>";
	    StartGETRequest('http://www.unilawyers.com/member/getlawyer.php?'+pars,function(){checklawyercardmessge()});
	}
}
function updateschedule()
{
	
	var strText;
	var fError = false;
	var obj = new Object();
	if (xmlhttp.readyState == 4){
		if (xmlhttp.status == 200){	
			strText = fetchXmlValue(xmlhttp.responseXML,'result');
			if (null == strText) {
				fError = true;
			} else {
				if('ok' == strText)
				{
				    parent.update(fetchXmlValue(xmlhttp.responseXML,'subject'));
					parent.updatemenu(fetchXmlValue(xmlhttp.responseXML,'menu'));
					parent.settime(fetchXmlValue(xmlhttp.responseXML,'curtime'));
				}
				else
				{
					fError = true;
				}
			}
		}else{
			fError = true;
		}
	}	
}
function addschedule(subject,fmtime,totime,description)
{
	 var pars = 'subject='+subject;
	 pars += '&fmtime='+fmtime;
	 pars += '&totime='+totime;
	 pars += '&action=add';
	 pars += '&description='+description;
	 StartGETRequest('http://www.unilawyers.com/member/updateaction.php?'+pars,function(){ updateschedule(); });
}
function editschedule(id,subject,fmtime,totime,description)
{
	 var pars = 'subject='+subject;
	 pars += '&fmtime='+fmtime;
	 pars += '&totime='+totime;
	 pars += '&action=edit';
	 pars += '&description='+description;
	 pars += '&id='+id;
	 StartGETRequest('http://www.unilawyers.com/member/updateaction.php?'+pars,function(){ updateschedule(); });
}
function deleteschedule(id)
{
	 var pars = 'action=delete';
	 pars += '&id='+id;
	 StartGETRequest('http://www.unilawyers.com/member/updateaction.php?'+pars,function(){ updateschedule(); });
}
function viewschedule(type,searchtime)
{
	  if(type==undefined)
	     var type='';
	  if(searchtime == undefined)
	     var searchtime='';
	  var pars = 'action=view';
	  pars += '&type='+type;
	  pars += '&searchtime=' + searchtime;
	  StartGETRequest('http://www.unilawyers.com/member/updateaction.php?'+pars,function(){ updateschedule() });
}

