var refresh;

function Trim(){
	return this.replace(/\s+$|^\s+/g,"");
}
String.prototype.Trim=Trim;

function $(elementId) {
	if (document.getElementById) { 
		return document.getElementById(elementId); 
	} else if (document.all) { 
		return document.all[elementId]; 
	} else if (document.layers) { 
		return document.layers[elementId]; 
	} 
}

function $F(elementId){
	if($(elementId).value!=undefined)
		return $(elementId).value.Trim();
	else
		return "";
}

function XHR(){
	var xhr;
	try{
		xhr=new XMLHttpRequest();
	}catch(e){
    		var a=['MSXML2.XMLHTTP.5.0','MSXML2.XMLHTTP.4.0','MSXML2.XMLHTTP.3.0','MSXML2.XMLHTTP','MICROSOFT.XMLHTTP.1.0','MICROSOFT.XMLHTTP.1','MICROSOFT.XMLHTTP'];
    		for (var i=0;i<a.length;i++){
      			try{
        			xhr = new ActiveXObject(a[i]);
        			break;
      			}catch(e){}
    		}
  	}
	return xhr;
}

function checkForm(){
	if($F("content")==""){
		alert("请把带*号的项填写完整！");
		return false;
	}
	return true;
}

function sendJS() {	//发送留言
	var xhr=XHR();
	if(xhr&&checkForm()){
		$("submit").value="Sending...";
		$("submit").disabled=true;
		
		xhr.open("POST", "index.php", true);
		xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		var aIdArray=new Array("flag="+Math.random());
		var aUserArr=["content"];
		var argLen=aUserArr.length;
		for(i=0;i<argLen;i++){
			aIdArray[i+1]="&"+aUserArr[i]+"="+escape($F(aUserArr[i]));
		}
    	var data =aIdArray.join('');
		xhr.onreadystatechange=function(){
			if(xhr.readyState==4){
				if(xhr.status==200){
					if(xhr.responseText==1){
						alert("留言成功");
					}else if(xhr.responseText==-1){
						alert("系统已经关闭");
					}else{
						alert("系统错误");
					}

					$("submit").value="Send It";	//禁用发送表单，防止重复提交
					$("submit").disabled=false;

					clearForm();				//清除表单的值
				}else{
					alert("网络传输错误！请重试！");	
				}
			}	
		};
    	xhr.send(data);
  	}
}

function clearForm(){
	$F("content")="";
}

function getListAuto(id,time){
	if($F("s") == 0 || id == ""){
		return false;
	}else{
		refresh = setInterval("getList("+id+")",time);
	}
}

function clear(){
	if(refresh != "") clearInterval(refresh);
}

function displayMan(id){
	if($F("s") == 0 || id == ""){
		alert("该主题已经关闭!");
		return false;
	}
	$("mydiv").style.display = $("mydiv").style.display == ''?"none":"";
}

function getListMan(id){
	var time = $F("time");
	if(time < 2 || time == "" || time >30){
		alert("数值必须在2-30之间");
		return false;
	}
	clear();
	//$("mydiv").style.display = "none";
	alert("设置成功");
	getListAuto(id,time*1000);
}

function getList(id,sort,bool){
	if($F("s") == 0 || id == ""){
		alert("该主题已经关闭!");
		return false;
	}
	if(bool == 1) clear();
	if(sort == undefined) sort = "desc";
	$("vipsay").innerHTML="留言加载中....请稍后!";
	var xhr=XHR();
	xhr.open("GET", "index.php?action=vip&sort="+sort+"&titleid="+id+"&r="+Math.random(), true);
	xhr.onreadystatechange=function(){
		if(xhr.readyState==4){
			if(xhr.status==200){
				$("vipsay").innerHTML = xhr.responseText;
			}else{
				alert("获取留言失败！请刷新重试！");	
			}
		}
		
	}
	xhr.send(null);
}

function getUserList(id){
	if($F("s") == 0 || id == ""){
		alert("该主题已经关闭!");
		return false;
	}

	$("say").innerHTML="留言加载中....请稍后!";
	var xhr=XHR();
	xhr.open("GET", "index.php?action=user&titleid="+id+"&r="+Math.random(), true);
	xhr.onreadystatechange=function(){
		if(xhr.readyState==4){
			if(xhr.status==200){
				$("say").innerHTML = xhr.responseText;
			}else{
				alert("获取留言失败！请刷新重试！");	
			}
		}
		
	}
	xhr.send(null);
}

function openWin(id){
	//window.open("ask.php?id="+id,"提交问题","height=200, width=350, top=200px, left=200px, toolbar=no, menubar=no, scrollbars=no, resizable=no,location=no, status=no");
	var xhr=XHR();
	xhr.open("GET", "ask.php?id="+id, true);
	xhr.onreadystatechange=function(){
		if(xhr.readyState==4){
			if(xhr.status==200){
				if(xhr.responseText == -1)
				{
					alert("请先登陆后再发表留言");
					window.location = "http://login.jyb.cn/login.php?forward="+document.location;
					return;
				}
				window.open("ask.php?id="+id,"提交问题","height=200, width=350, top=200px, left=200px, toolbar=no, menubar=no, scrollbars=no, resizable=no,location=no, status=no");
			}else{
				alert("获取留言失败！请刷新重试！");	
			}
		}
		
	}
	xhr.send(null);
}

function refresh_code(){
	document.getElementById("code").src="code.php?id="+Math.random(10);
}

function sendTell(objForm,e){
	var code=objForm.code.value;
	var txt=objForm.content.value;
    if(!txt){
		alert("请输入留言");
		refresh_code();
		return false;
	}
	if(!code){        
		alert("请输入验证码");
		refresh_code();
		return false;
    }
    objForm.submit();
}