
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();
}
function submit_comment(){
	var user = document.getElementById('user').value;
	var YW_VID = document.getElementById('YW_VID').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>姓名不能为空!</b>','user');
		return false;
	}
	if (user.length>content_m){
		showMsg('<b>您的大名字数不得大于'+content_m+'个字符</b>','comment_content');
		return false;
	}
	if(comment_content==""){
		showMsg('<b>评论内容不能为空</b>','comment_content');
		return false;
	}
	if (comment_content.length>content_n){
		showMsg('<b>评论内容不得大于'+content_n+'个字符</b>','comment_content');
		return false;
	}
	
	document.getElementById('post_btn').disabled=true;
	document.getElementById('post_btn').value="正在发表";
	url = ''+sitepath+'JS/ConTent.ASP?action=add';
	var q;	
	q='user='+encodeURIComponent(escape(user))+'&content='+encodeURIComponent(escape(comment_content))+'&YW_VID='+YW_VID;
	ajax_post (url,q,'comment');

}
function page(lid,pid){
document.getElementById("comment_load").innerHTML="<div style='padding:30px;text-align:center'><img src=''+sitepath+'imgs/Loading.gif'/>数据加载中...</div>";
ajax_get(""+sitepath+"JS/ConTent.ASP?YW_VID="+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)
		{
			submit_comment();
		}
	} 
}

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();
}
