String.prototype.trim=function()
{
	return this.replace(/(^\s*)|(\s*$)/g, "");
}
function setcookie(name,value,hours)//两个参数，一个是cookie的名子，一个是值
{
	var exp = new Date();
	exp.setTime(exp.getTime()+hours*3600000);
	document.cookie = name+"="+ escape(value)+";expires="+exp.toGMTString();
}
function getcookie(name)//取cookies函数        
{
	var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
	if(arr != null) return unescape(arr[2]);
	return null;
}
function delcookie(name)//删除cookie
{
	var exp = new Date();
	exp.setTime(exp.getTime()-1);
	var cval=getCookie(name);
	if(cval!=null) document.cookie= name + "="+cval+";expires="+exp.toGMTString();
}

//common function
function showanswer(id,optA,optB,optC,optD,isReg)
{
	//var useroption=document.getElementsByName("useroption"+id);
	//var useroption=eval("document.all."+"useroption"+id);
	var j,a,answer='',useranswer='',isRight;
	j=0;isRight=0;
	a = document.getElementsByName('useroption'+id);
	if(optA==1) answer+='A';
	if(optB==1) answer+='B';
	if(optC==1) answer+='C';
	if(optD==1) answer+='D';
	for (var i=0;i<a.length ;i++){
		if (a[i].checked){
			j++;
			switch (i){
				case 0:
					useranswer+='A';break;
				case 1:
					useranswer+='B';break;
				case 2:
					useranswer+='C';break;
				case 3:
					useranswer+='D';break;
				default:
					break;
			}
		}
	}
	if (j==0){
		alert('答案选项不能为空！');
		return ;
	}else{
		if (useranswer!=answer){
			isRight = 0;
		}else{
			isRight = 1;
		}
		document.getElementById('submit'+id).disabled=true;
		var yourans=document.getElementById('yourans'+id);
		if(isRight==1) {
			yourans.innerHTML = useranswer+'&nbsp;(<font color=red>√</font>)';
		} else {
			if(isReg){
				yourans.innerHTML = useranswer+'&nbsp;(<font color=red>×</font>)&nbsp;&nbsp;&nbsp;&nbsp;<font color=red>您答错了！此题已自动存入您的<a href="history.php">[实时错题记录]</a>，以备今后复习时重点练习！请仔细阅读本题评析或讨论</font>';
			} else {
				yourans.innerHTML = useranswer+'&nbsp;(<font color=red>×</font>)&nbsp;&nbsp;&nbsp;&nbsp;<font color=red>您答错了！在[登录]或[注册]后您可以在<a href="history.php">[实时错题记录]</a>回顾错题</font>';
			}
		}
		var anstbl=document.getElementById('anstbl'+id);
		anstbl.style.display = '';
		if(isReg){
			answerForReg(id,isRight,useranswer);
		}
		return;
	}
}
function showanswerls(id)
{
	document.getElementById('submit'+id).disabled=true;
	var anstbl=document.getElementById('anstbl'+id);
	anstbl.style.display = '';
	return;
}
function editti(id)
{
	window.open ('editti.asp?id='+id, 'newwindow', 'height=480, width=720, top=0, left=0, toolbar=no, menubar=no, scrollbars=no, resizable=yes,location=no, status=no');
}

//ajax
function saveErrorlog(testid,errorAnswer,isright)
{
	var http_request;
	if (window.XMLHttpRequest){//Mozilla,Safari, ....
		http_request = new XMLHttpRequest();
	}
	else if(window.ActiveXObject){//IE
		http_request = new ActiveXObject('Microsoft.XMLHTTP');
	}
	var linkurl='ajax_log.asp?id='+testid+'&error='+errorAnswer+'&isright='+isright;
	http_request.open('GET',linkurl,true);
	http_request.send(null);
}

//==================
var gPointer;
var pXMLHttpReq = null;

function createXMLHttpRequest()
{
	if(pXMLHttpReq) return;
	if (window.XMLHttpRequest){//Mozilla...
		pXMLHttpReq = new XMLHttpRequest();
	}else if(window.ActiveXObject){//IE
		try{
			pXMLHttpReq = new ActiveXObject("Msxml2.XMLHTTP");
		}catch(e){
			try{
				pXMLHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
			}catch(e){}
		}
	}
}
var voted = new Array();
function quevote(qid,vid)
{
	if(voted[qid]==null) {
		voted[qid] = 1;
		var vote=document.getElementById('quevote'+qid+'_'+vid);
		vote.innerHTML = number = parseInt(vote.innerHTML)+1;

		createXMLHttpRequest();
		if(pXMLHttpReq){
			pXMLHttpReq.onreadystatechange = null;
			pXMLHttpReq.open("POST", "ajaxAction.php", true);
			pXMLHttpReq.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
			pXMLHttpReq.send("action=votedone&xqID=" + qid + "&vid=" + vid);
		}
	}
	return false;
}
function isResponseReady()
{
	if(pXMLHttpReq.readyState==4){
		if(pXMLHttpReq.status==200){
			return true;
		}
	}
	return false;
}

function addMyFav(id,thisPointer)
{
	thisPointer.disabled=true;
	gPointer = thisPointer;
	createXMLHttpRequest();
	if(pXMLHttpReq){
		pXMLHttpReq.onreadystatechange = addMyFavResponse;
		pXMLHttpReq.open("POST", "ajaxAction.php", true);
		pXMLHttpReq.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		pXMLHttpReq.send("action=favadd&xqID=" + id);
	}
}
function addMyFavResponse()
{
	//var xmldoc = originalRequest.responseXML;
	if(isResponseReady()){
		var pStatus = pXMLHttpReq.responseText.trim();
		switch (pStatus){
		case '1':
			gPointer.disabled = true;
			gPointer.value = '收藏成功！';
			break;
		case '2':
			gPointer.disabled = true;
			alert("您的收藏空间已满。如想收藏新题，需要删除旧题留出空间。");
			break;
		case '3':
			gPointer.disabled = true;
			gPointer.value = '已收藏过';
			break;
		case '4':
			alert("不能识别所提交的参数！");
			break;
		case '10':
			alert("未登录无法进行此操作！请先登录论坛。");
			break;
		default:
			alert("系统出现错误！错误号：" + pStatus);
			break;
		}
	}
}

function delMyQue(id,thisPointer,way)
{
	if(confirm("确定删除第"+id+"题吗？")){
		thisPointer.disabled=true;
		gPointer = thisPointer;
		createXMLHttpRequest();
		if(pXMLHttpReq){
			pXMLHttpReq.onreadystatechange = delMyFavResponse;
			pXMLHttpReq.open("POST", "ajaxAction.php", true);
			pXMLHttpReq.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
			pXMLHttpReq.send("action="+way+"&xqID="+id);
		}
	}
}
function delMyFavResponse()
{
	if(isResponseReady()){
		var pStatus = pXMLHttpReq.responseText.trim();
		switch (pStatus){
		case '1':
			gPointer.disabled = true;
			gPointer.value = '删除完毕！';
			break;
		case '2':
			gPointer.disabled = true;
			gPointer.value = '已删除过';
			break;
		case '10':
			alert("未登录无法进行此操作！请先登录论坛。");
			break;
		default:
			alert("系统出现错误！错误号：" + pStatus);
			break;
		}
	}
}

function answerForReg(id,isRight,useranswer)
{
	createXMLHttpRequest();
	if(pXMLHttpReq){
		pXMLHttpReq.onreadystatechange = null;
		pXMLHttpReq.open("POST", "ajaxAction.php", true);
		pXMLHttpReq.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		pXMLHttpReq.send("action=satdone&xqID=" + id + "&isRight=" + isRight + "&ans=" + useranswer);
	}
}
