// AJAX类
function AJAXRequest() {
	var xmlObj = false;
	var CBfunc,ObjSelf;
	ObjSelf=this;
	try { xmlObj=new XMLHttpRequest; }
	catch(e) {
		try { xmlObj=new ActiveXObject("MSXML2.XMLHTTP"); }
		catch(e2) {
			try { xmlObj=new ActiveXObject("Microsoft.XMLHTTP"); }
			catch(e3) { xmlObj=false; }
		}
	}
	if (!xmlObj) return false;
	this.method="POST";
	this.url;
	this.async=true;
	this.content="";
	this.callback=function(cbobj) {return;}
	this.send=function() {
		if(!this.method||!this.url||!this.async) return false;
		xmlObj.open (this.method, this.url, this.async);
		if(this.method=="POST") xmlObj.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		xmlObj.onreadystatechange=function() {
			if(xmlObj.readyState==4) {
				if(xmlObj.status==200) {
					ObjSelf.callback(xmlObj);
				}
			}
		}
		if(this.method=="POST") xmlObj.send(this.content);
		else xmlObj.send(null);
	}
}
//by maxz 
function ajax_get(url,div)
{
	var ajax = new myAjax(); 
	var ajax_o = new Ajax_callback_object();
	ajax_o.div = div;
	ajax.get(url,ajax_o);
	return false;
}

function ajax_post(url,q,div)
{
	//var q = generate_post_query(form);
	var ajax = new myAjax(); 
	var ajax_o = new Ajax_callback_object();
	ajax_o.div = div;
	ajax.post(url,q,ajax_o);
}

function Ajax_callback_object() {
	
	this.div = '';
	this.callback = show_div;
	
	function show_div(response){
		document.getElementById(this.div).innerHTML=response;
	}
}

function generate_post_query(f){
	var q='';
	for(var i=0;i<f.elements.length;i++){
		if(i!=0);q+='&';
		q+=f.elements[i].name+'='+f.elements[i].value;
	}
	return q;
}

function create_xmlhttp() { 
	if(window.XMLHttpRequest) { 
	// Non-IE browsers 
		req = new XMLHttpRequest(); 
	} else if(window.ActiveXObject) { 
	// IE 
		req = new ActiveXObject("Microsoft.XMLHTTP"); 
	} 
	return req; 
} 
	
	
	
	
// ajax class 
function myAjax() { 
	this.xmlhttp = create_xmlhttp(); 
	
	this.get = ajax_get; 
	this.post = ajax_post; 
	
	this.response = ''; 
	this.callback_object = ''; 
	
	function ajax_get(url,callback_object) { 
	
		this.callback_object = callback_object; 
		
		var this_obj = this; 

		this.xmlhttp.onreadystatechange = function() { 
			if(this_obj.xmlhttp.readyState == 4) { 
				if(this_obj.xmlhttp.status == 200) { 
				     //alert(this_obj.xmlhttp.getAllResponseHeaders());
					this_obj.callback_object.callback(this_obj.xmlhttp.responseText);
					
				} else { 
					alert('There was a problem with the request. ('+this_obj.xmlhttp.status+')'); 
				} 
			} 
		} 
		this.xmlhttp.open("GET", url, true); 
		this.xmlhttp.send(null);
		
		return true; 
	}
	
	function ajax_post(url, query, callback_object) { 
	
		this.callback_object = callback_object; 
	
		var this_obj = this; 
	
		this.xmlhttp.onreadystatechange = function() { 
			if(this_obj.xmlhttp.readyState == 4) { 
				if(this_obj.xmlhttp.status == 200) { 
					this_obj.callback_object.callback(this_obj.xmlhttp.responseText);
				} else { 
					alert('There was a problem with the request. (' + this_obj.xmlhttp.status + ')'); 
				} 
			} 
		} 
	
		this.xmlhttp.open('POST', url); 
		this.xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); 
		this.xmlhttp.send(query); 
		return true; 
	} 
} 



function showMsg(text,obj){
	document.getElementById("Msg").innerHTML=text;
	if(obj!="")
		document.getElementById(obj).focus();
}





//-------------------------------by qiushi-------------------------


var xmlHttp = false;
var Vertifyinput=0;

function Vertify_Comment_Time()
{
	try 
	{
	  xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
	} 
	catch (e) 
	{
	  try 
	  {
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	  } 
	  catch (e2) 
	  {
		xmlHttp = false;
	  }
	}
	if (!xmlHttp && typeof XMLHttpRequest != 'undefined') 
	{
	  xmlHttp = new XMLHttpRequest();
	}
	
	var url ='/'+sitepath+'comment.asp?action=vertify';
	xmlHttp.open("GET", url, true);
	xmlHttp.onreadystatechange = return_vertigy;
	xmlHttp.send(null); 
	
}


function return_vertigy()
{
	if (xmlHttp.readyState == 4) 
   {
		response = xmlHttp.responseText;
		//document.getElementById('vertifyinput').value=response
		if(response=='yes')
		{
			Vertifyinput=1;	
		}
		else if(response=='no')
		{
			Vertifyinput=2;	
		}
		
		if (Vertifyinput==1)
		{
			showMsg('<b style=color:#FF0000>对不起，你两次留言的间隔为20秒</b>','comment_content');
			return false;	
		}
		else if(Vertifyinput==2)
		{		
			var user = document.getElementById('user').value;
			var zt_id = document.getElementById('zt_id').value;
			var comment_content = document.getElementById('comment_content').value;
			var content_m = document.getElementById('maxzs').value;
			var content_n = document.getElementById('maxzss').value;
			
			if(user==""){
				showMsg('<b style=color:#FF0000>姓名不能为空!</b>','user');
				return false;
			}
			if (user.length>content_m){
				showMsg('<b style=color:#FF0000>您的大名字数不得大于'+content_m+'个字符</b>','comment_content');
				return false;
			}
			if(comment_content==""){
				showMsg('<b style=color:#FF0000>评论内容不能为空</b>','comment_content');
				return false;
			}
			if (comment_content.length>content_n){
				showMsg('<b style=color:#FF0000>评论内容不得大于'+content_n+'个字符</b>','comment_content');
				return false;
			}
			
			document.getElementById('post_btn').disabled=true;
			document.getElementById('post_btn').value="正在发表";
			url = '/'+sitepath+'comment.asp?action=add';
			//alert(encodeURIComponent(escape(comment_content)))
			var q;	
			q='user='+encodeURIComponent(escape(user))+'&content='+encodeURIComponent(escape(comment_content))+'&zt_id='+zt_id;
			ajax_post (url,q,'comment');
			
		}	
	}	  
}


function textCounter(sElement,maxlimit) { 
	field = sElement;
	if (field.value.length > maxlimit){
		field.value = field.value.substring(0, maxlimit); 		
	} else{
		document.getElementById("remLen").value = maxlimit - field.value.length;
	}
	if (field.value.length == maxlimit){
		showerr("请将字符限制在500个以内");
	}
}




//-------------------------------by qiushi-------------------------




//function submit_comment(){
	
	

	
//}







function page(lid,pid){
document.getElementById("comment_load").innerHTML="<div style='padding:30px;text-align:center'><img src='/"+sitepath+"js/loading2.gif'/>数据加载中...</div>";
ajax_get("/"+sitepath+"comment.asp?zt_id="+lid+"&page="+pid+"","comment");
}


function showcontent(txt){
	document.getElementById('comment_content').value=txt;
}

ie = (document.all)? true:false 
if (ie)
{ 
	function ctlent(eventobject)
	{
		if(event.ctrlKey && window.event.keyCode==13)
		{
			Vertify_Comment_Time()
		}
	} 
}

function insertTags(tagOpen, tagClose, sampleText) {
    var txtarea = document.getElementById("comment_content");
	// IE
	if(document.selection) {
		var theSelection = document.selection.createRange().text;
		if(!theSelection) { theSelection=sampleText;}
		txtarea.focus();
		if(theSelection.charAt(theSelection.length - 1) == " "){
			theSelection = theSelection.substring(0, theSelection.length - 1);
			document.selection.createRange().text = tagOpen + theSelection + tagClose + " ";
		} else {
			document.selection.createRange().text = tagOpen + theSelection + tagClose;
		}
	// Mozilla
	} else if(txtarea.selectionStart || txtarea.selectionStart == '0') {
 		var startPos = txtarea.selectionStart;
		var endPos = txtarea.selectionEnd;
		var myText = (txtarea.value).substring(startPos, endPos);
		if(!myText) { myText=sampleText;}
		if(myText.charAt(myText.length - 1) == " "){ // exclude ending space char, if any
			subst = tagOpen + myText.substring(0, (myText.length - 1)) + tagClose + " "; 
		} else {
			subst = tagOpen + myText + tagClose; 
		}
		txtarea.value = txtarea.value.substring(0, startPos) + subst + txtarea.value.substring(endPos, txtarea.value.length);
		txtarea.focus();
		var cPos=startPos+(tagOpen.length+myText.length+tagClose.length);
		txtarea.selectionStart=cPos;
		txtarea.selectionEnd=cPos;
	// All others
	} else {
		tagOpen=tagOpen.replace(/\n/g,"");
		tagClose=tagClose.replace(/\n/g,"");
		document.infoform.infobox.value=tagOpen+sampleText+tagClose;
		txtarea.focus();
	}
	if (txtarea.createTextRange) txtarea.caretPos = document.selection.createRange().duplicate();
}
